1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
13 #ifndef quaternion_comparison_h
14 #define quaternion_comparison_h
16 #include <cml/core/cml_assert.h>
17 #include <cml/et/scalar_ops.h>
19 /* This is used below to create a more meaningful compile-time error when
20 * quaternion_comparison is not provided with quaternion or QuaternionExpr arguments:
22 struct quaternion_comparison_expects_quaternion_args_error
;
24 #define CML_QUAT_QUAT_ORDER(_order_, _op_, _OpT_) \
26 typename E1, class AT1, typename E2, class AT2, class O, class C > \
29 const quaternion<E1,AT1,O,C>& left, \
30 const quaternion<E2,AT2,O,C>& right) \
32 return detail::quaternion_##_order_ (left, right, _OpT_ <E1,E2>()); \
35 #define CML_QUAT_QUATXPR_ORDER(_order_, _op_, _OpT_) \
36 template<typename E, class AT, class O, class C, class XprT> \
39 const quaternion<E,AT,O,C>& left, \
40 QUATXPR_ARG_TYPE right) \
42 return detail::quaternion_##_order_ (left, right, \
43 _OpT_ <E, typename XprT::value_type>()); \
46 #define CML_QUATXPR_QUAT_ORDER(_order_, _op_, _OpT_) \
47 template<class XprT, typename E, class AT, class O, class C > \
50 QUATXPR_ARG_TYPE left, \
51 const quaternion<E,AT,O,C>& right) \
53 return detail::quaternion_##_order_ (left, right, \
54 _OpT_ <typename XprT::value_type, E>()); \
57 #define CML_QUATXPR_QUATXPR_ORDER(_order_, _op_, _OpT_) \
58 template<class XprT1, class XprT2> \
61 QUATXPR_ARG_TYPE_N(1) left, \
62 QUATXPR_ARG_TYPE_N(2) right) \
64 return detail::quaternion_##_order_ (left, right, \
66 typename XprT1::value_type, \
67 typename XprT2::value_type>()); \
74 /** Quaternion strict weak ordering relationship.
76 * OpT must implement a strict weak order on the quaternion element type.
77 * operator< and operator> on integer and floating-point types are
80 template<typename LeftT
, typename RightT
, typename OpT
>
82 quaternion_weak_order(const LeftT
& left
, const RightT
& right
, OpT
)
85 typedef et::ExprTraits
<LeftT
> left_traits
;
86 typedef et::ExprTraits
<RightT
> right_traits
;
88 /* quaternion_comparison() requires quaternion expressions: */
90 (et::QuaternionExpressions
<LeftT
,RightT
>::is_true
),
91 quaternion_comparison_expects_quaternion_args_error
);
92 /* Note: parens are required here so that the preprocessor ignores the
96 typedef typename
et::QuaternionPromote
<
97 typename
left_traits::result_type
,
98 typename
right_traits::result_type
101 for(ssize_t i
= 0; i
< result_type::array_size
; ++ i
) {
104 left_traits().get(left
,i
),
105 right_traits().get(right
,i
)
108 /* If weak order (a < b) is satisfied, return true: */
110 } else if(OpT().apply(
111 right_traits().get(right
,i
),
112 left_traits().get(left
,i
)
115 /* If !(b < a), then return false: */
119 /* Have !(a < b) && !(b < a) <=> (a >= b && b >= a) <=> (a == b).
120 * so need to test next element:
125 /* XXX Can this be unrolled in any reasonable way? */
127 /* If we get here, then left == right: */
131 /** Quaternion total order relationship.
133 * OpT must implement a total order on the quaternion element type. operator<=
134 * and operator>= on integer and floating-point types are examples.
136 template<typename LeftT
, typename RightT
, typename OpT
>
138 quaternion_total_order(const LeftT
& left
, const RightT
& right
, OpT
)
141 typedef et::ExprTraits
<LeftT
> left_traits
;
142 typedef et::ExprTraits
<RightT
> right_traits
;
144 /* quaternion_comparison() requires quaternion expressions: */
145 CML_STATIC_REQUIRE_M(
146 (et::QuaternionExpressions
<LeftT
,RightT
>::is_true
),
147 quaternion_comparison_expects_quaternion_args_error
);
148 /* Note: parens are required here so that the preprocessor ignores the
152 typedef typename
et::QuaternionPromote
<
153 typename
left_traits::result_type
,
154 typename
right_traits::result_type
157 for(ssize_t i
= 0; i
< result_type::array_size
; ++ i
) {
159 /* Test total order: */
161 left_traits().get(left
,i
),
162 right_traits().get(right
,i
)
165 /* Automatically true if weak order (a <= b) && !(b <= a) <=>
166 * (a <= b) && (b > a) <=> (a < b) is satisfied:
169 right_traits().get(right
,i
),
170 left_traits().get(left
,i
)
174 /* Otherwise, have equality (a <= b) && (b <= a), so continue
182 /* Total order isn't satisfied (a > b), so return false: */
186 /* XXX Can this be unrolled in any reasonable way? */
188 /* Total (==) or weak (<) order was satisfied, so return true: */
194 /* XXX There is a better way to handle these with operator traits... */
196 CML_QUAT_QUAT_ORDER( total_order
, operator==, et::OpEqual
)
197 CML_QUATXPR_QUAT_ORDER( total_order
, operator==, et::OpEqual
)
198 CML_QUAT_QUATXPR_ORDER( total_order
, operator==, et::OpEqual
)
199 CML_QUATXPR_QUATXPR_ORDER( total_order
, operator==, et::OpEqual
)
201 CML_QUAT_QUAT_ORDER( weak_order
, operator!=, et::OpNotEqual
)
202 CML_QUATXPR_QUAT_ORDER( weak_order
, operator!=, et::OpNotEqual
)
203 CML_QUAT_QUATXPR_ORDER( weak_order
, operator!=, et::OpNotEqual
)
204 CML_QUATXPR_QUATXPR_ORDER( weak_order
, operator!=, et::OpNotEqual
)
206 CML_QUAT_QUAT_ORDER( weak_order
, operator<, et::OpLess
)
207 CML_QUATXPR_QUAT_ORDER( weak_order
, operator<, et::OpLess
)
208 CML_QUAT_QUATXPR_ORDER( weak_order
, operator<, et::OpLess
)
209 CML_QUATXPR_QUATXPR_ORDER( weak_order
, operator<, et::OpLess
)
215 // -------------------------------------------------------------------------