From 4833976a0c7fce492cf69a33f2815fcedd3a2890 Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Tue, 7 Jan 2014 22:35:21 +0400 Subject: [PATCH] refactored frustum trajectory to use glyphs --- modules/viz/doc/widget.rst | 8 +- modules/viz/include/opencv2/viz/widgets.hpp | 9 +- modules/viz/src/shapes.cpp | 122 +++++++--------------------- modules/viz/src/types.cpp | 2 +- 4 files changed, 39 insertions(+), 102 deletions(-) diff --git a/modules/viz/doc/widget.rst b/modules/viz/doc/widget.rst index 936cc5a..4119df0 100644 --- a/modules/viz/doc/widget.rst +++ b/modules/viz/doc/widget.rst @@ -762,9 +762,9 @@ This 3D Widget represents a trajectory. :: { public: //! Displays trajectory of the given path by frustums - WTrajectoryFrustums(const std::vector &path, const Matx33d &K, double scale = 1.0, const Color &color = Color::white()); + WTrajectoryFrustums(InputArray path, const Matx33d &K, double scale = 1.0, const Color &color = Color::white()); //! Displays trajectory of the given path by frustums - WTrajectoryFrustums(const std::vector &path, const Vec2d &fov, double scale = 1.0, const Color &color = Color::white()); + WTrajectoryFrustums(InputArray path, const Vec2d &fov, double scale = 1.0, const Color &color = Color::white()); }; viz::WTrajectoryFrustums::WTrajectoryFrustums @@ -773,7 +773,7 @@ Constructs a WTrajectoryFrustums. .. ocv:function:: WTrajectoryFrustums(const std::vector &path, const Matx33d &K, double scale = 1.0, const Color &color = Color::white()) - :param path: List of poses on a trajectory. + :param path: List of poses on a trajectory. Takes std::vector> with T == [float | double] :param K: Intrinsic matrix of the camera. :param scale: Scale of the frustums. :param color: :ocv:class:`Color` of the frustums. @@ -782,7 +782,7 @@ Constructs a WTrajectoryFrustums. .. ocv:function:: WTrajectoryFrustums(const std::vector &path, const Vec2d &fov, double scale = 1.0, const Color &color = Color::white()) - :param path: List of poses on a trajectory. + :param path: List of poses on a trajectory. Takes std::vector> with T == [float | double] :param fov: Field of view of the camera (horizontal, vertical). :param scale: Scale of the frustums. :param color: :ocv:class:`Color` of the frustums. diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 8fedb0c..ad75199 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -269,10 +269,11 @@ namespace cv class CV_EXPORTS WTrajectoryFrustums : public Widget3D { public: - //! Displays trajectory of the given path by frustums - WTrajectoryFrustums(const std::vector &path, const Matx33d &K, double scale = 1., const Color &color = Color::white()); - //! Displays trajectory of the given path by frustums - WTrajectoryFrustums(const std::vector &path, const Vec2d &fov, double scale = 1., const Color &color = Color::white()); + //! Takes vector> and displays trajectory of the given path by frustums + WTrajectoryFrustums(InputArray path, const Matx33d &K, double scale = 1., const Color &color = Color::white()); + + //! Takes vector> and displays trajectory of the given path by frustums + WTrajectoryFrustums(InputArray path, const Vec2d &fov, double scale = 1., const Color &color = Color::white()); }; class CV_EXPORTS WTrajectorySpheres: public Widget3D diff --git a/modules/viz/src/shapes.cpp b/modules/viz/src/shapes.cpp index df38333..c177a9b 100644 --- a/modules/viz/src/shapes.cpp +++ b/modules/viz/src/shapes.cpp @@ -1056,41 +1056,6 @@ template<> cv::viz::WCameraPosition cv::viz::Widget::cast poly_data, vtkSmartPointer append_filter, const std::vector &path) - { - vtkIdType nr_points = path.size(); - - for (vtkIdType i = 0; i < nr_points; ++i) - { - vtkSmartPointer new_data = vtkSmartPointer::New(); - new_data->DeepCopy(poly_data); - - // Transform the default coordinate frame - vtkSmartPointer transform = vtkSmartPointer::New(); - transform->PreMultiply(); - vtkSmartPointer mat_trans = vtkSmartPointer::New(); - mat_trans = vtkmatrix(path[i].matrix); - transform->SetMatrix(mat_trans); - - vtkSmartPointer filter = vtkSmartPointer::New(); -#if VTK_MAJOR_VERSION <= 5 - filter->SetInput(new_data); -#else - filter->SetInputData(new_data); -#endif - filter->SetTransform(transform); - filter->Update(); - - append_filter->AddInputConnection(filter->GetOutputPort()); - } - } - }; -}}} - cv::viz::WTrajectory::WTrajectory(InputArray _path, int display_mode, double scale, const Color &color) { vtkSmartPointer appendFilter = vtkSmartPointer::New(); @@ -1103,7 +1068,6 @@ cv::viz::WTrajectory::WTrajectory(InputArray _path, int display_mode, double sca appendFilter->AddInputConnection(polydata->GetProducerPort()); } - vtkSmartPointer tensor_glyph; if (display_mode & WTrajectory::FRAMES) { vtkSmartPointer source = vtkSmartPointer::New(); @@ -1111,7 +1075,7 @@ cv::viz::WTrajectory::WTrajectory(InputArray _path, int display_mode, double sca vtkSmartPointer glyph = getPolyData(WCoordinateSystem(scale)); - tensor_glyph = vtkSmartPointer::New(); + vtkSmartPointer tensor_glyph = vtkSmartPointer::New(); tensor_glyph->SetInputConnection(source->GetOutputPort()); tensor_glyph->SetSourceConnection(glyph->GetProducerPort()); tensor_glyph->ExtractEigenvaluesOff(); // Treat as a rotation matrix, not as something with eigenvalues @@ -1142,42 +1106,26 @@ template<> cv::viz::WTrajectory cv::viz::Widget::cast() /////////////////////////////////////////////////////////////////////////////////////////////// /// WTrajectoryFrustums widget implementation -cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(const std::vector &path, const Matx33d &K, double scale, const Color &color) +cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(InputArray _path, const Matx33d &K, double scale, const Color &color) { - vtkSmartPointer camera = vtkSmartPointer::New(); - double f_x = K(0,0); - double f_y = K(1,1); - double c_y = K(1,2); - double aspect_ratio = f_y / f_x; - // Assuming that this is an ideal camera (c_y and c_x are at the center of the image) - double fovy = 2.0 * atan2(c_y, f_y) * 180 / CV_PI; - - camera->SetViewAngle(fovy); - camera->SetPosition(0.0, 0.0, 0.0); - camera->SetViewUp(0.0, 1.0, 0.0); - camera->SetFocalPoint(0.0, 0.0, 1.0); - camera->SetClippingRange(0.01, scale); - - double planesArray[24]; - camera->GetFrustumPlanes(aspect_ratio, planesArray); + vtkSmartPointer source = vtkSmartPointer::New(); + source->SetTrajectory(_path); - vtkSmartPointer planes = vtkSmartPointer::New(); - planes->SetFrustumPlanes(planesArray); + vtkSmartPointer glyph = getPolyData(WCameraPosition(K, scale)); - vtkSmartPointer frustumSource = vtkSmartPointer::New(); - frustumSource->SetPlanes(planes); - frustumSource->Update(); + vtkSmartPointer tensor_glyph = vtkSmartPointer::New(); + tensor_glyph->SetInputConnection(source->GetOutputPort()); + tensor_glyph->SetSourceConnection(glyph->GetProducerPort()); + tensor_glyph->ExtractEigenvaluesOff(); // Treat as a rotation matrix, not as something with eigenvalues + tensor_glyph->ThreeGlyphsOff(); + tensor_glyph->SymmetricOff(); + tensor_glyph->ColorGlyphsOff(); + tensor_glyph->Update(); - // Extract the edges - vtkSmartPointer filter = vtkSmartPointer::New(); - filter->SetInputConnection(frustumSource->GetOutputPort()); - filter->Update(); - - vtkSmartPointer appendFilter = vtkSmartPointer::New(); - TrajectoryUtils::applyPath(filter->GetOutput(), appendFilter, path); + vtkSmartPointer polydata = tensor_glyph->GetOutput(); vtkSmartPointer mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(appendFilter->GetOutputPort()); + mapper->SetInputConnection(polydata->GetProducerPort()); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); @@ -1186,38 +1134,26 @@ cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(const std::vector &p setColor(color); } -cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(const std::vector &path, const Vec2d &fov, double scale, const Color &color) +cv::viz::WTrajectoryFrustums::WTrajectoryFrustums(InputArray _path, const Vec2d &fov, double scale, const Color &color) { - vtkSmartPointer camera = vtkSmartPointer::New(); + vtkSmartPointer source = vtkSmartPointer::New(); + source->SetTrajectory(_path); - camera->SetViewAngle(fov[1] * 180 / CV_PI); // Vertical field of view - camera->SetPosition(0.0, 0.0, 0.0); - camera->SetViewUp(0.0, 1.0, 0.0); - camera->SetFocalPoint(0.0, 0.0, 1.0); - camera->SetClippingRange(0.01, scale); + vtkSmartPointer glyph = getPolyData(WCameraPosition(fov, scale)); - double aspect_ratio = tan(fov[0] * 0.5) / tan(fov[1] * 0.5); - - double planesArray[24]; - camera->GetFrustumPlanes(aspect_ratio, planesArray); - - vtkSmartPointer planes = vtkSmartPointer::New(); - planes->SetFrustumPlanes(planesArray); - - vtkSmartPointer frustumSource = vtkSmartPointer::New(); - frustumSource->SetPlanes(planes); - frustumSource->Update(); + vtkSmartPointer tensor_glyph = vtkSmartPointer::New(); + tensor_glyph->SetInputConnection(source->GetOutputPort()); + tensor_glyph->SetSourceConnection(glyph->GetProducerPort()); + tensor_glyph->ExtractEigenvaluesOff(); // Treat as a rotation matrix, not as something with eigenvalues + tensor_glyph->ThreeGlyphsOff(); + tensor_glyph->SymmetricOff(); + tensor_glyph->ColorGlyphsOff(); + tensor_glyph->Update(); - // Extract the edges - vtkSmartPointer filter = vtkSmartPointer::New(); - filter->SetInputConnection(frustumSource->GetOutputPort()); - filter->Update(); - - vtkSmartPointer appendFilter = vtkSmartPointer::New(); - TrajectoryUtils::applyPath(filter->GetOutput(), appendFilter, path); + vtkSmartPointer polydata = tensor_glyph->GetOutput(); vtkSmartPointer mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(appendFilter->GetOutputPort()); + mapper->SetInputConnection(polydata->GetProducerPort()); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); diff --git a/modules/viz/src/types.cpp b/modules/viz/src/types.cpp index a6ea95a..d233e73 100644 --- a/modules/viz/src/types.cpp +++ b/modules/viz/src/types.cpp @@ -245,6 +245,6 @@ void cv::viz::Camera::computeProjectionMatrix(Matx44d &proj) const cv::viz::Camera cv::viz::Camera::KinectCamera(const Size &window_size) { - Matx33d K(525.0, 0.0, 320.0, 0.0, 525.0, 240.0, 0.0, 0.0, 1.0) ; + Matx33d K(525.0, 0.0, 320.0, 0.0, 525.0, 240.0, 0.0, 0.0, 1.0); return Camera(K, window_size); } -- 2.7.4