PointShape.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Include own headers
  2. #include "PointShape.hpp"
  3. // Include modules
  4. #include "OSGWidget.hpp"
  5. #include "defaults.hpp"
  6. #include "MainWindow.hpp"
  7. // Include dependencies
  8. #include <osgUtil/MeshOptimizers>
  9. #include "lib3mf_implicit.hpp"
  10. PointShape::PointShape(const osg::ref_ptr<osg::Group> renderRoot, const ActiveTrackingSystem activeTrackingSystem, osg::Vec3f point, osg::Vec3f normal, osg::Vec3f normalModifier) {
  11. _renderRoot = renderRoot;
  12. _activeTrackingSystem = activeTrackingSystem;
  13. _screwMove = new osg::MatrixTransform;
  14. _selectionRotateGroup = new osg::MatrixTransform;
  15. _selectionRotateGroup->addChild(_screwMove.get());
  16. _selectionMoveToEndGroup = new osg::MatrixTransform;
  17. _selectionMoveToEndGroup->addChild(_selectionRotateGroup.get());
  18. _selectionTranslateGroup = new osg::MatrixTransform;
  19. _selectionTranslateGroup->addChild(_selectionMoveToEndGroup.get());
  20. _selectionSwitch = new osg::Switch;
  21. _selectionSwitch->addChild(_selectionTranslateGroup.get());
  22. _renderRoot->addChild(_selectionSwitch.get());
  23. moveTo(point);
  24. setNormalModifier(normalModifier);
  25. rotateToNormalVector(normal);
  26. }
  27. PointShape::~PointShape() {
  28. _renderRoot->removeChild(_selectionSwitch.get());
  29. }
  30. void PointShape::moveTo(osg::Vec3f position) {
  31. _selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
  32. }
  33. void PointShape::setNormalModifier(osg::Vec3f normalModifier) {
  34. _normalModifier = normalModifier;
  35. }
  36. void PointShape::rotateToNormalVector(osg::Vec3f normal) {
  37. osg::Matrix modifierRotation = osg::Matrix::rotate(_normalModifier.x() * M_PI / 180, osg::Vec3(1.0f, 0.0f, 0.0f), _normalModifier.y() * M_PI / 180, osg::Vec3(0.0f, 1.0f, 0.0f), _normalModifier.z() * M_PI / 180, osg::Vec3(0.0f, 0.0f, 1.0f));
  38. normal = modifierRotation.preMult(normal);
  39. normal.normalize();
  40. _selectionRotateGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  41. if (_activeTrackingSystem == OptiTrack || _activeTrackingSystem == SteamVRTrack) {
  42. osg::Vec3f movementVector = normal.operator*(_optiTrackSteamVRLength / 2);
  43. _selectionMoveToEndGroup->setMatrix(osg::Matrix::translate(movementVector));
  44. }
  45. if (_activeTrackingSystem == SteamVRTrack) {
  46. osg::Vec3f movementVector = osg::Vec3f(0.0f, 0.0f, 1.0f).operator*(_optiTrackSteamVRLength / 2);
  47. _screwMove->setMatrix(osg::Matrix::translate(movementVector));
  48. }
  49. }
  50. void PointShape::setVisibility(bool mode) {
  51. _selectionSwitch->setValue(0, mode);
  52. }
  53. void PointShape::setColor(osg::Vec4 color) {
  54. _shape->setColor(color);
  55. if (_thread) {
  56. OSGWidget::fixMaterialState(_thread, &color);
  57. }
  58. }
  59. void PointShape::setupOptiTrack(OptiTrackSettings optiTrackSettings) {
  60. if (_activeTrackingSystem == OptiTrack) {
  61. _optiTrackSteamVRLength = optiTrackSettings.length;
  62. _geode = new osg::Geode;
  63. _shape = new osg::ShapeDrawable;
  64. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), optiTrackSettings.radius, optiTrackSettings.length));
  65. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  66. _geode->addDrawable(_shape.get());
  67. OSGWidget::fixMaterialState(_geode);
  68. _selectionRotateGroup->addChild(_geode.get());
  69. }
  70. }
  71. void PointShape::setupEMFTrack(EMFTrackSettings emfTrackSettings) {
  72. if (_activeTrackingSystem == EMFTrack) {
  73. _geode = new osg::Geode;
  74. _shape = new osg::ShapeDrawable;
  75. _shape->setShape(new osg::Box(osg::Vec3(0.0f, 0.0f, static_cast<float>(emfTrackSettings.depth) / 2), static_cast<float>(emfTrackSettings.width), static_cast<float>(emfTrackSettings.height), static_cast<float>(emfTrackSettings.depth)));
  76. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  77. _geode->addDrawable(_shape.get());
  78. OSGWidget::fixMaterialState(_geode);
  79. _selectionRotateGroup->addChild(_geode.get());
  80. }
  81. }
  82. void PointShape::setupSteamVRTrack(SteamVRTrackSettings steamVrTrackSettings) {
  83. if (_activeTrackingSystem == SteamVRTrack) {
  84. _optiTrackSteamVRLength = steamVrTrackSettings.length;
  85. _geode = new osg::Geode;
  86. _shape = new osg::ShapeDrawable;
  87. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), STEAMVR_CONSTANT_RADIUS, steamVrTrackSettings.length));
  88. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  89. _geode->addDrawable(_shape.get());
  90. MainWindow::getInstance()->getOsgWidget()->loadSteamvrThread();
  91. _thread = new osg::Geode;
  92. _thread->addDrawable(MainWindow::getInstance()->getOsgWidget()->_steamvrThreadMesh.get());
  93. OSGWidget::fixMaterialState(_thread);
  94. _screwMove->addChild(_thread.get());
  95. OSGWidget::fixMaterialState(_geode);
  96. _selectionRotateGroup->addChild(_geode.get());
  97. }
  98. }
  99. void PointShape::setupActionPoints() {
  100. if (_activeTrackingSystem == ActionPoints) {
  101. _geode = new osg::Geode;
  102. _shape = new osg::ShapeDrawable();
  103. _shape->setShape(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 0.5f));
  104. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  105. _geode->addDrawable(_shape.get());
  106. OSGWidget::fixMaterialState(_geode);
  107. _selectionRotateGroup->addChild(_geode.get());
  108. }
  109. }
  110. osg::ref_ptr<osg::Geode> PointShape::getMesh() {
  111. return _geode;
  112. }
  113. osg::Matrix PointShape::emfYFix(osg::Vec3 normal) {
  114. osg::Vec3 xyNormal = osg::Vec3(normal.x(), normal.y(), 0.0f);
  115. xyNormal.normalize();
  116. float deg = (90.0f * M_PI / 180) * normal.x() * -1;
  117. return osg::Matrix::rotate(deg, normal);
  118. }