PointShape.cpp 3.9 KB

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