PickHandler.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Include own headers
  2. #include "PickHandler.hpp"
  3. // Include dependencies
  4. #include <osg/io_utils>
  5. #include <osgUtil/IntersectionVisitor>
  6. #include <osgUtil/LineSegmentIntersector>
  7. #include <osg/ShapeDrawable>
  8. #include <osg/MatrixTransform>
  9. #include <osg/Material>
  10. #include <osg/StateSet>
  11. #include <osgViewer/Viewer>
  12. PickHandler::PickHandler(StoreHandler* storeHandler, OpenScadRenderer* openScadRenderer, ThreeMFWriter* threeMFWriter, osg::ref_ptr<osg::Node> axesNode) {
  13. _storeHandler = storeHandler;
  14. _openScadRenderer = openScadRenderer;
  15. _threeMFWriter = threeMFWriter;
  16. _axesNode = axesNode;
  17. }
  18. osg::Node* PickHandler::getOrCreateSelectionCylinder() {
  19. if (!_selectionTranslateGroup) {
  20. osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  21. osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
  22. cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f));
  23. cylinder->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
  24. geode->addDrawable(cylinder.get());
  25. {
  26. osg::StateSet* stateSet = geode->getOrCreateStateSet();
  27. osg::Material* material = new osg::Material;
  28. material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
  29. stateSet->setAttributeAndModes(material, osg::StateAttribute::ON);
  30. stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
  31. }
  32. _selectionRotateGroup = new osg::MatrixTransform;
  33. _selectionRotateGroup->addChild(geode.get());
  34. _selectionMoveToEndGroup = new osg::MatrixTransform;
  35. _selectionMoveToEndGroup->addChild(_selectionRotateGroup.get());
  36. _selectionTranslateGroup = new osg::MatrixTransform;
  37. _selectionTranslateGroup->addChild(_selectionMoveToEndGroup.get());
  38. _selectionSwitch = new osg::Switch;
  39. _selectionSwitch->addChild(_selectionTranslateGroup.get());
  40. }
  41. return _selectionSwitch.get();
  42. }
  43. bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
  44. if (ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) {
  45. isSelection = !isSelection;
  46. setVisibility(false);
  47. }
  48. if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON && isSelection) {
  49. osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
  50. if (viewer) {
  51. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  52. osgUtil::IntersectionVisitor iv(intersector.get());
  53. iv.setTraversalMask(~0x1);
  54. viewer->getCamera()->accept(iv);
  55. if (intersector->containsIntersections()) {
  56. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  57. if (std::find(result.nodePath.begin(), result.nodePath.end(), _axesNode) != result.nodePath.end()) {
  58. moveTo(result.localIntersectionPoint);
  59. rotateToNormalVector(result.localIntersectionNormal);
  60. _storeHandler->addTrackingPoint(result.localIntersectionPoint, result.localIntersectionNormal);
  61. _openScadRenderer->render(_storeHandler->getPoints());
  62. _threeMFWriter->writeTrackPoints(_storeHandler->getPoints(), "/tmp/export.3mf");
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE && isSelection) {
  70. osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
  71. if (viewer) {
  72. osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
  73. osgUtil::IntersectionVisitor iv(intersector.get());
  74. iv.setTraversalMask(~0x1);
  75. viewer->getCamera()->accept(iv);
  76. if (intersector->containsIntersections()) {
  77. for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
  78. if (std::find(result.nodePath.begin(), result.nodePath.end(), _axesNode) != result.nodePath.end()) {
  79. moveTo(result.localIntersectionPoint);
  80. rotateToNormalVector(result.localIntersectionNormal);
  81. setVisibility(true);
  82. break;
  83. }
  84. }
  85. } else {
  86. setVisibility(false);
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. void PickHandler::moveTo(osg::Vec3f position) {
  93. _selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
  94. }
  95. void PickHandler::rotateToNormalVector(osg::Vec3f normal) {
  96. _selectionRotateGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
  97. osg::Vec3f movementVector = normal.operator*(5.0f);
  98. _selectionMoveToEndGroup->setMatrix(osg::Matrix::translate(movementVector));
  99. }
  100. void PickHandler::setVisibility(bool mode) {
  101. _selectionSwitch->setValue(0, mode);
  102. }