OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Creating Widgets

Goal

In this tutorial you will learn how to

Code

You can download the code from here.

1 
7 #include <opencv2/viz.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 
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 
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 
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 
96 int main()
97 {
98  help();
99 
101  viz::Viz3d myWindow("Creating Widgets");
102 
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 
107  myWindow.showWidget("TRIANGLE", tw);
108 
110  myWindow.spin();
111 
112  return 0;
113 }
Base class of all 3D widgets.
Definition: widgets.hpp:178
static Color white()
Template class for 3D points specified by its coordinates x, y and z.
Definition: types.hpp:218
_Tp y
Definition: types.hpp:243
Point3_< float > Point3f
Definition: types.hpp:247
_Tp x
Definition: types.hpp:243
This class a represents BGR color.
Definition: types.hpp:63
static Color red()
The Viz3d class represents a 3D visualizer window. This class is implicitly shared. :
Definition: viz3d.hpp:67
static void setProp(Widget &widget, vtkSmartPointer< vtkProp > prop)
Sets vtkProp of a given widget.
_Tp z
Definition: types.hpp:243
Definition: gr_skig.hpp:63
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67

Explanation

Here is the general structure of the program:

Results

Here is the result of the program.

red_triangle.png