From: ozantonkal Date: Mon, 22 Jul 2013 13:03:28 +0000 (+0200) Subject: shorten Trajectory widget implementation X-Git-Tag: submit/tizen/20180620.034203~3^2~3745^2~33^2~20 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0f44ca42117881df9c3d0213b5fb6586f5be5ac;p=platform%2Fupstream%2Fopencv.git shorten Trajectory widget implementation --- diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 48616ca3f8..101d8568d7 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -180,6 +180,9 @@ namespace cv public: TrajectoryWidget(const std::vector &path, const Color &color = Color::white(), bool show_frames = false, double scale = 1.0); TrajectoryWidget(const std::vector &path, const Matx33f &K, double scale = 1.0, const Color &color = Color::white()); // Camera frustums + + private: + struct ApplyPath; }; class CV_EXPORTS CloudWidget : public Widget3D diff --git a/modules/viz/src/shape_widgets.cpp b/modules/viz/src/shape_widgets.cpp index 4067ef5e91..ed07691bb5 100644 --- a/modules/viz/src/shape_widgets.cpp +++ b/modules/viz/src/shape_widgets.cpp @@ -984,6 +984,36 @@ cv::viz::CameraPositionWidget::CameraPositionWidget(const Vec2f &fov, double sca /////////////////////////////////////////////////////////////////////////////////////////////// /// trajectory widget implementation +struct cv::viz::TrajectoryWidget::ApplyPath +{ + static void applyPath(vtkSmartPointer poly_data, vtkSmartPointer append_filter, const std::vector &path) + { + vtkSmartPointer mat_trans = vtkSmartPointer::New(); + mat_trans->Identity(); + + 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(); + vtkMatrix4x4::Multiply4x4(convertToVtkMatrix(path[i].matrix), mat_trans, mat_trans); + transform->SetMatrix(mat_trans); + + vtkSmartPointer filter = vtkSmartPointer::New(); + filter->SetInput(new_data); + filter->SetTransform(transform); + filter->Update(); + + append_filter->AddInputConnection(filter->GetOutputPort()); + } + } +}; + cv::viz::TrajectoryWidget::TrajectoryWidget(const std::vector &path, const Color &color, bool show_frames, double scale) { vtkIdType nr_points = path.size(); @@ -1016,49 +1046,33 @@ cv::viz::TrajectoryWidget::TrajectoryWidget(const std::vector &path, c vtkSmartPointer appendFilter = vtkSmartPointer::New(); if (show_frames) { - vtkSmartPointer mat_trans = vtkSmartPointer::New(); - mat_trans->Identity(); + vtkSmartPointer axes = vtkSmartPointer::New(); + axes->SetOrigin (0, 0, 0); + axes->SetScaleFactor (scale); - for (vtkIdType i = 0; i < nr_points; ++i) - { - vtkSmartPointer axes = vtkSmartPointer::New(); - axes->SetOrigin (0, 0, 0); - axes->SetScaleFactor (scale); - - vtkSmartPointer axes_colors = vtkSmartPointer::New (); - axes_colors->SetNumberOfComponents(3); - axes_colors->InsertNextTuple3(255,0,0); - axes_colors->InsertNextTuple3(255,0,0); - axes_colors->InsertNextTuple3(0,255,0); - axes_colors->InsertNextTuple3(0,255,0); - axes_colors->InsertNextTuple3(0,0,255); - axes_colors->InsertNextTuple3(0,0,255); - - vtkSmartPointer axes_data = axes->GetOutput (); - axes_data->Update (); - axes_data->GetPointData ()->SetScalars (axes_colors); - - // Transform the default coordinate frame - vtkSmartPointer transform = vtkSmartPointer::New(); - transform->PreMultiply(); - vtkMatrix4x4::Multiply4x4(convertToVtkMatrix(path[i].matrix), mat_trans, mat_trans); - transform->SetMatrix(mat_trans); - - vtkSmartPointer filter = vtkSmartPointer::New(); - filter->SetInput(axes_data); - filter->SetTransform(transform); - filter->Update(); - - vtkSmartPointer axes_tubes = vtkSmartPointer::New (); - axes_tubes->SetInput (filter->GetOutput()); - axes_tubes->SetRadius (axes->GetScaleFactor () / 50.0); - axes_tubes->SetNumberOfSides (6); - - appendFilter->AddInputConnection(axes_tubes->GetOutputPort()); - } + vtkSmartPointer axes_colors = vtkSmartPointer::New (); + axes_colors->SetNumberOfComponents(3); + axes_colors->InsertNextTuple3(255,0,0); + axes_colors->InsertNextTuple3(255,0,0); + axes_colors->InsertNextTuple3(0,255,0); + axes_colors->InsertNextTuple3(0,255,0); + axes_colors->InsertNextTuple3(0,0,255); + axes_colors->InsertNextTuple3(0,0,255); + + vtkSmartPointer axes_data = axes->GetOutput (); + axes_data->Update (); + axes_data->GetPointData ()->SetScalars (axes_colors); + + vtkSmartPointer axes_tubes = vtkSmartPointer::New (); + axes_tubes->SetInput (axes_data); + axes_tubes->SetRadius (axes->GetScaleFactor() / 50.0); + axes_tubes->SetNumberOfSides (6); + axes_tubes->Update(); + + ApplyPath::applyPath(axes_tubes->GetOutput(), appendFilter, path); } - // Set the color only for polyData + // Set the color for polyData vtkSmartPointer colors = vtkSmartPointer::New(); colors->SetNumberOfComponents(3); @@ -1103,36 +1117,19 @@ cv::viz::TrajectoryWidget::TrajectoryWidget(const std::vector &path, c double planesArray[24]; camera->GetFrustumPlanes(aspect_ratio, planesArray); - vtkSmartPointer mat_trans = vtkSmartPointer::New(); - mat_trans->Identity(); + vtkSmartPointer planes = vtkSmartPointer::New(); + planes->SetFrustumPlanes(planesArray); - for (vtkIdType i = 0; i < nr_points; ++i) - { - vtkSmartPointer planes = vtkSmartPointer::New(); - planes->SetFrustumPlanes(planesArray); - - vtkSmartPointer frustumSource = vtkSmartPointer::New(); - frustumSource->SetPlanes(planes); - frustumSource->Update(); - - // Extract the edges - vtkSmartPointer filter = vtkSmartPointer::New(); - filter->SetInput(frustumSource->GetOutput()); - filter->Update(); - - // Transform the default coordinate frame - vtkSmartPointer transform = vtkSmartPointer::New(); - transform->PreMultiply(); - vtkMatrix4x4::Multiply4x4(convertToVtkMatrix(path[i].matrix), mat_trans, mat_trans); - transform->SetMatrix(mat_trans); - - vtkSmartPointer transform_filter = vtkSmartPointer::New(); - transform_filter->SetInput(filter->GetOutput()); - transform_filter->SetTransform(transform); - transform_filter->Update(); - - appendFilter->AddInputConnection(transform_filter->GetOutputPort()); - } + vtkSmartPointer frustumSource = vtkSmartPointer::New(); + frustumSource->SetPlanes(planes); + frustumSource->Update(); + + // Extract the edges + vtkSmartPointer filter = vtkSmartPointer::New(); + filter->SetInput(frustumSource->GetOutput()); + filter->Update(); + + ApplyPath::applyPath(filter->GetOutput(), appendFilter, path); vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInput(appendFilter->GetOutput()); diff --git a/modules/viz/test/test_viz3d.cpp b/modules/viz/test/test_viz3d.cpp index 2fe9ca1c16..66f811884d 100644 --- a/modules/viz/test/test_viz3d.cpp +++ b/modules/viz/test/test_viz3d.cpp @@ -89,7 +89,9 @@ TEST(Viz_viz3d, accuracy) viz::Color color = viz::Color::black(); viz::LineWidget lw(Point3f(0, 0, 0), Point3f(4.f, 4.f,4.f), viz::Color::green()); - viz::PlaneWidget pw(Vec4f(0.0,1.0,2.0,3.0), 5.0); + viz::PlaneWidget pw(Vec4f(0.0,1.0,2.0,3.0)); + viz::PlaneWidget pw2(Vec4f(0.0,1.0,2.0,3.0), 2.0, viz::Color::red()); + viz::PlaneWidget pw3(Vec4f(0.0,1.0,2.0,3.0), 3.0, viz::Color::blue()); viz::SphereWidget sw(Point3f(0, 0, 0), 0.2); viz::ArrowWidget aw(Point3f(0, 0, 0), Point3f(1, 1, 1), 0.01, viz::Color::red()); viz::CircleWidget cw(Point3f(0, 0, 0), 0.5, 0.01, viz::Color::green()); @@ -101,8 +103,10 @@ TEST(Viz_viz3d, accuracy) viz::CloudWidget pcw2(cloud, viz::Color::magenta()); // viz.showWidget("line", lw); -// viz.showWidget("plane", pw); - viz.showWidget("sphere", sw); + viz.showWidget("plane", pw); + viz.showWidget("plane2", pw2); + viz.showWidget("plane3", pw3); +// viz.showWidget("sphere", sw); // viz.showWidget("arrow", aw); // viz.showWidget("circle", cw); // viz.showWidget("cylinder", cyw); @@ -145,16 +149,33 @@ TEST(Viz_viz3d, accuracy) viz::CameraPositionWidget cpw(Vec3f(0.5, 0.5, 3.0), Vec3f(0.0,0.0,0.0), Vec3f(0.0,-1.0,0.0), 0.5); viz::CameraPositionWidget cpw2(0.5); viz::CameraPositionWidget frustum(K, 2.0, viz::Color::green()); +// viz::CameraPositionWidget frustum2(K, 4.0, viz::Color::red()); viz::CameraPositionWidget frustum2(K, 4.0, viz::Color::red()); + viz::CameraPositionWidget frustum3(Vec2f(CV_PI, CV_PI/2), 4.0); viz::Text3DWidget t3w1("Camera1", Point3f(0.4, 0.6, 3.0), 0.1); viz::Text3DWidget t3w2("Camera2", Point3f(0,0,0), 0.1); - viz.showWidget("CameraPositionWidget", cpw); - viz.showWidget("CameraPositionWidget2", cpw2, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); - viz.showWidget("camera_label", t3w1); - viz.showWidget("camera_label2", t3w2, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); - viz.showWidget("frustrum", frustum, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); - viz.showWidget("frustrum2", frustum2, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); +// viz.showWidget("CameraPositionWidget", cpw); +// viz.showWidget("CameraPositionWidget2", cpw2, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); +// viz.showWidget("camera_label", t3w1); +// viz.showWidget("camera_label2", t3w2, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); +// viz.showWidget("frustrum", frustum, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); +// viz.showWidget("frustrum2", frustum2, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); +// viz.showWidget("frustum3", frustum3, Affine3f(0.524, 0, 0, Vec3f(-1.0, 0.5, 0.5))); + + std::vector trajectory; + + trajectory.push_back(Affine3f().translate(Vec3f(0.5,0.5,0.5))); + trajectory.push_back(Affine3f().translate(Vec3f(1.0,0.0,0.0))); + trajectory.push_back(Affine3f().translate(Vec3f(2.0,0.5,0.0))); + trajectory.push_back(Affine3f(0.5, 0.0, 0.0, Vec3f(1.0,0.0,1.0))); +// + viz.showWidget("trajectory1", viz::TrajectoryWidget(trajectory, viz::Color(0,255,255), true, 0.5)); + viz.showWidget("trajectory2", viz::TrajectoryWidget(trajectory, K, 1.0, viz::Color(255,0,255))); + + + +// viz.showWidget("trajectory1", viz::TrajectoryWidget(trajectory/*, viz::Color::yellow()*/)); // viz.showWidget("CameraPositionWidget2", cpw2); // viz.showWidget("CameraPositionWidget3", cpw3);