|
@@ -1,9 +1,6 @@
|
|
|
// Include own headers
|
|
|
#include "PickHandler.hpp"
|
|
|
|
|
|
-// Include modules
|
|
|
-#include "OSGWidget.hpp"
|
|
|
-
|
|
|
// Include dependencies
|
|
|
#include <osg/io_utils>
|
|
|
|
|
@@ -16,65 +13,72 @@
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
|
|
-PickHandler::PickHandler(StoreHandler* storeHandler, OpenScadRenderer* openScadRenderer, ThreeMFWriter* threeMFWriter, osg::ref_ptr<osg::Node> axesNode) {
|
|
|
- _storeHandler = storeHandler;
|
|
|
- _openScadRenderer = openScadRenderer;
|
|
|
- _threeMFWriter = threeMFWriter;
|
|
|
- _axesNode = axesNode;
|
|
|
-}
|
|
|
+PickHandler::PickHandler(OSGWidget* osgWidget, osg::ref_ptr<osg::Group> root) {
|
|
|
+ _osgWidget = osgWidget;
|
|
|
|
|
|
-osg::Node* PickHandler::getOrCreateSelectionCylinder() {
|
|
|
- if (!_selectionTranslateGroup) {
|
|
|
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
|
|
|
- osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
|
|
|
- cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 10.0f));
|
|
|
- cylinder->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
|
|
|
- geode->addDrawable(cylinder.get());
|
|
|
+ _selectionRotateGroup = new osg::MatrixTransform;
|
|
|
|
|
|
- OSGWidget::fixMaterialState(geode);
|
|
|
+ _selectionMoveToEndGroup = new osg::MatrixTransform;
|
|
|
+ _selectionMoveToEndGroup->addChild(_selectionRotateGroup.get());
|
|
|
|
|
|
- _selectionRotateGroup = new osg::MatrixTransform;
|
|
|
- _selectionRotateGroup->addChild(geode.get());
|
|
|
+ _selectionTranslateGroup = new osg::MatrixTransform;
|
|
|
+ _selectionTranslateGroup->addChild(_selectionMoveToEndGroup.get());
|
|
|
|
|
|
- _selectionMoveToEndGroup = new osg::MatrixTransform;
|
|
|
- _selectionMoveToEndGroup->addChild(_selectionRotateGroup.get());
|
|
|
+ _selectionSwitch = new osg::Switch;
|
|
|
+ _selectionSwitch->addChild(_selectionTranslateGroup.get());
|
|
|
|
|
|
- _selectionTranslateGroup = new osg::MatrixTransform;
|
|
|
- _selectionTranslateGroup->addChild(_selectionMoveToEndGroup.get());
|
|
|
+ root->addChild(_selectionSwitch.get());
|
|
|
+}
|
|
|
|
|
|
- _selectionSwitch = new osg::Switch;
|
|
|
- _selectionSwitch->addChild(_selectionTranslateGroup.get());
|
|
|
- }
|
|
|
+osg::Node* PickHandler::getPickerRoot() {
|
|
|
return _selectionSwitch.get();
|
|
|
}
|
|
|
|
|
|
bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
|
|
|
- if (ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) {
|
|
|
- isSelection = !isSelection;
|
|
|
- setVisibility(false);
|
|
|
- }
|
|
|
- if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON && isSelection) {
|
|
|
- osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
- if (viewer) {
|
|
|
- osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
|
|
|
- osgUtil::IntersectionVisitor iv(intersector.get());
|
|
|
- iv.setTraversalMask(~0x1);
|
|
|
- viewer->getCamera()->accept(iv);
|
|
|
- if (intersector->containsIntersections()) {
|
|
|
- for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
|
|
|
- if (std::find(result.nodePath.begin(), result.nodePath.end(), _axesNode) != result.nodePath.end()) {
|
|
|
- moveTo(result.localIntersectionPoint);
|
|
|
- rotateToNormalVector(result.localIntersectionNormal);
|
|
|
- _storeHandler->addTrackingPoint(result.localIntersectionPoint, result.localIntersectionNormal);
|
|
|
- _openScadRenderer->render(_storeHandler->getPoints());
|
|
|
- _threeMFWriter->writeTrackPoints(_storeHandler->getPoints(), "/tmp/export.3mf");
|
|
|
- break;
|
|
|
+ if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) {
|
|
|
+ if (ea.getEventType() == osgGA::GUIEventAdapter::PUSH) {
|
|
|
+ osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
+ if (viewer) {
|
|
|
+ osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
|
|
|
+ osgUtil::IntersectionVisitor iv(intersector.get());
|
|
|
+ iv.setTraversalMask(~0x1);
|
|
|
+ viewer->getCamera()->accept(iv);
|
|
|
+ if (intersector->containsIntersections()) {
|
|
|
+ if (_addNewPoints) {
|
|
|
+ for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
|
|
|
+ if (std::find(result.nodePath.begin(), result.nodePath.end(), _osgWidget->getMesh()) != result.nodePath.end()) {
|
|
|
+ _clickStartedOnElement = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && _clickStartedOnElement) {
|
|
|
+ _clickStartedOnElement = false;
|
|
|
+ osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
+ if (viewer) {
|
|
|
+ osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
|
|
|
+ osgUtil::IntersectionVisitor iv(intersector.get());
|
|
|
+ iv.setTraversalMask(~0x1);
|
|
|
+ viewer->getCamera()->accept(iv);
|
|
|
+ if (intersector->containsIntersections()) {
|
|
|
+ for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
|
|
|
+ if (std::find(result.nodePath.begin(), result.nodePath.end(), _osgWidget->getMesh()) != result.nodePath.end()) {
|
|
|
+ moveTo(result.localIntersectionPoint);
|
|
|
+ rotateToNormalVector(result.localIntersectionNormal);
|
|
|
+ addPoint(result.localIntersectionPoint, result.localIntersectionNormal);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE && isSelection) {
|
|
|
+ // Update position of picker on mouse move
|
|
|
+ if (ea.getEventType() == osgGA::GUIEventAdapter::MOVE || ea.getEventType() == osgGA::GUIEventAdapter::DRAG) {
|
|
|
osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
if (viewer) {
|
|
|
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
|
|
@@ -82,15 +86,22 @@ bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapt
|
|
|
iv.setTraversalMask(~0x1);
|
|
|
viewer->getCamera()->accept(iv);
|
|
|
if (intersector->containsIntersections()) {
|
|
|
- for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
|
|
|
- if (std::find(result.nodePath.begin(), result.nodePath.end(), _axesNode) != result.nodePath.end()) {
|
|
|
- moveTo(result.localIntersectionPoint);
|
|
|
- rotateToNormalVector(result.localIntersectionNormal);
|
|
|
- setVisibility(true);
|
|
|
- break;
|
|
|
+ if (_addNewPoints) {
|
|
|
+ for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
|
|
|
+ if (std::find(result.nodePath.begin(), result.nodePath.end(), _osgWidget->getMesh()) != result.nodePath.end()) {
|
|
|
+ moveTo(result.localIntersectionPoint);
|
|
|
+ rotateToNormalVector(result.localIntersectionNormal);
|
|
|
+ setVisibility(true);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (const osgUtil::LineSegmentIntersector::Intersection result: intersector->getIntersections()) {
|
|
|
+ // TODO
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
+ MainWindow::getInstance()->getEditWiget()->invalidatePositions();
|
|
|
setVisibility(false);
|
|
|
}
|
|
|
}
|
|
@@ -98,16 +109,74 @@ bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapt
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+void PickHandler::setTrackingSystem(ActiveTrackingSystem activeTrackingSystem) {
|
|
|
+ _activeTrackingSystem = activeTrackingSystem;
|
|
|
+ updateRenderer();
|
|
|
+}
|
|
|
+
|
|
|
+void PickHandler::setSelection(bool addNewPoints) {
|
|
|
+ _addNewPoints = addNewPoints;
|
|
|
+ updateRenderer();
|
|
|
+}
|
|
|
+
|
|
|
void PickHandler::moveTo(osg::Vec3f position) {
|
|
|
_selectionTranslateGroup->setMatrix(osg::Matrix::translate(position));
|
|
|
+ MainWindow::getInstance()->getEditWiget()->updatePositions(position);
|
|
|
}
|
|
|
|
|
|
void PickHandler::rotateToNormalVector(osg::Vec3f normal) {
|
|
|
+ osg::Vec3 modifier = MainWindow::getInstance()->getStore()->getNormalModifier();
|
|
|
+ normal = normal.operator+(modifier);
|
|
|
_selectionRotateGroup->setMatrix(osg::Matrix::rotate(osg::Vec3f(0.0f, 0.0f, 1.0f), normal));
|
|
|
- osg::Vec3f movementVector = normal.operator*(5.0f);
|
|
|
+ osg::Vec3f movementVector = normal.operator*(_optiTrackSteamVRLength / 2);
|
|
|
_selectionMoveToEndGroup->setMatrix(osg::Matrix::translate(movementVector));
|
|
|
+ MainWindow::getInstance()->getEditWiget()->updateNormals(normal);
|
|
|
+}
|
|
|
+
|
|
|
+void PickHandler::updateRenderer() {
|
|
|
+ removeAllShapes();
|
|
|
+ switch(_activeTrackingSystem) {
|
|
|
+ case OptiTrack: {
|
|
|
+ OptiTrackSettings settings = MainWindow::getInstance()->getStore()->getOptiTrackSettings();
|
|
|
+ _optiTrackSteamVRLength = settings.length;
|
|
|
+ osg::ref_ptr<osg::Geode> geode = new osg::Geode;
|
|
|
+ osg::ref_ptr<osg::ShapeDrawable> cylinder = new osg::ShapeDrawable();
|
|
|
+ cylinder->setShape(new osg::Cylinder(osg::Vec3(0.0f, 0.0f, 0.0f), settings.radius, settings.length));
|
|
|
+ cylinder->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 0.2f));
|
|
|
+ geode->addDrawable(cylinder.get());
|
|
|
+ OSGWidget::fixMaterialState(geode);
|
|
|
+ _selectionRotateGroup->addChild(geode.get());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case EMFTrack: {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case SteamVRTrack: {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setVisibility(_addNewPoints);
|
|
|
+}
|
|
|
+
|
|
|
+void PickHandler::removeAllShapes() {
|
|
|
+ _selectionRotateGroup->removeChildren(0, _selectionRotateGroup->getNumChildren());
|
|
|
}
|
|
|
|
|
|
void PickHandler::setVisibility(bool mode) {
|
|
|
_selectionSwitch->setValue(0, mode);
|
|
|
}
|
|
|
+
|
|
|
+void PickHandler::addPoint(osg::Vec3 point, osg::Vec3 normal) {
|
|
|
+ switch(_activeTrackingSystem) {
|
|
|
+ case OptiTrack: {
|
|
|
+ MainWindow::getInstance()->getStore()->addOptiTrackPoint(point, normal);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case EMFTrack: {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case SteamVRTrack: {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|