From 3f9581825225d2a8a878d1e05a34eb2f45da473f Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Tue, 8 Apr 2014 20:26:56 +0400 Subject: [PATCH] Added cloud shading support - additional normals array is used --- modules/viz/doc/widget.rst | 20 ++++++++++++++++++++ modules/viz/include/opencv2/viz/widgets.hpp | 7 +++++++ modules/viz/src/clouds.cpp | 25 +++++++++++++++++++------ modules/viz/test/tests_simple.cpp | 16 ++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/modules/viz/doc/widget.rst b/modules/viz/doc/widget.rst index 0601ba1..751838d 100644 --- a/modules/viz/doc/widget.rst +++ b/modules/viz/doc/widget.rst @@ -897,6 +897,10 @@ This 3D Widget defines a point cloud. :: WCloud(InputArray cloud, InputArray colors); //! All points in cloud have the same color WCloud(InputArray cloud, const Color &color = Color::white()); + //! Each point in cloud is mapped to a color in colors, normals are used for shading + WCloud(InputArray cloud, InputArray colors, InputArray normals); + //! All points in cloud have the same color, normals are used for shading + WCloud(InputArray cloud, const Color &color, InputArray normals); }; viz::WCloud::WCloud @@ -917,6 +921,22 @@ Constructs a WCloud. Points in the cloud belong to mask when they are set to (NaN, NaN, NaN). +.. ocv:function:: WCloud(InputArray cloud, InputArray colors, InputArray normals) + + :param cloud: Set of points which can be of type: ``CV_32FC3``, ``CV_32FC4``, ``CV_64FC3``, ``CV_64FC4``. + :param colors: Set of colors. It has to be of the same size with cloud. + :param normals: Normals for each point in cloud. Size and type should match with the cloud parameter. + + Points in the cloud belong to mask when they are set to (NaN, NaN, NaN). + +.. ocv:function:: WCloud(InputArray cloud, const Color &color, InputArray normals) + + :param cloud: Set of points which can be of type: ``CV_32FC3``, ``CV_32FC4``, ``CV_64FC3``, ``CV_64FC4``. + :param color: A single :ocv:class:`Color` for the whole cloud. + :param normals: Normals for each point in cloud. Size and type should match with the cloud parameter. + + Points in the cloud belong to mask when they are set to (NaN, NaN, NaN). + .. note:: In case there are four channels in the cloud, fourth channel is ignored. viz::WCloudCollection diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 2949598..611db54 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -320,8 +320,15 @@ namespace cv public: //! Each point in cloud is mapped to a color in colors WCloud(InputArray cloud, InputArray colors); + //! All points in cloud have the same color WCloud(InputArray cloud, const Color &color = Color::white()); + + //! Each point in cloud is mapped to a color in colors, normals are used for shading + WCloud(InputArray cloud, InputArray colors, InputArray normals); + + //! All points in cloud have the same color, normals are used for shading + WCloud(InputArray cloud, const Color &color, InputArray normals); }; class CV_EXPORTS WPaintedCloud: public Widget3D diff --git a/modules/viz/src/clouds.cpp b/modules/viz/src/clouds.cpp index eec0263..48d057d 100644 --- a/modules/viz/src/clouds.cpp +++ b/modules/viz/src/clouds.cpp @@ -50,10 +50,28 @@ cv::viz::WCloud::WCloud(InputArray cloud, InputArray colors) { + WCloud cloud_widget(cloud, colors, cv::noArray()); + *this = cloud_widget; +} + +cv::viz::WCloud::WCloud(InputArray cloud, const Color &color) +{ + WCloud cloud_widget(cloud, Mat(cloud.size(), CV_8UC3, color)); + *this = cloud_widget; +} + +cv::viz::WCloud::WCloud(InputArray cloud, const Color &color, InputArray normals) +{ + WCloud cloud_widget(cloud, Mat(cloud.size(), CV_8UC3, color), normals); + *this = cloud_widget; +} + +cv::viz::WCloud::WCloud(cv::InputArray cloud, cv::InputArray colors, cv::InputArray normals) +{ CV_Assert(!cloud.empty() && !colors.empty()); vtkSmartPointer cloud_source = vtkSmartPointer::New(); - cloud_source->SetColorCloud(cloud, colors); + cloud_source->SetColorCloudNormals(cloud, colors, normals); cloud_source->Update(); vtkSmartPointer mapper = vtkSmartPointer::New(); @@ -69,12 +87,7 @@ cv::viz::WCloud::WCloud(InputArray cloud, InputArray colors) actor->SetMapper(mapper); WidgetAccessor::setProp(*this, actor); -} -cv::viz::WCloud::WCloud(InputArray cloud, const Color &color) -{ - WCloud cloud_widget(cloud, Mat(cloud.size(), CV_8UC3, color)); - *this = cloud_widget; } diff --git a/modules/viz/test/tests_simple.cpp b/modules/viz/test/tests_simple.cpp index d1b059c..f8f63c3 100644 --- a/modules/viz/test/tests_simple.cpp +++ b/modules/viz/test/tests_simple.cpp @@ -248,6 +248,22 @@ TEST(Viz, show_sampled_normals) viz.spin(); } +TEST(Viz, show_cloud_shaded_by_normals) +{ + Mesh mesh = Mesh::load(get_dragon_ply_file_path()); + computeNormals(mesh, mesh.normals); + + Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0)); + + WCloud cloud(mesh.cloud, Color::white(), mesh.normals); + cloud.setRenderingProperty(SHADING, SHADING_GOURAUD); + + Viz3d viz("show_cloud_shaded_by_normals"); + viz.showWidget("cloud", cloud, pose); + viz.showWidget("text2d", WText("Cloud shaded by normals", Point(20, 20), 20, Color::green())); + viz.spin(); +} + TEST(Viz, show_trajectories) { std::vector path = generate_test_trajectory(), sub0, sub1, sub2, sub3, sub4, sub5; -- 2.7.4