opencv/samples/cpp/tutorial_code/viz/creating_widgets.cpp

123 lines
3.3 KiB
C++
Raw Normal View History

2013-09-06 02:53:57 +08:00
/**
* @file creating_widgets.cpp
* @brief Creating custom widgets using VTK
* @author Ozan Cagri Tonkal
*/
2016-06-29 21:35:48 +08:00
#ifndef USE_VTK
#include <iostream>
int main()
{
std::cout << "This sample requires direct compilation with VTK. Stop" << std::endl;
return 0;
}
#else
2013-09-06 02:53:57 +08:00
#include <opencv2/viz.hpp>
2013-09-18 19:50:55 +08:00
#include <opencv2/viz/widget_accessor.hpp>
2013-09-06 02:53:57 +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;
/**
* @function help
* @brief Display instructions to use this tutorial program
*/
void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to create a custom widget. You can create your own "
<< "widgets by extending Widget2D/Widget3D, and with the help of WidgetAccessor." << endl
<< "Usage:" << endl
<< "./creating_widgets" << endl
<< endl;
}
/**
* @class TriangleWidget
* @brief Defining our own 3D Triangle widget
*/
2013-09-15 22:26:53 +08:00
class WTriangle : public viz::Widget3D
2013-09-06 02:53:57 +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-06 02:53:57 +08:00
};
/**
* @function TriangleWidget::TriangleWidget
* @brief Constructor
*/
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-06 02:53:57 +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-06 02:53:57 +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-06 02:53:57 +08:00
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
cells->InsertNextCell(triangle);
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +08:00
// Create a polydata object
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +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-06 02:53:57 +08:00
// Create mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
2013-09-06 02:53:57 +08:00
mapper->SetInput(polyData);
#else
mapper->SetInputData(polyData);
#endif
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +08:00
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +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
// Set the color of the widget. This has to be called after WidgetAccessor.
setColor(color);
2013-09-06 02:53:57 +08:00
}
/**
* @function main
*/
int main()
{
help();
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +08:00
/// Create a window
viz::Viz3d myWindow("Creating Widgets");
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +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-06 02:53:57 +08:00
/// Show widget in the visualizer window
myWindow.showWidget("TRIANGLE", tw);
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +08:00
/// Start event loop
myWindow.spin();
2013-09-18 19:50:55 +08:00
2013-09-06 02:53:57 +08:00
return 0;
}
2016-06-29 21:35:48 +08:00
#endif