Debian Codegraph @

src/Base/BoundBoxPyImp.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include "GeometryPyCXX.h"
26
27 // generated out of BoundBox.pyi
28 #include "BoundBoxPy.h"
29 #include "BoundBoxPy.cpp"
30
31 #include "MatrixPy.h"
32 #include "VectorPy.h"
33
34 using namespace Base;
35
36 // returns a string which represent the object e.g. when printed in python
37 std::string BoundBoxPy::representation() const
38 {
39 std::stringstream str;
40 str << "BoundBox (";
41 str << getBoundBoxPtr()->MinX << ", " << getBoundBoxPtr()->MinY << ", "
42 << getBoundBoxPtr()->MinZ << ", " << getBoundBoxPtr()->MaxX << ", "
43 << getBoundBoxPtr()->MaxY << ", " << getBoundBoxPtr()->MaxZ;
44 str << ")";
45
46 return str.str();
47 }
48
49 PyObject* BoundBoxPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/)
50 {
51 // create a new instance of BoundBoxPy and the Twin object
52 return new BoundBoxPy(new BoundBox3d);
53 }
54
55 // constructor method
56 int BoundBoxPy::PyInit(PyObject* args, PyObject* /*kwd*/)
57 {
58 if (PyArg_ParseTuple(args, "")) {
59 return 0;
60 }
61 PyErr_Clear(); // set by PyArg_ParseTuple()
62
63 double xMin = 0.0;
64 double yMin = 0.0;
65 double zMin = 0.0;
66 double xMax = 0.0;
67 double yMax = 0.0;
68 double zMax = 0.0;
69 PyObject* object1 {};
70 PyObject* object2 {};
71 BoundBoxPy::PointerType ptr = getBoundBoxPtr();
72 if (PyArg_ParseTuple(args, "d|ddddd", &xMin, &yMin, &zMin, &xMax, &yMax, &zMax)) {
73 ptr->MaxX = xMax;
74 ptr->MaxY = yMax;
75 ptr->MaxZ = zMax;
76 ptr->MinX = xMin;
77 ptr->MinY = yMin;
78 ptr->MinZ = zMin;
79 return 0;
80 }
81 PyErr_Clear(); // set by PyArg_ParseTuple()
82 if (PyArg_ParseTuple(args, "O!O!", &PyTuple_Type, &object1, &PyTuple_Type, &object2)) {
83 try {
84 Vector3d v1 = getVectorFromTuple<double>(object1);
85 Vector3d v2 = getVectorFromTuple<double>(object2);
86 ptr->Add(v1);
87 ptr->Add(v2);
88 return 0;
89 }
90 catch (const Py::Exception&) {
91 return -1;
92 }
93 }
94 PyErr_Clear(); // set by PyArg_ParseTuple()
95 if (PyArg_ParseTuple(args, "O!O!", &(VectorPy::Type), &object1, &(VectorPy::Type), &object2)) {
96 ptr->Add(*(static_cast<VectorPy*>(object1)->getVectorPtr()));
97 ptr->Add(*(static_cast<VectorPy*>(object2)->getVectorPtr()));
98 return 0;
99 }
100 PyErr_Clear(); // set by PyArg_ParseTuple()
101 if (PyArg_ParseTuple(args, "O!", &(BoundBoxPy::Type), &object1)) {
102 *ptr = *(static_cast<BoundBoxPy*>(object1)->getBoundBoxPtr());
103 return 0;
104 }
105
106 PyErr_SetString(
107 PyExc_TypeError,
108 "Either six floats, two instances of "
109 "Vector/Tuple or instance of BoundBox expected"
110 );
111 return -1;
112 }
113
114 PyObject* BoundBoxPy::setVoid(PyObject* args)
115 {
116 if (!PyArg_ParseTuple(args, "")) {
117 return nullptr;
118 }
119
120 getBoundBoxPtr()->SetVoid();
121 Py_Return;
122 }
123
124 PyObject* BoundBoxPy::isValid(PyObject* args) const
125 {
126 if (!PyArg_ParseTuple(args, "")) {
127 return nullptr;
128 }
129
130 return PyBool_FromLong(getBoundBoxPtr()->IsValid() ? 1 : 0);
131 }
132
133 PyObject* BoundBoxPy::add(PyObject* args)
134 {
135 double x {};
136 double y {};
137 double z {};
138 PyObject* object {};
139 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
140 getBoundBoxPtr()->Add(Vector3d(x, y, z));
141 Py_Return;
142 }
143
144 PyErr_Clear();
145 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
146 getBoundBoxPtr()->Add(getVectorFromTuple<double>(object));
147 Py_Return;
148 }
149
150 PyErr_Clear();
151 if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &object)) {
152 getBoundBoxPtr()->Add(*(static_cast<VectorPy*>(object)->getVectorPtr()));
153 Py_Return;
154 }
155
156 PyErr_Clear();
157 if (PyArg_ParseTuple(
158 args,
159 "O!;Need a Vector, BoundBox or three floats as argument",
160 &(BoundBoxPy::Type),
161 &object
162 )) {
163 getBoundBoxPtr()->Add(*(static_cast<BoundBoxPy*>(object)->getBoundBoxPtr()));
164 Py_Return;
165 }
166
167 PyErr_SetString(
168 PyExc_TypeError,
169 "Either three floats, instance of Vector or instance of BoundBox expected"
170 );
171 return nullptr;
172 }
173
174 PyObject* BoundBoxPy::getPoint(PyObject* args) const
175 {
176 unsigned short index {};
177 if (!PyArg_ParseTuple(args, "H", &index)) {
178 return nullptr;
179 }
180
181 if (index > 7) {
182 PyErr_SetString(PyExc_IndexError, "Invalid point index");
183 return nullptr;
184 }
185
186 return new VectorPy(new Vector3d(getBoundBoxPtr()->CalcPoint(index)));
187 }
188
189 PyObject* BoundBoxPy::getEdge(PyObject* args) const
190 {
191 unsigned short index {};
192 if (!PyArg_ParseTuple(args, "H", &index)) {
193 return nullptr;
194 }
195
196 if (index > 11) {
197 PyErr_SetString(PyExc_IndexError, "Invalid edge index");
198 return nullptr;
199 }
200
201 Vector3d pnt1;
202 Vector3d pnt2;
203 getBoundBoxPtr()->CalcEdge(index, pnt1, pnt2);
204 Py::Tuple tuple(2);
205 tuple.setItem(0, Py::Vector(pnt1));
206 tuple.setItem(1, Py::Vector(pnt2));
207 return Py::new_reference_to(tuple);
208 }
209
210 PyObject* BoundBoxPy::closestPoint(PyObject* args) const
211 {
212 double x {};
213 double y {};
214 double z {};
215 PyObject* object {};
216
217 Vector3d vec;
218
219 do {
220 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
221 vec = Vector3d(x, y, z);
222 break;
223 }
224
225 PyErr_Clear();
226 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
227 vec = getVectorFromTuple<double>(object);
228 break;
229 }
230
231 PyErr_Clear();
232 if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &object)) {
233 vec = *(static_cast<VectorPy*>(object)->getVectorPtr());
234 break;
235 }
236
237 PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
238 return nullptr;
239 } while (false);
240
241 Vector3d point = getBoundBoxPtr()->ClosestPoint(vec);
242 return new VectorPy(new Vector3d(point));
243 }
244
245 PyObject* BoundBoxPy::intersect(PyObject* args)
246 {
247 PyObject* object1 {};
248 PyObject* object2 {};
249 Py::Boolean retVal;
250
251 if (!getBoundBoxPtr()->IsValid()) {
252 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
253 return nullptr;
254 }
255
256 do {
257 if (PyArg_ParseTuple(args, "O!O!", &(VectorPy::Type), &object1, &(VectorPy::Type), &object2)) {
258 retVal = getBoundBoxPtr()->IsCutLine(
259 *(static_cast<VectorPy*>(object1)->getVectorPtr()),
260 *(static_cast<VectorPy*>(object2)->getVectorPtr())
261 );
262 break;
263 }
264
265 PyErr_Clear();
266 if (PyArg_ParseTuple(args, "O!", &(BoundBoxPy::Type), &object1)) {
267 if (!static_cast<BoundBoxPy*>(object1)->getBoundBoxPtr()->IsValid()) {
268 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
269 return nullptr;
270 }
271 retVal = getBoundBoxPtr()->Intersect(
272 *(static_cast<BoundBoxPy*>(object1)->getBoundBoxPtr())
273 );
274 break;
275 }
276
277 PyErr_SetString(PyExc_TypeError, "Either BoundBox or two Vectors expected");
278 return nullptr;
279 } while (false);
280
281 return Py::new_reference_to(retVal);
282 }
283
284 PyObject* BoundBoxPy::intersected(PyObject* args)
285 {
286 if (!getBoundBoxPtr()->IsValid()) {
287 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
288 return nullptr;
289 }
290
291 PyObject* object {};
292 if (!PyArg_ParseTuple(args, "O!", &(BoundBoxPy::Type), &object)) {
293 return nullptr;
294 }
295 if (!static_cast<BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
296 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
297 return nullptr;
298 }
299
300 BoundBox3d bbox = getBoundBoxPtr()->Intersected(
301 *static_cast<BoundBoxPy*>(object)->getBoundBoxPtr()
302 );
303 return new BoundBoxPy(new BoundBox3d(bbox));
304 }
305
306 PyObject* BoundBoxPy::united(PyObject* args)
307 {
308 if (!getBoundBoxPtr()->IsValid()) {
309 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
310 return nullptr;
311 }
312
313 PyObject* object {};
314 if (!PyArg_ParseTuple(args, "O!", &(BoundBoxPy::Type), &object)) {
315 return nullptr;
316 }
317 if (!static_cast<BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
318 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
319 return nullptr;
320 }
321
322 BoundBox3d bbox = getBoundBoxPtr()->United(*static_cast<BoundBoxPy*>(object)->getBoundBoxPtr());
323 return new BoundBoxPy(new BoundBox3d(bbox));
324 }
325
326 PyObject* BoundBoxPy::enlarge(PyObject* args)
327 {
328 double s {};
329 if (!PyArg_ParseTuple(args, "d;Need float parameter to enlarge", &s)) {
330 return nullptr;
331 }
332 getBoundBoxPtr()->Enlarge(s);
333 Py_Return;
334 }
335
336 PyObject* BoundBoxPy::getIntersectionPoint(PyObject* args)
337 {
338 PyObject* object1 {};
339 PyObject* object2 {};
340 double epsilon = 0.0001;
341 if (!PyArg_ParseTuple(
342 args,
343 "O!O!|d;Need base and direction vector",
344 &(VectorPy::Type),
345 &object1,
346 &(VectorPy::Type),
347 &object2,
348 &epsilon
349 )) {
350 return nullptr;
351 }
352
353 Vector3d point;
354 bool ok = getBoundBoxPtr()->IntersectionPoint(
355 *(static_cast<VectorPy*>(object1)->getVectorPtr()),
356 *(static_cast<VectorPy*>(object2)->getVectorPtr()),
357 point,
358 epsilon
359 );
360 if (ok) {
361 return new VectorPy(point);
362 }
363
364 PyErr_SetString(PyExc_FC_GeneralError, "No intersection");
365 return nullptr;
366 }
367
368 PyObject* BoundBoxPy::move(PyObject* args)
369 {
370 double x {};
371 double y {};
372 double z {};
373 PyObject* object {};
374
375 Vector3d vec;
376
377 do {
378 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
379 vec = Vector3d(x, y, z);
380 break;
381 }
382
383 PyErr_Clear();
384 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
385 vec = getVectorFromTuple<double>(object);
386 break;
387 }
388
389 PyErr_Clear();
390 if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &object)) {
391 vec = *(static_cast<VectorPy*>(object)->getVectorPtr());
392 break;
393 }
394
395 PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
396 return nullptr;
397 } while (false);
398
399 getBoundBoxPtr()->MoveX(vec.x);
400 getBoundBoxPtr()->MoveY(vec.y);
401 getBoundBoxPtr()->MoveZ(vec.z);
402
403 Py_Return;
404 }
405
406 PyObject* BoundBoxPy::scale(PyObject* args)
407 {
408 double x {};
409 double y {};
410 double z {};
411 PyObject* object {};
412
413 Vector3d vec;
414
415 do {
416 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
417 vec = Vector3d(x, y, z);
418 break;
419 }
420
421 PyErr_Clear();
422 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
423 vec = getVectorFromTuple<double>(object);
424 break;
425 }
426
427 PyErr_Clear();
428 if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &object)) {
429 vec = *(static_cast<VectorPy*>(object)->getVectorPtr());
430 break;
431 }
432
433 PyErr_SetString(PyExc_TypeError, "Either three floats or vector expected");
434 return nullptr;
435 } while (false);
436
437 getBoundBoxPtr()->ScaleX(vec.x);
438 getBoundBoxPtr()->ScaleY(vec.y);
439 getBoundBoxPtr()->ScaleZ(vec.z);
440
441 Py_Return;
442 }
443
444 PyObject* BoundBoxPy::transformed(PyObject* args)
445 {
446 PyObject* mat {};
447
448 if (!PyArg_ParseTuple(args, "O!", &(MatrixPy::Type), &mat)) {
449 return nullptr;
450 }
451
452 if (!getBoundBoxPtr()->IsValid()) {
453 throw Py::FloatingPointError("Cannot transform invalid bounding box");
454 }
455 BoundBox3d bbox = getBoundBoxPtr()->Transformed(*static_cast<MatrixPy*>(mat)->getMatrixPtr());
456 return new BoundBoxPy(new BoundBox3d(bbox));
457 }
458
459 PyObject* BoundBoxPy::isCutPlane(PyObject* args)
460 {
461 PyObject* object {};
462 PyObject* object2 {};
463 Py::Boolean retVal;
464
465 if (!getBoundBoxPtr()->IsValid()) {
466 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
467 return nullptr;
468 }
469
470 if (!PyArg_ParseTuple(
471 args,
472 "O!O!;Need base and normal vector of a plane",
473 &(VectorPy::Type),
474 &object,
475 &(VectorPy::Type),
476 &object2
477 )) {
478 return nullptr;
479 }
480
481 retVal = getBoundBoxPtr()->IsCutPlane(
482 *(static_cast<VectorPy*>(object)->getVectorPtr()),
483 *(static_cast<VectorPy*>(object2)->getVectorPtr())
484 );
485
486 return Py::new_reference_to(retVal);
487 }
488
489 PyObject* BoundBoxPy::isInside(PyObject* args)
490 {
491 double x {};
492 double y {};
493 double z {};
494 PyObject* object {};
495 Py::Boolean retVal;
496
497 if (!getBoundBoxPtr()->IsValid()) {
498 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box");
499 return nullptr;
500 }
501
502 do {
503 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
504 retVal = getBoundBoxPtr()->IsInBox(Vector3d(x, y, z));
505 break;
506 }
507
508 PyErr_Clear();
509 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &object)) {
510 retVal = getBoundBoxPtr()->IsInBox(getVectorFromTuple<double>(object));
511 break;
512 }
513
514 PyErr_Clear();
515 if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &object)) {
516 retVal = getBoundBoxPtr()->IsInBox(*(static_cast<VectorPy*>(object)->getVectorPtr()));
517 break;
518 }
519
520 PyErr_Clear();
521 if (PyArg_ParseTuple(args, "O!", &(BoundBoxPy::Type), &object)) {
522 if (!static_cast<BoundBoxPy*>(object)->getBoundBoxPtr()->IsValid()) {
523 PyErr_SetString(PyExc_FloatingPointError, "Invalid bounding box argument");
524 return nullptr;
525 }
526 retVal = getBoundBoxPtr()->IsInBox(*(static_cast<BoundBoxPy*>(object)->getBoundBoxPtr()));
527 break;
528 }
529
530 PyErr_SetString(PyExc_TypeError, "Either three floats, Vector(s) or BoundBox expected");
531 return nullptr;
532 } while (false);
533
534 return Py::new_reference_to(retVal);
535 }
536
537 Py::Object BoundBoxPy::getCenter() const
538 {
539 return Py::Vector(getBoundBoxPtr()->GetCenter()); // NOLINT
540 }
541
542 Py::Float BoundBoxPy::getXMax() const
543 {
544 return Py::Float(getBoundBoxPtr()->MaxX);
545 }
546
547 void BoundBoxPy::setXMax(Py::Float arg)
548 {
549 getBoundBoxPtr()->MaxX = arg;
550 }
551
552 Py::Float BoundBoxPy::getYMax() const
553 {
554 return Py::Float(getBoundBoxPtr()->MaxY);
555 }
556
557 void BoundBoxPy::setYMax(Py::Float arg)
558 {
559 getBoundBoxPtr()->MaxY = arg;
560 }
561
562 Py::Float BoundBoxPy::getZMax() const
563 {
564 return Py::Float(getBoundBoxPtr()->MaxZ);
565 }
566
567 void BoundBoxPy::setZMax(Py::Float arg)
568 {
569 getBoundBoxPtr()->MaxZ = arg;
570 }
571
572 Py::Float BoundBoxPy::getXMin() const
573 {
574 return Py::Float(getBoundBoxPtr()->MinX);
575 }
576
577 void BoundBoxPy::setXMin(Py::Float arg)
578 {
579 getBoundBoxPtr()->MinX = arg;
580 }
581
582 Py::Float BoundBoxPy::getYMin() const
583 {
584 return Py::Float(getBoundBoxPtr()->MinY);
585 }
586
587 void BoundBoxPy::setYMin(Py::Float arg)
588 {
589 getBoundBoxPtr()->MinY = arg;
590 }
591
592 Py::Float BoundBoxPy::getZMin() const
593 {
594 return Py::Float(getBoundBoxPtr()->MinZ);
595 }
596
597 void BoundBoxPy::setZMin(Py::Float arg)
598 {
599 getBoundBoxPtr()->MinZ = arg;
600 }
601
602 Py::Float BoundBoxPy::getXLength() const
603 {
604 return Py::Float(getBoundBoxPtr()->LengthX());
605 }
606
607 Py::Float BoundBoxPy::getYLength() const
608 {
609 return Py::Float(getBoundBoxPtr()->LengthY());
610 }
611
612 Py::Float BoundBoxPy::getZLength() const
613 {
614 return Py::Float(getBoundBoxPtr()->LengthZ());
615 }
616
617 Py::Float BoundBoxPy::getDiagonalLength() const
618 {
619 if (!getBoundBoxPtr()->IsValid()) {
620 throw Py::FloatingPointError("Cannot determine diagonal length of invalid bounding box");
621 }
622 return Py::Float(getBoundBoxPtr()->CalcDiagonalLength());
623 }
624
625 PyObject* BoundBoxPy::getCustomAttributes(const char* /*attr*/) const
626 {
627 return nullptr;
628 }
629
630 int BoundBoxPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
631 {
632 return 0;
633 }