]> Dogcows Code - chaz/yoink/blob - src/cml/matrix/matrix_promotions.h
beginnings of scene rendering
[chaz/yoink] / src / 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 /* Default matrix type promotion template. */
37 template<typename LeftT, typename RightT> struct MatrixPromote;
38
39 /** Type promotion for two matrix types.
40 *
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.
44 */
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> >
48 {
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;
54
55 /* The deduced matrix result type: */
56 typedef cml::matrix<
57 typename promoted_array::value_type,
58 typename promoted_array::generator_type,
59 BO1,
60 typename promoted_array::layout
61 > type;
62
63 /* The deduced temporary type: */
64 typedef typename type::temporary_type temporary_type;
65 };
66
67 /**
68 * NOTE: MatrixPromote* are somewhat ad hoc, and were added to
69 * simplify the code for matrix slerp/squad/etc.
70 */
71
72 /** Type promotion for two matrix types. */
73 template < class Mat1_T, class Mat2_T >
74 struct MatrixPromote2
75 {
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;
80 };
81
82 /** Type promotion for three matrix types. */
83 template < class Mat1_T, class Mat2_T, class Mat3_T >
84 struct MatrixPromote3
85 {
86 typedef typename MatrixPromote<
87 typename Mat1_T::temporary_type,
88 typename MatrixPromote<
89 typename Mat2_T::temporary_type, typename Mat3_T::temporary_type
90 >::temporary_type
91 >::temporary_type temporary_type;
92 typedef typename temporary_type::value_type value_type;
93 };
94
95 /** Type promotion for four matrix types. */
96 template < class Mat1_T, class Mat2_T, class Mat3_T, class Mat4_T >
97 struct MatrixPromote4
98 {
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
106 >::temporary_type
107 >::temporary_type
108 >::temporary_type temporary_type;
109 typedef typename temporary_type::value_type value_type;
110 };
111
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>
115 {
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;
118
119 /* The deduced temporary type: */
120 typedef typename type::temporary_type temporary_type;
121 };
122
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> >
126 {
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;
129
130 /* The deduced temporary type: */
131 typedef typename type::temporary_type temporary_type;
132 };
133
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> >
137 {
138 typedef cml::vector<E1,AT1> left_type;
139 typedef cml::vector<E2,AT2> right_type;
140 typedef CML_DEFAULT_BASIS_ORIENTATION basis_orient;
141
142 /* Get matrix size: */
143 enum {
144 array_rows = left_type::array_size,
145 array_cols = right_type::array_size
146 };
147
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;
154
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;
159
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;
163 };
164
165 } // namespace et
166 } // namespace cml
167
168 #endif
169
170 // -------------------------------------------------------------------------
171 // vim:ft=cpp
This page took 0.036583 seconds and 4 git commands to generate.