2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
8 #ifndef _PyImathVec4ArrayImpl_h_
9 #define _PyImathVec4ArrayImpl_h_
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.
18 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
19 #include <boost/python.hpp>
20 #include <boost/python/make_constructor.hpp>
21 #include <boost/format.hpp>
23 #include <ImathVecAlgo.h>
25 #include "PyImathVec.h"
26 #include "PyImathDecorators.h"
27 #include "PyImathMathExc.h"
28 #include "PyImathOperators.h"
29 #include "PyImathVecOperators.h"
32 using namespace boost::python;
33 using namespace IMATH_NAMESPACE;
35 // XXX fixme - template this
36 // really this should get generated automatically...
38 template <class T,int index>
40 Vec4Array_get(FixedArray<IMATH_NAMESPACE::Vec4<T> > &va)
42 return FixedArray<T>(&(va.unchecked_index(0)[index]),
43 va.len(), 4*va.stride(), va.handle(), va.writable());
48 setItemTuple(FixedArray<IMATH_NAMESPACE::Vec4<T> > &va, Py_ssize_t index, const tuple &t)
50 if(t.attr("__len__")() == 4)
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]);
58 va[va.canonical_index(index)] = v;
61 throw std::invalid_argument ("tuple of length 4 expected");
65 static IMATH_NAMESPACE::Vec4<T>
66 Vec4Array_min(const FixedArray<IMATH_NAMESPACE::Vec4<T> > &a) {
67 Vec4<T> tmp(Vec4<T>(0));
71 for (size_t i=1; i < len; ++i)
86 static IMATH_NAMESPACE::Vec4<T>
87 Vec4Array_max(const FixedArray<IMATH_NAMESPACE::Vec4<T> > &a)
89 Vec4<T> tmp(Vec4<T>(0));
93 for (size_t i=1; i < len; ++i)
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)
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","");
119 template <class T, IMATH_ENABLE_IF(std::is_integral<T>::value)>
120 void register_Vec4Array_floatonly(class_<FixedArray<Vec4<T>>>& vec4Array_class)
127 class_<FixedArray<IMATH_NAMESPACE::Vec4<T> > >
130 using boost::mpl::true_;
132 class_<FixedArray<IMATH_NAMESPACE::Vec4<T> > > vec4Array_class = FixedArray<IMATH_NAMESPACE::Vec4<T> >::register_("Fixed length array of IMATH_NAMESPACE::Vec4");
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>)
143 add_arithmetic_math_functions(vec4Array_class);
144 add_comparison_functions(vec4Array_class);
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"));
157 decoratecopy(vec4Array_class);
159 return vec4Array_class;
163 } // namespace PyImath
165 #endif // _PyImathVec4ArrayImpl_h_