| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// clang-format off |
| 7 |
|
| 8 |
#ifndef _PyImathVec3ArrayImpl_h_ |
| 9 |
#define _PyImathVec3ArrayImpl_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 "PyImathBox.h" |
| 26 |
#include "PyImathVec.h" |
| 27 |
#include "PyImathDecorators.h" |
| 28 |
#include "PyImathMathExc.h" |
| 29 |
#include "PyImathOperators.h" |
| 30 |
#include "PyImathVecOperators.h" |
| 31 |
|
| 32 |
namespace PyImath { |
| 33 |
using namespace boost::python; |
| 34 |
using namespace IMATH_NAMESPACE; |
| 35 |
|
| 36 |
// XXX fixme - template this |
| 37 |
// really this should get generated automatically... |
| 38 |
|
| 39 |
template <class T,int index> |
| 40 |
static FixedArray<T> |
| 41 |
Vec3Array_get(FixedArray<IMATH_NAMESPACE::Vec3<T> > &va) |
| 42 |
{ |
| 43 |
return FixedArray<T>(&(va.unchecked_index(0)[index]), |
| 44 |
va.len(), 3*va.stride(), va.handle(), va.writable()); |
| 45 |
} |
| 46 |
|
| 47 |
template <class T> |
| 48 |
static void |
| 49 |
setItemTuple(FixedArray<IMATH_NAMESPACE::Vec3<T> > &va, Py_ssize_t index, const tuple &t) |
| 50 |
{ |
| 51 |
if(t.attr("__len__")() == 3) |
| 52 |
{ |
| 53 |
Vec3<T> v; |
| 54 |
v.x = extract<T>(t[0]); |
| 55 |
v.y = extract<T>(t[1]); |
| 56 |
v.z = extract<T>(t[2]); |
| 57 |
va[va.canonical_index(index)] = v; |
| 58 |
} |
| 59 |
else |
| 60 |
throw std::invalid_argument ("tuple of length 3 expected"); |
| 61 |
} |
| 62 |
|
| 63 |
template <class T> |
| 64 |
static IMATH_NAMESPACE::Vec3<T> |
| 65 |
Vec3Array_min(const FixedArray<IMATH_NAMESPACE::Vec3<T> > &a) |
| 66 |
{ |
| 67 |
Vec3<T> tmp(Vec3<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 |
} |
| 80 |
return tmp; |
| 81 |
} |
| 82 |
|
| 83 |
template <class T> |
| 84 |
static IMATH_NAMESPACE::Vec3<T> |
| 85 |
Vec3Array_max(const FixedArray<IMATH_NAMESPACE::Vec3<T> > &a) |
| 86 |
{ |
| 87 |
Vec3<T> tmp(Vec3<T>(0)); |
| 88 |
size_t len = a.len(); |
| 89 |
if (len > 0) |
| 90 |
tmp = a[0]; |
| 91 |
for (size_t i=1; i < len; ++i) |
| 92 |
{ |
| 93 |
if (a[i].x > tmp.x) |
| 94 |
tmp.x = a[i].x; |
| 95 |
if (a[i].y > tmp.y) |
| 96 |
tmp.y = a[i].y; |
| 97 |
if (a[i].z > tmp.z) |
| 98 |
tmp.z = a[i].z; |
| 99 |
} |
| 100 |
return tmp; |
| 101 |
} |
| 102 |
|
| 103 |
template <class T> |
| 104 |
static IMATH_NAMESPACE::Box<IMATH_NAMESPACE::Vec3<T> > |
| 105 |
Vec3Array_bounds(const FixedArray<IMATH_NAMESPACE::Vec3<T> > &a) |
| 106 |
{ |
| 107 |
Box<Vec3<T> > tmp; |
| 108 |
size_t len = a.len(); |
| 109 |
for (size_t i=0; i < len; ++i) |
| 110 |
tmp.extendBy(a[i]); |
| 111 |
return tmp; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
// Trick to register methods for float-only-based vectors |
| 116 |
template <class T, IMATH_ENABLE_IF(!std::is_integral<T>::value)> |
| 117 |
void register_Vec3Array_floatonly(class_<FixedArray<Vec3<T>>>& vec3Array_class) |
| 118 |
{ |
| 119 |
generate_member_bindings<op_vecLength<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"length",""); |
| 120 |
generate_member_bindings<op_vecNormalize<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalize",""); |
| 121 |
generate_member_bindings<op_vecNormalized<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalized",""); |
| 122 |
generate_member_bindings<op_vecNormalizeExc<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalizeExc",""); |
| 123 |
generate_member_bindings<op_vecNormalizedExc<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalizedExc",""); |
| 124 |
} |
| 125 |
|
| 126 |
template <class T, IMATH_ENABLE_IF(std::is_integral<T>::value)> |
| 127 |
void register_Vec3Array_floatonly(class_<FixedArray<Vec3<T>>>& vec3Array_class) |
| 128 |
{ |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
template <class T> |
| 134 |
class_<FixedArray<IMATH_NAMESPACE::Vec3<T> > > |
| 135 |
register_Vec3Array() |
| 136 |
{ |
| 137 |
using boost::mpl::true_; |
| 138 |
using boost::mpl::false_; |
| 139 |
|
| 140 |
class_<FixedArray<IMATH_NAMESPACE::Vec3<T> > > vec3Array_class = FixedArray<IMATH_NAMESPACE::Vec3<T> >::register_("Fixed length array of IMATH_NAMESPACE::Vec3"); |
| 141 |
vec3Array_class |
| 142 |
.add_property("x",&Vec3Array_get<T,0>) |
| 143 |
.add_property("y",&Vec3Array_get<T,1>) |
| 144 |
.add_property("z",&Vec3Array_get<T,2>) |
| 145 |
.def("__setitem__", &setItemTuple<T>) |
| 146 |
.def("min", &Vec3Array_min<T>) |
| 147 |
.def("max", &Vec3Array_max<T>) |
| 148 |
.def("bounds", &Vec3Array_bounds<T>) |
| 149 |
; |
| 150 |
|
| 151 |
add_arithmetic_math_functions(vec3Array_class); |
| 152 |
add_comparison_functions(vec3Array_class); |
| 153 |
|
| 154 |
register_Vec3Array_floatonly(vec3Array_class); |
| 155 |
generate_member_bindings<op_vecLength2<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"length2",""); |
| 156 |
|
| 157 |
generate_member_bindings<op_vec3Cross<T>, true_>(vec3Array_class,"cross","return the cross product of (self,x)",boost::python::args("x")); |
| 158 |
generate_member_bindings<op_vecDot<IMATH_NAMESPACE::Vec3<T> >,true_>(vec3Array_class,"dot","return the inner product of (self,x)",boost::python::args("x")); |
| 159 |
|
| 160 |
generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__mul__" ,"self*x", boost::python::args("x")); |
| 161 |
generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec3<T>,IMATH_NAMESPACE::M44f>,false_>(vec3Array_class,"__mul__" ,"self*x", boost::python::args("x")); |
| 162 |
generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec3<T>,IMATH_NAMESPACE::M44d>,false_>(vec3Array_class,"__mul__" ,"self*x", boost::python::args("x")); |
| 163 |
|
| 164 |
generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__rmul__","x*self", boost::python::args("x")); |
| 165 |
generate_member_bindings<op_imul<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__imul__","self*=x",boost::python::args("x")); |
| 166 |
generate_member_bindings<op_div<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__div__" ,"self/x", boost::python::args("x")); |
| 167 |
generate_member_bindings<op_div<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__truediv__" ,"self/x", boost::python::args("x")); |
| 168 |
generate_member_bindings<op_idiv<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__idiv__","self/=x",boost::python::args("x")); |
| 169 |
generate_member_bindings<op_idiv<IMATH_NAMESPACE::Vec3<T>,T>, true_>(vec3Array_class,"__itruediv__","self/=x",boost::python::args("x")); |
| 170 |
|
| 171 |
decoratecopy(vec3Array_class); |
| 172 |
|
| 173 |
return vec3Array_class; |
| 174 |
} |
| 175 |
|
| 176 |
} // namespace PyImath |
| 177 |
|
| 178 |
#endif // _PyImathVec3ArrayImpl_h_ |