PointShape.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, float normalRotation) {
  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, normalRotation);
  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, float normalRotation) {
  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. osg::Matrix matrix = osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal);
  41. matrix = matrix.operator*(osg::Matrix::rotate(normalRotation * M_PI / 180, normal));
  42. _selectionRotateGroup->setMatrix(matrix);
  43. if (_activeTrackingSystem == OptiTrack || _activeTrackingSystem == SteamVRTrack) {
  44. float movementFixLength = _optiTrackSteamVRLength / 2;
  45. if (_compensation) {
  46. movementFixLength -= _compensationLength / 2;
  47. }
  48. osg::Vec3f movementVector = normal.operator*(movementFixLength);
  49. _selectionMoveToEndGroup->setMatrix(osg::Matrix::translate(movementVector));
  50. }
  51. if (_activeTrackingSystem == SteamVRTrack) {
  52. float threadFixLength = _optiTrackSteamVRLength / 2;
  53. if (_compensation) {
  54. threadFixLength += _compensationLength / 2;
  55. }
  56. osg::Vec3f movementVector = osg::Vec3f(0.0f, 0.0f, 1.0f).operator*(threadFixLength);
  57. _screwMove->setMatrix(osg::Matrix::translate(movementVector));
  58. }
  59. }
  60. void PointShape::setVisibility(bool mode) {
  61. _selectionSwitch->setValue(0, mode);
  62. }
  63. void PointShape::setColor(osg::Vec4 color) {
  64. _shape->setColor(color);
  65. if (_thread) {
  66. OSGWidget::fixMaterialState(_thread, &color);
  67. }
  68. if (_optiConnector) {
  69. _optiConnector->setColor(color);
  70. }
  71. }
  72. void PointShape::setupOptiTrack(OptiTrackSettings optiTrackSettings) {
  73. if (_activeTrackingSystem == OptiTrack) {
  74. _optiTrackSteamVRLength = optiTrackSettings.length;
  75. _geode = new osg::Geode;
  76. _shape = new osg::ShapeDrawable;
  77. float baseLength = optiTrackSettings.length - 5.0f;
  78. float smallShapePosition = optiTrackSettings.length - 7.5f;
  79. if (_compensation) {
  80. baseLength += _compensationLength;
  81. smallShapePosition += _compensationLength / 2;
  82. }
  83. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, -2.5f), optiTrackSettings.radius, baseLength));
  84. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  85. _geode->addDrawable(_shape.get());
  86. _optiConnector = new osg::ShapeDrawable;
  87. _optiConnector->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, smallShapePosition), 0.74f, 5.0f));
  88. _optiConnector->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  89. _geode->addDrawable(_optiConnector.get());
  90. OSGWidget::fixMaterialState(_geode);
  91. _selectionRotateGroup->addChild(_geode.get());
  92. }
  93. }
  94. void PointShape::setupEMFTrack(EMFTrackSettings emfTrackSettings) {
  95. if (_activeTrackingSystem == EMFTrack) {
  96. _geode = new osg::Geode;
  97. _shape = new osg::ShapeDrawable;
  98. float depth = static_cast<float>(emfTrackSettings.depth);
  99. if (_compensation) {
  100. depth += _compensationLength;
  101. }
  102. _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), depth));
  103. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  104. _geode->addDrawable(_shape.get());
  105. OSGWidget::fixMaterialState(_geode);
  106. _selectionRotateGroup->addChild(_geode.get());
  107. }
  108. }
  109. void PointShape::setupSteamVRTrack(SteamVRTrackSettings steamVrTrackSettings) {
  110. if (_activeTrackingSystem == SteamVRTrack) {
  111. _optiTrackSteamVRLength = steamVrTrackSettings.length;
  112. _geode = new osg::Geode;
  113. _shape = new osg::ShapeDrawable;
  114. float baseLength = steamVrTrackSettings.length;
  115. if (_compensation) {
  116. baseLength += _compensationLength;
  117. }
  118. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), STEAMVR_CONSTANT_RADIUS, baseLength));
  119. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  120. _geode->addDrawable(_shape.get());
  121. MainWindow::getInstance()->getOsgWidget()->loadSteamvrThread();
  122. _thread = new osg::Geode;
  123. _thread->addDrawable(MainWindow::getInstance()->getOsgWidget()->_steamvrThreadMesh.get());
  124. OSGWidget::fixMaterialState(_thread);
  125. _screwMove->addChild(_thread.get());
  126. OSGWidget::fixMaterialState(_geode);
  127. _selectionRotateGroup->addChild(_geode.get());
  128. }
  129. }
  130. void PointShape::setupActionPoints() {
  131. if (_activeTrackingSystem == ActionPoints) {
  132. _geode = new osg::Geode;
  133. _shape = new osg::ShapeDrawable();
  134. _shape->setShape(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 0.5f));
  135. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  136. _geode->addDrawable(_shape.get());
  137. OSGWidget::fixMaterialState(_geode);
  138. _selectionRotateGroup->addChild(_geode.get());
  139. }
  140. }
  141. void PointShape::setCompensation(bool compensation, float compensationLength) {
  142. _compensation = compensation;
  143. _compensationLength = compensationLength;
  144. }
  145. osg::ref_ptr<osg::Geode> PointShape::getMesh() {
  146. return _geode;
  147. }
  148. osg::Matrix PointShape::emfYFix(osg::Vec3 normal) {
  149. osg::Vec3 xyNormal = osg::Vec3(normal.x(), normal.y(), 0.0f);
  150. xyNormal.normalize();
  151. float deg = (90.0f * M_PI / 180) * normal.x() * -1;
  152. return osg::Matrix::rotate(deg, normal);
  153. }