+ rework BoundingBox class and its Python binding

This commit is contained in:
wmayer
2015-10-11 00:18:53 +02:00
parent 2d7066f7d4
commit f6023d36d2
39 changed files with 1167 additions and 842 deletions

View File

@@ -26,6 +26,7 @@
#include "Base/BoundBox.h"
// inclusion of the generated files (generated out of BoundBoxPy.xml)
#include "MatrixPy.h"
#include "VectorPy.h"
#include "GeometryPyCXX.h"
#include "BoundBoxPy.h"
@@ -104,6 +105,23 @@ int BoundBoxPy::PyInit(PyObject* args, PyObject* /*kwd*/)
return -1;
}
PyObject* BoundBoxPy::setVoid(PyObject *args)
{
if (!PyArg_ParseTuple(args,""))
return 0;
getBoundBoxPtr()->SetVoid();
Py_Return;
}
PyObject* BoundBoxPy::isValid(PyObject *args)
{
if (!PyArg_ParseTuple(args,""))
return 0;
return PyBool_FromLong(getBoundBoxPtr()->IsValid() ? 1 : 0);
}
PyObject* BoundBoxPy::add(PyObject *args)
{
double x,y,z;
@@ -135,42 +153,150 @@ PyObject* BoundBoxPy::add(PyObject *args)
return 0;
}
PyObject* BoundBoxPy::isIntersection(PyObject *args)
PyObject* BoundBoxPy::getPoint(PyObject *args)
{
double x,y,z;
PyObject *object,*object2;
Py::Boolean retVal;
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
retVal = getBoundBoxPtr()->IsInBox(Vector3d(x,y,z));
}
else if (PyArg_ParseTuple(args,"O!",&PyTuple_Type, &object)) {
PyErr_Clear();
retVal = getBoundBoxPtr()->IsInBox(getVectorFromTuple<double>(object));
}
else if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
PyErr_Clear();
retVal = getBoundBoxPtr()->IsInBox(*(static_cast<Base::VectorPy*>(object)->getVectorPtr()));
}
else if (PyArg_ParseTuple(args,"O!O!",&(Base::VectorPy::Type), &object,
&(Base::VectorPy::Type), &object2)) {
PyErr_Clear();
retVal = getBoundBoxPtr()->IsCutLine(
*(static_cast<Base::VectorPy*>(object )->getVectorPtr()),
*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()));
}
else if (PyArg_ParseTuple(args,"O!;Need vector, bounding box or three floats as argument",
&(Base::BoundBoxPy::Type), &object)) {
PyErr_Clear();
retVal = getBoundBoxPtr()->IsInBox(*(static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()));
}
else {
PyErr_SetString(PyExc_TypeError, "Either three floats, Vector(s) or BoundBox expected");
int index;
if (!PyArg_ParseTuple(args,"i",&index))
return 0;
if (index < 0 || index > 7) {
PyErr_SetString (PyExc_IndexError, "Invalid bounding box");
return 0;
}
Base::Vector3d pnt = getBoundBoxPtr()->CalcPoint(index);
return new Base::VectorPy(new Base::Vector3d(pnt));
}
PyObject* BoundBoxPy::getEdge(PyObject *args)
{
int index;
if (!PyArg_ParseTuple(args,"i",&index))
return 0;
if (index < 0 || index > 11) {
PyErr_SetString (PyExc_IndexError, "Invalid bounding box");
return 0;
}
Base::Vector3d pnt1, pnt2;
getBoundBoxPtr()->CalcEdge(index, pnt1, pnt2);
Py::Tuple tuple(2);
tuple.setItem(0, Py::Vector(pnt1));
tuple.setItem(1, Py::Vector(pnt2));
return Py::new_reference_to(tuple);
}
PyObject* BoundBoxPy::closestPoint(PyObject *args)
{
double x,y,z;
PyObject *object;
Base::Vector3d vec;
do {
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
vec = Vector3d(x,y,z);
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&PyTuple_Type, &object)) {
vec = getVectorFromTuple<double>(object);
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
break;
}
else {
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
return 0;
}
}
while(false);
Base::Vector3d point = getBoundBoxPtr()->ClosestPoint(vec);
return new Base::VectorPy(new Base::Vector3d(point));
}
PyObject* BoundBoxPy::intersect(PyObject *args)
{
PyObject *object,*object2;
Py::Boolean retVal;
if (!getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box");
return 0;
}
do {
if (PyArg_ParseTuple(args,"O!O!",&(Base::VectorPy::Type), &object,
&(Base::VectorPy::Type), &object2)) {
retVal = getBoundBoxPtr()->IsCutLine(
*(static_cast<Base::VectorPy*>(object )->getVectorPtr()),
*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()));
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&(Base::BoundBoxPy::Type), &object)) {
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box argument");
return 0;
}
retVal = getBoundBoxPtr()->Intersect(*(static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()));
break;
}
PyErr_SetString(PyExc_TypeError, "Either BoundBox or two Vectors expected");
return 0;
}
while(false);
return Py::new_reference_to(retVal);
}
PyObject* BoundBoxPy::intersected(PyObject *args)
{
if (!getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box");
return 0;
}
PyObject *object;
if (!PyArg_ParseTuple(args,"O!",&(Base::BoundBoxPy::Type), &object))
return 0;
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box argument");
return 0;
}
Base::BoundBox3d bbox = getBoundBoxPtr()->Intersected(*static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr());
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
}
PyObject* BoundBoxPy::united(PyObject *args)
{
if (!getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box");
return 0;
}
PyObject *object;
if (!PyArg_ParseTuple(args,"O!",&(Base::BoundBoxPy::Type), &object))
return 0;
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box argument");
return 0;
}
Base::BoundBox3d bbox = getBoundBoxPtr()->United(*static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr());
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
}
PyObject* BoundBoxPy::enlarge(PyObject *args)
{
double s;
@@ -191,7 +317,6 @@ PyObject* BoundBoxPy::getIntersectionPoint(PyObject *args)
*(static_cast<Base::VectorPy*>(object)->getVectorPtr()),
*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()),
point, epsilon);
// IsInBox() doesn't handle border points correctly
if (ok) {
return new VectorPy(point);
}
@@ -206,26 +331,34 @@ PyObject* BoundBoxPy::getIntersectionPoint(PyObject *args)
PyObject* BoundBoxPy::move(PyObject *args)
{
double x,y,z;
double x,y,z;
PyObject *object;
Base::Vector3d vec;
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
vec = Vector3d(x,y,z);
}
else if (PyArg_ParseTuple(args,"O!:Need vector to move",&PyTuple_Type, &object)) {
do {
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
vec = Vector3d(x,y,z);
break;
}
PyErr_Clear();
vec = getVectorFromTuple<double>(object);
}
else if (PyArg_ParseTuple(args,"O!:Need vector to move",&(Base::VectorPy::Type), &object)) {
if (PyArg_ParseTuple(args,"O!:Need vector to move",&PyTuple_Type, &object)) {
vec = getVectorFromTuple<double>(object);
break;
}
PyErr_Clear();
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
}
else {
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
return 0;
if (PyArg_ParseTuple(args,"O!:Need vector to move",&(Base::VectorPy::Type), &object)) {
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
break;
}
else {
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
return 0;
}
}
while(false);
getBoundBoxPtr()->MoveX(vec.x);
getBoundBoxPtr()->MoveY(vec.y);
@@ -234,11 +367,67 @@ PyObject* BoundBoxPy::move(PyObject *args)
Py_Return;
}
PyObject* BoundBoxPy::scale(PyObject *args)
{
double x,y,z;
PyObject *object;
Base::Vector3d vec;
do {
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
vec = Vector3d(x,y,z);
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!:Need vector to scale",&PyTuple_Type, &object)) {
vec = getVectorFromTuple<double>(object);
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!:Need vector to scale",&(Base::VectorPy::Type), &object)) {
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
break;
}
else {
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
return 0;
}
}
while(false);
getBoundBoxPtr()->ScaleX(vec.x);
getBoundBoxPtr()->ScaleY(vec.y);
getBoundBoxPtr()->ScaleZ(vec.z);
Py_Return;
}
PyObject* BoundBoxPy::transformed(PyObject *args)
{
PyObject *mat;
if (!PyArg_ParseTuple(args,"O!", &(Base::MatrixPy::Type), &mat))
return 0;
if (!getBoundBoxPtr()->IsValid())
throw Py::FloatingPointError("Cannot transform invalid bounding box");
Base::BoundBox3d bbox = getBoundBoxPtr()->Transformed(*static_cast<Base::MatrixPy*>(mat)->getMatrixPtr());
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
}
PyObject* BoundBoxPy::isCutPlane(PyObject *args)
{
PyObject *object,*object2;
Py::Boolean retVal;
if (!getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box");
return 0;
}
if (PyArg_ParseTuple(args,"O!O!:Need base and normal vector of a plane",
&(Base::VectorPy::Type), &object,&(Base::VectorPy::Type), &object2))
retVal = getBoundBoxPtr()->IsCutPlane(
@@ -252,30 +441,54 @@ PyObject* BoundBoxPy::isCutPlane(PyObject *args)
PyObject* BoundBoxPy::isInside(PyObject *args)
{
double x,y,z;
PyObject *object;
Py::Boolean retVal;
if (!PyArg_ParseTuple(args,"O", &object))
return 0;
if (PyObject_TypeCheck(object, &(Base::VectorPy::Type))) {
Base::VectorPy *vec = static_cast<Base::VectorPy*>(object);
retVal = getBoundBoxPtr()->IsInBox(*vec->getVectorPtr());
}
else if (PyObject_TypeCheck(object, &(Base::BoundBoxPy::Type))) {
Base::BoundBoxPy *box = static_cast<Base::BoundBoxPy*>(object);
retVal = getBoundBoxPtr()->IsInBox(*box->getBoundBoxPtr());
}
else {
PyErr_SetString(PyExc_TypeError, "Either a Vector or BoundBox object expected");
if (!getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box");
return 0;
}
do {
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
retVal = getBoundBoxPtr()->IsInBox(Vector3d(x,y,z));
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&PyTuple_Type, &object)) {
retVal = getBoundBoxPtr()->IsInBox(getVectorFromTuple<double>(object));
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
retVal = getBoundBoxPtr()->IsInBox(*(static_cast<Base::VectorPy*>(object)->getVectorPtr()));
break;
}
PyErr_Clear();
if (PyArg_ParseTuple(args,"O!",&(Base::BoundBoxPy::Type), &object)) {
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
PyErr_SetString (PyExc_FloatingPointError, "Invalid bounding box argument");
return 0;
}
retVal = getBoundBoxPtr()->IsInBox(*(static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()));
break;
}
PyErr_SetString(PyExc_TypeError, "Either three floats, Vector(s) or BoundBox expected");
return 0;
}
while(false);
return Py::new_reference_to(retVal);
}
Py::Object BoundBoxPy::getCenter(void) const
{
return Py::Vector(getBoundBoxPtr()->CalcCenter());
return Py::Vector(getBoundBoxPtr()->GetCenter());
}
Py::Float BoundBoxPy::getXMax(void) const
@@ -355,6 +568,8 @@ Py::Float BoundBoxPy::getZLength(void) const
Py::Float BoundBoxPy::getDiagonalLength(void) const
{
if (!getBoundBoxPtr()->IsValid())
throw Py::FloatingPointError("Cannot deterine diagonal length of invalid bounding box");
return Py::Float(getBoundBoxPtr()->CalcDiagonalLength());
}