X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fcml%2Fquaternion%2Fquaternion_comparison.h;fp=src%2Fcml%2Fquaternion%2Fquaternion_comparison.h;h=c1e6d0c5bfefca3b1cfdf03ec3e898839a64a822;hp=0000000000000000000000000000000000000000;hb=0fffd0097d7b496454413e57b398c903ecc252e4;hpb=79becf045222f385da5a1b9eb79081f6f5266c86 diff --git a/src/cml/quaternion/quaternion_comparison.h b/src/cml/quaternion/quaternion_comparison.h new file mode 100644 index 0000000..c1e6d0c --- /dev/null +++ b/src/cml/quaternion/quaternion_comparison.h @@ -0,0 +1,216 @@ +/* -*- C++ -*- ------------------------------------------------------------ + +Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/ + +The Configurable Math Library (CML) is distributed under the terms of the +Boost Software License, v1.0 (see cml/LICENSE for details). + + *-----------------------------------------------------------------------*/ +/** @file + * @brief + */ + +#ifndef quaternion_comparison_h +#define quaternion_comparison_h + +#include +#include + +/* This is used below to create a more meaningful compile-time error when + * quaternion_comparison is not provided with quaternion or QuaternionExpr arguments: + */ +struct quaternion_comparison_expects_quaternion_args_error; + +#define CML_QUAT_QUAT_ORDER(_order_, _op_, _OpT_) \ +template< \ + typename E1, class AT1, typename E2, class AT2, class O, class C > \ +inline bool \ +_op_ ( \ + const quaternion& left, \ + const quaternion& right) \ +{ \ + return detail::quaternion_##_order_ (left, right, _OpT_ ()); \ +} + +#define CML_QUAT_QUATXPR_ORDER(_order_, _op_, _OpT_) \ +template \ +inline bool \ +_op_ ( \ + const quaternion& left, \ + QUATXPR_ARG_TYPE right) \ +{ \ + return detail::quaternion_##_order_ (left, right, \ + _OpT_ ()); \ +} + +#define CML_QUATXPR_QUAT_ORDER(_order_, _op_, _OpT_) \ +template \ +inline bool \ +_op_ ( \ + QUATXPR_ARG_TYPE left, \ + const quaternion& right) \ +{ \ + return detail::quaternion_##_order_ (left, right, \ + _OpT_ ()); \ +} + +#define CML_QUATXPR_QUATXPR_ORDER(_order_, _op_, _OpT_) \ +template \ +inline bool \ +_op_ ( \ + QUATXPR_ARG_TYPE_N(1) left, \ + QUATXPR_ARG_TYPE_N(2) right) \ +{ \ + return detail::quaternion_##_order_ (left, right, \ + _OpT_ < \ + typename XprT1::value_type, \ + typename XprT2::value_type>()); \ +} + + +namespace cml { +namespace detail { + +/** Quaternion strict weak ordering relationship. + * + * OpT must implement a strict weak order on the quaternion element type. + * operator< and operator> on integer and floating-point types are + * examples. + */ +template +inline bool +quaternion_weak_order(const LeftT& left, const RightT& right, OpT) +{ + /* Shorthand: */ + typedef et::ExprTraits left_traits; + typedef et::ExprTraits right_traits; + + /* quaternion_comparison() requires quaternion expressions: */ + CML_STATIC_REQUIRE_M( + (et::QuaternionExpressions::is_true), + quaternion_comparison_expects_quaternion_args_error); + /* Note: parens are required here so that the preprocessor ignores the + * commas: + */ + + typedef typename et::QuaternionPromote< + typename left_traits::result_type, + typename right_traits::result_type + >::type result_type; + + for(ssize_t i = 0; i < result_type::array_size; ++ i) { + + if(OpT().apply( + left_traits().get(left,i), + right_traits().get(right,i) + )) + { + /* If weak order (a < b) is satisfied, return true: */ + return true; + } else if(OpT().apply( + right_traits().get(right,i), + left_traits().get(left,i) + )) + { + /* If !(b < a), then return false: */ + return false; + } else { + + /* Have !(a < b) && !(b < a) <=> (a >= b && b >= a) <=> (a == b). + * so need to test next element: + */ + continue; + } + } + /* XXX Can this be unrolled in any reasonable way? */ + + /* If we get here, then left == right: */ + return false; +} + +/** Quaternion total order relationship. + * + * OpT must implement a total order on the quaternion element type. operator<= + * and operator>= on integer and floating-point types are examples. + */ +template +inline bool +quaternion_total_order(const LeftT& left, const RightT& right, OpT) +{ + /* Shorthand: */ + typedef et::ExprTraits left_traits; + typedef et::ExprTraits right_traits; + + /* quaternion_comparison() requires quaternion expressions: */ + CML_STATIC_REQUIRE_M( + (et::QuaternionExpressions::is_true), + quaternion_comparison_expects_quaternion_args_error); + /* Note: parens are required here so that the preprocessor ignores the + * commas: + */ + + typedef typename et::QuaternionPromote< + typename left_traits::result_type, + typename right_traits::result_type + >::type result_type; + + for(ssize_t i = 0; i < result_type::array_size; ++ i) { + + /* Test total order: */ + if(OpT().apply( + left_traits().get(left,i), + right_traits().get(right,i) + )) + { + /* Automatically true if weak order (a <= b) && !(b <= a) <=> + * (a <= b) && (b > a) <=> (a < b) is satisfied: + */ + if(!OpT().apply( + right_traits().get(right,i), + left_traits().get(left,i) + )) + return true; + + /* Otherwise, have equality (a <= b) && (b <= a), so continue + * to next element: + */ + else + continue; + + } else { + + /* Total order isn't satisfied (a > b), so return false: */ + return false; + } + } + /* XXX Can this be unrolled in any reasonable way? */ + + /* Total (==) or weak (<) order was satisfied, so return true: */ + return true; +} + +} + +/* XXX There is a better way to handle these with operator traits... */ + +CML_QUAT_QUAT_ORDER( total_order, operator==, et::OpEqual) +CML_QUATXPR_QUAT_ORDER( total_order, operator==, et::OpEqual) +CML_QUAT_QUATXPR_ORDER( total_order, operator==, et::OpEqual) +CML_QUATXPR_QUATXPR_ORDER( total_order, operator==, et::OpEqual) + +CML_QUAT_QUAT_ORDER( weak_order, operator!=, et::OpNotEqual) +CML_QUATXPR_QUAT_ORDER( weak_order, operator!=, et::OpNotEqual) +CML_QUAT_QUATXPR_ORDER( weak_order, operator!=, et::OpNotEqual) +CML_QUATXPR_QUATXPR_ORDER( weak_order, operator!=, et::OpNotEqual) + +CML_QUAT_QUAT_ORDER( weak_order, operator<, et::OpLess) +CML_QUATXPR_QUAT_ORDER( weak_order, operator<, et::OpLess) +CML_QUAT_QUATXPR_ORDER( weak_order, operator<, et::OpLess) +CML_QUATXPR_QUATXPR_ORDER( weak_order, operator<, et::OpLess) + +} // namespace cml + +#endif + +// ------------------------------------------------------------------------- +// vim:ft=cpp