From b032b4dded5f62e8893737166a0a58e31b71c5c5 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Thu, 22 Aug 2013 19:42:19 +0200 Subject: [PATCH] move frompolyfile to widget class as static method, remove addpolygon and its alikes --- modules/viz/include/opencv2/viz/viz3d.hpp | 7 - modules/viz/include/opencv2/viz/widgets.hpp | 2 + modules/viz/src/shape_widgets.cpp | 1 - modules/viz/src/viz3d.cpp | 22 +- modules/viz/src/viz3d_impl.cpp | 609 ---------------------------- modules/viz/src/viz3d_impl.hpp | 43 -- modules/viz/src/widget.cpp | 40 ++ 7 files changed, 43 insertions(+), 681 deletions(-) diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index 96f999b..c27b08d 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -27,13 +27,6 @@ namespace cv void setBackgroundColor(const Color& color = Color::black()); - //to refactor - bool addPolygonMesh(const Mesh3d& mesh, const String& id = "polygon"); - bool updatePolygonMesh(const Mesh3d& mesh, const String& id = "polygon"); - bool addPolylineFromPolygonMesh (const Mesh3d& mesh, const String& id = "polyline"); - bool addPolygon(const Mat& cloud, const Color& color, const String& id = "polygon"); - - void showWidget(const String &id, const Widget &widget, const Affine3f &pose = Affine3f::Identity()); void removeWidget(const String &id); Widget getWidget(const String &id) const; diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index c746330..8ad9b3d 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -16,6 +16,8 @@ namespace cv Widget(); Widget(const Widget &other); Widget& operator =(const Widget &other); + + static Widget fromPlyFile(const String &file_name); ~Widget(); diff --git a/modules/viz/src/shape_widgets.cpp b/modules/viz/src/shape_widgets.cpp index 050b978..cb66e80 100644 --- a/modules/viz/src/shape_widgets.cpp +++ b/modules/viz/src/shape_widgets.cpp @@ -969,7 +969,6 @@ cv::viz::CameraPositionWidget::CameraPositionWidget(const Matx33f &K, const Mat // Create a camera vtkSmartPointer camera = vtkSmartPointer::New(); - float f_x = K(0,0); float f_y = K(1,1); float c_y = K(1,2); float aspect_ratio = float(image.cols)/float(image.rows); diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index bb88e13..696976d 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -44,26 +44,6 @@ void cv::viz::Viz3d::release() void cv::viz::Viz3d::setBackgroundColor(const Color& color) { impl_->setBackgroundColor(color); } -bool cv::viz::Viz3d::addPolygonMesh (const Mesh3d& mesh, const String& id) -{ - return impl_->addPolygonMesh(mesh, Mat(), id); -} - -bool cv::viz::Viz3d::updatePolygonMesh (const Mesh3d& mesh, const String& id) -{ - return impl_->updatePolygonMesh(mesh, Mat(), id); -} - -bool cv::viz::Viz3d::addPolylineFromPolygonMesh (const Mesh3d& mesh, const String& id) -{ - return impl_->addPolylineFromPolygonMesh(mesh, id); -} - -bool cv::viz::Viz3d::addPolygon(const Mat& cloud, const Color& color, const String& id) -{ - return impl_->addPolygon(cloud, color, id); -} - void cv::viz::Viz3d::spin() { impl_->spin(); } void cv::viz::Viz3d::spinOnce (int time, bool force_redraw) { impl_->spinOnce(time, force_redraw); } bool cv::viz::Viz3d::wasStopped() const { return impl_->wasStopped(); } @@ -94,4 +74,4 @@ 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(); } \ No newline at end of file +cv::String cv::viz::Viz3d::getWindowName() const { return impl_->getWindowName(); } diff --git a/modules/viz/src/viz3d_impl.cpp b/modules/viz/src/viz3d_impl.cpp index 12a3de1..f141705 100644 --- a/modules/viz/src/viz3d_impl.cpp +++ b/modules/viz/src/viz3d_impl.cpp @@ -332,29 +332,6 @@ bool cv::viz::Viz3d::VizImpl::setPointCloudRenderingProperties (int property, do } ///////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::setPointCloudSelected (const bool selected, 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); - if (selected) - { - actor->GetProperty ()->EdgeVisibilityOn (); - actor->GetProperty ()->SetEdgeColor (1.0, 0.0, 0.0); - actor->Modified (); - } - else - { - actor->GetProperty ()->EdgeVisibilityOff (); - actor->Modified (); - } - - 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); @@ -618,148 +595,6 @@ void cv::viz::Viz3d::VizImpl::resetCameraViewpoint (const std::string &id) renderer_->Render (); } -//////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::addModelFromPolyData (vtkSmartPointer polydata, const std::string & id) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - { - std::cout << "[addModelFromPolyData] A shape with id <" << id << "> already exists! Please choose a different id and retry." << std::endl; - return (false); - } - - vtkSmartPointer actor; - createActorFromVTKDataSet (polydata, actor); - actor->GetProperty ()->SetRepresentationToWireframe (); - renderer_->AddActor(actor); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = actor; - return (true); -} - -//////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::addModelFromPolyData (vtkSmartPointer polydata, vtkSmartPointer transform, const std::string & id) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - { - std::cout << "[addModelFromPolyData] A shape with id <"< already exists! Please choose a different id and retry." << std::endl; - return (false); - } - - vtkSmartPointer trans_filter = vtkSmartPointer::New (); - trans_filter->SetTransform (transform); - trans_filter->SetInput (polydata); - trans_filter->Update(); - - // Create an Actor - vtkSmartPointer actor; - createActorFromVTKDataSet (trans_filter->GetOutput (), actor); - actor->GetProperty ()->SetRepresentationToWireframe (); - renderer_->AddActor(actor); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = actor; - return (true); -} - - -//////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::addModelFromPLYFile (const std::string &filename, const std::string &id) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - return std::cout << "[addModelFromPLYFile] A shape with id <"< already exists! Please choose a different id and retry.." << std::endl, false; - - vtkSmartPointer reader = vtkSmartPointer::New (); - reader->SetFileName (filename.c_str ()); - - vtkSmartPointer actor; - createActorFromVTKDataSet (reader->GetOutput (), actor); - actor->GetProperty ()->SetRepresentationToWireframe (); - renderer_->AddActor(actor); - - (*shape_actor_map_)[id] = actor; - return true; -} - -//////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::addModelFromPLYFile (const std::string &filename, vtkSmartPointer transform, const std::string &id) -{ - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - return std::cout << "[addModelFromPLYFile] A shape with id <"< already exists! Please choose a different id and retry." << std::endl, false; - - vtkSmartPointer reader = vtkSmartPointer::New (); - reader->SetFileName (filename.c_str ()); - - vtkSmartPointer trans_filter = vtkSmartPointer::New (); - trans_filter->SetTransform (transform); - trans_filter->SetInputConnection (reader->GetOutputPort ()); - - vtkSmartPointer actor; - createActorFromVTKDataSet (trans_filter->GetOutput (), actor); - actor->GetProperty ()->SetRepresentationToWireframe (); - renderer_->AddActor(actor); - - (*shape_actor_map_)[id] = actor; - return (true); -} - -bool cv::viz::Viz3d::VizImpl::addPolylineFromPolygonMesh (const Mesh3d& /*mesh*/, const std::string &/*id*/) -{ -// CV_Assert(mesh.cloud.rows == 1 && mesh.cloud.type() == CV_32FC3); -// -// ShapeActorMap::iterator am_it = shape_actor_map_->find (id); -// if (am_it != shape_actor_map_->end ()) -// return std::cout << "[addPolylineFromPolygonMesh] A shape with id <"<< id << "> already exists! Please choose a different id and retry.\n" << std::endl, false; -// -// vtkSmartPointer poly_points = vtkSmartPointer::New (); -// poly_points->SetNumberOfPoints (mesh.cloud.size().area()); -// -// const cv::Point3f *cdata = mesh.cloud.ptr(); -// for (int i = 0; i < mesh.cloud.cols; ++i) -// poly_points->InsertPoint (i, cdata[i].x, cdata[i].y,cdata[i].z); -// -// -// // Create a cell array to store the lines in and add the lines to it -// vtkSmartPointer cells = vtkSmartPointer::New (); -// vtkSmartPointer polyData; -// allocVtkPolyData (polyData); -// -// for (size_t i = 0; i < mesh.polygons.size (); i++) -// { -// vtkSmartPointer polyLine = vtkSmartPointer::New(); -// polyLine->GetPointIds()->SetNumberOfIds(mesh.polygons[i].vertices.size()); -// for(unsigned int k = 0; k < mesh.polygons[i].vertices.size(); k++) -// { -// polyLine->GetPointIds()->SetId(k,mesh. polygons[i].vertices[k]); -// } -// -// cells->InsertNextCell (polyLine); -// } -// -// // Add the points to the dataset -// polyData->SetPoints (poly_points); -// -// // Add the lines to the dataset -// polyData->SetLines (cells); -// -// // Setup actor and mapper -// vtkSmartPointer < vtkPolyDataMapper > mapper = vtkSmartPointer::New (); -// mapper->SetInput (polyData); -// -// vtkSmartPointer actor = vtkSmartPointer::New (); -// actor->SetMapper (mapper); -// renderer_->AddActor(actor); -// -// // Save the pointer/ID pair to the global actor map -// (*shape_actor_map_)[id] = actor; - return (true); -} - - /////////////////////////////////////////////////////////////////////////////////// void cv::viz::Viz3d::VizImpl::setRepresentationToSurfaceForAllActors () { @@ -883,450 +718,6 @@ void cv::viz::Viz3d::VizImpl::setWindowPosition (int x, int y) { window_->SetPos void cv::viz::Viz3d::VizImpl::setWindowSize (int xw, int yw) { window_->SetSize (xw, yw); } cv::Size cv::viz::Viz3d::VizImpl::getWindowSize() const { return Size(window_->GetSize()[0], window_->GetSize()[1]); } -bool cv::viz::Viz3d::VizImpl::addPolygonMesh (const Mesh3d& /*mesh*/, const Mat& /*mask*/, const std::string &/*id*/) -{ -// CV_Assert(mesh.cloud.type() == CV_32FC3 && mesh.cloud.rows == 1 && !mesh.polygons.empty ()); -// CV_Assert(mesh.colors.empty() || (!mesh.colors.empty() && mesh.colors.size() == mesh.cloud.size() && mesh.colors.type() == CV_8UC3)); -// CV_Assert(mask.empty() || (!mask.empty() && mask.size() == mesh.cloud.size() && mask.type() == CV_8U)); -// -// if (cloud_actor_map_->find (id) != cloud_actor_map_->end ()) -// return std::cout << "[addPolygonMesh] A shape with id <" << id << "> already exists! Please choose a different id and retry." << std::endl, false; -// -// // int rgb_idx = -1; -// // std::vector fields; -// -// -// // rgb_idx = temp_viz::getFieldIndex (*cloud, "rgb", fields); -// // if (rgb_idx == -1) -// // rgb_idx = temp_viz::getFieldIndex (*cloud, "rgba", fields); -// -// vtkSmartPointer colors_array; -// #if 1 -// if (!mesh.colors.empty()) -// { -// colors_array = vtkSmartPointer::New (); -// colors_array->SetNumberOfComponents (3); -// colors_array->SetName ("Colors"); -// -// const unsigned char* data = mesh.colors.ptr(); -// -// //TODO check mask -// CV_Assert(mask.empty()); //because not implemented; -// -// for(int i = 0; i < mesh.colors.cols; ++i) -// colors_array->InsertNextTupleValue(&data[i*3]); -// -// // temp_viz::RGB rgb_data; -// // for (size_t i = 0; i < cloud->size (); ++i) -// // { -// // if (!isFinite (cloud->points[i])) -// // continue; -// // memcpy (&rgb_data, reinterpret_cast (&cloud->points[i]) + fields[rgb_idx].offset, sizeof (temp_viz::RGB)); -// // unsigned char color[3]; -// // color[0] = rgb_data.r; -// // color[1] = rgb_data.g; -// // color[2] = rgb_data.b; -// // colors->InsertNextTupleValue (color); -// // } -// } -// #endif -// -// // Create points from polyMesh.cloud -// vtkSmartPointer points = vtkSmartPointer::New (); -// vtkIdType nr_points = mesh.cloud.size().area(); -// -// points->SetNumberOfPoints (nr_points); -// -// -// // Get a pointer to the beginning of the data array -// float *data = static_cast (points->GetData ())->GetPointer (0); -// -// -// std::vector lookup; -// // If the dataset is dense (no NaNs) -// if (mask.empty()) -// { -// cv::Mat hdr(mesh.cloud.size(), CV_32FC3, (void*)data); -// mesh.cloud.copyTo(hdr); -// } -// else -// { -// lookup.resize (nr_points); -// -// const unsigned char *mdata = mask.ptr(); -// const cv::Point3f *cdata = mesh.cloud.ptr(); -// cv::Point3f* out = reinterpret_cast(data); -// -// int j = 0; // true point index -// for (int i = 0; i < nr_points; ++i) -// if(mdata[i]) -// { -// lookup[i] = j; -// out[j++] = cdata[i]; -// } -// nr_points = j; -// points->SetNumberOfPoints (nr_points); -// } -// -// // Get the maximum size of a polygon -// int max_size_of_polygon = -1; -// for (size_t i = 0; i < mesh.polygons.size (); ++i) -// if (max_size_of_polygon < static_cast (mesh.polygons[i].vertices.size ())) -// max_size_of_polygon = static_cast (mesh.polygons[i].vertices.size ()); -// -// vtkSmartPointer actor; -// -// if (mesh.polygons.size () > 1) -// { -// // Create polys from polyMesh.polygons -// vtkSmartPointer cell_array = vtkSmartPointer::New (); -// vtkIdType *cell = cell_array->WritePointer (mesh.polygons.size (), mesh.polygons.size () * (max_size_of_polygon + 1)); -// int idx = 0; -// if (lookup.size () > 0) -// { -// for (size_t i = 0; i < mesh.polygons.size (); ++i, ++idx) -// { -// size_t n_points = mesh.polygons[i].vertices.size (); -// *cell++ = n_points; -// //cell_array->InsertNextCell (n_points); -// for (size_t j = 0; j < n_points; j++, ++idx) -// *cell++ = lookup[mesh.polygons[i].vertices[j]]; -// //cell_array->InsertCellPoint (lookup[vertices[i].vertices[j]]); -// } -// } -// else -// { -// for (size_t i = 0; i < mesh.polygons.size (); ++i, ++idx) -// { -// size_t n_points = mesh.polygons[i].vertices.size (); -// *cell++ = n_points; -// //cell_array->InsertNextCell (n_points); -// for (size_t j = 0; j < n_points; j++, ++idx) -// *cell++ = mesh.polygons[i].vertices[j]; -// //cell_array->InsertCellPoint (vertices[i].vertices[j]); -// } -// } -// vtkSmartPointer polydata; -// allocVtkPolyData (polydata); -// cell_array->GetData ()->SetNumberOfValues (idx); -// cell_array->Squeeze (); -// polydata->SetStrips (cell_array); -// polydata->SetPoints (points); -// -// if (colors_array) -// polydata->GetPointData ()->SetScalars (colors_array); -// -// createActorFromVTKDataSet (polydata, actor, false); -// } -// else -// { -// vtkSmartPointer polygon = vtkSmartPointer::New (); -// size_t n_points = mesh.polygons[0].vertices.size (); -// polygon->GetPointIds ()->SetNumberOfIds (n_points - 1); -// -// if (lookup.size () > 0) -// { -// for (size_t j = 0; j < n_points - 1; ++j) -// polygon->GetPointIds ()->SetId (j, lookup[mesh.polygons[0].vertices[j]]); -// } -// else -// { -// for (size_t j = 0; j < n_points - 1; ++j) -// polygon->GetPointIds ()->SetId (j, mesh.polygons[0].vertices[j]); -// } -// vtkSmartPointer poly_grid; -// allocVtkUnstructuredGrid (poly_grid); -// poly_grid->Allocate (1, 1); -// poly_grid->InsertNextCell (polygon->GetCellType (), polygon->GetPointIds ()); -// poly_grid->SetPoints (points); -// poly_grid->Update (); -// if (colors_array) -// poly_grid->GetPointData ()->SetScalars (colors_array); -// -// createActorFromVTKDataSet (poly_grid, actor, false); -// } -// renderer_->AddActor (actor); -// actor->GetProperty ()->SetRepresentationToSurface (); -// // Backface culling renders the visualization slower, but guarantees that we see all triangles -// actor->GetProperty ()->BackfaceCullingOff (); -// actor->GetProperty ()->SetInterpolationToFlat (); -// actor->GetProperty ()->EdgeVisibilityOff (); -// actor->GetProperty ()->ShadingOff (); -// -// // Save the pointer/ID pair to the global actor map -// (*cloud_actor_map_)[id].actor = actor; -// //if (vertices.size () > 1) -// // (*cloud_actor_map_)[id].cells = static_cast(actor->GetMapper ())->GetInput ()->GetVerts ()->GetData (); -// -// const Eigen::Vector4f& sensor_origin = Eigen::Vector4f::Zero (); -// const Eigen::Quaternion& sensor_orientation = Eigen::Quaternionf::Identity (); -// -// // Save the viewpoint transformation matrix to the global actor map -// vtkSmartPointer transformation = vtkSmartPointer::New(); -// convertToVtkMatrix (sensor_origin, sensor_orientation, transformation); -// (*cloud_actor_map_)[id].viewpoint_transformation_ = transformation; -// -// return (true); -return true; -} - - -bool cv::viz::Viz3d::VizImpl::updatePolygonMesh (const Mesh3d& /*mesh*/, const cv::Mat& /*mask*/, const std::string &/*id*/) -{ -// CV_Assert(mesh.cloud.type() == CV_32FC3 && mesh.cloud.rows == 1 && !mesh.polygons.empty ()); -// CV_Assert(mesh.colors.empty() || (!mesh.colors.empty() && mesh.colors.size() == mesh.cloud.size() && mesh.colors.type() == CV_8UC3)); -// CV_Assert(mask.empty() || (!mask.empty() && mask.size() == mesh.cloud.size() && mask.type() == CV_8U)); -// -// // Check to see if this ID entry already exists (has it been already added to the visualizer?) -// CloudActorMap::iterator am_it = cloud_actor_map_->find (id); -// if (am_it == cloud_actor_map_->end ()) -// return (false); -// -// // Get the current poly data -// vtkSmartPointer polydata = static_cast(am_it->second.actor->GetMapper ())->GetInput (); -// if (!polydata) -// return (false); -// vtkSmartPointer cells = polydata->GetStrips (); -// if (!cells) -// return (false); -// vtkSmartPointer points = polydata->GetPoints (); -// // Copy the new point array in -// vtkIdType nr_points = mesh.cloud.size().area(); -// points->SetNumberOfPoints (nr_points); -// -// // Get a pointer to the beginning of the data array -// float *data = (static_cast (points->GetData ()))->GetPointer (0); -// -// int ptr = 0; -// std::vector lookup; -// // If the dataset is dense (no NaNs) -// if (mask.empty()) -// { -// cv::Mat hdr(mesh.cloud.size(), CV_32FC3, (void*)data); -// mesh.cloud.copyTo(hdr); -// -// } -// else -// { -// lookup.resize (nr_points); -// -// const unsigned char *mdata = mask.ptr(); -// const cv::Point3f *cdata = mesh.cloud.ptr(); -// cv::Point3f* out = reinterpret_cast(data); -// -// int j = 0; // true point index -// for (int i = 0; i < nr_points; ++i) -// if(mdata[i]) -// { -// lookup[i] = j; -// out[j++] = cdata[i]; -// } -// nr_points = j; -// points->SetNumberOfPoints (nr_points);; -// } -// -// // Update colors -// vtkUnsignedCharArray* colors_array = vtkUnsignedCharArray::SafeDownCast (polydata->GetPointData ()->GetScalars ()); -// -// if (!mesh.colors.empty() && colors_array) -// { -// if (mask.empty()) -// { -// const unsigned char* data = mesh.colors.ptr(); -// for(int i = 0; i < mesh.colors.cols; ++i) -// colors_array->InsertNextTupleValue(&data[i*3]); -// } -// else -// { -// const unsigned char* color = mesh.colors.ptr(); -// const unsigned char* mdata = mask.ptr(); -// -// int j = 0; -// for(int i = 0; i < mesh.colors.cols; ++i) -// if (mdata[i]) -// colors_array->SetTupleValue (j++, &color[i*3]); -// -// } -// } -// -// // Get the maximum size of a polygon -// int max_size_of_polygon = -1; -// for (size_t i = 0; i < mesh.polygons.size (); ++i) -// if (max_size_of_polygon < static_cast (mesh.polygons[i].vertices.size ())) -// max_size_of_polygon = static_cast (mesh.polygons[i].vertices.size ()); -// -// // Update the cells -// cells = vtkSmartPointer::New (); -// vtkIdType *cell = cells->WritePointer (mesh.polygons.size (), mesh.polygons.size () * (max_size_of_polygon + 1)); -// int idx = 0; -// if (lookup.size () > 0) -// { -// for (size_t i = 0; i < mesh.polygons.size (); ++i, ++idx) -// { -// size_t n_points = mesh.polygons[i].vertices.size (); -// *cell++ = n_points; -// for (size_t j = 0; j < n_points; j++, cell++, ++idx) -// *cell = lookup[mesh.polygons[i].vertices[j]]; -// } -// } -// else -// { -// for (size_t i = 0; i < mesh.polygons.size (); ++i, ++idx) -// { -// size_t n_points = mesh.polygons[i].vertices.size (); -// *cell++ = n_points; -// for (size_t j = 0; j < n_points; j++, cell++, ++idx) -// *cell = mesh.polygons[i].vertices[j]; -// } -// } -// cells->GetData ()->SetNumberOfValues (idx); -// cells->Squeeze (); -// // Set the the vertices -// polydata->SetStrips (cells); -// polydata->Update (); - return (true); -} - -//////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::addArrow (const cv::Point3f &p1, const cv::Point3f &p2, const Color& color, bool display_length, const std::string &id) -{ - // Check to see if this ID entry already exists (has it been already added to the visualizer?) - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - return std::cout << "[addArrow] A shape with id <" << id << "> already exists! Please choose a different id and retry." << std::endl, false; - - // Create an Actor - vtkSmartPointer leader = vtkSmartPointer::New (); - leader->GetPositionCoordinate()->SetCoordinateSystemToWorld (); - leader->GetPositionCoordinate()->SetValue (p1.x, p1.y, p1.z); - leader->GetPosition2Coordinate()->SetCoordinateSystemToWorld (); - leader->GetPosition2Coordinate()->SetValue (p2.x, p2.y, p2.z); - leader->SetArrowStyleToFilled(); - leader->SetArrowPlacementToPoint2 (); - - if (display_length) - leader->AutoLabelOn (); - else - leader->AutoLabelOff (); - - Color c = vtkcolor(color); - leader->GetProperty ()->SetColor (c.val); - renderer_->AddActor (leader); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = leader; - return (true); -} -//////////////////////////////////////////////////////////////////////////////////////////// -bool cv::viz::Viz3d::VizImpl::addArrow (const cv::Point3f &p1, const cv::Point3f &p2, const Color& color_line, const Color& color_text, const std::string &id) -{ - // Check to see if this ID entry already exists (has it been already added to the visualizer?) - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - { - std::cout << "[addArrow] A shape with id <" << id << "> already exists! Please choose a different id and retry." << std::endl; - return (false); - } - - // Create an Actor - vtkSmartPointer leader = vtkSmartPointer::New (); - leader->GetPositionCoordinate ()->SetCoordinateSystemToWorld (); - leader->GetPositionCoordinate ()->SetValue (p1.x, p1.y, p1.z); - leader->GetPosition2Coordinate ()->SetCoordinateSystemToWorld (); - leader->GetPosition2Coordinate ()->SetValue (p2.x, p2.y, p2.z); - leader->SetArrowStyleToFilled (); - leader->AutoLabelOn (); - - Color ct = vtkcolor(color_text); - leader->GetLabelTextProperty()->SetColor(ct.val); - - Color cl = vtkcolor(color_line); - leader->GetProperty ()->SetColor (cl.val); - renderer_->AddActor (leader); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = leader; - return (true); -} - -bool cv::viz::Viz3d::VizImpl::addPolygon (const cv::Mat& cloud, const Color& color, const std::string &id) -{ - CV_Assert(cloud.type() == CV_32FC3 && cloud.rows == 1); - - vtkSmartPointer points = vtkSmartPointer::New (); - vtkSmartPointer polygon = vtkSmartPointer::New (); - - int total = cloud.size().area(); - points->SetNumberOfPoints (total); - polygon->GetPointIds ()->SetNumberOfIds (total); - - for (int i = 0; i < total; ++i) - { - cv::Point3f p = cloud.ptr()[i]; - points->SetPoint (i, p.x, p.y, p.z); - polygon->GetPointIds ()->SetId (i, i); - } - - vtkSmartPointer poly_grid; - allocVtkUnstructuredGrid (poly_grid); - poly_grid->Allocate (1, 1); - poly_grid->InsertNextCell (polygon->GetCellType (), polygon->GetPointIds ()); - poly_grid->SetPoints (points); - poly_grid->Update (); - - - ////////////////////////////////////////////////////// - vtkSmartPointer data = poly_grid; - - Color c = vtkcolor(color); - - // Check to see if this ID entry already exists (has it been already added to the visualizer?) - ShapeActorMap::iterator am_it = shape_actor_map_->find (id); - if (am_it != shape_actor_map_->end ()) - { - vtkSmartPointer all_data = vtkSmartPointer::New (); - - // Add old data - all_data->AddInput (reinterpret_cast ((vtkActor::SafeDownCast (am_it->second))->GetMapper ())->GetInput ()); - - // Add new data - vtkSmartPointer surface_filter = vtkSmartPointer::New (); - surface_filter->SetInput (vtkUnstructuredGrid::SafeDownCast (data)); - vtkSmartPointer poly_data = surface_filter->GetOutput (); - all_data->AddInput (poly_data); - - // Create an Actor - vtkSmartPointer actor; - createActorFromVTKDataSet (all_data->GetOutput (), actor); - actor->GetProperty ()->SetRepresentationToWireframe (); - actor->GetProperty ()->SetColor (c.val); - actor->GetMapper ()->ScalarVisibilityOff (); - actor->GetProperty ()->BackfaceCullingOff (); - - removeActorFromRenderer (am_it->second); - renderer_->AddActor (actor); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = actor; - } - else - { - // Create an Actor - vtkSmartPointer actor; - createActorFromVTKDataSet (data, actor); - actor->GetProperty ()->SetRepresentationToWireframe (); - actor->GetProperty ()->SetColor (c.val); - actor->GetMapper ()->ScalarVisibilityOff (); - actor->GetProperty ()->BackfaceCullingOff (); - renderer_->AddActor (actor); - - // Save the pointer/ID pair to the global actor map - (*shape_actor_map_)[id] = actor; - } - - return (true); -} - void cv::viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget, const Affine3f &pose) { WidgetActorMap::iterator wam_itr = widget_actor_map_->find(id); diff --git a/modules/viz/src/viz3d_impl.hpp b/modules/viz/src/viz3d_impl.hpp index 83e41e4..085a718 100644 --- a/modules/viz/src/viz3d_impl.hpp +++ b/modules/viz/src/viz3d_impl.hpp @@ -19,30 +19,11 @@ public: void removeAllWidgets(); - //to refactor - bool addPolygonMesh (const Mesh3d& mesh, const cv::Mat& mask, const String& id = "polygon"); - bool updatePolygonMesh (const Mesh3d& mesh, const cv::Mat& mask, const String& id = "polygon"); - bool addPolylineFromPolygonMesh (const Mesh3d& mesh, const String& id = "polyline"); - // 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); - /** \brief Set whether the point cloud is selected or not - * \param[in] selected whether the cloud is selected or not (true = selected) - * \param[in] id the point cloud object id (default: cloud) - */ - // probably should just remove - bool setPointCloudSelected (const bool selected, const String& id = "cloud" ); - - - - - - - - /** \brief Returns true when the user tried to close the window */ bool wasStopped () const { if (interactor_ != NULL) return (stopped_); else return true; } @@ -60,30 +41,6 @@ public: } } - - - - - - - - - - - // to refactor - bool addPolygon(const cv::Mat& cloud, const Color& color, const String& id = "polygon"); - bool addArrow (const Point3f& pt1, const Point3f& pt2, const Color& color, bool display_length, const String& id = "arrow"); - bool addArrow (const Point3f& pt1, const Point3f& pt2, const Color& color_line, const Color& color_text, const String& id = "arrow"); - - // Probably remove this - bool addModelFromPolyData (vtkSmartPointer polydata, const String& id = "PolyData"); - bool addModelFromPolyData (vtkSmartPointer polydata, vtkSmartPointer transform, const String& id = "PolyData"); - - // I think this should be moved to 'static Widget Widget::fromPlyFile(const String&)'; - bool addModelFromPLYFile (const String &filename, const String& id = "PLYModel"); - bool addModelFromPLYFile (const String &filename, vtkSmartPointer transform, const String& id = "PLYModel"); - - // to implement in Viz3d with shorter name void setRepresentationToSurfaceForAllActors(); void setRepresentationToPointsForAllActors(); diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 65c5b1a..a704097 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -54,6 +54,46 @@ void cv::viz::Widget::release() } } +cv::viz::Widget cv::viz::Widget::fromPlyFile(const String &file_name) +{ + vtkSmartPointer reader = vtkSmartPointer::New (); + reader->SetFileName (file_name.c_str ()); + + vtkSmartPointer data = reader->GetOutput(); + CV_Assert(data); + + vtkSmartPointer actor = vtkSmartPointer::New(); + + vtkSmartPointer mapper = vtkSmartPointer::New (); + mapper->SetInput (data); + + vtkSmartPointer scalars = data->GetPointData ()->GetScalars (); + if (scalars) + { + cv::Vec3d minmax(scalars->GetRange()); + mapper->SetScalarRange(minmax.val); + mapper->SetScalarModeToUsePointData (); + + // interpolation OFF, if data is a vtkPolyData that contains only vertices, ON for anything else. + vtkPolyData* polyData = vtkPolyData::SafeDownCast (data); + bool interpolation = (polyData && polyData->GetNumberOfCells () != polyData->GetNumberOfVerts ()); + + mapper->SetInterpolateScalarsBeforeMapping (interpolation); + mapper->ScalarVisibilityOn (); + } + mapper->ImmediateModeRenderingOff (); + + actor->SetNumberOfCloudPoints (int (std::max (1, data->GetNumberOfPoints () / 10))); + actor->GetProperty ()->SetInterpolationToFlat (); + actor->GetProperty ()->BackfaceCullingOn (); + + actor->SetMapper (mapper); + + Widget widget; + widget.impl_->prop = actor; + return widget; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// widget accessor implementaion -- 2.7.4