parent
fcf437cf69
commit
7116e22b52
4 changed files with 514 additions and 0 deletions
@ -0,0 +1,155 @@ |
|||||||
|
.. _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 |
||||||
|
==== |
||||||
|
|
||||||
|
You can download the code from `here <../../../../samples/cpp/tutorial_code/viz/creating_widgets.cpp>`_. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
#include <opencv2/viz.hpp> |
||||||
|
#include <opencv2/viz/widget_accessor.hpp> |
||||||
|
#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; |
||||||
|
|
||||||
|
/** |
||||||
|
* @class TriangleWidget |
||||||
|
* @brief Defining our own 3D Triangle widget |
||||||
|
*/ |
||||||
|
class TriangleWidget : public viz::Widget3D |
||||||
|
{ |
||||||
|
public: |
||||||
|
TriangleWidget(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color = viz::Color::white()); |
||||||
|
}; |
||||||
|
|
||||||
|
/** |
||||||
|
* @function TriangleWidget::TriangleWidget |
||||||
|
*/ |
||||||
|
TriangleWidget::TriangleWidget(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color) |
||||||
|
{ |
||||||
|
// 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); |
||||||
|
|
||||||
|
vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New(); |
||||||
|
triangle->GetPointIds()->SetId(0,0); |
||||||
|
triangle->GetPointIds()->SetId(1,1); |
||||||
|
triangle->GetPointIds()->SetId(2,2); |
||||||
|
|
||||||
|
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New(); |
||||||
|
cells->InsertNextCell(triangle); |
||||||
|
|
||||||
|
// Create a polydata object |
||||||
|
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New(); |
||||||
|
|
||||||
|
// Add the geometry and topology to the polydata |
||||||
|
polyData->SetPoints(points); |
||||||
|
polyData->SetPolys(cells); |
||||||
|
|
||||||
|
// Create mapper and actor |
||||||
|
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); |
||||||
|
mapper->SetInput(polyData); |
||||||
|
|
||||||
|
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); |
||||||
|
actor->SetMapper(mapper); |
||||||
|
|
||||||
|
// Store this actor in the widget in order that visualizer can access it |
||||||
|
viz::WidgetAccessor::setProp(*this, actor); |
||||||
|
|
||||||
|
// Set the color of the widget. This has to be called after WidgetAccessor. |
||||||
|
setColor(color); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @function main |
||||||
|
*/ |
||||||
|
int main() |
||||||
|
{ |
||||||
|
/// Create a window |
||||||
|
viz::Viz3d myWindow("Creating Widgets"); |
||||||
|
|
||||||
|
/// Create a triangle widget |
||||||
|
TriangleWidget 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()); |
||||||
|
|
||||||
|
/// Show widget in the visualizer window |
||||||
|
myWindow.showWidget("TRIANGLE", tw); |
||||||
|
|
||||||
|
/// Start event loop |
||||||
|
myWindow.spin(); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
Explanation |
||||||
|
=========== |
||||||
|
|
||||||
|
Here is the general structure of the program: |
||||||
|
|
||||||
|
* Extend Widget3D class to create a new 3D widget. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
class TriangleWidget : public viz::Widget3D |
||||||
|
{ |
||||||
|
public: |
||||||
|
TriangleWidget(const Point3f &pt1, const Point3f &pt2, const Point3f &pt3, const viz::Color & color = viz::Color::white()); |
||||||
|
}; |
||||||
|
|
||||||
|
* 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); |
||||||
|
|
||||||
|
* Set color of the widget. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
// Set the color of the widget. This has to be called after WidgetAccessor. |
||||||
|
setColor(color); |
||||||
|
|
||||||
|
* Construct a triangle widget and display it in the window. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Create a triangle widget |
||||||
|
TriangleWidget 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()); |
||||||
|
|
||||||
|
/// Show widget in the visualizer window |
||||||
|
myWindow.showWidget("TRIANGLE", tw); |
||||||
|
|
||||||
|
Results |
||||||
|
======= |
||||||
|
|
||||||
|
Here is the result of the program. |
||||||
|
|
||||||
|
.. image:: images/red_triangle.png |
||||||
|
:alt: Creating Widgets |
||||||
|
:align: center |
@ -0,0 +1,118 @@ |
|||||||
|
.. _launching_viz: |
||||||
|
|
||||||
|
Launching Viz |
||||||
|
************* |
||||||
|
|
||||||
|
Goal |
||||||
|
==== |
||||||
|
|
||||||
|
In this tutorial you will learn how to |
||||||
|
|
||||||
|
.. container:: enumeratevisibleitemswithsquare |
||||||
|
|
||||||
|
* Open a visualization window. |
||||||
|
* Access a window by its name. |
||||||
|
* Start event loop. |
||||||
|
* Start event loop for a given amount of time. |
||||||
|
|
||||||
|
Code |
||||||
|
==== |
||||||
|
|
||||||
|
You can download the code from `here <../../../../samples/cpp/tutorial_code/viz/launching_viz.cpp>`_. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
#include <opencv2/viz.hpp> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
using namespace cv; |
||||||
|
using namespace std; |
||||||
|
|
||||||
|
/** |
||||||
|
* @function main |
||||||
|
*/ |
||||||
|
int main() |
||||||
|
{ |
||||||
|
/// Create a window |
||||||
|
viz::Viz3d myWindow("Viz Demo"); |
||||||
|
|
||||||
|
/// Start event loop |
||||||
|
myWindow.spin(); |
||||||
|
|
||||||
|
/// Event loop is over when pressed q, Q, e, E |
||||||
|
cout << "First event loop is over" << endl; |
||||||
|
|
||||||
|
/// Access window via its name |
||||||
|
viz::Viz3d sameWindow = viz::get("Viz Demo"); |
||||||
|
|
||||||
|
/// Start event loop |
||||||
|
sameWindow.spin(); |
||||||
|
|
||||||
|
/// Event loop is over when pressed q, Q, e, E |
||||||
|
cout << "Second event loop is over" << endl; |
||||||
|
|
||||||
|
/// Event loop is over when pressed q, Q, e, E |
||||||
|
/// Start event loop once for 1 millisecond |
||||||
|
sameWindow.spinOnce(1, true); |
||||||
|
while(!sameWindow.wasStopped()) |
||||||
|
{ |
||||||
|
/// Interact with window |
||||||
|
|
||||||
|
/// Event loop for 1 millisecond |
||||||
|
sameWindow.spinOnce(1, true); |
||||||
|
} |
||||||
|
|
||||||
|
/// Once more event loop is stopped |
||||||
|
cout << "Last event loop is over" << endl; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
Explanation |
||||||
|
=========== |
||||||
|
|
||||||
|
Here is the general structure of the program: |
||||||
|
|
||||||
|
* Create a window. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Create a window |
||||||
|
viz::Viz3d myWindow("Viz Demo"); |
||||||
|
|
||||||
|
* Start event loop. This event loop will run until user terminates it by pressing **e**, **E**, **q**, **Q**. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Start event loop |
||||||
|
myWindow.spin(); |
||||||
|
|
||||||
|
* Access same window via its name. Since windows are implicitly shared, **sameWindow** is exactly the same with **myWindow**. If the name does not exist, a new window is created. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Access window via its name |
||||||
|
viz::Viz3d sameWindow = viz::get("Viz Demo"); |
||||||
|
|
||||||
|
* Start a controlled event loop. Once it starts, **wasStopped** is set to false. Inside the while loop, in each iteration, **spinOnce** is called to prevent event loop from completely stopping. Inside the while loop, user can execute other statements including those which interact with the window. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Event loop is over when pressed q, Q, e, E |
||||||
|
/// Start event loop once for 1 millisecond |
||||||
|
sameWindow.spinOnce(1, true); |
||||||
|
while(!sameWindow.wasStopped()) |
||||||
|
{ |
||||||
|
/// Interact with window |
||||||
|
|
||||||
|
/// Event loop for 1 millisecond |
||||||
|
sameWindow.spinOnce(1, true); |
||||||
|
} |
||||||
|
|
||||||
|
Results |
||||||
|
======= |
||||||
|
|
||||||
|
Here is the result of the program. |
||||||
|
|
||||||
|
.. image:: images/window_demo.png |
||||||
|
:alt: Launching Viz |
||||||
|
:align: center |
@ -0,0 +1,162 @@ |
|||||||
|
.. _widget_pose: |
||||||
|
|
||||||
|
Pose of a widget |
||||||
|
**************** |
||||||
|
|
||||||
|
Goal |
||||||
|
==== |
||||||
|
|
||||||
|
In this tutorial you will learn how to |
||||||
|
|
||||||
|
.. container:: enumeratevisibleitemswithsquare |
||||||
|
|
||||||
|
* Add widgets to the visualization window |
||||||
|
* Use Affine3 to set pose of a widget |
||||||
|
* Rotating and translating a widget along an axis |
||||||
|
|
||||||
|
Code |
||||||
|
==== |
||||||
|
|
||||||
|
You can download the code from `here <../../../../samples/cpp/tutorial_code/viz/widget_pose.cpp>`_. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
#include <opencv2/viz.hpp> |
||||||
|
#include <opencv2/calib3d.hpp> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
using namespace cv; |
||||||
|
using namespace std; |
||||||
|
|
||||||
|
/** |
||||||
|
* @function main |
||||||
|
*/ |
||||||
|
int main() |
||||||
|
{ |
||||||
|
/// Create a window |
||||||
|
viz::Viz3d myWindow("Coordinate Frame"); |
||||||
|
|
||||||
|
/// Add coordinate axes |
||||||
|
myWindow.showWidget("Coordinate Widget", viz::CoordinateSystemWidget()); |
||||||
|
|
||||||
|
/// Add line to represent (1,1,1) axis |
||||||
|
viz::LineWidget axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f)); |
||||||
|
axis.setRenderingProperty(viz::VIZ_LINE_WIDTH, 4.0); |
||||||
|
myWindow.showWidget("Line Widget", axis); |
||||||
|
|
||||||
|
/// Construct a cube widget |
||||||
|
viz::CubeWidget cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue()); |
||||||
|
cube_widget.setRenderingProperty(viz::VIZ_LINE_WIDTH, 4.0); |
||||||
|
|
||||||
|
/// Display widget (update if already displayed) |
||||||
|
myWindow.showWidget("Cube Widget", cube_widget); |
||||||
|
|
||||||
|
/// Rodrigues vector |
||||||
|
Mat rot_vec = Mat::zeros(1,3,CV_32F); |
||||||
|
float translation_phase = 0.0, translation = 0.0; |
||||||
|
while(!myWindow.wasStopped()) |
||||||
|
{ |
||||||
|
/* Rotation using rodrigues */ |
||||||
|
/// Rotate around (1,1,1) |
||||||
|
rot_vec.at<float>(0,0) += CV_PI * 0.01f; |
||||||
|
rot_vec.at<float>(0,1) += CV_PI * 0.01f; |
||||||
|
rot_vec.at<float>(0,2) += CV_PI * 0.01f; |
||||||
|
|
||||||
|
/// Shift on (1,1,1) |
||||||
|
translation_phase += CV_PI * 0.01f; |
||||||
|
translation = sin(translation_phase); |
||||||
|
|
||||||
|
Mat rot_mat; |
||||||
|
Rodrigues(rot_vec, rot_mat); |
||||||
|
|
||||||
|
/// Construct pose |
||||||
|
Affine3f pose(rot_mat, Vec3f(translation, translation, translation)); |
||||||
|
|
||||||
|
myWindow.setWidgetPose("Cube Widget", pose); |
||||||
|
|
||||||
|
myWindow.spinOnce(1, true); |
||||||
|
} |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
Explanation |
||||||
|
=========== |
||||||
|
|
||||||
|
Here is the general structure of the program: |
||||||
|
|
||||||
|
* Create a visualization window. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Create a window |
||||||
|
viz::Viz3d myWindow("Coordinate Frame"); |
||||||
|
|
||||||
|
* Show coordinate axes in the window using CoordinateSystemWidget. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Add coordinate axes |
||||||
|
myWindow.showWidget("Coordinate Widget", viz::CoordinateSystemWidget()); |
||||||
|
|
||||||
|
* Display a line representing the axis (1,1,1). |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Add line to represent (1,1,1) axis |
||||||
|
viz::LineWidget axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f)); |
||||||
|
axis.setRenderingProperty(viz::VIZ_LINE_WIDTH, 4.0); |
||||||
|
myWindow.showWidget("Line Widget", axis); |
||||||
|
|
||||||
|
* Construct a cube. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Construct a cube widget |
||||||
|
viz::CubeWidget cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue()); |
||||||
|
cube_widget.setRenderingProperty(viz::VIZ_LINE_WIDTH, 4.0); |
||||||
|
myWindow.showWidget("Cube Widget", cube_widget); |
||||||
|
|
||||||
|
* Create rotation matrix from rodrigues vector |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Rotate around (1,1,1) |
||||||
|
rot_vec.at<float>(0,0) += CV_PI * 0.01f; |
||||||
|
rot_vec.at<float>(0,1) += CV_PI * 0.01f; |
||||||
|
rot_vec.at<float>(0,2) += CV_PI * 0.01f; |
||||||
|
|
||||||
|
... |
||||||
|
|
||||||
|
Mat rot_mat; |
||||||
|
Rodrigues(rot_vec, rot_mat); |
||||||
|
|
||||||
|
* Use Affine3f to set pose of the cube. |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
/// Construct pose |
||||||
|
Affine3f pose(rot_mat, Vec3f(translation, translation, translation)); |
||||||
|
myWindow.setWidgetPose("Cube Widget", pose); |
||||||
|
|
||||||
|
* Animate the rotation using wasStopped and spinOnce |
||||||
|
|
||||||
|
.. code-block:: cpp |
||||||
|
|
||||||
|
while(!myWindow.wasStopped()) |
||||||
|
{ |
||||||
|
... |
||||||
|
|
||||||
|
myWindow.spinOnce(1, true); |
||||||
|
} |
||||||
|
|
||||||
|
Results |
||||||
|
======= |
||||||
|
|
||||||
|
Here is the result of the program. |
||||||
|
|
||||||
|
.. raw:: html |
||||||
|
|
||||||
|
<div align="center"> |
||||||
|
<iframe width="420" height="315" src="https://www.youtube.com/embed/Jo47zc6-hvI" frameborder="0" allowfullscreen></iframe> |
||||||
|
</div> |
@ -0,0 +1,79 @@ |
|||||||
|
/**
|
||||||
|
* @file creating_widgets.cpp |
||||||
|
* @brief Creating custom widgets using VTK |
||||||
|
* @author Ozan Cagri Tonkal |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <opencv2/viz.hpp> |
||||||
|
#include <opencv2/calib3d.hpp> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
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 visualize a cube rotated around (1,1,1) and shifted " |
||||||
|
<< "using Rodrigues matrix." << endl |
||||||
|
<< "Usage:" << endl |
||||||
|
<< "./coordinate_frame" << endl |
||||||
|
<< endl; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* @function main |
||||||
|
*/ |
||||||
|
int main() |
||||||
|
{ |
||||||
|
help(); |
||||||
|
|
||||||
|
/// Create a window
|
||||||
|
viz::Viz3d myWindow("Coordinate Frame"); |
||||||
|
|
||||||
|
/// Add coordinate axes
|
||||||
|
myWindow.showWidget("Coordinate Widget", viz::CoordinateSystemWidget()); |
||||||
|
|
||||||
|
/// Add line to represent (1,1,1) axis
|
||||||
|
viz::LineWidget axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f)); |
||||||
|
axis.setRenderingProperty(viz::LINE_WIDTH, 4.0); |
||||||
|
myWindow.showWidget("Line Widget", axis); |
||||||
|
|
||||||
|
/// Construct a cube widget
|
||||||
|
viz::CubeWidget cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue()); |
||||||
|
cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0); |
||||||
|
myWindow.showWidget("Cube Widget", cube_widget);
|
||||||
|
|
||||||
|
/// Rodrigues vector
|
||||||
|
Mat rot_vec = Mat::zeros(1,3,CV_32F); |
||||||
|
float translation_phase = 0.0, translation = 0.0; |
||||||
|
while(!myWindow.wasStopped()) |
||||||
|
{ |
||||||
|
/* Rotation using rodrigues */ |
||||||
|
/// Rotate around (1,1,1)
|
||||||
|
rot_vec.at<float>(0,0) += CV_PI * 0.01f; |
||||||
|
rot_vec.at<float>(0,1) += CV_PI * 0.01f; |
||||||
|
rot_vec.at<float>(0,2) += CV_PI * 0.01f; |
||||||
|
|
||||||
|
/// Shift on (1,1,1)
|
||||||
|
translation_phase += CV_PI * 0.01f; |
||||||
|
translation = sin(translation_phase); |
||||||
|
|
||||||
|
Mat rot_mat; |
||||||
|
Rodrigues(rot_vec, rot_mat);
|
||||||
|
|
||||||
|
/// Construct pose
|
||||||
|
Affine3f pose(rot_mat, Vec3f(translation, translation, translation)); |
||||||
|
|
||||||
|
myWindow.setWidgetPose("Cube Widget", pose); |
||||||
|
|
||||||
|
myWindow.spinOnce(1, true); |
||||||
|
} |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
Loading…
Reference in new issue