widget_pose.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file widget_pose.cpp
  3. * @brief Setting pose of a widget
  4. * @author Ozan Cagri Tonkal
  5. */
  6. #include <opencv2/viz.hpp>
  7. #include <opencv2/calib3d.hpp>
  8. #include <iostream>
  9. using namespace cv;
  10. using namespace std;
  11. /**
  12. * @function help
  13. * @brief Display instructions to use this tutorial program
  14. */
  15. static void help()
  16. {
  17. cout
  18. << "--------------------------------------------------------------------------" << endl
  19. << "This program shows how to visualize a cube rotated around (1,1,1) and shifted "
  20. << "using Rodrigues vector." << endl
  21. << "Usage:" << endl
  22. << "./widget_pose" << endl
  23. << endl;
  24. }
  25. /**
  26. * @function main
  27. */
  28. int main()
  29. {
  30. help();
  31. /// Create a window
  32. viz::Viz3d myWindow("Coordinate Frame");
  33. /// Add coordinate axes
  34. myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
  35. /// Add line to represent (1,1,1) axis
  36. viz::WLine axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f));
  37. axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);
  38. myWindow.showWidget("Line Widget", axis);
  39. /// Construct a cube widget
  40. viz::WCube cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue());
  41. cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);
  42. myWindow.showWidget("Cube Widget", cube_widget);
  43. /// Rodrigues vector
  44. Mat rot_vec = Mat::zeros(1,3,CV_32F);
  45. float translation_phase = 0.0, translation = 0.0;
  46. rot_vec.at<float>(0, 0) += (float)CV_PI * 0.01f;
  47. rot_vec.at<float>(0, 1) += (float)CV_PI * 0.01f;
  48. rot_vec.at<float>(0, 2) += (float)CV_PI * 0.01f;
  49. /// Shift on (1,1,1)
  50. translation_phase += (float)CV_PI * 0.01f;
  51. translation = sin(translation_phase);
  52. Mat rot_mat;
  53. Rodrigues(rot_vec, rot_mat);
  54. cout << "rot_mat = " << rot_mat << endl;
  55. /// Construct pose
  56. Affine3f pose(rot_mat, Vec3f(translation, translation, translation));
  57. Affine3f pose2(pose.matrix);
  58. cout << "pose = " << pose.matrix << endl;
  59. cout << "pose = " << pose2.matrix << endl;
  60. while(!myWindow.wasStopped())
  61. {
  62. /* Rotation using rodrigues */
  63. /// Rotate around (1,1,1)
  64. rot_vec.at<float>(0,0) += (float)CV_PI * 0.01f;
  65. rot_vec.at<float>(0,1) += (float)CV_PI * 0.01f;
  66. rot_vec.at<float>(0,2) += (float)CV_PI * 0.01f;
  67. /// Shift on (1,1,1)
  68. translation_phase += (float)CV_PI * 0.01f;
  69. translation = sin(translation_phase);
  70. Mat rot_mat1;
  71. Rodrigues(rot_vec, rot_mat1);
  72. /// Construct pose
  73. Affine3f pose1(rot_mat1, Vec3f(translation, translation, translation));
  74. myWindow.setWidgetPose("Cube Widget", pose1);
  75. myWindow.spinOnce(1, true);
  76. }
  77. return 0;
  78. }