Goal
In this tutorial you will learn how to
- 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.
21 <<
"--------------------------------------------------------------------------" << endl
22 <<
"This program shows how to visualize a cube rotated around (1,1,1) and shifted "
23 <<
"using Rodrigues vector." << endl
25 <<
"./widget_pose" << endl
45 myWindow.showWidget(
"Line Widget", axis);
50 myWindow.showWidget(
"Cube Widget", cube_widget);
54 float translation_phase = 0.0, translation = 0.0;
55 while(!myWindow.wasStopped())
59 rot_vec.
at<
float>(0,0) += CV_PI * 0.01f;
60 rot_vec.
at<
float>(0,1) += CV_PI * 0.01f;
61 rot_vec.
at<
float>(0,2) += CV_PI * 0.01f;
64 translation_phase += CV_PI * 0.01f;
65 translation =
sin(translation_phase);
71 Affine3f pose(rot_mat,
Vec3f(translation, translation, translation));
73 myWindow.setWidgetPose(
"Cube Widget", pose);
75 myWindow.spinOnce(1,
true);
void Rodrigues(InputArray src, OutputArray dst, OutputArray jacobian=noArray())
Converts a rotation matrix to a rotation vector or vice versa.
__device__ __forceinline__ float1 sin(const uchar1 &a)
Definition: vec_math.hpp:285
Point3_< float > Point3f
Definition: types.hpp:247
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
Vec< float, 3 > Vec3f
Definition: matx.hpp:376
Compond widgets.
Definition: widgets.hpp:504
The Viz3d class represents a 3D visualizer window. This class is implicitly shared. :
Definition: viz3d.hpp:67
Simple widgets.
Definition: widgets.hpp:232
This 3D Widget defines a cube.
Definition: widgets.hpp:369
#define CV_32F
Definition: cvdef.h:106
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
_Tp & at(int i0=0)
Returns a reference to the specified array element.
Affine transform.
Definition: affine.hpp:61
Explanation
Here is the general structure of the program:
- Create a visualization window.
viz::Viz3d myWindow("Coordinate Frame");
- Show coordinate axes in the window using CoordinateSystemWidget.
myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
- Display a line representing the axis (1,1,1).
myWindow.showWidget("Line Widget", axis);
- Construct a cube.
viz::WCube cube_widget(
Point3f(0.5,0.5,0.0),
Point3f(0.0,0.0,-0.5),
true, viz::Color::blue());
myWindow.showWidget("Cube Widget", cube_widget);
- Create rotation matrix from rodrigues vector
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;
- Use Affine3f to set pose of the cube.
Affine3f pose(rot_mat,
Vec3f(translation, translation, translation));
myWindow.setWidgetPose("Cube Widget", pose);
- Animate the rotation using wasStopped and spinOnce
while(!myWindow.wasStopped())
{
...
myWindow.spinOnce(1, true);
}
Results
Here is the result of the program.