From b6527a709882ae72bbb959f2d08e9c751dfbd96a Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 12 Dec 2021 12:49:53 +0100 Subject: [PATCH] Gui: add function View3DInventorViewer::projectPointToLine and expose to Python --- src/Gui/View3DInventorViewer.cpp | 21 +++++++++++++++++++-- src/Gui/View3DInventorViewer.h | 4 ++++ src/Gui/View3DPy.cpp | 29 +++++++++++++++++++++++++++++ src/Gui/View3DPy.h | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index b6e65286f3..4e692e41a3 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -2597,7 +2597,7 @@ SbRotation View3DInventorViewer::getCameraOrientation() const return cam->orientation.getValue(); } -SbVec3f View3DInventorViewer::getPointOnFocalPlane(const SbVec2s& pnt) const +SbVec2f View3DInventorViewer::getNormalizedPosition(const SbVec2s& pnt) const { const SbViewportRegion& vp = this->getSoRenderManager()->getViewportRegion(); @@ -2620,6 +2620,12 @@ SbVec3f View3DInventorViewer::getPointOnFocalPlane(const SbVec2s& pnt) const pY = (pY - 0.5f*dY) / fRatio + 0.5f*dY; } + return SbVec2f(pX, pY); +} + +SbVec3f View3DInventorViewer::getPointOnFocalPlane(const SbVec2s& pnt) const +{ + SbVec2f pnt2d = getNormalizedPosition(pnt); SoCamera* pCam = this->getSoRenderManager()->getCamera(); if (!pCam) return SbVec3f(); // return invalid point @@ -2636,7 +2642,7 @@ SbVec3f View3DInventorViewer::getPointOnFocalPlane(const SbVec2s& pnt) const SbLine line; SbVec3f pt; SbPlane focalPlane = vol.getPlane(focalDist); - vol.projectPointToLine(SbVec2f(pX,pY), line); + vol.projectPointToLine(pnt2d, line); focalPlane.intersect(line, pt); return pt; @@ -2718,6 +2724,17 @@ SbVec3f View3DInventorViewer::projectOnFarPlane(const SbVec2f& pt) const return pt2; } +void View3DInventorViewer::projectPointToLine(const SbVec2s& pt, SbVec3f& pt1, SbVec3f& pt2) const +{ + SbVec2f pnt2d = getNormalizedPosition(pt); + SoCamera* pCam = this->getSoRenderManager()->getCamera(); + + if (!pCam) return; + + SbViewVolume vol = pCam->getViewVolume(); + vol.projectPointToLine(pnt2d, pt1, pt2); +} + void View3DInventorViewer::toggleClippingPlane(int toggle, bool beforeEditing, bool noManip, const Base::Placement &pla) { diff --git a/src/Gui/View3DInventorViewer.h b/src/Gui/View3DInventorViewer.h index 71fb7c17d6..6006df0e25 100644 --- a/src/Gui/View3DInventorViewer.h +++ b/src/Gui/View3DInventorViewer.h @@ -311,6 +311,10 @@ public: SbVec3f projectOnNearPlane(const SbVec2f&) const; /** Project the given normalized 2d point onto the far plane */ SbVec3f projectOnFarPlane(const SbVec2f&) const; + /** Project the given 2d point to a line */ + void projectPointToLine(const SbVec2s&, SbVec3f& pt1, SbVec3f& pt2) const; + /** Get the normalized position of the 2d point. */ + SbVec2f getNormalizedPosition(const SbVec2s&) const; //@} /** @name Dimension controls diff --git a/src/Gui/View3DPy.cpp b/src/Gui/View3DPy.cpp index 2cbd9d2714..75f294ede9 100644 --- a/src/Gui/View3DPy.cpp +++ b/src/Gui/View3DPy.cpp @@ -166,6 +166,10 @@ void View3DInventorPy::init_type() "getPointOnScreen(3D vector) -> pixel coords (as integer)\n" "\n" "Return the projected 3D point (in pixel coordinates).\n"); + add_varargs_method("projectPointToLine",&View3DInventorPy::projectPointToLine, + "projectPointToLine(pixel coords (as integer)) -> line defined by two points\n" + "\n" + "Return the projecting 3D line to the given 2D point"); add_varargs_method("addEventCallback",&View3DInventorPy::addEventCallback,"addEventCallback()"); add_varargs_method("removeEventCallback",&View3DInventorPy::removeEventCallback,"removeEventCallback()"); add_varargs_method("setAnnotation",&View3DInventorPy::setAnnotation,"setAnnotation()"); @@ -1710,6 +1714,31 @@ Py::Object View3DInventorPy::getPointOnScreen(const Py::Tuple& args) } } +Py::Object View3DInventorPy::projectPointToLine(const Py::Tuple& args) +{ + short x,y; + if (!PyArg_ParseTuple(args.ptr(), "hh", &x, &y)) { + PyErr_Clear(); + Py::Tuple t(args[0]); + x = (int)Py::Int(t[0]); + y = (int)Py::Int(t[1]); + } + try { + SbVec3f pt1, pt2; + getView3DIventorPtr()->getViewer()->projectPointToLine(SbVec2s(x,y), pt1, pt2); + Py::Tuple tuple(2); + tuple.setItem(0, Py::Vector(Base::Vector3f(pt1[0], pt1[1], pt1[2]))); + tuple.setItem(1, Py::Vector(Base::Vector3f(pt2[0], pt2[1], pt2[2]))); + return tuple; + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + catch (const Py::Exception&) { + throw; + } +} + Py::Object View3DInventorPy::listNavigationTypes(const Py::Tuple&) { std::vector types; diff --git a/src/Gui/View3DPy.h b/src/Gui/View3DPy.h index 2ca76f6813..52f96a10ff 100644 --- a/src/Gui/View3DPy.h +++ b/src/Gui/View3DPy.h @@ -115,6 +115,7 @@ public: Py::Object getObjectsInfo(const Py::Tuple&); Py::Object getSize(const Py::Tuple&); Py::Object getPointOnFocalPlane(const Py::Tuple&); + Py::Object projectPointToLine(const Py::Tuple&); Py::Object getPointOnScreen(const Py::Tuple&); Py::Object addEventCallback(const Py::Tuple&); Py::Object removeEventCallback(const Py::Tuple&);