Skip to content

Commit

Permalink
+ Expose method getPointOnScreen() to Python
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 20, 2013
1 parent f4da53b commit f5a4c28
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Gui/View3DPy.cpp
Expand Up @@ -125,6 +125,10 @@ void View3DInventorPy::init_type()
"\n"
"Return the according 3D point on the focal plane to the given 2D point (in\n"
"pixel coordinates).\n");
add_varargs_method("getPointOnScreen",&View3DInventorPy::getPointOnScreen,
"getPointOnScreen(3D vector) -> pixel coords (as integer)\n"
"\n"
"Return the projected 3D point (in pixel coordinates).\n");
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 @@ -1183,6 +1187,56 @@ Py::Object View3DInventorPy::getPoint(const Py::Tuple& args)
}
}

Py::Object View3DInventorPy::getPointOnScreen(const Py::Tuple& args)
{
PyObject* v;
double vx,vy,vz;
if (PyArg_ParseTuple(args.ptr(), "O!", &Base::VectorPy::Type, &v)) {
Base::Vector3d* vec = static_cast<Base::VectorPy*>(v)->getVectorPtr();
vx = vec->x;
vy = vec->y;
vz = vec->z;
}
else {
PyErr_Clear();
if (!PyArg_ParseTuple(args.ptr(), "ddd", &vx,&vy,&vz)) {
throw Py::Exception("Wrong argument, Vector or three floats expected expected");
}
}

try {
const SbViewportRegion& vp = _view->getViewer()->getViewportRegion();
float fRatio = vp.getViewportAspectRatio();
const SbVec2s& sp = vp.getViewportSizePixels();
//float dX, dY; vp.getViewportSize().getValue(dX, dY);
SbViewVolume vv = _view->getViewer()->getCamera()->getViewVolume(fRatio);

SbVec3f pt(vx,vy,vz);
vv.projectToScreen(pt, pt);

//if (fRatio > 1.0f) {
// pt[0] = (pt[0] - 0.5f*dX) / fRatio + 0.5f*dX;
//}
//else {
// pt[1] = (pt[1] - 0.5f*dY) * fRatio + 0.5f*dY;
//}

int x = pt[0] * sp[0];
int y = pt[1] * sp[1];
Py::Tuple tuple(2);
tuple.setItem(0, Py::Int(x));
tuple.setItem(1, Py::Int(y));

return tuple;
}
catch (const Base::Exception& e) {
throw Py::Exception(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
Expand Up @@ -82,6 +82,7 @@ class View3DInventorPy : public Py::PythonExtension<View3DInventorPy>
Py::Object getObjectsInfo(const Py::Tuple&);
Py::Object getSize(const Py::Tuple&);
Py::Object getPoint(const Py::Tuple&);
Py::Object getPointOnScreen(const Py::Tuple&);
Py::Object addEventCallback(const Py::Tuple&);
Py::Object removeEventCallback(const Py::Tuple&);
Py::Object setAnnotation(const Py::Tuple&);
Expand Down

0 comments on commit f5a4c28

Please sign in to comment.