Imported Upstream version 3.1.9
[platform/upstream/Imath.git] / src / python / PyImath / PyImathVec4ArrayImpl.h
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #ifndef _PyImathVec4ArrayImpl_h_
9 #define _PyImathVec4ArrayImpl_h_
10
11 //
12 // This .C file was turned into a header file so that instantiations
13 // of the various V4* 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 "PyImathVec.h"
26 #include "PyImathDecorators.h"
27 #include "PyImathMathExc.h"
28 #include "PyImathOperators.h"
29 #include "PyImathVecOperators.h"
30
31 namespace PyImath {
32 using namespace boost::python;
33 using namespace IMATH_NAMESPACE;
34
35 // XXX fixme - template this
36 // really this should get generated automatically...
37
38 template <class T,int index>
39 static FixedArray<T>
40 Vec4Array_get(FixedArray<IMATH_NAMESPACE::Vec4<T> > &va)
41 {
42     return FixedArray<T>(&(va.unchecked_index(0)[index]),
43                          va.len(), 4*va.stride(), va.handle(), va.writable());
44 }
45
46 template <class T>
47 static void
48 setItemTuple(FixedArray<IMATH_NAMESPACE::Vec4<T> > &va, Py_ssize_t index, const tuple &t)
49 {
50     if(t.attr("__len__")() == 4)
51     {
52         Vec4<T> v;
53         v.x = extract<T>(t[0]);
54         v.y = extract<T>(t[1]);
55         v.z = extract<T>(t[2]);
56         v.w = extract<T>(t[3]);
57
58         va[va.canonical_index(index)] = v;
59     }
60     else
61       throw std::invalid_argument ("tuple of length 4 expected");
62 }
63
64 template <class T>
65 static IMATH_NAMESPACE::Vec4<T>
66 Vec4Array_min(const FixedArray<IMATH_NAMESPACE::Vec4<T> > &a) {
67     Vec4<T> tmp(Vec4<T>(0));
68     size_t len = a.len();
69     if (len > 0)
70         tmp = a[0];
71     for (size_t i=1; i < len; ++i)
72     {
73         if (a[i].x < tmp.x)
74             tmp.x = a[i].x;
75         if (a[i].y < tmp.y)
76             tmp.y = a[i].y;
77         if (a[i].z < tmp.z)
78             tmp.z = a[i].z;
79         if (a[i].w < tmp.w)
80             tmp.w = a[i].w;
81     }
82     return tmp;
83 }
84
85 template <class T>
86 static IMATH_NAMESPACE::Vec4<T>
87 Vec4Array_max(const FixedArray<IMATH_NAMESPACE::Vec4<T> > &a)
88 {
89     Vec4<T> tmp(Vec4<T>(0));
90     size_t len = a.len();
91     if (len > 0)
92         tmp = a[0];
93     for (size_t i=1; i < len; ++i)
94     {
95         if (a[i].x > tmp.x)
96             tmp.x = a[i].x;
97         if (a[i].y > tmp.y)
98             tmp.y = a[i].y;
99         if (a[i].z > tmp.z)
100             tmp.z = a[i].z;
101         if (a[i].w > tmp.w)
102             tmp.w = a[i].w;
103     }
104     return tmp;
105 }
106
107
108 // Trick to register methods for float-only-based vectors
109 template <class T, IMATH_ENABLE_IF(!std::is_integral<T>::value)>
110 void register_Vec4Array_floatonly(class_<FixedArray<Vec4<T>>>& vec4Array_class)
111 {
112     generate_member_bindings<op_vecLength<IMATH_NAMESPACE::Vec4<T> >     >(vec4Array_class,"length","");
113     generate_member_bindings<op_vecNormalize<IMATH_NAMESPACE::Vec4<T> >  >(vec4Array_class,"normalize","");
114     generate_member_bindings<op_vecNormalized<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"normalized","");
115     generate_member_bindings<op_vecNormalizeExc<IMATH_NAMESPACE::Vec4<T> >  >(vec4Array_class,"normalizeExc","");
116     generate_member_bindings<op_vecNormalizedExc<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"normalizedExc","");
117 }
118
119 template <class T, IMATH_ENABLE_IF(std::is_integral<T>::value)>
120 void register_Vec4Array_floatonly(class_<FixedArray<Vec4<T>>>& vec4Array_class)
121 {
122 }
123
124
125
126 template <class T>
127 class_<FixedArray<IMATH_NAMESPACE::Vec4<T> > >
128 register_Vec4Array()
129 {
130     using boost::mpl::true_;
131
132     class_<FixedArray<IMATH_NAMESPACE::Vec4<T> > > vec4Array_class = FixedArray<IMATH_NAMESPACE::Vec4<T> >::register_("Fixed length array of IMATH_NAMESPACE::Vec4");
133     vec4Array_class
134         .add_property("x",&Vec4Array_get<T,0>)
135         .add_property("y",&Vec4Array_get<T,1>)
136         .add_property("z",&Vec4Array_get<T,2>)
137         .add_property("w",&Vec4Array_get<T,3>)
138         .def("__setitem__", &setItemTuple<T>)
139         .def("min", &Vec4Array_min<T>)
140         .def("max", &Vec4Array_max<T>)
141         ;
142
143     add_arithmetic_math_functions(vec4Array_class);
144     add_comparison_functions(vec4Array_class);
145
146     register_Vec4Array_floatonly(vec4Array_class);
147     generate_member_bindings<op_vecLength2<IMATH_NAMESPACE::Vec4<T> >    >(vec4Array_class,"length2","");
148     generate_member_bindings<op_vecDot<IMATH_NAMESPACE::Vec4<T> >,true_>(vec4Array_class,"dot","return the inner product of (self,x)",boost::python::args("x"));
149     generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec4<T>,T>,  true_>(vec4Array_class,"__mul__" ,"self*x", boost::python::args("x"));
150     generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec4<T>,T>,  true_>(vec4Array_class,"__rmul__","x*self", boost::python::args("x"));
151     generate_member_bindings<op_imul<IMATH_NAMESPACE::Vec4<T>,T>, true_>(vec4Array_class,"__imul__","self*=x",boost::python::args("x"));
152     generate_member_bindings<op_div<IMATH_NAMESPACE::Vec4<T>,T>,  true_>(vec4Array_class,"__div__" ,"self/x", boost::python::args("x"));
153     generate_member_bindings<op_div<IMATH_NAMESPACE::Vec4<T>,T>,  true_>(vec4Array_class,"__truediv__" ,"self/x", boost::python::args("x"));
154     generate_member_bindings<op_idiv<IMATH_NAMESPACE::Vec4<T>,T>, true_>(vec4Array_class,"__idiv__","self/=x",boost::python::args("x"));
155     generate_member_bindings<op_idiv<IMATH_NAMESPACE::Vec4<T>,T>, true_>(vec4Array_class,"__itruediv__","self/=x",boost::python::args("x"));
156
157     decoratecopy(vec4Array_class);
158
159     return vec4Array_class;
160 }
161
162
163 }  // namespace PyImath
164
165 #endif   // _PyImathVec4ArrayImpl_h_