Explanation
- Extend Widget3d class to create a new 3D widget.
- Assign a VTK actor to the widget.
- Construct a triangle widget and display it in the window.
Code
#include <opencv2/viz/vizcore.hpp> #include <opencv2/viz/widget_accessor.hpp> #include <iostream> #include <vtkPoints.h> #include <vtkTriangle.h> #include <vtkCellArray.h> #include < vtkpolydata.h> #include <vtkPolyDataMapper.h> #include <vtkIdList.h> #include <vtkactor.h># Include <vtkprop.h>using namespace cv;using namespace std;/** * @class Wtriangle * @brief defining our own 3D Triang Le widget */class wtriangle:public viz::widget3d{public:wtriangle (const point3f &PT1, const point3f & ;p T2, const point3f &PT3, const viz::color & Color = Viz::color::white ());};/ * * @function wtriangle::wtriangle */wtriangle::wtriangle (const point3f &PT1, const point3f &PT2, const point3f & AMP;PT3, const Viz::color & Color) {//Create a triangle vtksmartpointer<vtkpoints> points = Vtksmartpoint Er<vtkpoints>::new (); Points->insertnextpoint (pt1.x, Pt1.y, pt1.z); Points->insertnexTpoint (pt2.x, Pt2.y, pt2.z); Points->insertnextpoint (pt3.x, Pt3.y, pt3.z); Vtksmartpointer<vtktriangle> triangle = vtksmartpointer<vtktriangle>::new (); Triangle->getpointids ()->setid (0,0); Triangle->getpointids ()->setid (); Triangle->getpointids ()->setid (2,2); Vtksmartpointer<vtkcellarray> cells = vtksmartpointer<vtkcellarray>::new (); Cells->insertnextcell (triangle); Create a Polydata object vtksmartpointer<vtkpolydata> polydata = Vtksmartpointer<vtkpolydata>::new (); Add the geometry and topology to the Polydata polydata->setpoints (points); Polydata->setpolys (cells); Create Mapper and actor vtksmartpointer<vtkpolydatamapper> mapper = Vtksmartpointer<vtkpolydatamapper>: : New (); #if vtk_major_version <= 5 mapper->setinput (polydata); #else mapper->setinputdata (polydata); #endif Vtksmartpointer<vtkactor> actor = VTKSMARTPOINTER<VTKACTOR>::new (); Actor->setmapper (mapper); Store This actor in the widgets in order that visualizer can access it Viz::widgetaccessor::setprop (*this, actor); Set the color of the widget. This have to is called after Widgetaccessor. SetColor (color);} /** * @function main */int main () {//Create a window viz::viz3d mywindow ("Creating Widgets"); Create a triangle widget wtriangle tw (point3f (0.0,0.0,0.0), point3f (1.0,1.0,1.0), point3f (0.0,1.0,0.0), Viz::color: : Red ()); Show widget in the visualizer window Mywindow.showwidget ("TRIANGLE", TW); Start event Loop Mywindow.spin (); return 0;}
OpenCV tutorials--creating Widgets