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