PointShape.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. _selectionTranslateGroup = new osg::MatrixTransform;
  17. _selectionTranslateGroup->addChild(_selectionRotateGroup.get());
  18. _selectionSwitch = new osg::Switch;
  19. _selectionSwitch->addChild(_selectionTranslateGroup.get());
  20. _renderRoot->addChild(_selectionSwitch.get());
  21. moveTo(point);
  22. setNormalModifier(normalModifier);
  23. rotateToNormalVector(normal, normalRotation);
  24. }
  25. PointShape::~PointShape() {
  26. _renderRoot->removeChild(_selectionSwitch.get());
  27. }
  28. void PointShape::moveTo(osg::Vec3f position) {
  29. _selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
  30. }
  31. void PointShape::setNormalModifier(osg::Vec3f normalModifier) {
  32. _normalModifier = normalModifier;
  33. }
  34. void PointShape::rotateToNormalVector(osg::Vec3f normal, float normalRotation) {
  35. 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));
  36. normal = modifierRotation.preMult(normal);
  37. normal.normalize();
  38. osg::Matrix matrix = osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal);
  39. matrix = matrix.operator*(osg::Matrix::rotate(normalRotation * M_PI / 180, normal));
  40. _selectionRotateGroup->setMatrix(matrix);
  41. }
  42. void PointShape::setVisibility(bool mode) {
  43. _selectionSwitch->setValue(0, mode);
  44. }
  45. void PointShape::setColor(osg::Vec4 color) {
  46. _shape->setColor(color);
  47. if (_thread) {
  48. OSGWidget::fixMaterialState(_thread, &color);
  49. }
  50. if (_optiConnector) {
  51. _optiConnector->setColor(color);
  52. }
  53. }
  54. void PointShape::setupOptiTrack(OptiTrackSettings optiTrackSettings) {
  55. if (_activeTrackingSystem == OptiTrack) {
  56. _optiTrackSettings = optiTrackSettings;
  57. _geode = new osg::Geode;
  58. _shape = new osg::ShapeDrawable;
  59. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, (optiTrackSettings.length - 5.0f) / 2), optiTrackSettings.radius, optiTrackSettings.length - 5.0f));
  60. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  61. _geode->addDrawable(_shape.get());
  62. _optiConnector = new osg::ShapeDrawable;
  63. _optiConnector->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, optiTrackSettings.length - 2.5f), 0.74f, 5.0f));
  64. _optiConnector->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  65. _geode->addDrawable(_optiConnector.get());
  66. OSGWidget::fixMaterialState(_geode);
  67. _selectionRotateGroup->addChild(_geode.get());
  68. }
  69. }
  70. void PointShape::setupEMFTrack(EMFTrackSettings emfTrackSettings) {
  71. if (_activeTrackingSystem == EMFTrack) {
  72. _emfTrackSettings = emfTrackSettings;
  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. _steamVrTrackSettings = steamVrTrackSettings;
  85. _geode = new osg::Geode;
  86. _shape = new osg::ShapeDrawable;
  87. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, steamVrTrackSettings.length / 2), 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. osg::Vec3f movementVector = osg::Vec3f(0.0f, 0.0f, 1.0f).operator*(steamVrTrackSettings.length);
  96. _screwMove->setMatrix(osg::Matrix::translate(movementVector));
  97. OSGWidget::fixMaterialState(_geode);
  98. _selectionRotateGroup->addChild(_geode.get());
  99. }
  100. }
  101. void PointShape::setupActionPoints() {
  102. if (_activeTrackingSystem == ActionPoints) {
  103. _geode = new osg::Geode;
  104. _shape = new osg::ShapeDrawable();
  105. _shape->setShape(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 0.5f));
  106. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  107. _geode->addDrawable(_shape.get());
  108. OSGWidget::fixMaterialState(_geode);
  109. _selectionRotateGroup->addChild(_geode.get());
  110. }
  111. }
  112. void PointShape::setCompensation(bool compensation, float compensationLength) {
  113. _compensation = compensation;
  114. _compensationLength = compensationLength;
  115. switch(_activeTrackingSystem) {
  116. case OptiTrack: {
  117. float baseLength = _optiTrackSettings.length - 5.0f;
  118. float basePosition = _optiTrackSettings.length - 5.0f;
  119. if (_compensation) {
  120. baseLength += _compensationLength;
  121. basePosition -= _compensationLength;
  122. }
  123. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, basePosition / 2), _optiTrackSettings.radius, baseLength));
  124. break;
  125. }
  126. case EMFTrack: {
  127. float depth = static_cast<float>(_emfTrackSettings.depth);
  128. float depthPosition = static_cast<float>(_emfTrackSettings.depth);
  129. if (_compensation) {
  130. depth += _compensationLength;
  131. depthPosition -= _compensationLength;
  132. }
  133. _shape->setShape(new osg::Box(osg::Vec3(0.0f, 0.0f, depthPosition / 2), static_cast<float>(_emfTrackSettings.width), static_cast<float>(_emfTrackSettings.height), depth));
  134. break;
  135. }
  136. case SteamVRTrack: {
  137. float baseLength = _steamVrTrackSettings.length;
  138. float basePosition = _steamVrTrackSettings.length;
  139. if (_compensation) {
  140. baseLength += _compensationLength;
  141. basePosition -= _compensationLength;
  142. }
  143. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, basePosition / 2), STEAMVR_CONSTANT_RADIUS, baseLength));
  144. break;
  145. }
  146. case ActionPoints: {
  147. break;
  148. }
  149. }
  150. }
  151. osg::ref_ptr<osg::Geode> PointShape::getMesh() {
  152. return _geode;
  153. }
  154. osg::Matrix PointShape::emfYFix(osg::Vec3 normal) {
  155. osg::Vec3 xyNormal = osg::Vec3(normal.x(), normal.y(), 0.0f);
  156. xyNormal.normalize();
  157. float deg = (90.0f * M_PI / 180) * normal.x() * -1;
  158. return osg::Matrix::rotate(deg, normal);
  159. }