PickHandler.cpp 10 KB

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