Skip to content

Commit

Permalink
Gui: add function View3DInventorViewer::projectPointToLine and expose…
Browse files Browse the repository at this point in the history
… to Python
  • Loading branch information
wwmayer committed Dec 12, 2021
1 parent 4dae213 commit b6527a7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Gui/View3DInventorViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Gui/View3DInventorViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ class GuiExport View3DInventorViewer : public Quarter::SoQTQuarterAdaptor, publi
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
Expand Down
29 changes: 29 additions & 0 deletions src/Gui/View3DPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
Expand Down Expand Up @@ -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<Base::Type> types;
Expand Down
1 change: 1 addition & 0 deletions src/Gui/View3DPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class View3DInventorPy : public Py::PythonExtension<View3DInventorPy>
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&);
Expand Down

0 comments on commit b6527a7

Please sign in to comment.