PointShape.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Include own headers
  2. #include "PointShape.hpp"
  3. // Include modules
  4. #include "OSGWidget.hpp"
  5. // Include dependencies
  6. #include <osgDB/ReadFile>
  7. PointShape::PointShape(const osg::ref_ptr<osg::Group> renderRoot, const ActiveTrackingSystem activeTrackingSystem, osg::Vec3f point, osg::Vec3f normal, osg::Vec3f normalModifier) {
  8. _renderRoot = renderRoot;
  9. _activeTrackingSystem = activeTrackingSystem;
  10. _selectionRotateGroup = new osg::MatrixTransform;
  11. _screwMove = new osg::MatrixTransform;
  12. _selectionMoveToEndGroup = new osg::MatrixTransform;
  13. _selectionMoveToEndGroup->addChild(_selectionRotateGroup.get());
  14. _selectionMoveToEndGroup->addChild(_screwMove.get());
  15. _selectionTranslateGroup = new osg::MatrixTransform;
  16. _selectionTranslateGroup->addChild(_selectionMoveToEndGroup.get());
  17. _selectionSwitch = new osg::Switch;
  18. _selectionSwitch->addChild(_selectionTranslateGroup.get());
  19. _renderRoot->addChild(_selectionSwitch.get());
  20. moveTo(point);
  21. setNormalModifier(normalModifier);
  22. rotateToNormalVector(normal);
  23. }
  24. PointShape::~PointShape() {
  25. _renderRoot->removeChild(_selectionSwitch.get());
  26. }
  27. void PointShape::moveTo(osg::Vec3f position) {
  28. _selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
  29. }
  30. void PointShape::setNormalModifier(osg::Vec3f normalModifier) {
  31. _normalModifier = normalModifier;
  32. }
  33. void PointShape::rotateToNormalVector(osg::Vec3f normal) {
  34. normal = normal.operator+(_normalModifier);
  35. normal.normalize();
  36. _selectionRotateGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  37. if (_activeTrackingSystem == OptiTrack || _activeTrackingSystem == SteamVRTrack) {
  38. osg::Vec3f movementVector = normal.operator*(_optiTrackSteamVRLength / 2);
  39. _selectionMoveToEndGroup->setMatrix(osg::Matrix::translate(movementVector));
  40. }
  41. if (_activeTrackingSystem == SteamVRTrack) {
  42. osg::Vec3f movementVector = normal.operator*(_optiTrackSteamVRLength);
  43. _screwMove->setMatrix(osg::Matrix::translate(movementVector));
  44. }
  45. }
  46. void PointShape::setVisibility(bool mode) {
  47. _selectionSwitch->setValue(0, mode);
  48. }
  49. void PointShape::setColor(osg::Vec4 color) {
  50. _shape->setColor(color);
  51. }
  52. void PointShape::setupOptiTrack(OptiTrackSettings optiTrackSettings) {
  53. if (_activeTrackingSystem == OptiTrack) {
  54. _optiTrackSteamVRLength = optiTrackSettings.length;
  55. _geode = new osg::Geode;
  56. _shape = new osg::ShapeDrawable();
  57. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), optiTrackSettings.radius, optiTrackSettings.length));
  58. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  59. _geode->addDrawable(_shape.get());
  60. OSGWidget::fixMaterialState(_geode);
  61. _selectionRotateGroup->addChild(_geode.get());
  62. }
  63. }
  64. void PointShape::setupSteamVRTrack(SteamVRTrackSettings steamVrTrackSettings) {
  65. if (_activeTrackingSystem == SteamVRTrack) {
  66. _optiTrackSteamVRLength = steamVrTrackSettings.length;
  67. _geode = new osg::Geode;
  68. _shape = new osg::ShapeDrawable();
  69. _shape->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, steamVrTrackSettings.length));
  70. _shape->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.2f));
  71. _geode->addDrawable(_shape.get());
  72. osg::ref_ptr<osg::Node> screw = osgDB::readNodeFile("../resources/steamvrthread.stl");
  73. _screwMove->addChild(screw.get());
  74. OSGWidget::fixMaterialState(_geode);
  75. _selectionRotateGroup->addChild(_geode.get());
  76. }
  77. }
  78. void PointShape::setupActionPoints() {
  79. if (_activeTrackingSystem == ActionPoints) {
  80. _geode = new osg::Geode;
  81. _shape = new osg::ShapeDrawable();
  82. _shape->setShape(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 0.5f));
  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. osg::ref_ptr<osg::Geode> PointShape::getMesh() {
  90. return _geode;
  91. }