creating_widgets.cpp 3.3 KB

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