PickHandler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Include own headers
  2. #include "PickHandler.hpp"
  3. // Include modules
  4. #include "TrackPointRenderer.hpp"
  5. // Include dependencies
  6. #include <osg/io_utils>
  7. #include <osgUtil/IntersectionVisitor>
  8. #include <osgUtil/LineSegmentIntersector>
  9. #include <osg/ShapeDrawable>
  10. #include <osg/MatrixTransform>
  11. #include <osg/Material>
  12. #include <osg/StateSet>
  13. #include <osgViewer/Viewer>
  14. PickHandler::PickHandler(OSGWidget* osgWidget, osg::ref_ptr<osg::Group> root) {
  15. _osgWidget = osgWidget;
  16. _selectionSwitch = new osg::Switch;
  17. root->addChild(_selectionSwitch.get());
  18. }
  19. osg::Node* PickHandler::getPickerRoot() {
  20. return _selectionSwitch.get();
  21. }
  22. bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
  23. if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) {
  24. if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH) {
  25. osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
  26. if (viewer) {
  27. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  28. osgUtil::IntersectionVisitor iv(intersector.get());
  29. iv.setTraversalMask(~0x1);
  30. viewer->getCamera()->accept(iv);
  31. if (intersector->containsIntersections()) {
  32. if (_addNewPoints) {
  33. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  34. if (std::find(result.nodePath.begin(), result.nodePath.end(), _osgWidget->getMesh()) != result.nodePath.end()) {
  35. _clickStartedOnElement = true;
  36. break;
  37. }
  38. }
  39. } else {
  40. bool found = false;
  41. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  42. if (found) break;
  43. for (PointShape* shape: _osgWidget->getPointRenderer()->getShapes()) {
  44. if (std::find(result.nodePath.begin(), result.nodePath.end(), shape->getMesh()) != result.nodePath.end()) {
  45. _clickStartedOnElement = true;
  46. found = true;
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. } else if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && _clickStartedOnElement) {
  55. _clickStartedOnElement = false;
  56. osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
  57. if (viewer) {
  58. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  59. osgUtil::IntersectionVisitor iv(intersector.get());
  60. iv.setTraversalMask(~0x1);
  61. viewer->getCamera()->accept(iv);
  62. if (intersector->containsIntersections()) {
  63. if (_addNewPoints) {
  64. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  65. if (std::find(result.nodePath.begin(), result.nodePath.end(), _osgWidget->getMesh()) != result.nodePath.end()) {
  66. moveTo(result.localIntersectionPoint);
  67. rotateToNormalVector(result.localIntersectionNormal);
  68. addPoint(result.localIntersectionPoint, result.localIntersectionNormal);
  69. break;
  70. }
  71. }
  72. } else {
  73. bool found = false;
  74. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  75. if (found) break;
  76. int shapeId = 0;
  77. for (PointShape* shape: _osgWidget->getPointRenderer()->getShapes()) {
  78. if (std::find(result.nodePath.begin(), result.nodePath.end(), shape->getMesh()) != result.nodePath.end()) {
  79. _selectedPoint = shapeId;
  80. MainWindow::getInstance()->getEditWiget()->setSelection(shapeId);
  81. found = true;
  82. break;
  83. }
  84. shapeId++;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. // Update position of picker on mouse move
  93. if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE || ea.getEventType() == osgGA::GUIEventAdapter::DRAG) {
  94. osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
  95. if (viewer) {
  96. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  97. osgUtil::IntersectionVisitor iv(intersector.get());
  98. iv.setTraversalMask(~0x1);
  99. viewer->getCamera()->accept(iv);
  100. invalidateTrackPointColors();
  101. if (intersector->containsIntersections()) {
  102. if (_addNewPoints) {
  103. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  104. if (std::find(result.nodePath.begin(), result.nodePath.end(), _osgWidget->getMesh()) != result.nodePath.end()) {
  105. moveTo(result.localIntersectionPoint);
  106. rotateToNormalVector(result.localIntersectionNormal);
  107. setVisibility(true);
  108. break;
  109. }
  110. }
  111. } else {
  112. bool found = false;
  113. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  114. if (found) break;
  115. for (PointShape* shape: _osgWidget->getPointRenderer()->getShapes()) {
  116. if (std::find(result.nodePath.begin(), result.nodePath.end(), shape->getMesh()) != result.nodePath.end()) {
  117. shape->setColor(osg::Vec4(0.0f, 0.0f, 1.0f, 0.2f));
  118. found = true;
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. } else {
  125. if (_addNewPoints) {
  126. MainWindow::getInstance()->getEditWiget()->invalidatePositions();
  127. }
  128. setVisibility(false);
  129. }
  130. }
  131. }
  132. return false;
  133. }
  134. void PickHandler::setSelection(bool addNewPoints) {
  135. _addNewPoints = addNewPoints;
  136. if (addNewPoints) {
  137. _selectedPoint = -1;
  138. }
  139. invalidateTrackPointColors();
  140. updateRenderer();
  141. }
  142. void PickHandler::updateRenderer() {
  143. delete _shape;
  144. ActiveTrackingSystem activeTrackingSystem = MainWindow::getInstance()->getEditWiget()->getSelectedTrackingSystem();
  145. switch(activeTrackingSystem) {
  146. case OptiTrack: {
  147. OptiTrackSettings settings = MainWindow::getInstance()->getStore()->getOptiTrackSettings();
  148. _shape = new PointShape(_selectionSwitch, activeTrackingSystem, osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), 0.0f);
  149. _shape->setupOptiTrack(settings);
  150. break;
  151. }
  152. case EMFTrack: {
  153. EMFTrackSettings settings = MainWindow::getInstance()->getStore()->getEMFTrackSettings();
  154. _shape = new PointShape(_selectionSwitch, activeTrackingSystem, osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), 0.0f);
  155. _shape->setupEMFTrack(settings);
  156. break;
  157. }
  158. case SteamVRTrack: {
  159. SteamVRTrackSettings settings = MainWindow::getInstance()->getStore()->getSteamVRTrackSettings();
  160. _shape = new PointShape(_selectionSwitch, activeTrackingSystem, osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), 0.0f);
  161. _shape->setupSteamVRTrack(settings);
  162. break;
  163. }
  164. case ActionPoints: {
  165. _shape = new PointShape(_selectionSwitch, activeTrackingSystem, osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, 0.0f), 0.0f);
  166. _shape->setupActionPoints();
  167. break;
  168. }
  169. }
  170. if (_shape) {
  171. _shape->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
  172. }
  173. setVisibility(_addNewPoints);
  174. }
  175. void PickHandler::moveTo(osg::Vec3f position) {
  176. if (_shape) {
  177. _shape->moveTo(position);
  178. MainWindow::getInstance()->getEditWiget()->updatePositions(position);
  179. }
  180. }
  181. void PickHandler::rotateToNormalVector(osg::Vec3f normal) {
  182. MainWindow::getInstance()->getEditWiget()->updateNormals(normal);
  183. if (_shape) {
  184. osg::Vec3 modifier = MainWindow::getInstance()->getStore()->getNormalModifier();
  185. float normalRotation = MainWindow::getInstance()->getStore()->getNormalRotation();
  186. ActiveTrackingSystem activeTrackingSystem = MainWindow::getInstance()->getEditWiget()->getSelectedTrackingSystem();
  187. if (activeTrackingSystem == EMFTrack) {
  188. normal = normal.operator*(-1.0f);
  189. }
  190. _shape->rotateToNormalVector(normal, normalRotation);
  191. _shape->setNormalModifier(modifier);
  192. }
  193. }
  194. void PickHandler::setVisibility(bool mode) {
  195. _selectionSwitch->setValue(0, mode);
  196. }
  197. void PickHandler::addPoint(osg::Vec3 point, osg::Vec3 normal) {
  198. ActiveTrackingSystem activeTrackingSystem = MainWindow::getInstance()->getEditWiget()->getSelectedTrackingSystem();
  199. MainWindow::getInstance()->getStore()->addTrackPoint(point, normal, activeTrackingSystem);
  200. _osgWidget->getPointRenderer()->render(activeTrackingSystem);
  201. if (activeTrackingSystem == ActionPoints) {
  202. MainWindow::getInstance()->getEditWiget()->resetActionPointSettings();
  203. }
  204. }
  205. void PickHandler::invalidateTrackPointColors() {
  206. int shapeId = 0;
  207. for (PointShape* shape: _osgWidget->getPointRenderer()->getShapes()) {
  208. if (_selectedPoint != shapeId) {
  209. shape->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 0.2f));
  210. }
  211. shapeId++;
  212. }
  213. }