-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathxregVTK3DPlotter.h
350 lines (254 loc) · 11.7 KB
/
xregVTK3DPlotter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* MIT License
*
* Copyright (c) 2020 Robert Grupp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef XREGVTK3DPLOTTER_H_
#define XREGVTK3DPLOTTER_H_
#include <vtkNew.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkCubeAxesActor.h>
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkScalarBarActor.h>
#include <itkImage.h>
#include <opencv2/core/core.hpp>
#include "xregCommon.h"
class vtkPolyData;
namespace xreg
{
struct CameraModel;
struct TriMesh;
struct AASlab3;
/// \brief Helper class for common plotting operations with VTK
class VTK3DPlotter
{
public:
using RGBVec = Pt3;
using RGBAVec = Pt4;
using Scalar = CoordScalar;
using vtkImageDataPtr = vtkSmartPointer<vtkImageData>;
/// \brief Constructor - does some minor initialization.
VTK3DPlotter();
// no copying
VTK3DPlotter(const VTK3DPlotter&) = delete;
VTK3DPlotter& operator=(const VTK3DPlotter&) = delete;
/// \brief Shows the window and starts the interator - blocking.
void show();
/// \brief Sets the title string of the window
void set_title(const std::string& s);
/// \brief Sets the window size (in pixels)
void set_size(const size_type num_rows, const size_type num_cols);
void set_full_screen(const bool fs);
/// \brief Sets whether axes graphics should be displayed.
void set_show_axes(const bool show_axes);
/// \brief Plots a collection of points.
void add_pts(const Pt3List& pts);
/// \brief Plots a collection of points.
void add_pts(const Pt3List& pts, const Scalar r, const Scalar g, const Scalar b);
/// \brief Plots a collection of points.
void add_pts(const Pt3List& pts, const RGBVec& rgb);
/// \brief Plots a collection of points.
void add_pts(const Pt3List& pts, const RGBAVec& rgba);
/// \brief Plots a collection of points.
void add_pts(const Pt3List& pts, const Scalar r, const Scalar g, const Scalar b, const Scalar a);
/// \brief Plots a sphere with center and radius
void add_sphere(const Pt3& center, const Scalar radius,
const Scalar r, const Scalar g, const Scalar b, const Scalar a = 1);
/// \brief Plots a sphere with center and radius
void add_sphere(const Pt3& center, const Scalar radius, const RGBVec& rgb);
/// \brief Plots a sphere with center and radius
void add_sphere(const Pt3& center, const Scalar radius, const RGBAVec& rgba);
/// \brief Plots a line segment connecting two points
void add_line(const Pt3& pt1, const Pt3& pt2,
const Scalar r, const Scalar g, const Scalar b, const Scalar a = 1,
const Scalar width = 1.0);
/// \brief Plots a line segment connecting two points
void add_line(const Pt3& pt1, const Pt3& pt2, const RGBVec& rgb, const Scalar width = 1.0);
/// \brief Plots a line segment connecting two points
void add_line(const Pt3& pt1, const Pt3& pt2, const RGBAVec& rgba, const Scalar width = 1.0);
/// \brief Draws an arrow
void add_arrow(const Pt3& base_pt, const Pt3& tip_pt,
const Scalar r, const Scalar g, const Scalar b,
const Scalar a = 1);
/// \brief Draws an arrow
void add_arrow(const Pt3& base_pt, const Pt3& tip_pt, const RGBVec& rgb);
/// \brief Draws an arrow
void add_arrow(const Pt3& base_pt, const Pt3& tip_pt, const RGBAVec& rgba);
/// \brief Draws an arrow with default color.
void add_arrow(const Pt3& base_pt, const Pt3& tip_pt);
/// \brief Plots a plane surface.
///
/// The plane is defined by an origin, o, and two points, p1, p2. The vector
/// p1 - o defines the x-axis and extent along the x direction, likewise the
/// vector p2 - o defines the y-axis and extent along the y direction, and
/// o is the origin. o, p1, p2 define three corners of the plane and the
/// fourth is inferred.
void add_plane(const Pt3& origin_pt, const Pt3& pt1, const Pt3& pt2,
const Scalar r, const Scalar g, const Scalar b, const Scalar a = 1);
/// \brief Plots a plane surface.
void add_plane(const Pt3& origin_pt, const Pt3& pt1, const Pt3& pt2,
const RGBVec& rgb);
/// \brief Plots a plane surface.
void add_plane(const Pt3& origin_pt, const Pt3& pt1, const Pt3& pt2,
const RGBAVec& rgba);
/// \brief Plot a circle
void add_circle(const Pt3& center_pt, const Scalar radius,
const Pt3& normal_vec);
/// \brief Plot a circle
void add_circle(const Pt3& center_pt, const Scalar radius,
const Pt3& normal_vec,
const RGBVec& rgb);
/// \brief Plot a circle
void add_circle(const Pt3& center_pt, const Scalar radius,
const Pt3& normal_vec,
const RGBAVec& rgba);
/// \brief Plot a circle
void add_circle(const Pt3& center_pt, const Scalar radius,
const Pt3& normal_vec,
const Scalar r, const Scalar g, const Scalar b);
/// \brief Plot a circle
void add_circle(const Pt3& center_pt, const Scalar radius,
const Pt3& normal_vec,
const Scalar r, const Scalar g, const Scalar b,
const Scalar a);
/// \brief Plots an axis-aligned slab that has been re-oriented
void add_aa_slab(const AASlab3& slab, const FrameTransform& slab_pose);
/// \brief Plots an axis-aligned slab that has been re-oriented
void add_aa_slab(const AASlab3& slab, const FrameTransform& slab_pose,
const RGBVec& rgb);
/// \brief Plots an axis-aligned slab that has been re-oriented
void add_aa_slab(const AASlab3& slab, const FrameTransform& slab_pose,
const Scalar r, const Scalar g, const Scalar b);
/// \brief Plots an axis-aligned slab that has been re-oriented
void add_aa_slab(const AASlab3& slab, const FrameTransform& slab_pose,
const RGBAVec& rgba);
/// \brief Plots an axis-aligned slab that has been re-oriented
void add_aa_slab(const AASlab3& slab, const FrameTransform& slab_pose,
const Scalar r, const Scalar g, const Scalar b, const Scalar a);
/// \brief Plots a 2D image plane with respect to the camera model "world"
/// coordinates.
void add_2D_image(itk::Image<unsigned char,2>* img, const CameraModel& cam);
/// \brief Plots a 2D image plane with respect to the camera model "world"
/// coordinates.
void add_2D_image(itk::Image<unsigned short,2>* img, const CameraModel& cam);
/// \brief Plots a 2D image plane with respect to the camera model "world"
/// coordinates.
void add_2D_image(itk::Image<float,2>* img, const CameraModel& cam);
/// \brief Plots a surface defined with a triangular surface mesh.
void add_mesh(const TriMesh& mesh, const Scalar r, const Scalar g,
const Scalar b, const Scalar a = 1);
/// \brief Plots a surface defined with a triangular surface mesh.
void add_mesh(const TriMesh& mesh, const RGBVec& rgb);
/// \brief Plots a surface defined with a triangular surface mesh.
void add_mesh(const TriMesh& mesh, const RGBAVec& rgba);
/// \brief Plots a surface defined with a triangular surface mesh.
///
/// Uses default color.
void add_mesh(const TriMesh& mesh);
void add_mesh_w_scalar_map(const TriMesh& mesh, const CoordScalarList& scalars,
const AnyParamMap& scalar_map_params = AnyParamMap());
/// \brief Plots an existing VTK PolyData object.
void add_polydata(vtkPolyData* polydata, const Scalar r, const Scalar g,
const Scalar b, const Scalar a = 1);
/// \brief Plots an existing VTK PolyData object.
void add_polydata(vtkPolyData* polydata, const RGBVec& rgb);
/// \brief Plots an existing VTK PolyData object.
void add_polydata(vtkPolyData* polydata, const RGBAVec& rgba);
/// \brief Plots an existing VTK PolyData object.
///
/// Uses default color.
void add_polydata(vtkPolyData* polydata);
/// \brief Retrieve the renderer object used.
///
/// Useful for using other VTK routines to update the display.
vtkRenderer* renderer();
/// \brief Sets the flag to use, or not use, a custom camera pose
///
/// \see set_cam_pose
/// NOTE: The camera coordinate frame is changed according to the geometry
/// defined by the xreg::CameraModel class.
void set_use_custom_cam(const bool use_custom_cam);
/// \brief Sets a custom camera pose
///
/// NOTE: The camera coordinate frame is changed according to the geometry
/// defined by the xreg::CameraModel class.
void set_cam_pose(const FrameTransform& cam_pose, const bool use_custom_cam = true);
void set_custom_az_el(const Scalar az_deg, const Scalar el_deg);
void clear_custom_az_el();
vtkCamera* vtk_cam();
void set_interaction(const bool inter_on);
/// \brief Render, without displaying to the user, and write to a PNG file.
///
/// Similar to calling show() and manually capturing a screenshot of the
// initial render.
void draw_to_png(const std::string& dst_path);
/// \brief Render, without displaying to the user, and return as a VTK Image.
vtkImageDataPtr draw_to_vtk_img_data();
/// \brief Render, without displaying to the user, and return as an OpenCV
/// mat object in BGR format.
///
/// The user can optionally disable the up/down flipping (VTK places the
/// zero index in the lower left, compared to OpenCV convention of
/// upper left).
cv::Mat draw_to_ocv(const bool flip_ud = true);
void set_bg_color(const RGBVec& bg);
void set_bg_color(const Scalar& r, const Scalar& g, const Scalar& b);
/// \brief Default background color
static RGBVec DefaultBGColor();
/// \brief Default mesh color RGB
static RGBVec DefaultMeshColor();
/// \brief Default mesh color RGB with alpha
static RGBAVec DefaultMeshColorAlpha();
/// \brief A reasonable color to use for bone surfaces, RGB
static RGBVec BoneColor();
/// \brief A reasonable color to use for bone surfaces, RGB with alpha
static RGBAVec BoneColorAlpha();
static std::string KeyUsageStr();
private:
enum { kDEFAULT_WIN_NUM_ROWS = 1024 };
enum { kDEFAULT_WIN_NUM_COLS = 1024 };
void setup_axes();
bool render_offscreen_ = false;
bool use_custom_cam_ = false;
bool inter_on_ = true;
bool win_title_str_set_ = false;
std::string win_title_str_;
// These values are set to default in the ctor.
size_type win_num_rows_;
size_type win_num_cols_;
bool do_full_screen_ = false;
vtkNew<vtkRenderWindowInteractor> iren_;
vtkNew<vtkRenderer> ren_;
vtkNew<vtkRenderWindow> ren_win_;
vtkNew<vtkCubeAxesActor> cube_axes_actor_;
vtkSmartPointer<vtkScalarBarActor> scalar_bar_;
FrameTransform cam_pose_ = FrameTransform::Identity();
RGBVec bg_color_ = DefaultBGColor();
bool use_custom_az_el_ = false;
Scalar custom_az_deg_ = 0;
Scalar custom_el_deg_ = 0;
static unsigned long& WindowCount();
};
} // xreg
#endif