2013-09-07 21:44:26 +08:00
|
|
|
.. _creating_widgets:
|
|
|
|
|
|
|
|
Creating Widgets
|
|
|
|
****************
|
|
|
|
|
|
|
|
Goal
|
|
|
|
====
|
|
|
|
|
|
|
|
In this tutorial you will learn how to
|
|
|
|
|
|
|
|
.. container:: enumeratevisibleitemswithsquare
|
|
|
|
|
|
|
|
* Create your own widgets using WidgetAccessor and VTK.
|
|
|
|
* Show your widget in the visualization window.
|
|
|
|
|
|
|
|
Code
|
|
|
|
====
|
|
|
|
|
2013-09-18 02:17:13 +08:00
|
|
|
You can download the code from :download:`here <../../../../samples/cpp/tutorial_code/viz/creating_widgets.cpp>`.
|
2013-09-07 21:44:26 +08:00
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
#include <opencv2/viz.hpp>
|
2013-09-18 19:50:55 +08:00
|
|
|
#include <opencv2/viz/widget_accessor.hpp>
|
2013-09-07 21:44:26 +08:00
|
|
|
#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;
|
|
|
|
|
2014-11-27 18:23:24 +08:00
|
|
|
/*
|
2013-09-15 22:26:53 +08:00
|
|
|
* @class WTriangle
|
2013-09-07 21:44:26 +08:00
|
|
|
* @brief Defining our own 3D Triangle widget
|
|
|
|
*/
|
2013-09-15 22:26:53 +08:00
|
|
|
class WTriangle : public viz::Widget3D
|
2013-09-07 21:44:26 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-18 19:50:55 +08:00
|
|
|
WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color = viz::Color::white());
|
2013-09-07 21:44:26 +08:00
|
|
|
};
|
|
|
|
|
2014-11-27 18:23:24 +08:00
|
|
|
/*
|
2013-09-15 22:26:53 +08:00
|
|
|
* @function WTriangle::WTriangle
|
2013-09-07 21:44:26 +08:00
|
|
|
*/
|
2013-09-15 22:26:53 +08:00
|
|
|
WTriangle::WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color)
|
2013-09-07 21:44:26 +08:00
|
|
|
{
|
|
|
|
// Create a triangle
|
|
|
|
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<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);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
|
|
|
|
triangle->GetPointIds()->SetId(0,0);
|
|
|
|
triangle->GetPointIds()->SetId(1,1);
|
|
|
|
triangle->GetPointIds()->SetId(2,2);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
|
|
|
|
cells->InsertNextCell(triangle);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
// Create a polydata object
|
|
|
|
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
// Add the geometry and topology to the polydata
|
|
|
|
polyData->SetPoints(points);
|
|
|
|
polyData->SetPolys(cells);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
// Create mapper and actor
|
|
|
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
|
2013-09-09 00:55:41 +08:00
|
|
|
#if VTK_MAJOR_VERSION <= 5
|
2013-09-07 21:44:26 +08:00
|
|
|
mapper->SetInput(polyData);
|
2013-09-09 00:55:41 +08:00
|
|
|
#else
|
|
|
|
mapper->SetInputData(polyData);
|
|
|
|
#endif
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
|
|
|
|
actor->SetMapper(mapper);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
// Store this actor in the widget in order that visualizer can access it
|
|
|
|
viz::WidgetAccessor::setProp(*this, actor);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
// Set the color of the widget. This has to be called after WidgetAccessor.
|
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2014-11-27 18:23:24 +08:00
|
|
|
/*
|
2013-09-07 21:44:26 +08:00
|
|
|
* @function main
|
|
|
|
*/
|
|
|
|
int main()
|
2013-09-18 19:50:55 +08:00
|
|
|
{
|
2013-09-07 21:44:26 +08:00
|
|
|
/// Create a window
|
|
|
|
viz::Viz3d myWindow("Creating Widgets");
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
/// Create a triangle widget
|
2013-09-15 22:26:53 +08:00
|
|
|
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());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
/// Show widget in the visualizer window
|
|
|
|
myWindow.showWidget("TRIANGLE", tw);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
/// Start event loop
|
|
|
|
myWindow.spin();
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
Explanation
|
|
|
|
===========
|
|
|
|
|
|
|
|
Here is the general structure of the program:
|
|
|
|
|
|
|
|
* Extend Widget3D class to create a new 3D widget.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2013-09-15 22:26:53 +08:00
|
|
|
class WTriangle : public viz::Widget3D
|
2013-09-07 21:44:26 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-18 19:50:55 +08:00
|
|
|
WTriangle(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color = viz::Color::white());
|
2013-09-07 21:44:26 +08:00
|
|
|
};
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
* Assign a VTK actor to the widget.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
// Store this actor in the widget in order that visualizer can access it
|
|
|
|
viz::WidgetAccessor::setProp(*this, actor);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
* Set color of the widget.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
// Set the color of the widget. This has to be called after WidgetAccessor.
|
|
|
|
setColor(color);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
* Construct a triangle widget and display it in the window.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
|
|
|
/// Create a triangle widget
|
2013-09-15 22:26:53 +08:00
|
|
|
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());
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
/// Show widget in the visualizer window
|
|
|
|
myWindow.showWidget("TRIANGLE", tw);
|
2013-09-18 19:50:55 +08:00
|
|
|
|
2013-09-07 21:44:26 +08:00
|
|
|
Results
|
|
|
|
=======
|
|
|
|
|
|
|
|
Here is the result of the program.
|
|
|
|
|
|
|
|
.. image:: images/red_triangle.png
|
|
|
|
:alt: Creating Widgets
|
|
|
|
:align: center
|