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 /* Default matrix type promotion template. */
37 template<typename LeftT
, typename RightT
> struct MatrixPromote
;
39 /** Type promotion for two matrix types.
41 * @note This always uses the basis orientation of the left-hand matrix.
42 * @bug This always uses the basis orientation of the left-hand matrix,
43 * which is not always correct.
45 template<typename E1
, class AT1
, typename L1
, typename BO1
,
46 typename E2
, class AT2
, typename L2
, typename BO2
>
47 struct MatrixPromote
< cml::matrix
<E1
,AT1
,BO1
,L1
>, cml::matrix
<E2
,AT2
,BO2
,L2
> >
49 /* Promote the arrays: */
50 typedef typename ArrayPromote
<
51 typename
cml::matrix
<E1
,AT1
,BO1
,L1
>::array_type
,
52 typename
cml::matrix
<E2
,AT2
,BO2
,L2
>::array_type
53 >::type promoted_array
;
55 /* The deduced matrix result type: */
57 typename
promoted_array::value_type
,
58 typename
promoted_array::generator_type
,
60 typename
promoted_array::layout
63 /* The deduced temporary type: */
64 typedef typename
type::temporary_type temporary_type
;
68 * NOTE: MatrixPromote* are somewhat ad hoc, and were added to
69 * simplify the code for matrix slerp/squad/etc.
72 /** Type promotion for two matrix types. */
73 template < class Mat1_T
, class Mat2_T
>
76 typedef typename MatrixPromote
<
77 typename
Mat1_T::temporary_type
, typename
Mat2_T::temporary_type
78 >::temporary_type temporary_type
;
79 typedef typename
temporary_type::value_type value_type
;
82 /** Type promotion for three matrix types. */
83 template < class Mat1_T
, class Mat2_T
, class Mat3_T
>
86 typedef typename MatrixPromote
<
87 typename
Mat1_T::temporary_type
,
88 typename MatrixPromote
<
89 typename
Mat2_T::temporary_type
, typename
Mat3_T::temporary_type
91 >::temporary_type temporary_type
;
92 typedef typename
temporary_type::value_type value_type
;
95 /** Type promotion for four matrix types. */
96 template < class Mat1_T
, class Mat2_T
, class Mat3_T
, class Mat4_T
>
99 typedef typename MatrixPromote
<
100 typename
Mat1_T::temporary_type
,
101 typename MatrixPromote
<
102 typename
Mat2_T::temporary_type
,
103 typename MatrixPromote
<
104 typename
Mat3_T::temporary_type
,
105 typename
Mat4_T::temporary_type
108 >::temporary_type temporary_type
;
109 typedef typename
temporary_type::value_type value_type
;
112 /** Type promotion for a matrix and a scalar. */
113 template<typename E
, class AT
, typename BO
, typename L
, typename S
>
114 struct MatrixPromote
<cml::matrix
<E
,AT
,BO
,L
>, S
>
116 /* The deduced matrix result type (the array type is the same): */
117 typedef cml::matrix
<typename ScalarPromote
<E
,S
>::type
, AT
, BO
, L
> type
;
119 /* The deduced temporary type: */
120 typedef typename
type::temporary_type temporary_type
;
123 /** Type promotion for a scalar and a matrix. */
124 template<typename S
, typename E
, class AT
, typename BO
, typename L
>
125 struct MatrixPromote
<S
, cml::matrix
<E
,AT
,BO
,L
> >
127 /* The deduced matrix result type (the array type is the same): */
128 typedef cml::matrix
<typename ScalarPromote
<S
,E
>::type
, AT
, BO
, L
> type
;
130 /* The deduced temporary type: */
131 typedef typename
type::temporary_type temporary_type
;
134 /** Type promotion for outer product. */
135 template<typename E1
, class AT1
, typename E2
, class AT2
>
136 struct MatrixPromote
< cml::vector
<E1
,AT1
>, cml::vector
<E2
,AT2
> >
138 typedef cml::vector
<E1
,AT1
> left_type
;
139 typedef cml::vector
<E2
,AT2
> right_type
;
140 typedef CML_DEFAULT_BASIS_ORIENTATION basis_orient
;
142 /* Get matrix size: */
144 array_rows
= left_type::array_size
,
145 array_cols
= right_type::array_size
148 /* Deduce the corresponding matrix types for the vectors: */
149 typedef CML_DEFAULT_ARRAY_LAYOUT layout
;
150 typedef typename select_if
<
151 array_rows
== -1, dynamic
<>, fixed
<array_rows
,1>
152 >::result left_storage
;
153 typedef cml::matrix
<E1
,left_storage
,basis_orient
,layout
> left_matrix
;
155 typedef typename select_if
<
156 array_cols
== -1, dynamic
<>, fixed
<1,array_cols
>
157 >::result right_storage
;
158 typedef cml::matrix
<E2
,right_storage
,basis_orient
,layout
> right_matrix
;
160 /* Finally, promote the matrix types to get the result: */
161 typedef typename
et::MatrixPromote
<left_matrix
,right_matrix
>::type type
;
162 typedef typename
type::temporary_type temporary_type
;
170 // -------------------------------------------------------------------------