Gui: add function View3DInventorViewer::projectPointToLine and expose to Python

This commit is contained in:
wmayer
2021-12-12 12:49:53 +01:00
parent 4dae213b45
commit b6527a7098
4 changed files with 53 additions and 2 deletions

View File

@@ -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)
{

View File

@@ -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

View File

@@ -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<Base::Type> types;

View File

@@ -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&);