From cf36b8f817b025ad4b9e4883e12355680ce5685e Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Sat, 24 Aug 2013 11:49:56 +0200 Subject: [PATCH] rendering properties in Widget and Viz3d --- modules/viz/include/opencv2/viz/viz3d.hpp | 4 +- modules/viz/include/opencv2/viz/widgets.hpp | 7 +- modules/viz/src/viz3d.cpp | 5 +- modules/viz/src/viz3d_impl.cpp | 173 -------------------- modules/viz/src/viz3d_impl.hpp | 6 +- modules/viz/src/widget.cpp | 125 ++++++++++++++ 6 files changed, 137 insertions(+), 183 deletions(-) diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index c27b08df5..7b0875513 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -7,7 +7,6 @@ #include #include #include -#include namespace cv { @@ -55,6 +54,9 @@ namespace cv void registerKeyboardCallback(KeyboardCallback callback, void* cookie = 0); void registerMouseCallback(MouseCallback callback, void* cookie = 0); + + void setRenderingProperty(int property, double value, const String &id); + double getRenderingProperty(int property, const String &id); private: struct VizImpl; diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 8ad9b3d31..99f4bd420 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -16,10 +16,12 @@ namespace cv Widget(); Widget(const Widget &other); Widget& operator =(const Widget &other); + ~Widget(); static Widget fromPlyFile(const String &file_name); - - ~Widget(); + + void setRenderingProperty(int property, double value); + double getRenderingProperty(int property) const; template _W cast(); private: @@ -43,7 +45,6 @@ namespace cv Affine3f getPose() const; void setColor(const Color &color); - private: struct MatrixConverter; diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index 696976d76..fc9ce01c0 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -54,8 +54,6 @@ void cv::viz::Viz3d::registerKeyboardCallback(KeyboardCallback callback, void* c void cv::viz::Viz3d::registerMouseCallback(MouseCallback callback, void* cookie) { impl_->registerMouseCallback(callback, cookie); } - - void cv::viz::Viz3d::showWidget(const String &id, const Widget &widget, const Affine3f &pose) { impl_->showWidget(id, widget, pose); } void cv::viz::Viz3d::removeWidget(const String &id) { impl_->removeWidget(id); } cv::viz::Widget cv::viz::Viz3d::getWidget(const String &id) const { return impl_->getWidget(id); } @@ -75,3 +73,6 @@ void cv::viz::Viz3d::converTo3DRay(const Point3d &window_coord, Point3d &origin, cv::Size cv::viz::Viz3d::getWindowSize() const { return impl_->getWindowSize(); } void cv::viz::Viz3d::setWindowSize(const Size &window_size) { impl_->setWindowSize(window_size.width, window_size.height); } cv::String cv::viz::Viz3d::getWindowName() const { return impl_->getWindowName(); } + +void cv::viz::Viz3d::setRenderingProperty(int property, double value, const String &id) { getWidget(id).setRenderingProperty(property, value); } +double cv::viz::Viz3d::getRenderingProperty(int property, const String &id) { return getWidget(id).getRenderingProperty(property); } diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index f1417053b..aa51b1015 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -249,179 +249,6 @@ void cv::viz::Viz3d::VizImpl::setBackgroundColor (const Color& color) renderer_->SetBackground (c.val); } -///////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::getPointCloudRenderingProperties (int property, double &value, const std::string &id) -{ - CloudActorMap::iterator am_it = cloud_actor_map_->find (id); - if (am_it == cloud_actor_map_->end ()) - return false; - - vtkLODActor* actor = vtkLODActor::SafeDownCast (am_it->second.actor); - - switch (property) - { - case VIZ_POINT_SIZE: - { - value = actor->GetProperty ()->GetPointSize (); - actor->Modified (); - break; - } - case VIZ_OPACITY: - { - value = actor->GetProperty ()->GetOpacity (); - actor->Modified (); - break; - } - case VIZ_LINE_WIDTH: - { - value = actor->GetProperty ()->GetLineWidth (); - actor->Modified (); - break; - } - default: - CV_Assert("getPointCloudRenderingProperties: Unknown property"); - } - - return true; -} - -///////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::setPointCloudRenderingProperties (int property, double value, const std::string &id) -{ - CloudActorMap::iterator am_it = cloud_actor_map_->find (id); - if (am_it == cloud_actor_map_->end ()) - return std::cout << "[setPointCloudRenderingProperties] Could not find any PointCloud datasets with id <" << id << ">!" << std::endl, false; - - vtkLODActor* actor = vtkLODActor::SafeDownCast (am_it->second.actor); - - switch (property) - { - case VIZ_POINT_SIZE: - { - actor->GetProperty ()->SetPointSize (float (value)); - actor->Modified (); - break; - } - case VIZ_OPACITY: - { - actor->GetProperty ()->SetOpacity (value); - actor->Modified (); - break; - } - // Turn on/off flag to control whether data is rendered using immediate - // mode or note. Immediate mode rendering tends to be slower but it can - // handle larger datasets. The default value is immediate mode off. If you - // are having problems rendering a large dataset you might want to consider - // using immediate more rendering. - case VIZ_IMMEDIATE_RENDERING: - { - actor->GetMapper ()->SetImmediateModeRendering (int (value)); - actor->Modified (); - break; - } - case VIZ_LINE_WIDTH: - { - actor->GetProperty ()->SetLineWidth (float (value)); - actor->Modified (); - break; - } - default: - CV_Assert("setPointCloudRenderingProperties: Unknown property"); - } - return true; -} - -///////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::setShapeRenderingProperties (int property, double value, const std::string &id) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it == shape_actor_map_->end ()) - return std::cout << "[setShapeRenderingProperties] Could not find any shape with id <" << id << ">!\n" << std::endl, false; - - vtkActor* actor = vtkActor::SafeDownCast (am_it->second); - - switch (property) - { - case VIZ_POINT_SIZE: - { - actor->GetProperty ()->SetPointSize (float (value)); - actor->Modified (); - break; - } - case VIZ_OPACITY: - { - actor->GetProperty ()->SetOpacity (value); - actor->Modified (); - break; - } - case VIZ_LINE_WIDTH: - { - actor->GetProperty ()->SetLineWidth (float (value)); - actor->Modified (); - break; - } - case VIZ_FONT_SIZE: - { - vtkTextActor* text_actor = vtkTextActor::SafeDownCast (am_it->second); - vtkSmartPointer tprop = text_actor->GetTextProperty (); - tprop->SetFontSize (int (value)); - text_actor->Modified (); - break; - } - case VIZ_REPRESENTATION: - { - switch (int (value)) - { - case REPRESENTATION_POINTS: actor->GetProperty ()->SetRepresentationToPoints (); break; - case REPRESENTATION_WIREFRAME: actor->GetProperty ()->SetRepresentationToWireframe (); break; - case REPRESENTATION_SURFACE: actor->GetProperty ()->SetRepresentationToSurface (); break; - } - actor->Modified (); - break; - } - case VIZ_SHADING: - { - switch (int (value)) - { - case SHADING_FLAT: actor->GetProperty ()->SetInterpolationToFlat (); break; - case SHADING_GOURAUD: - { - if (!actor->GetMapper ()->GetInput ()->GetPointData ()->GetNormals ()) - { - std::cout << "[cv::viz::PCLVisualizer::setShapeRenderingProperties] Normals do not exist in the dataset, but Gouraud shading was requested. Estimating normals...\n" << std::endl; - - vtkSmartPointer normals = vtkSmartPointer::New (); - normals->SetInput (actor->GetMapper ()->GetInput ()); - normals->Update (); - vtkDataSetMapper::SafeDownCast (actor->GetMapper ())->SetInput (normals->GetOutput ()); - } - actor->GetProperty ()->SetInterpolationToGouraud (); - break; - } - case SHADING_PHONG: - { - if (!actor->GetMapper ()->GetInput ()->GetPointData ()->GetNormals ()) - { - std::cout << "[cv::viz::PCLVisualizer::setShapeRenderingProperties] Normals do not exist in the dataset, but Phong shading was requested. Estimating normals...\n" << std::endl; - vtkSmartPointer normals = vtkSmartPointer::New (); - normals->SetInput (actor->GetMapper ()->GetInput ()); - normals->Update (); - vtkDataSetMapper::SafeDownCast (actor->GetMapper ())->SetInput (normals->GetOutput ()); - } - actor->GetProperty ()->SetInterpolationToPhong (); - break; - } - } - actor->Modified (); - break; - } - default: - CV_Assert("setShapeRenderingProperties: Unknown property"); - - } - return true; -} - ///////////////////////////////////////////////////////////////////////////////////////////// void cv::viz::Viz3d::VizImpl::initCameraParameters () { diff --git a/modules/viz/src/viz3d_impl.hpp b/modules/viz/src/viz3d_impl.hpp index 085a718c1..60ec70762 100644 --- a/modules/viz/src/viz3d_impl.hpp +++ b/modules/viz/src/viz3d_impl.hpp @@ -19,10 +19,8 @@ public: void removeAllWidgets(); - // to refactor: Widget3D:: & Viz3d:: - bool setPointCloudRenderingProperties (int property, double value, const String& id = "cloud"); - bool getPointCloudRenderingProperties (int property, double &value, const String& id = "cloud"); - bool setShapeRenderingProperties (int property, double value, const String& id); + void setRenderingProperty(int property, double value, const String &id); + double getRenderingProperty(int property, const String &id); /** \brief Returns true when the user tried to close the window */ bool wasStopped () const { if (interactor_ != NULL) return (stopped_); else return true; } diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index a70409772..808e3f047 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -94,6 +94,131 @@ cv::viz::Widget cv::viz::Widget::fromPlyFile(const String &file_name) return widget; } +void cv::viz::Widget::setRenderingProperty(int property, double value) +{ + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + + switch (property) + { + case VIZ_POINT_SIZE: + { + actor->GetProperty ()->SetPointSize (float (value)); + actor->Modified (); + break; + } + case VIZ_OPACITY: + { + actor->GetProperty ()->SetOpacity (value); + actor->Modified (); + break; + } + // Turn on/off flag to control whether data is rendered using immediate + // mode or note. Immediate mode rendering tends to be slower but it can + // handle larger datasets. The default value is immediate mode off. If you + // are having problems rendering a large dataset you might want to consider + // using immediate more rendering. + case VIZ_IMMEDIATE_RENDERING: + { + actor->GetMapper ()->SetImmediateModeRendering (int (value)); + actor->Modified (); + break; + } + case VIZ_LINE_WIDTH: + { + actor->GetProperty ()->SetLineWidth (float (value)); + actor->Modified (); + break; + } + default: + CV_Assert("setPointCloudRenderingProperties: Unknown property"); + } +} + +double cv::viz::Widget::getRenderingProperty(int property) const +{ + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this)); + CV_Assert(actor); + + double value = 0.0; + switch (property) + { + case VIZ_POINT_SIZE: + { + value = actor->GetProperty ()->GetPointSize (); + actor->Modified (); + break; + } + case VIZ_OPACITY: + { + value = actor->GetProperty ()->GetOpacity (); + actor->Modified (); + break; + } + case VIZ_LINE_WIDTH: + { + value = actor->GetProperty ()->GetLineWidth (); + actor->Modified (); + break; + } + case VIZ_FONT_SIZE: + { + vtkTextActor* text_actor = vtkTextActor::SafeDownCast (actor); + vtkSmartPointer tprop = text_actor->GetTextProperty (); + tprop->SetFontSize (int (value)); + text_actor->Modified (); + break; + } + case VIZ_REPRESENTATION: + { + switch (int (value)) + { + case REPRESENTATION_POINTS: actor->GetProperty ()->SetRepresentationToPoints (); break; + case REPRESENTATION_WIREFRAME: actor->GetProperty ()->SetRepresentationToWireframe (); break; + case REPRESENTATION_SURFACE: actor->GetProperty ()->SetRepresentationToSurface (); break; + } + actor->Modified (); + break; + } + case VIZ_SHADING: + { + switch (int (value)) + { + case SHADING_FLAT: actor->GetProperty ()->SetInterpolationToFlat (); break; + case SHADING_GOURAUD: + { + if (!actor->GetMapper ()->GetInput ()->GetPointData ()->GetNormals ()) + { + vtkSmartPointer normals = vtkSmartPointer::New (); + normals->SetInput (actor->GetMapper ()->GetInput ()); + normals->Update (); + vtkDataSetMapper::SafeDownCast (actor->GetMapper ())->SetInput (normals->GetOutput ()); + } + actor->GetProperty ()->SetInterpolationToGouraud (); + break; + } + case SHADING_PHONG: + { + if (!actor->GetMapper ()->GetInput ()->GetPointData ()->GetNormals ()) + { + vtkSmartPointer normals = vtkSmartPointer::New (); + normals->SetInput (actor->GetMapper ()->GetInput ()); + normals->Update (); + vtkDataSetMapper::SafeDownCast (actor->GetMapper ())->SetInput (normals->GetOutput ()); + } + actor->GetProperty ()->SetInterpolationToPhong (); + break; + } + } + actor->Modified (); + break; + } + default: + CV_Assert("getPointCloudRenderingProperties: Unknown property"); + } + return value; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// widget accessor implementaion