]> Dogcows Code - chaz/yoink/blob - src/Moof/cml/matrix/matrix_promotions.h
cml version bump to 1.0.1
[chaz/yoink] / src / Moof / cml / matrix / matrix_promotions.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 *
12 * Defines promotions for matrices used in matrix/matrix or matrix/scalar
13 * expressions.
14 *
15 * @sa UnaryMat4_TOp
16 * @sa BinaryMat4_TOp
17 */
18
19 #ifndef matrix_promotions_h
20 #define matrix_promotions_h
21
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>
27
28 /* This is used below to create a more meaningful compile-time error when
29 * either argument to OuterPromote has the wrong orientation.
30 */
31 struct outer_promote_expects_properly_oriented_args_error;
32
33 namespace cml {
34 namespace et {
35
36 /** Promote two types to a matrixt type. */
37 template<typename LeftT, typename RightT> struct MatrixPromote
38 {
39 /* Default matrix type promotion template. */
40 template<typename M1, typename M2> struct MatrixPromoteHelper;
41
42 /** Type promotion for two matrix types.
43 *
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.
47 */
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>
52 >
53 {
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;
59
60 /* The deduced matrix result type: */
61 typedef cml::matrix<
62 typename promoted_array::value_type,
63 typename promoted_array::generator_type,
64 BO1,
65 typename promoted_array::layout
66 > type;
67
68 /* The deduced temporary type: */
69 typedef typename type::temporary_type temporary_type;
70 };
71
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>
75 {
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;
78
79 /* The deduced temporary type: */
80 typedef typename type::temporary_type temporary_type;
81 };
82
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> >
86 {
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;
89
90 /* The deduced temporary type: */
91 typedef typename type::temporary_type temporary_type;
92 };
93
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> >
97 {
98 typedef cml::vector<E1,AT1> left_type;
99 typedef cml::vector<E2,AT2> right_type;
100 typedef CML_DEFAULT_BASIS_ORIENTATION basis_orient;
101
102 /* Get matrix size: */
103 enum {
104 array_rows = left_type::array_size,
105 array_cols = right_type::array_size
106 };
107
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;
114
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;
119
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;
123 };
124
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;
130
131 typedef typename MatrixPromoteHelper<LeftBaseT,RightBaseT>::type type;
132 typedef typename type::temporary_type temporary_type;
133 };
134
135 /**
136 * NOTE: MatrixPromote* are somewhat ad hoc, and were added to
137 * simplify the code for matrix slerp/squad/etc.
138 */
139
140 /** Type promotion for two matrix types. */
141 template < class Mat1_T, class Mat2_T >
142 struct MatrixPromote2
143 {
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;
148 };
149
150 /** Type promotion for three matrix types. */
151 template < class Mat1_T, class Mat2_T, class Mat3_T >
152 struct MatrixPromote3
153 {
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
159 >::temporary_type
160 >::temporary_type temporary_type;
161 typedef typename temporary_type::value_type value_type;
162 };
163
164 /** Type promotion for four matrix types. */
165 template < class Mat1_T, class Mat2_T, class Mat3_T, class Mat4_T >
166 struct MatrixPromote4
167 {
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
175 >::temporary_type
176 >::temporary_type
177 >::temporary_type temporary_type;
178 typedef typename temporary_type::value_type value_type;
179 };
180
181 } // namespace et
182 } // namespace cml
183
184 #endif
185
186 // -------------------------------------------------------------------------
187 // vim:ft=cpp
This page took 0.043113 seconds and 4 git commands to generate.