/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 1998-2011, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Industrial Light & Magic nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// #include #include #include "PyImathDecorators.h" #include "PyImathExport.h" #include #include #include #include #include #include #include namespace PyImath{ using namespace boost::python; using namespace IMATH_NAMESPACE; template struct ShearName {static const char *value;}; template <> const char *ShearName::value = "Shear6f"; template <> const char *ShearName::value = "Shear6d"; template static std::string Shear_str(const Shear6 &v) { std::stringstream stream; stream << ShearName::value << "(" << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ", " << v[4] << ", " << v[5] << ")"; return stream.str(); } // Non-specialized repr is same as str template static std::string Shear_repr(const Shear6 &v) { return Shear_str(v); } // Specialization for float to full precision template <> std::string Shear_repr(const Shear6 &v) { return (boost::format("%s(%.9g, %.9g, %.9g, %.9g, %.9g, %.9g)") % ShearName::value % v[0] % v[1] % v[2] % v[3] % v[4] % v[5]).str(); } // Specialization for double to full precision template <> std::string Shear_repr(const Shear6 &v) { return (boost::format("%s(%.17g, %.17g, %.17g, %.17g, %.17g, %.17g)") % ShearName::value % v[0] % v[1] % v[2] % v[3] % v[4] % v[5]).str(); } template static Shear6 * shearTupleConstructor(tuple t) { if(t.attr("__len__")() == 3){ return new Shear6(extract(t[0]), extract(t[1]), extract(t[2]), T(0), T(0), T(0)); } else if(t.attr("__len__")() == 6){ return new Shear6(extract(t[0]), extract(t[1]), extract(t[2]), extract(t[3]), extract(t[4]), extract(t[5])); } else THROW(IEX_NAMESPACE::LogicExc, "Shear6 expects tuple of length 3 or 6"); } template static Shear6 * shearConstructor1(T a) { return new Shear6(a, a, a, a, a, a); } template static Shear6 * shearConversionConstructor(const Shear6 &shear) { Shear6 *s = new Shear6; *s = shear; return s; } template static const Shear6 & iadd(Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear += other; } template static Shear6 add(const Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear + other; } template static const Shear6 & isub(Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear -= other; } template static Shear6 sub(const Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear - other; } template static Shear6 neg(const Shear6 &shear) { MATH_EXC_ON; return -shear; } template static const Shear6 & imul(Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear *= other; } template static const Shear6 & imulT(Shear6 &shear, T t) { MATH_EXC_ON; return shear *= t; } template static Shear6 mul(const Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear * other; } template static Shear6 mulT(const Shear6 &shear, T t) { MATH_EXC_ON; return shear * t; } template static const Shear6 & idiv(Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear /= other; } template static const Shear6 & idivT(Shear6 &shear, T t) { MATH_EXC_ON; return shear /= t; } template static Shear6 div(const Shear6 &shear, const Shear6 &other) { MATH_EXC_ON; return shear / other; } template static Shear6 divT(const Shear6 &shear, T t) { MATH_EXC_ON; return shear / t; } template static Shear6 subtract1(Shear6 &v, tuple t) { MATH_EXC_ON; Shear6 w; if(t.attr("__len__")() == 6){ w[0] = v[0] - extract(t[0]); w[1] = v[1] - extract(t[1]); w[2] = v[2] - extract(t[2]); w[3] = v[3] - extract(t[3]); w[4] = v[4] - extract(t[4]); w[5] = v[5] - extract(t[5]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 6"); return w; } template static Shear6 subtract2(Shear6 &v, tuple t) { MATH_EXC_ON; Shear6 w; if(t.attr("__len__")() == 6){ w[0] = extract(t[0]) - v[0]; w[1] = extract(t[1]) - v[1]; w[2] = extract(t[2]) - v[2]; w[3] = extract(t[3]) - v[3]; w[4] = extract(t[4]) - v[4]; w[5] = extract(t[5]) - v[5]; } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 6"); return w; } template static Shear6 subtractT1(Shear6 &v, T a) { MATH_EXC_ON; Shear6 w; w[0] = v[0] - a; w[1] = v[1] - a; w[2] = v[2] - a; w[3] = v[3] - a; w[4] = v[4] - a; w[5] = v[5] - a; return w; } template static Shear6 subtractT2(Shear6 &v, T a) { MATH_EXC_ON; Shear6 w; w[0] = a - v[0]; w[1] = a - v[1]; w[2] = a - v[2]; w[3] = a - v[3]; w[4] = a - v[4]; w[5] = a - v[5]; return w; } template static Shear6 addTuple(Shear6 &v, tuple t) { MATH_EXC_ON; Shear6 w; if(t.attr("__len__")() == 6){ w[0] = v[0] + extract(t[0]); w[1] = v[1] + extract(t[1]); w[2] = v[2] + extract(t[2]); w[3] = v[3] + extract(t[3]); w[4] = v[4] + extract(t[4]); w[5] = v[5] + extract(t[5]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 6"); return w; } template static Shear6 addT(Shear6 &v, T a) { MATH_EXC_ON; Shear6 w; w[0] = v[0] + a; w[1] = v[1] + a; w[2] = v[2] + a; w[3] = v[3] + a; w[4] = v[4] + a; w[5] = v[5] + a; return w; } template static Shear6 multTuple(Shear6 &v, tuple t) { MATH_EXC_ON; Shear6 w; if(t.attr("__len__")() == 6){ w[0] = v[0] * extract(t[0]); w[1] = v[1] * extract(t[1]); w[2] = v[2] * extract(t[2]); w[3] = v[3] * extract(t[3]); w[4] = v[4] * extract(t[4]); w[5] = v[5] * extract(t[5]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 6"); return w; } template static Shear6 rdiv(Shear6 &v, T a) { MATH_EXC_ON; Shear6 w; if(v != Shear6()){ w[0] = a/v[0]; w[1] = a/v[1]; w[2] = a/v[2]; w[3] = a/v[3]; w[4] = a/v[4]; w[5] = a/v[5]; } else THROW(IEX_NAMESPACE::LogicExc, "Division by Zero"); return w; } template static Shear6 divTuple(Shear6 &v, const tuple &t) { MATH_EXC_ON; if(t.attr("__len__")() != 6) THROW(IEX_NAMESPACE::LogicExc, "Shear6 expects tuple of length 6"); Shear6 w; for(int i = 0; i < 6; ++i) { T a = extract(t[i]); if(a != T (0)) w[i] = v[i] / a; else THROW(IEX_NAMESPACE::LogicExc, "Division by Zero"); } return w; } template static Shear6 rdivTuple(Shear6 &v, const tuple &t) { MATH_EXC_ON; if(t.attr("__len__")() != 6) THROW(IEX_NAMESPACE::LogicExc, "Shear6 expects tuple of length 6"); Shear6 w; for(int i = 0; i < 6; ++i) { T a = extract(t[i]); if(v[i] != T (0)) w[i] = a / v[i]; else THROW(IEX_NAMESPACE::LogicExc, "Division by Zero"); } return w; } template static bool lessThan(Shear6 &v, const Shear6 &w) { bool isLessThan = (v[0] <= w[0] && v[1] <= w[1] && v[2] <= w[2] && v[3] <= w[3] && v[4] <= w[4] && v[5] <= w[5]) && v != w; return isLessThan; } template static bool greaterThan(Shear6 &v, const Shear6 &w) { bool isGreaterThan = (v[0] >= w[0] && v[1] >= w[1] && v[2] >= w[2] && v[3] >= w[3] && v[4] >= w[4] && v[5] >= w[5]) && v != w; return isGreaterThan; } template static bool lessThanEqual(Shear6 &v, const Shear6 &w) { bool isLessThanEqual = (v[0] <= w[0] && v[1] <= w[1] && v[2] <= w[2] && v[3] <= w[3] && v[4] <= w[4] && v[5] <= w[5]); return isLessThanEqual; } template static bool greaterThanEqual(Shear6 &v, const Shear6 &w) { bool isGreaterThanEqual = (v[0] >= w[0] && v[1] >= w[1] && v[2] >= w[2] && v[3] >= w[3] && v[4] >= w[4] && v[5] >= w[5]); return isGreaterThanEqual; } template static T getitem(Shear6 &shear, int i) { return shear[i]; } template static void setitem(Shear6 &shear, int i, T a) { if(i < 0 || i > 5) THROW(IEX_NAMESPACE::LogicExc, "Index out of range"); shear[i] = a; } template static int len(Shear6 &shear) { return 6; } template class_ > register_Shear() { const char *name = ShearName::value; void (IMATH_NAMESPACE::Shear6::*setValue1)(T,T,T,T,T,T) = &IMATH_NAMESPACE::Shear6::setValue; void (IMATH_NAMESPACE::Shear6::*setValue2)(const Shear6 &) = &IMATH_NAMESPACE::Shear6::setValue; void (IMATH_NAMESPACE::Shear6::*getValue1)(Shear6 &) const = &IMATH_NAMESPACE::Shear6::getValue; class_ > shear_class(name, name, init >("copy construction")); shear_class .def(init<>("default construction: (0 0 0 0 0 0)")) .def(init("Shear(XY,XZ,YZ) construction: (XY XZ YZ 0 0 0)")) .def(init &>("Shear(v) construction: (v.x v.y v.z 0 0 0)")) .def(init &>("Shear(v) construction: (v.x v.y v.z 0 0 0)")) .def(init &>("Shear(v) construction: (v.x v.y v.z 0 0 0)")) .def(init("Shear(XY, XZ, YZ, YX, ZX, ZY) construction")) .def("__init__", make_constructor(shearConstructor1)) .def("__init__", make_constructor(shearTupleConstructor),"Construction from tuple") .def("__init__", make_constructor(shearConversionConstructor)) .def("__init__", make_constructor(shearConversionConstructor)) .def("__init__", make_constructor(shearConversionConstructor)) .def("__iadd__",&iadd,return_internal_reference<>()) .def("__add__",&add) .def("__isub__",&isub,return_internal_reference<>()) .def("__sub__",&sub) .def("__neg__",&neg) .def("__imul__",&imul,return_internal_reference<>()) .def("__imul__",&imulT,return_internal_reference<>()) .def("__mul__",&mul) .def("__mul__",&mulT) .def("__rmul__",&mulT) .def("__idiv__",&idiv,return_internal_reference<>()) .def("__idiv__",&idivT,return_internal_reference<>()) .def("__itruediv__",&idiv,return_internal_reference<>()) .def("__itruediv__",&idivT,return_internal_reference<>()) .def("__div__",&div) .def("__div__",&divT) .def("__truediv__",&div) .def("__truediv__",&divT) .def(self == self) .def(self != self) .def("__str__",&Shear_str) .def("__repr__",&Shear_repr) .def("setValue", setValue1) .def("setValue", setValue2) .def("getValue", getValue1) .def("negate", &Shear6::negate, return_internal_reference<>()) .def("baseTypeMin", &Shear6::baseTypeMin) .staticmethod("baseTypeMin") .def("baseTypeMax", &Shear6::baseTypeMax) .staticmethod("baseTypeMax") .def("baseTypeSmallest", &Shear6::baseTypeSmallest) .staticmethod("baseTypeSmallest") .def("baseTypeEpsilon", &Shear6::baseTypeEpsilon) .staticmethod("baseTypeEpsilon") .def("equalWithAbsError", &Shear6::equalWithAbsError) .def("equalWithRelError", &Shear6::equalWithRelError) .def("__sub__", &subtract1) .def("__sub__", &subtractT1) .def("__rsub__", &subtract2) .def("__rsub__", &subtractT2) .def("__add__", &addTuple) .def("__add__", &addT) .def("__radd__", &addTuple) .def("__radd__", &addT) .def("__mul__", &multTuple) .def("__rmul__", &multTuple) .def("__div__", &divTuple) .def("__truediv__", &divTuple) .def("__rdiv__", &rdiv) .def("__rdiv__", &rdivTuple) .def("__lt__", &lessThan) .def("__gt__", &greaterThan) .def("__le__", &lessThanEqual) .def("__ge__", &greaterThanEqual) .def("__getitem__", &getitem) .def("__setitem__", &setitem) .def("__len__", &len) ; decoratecopy(shear_class); return shear_class; } template PYIMATH_EXPORT class_ > register_Shear(); template PYIMATH_EXPORT class_ > register_Shear(); }//namespace PyIMath