10 #include <pcl/console/print.h>
11 #include <pcl/correspondence.h>
12 #include <pcl/geometry/planar_polygon.h>
13 #include <pcl/memory.h>
14 #include <pcl/pcl_macros.h>
15 #include <pcl/point_types.h>
16 #include <pcl/visualization/interactor_style.h>
19 #include <vtkInteractorStyleImage.h>
20 #include <vtkRenderWindowInteractor.h>
21 #include <vtkVersion.h>
23 #include <boost/shared_array.hpp>
26 class vtkContextActor;
31 namespace visualization {
32 using Vector3ub = Eigen::Array<unsigned char, 3, 1>;
50 void OnLeftButtonDown()
override;
52 void OnChar()
override;
54 void adjustCamera(vtkImageData*
image, vtkRenderer* ren);
56 void adjustCamera(vtkRenderer* ren);
85 using Ptr = shared_ptr<ImageViewer>;
86 using ConstPtr = shared_ptr<const ImageViewer>;
101 interactor_->SetInteractorStyle(style);
111 void showMonoImage(
const unsigned char*
data,
114 const std::string& layer_id =
"mono_image",
115 double opacity = 1.0);
123 void addMonoImage(
const unsigned char*
data,
126 const std::string& layer_id =
"mono_image",
127 double opacity = 1.0);
135 const pcl::PointCloud<pcl::Intensity>::ConstPtr& cloud,
136 const std::string& layer_id =
"mono_image",
137 double opacity = 1.0) {
138 return (showMonoImage(*cloud, layer_id, opacity));
148 const pcl::PointCloud<pcl::Intensity>::ConstPtr& cloud,
149 const std::string& layer_id =
"mono_image",
150 double opacity = 1.0) {
151 return (addMonoImage(*cloud, layer_id, opacity));
159 void showMonoImage(
const pcl::PointCloud<pcl::Intensity>& cloud,
160 const std::string& layer_id =
"mono_image",
161 double opacity = 1.0);
168 void addMonoImage(
const pcl::PointCloud<pcl::Intensity>& cloud,
169 const std::string& layer_id =
"mono_image",
170 double opacity = 1.0);
178 const pcl::PointCloud<pcl::Intensity8u>::ConstPtr& cloud,
179 const std::string& layer_id =
"mono_image",
180 double opacity = 1.0) {
181 return (showMonoImage(*cloud, layer_id, opacity));
191 const pcl::PointCloud<pcl::Intensity8u>::ConstPtr& cloud,
192 const std::string& layer_id =
"mono_image",
193 double opacity = 1.0) {
194 return (addMonoImage(*cloud, layer_id, opacity));
202 void showMonoImage(
const pcl::PointCloud<pcl::Intensity8u>& cloud,
203 const std::string& layer_id =
"mono_image",
204 double opacity = 1.0);
211 void addMonoImage(
const pcl::PointCloud<pcl::Intensity8u>& cloud,
212 const std::string& layer_id =
"mono_image",
213 double opacity = 1.0);
222 void showRGBImage(
const unsigned char*
data,
225 const std::string& layer_id =
"rgb_image",
226 double opacity = 1.0);
236 void addRGBImage(
const unsigned char*
data,
239 const std::string& layer_id =
"rgb_image",
240 double opacity = 1.0,
241 bool autoresize =
true);
248 template <
typename T>
249 inline void showRGBImage(
const typename pcl::PointCloud<T>::ConstPtr& cloud,
250 const std::string& layer_id =
"rgb_image",
251 double opacity = 1.0) {
252 return (showRGBImage<T>(*cloud, layer_id, opacity));
260 template <
typename T>
261 inline void addRGBImage(
const typename pcl::PointCloud<T>::ConstPtr& cloud,
262 const std::string& layer_id =
"rgb_image",
263 double opacity = 1.0) {
264 return (addRGBImage<T>(*cloud, layer_id, opacity));
272 template <
typename T>
273 void showRGBImage(
const pcl::PointCloud<T>& cloud,
274 const std::string& layer_id =
"rgb_image",
275 double opacity = 1.0);
282 template <
typename T>
283 void addRGBImage(
const pcl::PointCloud<T>& cloud,
284 const std::string& layer_id =
"rgb_image",
285 double opacity = 1.0);
298 void showFloatImage(
const float*
data,
301 float min_value = std::numeric_limits<float>::min(),
302 float max_value = std::numeric_limits<float>::max(),
303 bool grayscale =
false,
304 const std::string& layer_id =
"float_image",
305 double opacity = 1.0);
317 void addFloatImage(
const float*
data,
320 float min_value = std::numeric_limits<float>::min(),
321 float max_value = std::numeric_limits<float>::max(),
322 bool grayscale =
false,
323 const std::string& layer_id =
"float_image",
324 double opacity = 1.0);
336 void showShortImage(
const unsigned short* short_image,
339 unsigned short min_value =
340 std::numeric_limits<unsigned short>::min(),
341 unsigned short max_value =
342 std::numeric_limits<unsigned short>::max(),
343 bool grayscale =
false,
344 const std::string& layer_id =
"short_image",
345 double opacity = 1.0);
358 void addShortImage(
const unsigned short* short_image,
361 unsigned short min_value =
362 std::numeric_limits<unsigned short>::min(),
363 unsigned short max_value =
364 std::numeric_limits<unsigned short>::max(),
365 bool grayscale =
false,
366 const std::string& layer_id =
"short_image",
367 double opacity = 1.0);
376 void showAngleImage(
const float*
data,
379 const std::string& layer_id =
"angle_image",
380 double opacity = 1.0);
388 void addAngleImage(
const float*
data,
391 const std::string& layer_id =
"angle_image",
392 double opacity = 1.0);
401 void showHalfAngleImage(
const float*
data,
404 const std::string& layer_id =
"half_angle_image",
405 double opacity = 1.0);
413 void addHalfAngleImage(
const float*
data,
416 const std::string& layer_id =
"half_angle_image",
417 double opacity = 1.0);
428 void markPoint(std::size_t u,
433 const std::string& layer_id =
"points",
434 double opacity = 1.0);
444 void markPoints(
const std::vector<int>&
uv,
448 const std::string& layer_id =
"markers",
449 double opacity = 1.0);
459 void markPoints(
const std::vector<float>&
uv,
463 const std::string& layer_id =
"markers",
464 double opacity = 1.0);
469 void setWindowTitle(
const std::string&
name);
480 void spinOnce(
int time = 1,
bool force_redraw =
true);
489 void (*
callback)(
const pcl::visualization::KeyboardEvent&,
void*),
490 void* cookie =
nullptr) {
491 return (registerKeyboardCallback(
492 [=](
const pcl::visualization::KeyboardEvent&
e) {
493 (*callback)(
e, cookie);
504 template <
typename T>
506 void (T::*
callback)(
const pcl::visualization::KeyboardEvent&,
509 void* cookie =
nullptr) {
510 return (registerKeyboardCallback(
511 [=, &instance](
const pcl::visualization::KeyboardEvent&
e) {
521 boost::signals2::connection registerKeyboardCallback(
522 std::function<
void(
const pcl::visualization::KeyboardEvent&)> cb);
531 void (*
callback)(
const pcl::visualization::MouseEvent&,
void*),
532 void* cookie =
nullptr) {
533 return (registerMouseCallback(
534 [=](
const pcl::visualization::MouseEvent&
e) {
535 (*callback)(
e, cookie);
546 template <
typename T>
548 void (T::*
callback)(
const pcl::visualization::MouseEvent&,
void*),
550 void* cookie =
nullptr) {
551 return (registerMouseCallback(
552 [=, &instance](
const pcl::visualization::MouseEvent&
e) {
562 boost::signals2::connection registerMouseCallback(
563 std::function<
void(
const pcl::visualization::MouseEvent&)> cb);
569 void setPosition(
int x,
int y);
575 void setSize(
int xw,
int yw);
587 interactor_->TerminateApp();
598 bool addCircle(
unsigned int x,
601 const std::string& layer_id =
"circles",
602 double opacity = 1.0);
616 bool addCircle(
unsigned int x,
622 const std::string& layer_id =
"circles",
623 double opacity = 1.0);
632 bool addRectangle(
const pcl::PointXY& min_pt,
633 const pcl::PointXY& max_pt,
634 const std::string& layer_id =
"rectangles",
635 double opacity = 1.0);
648 bool addRectangle(
const pcl::PointXY& min_pt,
649 const pcl::PointXY& max_pt,
653 const std::string& layer_id =
"rectangles",
654 double opacity = 1.0);
665 bool addRectangle(
unsigned int x_min,
669 const std::string& layer_id =
"rectangles",
670 double opacity = 1.0);
685 bool addRectangle(
unsigned int x_min,
692 const std::string& layer_id =
"rectangles",
693 double opacity = 1.0);
702 template <
typename T>
703 bool addRectangle(
const typename pcl::PointCloud<T>::ConstPtr&
image,
706 const std::string& layer_id =
"rectangles",
707 double opacity = 1.0);
720 template <
typename T>
721 bool addRectangle(
const typename pcl::PointCloud<T>::ConstPtr&
image,
727 const std::string& layer_id =
"rectangles",
728 double opacity = 1.0);
740 template <
typename T>
741 bool addRectangle(
const typename pcl::PointCloud<T>::ConstPtr&
image,
742 const pcl::PointCloud<T>& mask,
746 const std::string& layer_id =
"rectangles",
747 double opacity = 1.0);
756 template <
typename T>
757 bool addRectangle(
const typename pcl::PointCloud<T>::ConstPtr&
image,
758 const pcl::PointCloud<T>& mask,
759 const std::string& layer_id =
"image_mask",
760 double opacity = 1.0);
771 bool addFilledRectangle(
unsigned int x_min,
775 const std::string& layer_id =
"boxes",
776 double opacity = 0.5);
791 bool addFilledRectangle(
unsigned int x_min,
798 const std::string& layer_id =
"boxes",
799 double opacity = 0.5);
814 bool addLine(
unsigned int x_min,
821 const std::string& layer_id =
"line",
822 double opacity = 1.0);
833 bool addLine(
unsigned int x_min,
837 const std::string& layer_id =
"line",
838 double opacity = 1.0);
852 bool addText(
unsigned int x,
854 const std::string& text,
858 const std::string& layer_id =
"line",
859 double opacity = 1.0);
869 bool addText(
unsigned int x,
871 const std::string& text,
872 const std::string& layer_id =
"line",
873 double opacity = 1.0);
885 template <
typename T>
886 bool addMask(
const typename pcl::PointCloud<T>::ConstPtr&
image,
887 const pcl::PointCloud<T>& mask,
891 const std::string& layer_id =
"image_mask",
892 double opacity = 0.5);
901 template <
typename T>
902 bool addMask(
const typename pcl::PointCloud<T>::ConstPtr&
image,
903 const pcl::PointCloud<T>& mask,
904 const std::string& layer_id =
"image_mask",
905 double opacity = 0.5);
919 template <
typename T>
920 bool addPlanarPolygon(
const typename pcl::PointCloud<T>::ConstPtr&
image,
921 const pcl::PlanarPolygon<T>&
polygon,
925 const std::string& layer_id =
"planar_polygon",
926 double opacity = 1.0);
936 template <
typename T>
937 bool addPlanarPolygon(
const typename pcl::PointCloud<T>::ConstPtr&
image,
938 const pcl::PlanarPolygon<T>&
polygon,
939 const std::string& layer_id =
"planar_polygon",
940 double opacity = 1.0);
949 bool addLayer(
const std::string& layer_id,
952 double opacity = 0.5);
957 void removeLayer(
const std::string& layer_id);
966 template <
typename Po
intT>
967 bool showCorrespondences(
const pcl::PointCloud<PointT>& source_img,
968 const pcl::PointCloud<PointT>& target_img,
971 const std::string& layer_id =
"correspondences");
984 void convertIntensityCloudToUChar(
985 const pcl::PointCloud<pcl::Intensity>& cloud,
986 boost::shared_array<unsigned char>
data);
995 void convertIntensityCloud8uToUChar(
996 const pcl::PointCloud<pcl::Intensity8u>& cloud,
997 boost::shared_array<unsigned char>
data);
1005 template <
typename T>
1006 void convertRGBCloudToUChar(
const pcl::PointCloud<T>& cloud,
1007 boost::shared_array<unsigned char>&
data);
1015 void emitMouseEvent(
unsigned long event_id);
1020 void emitKeyboardEvent(
unsigned long event_id);
1023 static void MouseCallback(vtkObject*,
1027 static void KeyboardCallback(vtkObject*,
1040 unsigned long event_id,
1041 void* call_data)
override {
1042 if (event_id != vtkCommand::TimerEvent)
return;
1043 int timer_id = *
static_cast<int*
>(call_data);
1044 if (timer_id != right_timer_id)
return;
1045 window->interactor_->TerminateApp();
1054 void Execute(vtkObject*,
unsigned long event_id,
void*)
override {
1055 if (event_id != vtkCommand::ExitEvent)
return;
1056 window->stopped_ =
true;
1057 window->interactor_->TerminateApp();
1080 LayerMap::iterator createLayer(
const std::string& layer_id,
1083 double opacity = 0.5,
1084 bool fill_box =
true);
1086 boost::signals2::signal<void(
const pcl::visualization::MouseEvent&)>
1088 boost::signals2::signal<void(
const pcl::visualization::KeyboardEvent&)>
1116 boost::shared_array<unsigned char>
data_;
1151 PCL_MAKE_ALIGNED_OPERATOR_NEW
std::shared_ptr< core::Tensor > image
std::function< void(std::shared_ptr< core::Tensor >)> callback
boost::geometry::model::polygon< point_xy > polygon
An image viewer interactor style, tailored for ImageViewer.
void OnMouseWheelForward() override
void OnMiddleButtonDown() override
static ImageViewerInteractorStyle * New()
void OnRightButtonDown() override
void OnMouseWheelBackward() override
ImageViewer is a class for 2D image visualization.
vtkSmartPointer< vtkRenderWindow > win_
The render window.
vtkSmartPointer< vtkImageViewer > image_viewer_
The ImageViewer widget.
std::vector< Layer > LayerMap
vtkSmartPointer< vtkCallbackCommand > keyboard_command_
void resetStoppedFlag()
Set the stopped flag back to false.
boost::signals2::connection registerKeyboardCallback(void(T::*callback)(const pcl::visualization::KeyboardEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for keyboard events.
shared_ptr< const ImageViewer > ConstPtr
boost::signals2::connection registerMouseCallback(void(*callback)(const pcl::visualization::MouseEvent &, void *), void *cookie=nullptr)
Register a callback std::function for mouse events.
void addMonoImage(const pcl::PointCloud< pcl::Intensity8u >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
void showRGBImage(const typename pcl::PointCloud< T >::ConstPtr &cloud, const std::string &layer_id="rgb_image", double opacity=1.0)
Show a 2D image on screen, obtained from the RGB channel of a point cloud.
std::vector< unsigned char * > image_data_
Internal data array. Used everytime add***Image is called. Cleared, everytime the render loop is exec...
boost::signals2::connection registerKeyboardCallback(void(*callback)(const pcl::visualization::KeyboardEvent &, void *), void *cookie=nullptr)
Register a callback function for keyboard events.
vtkSmartPointer< vtkImageSlice > slice_
Global prop. This is the actual "actor".
boost::shared_array< unsigned char > data_
The data array representing the image. Used internally.
int timer_id_
Global timer ID. Used in destructor only.
boost::signals2::signal< void(const pcl::visualization::KeyboardEvent &)> keyboard_signal_
LayerMap layer_map_
Internal list with different 2D layers shapes.
void showMonoImage(const pcl::PointCloud< pcl::Intensity8u >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
boost::signals2::signal< void(const pcl::visualization::MouseEvent &)> mouse_signal_
bool stopped_
Set to false if the interaction loop is running.
boost::signals2::connection registerMouseCallback(void(T::*callback)(const pcl::visualization::MouseEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for mouse events.
vtkSmartPointer< vtkRenderer > ren_
The renderer.
vtkSmartPointer< ExitMainLoopTimerCallback > exit_main_loop_timer_callback_
Callback object enabling us to leave the main loop, when a timer fires.
void close()
Stop the interaction and close the visualizaton window.
void addRGBImage(const typename pcl::PointCloud< T >::ConstPtr &cloud, const std::string &layer_id="rgb_image", double opacity=1.0)
Add an RGB 2D image layer, but do not render it (use spin/spinOnce to update).
vtkSmartPointer< vtkRenderWindowInteractor > interactor_
shared_ptr< ImageViewer > Ptr
std::size_t data_size_
The data array (representing the image) size. Used internally.
void addMonoImage(const pcl::PointCloud< pcl::Intensity >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
vtkSmartPointer< ImageViewerInteractorStyle > interactor_style_
The interactor style.
vtkSmartPointer< vtkCallbackCommand > mouse_command_
vtkSmartPointer< ExitCallback > exit_callback_
bool wasStopped() const
Returns true when the user tried to close the window.
vtkSmartPointer< vtkImageFlip > algo_
Image reslice, used for flipping the image.
void showMonoImage(const pcl::PointCloud< pcl::Intensity >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
void setInteractorStyle(vtkInteractorObserver *style)
Set up the interactor style. By default the interactor style is set to vtkInteractorStyleImage you ca...
std::vector< Pair > Correspondences
Eigen::Array< unsigned char, 3, 1 > Vector3ub
static const Vector3ub red_color(255, 0, 0)
static const Vector3ub blue_color(0, 0, 255)
static const Vector3ub green_color(0, 255, 0)
static ExitCallback * New()
void Execute(vtkObject *, unsigned long event_id, void *) override
ExitMainLoopTimerCallback()
void Execute(vtkObject *vtkNotUsed(caller), unsigned long event_id, void *call_data) override
static ExitMainLoopTimerCallback * New()
bool operator()(const Layer &layer)
LayerComparator(const std::string &str)
Internal structure describing a layer.
vtkSmartPointer< vtkContextActor > actor