Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedtest and Inversion python bindings for Arrays #20

Merged
merged 11 commits into from
Jul 17, 2020
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Contributors to the OpenEXR Project.

if(IMATH_BUILD_BOTH_STATIC_SHARED OR IMATH_BUILD_BOTH_STATIC_SHARED)
if(IMATH_BUILD_BOTH_STATIC_SHARED)
if (${CMAKE_VERSION} VERSION_LESS "3.12.0")
message(FATAL_ERROR "CMake 3.12 or newer is required for object library support when building both static and shared libraries")
endif()
Expand Down
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ include(CTest)
if(BUILD_TESTING)
enable_testing()
add_subdirectory( PyImathTest )
add_subdirectory( PyImathSpeedTest )
if(TARGET Python2::ImathNumPy OR TARGET Python3::ImathNumPy)
add_subdirectory( PyImathNumpyTest )
endif()
Expand Down
26 changes: 26 additions & 0 deletions python/PyImath/PyImathMatrix22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,39 @@ setM22ArrayItem(FixedArray<IMATH_NAMESPACE::Matrix22<T> > &ma,
ma[ma.canonical_index(index)] = m;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix22<T> >
inverse22_array(FixedArray<IMATH_NAMESPACE::Matrix22<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
FixedArray<IMATH_NAMESPACE::Matrix22<T> > dst(len);
for (size_t i=0; i<len; ++i) dst[i] = ma[i].inverse(singExc);
return dst;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix22<T> > &
invert22_array(FixedArray<IMATH_NAMESPACE::Matrix22<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
for (size_t i=0; i<len; ++i) ma[i].invert(singExc);
return ma;
}

BOOST_PYTHON_FUNCTION_OVERLOADS(invert22_array_overloads, invert22_array, 1, 2);
BOOST_PYTHON_FUNCTION_OVERLOADS(inverse22_array_overloads, inverse22_array, 1, 2);

template <class T>
class_<FixedArray<IMATH_NAMESPACE::Matrix22<T> > >
register_M22Array()
{
class_<FixedArray<IMATH_NAMESPACE::Matrix22<T> > > matrixArray_class = FixedArray<IMATH_NAMESPACE::Matrix22<T> >::register_("Fixed length array of IMATH_NAMESPACE::Matrix22");
matrixArray_class
.def("__setitem__", &setM22ArrayItem<T>)
.def("inverse",&inverse22_array<T>,inverse22_array_overloads("inverse() return an inverted copy of this matrix"))
.def("invert",&invert22_array<T>,invert22_array_overloads("invert() invert these matricies")[return_internal_reference<>()])
;
return matrixArray_class;
}
Expand Down
53 changes: 51 additions & 2 deletions python/PyImath/PyImathMatrix33.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ invert33 (Matrix33<T> &m, bool singExc = true)
}

template <class T>
static Matrix33<T>
inverse33 (Matrix33<T> &m, bool singExc = true)
static Matrix33<T>inverse33 (Matrix33<T> &m, bool singExc = true)
{
MATH_EXC_ON;
return m.inverse(singExc);
Expand Down Expand Up @@ -1097,13 +1096,63 @@ setM33ArrayItem(FixedArray<IMATH_NAMESPACE::Matrix33<T> > &ma,
ma[ma.canonical_index(index)] = m;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix33<T> >
gjInverse33_array(FixedArray<IMATH_NAMESPACE::Matrix33<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
FixedArray<IMATH_NAMESPACE::Matrix33<T> > dst(len);
for (size_t i=0; i<len; ++i) dst[i] = ma[i].gjInverse(singExc);
return dst;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix33<T> > &
gjInvert33_array(FixedArray<IMATH_NAMESPACE::Matrix33<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
for (size_t i=0; i<len; ++i) ma[i].gjInvert(singExc);
return ma;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix33<T> >
inverse33_array(FixedArray<IMATH_NAMESPACE::Matrix33<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
FixedArray<IMATH_NAMESPACE::Matrix33<T> > dst(len);
for (size_t i=0; i<len; ++i) dst[i] = ma[i].inverse(singExc);
return dst;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix33<T> > &
invert33_array(FixedArray<IMATH_NAMESPACE::Matrix33<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
for (size_t i=0; i<len; ++i) ma[i].invert(singExc);
return ma;
}

BOOST_PYTHON_FUNCTION_OVERLOADS(invert33_array_overloads, invert33_array, 1, 2);
BOOST_PYTHON_FUNCTION_OVERLOADS(inverse33_array_overloads, inverse33_array, 1, 2);


template <class T>
class_<FixedArray<IMATH_NAMESPACE::Matrix33<T> > >
register_M33Array()
{
class_<FixedArray<IMATH_NAMESPACE::Matrix33<T> > > matrixArray_class = FixedArray<IMATH_NAMESPACE::Matrix33<T> >::register_("Fixed length array of IMATH_NAMESPACE::Matrix33");
matrixArray_class
.def("__setitem__", &setM33ArrayItem<T>)
.def("inverse",&inverse33_array<T>,inverse33_array_overloads("inverse() return an inverted copy of these matricies"))
.def("invert",&invert33_array<T>,invert33_array_overloads("invert() invert these matricies")[return_internal_reference<>()])
.def("gjInverse",&gjInverse33_array<T>,inverse33_array_overloads("gjInverse() return an inverted copy of these matricies"))
.def("gjInvert",&gjInvert33_array<T>,invert33_array_overloads("gjInvert() invert these matricies")[return_internal_reference<>()])
;
return matrixArray_class;
}
Expand Down
51 changes: 50 additions & 1 deletion python/PyImath/PyImathMatrix44.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1187,14 +1187,63 @@ setM44ArrayItem(FixedArray<IMATH_NAMESPACE::Matrix44<T> > &ma,
ma[ma.canonical_index(index)] = m;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix44<T> >
inverse44_array(FixedArray<IMATH_NAMESPACE::Matrix44<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
FixedArray<IMATH_NAMESPACE::Matrix44<T> > dst(len);
for (size_t i=0; i<len; ++i) dst[i] = ma[i].inverse(singExc);
return dst;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix44<T> > &
invert44_array(FixedArray<IMATH_NAMESPACE::Matrix44<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
for (size_t i=0; i<len; ++i) ma[i].invert(singExc);
return ma;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix44<T> >
gjInverse44_array(FixedArray<IMATH_NAMESPACE::Matrix44<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
FixedArray<IMATH_NAMESPACE::Matrix44<T> > dst(len);
for (size_t i=0; i<len; ++i) dst[i] = ma[i].gjInverse(singExc);
return dst;
}

template <class T>
static FixedArray<IMATH_NAMESPACE::Matrix44<T> > &
gjInvert44_array(FixedArray<IMATH_NAMESPACE::Matrix44<T> >&ma, bool singExc = true)
{
MATH_EXC_ON;
size_t len = ma.len();
for (size_t i=0; i<len; ++i) ma[i].gjInvert(singExc);
return ma;
}

BOOST_PYTHON_FUNCTION_OVERLOADS(invert44_array_overloads, invert44_array, 1, 2);
BOOST_PYTHON_FUNCTION_OVERLOADS(inverse44_array_overloads, inverse44_array, 1, 2);

template <class T>
class_<FixedArray<IMATH_NAMESPACE::Matrix44<T> > >
register_M44Array()
{
class_<FixedArray<IMATH_NAMESPACE::Matrix44<T> > > matrixArray_class = FixedArray<IMATH_NAMESPACE::Matrix44<T> >::register_("Fixed length array of IMATH_NAMESPACE::Matrix44");
matrixArray_class
.def("__setitem__", &setM44ArrayItem<T>)
;
.def("inverse",&inverse44_array<T>,inverse44_array_overloads("inverse() return an inverted copy of this matrix"))
.def("invert",&invert44_array<T>,invert44_array_overloads("invert() invert these matricies")[return_internal_reference<>()])
.def("gjInverse",&gjInverse44_array<T>,inverse44_array_overloads("gjInverse() return an inverted copy of this matrix"))
.def("gjInvert",&gjInvert44_array<T>,invert44_array_overloads("giInvert() invert these matricies")[return_internal_reference<>()])
;
return matrixArray_class;
}

Expand Down
2 changes: 2 additions & 0 deletions python/PyImath/PyImathVec2Impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,8 @@ register_Vec2Array()
generate_member_bindings<op_vecLength2<IMATH_NAMESPACE::Vec2<T> > >(vec2Array_class,"length2","");
generate_member_bindings<op_vecNormalize<IMATH_NAMESPACE::Vec2<T> > >(vec2Array_class,"normalize","");
generate_member_bindings<op_vecNormalized<IMATH_NAMESPACE::Vec2<T> > >(vec2Array_class,"normalized","");
generate_member_bindings<op_vecNormalizeExc<IMATH_NAMESPACE::Vec2<T> > >(vec2Array_class,"normalizeExc","");
generate_member_bindings<op_vecNormalizedExc<IMATH_NAMESPACE::Vec2<T> > >(vec2Array_class,"normalizedExc","");

generate_member_bindings<op_vec2Cross<T>, true_>(vec2Array_class,"cross","return the cross product of (self,x)",boost::python::args("x"));
generate_member_bindings<op_vecDot<IMATH_NAMESPACE::Vec2<T> >,true_>(vec2Array_class,"dot","return the inner product of (self,x)",boost::python::args("x"));
Expand Down
2 changes: 2 additions & 0 deletions python/PyImath/PyImathVec3ArrayImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ register_Vec3Array()
generate_member_bindings<op_vecLength2<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"length2","");
generate_member_bindings<op_vecNormalize<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalize","");
generate_member_bindings<op_vecNormalized<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalized","");
generate_member_bindings<op_vecNormalizeExc<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalizeExc","");
generate_member_bindings<op_vecNormalizedExc<IMATH_NAMESPACE::Vec3<T> > >(vec3Array_class,"normalizedExc","");

generate_member_bindings<op_vec3Cross<T>, true_>(vec3Array_class,"cross","return the cross product of (self,x)",boost::python::args("x"));
generate_member_bindings<op_vecDot<IMATH_NAMESPACE::Vec3<T> >,true_>(vec3Array_class,"dot","return the inner product of (self,x)",boost::python::args("x"));
Expand Down
2 changes: 2 additions & 0 deletions python/PyImath/PyImathVec4ArrayImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ register_Vec4Array()
generate_member_bindings<op_vecLength2<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"length2","");
generate_member_bindings<op_vecNormalize<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"normalize","");
generate_member_bindings<op_vecNormalized<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"normalized","");
generate_member_bindings<op_vecNormalizeExc<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"normalizeExc","");
generate_member_bindings<op_vecNormalizedExc<IMATH_NAMESPACE::Vec4<T> > >(vec4Array_class,"normalizedExc","");

generate_member_bindings<op_vecDot<IMATH_NAMESPACE::Vec4<T> >,true_>(vec4Array_class,"dot","return the inner product of (self,x)",boost::python::args("x"));
generate_member_bindings<op_mul<IMATH_NAMESPACE::Vec4<T>,T>, true_>(vec4Array_class,"__mul__" ,"self*x", boost::python::args("x"));
Expand Down
10 changes: 10 additions & 0 deletions python/PyImath/PyImathVecOperators.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ struct op_vecNormalized {
static inline T apply(const T &v) { return v.normalized(); }
};

template <class T>
struct op_vecNormalizeExc {
static inline void apply(T &v) { v.normalizeExc(); }
};

template <class T>
struct op_vecNormalizedExc {
static inline T apply(const T &v) { return v.normalizedExc(); }
};

template <class T>
struct op_vec3Cross {
static inline IMATH_NAMESPACE::Vec3<T> apply(const IMATH_NAMESPACE::Vec3<T> &a, const IMATH_NAMESPACE::Vec3<T> &b) { return a.cross(b); }
Expand Down
13 changes: 13 additions & 0 deletions python/PyImathSpeedTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Contributors to the OpenEXR Project.

# Speed tests are not performed in Python2 as getting acurate time is difficult.
# set -DSPEED=ON when running cmake to perform these tests.
if(TARGET Python3::Interpreter AND SPEED)
add_test(PyImath.PyImathSpeedTest_Python3
${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/pyImathSpeedTest.in
)
set_tests_properties(PyImath.PyImathSpeedTest_Python3 PROPERTIES
ENVIRONMENT "PYTHONPATH=${CMAKE_BINARY_DIR}/python${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR}"
)
endif()
Loading