Debian Codegraph @

src/python/PyImath/PyImathVec3Impl.h

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #ifndef _PyImathVec3Impl_h_
9 #define _PyImathVec3Impl_h_
10
11 //
12 // This .C file was turned into a header file so that instantiations
13 // of the various V3* types can be spread across multiple files in
14 // order to work around MSVC limitations.
15 //
16
17 #include <Python.h>
18 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
19 #include <boost/python.hpp>
20 #include <boost/python/make_constructor.hpp>
21 #include <boost/format.hpp>
22 #include <ImathVec.h>
23 #include <ImathVecAlgo.h>
24 #include "PyImath.h"
25 #include "PyImathMathExc.h"
26 #include "PyImathVec.h"
27 #include "PyImathDecorators.h"
28
29 namespace PyImath {
30 using namespace boost::python;
31 using namespace IMATH_NAMESPACE;
32
33 template <class T> struct Vec3Name { static const char *value(); };
34
35 // create a new default constructor that initializes Vec3<T> to zero.
36 template <class T>
37 static Vec3<T> * Vec3_construct_default()
38 {
39 return new Vec3<T>(T(0),T(0),T(0));
40 }
41
42 template <class T>
43 static Vec3<T> * Vec3_object_constructor1(const object &obj)
44 {
45 Vec3<T> w;
46 extract<Vec3<int> > e1(obj);
47 extract<Vec3<float> > e2(obj);
48 extract<Vec3<double> > e3(obj);
49 extract<tuple> e4(obj);
50 extract<double> e5(obj);
51 extract<list> e6(obj);
52
53 if(e1.check()) { w = e1(); }
54 else if(e2.check()) { w = e2(); }
55 else if(e3.check()) { w = e3(); }
56 else if(e4.check())
57 {
58 tuple t = e4();
59 if(t.attr("__len__")() == 3)
60 {
61 w.x = extract<T>(t[0]);
62 w.y = extract<T>(t[1]);
63 w.z = extract<T>(t[2]);
64 }
65 else
66 throw std::invalid_argument ("tuple must have length of 3");
67
68 }
69 else if(e5.check()) { T a = e5(); w.setValue(a, a, a); }
70 else if(e6.check())
71 {
72 list l = e6();
73 if(l.attr("__len__")() == 3)
74 {
75 w.x = extract<T>(l[0]);
76 w.y = extract<T>(l[1]);
77 w.z = extract<T>(l[2]);
78 }
79 else
80 throw std::invalid_argument ("list must have length of 3");
81 }
82 else
83 throw std::invalid_argument ("invalid parameters passed to Vec3 constructor");
84
85 Vec3<T> *v = new Vec3<T>;
86 *v = w;
87
88 return v;
89
90 }
91
92 template <class T>
93 static Vec3<T> * Vec3_object_constructor2(const object &obj1, const object &obj2, const object &obj3)
94 {
95 extract<double> e1(obj1);
96 extract<double> e2(obj2);
97 extract<double> e3(obj3);
98 Vec3<T> *v = new Vec3<T>;
99
100 if(e1.check()) { v->x = e1();}
101 else { throw std::invalid_argument ("invalid parameters passed to Vec3 constructor"); }
102
103 if(e2.check()) { v->y = e2();}
104 else { throw std::invalid_argument ("invalid parameters passed to Vec3 constructor"); }
105
106 if(e3.check()) { v->z = e3();}
107 else { throw std::invalid_argument ("invalid parameters passed to Vec3 constructor"); }
108
109 return v;
110 }
111
112
113
114 // Implementations of str and repr are same here,
115 // but we'll specialize repr for float and double to make them exact.
116 template <class T>
117 static std::string Vec3_str(const Vec3<T> &v)
118 {
119 std::stringstream stream;
120 stream << Vec3Name<T>::value() << "(" << v.x << ", " << v.y << ", " << v.z << ")";
121 return stream.str();
122 }
123 template <class T>
124 static std::string Vec3_repr(const Vec3<T> &v)
125 {
126 std::stringstream stream;
127 stream << Vec3Name<T>::value() << "(" << v.x << ", " << v.y << ", " << v.z << ")";
128 return stream.str();
129 }
130
131 template <class T>
132 static IMATH_NAMESPACE::Vec3<T>
133 Vec3_cross(const IMATH_NAMESPACE::Vec3<T> &v, const IMATH_NAMESPACE::Vec3<T> &other)
134 {
135 MATH_EXC_ON;
136 return v.cross(other);
137 }
138
139 template <class T>
140 static FixedArray<IMATH_NAMESPACE::Vec3<T> >
141 Vec3_cross_Vec3Array(const IMATH_NAMESPACE::Vec3<T> &va, const FixedArray<IMATH_NAMESPACE::Vec3<T> > &vb)
142 {
143 MATH_EXC_ON;
144 size_t len = vb.len();
145 FixedArray<IMATH_NAMESPACE::Vec3<T> > f(len);
146 for (size_t i = 0; i < len; ++i)
147 f[i] = va.cross(vb[i]);
148 return f;
149 }
150
151 template <class T>
152 static T
153 Vec3_dot(const IMATH_NAMESPACE::Vec3<T> &v, const IMATH_NAMESPACE::Vec3<T> &other)
154 {
155 MATH_EXC_ON;
156 return v.dot(other);
157 }
158
159 template <class T>
160 static FixedArray<T>
161 Vec3_dot_Vec3Array(const IMATH_NAMESPACE::Vec3<T> &va, const FixedArray<IMATH_NAMESPACE::Vec3<T> > &vb)
162 {
163 MATH_EXC_ON;
164 size_t len = vb.len();
165 FixedArray<T> f(len);
166 for (size_t i = 0; i < len; ++i)
167 f[i] = va.dot(vb[i]);
168 return f;
169 }
170
171 template <class T>
172 static T
173 Vec3_length(const IMATH_NAMESPACE::Vec3<T> &v)
174 {
175 MATH_EXC_ON;
176 return v.length();
177 }
178
179 template <class T>
180 static T
181 Vec3_length2(const IMATH_NAMESPACE::Vec3<T> &v)
182 {
183 MATH_EXC_ON;
184 return v.length2();
185 }
186
187 template <class T>
188 static const Vec3<T> &
189 Vec3_normalize(IMATH_NAMESPACE::Vec3<T> &v)
190 {
191 MATH_EXC_ON;
192 return v.normalize();
193 }
194
195 template <class T>
196 static const Vec3<T> &
197 Vec3_normalizeExc(IMATH_NAMESPACE::Vec3<T> &v)
198 {
199 MATH_EXC_ON;
200 return v.normalizeExc();
201 }
202
203 template <class T>
204 static const Vec3<T> &
205 Vec3_normalizeNonNull(IMATH_NAMESPACE::Vec3<T> &v)
206 {
207 MATH_EXC_ON;
208 return v.normalizeNonNull();
209 }
210
211 template <class T>
212 static Vec3<T>
213 Vec3_normalized(const IMATH_NAMESPACE::Vec3<T> &v)
214 {
215 MATH_EXC_ON;
216 return v.normalized();
217 }
218
219 template <class T>
220 static Vec3<T>
221 Vec3_normalizedExc(const IMATH_NAMESPACE::Vec3<T> &v)
222 {
223 MATH_EXC_ON;
224 return v.normalizedExc();
225 }
226
227 template <class T>
228 static Vec3<T>
229 Vec3_normalizedNonNull(const IMATH_NAMESPACE::Vec3<T> &v)
230 {
231 MATH_EXC_ON;
232 return v.normalizedNonNull();
233 }
234
235 template <class T>
236 static Vec3<T>
237 closestVertex(Vec3<T> &p, const Vec3<T> &v0, const Vec3<T> &v1, const Vec3<T> &v2)
238 {
239 MATH_EXC_ON;
240 return IMATH_NAMESPACE::closestVertex(v0, v1, v2, p);
241 }
242
243 template <class T>
244 static const Vec3<T> &
245 Vec3_negate(IMATH_NAMESPACE::Vec3<T> &v)
246 {
247 MATH_EXC_ON;
248 return v.negate();
249 }
250
251 template <class T>
252 static Vec3<T>
253 orthogonal(const Vec3<T> &v, const Vec3<T> &v0)
254 {
255 MATH_EXC_ON;
256 return IMATH_NAMESPACE::orthogonal(v, v0);
257 }
258
259 template <class T>
260 static Vec3<T>
261 project(const Vec3<T> &v, const Vec3<T> &v0)
262 {
263 MATH_EXC_ON;
264 return IMATH_NAMESPACE::project(v0, v);
265 }
266
267 template <class T>
268 static Vec3<T>
269 reflect(const Vec3<T> &v, const Vec3<T> &v0)
270 {
271 MATH_EXC_ON;
272 return IMATH_NAMESPACE::reflect(v, v0);
273 }
274
275 template <class T>
276 static void
277 setValue(Vec3<T> &v, T a, T b, T c)
278 {
279 v.x = a;
280 v.y = b;
281 v.z = c;
282 }
283
284 template <class T>
285 static Vec3<T>
286 Vec3_add (const Vec3<T> &v, const Vec3<T> &w)
287 {
288 MATH_EXC_ON;
289 return v + w;
290 }
291
292 template <class T>
293 static Vec3<T>
294 Vec3_sub (const Vec3<T> &v, const Vec3<T> &w)
295 {
296 MATH_EXC_ON;
297 return v - w;
298 }
299
300 template <class T>
301 static Vec3<T>
302 Vec3_neg (const Vec3<T> &v)
303 {
304 MATH_EXC_ON;
305 return -v;
306 }
307
308 template <class T, class U>
309 static Vec3<T>
310 Vec3_mul (const Vec3<T> &v, Vec3<U> &w)
311 {
312 MATH_EXC_ON;
313 Vec3<T> w2 (w);
314 return v * w2;
315 }
316
317 template <class T>
318 static Vec3<T>
319 Vec3_mulT (const Vec3<T> &v, T t)
320 {
321 MATH_EXC_ON;
322 return v * t;
323 }
324
325 template <class T>
326 static FixedArray<IMATH_NAMESPACE::Vec3<T> >
327 Vec3_mulTArray (const Vec3<T> &v, const FixedArray<T> &t)
328 {
329 MATH_EXC_ON;
330 size_t len = t.len();
331 FixedArray<IMATH_NAMESPACE::Vec3<T> > retval(len);
332 for (size_t i=0; i<len; ++i) retval[i] = v*t[i];
333 return retval;
334 }
335
336 template <class T>
337 static FixedArray<IMATH_NAMESPACE::Vec3<T> >
338 Vec3_rmulTArray (const Vec3<T> &v, const FixedArray<T> &t)
339 {
340 return Vec3_mulTArray(v,t);
341 }
342
343 template <class T,class S>
344 static Vec3<T>
345 Vec3_div (Vec3<T> &v, Vec3<S> &w)
346 {
347 MATH_EXC_ON;
348 return v / w;
349 }
350
351 template <class T>
352 static Vec3<T>
353 Vec3_rmulT (Vec3<T> &v, T t)
354 {
355 MATH_EXC_ON;
356 return t * v;
357 }
358
359 template <class T, class U>
360 static const Vec3<T> &
361 Vec3_imulV(Vec3<T> &v, const Vec3<U> &w)
362 {
363 MATH_EXC_ON;
364 return v *= w;
365 }
366
367 template <class T>
368 static const Vec3<T> &
369 Vec3_imulT(IMATH_NAMESPACE::Vec3<T> &v, T t)
370 {
371 MATH_EXC_ON;
372 return v *= t;
373 }
374
375 template <class T, class U>
376 static Vec3<T>
377 Vec3_mulM33 (Vec3<T> &v, const Matrix33<U> &m)
378 {
379 MATH_EXC_ON;
380 return v * m;
381 }
382
383 template <class T, class U>
384 static Vec3<T>
385 Vec3_mulM44 (Vec3<T> &v, const Matrix44<U> &m)
386 {
387 MATH_EXC_ON;
388 return v * m;
389 }
390
391 template <class T>
392 static const Vec3<T> &
393 Vec3_idivObj(IMATH_NAMESPACE::Vec3<T> &v, const object &o)
394 {
395 MATH_EXC_ON;
396 Vec3<T> v2;
397 if (PyImath::V3<T>::convert (o.ptr(), &v2))
398 {
399 return v /= v2;
400 }
401 else
402 {
403 extract<double> e(o);
404 if (e.check())
405 return v /= e();
406 else
407 throw std::invalid_argument ("V3 division expects an argument"
408 "convertible to a V3");
409 }
410 }
411
412 template <class T>
413 static Vec3<T>
414 Vec3_subT(const Vec3<T> &v, T a)
415 {
416 MATH_EXC_ON;
417 Vec3<T> w;
418 w.setValue(v.x - a, v.y - a, v.z - a);
419 return w;
420 }
421
422 template <class T,class BoostPyType>
423 static Vec3<T>
424 Vec3_subTuple(const Vec3<T> &v, const BoostPyType &t)
425 {
426 MATH_EXC_ON;
427 Vec3<T> w;
428
429 if(t.attr("__len__")() == 3)
430 {
431 w.x = v.x - extract<T>(t[0]);
432 w.y = v.y - extract<T>(t[1]);
433 w.z = v.z - extract<T>(t[2]);
434 }
435 else
436 throw std::invalid_argument ("tuple must have length of 3");
437
438 return w;
439 }
440
441 template <class T>
442 static Vec3<T>
443 Vec3_rsubT(const Vec3<T> &v, T a)
444 {
445 MATH_EXC_ON;
446 Vec3<T> w;
447 w.setValue(a - v.x, a - v.y, a - v.z);
448 return w;
449 }
450
451 template <class T, class BoostPyType>
452 static Vec3<T>
453 Vec3_rsubTuple(const Vec3<T> &v, const BoostPyType &t)
454 {
455 MATH_EXC_ON;
456 Vec3<T> w;
457
458 if(t.attr("__len__")() == 3)
459 {
460 w.x = extract<T>(t[0]) - v.x;
461 w.y = extract<T>(t[1]) - v.y;
462 w.z = extract<T>(t[2]) - v.z;
463 }
464 else
465 throw std::invalid_argument ("tuple must have length of 3");
466
467 return w;
468 }
469
470 template <class T, class BoostPyType>
471 static Vec3<T>
472 Vec3_addTuple(const Vec3<T> &v, const BoostPyType &t)
473 {
474 MATH_EXC_ON;
475 Vec3<T> w;
476
477 if(t.attr("__len__")() == 3)
478 {
479 w.x = v.x + extract<T>(t[0]);
480 w.y = v.y + extract<T>(t[1]);
481 w.z = v.z + extract<T>(t[2]);
482 }
483 else
484 throw std::invalid_argument ("tuple must have length of 3");
485
486 return w;
487 }
488
489 template <class T>
490 static Vec3<T>
491 Vec3_addT(const Vec3<T> &v, T a)
492 {
493 MATH_EXC_ON;
494 Vec3<T> w;
495 w.setValue(v.x + a, v.y + a, v.z + a);
496 return w;
497 }
498
499 template <class T, class U>
500 static Vec3<T>
501 Vec3_addV(const Vec3<T> &v, const Vec3<U> &w)
502 {
503 MATH_EXC_ON;
504 return v + w;
505 }
506
507 template <class T, class U>
508 static const Vec3<T> &
509 Vec3_iaddV(Vec3<T> &v, const Vec3<U> &w)
510 {
511 MATH_EXC_ON;
512 return v += w;
513 }
514
515 template <class T, class U>
516 static Vec3<T>
517 Vec3_subV(const Vec3<T> &v, const Vec3<U> &w)
518 {
519 MATH_EXC_ON;
520 return v - w;
521 }
522
523 template <class T, class U>
524 static const Vec3<T> &
525 Vec3_isubV(Vec3<T> &v, const Vec3<U> &w)
526 {
527 MATH_EXC_ON;
528 return v -= w;
529 }
530
531 template <class T>
532 static Vec3<T>
533 mult(const Vec3<T> &v, tuple t)
534 {
535 MATH_EXC_ON;
536 Vec3<T> w;
537
538 if(t.attr("__len__")() == 1){
539 w.x = v.x*extract<T>(t[0]);
540 w.y = v.y*extract<T>(t[0]);
541 w.z = v.z*extract<T>(t[0]);
542 }
543 else if(t.attr("__len__")() == 3){
544 w.x = v.x*extract<T>(t[0]);
545 w.y = v.y*extract<T>(t[1]);
546 w.z = v.z*extract<T>(t[2]);
547 }
548 else
549 throw std::invalid_argument ("tuple must have length of 1 or 3");
550
551 return w;
552 }
553
554 template <class T, class U>
555 static const Vec3<T> &
556 Vec3_imulM44 (Vec3<T> &v, const Matrix44<U> &m)
557 {
558 MATH_EXC_ON;
559 return v *= m;
560 }
561
562 template <class T, class BoostPyType>
563 static Vec3<T>
564 Vec3_divTuple(const Vec3<T> &v, const BoostPyType &t)
565 {
566 if(t.attr("__len__")() == 3)
567 {
568 T x = extract<T>(t[0]);
569 T y = extract<T>(t[1]);
570 T z = extract<T>(t[2]);
571 if(x != T(0) && y != T(0) && z != T(0))
572 return Vec3<T>(v.x / x, v.y / y, v.z / z);
573 else
574 throw std::domain_error ("Division by zero");
575 }
576 else
577 throw std::invalid_argument ("Vec3 expects tuple of length 3");
578 }
579
580 template <class T, class BoostPyType>
581 static Vec3<T>
582 Vec3_rdivTuple(const Vec3<T> &v, const BoostPyType &t)
583 {
584 MATH_EXC_ON;
585 Vec3<T> w;
586 if(t.attr("__len__")() == 3)
587 {
588 T x = extract<T>(t[0]);
589 T y = extract<T>(t[1]);
590 T z = extract<T>(t[2]);
591
592 if(v.x != T(0) && v.y != T(0) && v.z != T(0)){
593 w.setValue(x / v.x, y / v.y, z / v.z);
594 }
595 else
596 throw std::domain_error ("Division by zero");
597 }
598 else
599 throw std::invalid_argument ("tuple must have length of 3");
600
601 return w;
602 }
603
604 template <class T>
605 static Vec3<T>
606 Vec3_divT(const Vec3<T> &v, T a)
607 {
608 MATH_EXC_ON;
609 Vec3<T> w;
610 if(a != T(0)){
611 w.setValue(v.x / a, v.y / a, v.z / a);
612 }
613 else
614 throw std::domain_error ("Division by zero");
615
616 return w;
617 }
618
619 template <class T>
620 static Vec3<T>
621 Vec3_rdivT(const Vec3<T> &v, T a)
622 {
623 MATH_EXC_ON;
624 Vec3<T> w;
625 if(v.x != T(0) && v.y != T(0) && v.z != T(0)){
626 w.setValue(a / v.x, a / v.y, a / v.z);
627 }
628 else
629 throw std::domain_error ("Division by zero");
630
631 return w;
632 }
633
634 template <class T>
635 static Vec3<T>
636 Vec3_Vec3_mulT(const Vec3<T>& v, const Vec3<T>& w)
637 {
638 MATH_EXC_ON;
639 return v*w;
640 }
641
642 template <class T>
643 static Vec3<T>
644 Vec3_Vec3_divT(const Vec3<T>& v, const Vec3<T>& w)
645 {
646 MATH_EXC_ON;
647 return v/w;
648 }
649
650 template <class T>
651 static bool
652 lessThan(const Vec3<T> &v, const object &obj)
653 {
654 extract<Vec3<T> > e1(obj);
655 extract<tuple> e2(obj);
656
657 Vec3<T> w;
658 if(e1.check())
659 {
660 w = e1();
661 }
662 else if(e2.check())
663 {
664 tuple t = e2();
665 T x = extract<T>(t[0]);
666 T y = extract<T>(t[1]);
667 T z = extract<T>(t[2]);
668 w.setValue(x,y,z);
669 }
670 else
671 throw std::invalid_argument ("invalid parameters passed to operator <");
672
673 bool isLessThan = (v.x <= w.x && v.y <= w.y && v.z <= w.z)
674 && v != w;
675
676 return isLessThan;
677 }
678
679 template <class T>
680 static bool
681 greaterThan(const Vec3<T> &v, const object &obj)
682 {
683 extract<Vec3<T> > e1(obj);
684 extract<tuple> e2(obj);
685
686 Vec3<T> w;
687 if(e1.check())
688 {
689 w = e1();
690 }
691 else if(e2.check())
692 {
693 tuple t = e2();
694 T x = extract<T>(t[0]);
695 T y = extract<T>(t[1]);
696 T z = extract<T>(t[2]);
697 w.setValue(x,y,z);
698 }
699 else
700 throw std::invalid_argument ("invalid parameters passed to operator >");
701
702 bool isGreaterThan = (v.x >= w.x && v.y >= w.y && v.z >= w.z)
703 && v != w;
704
705 return isGreaterThan;
706 }
707
708 template <class T>
709 static bool
710 lessThanEqual(const Vec3<T> &v, const object &obj)
711 {
712 extract<Vec3<T> > e1(obj);
713 extract<tuple> e2(obj);
714
715 Vec3<T> w;
716 if(e1.check())
717 {
718 w = e1();
719 }
720 else if(e2.check())
721 {
722 tuple t = e2();
723 T x = extract<T>(t[0]);
724 T y = extract<T>(t[1]);
725 T z = extract<T>(t[2]);
726 w.setValue(x,y,z);
727 }
728 else
729 throw std::invalid_argument ("invalid parameters passed to operator <=");
730
731 bool isLessThanEqual = (v.x <= w.x && v.y <= w.y && v.z <= w.z);
732
733 return isLessThanEqual;
734 }
735
736 template <class T>
737 static bool
738 greaterThanEqual(const Vec3<T> &v, const object &obj)
739 {
740 extract<Vec3<T> > e1(obj);
741 extract<tuple> e2(obj);
742
743 Vec3<T> w;
744 if(e1.check())
745 {
746 w = e1();
747 }
748 else if(e2.check())
749 {
750 tuple t = e2();
751 T x = extract<T>(t[0]);
752 T y = extract<T>(t[1]);
753 T z = extract<T>(t[2]);
754 w.setValue(x,y,z);
755 }
756 else
757 throw std::invalid_argument ("invalid parameters passed to operator >=");
758
759 bool isGreaterThanEqual = (v.x >= w.x && v.y >= w.y && v.z >= w.z);
760
761 return isGreaterThanEqual;
762 }
763
764
765 template <class T>
766 static bool
767 equalWithAbsErrorObj(const Vec3<T> &v, const object &obj1, const object &obj2)
768 {
769 extract<Vec3<int> > e1(obj1);
770 extract<Vec3<float> > e2(obj1);
771 extract<Vec3<double> > e3(obj1);
772
773 extract<tuple> e4(obj1);
774 extract<double> e5(obj2);
775
776 Vec3<T> w;
777 if(e1.check()) { w = e1(); }
778 else if(e2.check()) { w = e2(); }
779 else if(e3.check()) { w = e3(); }
780 else if(e4.check())
781 {
782 tuple t = e4();
783 if(t.attr("__len__")() == 3)
784 {
785 w.x = extract<T>(t[0]);
786 w.y = extract<T>(t[1]);
787 w.z = extract<T>(t[2]);
788 }
789 else
790 throw std::invalid_argument ("tuple of length 3 expected");
791 }
792 else
793 throw std::invalid_argument ("invalid parameters passed to equalWithAbsError");
794
795 if(e5.check()) { return v.equalWithAbsError(w, e5()); }
796 else
797 throw std::invalid_argument ("invalid parameters passed to equalWithAbsError");
798 }
799
800 template <class T>
801 static bool
802 equalWithRelErrorObj(const Vec3<T> &v, const object &obj1, const object &obj2)
803 {
804 extract<Vec3<int> > e1(obj1);
805 extract<Vec3<float> > e2(obj1);
806 extract<Vec3<double> > e3(obj1);
807
808 extract<tuple> e4(obj1);
809 extract<double> e5(obj2);
810
811 Vec3<T> w;
812 if(e1.check()) { w = e1(); }
813 else if(e2.check()) { w = e2(); }
814 else if(e3.check()) { w = e3(); }
815 else if(e4.check())
816 {
817 tuple t = e4();
818 if(t.attr("__len__")() == 3)
819 {
820 w.x = extract<T>(t[0]);
821 w.y = extract<T>(t[1]);
822 w.z = extract<T>(t[2]);
823 }
824 else
825 throw std::invalid_argument ("tuple of length 3 expected");
826 }
827 else
828 throw std::invalid_argument ("invalid parameters passed to equalWithRelError");
829
830 if(e5.check()) { return v.equalWithRelError(w, e5()); }
831 else
832 throw std::invalid_argument ("invalid parameters passed to equalWithRelError");
833
834 }
835
836
837 template <class T>
838 static bool
839 equal(const Vec3<T> &v, const tuple &t)
840 {
841 Vec3<T> w;
842 if(t.attr("__len__")() == 3)
843 {
844 w.x = extract<T>(t[0]);
845 w.y = extract<T>(t[1]);
846 w.z = extract<T>(t[2]);
847
848 return (v == w);
849 }
850 else
851 throw std::invalid_argument ("tuple of length 3 expected");
852 }
853
854 template <class T>
855 static bool
856 notequal(const Vec3<T> &v, const tuple &t)
857 {
858 Vec3<T> w;
859 if(t.attr("__len__")() == 3)
860 {
861 w.x = extract<T>(t[0]);
862 w.y = extract<T>(t[1]);
863 w.z = extract<T>(t[2]);
864
865 return (v != w);
866 }
867 else
868 throw std::invalid_argument ("tuple of length 3 expected");
869 }
870
871
872
873 // Trick to register methods for float-only-based vectors
874 template <class T, IMATH_ENABLE_IF(!std::is_integral<T>::value)>
875 void register_Vec3_floatonly(class_<Vec3<T>>& vec3_class)
876 {
877 vec3_class
878 .def("length", &Vec3_length<T>,"length() magnitude of the vector")
879 .def("normalize", &Vec3_normalize<T>,return_internal_reference<>(),
880 "v.normalize() destructively normalizes v and returns a reference to it")
881 .def("normalizeExc", &Vec3_normalizeExc<T>,return_internal_reference<>(),
882 "v.normalizeExc() destructively normalizes V and returns a reference to it, throwing an exception if length() == 0")
883 .def("normalizeNonNull", &Vec3_normalizeNonNull<T>,return_internal_reference<>(),
884 "v.normalizeNonNull() destructively normalizes V and returns a reference to it, faster if lngth() != 0")
885 .def("normalized", &Vec3_normalized<T>, "v.normalized() returns a normalized copy of v")
886 .def("normalizedExc", &Vec3_normalizedExc<T>, "v.normalizedExc() returns a normalized copy of v, throwing an exception if length() == 0")
887 .def("normalizedNonNull", &Vec3_normalizedNonNull<T>, "v.normalizedNonNull() returns a normalized copy of v, faster if lngth() != 0")
888 .def("orthogonal", &orthogonal<T>)
889 .def("project", &project<T>)
890 .def("reflect", &reflect<T>)
891 ;
892 }
893
894 template <class T, IMATH_ENABLE_IF(std::is_integral<T>::value)>
895 void register_Vec3_floatonly(class_<Vec3<T>>& vec2_class)
896 {
897 }
898
899
900
901 template <class T>
902 class_<Vec3<T> >
903 register_Vec3()
904 {
905 typedef PyImath::StaticFixedArray<Vec3<T>,T,3> Vec3_helper;
906
907 class_<Vec3<T> > vec3_class(Vec3Name<T>::value(), Vec3Name<T>::value(),init<Vec3<T> >("copy construction"));
908 vec3_class
909 .def("__init__",make_constructor(Vec3_construct_default<T>),"initialize to (0,0,0)")
910 .def("__init__",make_constructor(Vec3_object_constructor1<T>))
911 .def("__init__",make_constructor(Vec3_object_constructor2<T>))
912 .def_readwrite("x", &Vec3<T>::x)
913 .def_readwrite("y", &Vec3<T>::y)
914 .def_readwrite("z", &Vec3<T>::z)
915 .def("baseTypeEpsilon", &Vec3<T>::baseTypeEpsilon,"baseTypeEpsilon() epsilon value of the base type of the vector")
916 .staticmethod("baseTypeEpsilon")
917 .def("baseTypeMax", &Vec3<T>::baseTypeMax,"baseTypeMax() max value of the base type of the vector")
918 .staticmethod("baseTypeMax")
919 .def("baseTypeLowest", &Vec3<T>::baseTypeLowest,"baseTypeLowest() largest negative value of the base type of the vector")
920 .staticmethod("baseTypeLowest")
921 .def("baseTypeSmallest", &Vec3<T>::baseTypeSmallest,"baseTypeSmallest() smallest value of the base type of the vector")
922 .staticmethod("baseTypeSmallest")
923 .def("cross", &Vec3_cross<T>,"v1.cross(v2) right handed cross product")
924 .def("cross", &Vec3_cross_Vec3Array<T>,"v1.cross(v2) right handed array cross product")
925 .def("dimensions", &Vec3<T>::dimensions,"dimensions() number of dimensions in the vector")
926 .staticmethod("dimensions")
927 .def("dot", &Vec3_dot<T>,"v1.dot(v2) inner product of the two vectors")
928 .def("dot", &Vec3_dot_Vec3Array<T>,"v1.dot(v2) array inner product")
929
930 .def("equalWithAbsError", &Vec3<T>::equalWithAbsError,
931 "v1.equalWithAbsError(v2) true if the elements "
932 "of v1 and v2 are the same with an absolute error of no more than e, "
933 "i.e., abs(v1[i] - v2[i]) <= e")
934 .def("equalWithAbsError", &equalWithAbsErrorObj<T>)
935
936 .def("equalWithRelError", &Vec3<T>::equalWithRelError,
937 "v1.equalWithAbsError(v2) true if the elements "
938 "of v1 and v2 are the same with an absolute error of no more than e, "
939 "i.e., abs(v1[i] - v2[i]) <= e * abs(v1[i])")
940 .def("equalWithRelError", &equalWithRelErrorObj<T>)
941
942 .def("length2", &Vec3_length2<T>,"length2() square magnitude of the vector")
943 .def("__len__", Vec3_helper::len)
944 .def("__getitem__", Vec3_helper::getitem,return_value_policy<copy_non_const_reference>())
945 .def("__setitem__", Vec3_helper::setitem)
946 .def("closestVertex", &closestVertex<T>)
947 .def("negate", &Vec3_negate<T>, return_internal_reference<>())
948 .def("setValue", &setValue<T>)
949 .def("__neg__", &Vec3_neg<T>)
950 .def("__mul__", &Vec3_mul<T, int>)
951 .def("__mul__", &Vec3_mul<T, float>)
952 .def("__mul__", &Vec3_mul<T, double>)
953 .def("__mul__", &Vec3_mulT<T>)
954 .def("__mul__", &Vec3_mulTArray<T>)
955 .def("__rmul__", &Vec3_rmulT<T>)
956 .def("__rmul__", &Vec3_rmulTArray<T>)
957 .def("__imul__", &Vec3_imulV<T, int>,return_internal_reference<>())
958 .def("__imul__", &Vec3_imulV<T, float>,return_internal_reference<>())
959 .def("__imul__", &Vec3_imulV<T, double>,return_internal_reference<>())
960 .def("__imul__", &Vec3_imulT<T>,return_internal_reference<>())
961 .def("__div__", &Vec3_Vec3_divT<T>)
962 .def("__truediv__", &Vec3_Vec3_divT<T>)
963 .def("__mul__", &Vec3_mulM33<T, float>)
964 .def("__mul__", &Vec3_mulM33<T, double>)
965 .def("__mul__", &Vec3_mulM44<T, float>)
966 .def("__mul__", &Vec3_mulM44<T, double>)
967 .def("__mul__", &Vec3_Vec3_mulT<T>)
968 .def("__div__", &Vec3_div<T,int>)
969 .def("__div__", &Vec3_div<T,float>)
970 .def("__div__", &Vec3_div<T,double>)
971 .def("__div__", &Vec3_divTuple<T,tuple>)
972 .def("__div__", &Vec3_divTuple<T,list>)
973 .def("__div__", &Vec3_divT<T>)
974 .def("__truediv__", &Vec3_div<T,int>)
975 .def("__truediv__", &Vec3_div<T,float>)
976 .def("__truediv__", &Vec3_div<T,double>)
977 .def("__truediv__", &Vec3_divTuple<T,tuple>)
978 .def("__truediv__", &Vec3_divTuple<T,list>)
979 .def("__truediv__", &Vec3_divT<T>)
980 .def("__rdiv__", &Vec3_rdivTuple<T,tuple>)
981 .def("__rdiv__", &Vec3_rdivTuple<T,list>)
982 .def("__rdiv__", &Vec3_rdivT<T>)
983 .def("__rtruediv__", &Vec3_rdivTuple<T,tuple>)
984 .def("__rtruediv__", &Vec3_rdivTuple<T,list>)
985 .def("__rtruediv__", &Vec3_rdivT<T>)
986 .def("__idiv__", &Vec3_idivObj<T>,return_internal_reference<>())
987 .def("__itruediv__", &Vec3_idivObj<T>,return_internal_reference<>())
988 .def("__xor__", &Vec3_dot<T>)
989 .def("__mod__", &Vec3_cross<T>)
990 .def(self == self) // NOSONAR - suppress SonarCloud bug report.
991 .def(self != self) // NOSONAR - suppress SonarCloud bug report.
992 .def("__add__", &Vec3_add<T>)
993 .def("__add__", &Vec3_addV<T, int>)
994 .def("__add__", &Vec3_addV<T, float>)
995 .def("__add__", &Vec3_addV<T, double>)
996 .def("__add__", &Vec3_addT<T>)
997 .def("__add__", &Vec3_addTuple<T,tuple>)
998 .def("__add__", &Vec3_addTuple<T,list>)
999 .def("__radd__", &Vec3_addT<T>)
1000 .def("__radd__", &Vec3_addTuple<T,tuple>)
1001 .def("__radd__", &Vec3_addTuple<T,list>)
1002 .def("__radd__", &Vec3_add<T>)
1003 .def("__iadd__", &Vec3_iaddV<T, int>, return_internal_reference<>())
1004 .def("__iadd__", &Vec3_iaddV<T, float>, return_internal_reference<>())
1005 .def("__iadd__", &Vec3_iaddV<T, double>, return_internal_reference<>())
1006 .def("__sub__", &Vec3_sub<T>)
1007 .def("__sub__", &Vec3_subV<T, int>)
1008 .def("__sub__", &Vec3_subV<T, float>)
1009 .def("__sub__", &Vec3_subV<T, double>)
1010 .def("__sub__", &Vec3_subT<T>)
1011 .def("__sub__", &Vec3_subTuple<T,tuple>)
1012 .def("__sub__", &Vec3_subTuple<T,list>)
1013 .def("__rsub__", &Vec3_rsubT<T>)
1014 .def("__rsub__", &Vec3_rsubTuple<T,tuple>)
1015 .def("__rsub__", &Vec3_rsubTuple<T,list>)
1016 .def("__isub__", &Vec3_isubV<T, int>, return_internal_reference<>())
1017 .def("__isub__", &Vec3_isubV<T, float>, return_internal_reference<>())
1018 .def("__isub__", &Vec3_isubV<T, double>, return_internal_reference<>())
1019 .def("__mul__", &mult<T>)
1020 .def("__rmul__", &mult<T>)
1021 .def("__imul__", &Vec3_imulM44<T, float>, return_internal_reference<>())
1022 .def("__imul__", &Vec3_imulM44<T, double>, return_internal_reference<>())
1023 .def("__lt__", &lessThan<T>)
1024 .def("__gt__", &greaterThan<T>)
1025 .def("__le__", &lessThanEqual<T>)
1026 .def("__ge__", &greaterThanEqual<T>)
1027 .def("__eq__", &equal<T>)
1028 .def("__ne__", &notequal<T>)
1029 //.def(self_ns::str(self))
1030 .def("__str__",&Vec3_str<T>)
1031 .def("__repr__",&Vec3_repr<T>)
1032 ;
1033
1034 register_Vec3_floatonly<T>(vec3_class);
1035
1036 decoratecopy(vec3_class);
1037
1038 //add_swizzle3_operators(v3f_class);
1039 return vec3_class;
1040 }
1041
1042
1043
1044 } // namespace PyImath
1045
1046 #endif // _PyImathVec3Impl_h_