1 /**
2 * @file creating_widgets.cpp
3 * @brief Creating custom widgets using VTK
4 * @author Ozan Cagri Tonkal
5 */
6
7 #include <opencv2/viz.hpp>
8 #include <opencv2/viz/widget_accessor.hpp>
9 #include <iostream>
10
11 #include <vtkPoints.h>
12 #include <vtkTriangle.h>
13 #include <vtkCellArray.h>
14 #include <vtkPolyData.h>
15 #include <vtkPolyDataMapper.h>
16 #include <vtkIdList.h>
17 #include <vtkActor.h>
18 #include <vtkProp.h>
19
20 using namespace cv;
21 using namespace std;
22
23 /**
24 * @function help
25 * @brief Display instructions to use this tutorial program
26 */
help()27 void help()
28 {
29 cout
30 << "--------------------------------------------------------------------------" << endl
31 << "This program shows how to create a custom widget. You can create your own "
32 << "widgets by extending Widget2D/Widget3D, and with the help of WidgetAccessor." << endl
33 << "Usage:" << endl
34 << "./creating_widgets" << endl
35 << endl;
36 }
37
38 /**
39 * @class TriangleWidget
40 * @brief Defining our own 3D Triangle widget
41 */
42 class WTriangle : public viz::Widget3D
43 {
44 public:
45 WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color = viz::Color::white());
46 };
47
48 /**
49 * @function TriangleWidget::TriangleWidget
50 * @brief Constructor
51 */
WTriangle(const Point3f & pt1,const Point3f & pt2,const Point3f & pt3,const viz::Color & color)52 WTriangle::WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color)
53 {
54 // Create a triangle
55 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
56 points->InsertNextPoint(pt1.x, pt1.y, pt1.z);
57 points->InsertNextPoint(pt2.x, pt2.y, pt2.z);
58 points->InsertNextPoint(pt3.x, pt3.y, pt3.z);
59
60 vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
61 triangle->GetPointIds()->SetId(0,0);
62 triangle->GetPointIds()->SetId(1,1);
63 triangle->GetPointIds()->SetId(2,2);
64
65 vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
66 cells->InsertNextCell(triangle);
67
68 // Create a polydata object
69 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
70
71 // Add the geometry and topology to the polydata
72 polyData->SetPoints(points);
73 polyData->SetPolys(cells);
74
75 // Create mapper and actor
76 vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
77 #if VTK_MAJOR_VERSION <= 5
78 mapper->SetInput(polyData);
79 #else
80 mapper->SetInputData(polyData);
81 #endif
82
83 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
84 actor->SetMapper(mapper);
85
86 // Store this actor in the widget in order that visualizer can access it
87 viz::WidgetAccessor::setProp(*this, actor);
88
89 // Set the color of the widget. This has to be called after WidgetAccessor.
90 setColor(color);
91 }
92
93 /**
94 * @function main
95 */
main()96 int main()
97 {
98 help();
99
100 /// Create a window
101 viz::Viz3d myWindow("Creating Widgets");
102
103 /// Create a triangle widget
104 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());
105
106 /// Show widget in the visualizer window
107 myWindow.showWidget("TRIANGLE", tw);
108
109 /// Start event loop
110 myWindow.spin();
111
112 return 0;
113 }
114