PickHandler.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. _optiTrackSteamVRLength = settings.length;
  149. _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));
  150. _shape->setupOptiTrack(settings);
  151. break;
  152. }
  153. case EMFTrack: {
  154. break;
  155. }
  156. case SteamVRTrack: {
  157. SteamVRTrackSettings settings = MainWindow::getInstance()->getStore()->getSteamVRTrackSettings();
  158. _optiTrackSteamVRLength = settings.length;
  159. _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));
  160. _shape->setupSteamVRTrack(settings);
  161. break;
  162. }
  163. case ActionPoints: {
  164. _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));
  165. _shape->setupActionPoints();
  166. break;
  167. }
  168. }
  169. if (_shape) {
  170. _shape->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
  171. }
  172. setVisibility(_addNewPoints);
  173. }
  174. void PickHandler::moveTo(osg::Vec3f position) {
  175. if (_shape) {
  176. _shape->moveTo(position);
  177. MainWindow::getInstance()->getEditWiget()->updatePositions(position);
  178. }
  179. }
  180. void PickHandler::rotateToNormalVector(osg::Vec3f normal) {
  181. MainWindow::getInstance()->getEditWiget()->updateNormals(normal);
  182. if (_shape) {
  183. osg::Vec3 modifier = MainWindow::getInstance()->getStore()->getNormalModifier();
  184. _shape->rotateToNormalVector(normal);
  185. _shape->setNormalModifier(modifier);
  186. }
  187. }
  188. void PickHandler::setVisibility(bool mode) {
  189. _selectionSwitch->setValue(0, mode);
  190. }
  191. void PickHandler::addPoint(osg::Vec3 point, osg::Vec3 normal) {
  192. ActiveTrackingSystem activeTrackingSystem = MainWindow::getInstance()->getEditWiget()->getSelectedTrackingSystem();
  193. MainWindow::getInstance()->getStore()->addTrackPoint(point, normal, activeTrackingSystem);
  194. _osgWidget->getPointRenderer()->render(MainWindow::getInstance()->getEditWiget()->getSelectedTrackingSystem());
  195. }
  196. void PickHandler::invalidateTrackPointColors() {
  197. int shapeId = 0;
  198. for (PointShape* shape: _osgWidget->getPointRenderer()->getShapes()) {
  199. if (_selectedPoint != shapeId) {
  200. shape->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 0.2f));
  201. }
  202. shapeId++;
  203. }
  204. }