PointShape.cpp 5.6 KB

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