/////////////////////////////////////////////////////////////////////////// // // 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. // /////////////////////////////////////////////////////////////////////////// #ifndef _PyImathVec2Impl_h_ #define _PyImathVec2Impl_h_ // // This .C file was turned into a header file so that instantiations // of the various V2* types can be spread across multiple files in // order to work around MSVC limitations. // #include #include "PyImathDecorators.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace PyImath { using namespace boost::python; using namespace IMATH_NAMESPACE; template struct Vec2Name { static const char *value; }; // create a new default constructor that initializes Vec2 to zero. template static Vec2 * Vec2_construct_default() { return new Vec2(T(0),T(0)); } template static Vec2 * Vec2_tuple_constructor(const BoostPyType &t) { if(t.attr("__len__")() == 1) return new Vec2(extract(t[0]), extract(t[0])); else if(t.attr("__len__")() == 2) return new Vec2(extract(t[0]), extract(t[1])); else THROW(IEX_NAMESPACE::LogicExc, "Vec2 constructor expects tuple of length 1 or 2"); } template static Vec2 * Vec2_object_constructor1(const object &obj) { Vec2 w; extract > e1(obj); extract > e2(obj); extract > e3(obj); extract e4(obj); extract e5(obj); extract e6(obj); if(e1.check()){ w = e1(); } else if(e2.check()) { w = e2(); } else if(e3.check()) { w = e3(); } else if(e4.check()) { tuple t = e4(); if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 2"); } else if(e5.check()) { T a = e5(); w.setValue(a, a); } else if(e6.check()) { list l = e6(); if(l.attr("__len__")() == 2) { w.x = extract(l[0]); w.y = extract(l[1]); } else THROW(IEX_NAMESPACE::LogicExc, "list must have length of 2"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to Vec2 constructor"); Vec2 *v = new Vec2; *v = w; return v; } template static Vec2 * Vec2_object_constructor2(const object &obj1, const object &obj2) { extract e1(obj1); extract e2(obj2); Vec2 *v = new Vec2; if(e1.check()) { v->x = boost::numeric_cast(e1());} else { THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to Vec2 constructor"); } if(e2.check()) { v->y = boost::numeric_cast(e2());} else { THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to Vec2 constructor"); } return v; } // Implementations of str and repr are same here, // but we'll specialize repr for float and double to make them exact. template static std::string Vec2_str(const Vec2 &v) { std::stringstream stream; stream << Vec2Name::value << "(" << v.x << ", " << v.y << ")"; return stream.str(); } template static std::string Vec2_repr(const Vec2 &v) { std::stringstream stream; stream << Vec2Name::value << "(" << v.x << ", " << v.y << ")"; return stream.str(); } template static T Vec2_cross(const IMATH_NAMESPACE::Vec2 &v, const IMATH_NAMESPACE::Vec2 &other) { MATH_EXC_ON; return v.cross(other); } template static FixedArray Vec2_cross_Vec2Array(const IMATH_NAMESPACE::Vec2 &va, const FixedArray > &vb) { PY_IMATH_LEAVE_PYTHON; size_t len = vb.len(); FixedArray f(len); for (size_t i = 0; i < len; ++i) f[i] = va.cross(vb[i]); return f; } template static T Vec2_dot(const IMATH_NAMESPACE::Vec2 &v, const IMATH_NAMESPACE::Vec2 &other) { MATH_EXC_ON; return v.dot(other); } template static FixedArray Vec2_dot_Vec2Array(const IMATH_NAMESPACE::Vec2 &va, const FixedArray > &vb) { PY_IMATH_LEAVE_PYTHON; size_t len = vb.len(); FixedArray f(len); for (size_t i = 0; i < len; ++i) f[i] = va.dot(vb[i]); return f; } template static T Vec2_length(const IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.length(); } template static T Vec2_length2(const IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.length2(); } template static const Vec2 & Vec2_normalize(IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.normalize(); } template static const Vec2 & Vec2_normalizeExc(IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.normalizeExc(); } template static const Vec2 & Vec2_normalizeNonNull(IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.normalizeNonNull(); } template static Vec2 Vec2_normalized(const IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.normalized(); } template static Vec2 Vec2_normalizedExc(const IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.normalizedExc(); } template static Vec2 Vec2_normalizedNonNull(const IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.normalizedNonNull(); } template static Vec2 closestVertex(Vec2 &p, const Vec2 &v0, const Vec2 &v1, const Vec2 &v2) { MATH_EXC_ON; return IMATH_NAMESPACE::closestVertex(v0, v1, v2, p); } template static const Vec2 & Vec2_negate(IMATH_NAMESPACE::Vec2 &v) { MATH_EXC_ON; return v.negate(); } template static Vec2 orthogonal(const Vec2 &v, const Vec2 &v0) { MATH_EXC_ON; return IMATH_NAMESPACE::orthogonal(v, v0); } template static Vec2 project(const Vec2 &v, const Vec2 &v0) { MATH_EXC_ON; return IMATH_NAMESPACE::project(v0, v); } template static Vec2 reflect(const Vec2 &v, const Vec2 &v0) { MATH_EXC_ON; return IMATH_NAMESPACE::reflect(v, v0); } template static void setValue(Vec2 &v, T a, T b) { v.x = a; v.y = b; } template static Vec2 Vec2_add (const Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v + w; } template static Vec2 Vec2_sub (const Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v - w; } template static Vec2 Vec2_neg (const Vec2 &v) { MATH_EXC_ON; return -v; } template static Vec2 Vec2_mul (const Vec2 &v, const Vec2 &w) { MATH_EXC_ON; Vec2 w2 (w); return v * w2; } template static Vec2 Vec2_mulT (const Vec2 &v, T t) { MATH_EXC_ON; return v * t; } template static FixedArray > Vec2_mulTArray (const Vec2 &v, const FixedArray &t) { PY_IMATH_LEAVE_PYTHON; size_t len = t.len(); FixedArray > retval(len); for (size_t i=0; i static FixedArray > Vec2_rmulTArray (const Vec2 &v, const FixedArray &t) { return Vec2_mulTArray(v,t); } template static Vec2 Vec2_div (Vec2 &v, Vec2 &w) { MATH_EXC_ON; return v / w; } template static Vec2 Vec2_rmulT (Vec2 &v, T t) { MATH_EXC_ON; return t * v; } template static const Vec2 & Vec2_imulV(Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v *= w; } template static const Vec2 & Vec2_imulT(IMATH_NAMESPACE::Vec2 &v, T t) { MATH_EXC_ON; return v *= t; } template static Vec2 Vec2_mulM33 (Vec2 &v, const Matrix33 &m) { MATH_EXC_ON; return v * m; } template static const Vec2 & Vec2_idivObj(IMATH_NAMESPACE::Vec2 &v, const object &o) { MATH_EXC_ON; Vec2 v2; if (PyImath::V2::convert (o.ptr(), &v2)) { return v /= v2; } else { extract e(o); if (e.check()) return v /= e(); else THROW (IEX_NAMESPACE::ArgExc, "V2 division expects an argument" "convertible to a V2"); } } template static Vec2 Vec2_subT(const Vec2 &v, T a) { MATH_EXC_ON; Vec2 w; w.setValue(v.x - a, v.y - a); return w; } template static Vec2 Vec2_subTuple(const Vec2 &v, const BoostPyType &t) { MATH_EXC_ON; Vec2 w; if(t.attr("__len__")() == 2) { w.x = v.x - extract(t[0]); w.y = v.y - extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 2"); return w; } template static Vec2 Vec2_rsubT(const Vec2 &v, T a) { MATH_EXC_ON; Vec2 w; w.setValue(a - v.x, a - v.y); return w; } template static Vec2 Vec2_rsubTuple(const Vec2 &v, const BoostPyType &t) { MATH_EXC_ON; Vec2 w; if(t.attr("__len__")() == 2) { w.x = extract(t[0]) - v.x; w.y = extract(t[1]) - v.y; } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 2"); return w; } template static Vec2 Vec2_addTuple(const Vec2 &v, const BoostPyType &t) { MATH_EXC_ON; Vec2 w; if(t.attr("__len__")() == 2) { w.x = v.x + extract(t[0]); w.y = v.y + extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 2"); return w; } template static Vec2 Vec2_addT(const Vec2 &v, T a) { MATH_EXC_ON; Vec2 w; w.setValue(v.x + a, v.y + a); return w; } template static Vec2 Vec2_addV(const Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v + w; } template static const Vec2 & Vec2_iaddV(Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v += w; } template static Vec2 Vec2_subV(const Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v - w; } template static const Vec2 & Vec2_isubV(Vec2 &v, const Vec2 &w) { MATH_EXC_ON; return v -= w; } template static Vec2 Vec2_mulTuple(const Vec2 &v, BoostPyType t) { MATH_EXC_ON; Vec2 w; if(t.attr("__len__")() == 1){ w.x = v.x*extract(t[0]); w.y = v.y*extract(t[0]); } else if(t.attr("__len__")() == 2){ w.x = v.x*extract(t[0]); w.y = v.y*extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 1 or 2"); return w; } template static const Vec2 & Vec2_imulM33 (Vec2 &v, const Matrix33 &m) { MATH_EXC_ON; return v *= m; } template static Vec2 Vec2_divTuple(const Vec2 &v, const BoostPyType &t) { if(t.attr("__len__")() == 2) { T x = extract(t[0]); T y = extract(t[1]); if(x != T(0) && y != T(0)) return Vec2(v.x / x, v.y / y); else THROW(IEX_NAMESPACE::MathExc, "Division by zero"); } else THROW(IEX_NAMESPACE::LogicExc, "Vec2 expects tuple of length 2"); } template static Vec2 Vec2_rdivTuple(const Vec2 &v, const BoostPyType &t) { MATH_EXC_ON; Vec2 w; if(t.attr("__len__")() == 2) { T x = extract(t[0]); T y = extract(t[1]); if(v.x != T(0) && v.y != T(0)){ w.setValue(x / v.x, y / v.y); } else THROW(IEX_NAMESPACE::MathExc, "Division by zero"); } else THROW(IEX_NAMESPACE::LogicExc, "tuple must have length of 2"); return w; } template static Vec2 Vec2_divT(const Vec2 &v, T a) { MATH_EXC_ON; Vec2 w; if(a != T(0)){ w.setValue(v.x / a, v.y / a); } else THROW(IEX_NAMESPACE::MathExc, "Division by zero"); return w; } template static Vec2 Vec2_rdivT(const Vec2 &v, T a) { MATH_EXC_ON; Vec2 w; if(v.x != T(0) && v.y != T(0)){ w.setValue(a / v.x, a / v.y); } else THROW(IEX_NAMESPACE::MathExc, "Division by zero"); return w; } template static bool lessThan(const Vec2 &v, const object &obj) { extract > e1(obj); extract e2(obj); Vec2 w; if(e1.check()) { w = e1(); } else if(e2.check()) { tuple t = e2(); if(t.attr("__len__")() == 2){ T x = extract(t[0]); T y = extract(t[1]); w.setValue(x,y); } else THROW(IEX_NAMESPACE::LogicExc, "Vec2 expects tuple of length 2"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to operator <"); bool isLessThan = (v.x <= w.x && v.y <= w.y) && v != w; return isLessThan; } template static bool greaterThan(const Vec2 &v, const object &obj) { extract > e1(obj); extract e2(obj); Vec2 w; if(e1.check()) { w = e1(); } else if(e2.check()) { tuple t = e2(); if(t.attr("__len__")() == 2){ T x = extract(t[0]); T y = extract(t[1]); w.setValue(x,y); } else THROW(IEX_NAMESPACE::LogicExc, "Vec2 expects tuple of length 2"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to operator >"); bool isGreaterThan = (v.x >= w.x && v.y >= w.y) && v != w; return isGreaterThan; } template static bool lessThanEqual(const Vec2 &v, const object &obj) { extract > e1(obj); extract e2(obj); Vec2 w; if(e1.check()) { w = e1(); } else if(e2.check()) { tuple t = e2(); if(t.attr("__len__")() == 2){ T x = extract(t[0]); T y = extract(t[1]); w.setValue(x,y); } else THROW(IEX_NAMESPACE::LogicExc, "Vec2 expects tuple of length 2"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to operator <="); bool isLessThanEqual = (v.x <= w.x && v.y <= w.y); return isLessThanEqual; } template static bool greaterThanEqual(const Vec2 &v, const object &obj) { extract > e1(obj); extract e2(obj); Vec2 w; if(e1.check()) { w = e1(); } else if(e2.check()) { tuple t = e2(); if(t.attr("__len__")() == 2){ T x = extract(t[0]); T y = extract(t[1]); w.setValue(x,y); } else THROW(IEX_NAMESPACE::LogicExc, "Vec2 expects tuple of length 2"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to operator >="); bool isGreaterThanEqual = (v.x >= w.x && v.y >= w.y); return isGreaterThanEqual; } template static void setItemTuple(FixedArray > &va, Py_ssize_t index, const BoostPyType &t) { if(t.attr("__len__")() == 2) { Vec2 v; v.x = extract(t[0]); v.y = extract(t[1]); va[va.canonical_index(index)] = v; } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); } template static bool equalWithAbsErrorObj(const Vec2 &v, const object &obj1, const object &obj2) { extract > e1(obj1); extract > e2(obj1); extract > e3(obj1); extract e4(obj1); extract e5(obj2); Vec2 w; if(e1.check()) { w = e1(); } else if(e2.check()) { w = e2(); } else if(e3.check()) { w = e3(); } else if(e4.check()) { tuple t = e4(); if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to equalWithAbsError"); if(e5.check()) { return v.equalWithAbsError(w, e5()); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to equalWithAbsError"); } template static bool equalWithRelErrorObj(const Vec2 &v, const object &obj1, const object &obj2) { extract > e1(obj1); extract > e2(obj1); extract > e3(obj1); extract e4(obj1); extract e5(obj2); Vec2 w; if(e1.check()) { w = e1(); } else if(e2.check()) { w = e2(); } else if(e3.check()) { w = e3(); } else if(e4.check()) { tuple t = e4(); if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to equalWithRelError"); if(e5.check()) { return v.equalWithRelError(w, e5()); } else THROW(IEX_NAMESPACE::LogicExc, "invalid parameters passed to equalWithRelError"); } /* template static bool equalWithAbsErrorTuple(Vec2 &v, const tuple &t, T e) { Vec2 w; if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); return v.equalWithAbsError(w, e); } template static bool equalWithRelErrorTuple(Vec2 &v, const tuple &t, T e) { std::cout << "RelError Tuple called" << std::endl; Vec2 w; if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); return v.equalWithRelError(w, e); } */ template static bool equal(const Vec2 &v, const BoostPyType &t) { Vec2 w; if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); return (v == w); } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); } template static bool notequal(const Vec2 &v, const BoostPyType &t) { Vec2 w; if(t.attr("__len__")() == 2) { w.x = extract(t[0]); w.y = extract(t[1]); return (v != w); } else THROW(IEX_NAMESPACE::LogicExc, "tuple of length 2 expected"); } template class_ > register_Vec2() { typedef PyImath::StaticFixedArray,T,2> Vec2_helper; class_ > vec2_class(Vec2Name::value, Vec2Name::value,init >("copy construction")); vec2_class .def("__init__",make_constructor(Vec2_construct_default),"initialize to (0,0)") .def("__init__",make_constructor(Vec2_object_constructor1)) .def("__init__",make_constructor(Vec2_object_constructor2)) .def_readwrite("x", &Vec2::x) .def_readwrite("y", &Vec2::y) .def("baseTypeEpsilon", &Vec2::baseTypeEpsilon,"baseTypeEpsilon() epsilon value of the base type of the vector") .staticmethod("baseTypeEpsilon") .def("baseTypeMax", &Vec2::baseTypeMax,"baseTypeMax() max value of the base type of the vector") .staticmethod("baseTypeMax") .def("baseTypeMin", &Vec2::baseTypeMin,"baseTypeMin() min value of the base type of the vector") .staticmethod("baseTypeMin") .def("baseTypeSmallest", &Vec2::baseTypeSmallest,"baseTypeSmallest() smallest value of the base type of the vector") .staticmethod("baseTypeSmallest") .def("cross", &Vec2_cross,"v1.cross(v2) right handed cross product") .def("cross", &Vec2_cross_Vec2Array,"v1.cross(v2) right handed array cross product") .def("dimensions", &Vec2::dimensions,"dimensions() number of dimensions in the vector") .staticmethod("dimensions") .def("dot", &Vec2_dot,"v1.dot(v2) inner product of the two vectors") .def("dot", &Vec2_dot_Vec2Array,"v1.dot(v2) array inner product") .def("equalWithAbsError", &Vec2::equalWithAbsError, "v1.equalWithAbsError(v2) true if the elements " "of v1 and v2 are the same with an absolute error of no more than e, " "i.e., abs(v1[i] - v2[i]) <= e") .def("equalWithAbsError", &equalWithAbsErrorObj) .def("equalWithRelError", &Vec2::equalWithRelError, "v1.equalWithAbsError(v2) true if the elements " "of v1 and v2 are the same with an absolute error of no more than e, " "i.e., abs(v1[i] - v2[i]) <= e * abs(v1[i])") .def("equalWithRelError", &equalWithRelErrorObj) .def("length", &Vec2_length,"length() magnitude of the vector") .def("length2", &Vec2_length2,"length2() square magnitude of the vector") .def("normalize", &Vec2_normalize,return_internal_reference<>(), "v.normalize() destructively normalizes v and returns a reference to it") .def("normalizeExc", &Vec2_normalizeExc,return_internal_reference<>(), "v.normalizeExc() destructively normalizes V and returns a reference to it, throwing an exception if length() == 0") .def("normalizeNonNull", &Vec2_normalizeNonNull,return_internal_reference<>(), "v.normalizeNonNull() destructively normalizes V and returns a reference to it, faster if lngth() != 0") .def("normalized", &Vec2_normalized, "v.normalized() returns a normalized copy of v") .def("normalizedExc", &Vec2_normalizedExc, "v.normalizedExc() returns a normalized copy of v, throwing an exception if length() == 0") .def("normalizedNonNull", &Vec2_normalizedNonNull, "v.normalizedNonNull() returns a normalized copy of v, faster if lngth() != 0") .def("__len__", Vec2_helper::len) .def("__getitem__", Vec2_helper::getitem,return_value_policy()) .def("__setitem__", Vec2_helper::setitem) .def("closestVertex", &closestVertex) .def("negate", &Vec2_negate, return_internal_reference<>()) .def("orthogonal", &orthogonal) .def("project", &project) .def("reflect", &reflect) .def("setValue", &setValue) .def("__neg__", &Vec2_neg) .def("__mul__", &Vec2_mul) .def("__mul__", &Vec2_mul) .def("__mul__", &Vec2_mul) .def("__mul__", &Vec2_mulT) .def("__mul__", &Vec2_mulTArray) .def("__mul__", &Vec2_mulTuple) .def("__mul__", &Vec2_mulTuple) .def("__rmul__", &Vec2_rmulT) .def("__rmul__", &Vec2_rmulTArray) .def("__rmul__", &Vec2_mulTuple) .def("__rmul__", &Vec2_mulTuple) .def("__imul__", &Vec2_imulV,return_internal_reference<>()) .def("__imul__", &Vec2_imulV,return_internal_reference<>()) .def("__imul__", &Vec2_imulV,return_internal_reference<>()) .def("__imul__", &Vec2_imulT,return_internal_reference<>()) .def(self * self) .def("__mul__", &Vec2_mulM33) .def("__mul__", &Vec2_mulM33) .def("__imul__", &Vec2_imulM33, return_internal_reference<>()) .def("__imul__", &Vec2_imulM33, return_internal_reference<>()) .def(self / self) .def("__div__", &Vec2_div) .def("__div__", &Vec2_div) .def("__div__", &Vec2_div) .def("__div__", &Vec2_divTuple) .def("__div__", &Vec2_divTuple) .def("__div__", &Vec2_divT) .def("__truediv__", &Vec2_div) .def("__truediv__", &Vec2_div) .def("__truediv__", &Vec2_div) .def("__truediv__", &Vec2_divTuple) .def("__truediv__", &Vec2_divTuple) .def("__truediv__", &Vec2_divT) .def("__rdiv__", &Vec2_rdivTuple) .def("__rdiv__", &Vec2_rdivTuple) .def("__rdiv__", &Vec2_rdivT) .def("__idiv__", &Vec2_idivObj,return_internal_reference<>()) .def("__itruediv__", &Vec2_idivObj,return_internal_reference<>()) .def("__xor__", &Vec2_dot) .def("__mod__", &Vec2_cross) .def(self == self) .def(self != self) .def("__eq__", &equal) .def("__ne__", ¬equal) .def("__add__", &Vec2_add) .def("__add__", &Vec2_addV) .def("__add__", &Vec2_addV) .def("__add__", &Vec2_addV) .def("__add__", &Vec2_addT) .def("__add__", &Vec2_addTuple) .def("__add__", &Vec2_addTuple) .def("__radd__", &Vec2_add) .def("__radd__", &Vec2_addT) .def("__radd__", &Vec2_addTuple) .def("__radd__", &Vec2_addTuple) .def("__iadd__", &Vec2_iaddV, return_internal_reference<>()) .def("__iadd__", &Vec2_iaddV, return_internal_reference<>()) .def("__iadd__", &Vec2_iaddV, return_internal_reference<>()) .def("__sub__", &Vec2_sub) .def("__sub__", &Vec2_subV) .def("__sub__", &Vec2_subV) .def("__sub__", &Vec2_subV) .def("__sub__", &Vec2_subT) .def("__sub__", &Vec2_subTuple) .def("__sub__", &Vec2_subTuple) .def("__rsub__", &Vec2_rsubT) .def("__rsub__", &Vec2_rsubTuple) .def("__rsub__", &Vec2_rsubTuple) .def("__isub__", &Vec2_isubV, return_internal_reference<>()) .def("__isub__", &Vec2_isubV, return_internal_reference<>()) .def("__isub__", &Vec2_isubV, return_internal_reference<>()) .def("__lt__", &lessThan) .def("__gt__", &greaterThan) .def("__le__", &lessThanEqual) .def("__ge__", &greaterThanEqual) //.def(self_ns::str(self)) .def("__str__",&Vec2_str) .def("__repr__",&Vec2_repr) ; decoratecopy(vec2_class); //add_swizzle2_operators(v2f_class); return vec2_class; } // XXX fixme - template this // really this should get generated automatically... template static FixedArray Vec2Array_get(FixedArray > &va) { return FixedArray(&va[0][index],va.len(),2*va.stride()); } template static IMATH_NAMESPACE::Vec2 Vec2Array_min(const FixedArray > &a) { Vec2 tmp(Vec2(0)); size_t len = a.len(); if (len > 0) tmp = a[0]; for (size_t i=1; i < len; ++i) { if (a[i].x < tmp.x) tmp.x = a[i].x; if (a[i].y < tmp.y) tmp.y = a[i].y; } return tmp; } template static IMATH_NAMESPACE::Vec2 Vec2Array_max(const FixedArray > &a) { Vec2 tmp(Vec2(0)); size_t len = a.len(); if (len > 0) tmp = a[0]; for (size_t i=1; i < len; ++i) { if (a[i].x > tmp.x) tmp.x = a[i].x; if (a[i].y > tmp.y) tmp.y = a[i].y; } return tmp; } template static IMATH_NAMESPACE::Box > Vec2Array_bounds(const FixedArray > &a) { Box > tmp; size_t len = a.len(); for (size_t i=0; i < len; ++i) tmp.extendBy(a[i]); return tmp; } template class_ > > register_Vec2Array() { using boost::mpl::true_; using boost::mpl::false_; class_ > > vec2Array_class = FixedArray >::register_("Fixed length array of IMATH_NAMESPACE::Vec2"); vec2Array_class .add_property("x",&Vec2Array_get) .add_property("y",&Vec2Array_get) .def("__setitem__", &setItemTuple) .def("__setitem__", &setItemTuple) .def("min", &Vec2Array_min) .def("max", &Vec2Array_max) .def("bounds", &Vec2Array_bounds) ; add_arithmetic_math_functions(vec2Array_class); add_comparison_functions(vec2Array_class); generate_member_bindings > >(vec2Array_class,"length",""); generate_member_bindings > >(vec2Array_class,"length2",""); generate_member_bindings > >(vec2Array_class,"normalize",""); generate_member_bindings > >(vec2Array_class,"normalized",""); generate_member_bindings, true_>(vec2Array_class,"cross","return the cross product of (self,x)",boost::python::args("x")); generate_member_bindings >,true_>(vec2Array_class,"dot","return the inner product of (self,x)",boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__mul__" ,"self*x", boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__rmul__","x*self", boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__imul__","self*=x",boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__div__" ,"self/x", boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__truediv__" ,"self/x", boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__idiv__","self/=x",boost::python::args("x")); generate_member_bindings,T>, true_>(vec2Array_class,"__itruediv__","self/=x",boost::python::args("x")); decoratecopy(vec2Array_class); return vec2Array_class; } } #endif