1
/***************************************************************************
2
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
26
// inclusion of the generated files (generated out of BoundBoxPy.xml)
29
#include "GeometryPyCXX.h"
30
#include "BoundBoxPy.h"
31
#include "BoundBoxPy.cpp"
35
// returns a string which represent the object e.g. when printed in python
36
std::string BoundBoxPy::representation() const
38
std::stringstream str;
40
str << getBoundBoxPtr()->MinX << ", " << getBoundBoxPtr()->MinY << ", "
41
<< getBoundBoxPtr()->MinZ << ", " << getBoundBoxPtr()->MaxX << ", "
42
<< getBoundBoxPtr()->MaxY << ", " << getBoundBoxPtr()->MaxZ;
48
PyObject* BoundBoxPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/)
50
// create a new instance of BoundBoxPy and the Twin object
51
return new BoundBoxPy(new BoundBox3d);
55
int BoundBoxPy::PyInit(PyObject* args, PyObject* /*kwd*/)
57
if (PyArg_ParseTuple(args, "")) {
60
PyErr_Clear(); // set by PyArg_ParseTuple()
70
BoundBoxPy::PointerType ptr = getBoundBoxPtr();
71
if (PyArg_ParseTuple(args, "d|ddddd", &xMin, &yMin, &zMin, &xMax, &yMax, &zMax)) {
80
PyErr_Clear(); // set by PyArg_ParseTuple()
81
if (PyArg_ParseTuple(args, "O!O!", &PyTuple_Type, &object1, &PyTuple_Type, &object2)) {
83
Vector3d v1 = getVectorFromTuple<double>(object1);
84
Vector3d v2 = getVectorFromTuple<double>(object2);
89
catch (const Py::Exception&) {
93
PyErr_Clear(); // set by PyArg_ParseTuple()
94
if (PyArg_ParseTuple(args,
96
&(Base::VectorPy::Type),
98
&(Base::VectorPy::Type),
100
ptr->Add(*(static_cast<Base::VectorPy*>(object1)->getVectorPtr()));
101
ptr->Add(*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()));
104
PyErr_Clear(); // set by PyArg_ParseTuple()
105
if (PyArg_ParseTuple(args, "O!", &(Base::BoundBoxPy::Type), &object1)) {
106
*ptr = *(static_cast<Base::BoundBoxPy*>(object1)->getBoundBoxPtr());
110
PyErr_SetString(PyExc_TypeError,
111
"Either six floats, two instances of "
112
"Vector/Tuple or instance of BoundBox expected");
116
PyObject* BoundBoxPy::setVoid(PyObject* args)
118
if (!PyArg_ParseTuple(args, "")) {
122
getBoundBoxPtr()->SetVoid();
126
PyObject* BoundBoxPy::isValid(PyObject* args)
128
if (!PyArg_ParseTuple(args, "")) {
132
return PyBool_FromLong(getBoundBoxPtr()->IsValid() ? 1 : 0);
135
PyObject* BoundBoxPy::add(PyObject* args)
141
if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
142
getBoundBoxPtr()->Add(Vector3d(x, y, z));
147
if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
148
getBoundBoxPtr()->Add(getVectorFromTuple<double>(object));
153
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &object)) {
154
getBoundBoxPtr()->Add(*(static_cast<Base::VectorPy*>(object)->getVectorPtr()));
159
if (PyArg_ParseTuple(args,
160
"O!;Need a Vector, BoundBox or three floats as argument",
161
&(Base::BoundBoxPy::Type),
163
getBoundBoxPtr()->Add(*(static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()));
167
PyErr_SetString(PyExc_TypeError,
168
"Either three floats, instance of Vector or instance of BoundBox expected");
172
PyObject* BoundBoxPy::getPoint(PyObject* args)
174
unsigned short index {};
175
if (!PyArg_ParseTuple(args, "H", &index)) {
180
PyErr_SetString(PyExc_IndexError, "Invalid point index");
184
Base::Vector3d pnt = getBoundBoxPtr()->CalcPoint(index);
185
return new Base::VectorPy(new Base::Vector3d(pnt));
188
PyObject* BoundBoxPy::getEdge(PyObject* args)
190
unsigned short index {};
191
if (!PyArg_ParseTuple(args, "H", &index)) {
196
PyErr_SetString(PyExc_IndexError, "Invalid edge index");
202
getBoundBoxPtr()->CalcEdge(index, pnt1, pnt2);
204
tuple.setItem(0, Py::Vector(pnt1));
205
tuple.setItem(1, Py::Vector(pnt2));
206
return Py::new_reference_to(tuple);
209
PyObject* BoundBoxPy::closestPoint(PyObject* args)
219
if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
220
vec = Vector3d(x, y, z);
225
if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
226
vec = getVectorFromTuple<double>(object);
231
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &object)) {
232
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
236
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
240
Base::Vector3d point = getBoundBoxPtr()->ClosestPoint(vec);
241
return new Base::VectorPy(new Base::Vector3d(point));
244
PyObject* BoundBoxPy::intersect(PyObject* args)
246
PyObject* object1 {};
247
PyObject* object2 {};
250
if (!getBoundBoxPtr()->IsValid()) {
251
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
256
if (PyArg_ParseTuple(args,
258
&(Base::VectorPy::Type),
260
&(Base::VectorPy::Type),
262
retVal = getBoundBoxPtr()->IsCutLine(
263
*(static_cast<Base::VectorPy*>(object1)->getVectorPtr()),
264
*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()));
269
if (PyArg_ParseTuple(args, "O!", &(Base::BoundBoxPy::Type), &object1)) {
270
if (!static_cast<Base::BoundBoxPy*>(object1)->getBoundBoxPtr()->IsValid()) {
271
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
274
retVal = getBoundBoxPtr()->Intersect(
275
*(static_cast<Base::BoundBoxPy*>(object1)->getBoundBoxPtr()));
279
PyErr_SetString(PyExc_TypeError, "Either BoundBox or two Vectors expected");
283
return Py::new_reference_to(retVal);
286
PyObject* BoundBoxPy::intersected(PyObject* args)
288
if (!getBoundBoxPtr()->IsValid()) {
289
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
294
if (!PyArg_ParseTuple(args, "O!", &(Base::BoundBoxPy::Type), &object)) {
297
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
298
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
302
Base::BoundBox3d bbox =
303
getBoundBoxPtr()->Intersected(*static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr());
304
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
307
PyObject* BoundBoxPy::united(PyObject* args)
309
if (!getBoundBoxPtr()->IsValid()) {
310
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
315
if (!PyArg_ParseTuple(args, "O!", &(Base::BoundBoxPy::Type), &object)) {
318
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
319
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
323
Base::BoundBox3d bbox =
324
getBoundBoxPtr()->United(*static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr());
325
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
328
PyObject* BoundBoxPy::enlarge(PyObject* args)
331
if (!PyArg_ParseTuple(args, "d;Need float parameter to enlarge", &s)) {
334
getBoundBoxPtr()->Enlarge(s);
338
PyObject* BoundBoxPy::getIntersectionPoint(PyObject* args)
340
PyObject* object1 {};
341
PyObject* object2 {};
342
double epsilon = 0.0001;
343
if (!PyArg_ParseTuple(args,
344
"O!O!|d;Need base and direction vector",
345
&(Base::VectorPy::Type),
347
&(Base::VectorPy::Type),
353
Base::Vector3d point;
354
bool ok = getBoundBoxPtr()->IntersectionPoint(
355
*(static_cast<Base::VectorPy*>(object1)->getVectorPtr()),
356
*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()),
360
return new VectorPy(point);
363
PyErr_SetString(Base::PyExc_FC_GeneralError, "No intersection");
367
PyObject* BoundBoxPy::move(PyObject* args)
377
if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
378
vec = Vector3d(x, y, z);
383
if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
384
vec = getVectorFromTuple<double>(object);
389
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &object)) {
390
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
394
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
398
getBoundBoxPtr()->MoveX(vec.x);
399
getBoundBoxPtr()->MoveY(vec.y);
400
getBoundBoxPtr()->MoveZ(vec.z);
405
PyObject* BoundBoxPy::scale(PyObject* args)
415
if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
416
vec = Vector3d(x, y, z);
421
if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
422
vec = getVectorFromTuple<double>(object);
427
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &object)) {
428
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
432
PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
436
getBoundBoxPtr()->ScaleX(vec.x);
437
getBoundBoxPtr()->ScaleY(vec.y);
438
getBoundBoxPtr()->ScaleZ(vec.z);
443
PyObject* BoundBoxPy::transformed(PyObject* args)
447
if (!PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type), &mat)) {
451
if (!getBoundBoxPtr()->IsValid()) {
452
throw Py::FloatingPointError("Cannot transform invalid bounding box");
454
Base::BoundBox3d bbox =
455
getBoundBoxPtr()->Transformed(*static_cast<Base::MatrixPy*>(mat)->getMatrixPtr());
456
return new Base::BoundBoxPy(new Base::BoundBox3d(bbox));
459
PyObject* BoundBoxPy::isCutPlane(PyObject* args)
462
PyObject* object2 {};
465
if (!getBoundBoxPtr()->IsValid()) {
466
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
470
if (!PyArg_ParseTuple(args,
471
"O!O!;Need base and normal vector of a plane",
472
&(Base::VectorPy::Type),
474
&(Base::VectorPy::Type),
479
retVal = getBoundBoxPtr()->IsCutPlane(*(static_cast<Base::VectorPy*>(object)->getVectorPtr()),
480
*(static_cast<Base::VectorPy*>(object2)->getVectorPtr()));
482
return Py::new_reference_to(retVal);
485
PyObject* BoundBoxPy::isInside(PyObject* args)
493
if (!getBoundBoxPtr()->IsValid()) {
494
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
499
if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
500
retVal = getBoundBoxPtr()->IsInBox(Vector3d(x, y, z));
505
if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
506
retVal = getBoundBoxPtr()->IsInBox(getVectorFromTuple<double>(object));
511
if (PyArg_ParseTuple(args, "O!", &(Base::VectorPy::Type), &object)) {
513
getBoundBoxPtr()->IsInBox(*(static_cast<Base::VectorPy*>(object)->getVectorPtr()));
518
if (PyArg_ParseTuple(args, "O!", &(Base::BoundBoxPy::Type), &object)) {
519
if (!static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
520
PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
523
retVal = getBoundBoxPtr()->IsInBox(
524
*(static_cast<Base::BoundBoxPy*>(object)->getBoundBoxPtr()));
528
PyErr_SetString(PyExc_TypeError, "Either three floats, Vector(s) or BoundBox expected");
532
return Py::new_reference_to(retVal);
535
Py::Object BoundBoxPy::getCenter() const
537
return Py::Vector(getBoundBoxPtr()->GetCenter()); // NOLINT
540
Py::Float BoundBoxPy::getXMax() const
542
return Py::Float(getBoundBoxPtr()->MaxX);
545
void BoundBoxPy::setXMax(Py::Float arg)
547
getBoundBoxPtr()->MaxX = arg;
550
Py::Float BoundBoxPy::getYMax() const
552
return Py::Float(getBoundBoxPtr()->MaxY);
555
void BoundBoxPy::setYMax(Py::Float arg)
557
getBoundBoxPtr()->MaxY = arg;
560
Py::Float BoundBoxPy::getZMax() const
562
return Py::Float(getBoundBoxPtr()->MaxZ);
565
void BoundBoxPy::setZMax(Py::Float arg)
567
getBoundBoxPtr()->MaxZ = arg;
570
Py::Float BoundBoxPy::getXMin() const
572
return Py::Float(getBoundBoxPtr()->MinX);
575
void BoundBoxPy::setXMin(Py::Float arg)
577
getBoundBoxPtr()->MinX = arg;
580
Py::Float BoundBoxPy::getYMin() const
582
return Py::Float(getBoundBoxPtr()->MinY);
585
void BoundBoxPy::setYMin(Py::Float arg)
587
getBoundBoxPtr()->MinY = arg;
590
Py::Float BoundBoxPy::getZMin() const
592
return Py::Float(getBoundBoxPtr()->MinZ);
595
void BoundBoxPy::setZMin(Py::Float arg)
597
getBoundBoxPtr()->MinZ = arg;
600
Py::Float BoundBoxPy::getXLength() const
602
return Py::Float(getBoundBoxPtr()->LengthX());
605
Py::Float BoundBoxPy::getYLength() const
607
return Py::Float(getBoundBoxPtr()->LengthY());
610
Py::Float BoundBoxPy::getZLength() const
612
return Py::Float(getBoundBoxPtr()->LengthZ());
615
Py::Float BoundBoxPy::getDiagonalLength() const
617
if (!getBoundBoxPtr()->IsValid()) {
618
throw Py::FloatingPointError("Cannot determine diagonal length of invalid bounding box");
620
return Py::Float(getBoundBoxPtr()->CalcDiagonalLength());
623
PyObject* BoundBoxPy::getCustomAttributes(const char* /*attr*/) const
628
int BoundBoxPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)