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 *-----------------------------------------------------------------------*/
12 * Defines promotions for matrices used in matrix/matrix or matrix/scalar
19 #ifndef matrix_promotions_h
20 #define matrix_promotions_h
22 #include <cml/core/cml_meta.h>
23 #include <cml/et/scalar_promotions.h>
24 #include <cml/et/array_promotions.h>
25 #include <cml/fixed.h>
26 #include <cml/dynamic.h>
28 /* This is used below to create a more meaningful compile-time error when
29 * either argument to OuterPromote has the wrong orientation.
31 struct outer_promote_expects_properly_oriented_args_error
;
36 /** Promote two types to a matrixt type. */
37 template<typename LeftT
, typename RightT
> struct MatrixPromote
39 /* Default matrix type promotion template. */
40 template<typename M1
, typename M2
> struct MatrixPromoteHelper
;
42 /** Type promotion for two matrix types.
44 * @note This always uses the basis orientation of the left-hand matrix.
45 * @bug This always uses the basis orientation of the left-hand matrix,
46 * which is not always correct.
48 template<typename E1
, class AT1
, typename L1
, typename BO1
,
49 typename E2
, class AT2
, typename L2
, typename BO2
>
50 struct MatrixPromoteHelper
<
51 cml::matrix
<E1
,AT1
,BO1
,L1
>, cml::matrix
<E2
,AT2
,BO2
,L2
>
54 /* Promote the arrays: */
55 typedef typename ArrayPromote
<
56 typename
cml::matrix
<E1
,AT1
,BO1
,L1
>::array_type
,
57 typename
cml::matrix
<E2
,AT2
,BO2
,L2
>::array_type
58 >::type promoted_array
;
60 /* The deduced matrix result type: */
62 typename
promoted_array::value_type
,
63 typename
promoted_array::generator_type
,
65 typename
promoted_array::layout
68 /* The deduced temporary type: */
69 typedef typename
type::temporary_type temporary_type
;
72 /** Type promotion for a matrix and a scalar. */
73 template<typename E
, class AT
, typename BO
, typename L
, typename S
>
74 struct MatrixPromoteHelper
<cml::matrix
<E
,AT
,BO
,L
>, S
>
76 /* The deduced matrix result type (the array type is the same): */
77 typedef cml::matrix
<typename ScalarPromote
<E
,S
>::type
, AT
, BO
, L
> type
;
79 /* The deduced temporary type: */
80 typedef typename
type::temporary_type temporary_type
;
83 /** Type promotion for a scalar and a matrix. */
84 template<typename S
, typename E
, class AT
, typename BO
, typename L
>
85 struct MatrixPromoteHelper
<S
, cml::matrix
<E
,AT
,BO
,L
> >
87 /* The deduced matrix result type (the array type is the same): */
88 typedef cml::matrix
<typename ScalarPromote
<S
,E
>::type
, AT
, BO
, L
> type
;
90 /* The deduced temporary type: */
91 typedef typename
type::temporary_type temporary_type
;
94 /** Type promotion for outer product. */
95 template<typename E1
, class AT1
, typename E2
, class AT2
>
96 struct MatrixPromoteHelper
< cml::vector
<E1
,AT1
>, cml::vector
<E2
,AT2
> >
98 typedef cml::vector
<E1
,AT1
> left_type
;
99 typedef cml::vector
<E2
,AT2
> right_type
;
100 typedef CML_DEFAULT_BASIS_ORIENTATION basis_orient
;
102 /* Get matrix size: */
104 array_rows
= left_type::array_size
,
105 array_cols
= right_type::array_size
108 /* Deduce the corresponding matrix types for the vectors: */
109 typedef CML_DEFAULT_ARRAY_LAYOUT layout
;
110 typedef typename select_if
<
111 array_rows
== -1, dynamic
<>, fixed
<array_rows
,1>
112 >::result left_storage
;
113 typedef cml::matrix
<E1
,left_storage
,basis_orient
,layout
> left_matrix
;
115 typedef typename select_if
<
116 array_cols
== -1, dynamic
<>, fixed
<1,array_cols
>
117 >::result right_storage
;
118 typedef cml::matrix
<E2
,right_storage
,basis_orient
,layout
> right_matrix
;
120 /* Finally, promote the matrix types to get the result: */
121 typedef typename
et::MatrixPromote
<left_matrix
,right_matrix
>::type type
;
122 typedef typename
type::temporary_type temporary_type
;
125 /** Remove const and & from the to-be-promoted types. */
126 typedef typename remove_const
<
127 typename remove_reference
<LeftT
>::type
>::type LeftBaseT
;
128 typedef typename remove_const
<
129 typename remove_reference
<RightT
>::type
>::type RightBaseT
;
131 typedef typename MatrixPromoteHelper
<LeftBaseT
,RightBaseT
>::type type
;
132 typedef typename
type::temporary_type temporary_type
;
136 * NOTE: MatrixPromote* are somewhat ad hoc, and were added to
137 * simplify the code for matrix slerp/squad/etc.
140 /** Type promotion for two matrix types. */
141 template < class Mat1_T
, class Mat2_T
>
142 struct MatrixPromote2
144 typedef typename MatrixPromote
<
145 typename
Mat1_T::temporary_type
, typename
Mat2_T::temporary_type
146 >::temporary_type temporary_type
;
147 typedef typename
temporary_type::value_type value_type
;
150 /** Type promotion for three matrix types. */
151 template < class Mat1_T
, class Mat2_T
, class Mat3_T
>
152 struct MatrixPromote3
154 typedef typename MatrixPromote
<
155 typename
Mat1_T::temporary_type
,
156 typename MatrixPromote
<
157 typename
Mat2_T::temporary_type
,
158 typename
Mat3_T::temporary_type
160 >::temporary_type temporary_type
;
161 typedef typename
temporary_type::value_type value_type
;
164 /** Type promotion for four matrix types. */
165 template < class Mat1_T
, class Mat2_T
, class Mat3_T
, class Mat4_T
>
166 struct MatrixPromote4
168 typedef typename MatrixPromote
<
169 typename
Mat1_T::temporary_type
,
170 typename MatrixPromote
<
171 typename
Mat2_T::temporary_type
,
172 typename MatrixPromote
<
173 typename
Mat3_T::temporary_type
,
174 typename
Mat4_T::temporary_type
177 >::temporary_type temporary_type
;
178 typedef typename
temporary_type::value_type value_type
;
186 // -------------------------------------------------------------------------