From bb891f05702ef08f04d090a763af03063022b902 Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Sun, 12 Jan 2014 00:05:59 +0400 Subject: [PATCH] added cone implementation --- modules/viz/doc/widget.rst | 42 ++++++++++++++++++++- modules/viz/include/opencv2/viz/widgets.hpp | 11 ++++++ modules/viz/src/precomp.hpp | 1 + modules/viz/src/shapes.cpp | 40 ++++++++++++++++++++ modules/viz/test/tests_simple.cpp | 2 + 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/modules/viz/doc/widget.rst b/modules/viz/doc/widget.rst index 474e0f2fb5..5b72adbd59 100644 --- a/modules/viz/doc/widget.rst +++ b/modules/viz/doc/widget.rst @@ -349,7 +349,7 @@ viz::WCircle This 3D Widget defines a circle. :: - class CV_EXPORTS WCircle : public Widget3D + class CV_EXPORTS WCircle : public Widget3D { public: //! creates default planar circle centred at origin with plane normal along z-axis @@ -381,6 +381,46 @@ Constructs repositioned planar circle. :param thickness: Thickness of the circle. :param color: :ocv:class:`Color` of the circle. + +viz::WCone +----------------- +.. ocv:class:: WCone + +This 3D Widget defines a cone. :: + + class CV_EXPORTS WCone : public Widget3D + { + public: + //! create default cone, oriented along x-axis with center of its base located at origin + WCone(double lenght, double radius, int resolution = 6.0, const Color &color = Color::white()); + + //! creates repositioned cone + WCone(double radius, const Point3d& center, const Point3d& tip, int resolution = 6.0, const Color &color = Color::white()); + }; + +viz::WCone::WCone +------------------------------- +Constructs default cone oriented along x-axis with center of its base located at origin + +.. ocv:function:: WCone(double length, double radius, int resolution = 6.0, const Color &color = Color::white()); + + :param length: Length of the cone. + :param radius: Radius of the cone. + :param resolution: Resolution of the cone. + :param color: :ocv:class:`Color` of the cone. + +viz::WCone::WCone +------------------------------- +Constructs repositioned planar cone. + +.. ocv:function:: WCone(double radius, const Point3d& center, const Point3d& tip, int resolution = 6.0, const Color &color = Color::white()); + + :param radius: Radius of the cone. + :param center: Center of the cone base. + :param tip: Tip of the cone. + :param resolution: Resolution of the cone. + :param color: :ocv:class:`Color` of the cone. + viz::WCylinder -------------- .. ocv:class:: WCylinder diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 3ebcd35c9e..f745e54848 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -171,6 +171,16 @@ namespace cv WCircle(double radius, const Point3d& center, const Vec3d& normal, double thickness = 0.01, const Color &color = Color::white()); }; + class CV_EXPORTS WCone : public Widget3D + { + public: + //! create default cone, oriented along x-axis with center of its base located at origin + WCone(double length, double radius, int resolution = 6.0, const Color &color = Color::white()); + + //! creates repositioned cone + WCone(double radius, const Point3d& center, const Point3d& tip, int resolution = 6.0, const Color &color = Color::white()); + }; + class CV_EXPORTS WCylinder : public Widget3D { public: @@ -341,6 +351,7 @@ namespace cv template<> CV_EXPORTS WCylinder Widget::cast(); template<> CV_EXPORTS WArrow Widget::cast(); template<> CV_EXPORTS WCircle Widget::cast(); + template<> CV_EXPORTS WCone Widget::cast(); template<> CV_EXPORTS WCube Widget::cast(); template<> CV_EXPORTS WCoordinateSystem Widget::cast(); template<> CV_EXPORTS WPolyLine Widget::cast(); diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index bc91191d17..4b963661d4 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -127,6 +127,7 @@ #include #include #include +#include #include #if !defined(_WIN32) || defined(__CYGWIN__) diff --git a/modules/viz/src/shapes.cpp b/modules/viz/src/shapes.cpp index 89e3cd38b1..5e76286c82 100644 --- a/modules/viz/src/shapes.cpp +++ b/modules/viz/src/shapes.cpp @@ -253,6 +253,46 @@ template<> cv::viz::WCircle cv::viz::Widget::cast() return static_cast(widget); } +/////////////////////////////////////////////////////////////////////////////////////////////// +/// WCone widget implementation + +cv::viz::WCone::WCone(double length, double radius, int resolution, const Color &color) +{ + vtkSmartPointer cone_source = vtkSmartPointer::New(); + cone_source->SetCenter(length*0.5, 0.0, 0.0); + cone_source->SetHeight(length); + cone_source->SetRadius(radius); + cone_source->SetResolution(resolution); + cone_source->Update(); + + vtkSmartPointer mapper = vtkSmartPointer::New(); + VtkUtils::SetInputData(mapper, cone_source->GetOutput()); + + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + + WidgetAccessor::setProp(*this, actor); + setColor(color); +} + +cv::viz::WCone::WCone(double radius, const Point3d& center, const Point3d& tip, int resolution, const Color &color) +{ + Vec3d arbitrary = get_random_vec(); + Vec3d xvec = normalized(Vec3d(tip - center)); + Vec3d zvec = normalized(xvec.cross(arbitrary)); + Vec3d yvec = zvec.cross(xvec); + + WCone circle(norm(tip - center), radius, resolution, color); + circle.applyTransform(makeTransformToGlobal(xvec, yvec, zvec, center)); + *this = circle; +} + +template<> cv::viz::WCone cv::viz::Widget::cast() +{ + Widget3D widget = this->cast(); + return static_cast(widget); +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// cylinder widget implementation diff --git a/modules/viz/test/tests_simple.cpp b/modules/viz/test/tests_simple.cpp index f250d03a09..58e909f5fb 100644 --- a/modules/viz/test/tests_simple.cpp +++ b/modules/viz/test/tests_simple.cpp @@ -299,6 +299,8 @@ TEST(Viz, show_simple_widgets) viz.showWidget("cir2", WCircle(0.5, Point3d(0.5, 0.0, 0.0), Vec3d(1.0, 0.0, 0.0), 0.01, Color::apricot())); viz.showWidget("cyl0", WCylinder(Vec3d(-0.5, 0.5, -0.5), Vec3d(0.5, 0.5, -0.5), 0.125, 30, Color::brown())); + viz.showWidget("con0", WCone(0.25, 0.125, 6, Color::azure())); + viz.showWidget("con1", WCone(0.125, Point3d(0.5, -0.5, 0.5), Point3d(0.5, -1.0, 0.5), 6, Color::turquoise())); viz.spin(); } -- GitLab