Debian Codegraph @

src/python/PyImath/PyImathShear.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #include <Python.h>
9 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
10 #include <boost/python.hpp>
11 #include <boost/python/make_constructor.hpp>
12 #include <boost/format.hpp>
13 #include "PyImath.h"
14 #include "PyImathMathExc.h"
15 #include "PyImathShear.h"
16 #include "PyImathPlane.h"
17 #include "PyImathDecorators.h"
18 #include "PyImathExport.h"
19
20
21 namespace PyImath{
22 using namespace boost::python;
23 using namespace IMATH_NAMESPACE;
24
25 template <class T> struct ShearName {static const char *value;};
26 template <> const char *ShearName<float>::value = "Shear6f";
27 template <> const char *ShearName<double>::value = "Shear6d";
28
29 template <class T>
30 static std::string Shear_str(const Shear6<T> &v)
31 {
32 std::stringstream stream;
33 stream << ShearName<T>::value << "("
34 << v[0] << ", " << v[1] << ", " << v[2] << ", "
35 << v[3] << ", " << v[4] << ", " << v[5] << ")";
36 return stream.str();
37 }
38
39 // Non-specialized repr is same as str
40 template <class T>
41 static std::string Shear_repr(const Shear6<T> &v)
42 {
43 return Shear_str(v);
44 }
45
46 // Specialization for float to full precision
47 template <>
48 std::string Shear_repr(const Shear6<float> &v)
49 {
50 return (boost::format("%s(%.9g, %.9g, %.9g, %.9g, %.9g, %.9g)")
51 % ShearName<float>::value
52 % v[0] % v[1] % v[2]
53 % v[3] % v[4] % v[5]).str();
54 }
55
56 // Specialization for double to full precision
57 template <>
58 std::string Shear_repr(const Shear6<double> &v)
59 {
60 return (boost::format("%s(%.17g, %.17g, %.17g, %.17g, %.17g, %.17g)")
61 % ShearName<double>::value
62 % v[0] % v[1] % v[2]
63 % v[3] % v[4] % v[5]).str();
64 }
65
66 template <class T>
67 static Shear6<T> * shearTupleConstructor(tuple t)
68 {
69 if(t.attr("__len__")() == 3){
70 return new Shear6<T>(extract<T>(t[0]), extract<T>(t[1]), extract<T>(t[2]),
71 T(0), T(0), T(0));
72 }
73 else if(t.attr("__len__")() == 6){
74 return new Shear6<T>(extract<T>(t[0]), extract<T>(t[1]), extract<T>(t[2]),
75 extract<T>(t[3]), extract<T>(t[4]), extract<T>(t[5]));
76 }
77 else
78 throw std::invalid_argument ("Shear6 expects tuple of length 3 or 6");
79 }
80
81 template <class T>
82 static Shear6<T> * shearConstructor1(T a)
83 {
84 return new Shear6<T>(a, a, a, a, a, a);
85 }
86
87 template <class T, class S>
88 static Shear6<T> * shearConversionConstructor(const Shear6<S> &shear)
89 {
90 Shear6<T> *s = new Shear6<T>;
91 *s = shear;
92 return s;
93 }
94
95 template <class T>
96 static const Shear6<T> &
97 iadd(Shear6<T> &shear, const Shear6<T> &other)
98 {
99 MATH_EXC_ON;
100 return shear += other;
101 }
102
103 template <class T>
104 static Shear6<T>
105 add(const Shear6<T> &shear, const Shear6<T> &other)
106 {
107 MATH_EXC_ON;
108 return shear + other;
109 }
110
111 template <class T>
112 static const Shear6<T> &
113 isub(Shear6<T> &shear, const Shear6<T> &other)
114 {
115 MATH_EXC_ON;
116 return shear -= other;
117 }
118
119 template <class T>
120 static Shear6<T>
121 sub(const Shear6<T> &shear, const Shear6<T> &other)
122 {
123 MATH_EXC_ON;
124 return shear - other;
125 }
126
127 template <class T>
128 static Shear6<T>
129 neg(const Shear6<T> &shear)
130 {
131 MATH_EXC_ON;
132 return -shear;
133 }
134
135 template <class T>
136 static const Shear6<T> &
137 imul(Shear6<T> &shear, const Shear6<T> &other)
138 {
139 MATH_EXC_ON;
140 return shear *= other;
141 }
142
143 template <class T>
144 static const Shear6<T> &
145 imulT(Shear6<T> &shear, T t)
146 {
147 MATH_EXC_ON;
148 return shear *= t;
149 }
150
151 template <class T>
152 static Shear6<T>
153 mul(const Shear6<T> &shear, const Shear6<T> &other)
154 {
155 MATH_EXC_ON;
156 return shear * other;
157 }
158
159 template <class T>
160 static Shear6<T>
161 mulT(const Shear6<T> &shear, T t)
162 {
163 MATH_EXC_ON;
164 return shear * t;
165 }
166
167 template <class T>
168 static const Shear6<T> &
169 idiv(Shear6<T> &shear, const Shear6<T> &other)
170 {
171 MATH_EXC_ON;
172 return shear /= other;
173 }
174
175 template <class T>
176 static const Shear6<T> &
177 idivT(Shear6<T> &shear, T t)
178 {
179 MATH_EXC_ON;
180 return shear /= t;
181 }
182
183 template <class T>
184 static Shear6<T>
185 div(const Shear6<T> &shear, const Shear6<T> &other)
186 {
187 MATH_EXC_ON;
188 return shear / other;
189 }
190
191 template <class T>
192 static Shear6<T>
193 divT(const Shear6<T> &shear, T t)
194 {
195 MATH_EXC_ON;
196 return shear / t;
197 }
198
199 template <class T>
200 static Shear6<T>
201 subtract1(Shear6<T> &v, tuple t)
202 {
203 MATH_EXC_ON;
204 Shear6<T> w;
205
206 if(t.attr("__len__")() == 6){
207 w[0] = v[0] - extract<T>(t[0]);
208 w[1] = v[1] - extract<T>(t[1]);
209 w[2] = v[2] - extract<T>(t[2]);
210 w[3] = v[3] - extract<T>(t[3]);
211 w[4] = v[4] - extract<T>(t[4]);
212 w[5] = v[5] - extract<T>(t[5]);
213 }
214 else
215 throw std::domain_error ("tuple must have length of 6");
216
217 return w;
218 }
219
220 template <class T>
221 static Shear6<T>
222 subtract2(Shear6<T> &v, tuple t)
223 {
224 MATH_EXC_ON;
225 Shear6<T> w;
226
227 if(t.attr("__len__")() == 6){
228 w[0] = extract<T>(t[0]) - v[0];
229 w[1] = extract<T>(t[1]) - v[1];
230 w[2] = extract<T>(t[2]) - v[2];
231 w[3] = extract<T>(t[3]) - v[3];
232 w[4] = extract<T>(t[4]) - v[4];
233 w[5] = extract<T>(t[5]) - v[5];
234 }
235 else
236 throw std::domain_error ("tuple must have length of 6");
237
238 return w;
239 }
240
241 template <class T>
242 static Shear6<T>
243 subtractT1(Shear6<T> &v, T a)
244 {
245 MATH_EXC_ON;
246 Shear6<T> w;
247
248 w[0] = v[0] - a;
249 w[1] = v[1] - a;
250 w[2] = v[2] - a;
251 w[3] = v[3] - a;
252 w[4] = v[4] - a;
253 w[5] = v[5] - a;
254
255 return w;
256 }
257
258 template <class T>
259 static Shear6<T>
260 subtractT2(Shear6<T> &v, T a)
261 {
262 MATH_EXC_ON;
263 Shear6<T> w;
264
265 w[0] = a - v[0];
266 w[1] = a - v[1];
267 w[2] = a - v[2];
268 w[3] = a - v[3];
269 w[4] = a - v[4];
270 w[5] = a - v[5];
271
272 return w;
273 }
274
275
276 template <class T>
277 static Shear6<T>
278 addTuple(Shear6<T> &v, tuple t)
279 {
280 MATH_EXC_ON;
281 Shear6<T> w;
282
283 if(t.attr("__len__")() == 6){
284 w[0] = v[0] + extract<T>(t[0]);
285 w[1] = v[1] + extract<T>(t[1]);
286 w[2] = v[2] + extract<T>(t[2]);
287 w[3] = v[3] + extract<T>(t[3]);
288 w[4] = v[4] + extract<T>(t[4]);
289 w[5] = v[5] + extract<T>(t[5]);
290 }
291 else
292 throw std::domain_error ("tuple must have length of 6");
293
294 return w;
295 }
296
297 template <class T>
298 static Shear6<T>
299 addT(Shear6<T> &v, T a)
300 {
301 MATH_EXC_ON;
302 Shear6<T> w;
303
304 w[0] = v[0] + a;
305 w[1] = v[1] + a;
306 w[2] = v[2] + a;
307 w[3] = v[3] + a;
308 w[4] = v[4] + a;
309 w[5] = v[5] + a;
310
311 return w;
312 }
313
314 template <class T>
315 static Shear6<T>
316 multTuple(Shear6<T> &v, tuple t)
317 {
318 MATH_EXC_ON;
319 Shear6<T> w;
320
321 if(t.attr("__len__")() == 6){
322 w[0] = v[0] * extract<T>(t[0]);
323 w[1] = v[1] * extract<T>(t[1]);
324 w[2] = v[2] * extract<T>(t[2]);
325 w[3] = v[3] * extract<T>(t[3]);
326 w[4] = v[4] * extract<T>(t[4]);
327 w[5] = v[5] * extract<T>(t[5]);
328 }
329 else
330 throw std::domain_error ("tuple must have length of 6");
331
332 return w;
333 }
334
335 template <class T>
336 static Shear6<T>
337 rdiv(Shear6<T> &v, T a)
338 {
339 MATH_EXC_ON;
340 Shear6<T> w;
341
342 if(v != Shear6<T>()){
343 w[0] = a/v[0];
344 w[1] = a/v[1];
345 w[2] = a/v[2];
346 w[3] = a/v[3];
347 w[4] = a/v[4];
348 w[5] = a/v[5];
349 }
350 else
351 throw std::domain_error ("Division by Zero");
352
353 return w;
354 }
355
356 template <class T>
357 static Shear6<T>
358 divTuple(Shear6<T> &v, const tuple &t)
359 {
360 MATH_EXC_ON;
361 if(t.attr("__len__")() != 6)
362 throw std::domain_error ("Shear6 expects tuple of length 6");
363
364 Shear6<T> w;
365 for(int i = 0; i < 6; ++i)
366 {
367 T a = extract<T>(t[i]);
368 if(a != T (0))
369 w[i] = v[i] / a;
370 else
371 throw std::domain_error ("Division by Zero");
372 }
373
374 return w;
375 }
376
377 template <class T>
378 static Shear6<T>
379 rdivTuple(Shear6<T> &v, const tuple &t)
380 {
381 MATH_EXC_ON;
382 if(t.attr("__len__")() != 6)
383 throw std::domain_error ("Shear6 expects tuple of length 6");
384
385 Shear6<T> w;
386 for(int i = 0; i < 6; ++i)
387 {
388 T a = extract<T>(t[i]);
389 if(v[i] != T (0))
390 w[i] = a / v[i];
391 else
392 throw std::domain_error ("Division by Zero");
393 }
394
395 return w;
396 }
397
398 template <class T>
399 static bool
400 lessThan(Shear6<T> &v, const Shear6<T> &w)
401 {
402 bool isLessThan = (v[0] <= w[0] && v[1] <= w[1] && v[2] <= w[2]
403 && v[3] <= w[3] && v[4] <= w[4] && v[5] <= w[5])
404 && v != w;
405
406 return isLessThan;
407 }
408
409 template <class T>
410 static bool
411 greaterThan(Shear6<T> &v, const Shear6<T> &w)
412 {
413 bool isGreaterThan = (v[0] >= w[0] && v[1] >= w[1] && v[2] >= w[2]
414 && v[3] >= w[3] && v[4] >= w[4] && v[5] >= w[5])
415 && v != w;
416
417 return isGreaterThan;
418 }
419
420 template <class T>
421 static bool
422 lessThanEqual(Shear6<T> &v, const Shear6<T> &w)
423 {
424 bool isLessThanEqual = (v[0] <= w[0] && v[1] <= w[1] && v[2] <= w[2]
425 && v[3] <= w[3] && v[4] <= w[4] && v[5] <= w[5]);
426
427 return isLessThanEqual;
428 }
429
430 template <class T>
431 static bool
432 greaterThanEqual(Shear6<T> &v, const Shear6<T> &w)
433 {
434 bool isGreaterThanEqual = (v[0] >= w[0] && v[1] >= w[1] && v[2] >= w[2]
435 && v[3] >= w[3] && v[4] >= w[4] && v[5] >= w[5]);
436
437 return isGreaterThanEqual;
438 }
439
440 template <class T>
441 static T
442 getitem(Shear6<T> &shear, int i)
443 {
444 return shear[i];
445 }
446
447 template <class T>
448 static void
449 setitem(Shear6<T> &shear, int i, T a)
450 {
451 if(i < 0 || i > 5)
452 throw std::domain_error ("Index out of range");
453
454 shear[i] = a;
455 }
456
457 template <class T>
458 static int
459 len(Shear6<T> &shear)
460 {
461 return 6;
462 }
463
464
465
466 template <class T>
467 class_<Shear6<T> >
468 register_Shear()
469 {
470 const char *name = ShearName<T>::value;
471
472 void (IMATH_NAMESPACE::Shear6<T>::*setValue1)(T,T,T,T,T,T) = &IMATH_NAMESPACE::Shear6<T>::setValue;
473 void (IMATH_NAMESPACE::Shear6<T>::*setValue2)(const Shear6<T> &) = &IMATH_NAMESPACE::Shear6<T>::setValue;
474 void (IMATH_NAMESPACE::Shear6<T>::*getValue1)(Shear6<T> &) const = &IMATH_NAMESPACE::Shear6<T>::getValue;
475
476 class_<Shear6<T> > shear_class(name, name, init<Shear6<T> >("copy construction"));
477 shear_class
478 .def(init<>("default construction: (0 0 0 0 0 0)"))
479 .def(init<T,T,T>("Shear(XY,XZ,YZ) construction: (XY XZ YZ 0 0 0)"))
480 .def(init<const Vec3<float> &>("Shear(v) construction: (v.x v.y v.z 0 0 0)"))
481 .def(init<const Vec3<double> &>("Shear(v) construction: (v.x v.y v.z 0 0 0)"))
482 .def(init<const Vec3<int> &>("Shear(v) construction: (v.x v.y v.z 0 0 0)"))
483 .def(init<T,T,T,T,T,T>("Shear(XY, XZ, YZ, YX, ZX, ZY) construction"))
484 .def("__init__", make_constructor(shearConstructor1<T>))
485 .def("__init__", make_constructor(shearTupleConstructor<T>),"Construction from tuple")
486 .def("__init__", make_constructor(shearConversionConstructor<T,float>))
487 .def("__init__", make_constructor(shearConversionConstructor<T,double>))
488 .def("__init__", make_constructor(shearConversionConstructor<T,int>))
489 .def("__iadd__",&iadd<T>,return_internal_reference<>())
490 .def("__add__",&add<T>)
491 .def("__isub__",&isub<T>,return_internal_reference<>())
492 .def("__sub__",&sub<T>)
493 .def("__neg__",&neg<T>)
494 .def("__imul__",&imul<T>,return_internal_reference<>())
495 .def("__imul__",&imulT<T>,return_internal_reference<>())
496 .def("__mul__",&mul<T>)
497 .def("__mul__",&mulT<T>)
498 .def("__rmul__",&mulT<T>)
499 .def("__idiv__",&idiv<T>,return_internal_reference<>())
500 .def("__idiv__",&idivT<T>,return_internal_reference<>())
501 .def("__itruediv__",&idiv<T>,return_internal_reference<>())
502 .def("__itruediv__",&idivT<T>,return_internal_reference<>())
503 .def("__div__",&div<T>)
504 .def("__div__",&divT<T>)
505 .def("__truediv__",&div<T>)
506 .def("__truediv__",&divT<T>)
507 .def(self == self) // NOSONAR - suppress SonarCloud bug report.
508 .def(self != self) // NOSONAR - suppress SonarCloud bug report.
509 .def("__str__",&Shear_str<T>)
510 .def("__repr__",&Shear_repr<T>)
511 .def("setValue", setValue1)
512 .def("setValue", setValue2)
513 .def("getValue", getValue1)
514 .def("negate", &Shear6<T>::negate, return_internal_reference<>())
515 .def("baseTypeLowest", &Shear6<T>::baseTypeLowest)
516 .staticmethod("baseTypeLowest")
517 .def("baseTypeMax", &Shear6<T>::baseTypeMax)
518 .staticmethod("baseTypeMax")
519 .def("baseTypeSmallest", &Shear6<T>::baseTypeSmallest)
520 .staticmethod("baseTypeSmallest")
521 .def("baseTypeEpsilon", &Shear6<T>::baseTypeEpsilon)
522 .staticmethod("baseTypeEpsilon")
523 .def("equalWithAbsError", &Shear6<T>::equalWithAbsError)
524 .def("equalWithRelError", &Shear6<T>::equalWithRelError)
525 .def("__sub__", &subtract1<T>)
526 .def("__sub__", &subtractT1<T>)
527 .def("__rsub__", &subtract2<T>)
528 .def("__rsub__", &subtractT2<T>)
529 .def("__add__", &addTuple<T>)
530 .def("__add__", &addT<T>)
531 .def("__radd__", &addTuple<T>)
532 .def("__radd__", &addT<T>)
533 .def("__mul__", &multTuple<T>)
534 .def("__rmul__", &multTuple<T>)
535 .def("__div__", &divTuple<T>)
536 .def("__truediv__", &divTuple<T>)
537 .def("__rdiv__", &rdiv<T>)
538 .def("__rdiv__", &rdivTuple<T>)
539 .def("__rtruediv__", &rdiv<T>)
540 .def("__rtruediv__", &rdivTuple<T>)
541 .def("__lt__", &lessThan<T>)
542 .def("__gt__", &greaterThan<T>)
543 .def("__le__", &lessThanEqual<T>)
544 .def("__ge__", &greaterThanEqual<T>)
545 .def("__getitem__", &getitem<T>)
546 .def("__setitem__", &setitem<T>)
547 .def("__len__", &len<T>)
548 ;
549
550 decoratecopy(shear_class);
551
552 return shear_class;
553 }
554
555 template PYIMATH_EXPORT class_<Shear6<float> > register_Shear();
556 template PYIMATH_EXPORT class_<Shear6<double> > register_Shear();
557
558 }//namespace PyIMath