// // SPDX-License-Identifier: BSD-3-Clause // Copyright Contributors to the OpenEXR Project. // // clang-format off #define BOOST_PYTHON_MAX_ARITY 17 #include #define BOOST_BIND_GLOBAL_PLACEHOLDERS #include #include #include #include #include #include #include #include #include "PyImath.h" #include "PyImathVec.h" #include "PyImathMathExc.h" #include "PyImathMatrix.h" #include "PyImathExport.h" #include "PyImathDecorators.h" namespace PyImath { template<> const char *PyImath::M22fArray::name() { return "M22fArray"; } template<> const char *PyImath::M22dArray::name() { return "M22dArray"; } using namespace boost::python; using namespace IMATH_NAMESPACE; template struct MatrixRow { explicit MatrixRow(T *data) : _data(data) {} T & operator [] (int i) { return _data[i]; } T *_data; static const char *name; static void register_class() { typedef PyImath::StaticFixedArray MatrixRow_helper; class_ matrixRow_class(name,no_init); matrixRow_class .def("__len__", MatrixRow_helper::len) .def("__getitem__", MatrixRow_helper::getitem,return_value_policy()) .def("__setitem__", MatrixRow_helper::setitem) ; } }; template <> const char *MatrixRow::name = "M22fRow"; template <> const char *MatrixRow::name = "M22dRow"; template struct IndexAccessMatrixRow { typedef MatrixRow result_type; static MatrixRow apply(Container &c, int i) { return MatrixRow(c[i]); } }; template struct Matrix22Name { static const char *value; }; template<> const char *Matrix22Name::value = "M22f"; template<> const char *Matrix22Name::value = "M22d"; template static std::string Matrix22_str(const Matrix22 &v) { std::stringstream stream; stream << Matrix22Name::value << "("; for (int row = 0; row < 2; row++) { stream << "("; for (int col = 0; col < 2; col++) { stream << v[row][col]; stream << (col != 1 ? ", " : ""); } stream << ")" << (row != 1 ? ", " : ""); } stream << ")"; return stream.str(); } // Non-specialized repr is same as str template static std::string Matrix22_repr(const Matrix22 &v) { return Matrix22_str(v); } // Specialization for float to full precision template <> std::string Matrix22_repr(const Matrix22 &v) { return (boost::format("%s((%.9g, %.9g), (%.9g, %.9g))") % Matrix22Name::value % v[0][0] % v[0][1] % v[1][0] % v[1][1]).str(); } // Specialization for double to full precision template <> std::string Matrix22_repr(const Matrix22 &v) { return (boost::format("%s((%.17g, %.17g), (%.17g, %.17g))") % Matrix22Name::value % v[0][0] % v[0][1] % v[1][0] % v[1][1]).str(); } template static const Matrix22 & invert22 (Matrix22 &m, bool singExc = true) { MATH_EXC_ON; return m.invert(singExc); } template static Matrix22 inverse22 (Matrix22 &m, bool singExc = true) { MATH_EXC_ON; return m.inverse(singExc); } template static const Matrix22 & iadd22(Matrix22 &m, const Matrix22 &m2) { MATH_EXC_ON; Matrix22 m3; m3.setValue (m2); return m += m3; } template static const Matrix22 & iadd22T(Matrix22 &mat, T a) { MATH_EXC_ON; return mat += a; } template static Matrix22 add22(Matrix22 &m, const Matrix22 &m2) { MATH_EXC_ON; return m + m2; } template static const Matrix22 & isub22(Matrix22 &m, const Matrix22 &m2) { MATH_EXC_ON; Matrix22 m3; m3.setValue (m2); return m -= m3; } template static const Matrix22 & isub22T(Matrix22 &mat, T a) { MATH_EXC_ON; return mat -= a; } template static Matrix22 sub22(Matrix22 &m, const Matrix22 &m2) { MATH_EXC_ON; return m - m2; } template static const Matrix22 & negate22 (Matrix22 &m) { MATH_EXC_ON; return m.negate(); } template static Matrix22 neg22 (Matrix22 &m) { MATH_EXC_ON; return -m; } template static const Matrix22 & imul22T(Matrix22 &m, const T &t) { MATH_EXC_ON; return m *= t; } template static Matrix22 mul22T(Matrix22 &m, const T &t) { MATH_EXC_ON; return m * t; } template static Matrix22 rmul22T(Matrix22 &m, const T &t) { MATH_EXC_ON; return t * m; } template static const Matrix22 & idiv22T(Matrix22 &m, const T &t) { MATH_EXC_ON; return m /= t; } template static Matrix22 div22T(Matrix22 &m, const T &t) { MATH_EXC_ON; return m / t; } template void outerProduct22(Matrix22 &mat, const Vec2 &a, const Vec2 &b) { MATH_EXC_ON; mat = IMATH_NAMESPACE::outerProduct(a,b); } template static void multDirMatrix22(Matrix22 &mat, const Vec2 &src, Vec2 &dst) { MATH_EXC_ON; mat.multDirMatrix(src, dst); } template static Vec2 multDirMatrix22_return_value(Matrix22 &mat, const Vec2 &src) { MATH_EXC_ON; Vec2 dst; mat.multDirMatrix(src, dst); return dst; } template static FixedArray > multDirMatrix22_array(Matrix22 &mat, const FixedArray >&src) { MATH_EXC_ON; size_t len = src.len(); FixedArray > dst(len); for (size_t i=0; i static const Matrix22 & rotate22(Matrix22 &mat, const T &r) { MATH_EXC_ON; return mat.rotate(r); } template static void extractEuler(Matrix22 &mat, Vec2 &dstObj) { MATH_EXC_ON; T dst; IMATH_NAMESPACE::extractEuler(mat, dst); dstObj.setValue(dst, T (0)); } template static const Matrix22 & scaleSc22(Matrix22 &mat, const T &s) { MATH_EXC_ON; Vec2 sVec(s, s); return mat.scale(sVec); } template static const Matrix22 & scaleV22(Matrix22 &mat, const Vec2 &s) { MATH_EXC_ON; return mat.scale(s); } template static const Matrix22 & scale22Tuple(Matrix22 &mat, const tuple &t) { MATH_EXC_ON; if(t.attr("__len__")() == 2) { Vec2 s; s.x = extract(t[0]); s.y = extract(t[1]); return mat.scale(s); } else throw std::invalid_argument ( "m.scale needs tuple of length 2"); } template static const Matrix22 & setRotation22(Matrix22 &mat, const T &r) { MATH_EXC_ON; return mat.setRotation(r); } template static const Matrix22 & setScaleSc22(Matrix22 &mat, const T &s) { MATH_EXC_ON; return mat.setScale(s); } template static const Matrix22 & setScaleV22(Matrix22 &mat, const Vec2 &s) { MATH_EXC_ON; return mat.setScale(s); } template static const Matrix22 & setScale22Tuple(Matrix22 &mat, const tuple &t) { MATH_EXC_ON; if(t.attr("__len__")() == 2) { Vec2 s; s.x = extract(t[0]); s.y = extract(t[1]); return mat.setScale(s); } else throw std::invalid_argument ( "m.setScale needs tuple of length 2"); } template static void setValue22(Matrix22 &mat, const Matrix22 &value) { MATH_EXC_ON; mat.setValue(value); } template static Matrix22 subtractTL22(Matrix22 &mat, T a) { MATH_EXC_ON; Matrix22 m(mat.x); for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j) m.x[i][j] -= a; return m; } template static Matrix22 subtractTR22(Matrix22 &mat, T a) { MATH_EXC_ON; Matrix22 m(mat.x); for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j) m.x[i][j] = a - m.x[i][j]; return m; } template static Matrix22 add22T(Matrix22 &mat, T a) { MATH_EXC_ON; Matrix22 m(mat.x); for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j) m.x[i][j] += a; return m; } template static Matrix22 mul22(Matrix22 &mat1, Matrix22 &mat2) { MATH_EXC_ON; Matrix22 mat2T; mat2T.setValue (mat2); return mat1 * mat2T; } template static Matrix22 rmul22(Matrix22 &mat2, Matrix22 &mat1) { MATH_EXC_ON; Matrix22 mat1T; mat1T.setValue (mat1); return mat1T * mat2; } template static const Matrix22 & imul22(Matrix22 &mat1, Matrix22 &mat2) { MATH_EXC_ON; Matrix22 mat2T; mat2T.setValue (mat2); return mat1 *= mat2T; } template static bool lessThan22(Matrix22 &mat1, const Matrix22 &mat2) { for(int i = 0; i < 2; ++i){ for(int j = 0; j < 2; ++j){ if(mat1[i][j] > mat2[i][j]){ return false; } } } return (mat1 != mat2); } template static bool lessThanEqual22(Matrix22 &mat1, const Matrix22 &mat2) { for(int i = 0; i < 2; ++i){ for(int j = 0; j < 2; ++j){ if(mat1[i][j] > mat2[i][j]){ return false; } } } return true; } template static bool greaterThan22(Matrix22 &mat1, const Matrix22 &mat2) { for(int i = 0; i < 2; ++i){ for(int j = 0; j < 2; ++j){ if(mat1[i][j] < mat2[i][j]){ std::cout << mat1[i][j] << " " << mat2[i][j] << std::endl; return false; } } } return (mat1 != mat2); } template static bool greaterThanEqual22(Matrix22 &mat1, const Matrix22 &mat2) { for(int i = 0; i < 2; ++i){ for(int j = 0; j < 2; ++j){ if(mat1[i][j] < mat2[i][j]){ return false; } } } return true; } BOOST_PYTHON_FUNCTION_OVERLOADS(invert22_overloads, invert22, 1, 2); BOOST_PYTHON_FUNCTION_OVERLOADS(inverse22_overloads, inverse22, 1, 2); BOOST_PYTHON_FUNCTION_OVERLOADS(outerProduct22_overloads, outerProduct22, 3, 3); template static Matrix22 * Matrix2_tuple_constructor(const tuple &t0, const tuple &t1) { if(t0.attr("__len__")() == 2 && t1.attr("__len__")() == 2) { return new Matrix22(extract(t0[0]), extract(t0[1]), extract(t1[0]), extract(t1[1])); } else throw std::invalid_argument ("Matrix22 takes 2 tuples of length 2"); } template static Matrix22 *Matrix2_matrix_constructor(const Matrix22 &mat) { Matrix22 *m = new Matrix22; for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j) m->x[i][j] = T (mat.x[i][j]); return m; } template class_ > register_Matrix22() { typedef PyImath::StaticFixedArray,T,2,IndexAccessMatrixRow,T,2> > Matrix22_helper; MatrixRow::register_class(); class_ > matrix22_class(Matrix22Name::value, Matrix22Name::value,init >("copy construction")); matrix22_class .def(init<>("initialize to identity")) .def(init("initialize all entries to a single value")) .def(init("make from components")) .def("__init__", make_constructor(Matrix2_tuple_constructor)) .def("__init__", make_constructor(Matrix2_matrix_constructor)) .def("__init__", make_constructor(Matrix2_matrix_constructor)) //.def_readwrite("x00", &Matrix22::x[0][0]) //.def_readwrite("x01", &Matrix22::x[0][1]) //.def_readwrite("x02", &Matrix22::x[0][2]) //.def_readwrite("x10", &Matrix22::x[1][0]) //.def_readwrite("x11", &Matrix22::x[1][1]) //.def_readwrite("x12", &Matrix22::x[1][2]) //.def_readwrite("x20", &Matrix22::x[2][0]) //.def_readwrite("x21", &Matrix22::x[2][1]) //.def_readwrite("x22", &Matrix22::x[2][2]) .def("baseTypeEpsilon", &Matrix22::baseTypeEpsilon,"baseTypeEpsilon() epsilon value of the base type of the vector") .staticmethod("baseTypeEpsilon") .def("baseTypeMax", &Matrix22::baseTypeMax,"baseTypeMax() max value of the base type of the vector") .staticmethod("baseTypeMax") .def("baseTypeLowest", &Matrix22::baseTypeLowest,"baseTypeLowest() largest negative value of the base type of the vector") .staticmethod("baseTypeLowest") .def("baseTypeSmallest", &Matrix22::baseTypeSmallest,"baseTypeSmallest() smallest value of the base type of the vector") .staticmethod("baseTypeSmallest") .def("equalWithAbsError", &Matrix22::equalWithAbsError,"m1.equalWithAbsError(m2,e) true if the elements " "of v1 and v2 are the same with an absolute error of no more than e, " "i.e., abs(m1[i] - m2[i]) <= e") .def("equalWithRelError", &Matrix22::equalWithRelError,"m1.equalWithAbsError(m2,e) true if the elements " "of m1 and m2 are the same with an absolute error of no more than e, " "i.e., abs(m1[i] - m2[i]) <= e * abs(m1[i])") // need a different version for matrix data access .def("__len__", Matrix22_helper::len) .def("__getitem__", Matrix22_helper::getitem) //.def("__setitem__", Matrix22_helper::setitem) .def("makeIdentity",&Matrix22::makeIdentity,"makeIdentity() make this matrix the identity matrix") .def("transpose",&Matrix22::transpose,return_internal_reference<>(),"transpose() transpose this matrix") .def("transposed",&Matrix22::transposed,"transposed() return a transposed copy of this matrix") .def("invert",&invert22,invert22_overloads("invert() invert this matrix")[return_internal_reference<>()]) .def("inverse",&inverse22,inverse22_overloads("inverse() return an inverted copy of this matrix")) .def("determinant",&Matrix22::determinant,"determinant() return the determinant of this matrix") .def(self == self) // NOSONAR - suppress SonarCloud bug report. .def(self != self) // NOSONAR - suppress SonarCloud bug report. .def("__iadd__", &iadd22,return_internal_reference<>()) .def("__iadd__", &iadd22,return_internal_reference<>()) .def("__iadd__", &iadd22T,return_internal_reference<>()) .def("__add__", &add22) .def("__isub__", &isub22,return_internal_reference<>()) .def("__isub__", &isub22,return_internal_reference<>()) .def("__isub__", &isub22T,return_internal_reference<>()) .def("__sub__", &sub22) .def("negate",&negate22,return_internal_reference<>(),"negate() negate all entries in this matrix") .def("__neg__", &neg22) .def("__imul__", &imul22T,return_internal_reference<>()) .def("__mul__", &mul22T) .def("__rmul__", &rmul22T) .def("__idiv__", &idiv22T,return_internal_reference<>()) .def("__itruediv__", &idiv22T,return_internal_reference<>()) .def("__div__", &div22T) .def("__truediv__", &div22T) .def("__add__", &add22T) .def("__radd__", &add22T) .def("__sub__", &subtractTL22) .def("__rsub__", &subtractTR22) .def("__mul__", &mul22) .def("__mul__", &mul22) .def("__rmul__", &rmul22) .def("__rmul__", &rmul22) .def("__imul__", &imul22,return_internal_reference<>()) .def("__imul__", &imul22,return_internal_reference<>()) .def("__lt__", &lessThan22) .def("__le__", &lessThanEqual22) .def("__gt__", &greaterThan22) .def("__ge__", &greaterThanEqual22) //.def(self_ns::str(self)) .def("__str__",&Matrix22_str) .def("__repr__",&Matrix22_repr) .def("extractEuler", &extractEuler, "M.extractEuler(r) -- extracts the " "rotation component of M into r. " "Assumes that M contains no shear or " "non-uniform scaling; results are " "meaningless if it does.") .def("multDirMatrix", &multDirMatrix22, "mult matrix") .def("multDirMatrix", &multDirMatrix22_return_value, "mult matrix") .def("multDirMatrix", &multDirMatrix22_array, "mult matrix") .def("multDirMatrix", &multDirMatrix22, "mult matrix") .def("multDirMatrix", &multDirMatrix22_return_value, "mult matrix") .def("multDirMatrix", &multDirMatrix22_array, "mult matrix") .def("rotate", &rotate22, return_internal_reference<>(),"rotate matrix") .def("scale", &scaleSc22, return_internal_reference<>(),"scale matrix") .def("scale", &scaleV22, return_internal_reference<>(),"scale matrix") .def("scale", &scale22Tuple, return_internal_reference<>(),"scale matrix") .def("setRotation", &setRotation22, return_internal_reference<>(),"setRotation()") .def("setScale", &setScaleSc22, return_internal_reference<>(),"setScale()") .def("setScale", &setScaleV22, return_internal_reference<>(),"setScale()") .def("setScale", &setScale22Tuple, return_internal_reference<>(),"setScale()") .def("setValue", &setValue22, "setValue()") ; decoratecopy(matrix22_class); return matrix22_class; /* const Matrix22 & operator = (const Matrix22 &v); const Matrix22 & operator = (T a); T * getValue (); const T * getValue () const; template void getValue (Matrix22 &v) const; template Matrix22 & setValue (const Matrix22 &v); template Matrix22 & setTheMatrix (const Matrix22 &v); template void multVecMatrix(const Vec2 &src, Vec2 &dst) const; template void multDirMatrix(const Vec2 &src, Vec2 &dst) const; template const Matrix22 & setRotation (S r); template const Matrix22 & rotate (S r); const Matrix22 & setScale (T s); template const Matrix22 & setScale (const Vec2 &s); template const Matrix22 & scale (const Vec2 &s); template const Matrix22 & setTranslation (const Vec2 &t); Vec2 translation () const; template const Matrix22 & translate (const Vec2 &t); template const Matrix22 & setShear (const S &h); template const Matrix22 & setShear (const Vec2 &h); template const Matrix22 & shear (const S &xy); template const Matrix22 & shear (const Vec2 &h); */ } template static void setM22ArrayItem(FixedArray > &ma, Py_ssize_t index, const IMATH_NAMESPACE::Matrix22 &m) { ma[ma.canonical_index(index)] = m; } template static FixedArray > inverse22_array(FixedArray >&ma, bool singExc = true) { MATH_EXC_ON; size_t len = ma.len(); FixedArray > dst(len); for (size_t i=0; i static FixedArray > & invert22_array(FixedArray >&ma, bool singExc = true) { MATH_EXC_ON; size_t len = ma.len(); for (size_t i=0; i class_ > > register_M22Array() { class_ > > matrixArray_class = FixedArray >::register_("Fixed length array of IMATH_NAMESPACE::Matrix22"); matrixArray_class .def("__setitem__", &setM22ArrayItem) .def("inverse",&inverse22_array,inverse22_array_overloads("inverse() return an inverted copy of this matrix")) .def("invert",&invert22_array,invert22_array_overloads("invert() invert these matricies")[return_internal_reference<>()]) ; add_comparison_functions(matrixArray_class); return matrixArray_class; } template PYIMATH_EXPORT class_ > register_Matrix22(); template PYIMATH_EXPORT class_ > register_Matrix22(); template PYIMATH_EXPORT class_ > > register_M22Array(); template PYIMATH_EXPORT class_ > > register_M22Array(); template<> PYIMATH_EXPORT IMATH_NAMESPACE::Matrix22 FixedArrayDefaultValue >::value() { return IMATH_NAMESPACE::Matrix22(); } template<> PYIMATH_EXPORT IMATH_NAMESPACE::Matrix22 FixedArrayDefaultValue >::value() { return IMATH_NAMESPACE::Matrix22(); } }