PointShape.cpp 5.7 KB

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