/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2007-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 _PyImathOperators_h_ #define _PyImathOperators_h_ #include #include namespace PyImath { template struct op_add { static inline Ret apply(const T1 &a, const T2 &b) { return a+b; } }; template struct op_sub { static inline Ret apply(const T1 &a, const T2 &b) { return a-b; } }; template struct op_rsub { static inline Ret apply(const T1 &a, const T2 &b) { return b-a; } }; template struct op_mul { static inline Ret apply(const T1 &a, const T2 &b) { return a*b; } }; template struct op_div { static inline Ret apply(const T1 &a, const T2 &b) { return a/b; } }; template struct op_mod { static inline Ret apply(const T1 &a, const T2 &b) { return a%b; } }; template struct op_pow { static inline Ret apply(const T1 &a, const T2 &b) { return std::pow(a,b); } }; template struct op_rpow { static inline Ret apply(const T1 &a, const T2 &b) { return std::pow(b,a); } }; template struct op_neg { static inline Ret apply(const T1 &a) { return -a; } }; template struct op_abs { static inline Ret apply(const T1 &a) { return std::abs(a); } }; template struct op_inverse { static inline Ret apply(const T1 &a) { return ~a; } }; template struct op_lshift { static inline Ret apply(const T1 &a, const T2 &b) { return a << b; } }; template struct op_rshift { static inline Ret apply(const T1 &a, const T2 &b) { return a >> b; } }; template struct op_bitand { static inline Ret apply(const T1 &a, const T2 &b) { return a & b; } }; template struct op_xor { static inline Ret apply(const T1 &a, const T2 &b) { return a ^ b; } }; template struct op_bitor { static inline Ret apply(const T1 &a, const T2 &b) { return a | b; } }; template struct op_iadd { static inline void apply(T1 &a, const T2 &b) { a += b; } }; template struct op_isub { static inline void apply(T1 &a, const T2 &b) { a -= b; } }; template struct op_imul { static inline void apply(T1 &a, const T2 &b) { a *= b; } }; template struct op_idiv { static inline void apply(T1 &a, const T2 &b) { a /= b; } }; template struct op_imod { static inline void apply(T1 &a, const T2 &b) { a %= b; } }; template struct op_ipow { static inline void apply(T1 &a, const T2 &b) { a = std::pow(a,b); } }; template struct op_ilshift { static inline void apply(T1 &a, const T2 &b) { a <<= b; } }; template struct op_irshift { static inline void apply(T1 &a, const T2 &b) { a >>= b; } }; template struct op_ixor { static inline void apply(T1 &a, const T2 &b) { a ^= b; } }; template struct op_ibitand { static inline void apply(T1 &a, const T2 &b) { a &= b; } }; template struct op_ibitor { static inline void apply(T1 &a, const T2 &b) { a |= b; } }; // the logical function return values default to 'int' for use // as mask arrays. template struct op_lt { static inline Ret apply(const T1 &a, const T2 &b) { return a < b; } }; template struct op_gt { static inline Ret apply(const T1 &a, const T2 &b) { return a > b; } }; template struct op_le { static inline Ret apply(const T1 &a, const T2 &b) { return a <= b; } }; template struct op_ge { static inline Ret apply(const T1 &a, const T2 &b) { return a >= b; } }; template struct op_eq { static inline Ret apply(const T1 &a, const T2 &b) { return a == b; } }; template struct op_ne { static inline Ret apply(const T1 &a, const T2 &b) { return a != b; } }; template static T fa_reduce(const FixedArray &a) { T tmp(T(0)); // should use default construction but V3f doens't initialize size_t len = a.len(); for (size_t i=0; i < len; ++i) tmp += a[i]; return tmp; } template static T fa_min(const FixedArray &a) { T tmp(T(0)); size_t len = a.len(); if (len > 0) tmp = a[0]; for (size_t i=1; i < len; ++i) if (a[i] < tmp) tmp = a[i]; return tmp; } template static T fa_max(const FixedArray &a) { T tmp(T(0)); size_t len = a.len(); if (len > 0) tmp = a[0]; for (size_t i=1; i < len; ++i) if (a[i] > tmp) tmp = a[i]; return tmp; } template static void add_arithmetic_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; using boost::mpl::false_; generate_member_bindings, true_ >(c,"__add__", "self+x", boost::python::args("x")); generate_member_bindings, false_>(c,"__radd__","x+self", boost::python::args("x")); generate_member_bindings, true_ >(c,"__sub__", "self-x", boost::python::args("x")); generate_member_bindings,false_>(c,"__rsub__","x-self", boost::python::args("x")); generate_member_bindings, true_ >(c,"__mul__", "self*x", boost::python::args("x")); generate_member_bindings, false_>(c,"__rmul__","x*self", boost::python::args("x")); generate_member_bindings, true_ >(c,"__div__", "self/x", boost::python::args("x")); generate_member_bindings, true_ >(c,"__truediv__", "self/x", boost::python::args("x")); generate_member_bindings >(c,"__neg__", "-x"); generate_member_bindings,true_ >(c,"__iadd__","self+=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__isub__","self-=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__imul__","self*=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__idiv__","self/=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__itruediv__","self/=x",boost::python::args("x")); c.def("reduce",&fa_reduce); } template static void add_reduction_functions(boost::python::class_ > &c) { c.def("min",&fa_min); c.def("max",&fa_max); } template static void add_pow_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; using boost::mpl::false_; generate_member_bindings, true_ >(c,"__pow__", "self**x", boost::python::args("x")); generate_member_bindings,false_>(c,"__rpow__","x**self", boost::python::args("x")); generate_member_bindings,true_ >(c,"__ipow__","x**=self",boost::python::args("x")); } template static void add_mod_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__mod__", "self%x", boost::python::args("x")); generate_member_bindings,true_>(c,"__imod__","self%=x",boost::python::args("x")); } template static void add_shift_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__lshift__", "self<,true_>(c,"__ilshift__","self<<=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__rshift__", "self>>x", boost::python::args("x")); generate_member_bindings,true_>(c,"__irshift__","self>>=x",boost::python::args("x")); } template static void add_bitwise_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__and__", "self&x", boost::python::args("x")); generate_member_bindings,true_>(c,"__iand__","self&=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__or__", "self|x", boost::python::args("x")); generate_member_bindings, true_>(c,"__ior__", "self|=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__xor__", "self^x", boost::python::args("x")); generate_member_bindings, true_>(c,"__ixor__","self^=x",boost::python::args("x")); } template static void add_comparison_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__eq__","self==x",boost::python::args("x")); generate_member_bindings, true_>(c,"__ne__","self!=x",boost::python::args("x")); } template static void add_ordered_comparison_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__lt__","self, true_>(c,"__le__","self<=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__gt__","self>x", boost::python::args("x")); generate_member_bindings, true_>(c,"__ge__","self>=x",boost::python::args("x")); } template static void add_explicit_construction_from_type(boost::python::class_ > &c) { using namespace boost::python; c.def(init >("copy contents of other array into this one")); } } #endif // _PyImathOperators_h_