1 | /************************************************************** ggt-head beg |
---|
2 | * |
---|
3 | * GGT: Generic Graphics Toolkit |
---|
4 | * |
---|
5 | * Original Authors: |
---|
6 | * Allen Bierbaum |
---|
7 | * |
---|
8 | * ----------------------------------------------------------------- |
---|
9 | * File: VecExprMeta.h,v |
---|
10 | * Date modified: 2004/11/11 21:34:33 |
---|
11 | * Version: 1.3 |
---|
12 | * ----------------------------------------------------------------- |
---|
13 | * |
---|
14 | *********************************************************** ggt-head end */ |
---|
15 | /*************************************************************** ggt-cpr beg |
---|
16 | * |
---|
17 | * GGT: The Generic Graphics Toolkit |
---|
18 | * Copyright (C) 2001,2002 Allen Bierbaum |
---|
19 | * |
---|
20 | * This library is free software; you can redistribute it and/or |
---|
21 | * modify it under the terms of the GNU Lesser General Public |
---|
22 | * License as published by the Free Software Foundation; either |
---|
23 | * version 2.1 of the License, or (at your option) any later version. |
---|
24 | * |
---|
25 | * This library is distributed in the hope that it will be useful, |
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
28 | * Lesser General Public License for more details. |
---|
29 | * |
---|
30 | * You should have received a copy of the GNU Lesser General Public |
---|
31 | * License along with this library; if not, write to the Free Software |
---|
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
33 | * |
---|
34 | ************************************************************ ggt-cpr end */ |
---|
35 | #ifndef _GMTL_VEC_EXPR_META_H |
---|
36 | #define _GMTL_VEC_EXPR_META_H |
---|
37 | |
---|
38 | #include <gmtl/Util/Meta.h> |
---|
39 | #include <gmtl/VecOpsMeta.h> |
---|
40 | #include <gmtl/VecBase.h> |
---|
41 | |
---|
42 | /** Expression template classes for vec operations |
---|
43 | */ |
---|
44 | namespace gmtl |
---|
45 | { |
---|
46 | namespace meta |
---|
47 | { |
---|
48 | /** @ingroup VecExprMeta */ |
---|
49 | //@{ |
---|
50 | |
---|
51 | |
---|
52 | // ------------ Expression templates primitives ----------- // |
---|
53 | // Expression templates for vectors |
---|
54 | // |
---|
55 | // NOTE: These could probably be optimized more in the future |
---|
56 | |
---|
57 | // Concepts: |
---|
58 | // |
---|
59 | // VectorExpression: |
---|
60 | // types: |
---|
61 | // interface: |
---|
62 | // TYPE eval(unsigned i): Returns evaluation of expression at elt i |
---|
63 | |
---|
64 | |
---|
65 | /** template to hold a scalar argument. */ |
---|
66 | template <typename T> |
---|
67 | struct ScalarArg |
---|
68 | { |
---|
69 | typedef T DataType; |
---|
70 | |
---|
71 | const T mScalar; |
---|
72 | |
---|
73 | inline ScalarArg(const T scalar) : mScalar(scalar) {} |
---|
74 | inline T operator[](const unsigned) const |
---|
75 | { return mScalar; } |
---|
76 | }; |
---|
77 | |
---|
78 | template <typename T> |
---|
79 | inline ScalarArg<T> makeScalarArg(T val) |
---|
80 | { return ScalarArg<T>(val); } |
---|
81 | |
---|
82 | // --- TRAITS --- // |
---|
83 | /** Traits class for expression template parameters. |
---|
84 | * NOTE: These types are VERY important to the performance of the code. |
---|
85 | * They allow the compiler to optimize (ie. eliminate) much code. |
---|
86 | */ |
---|
87 | template<typename T> |
---|
88 | struct ExprTraits |
---|
89 | { |
---|
90 | typedef const T ExprRef; // Refer using a constant reference |
---|
91 | }; |
---|
92 | |
---|
93 | template<typename T, unsigned SIZE> |
---|
94 | struct ExprTraits< VecBase<T, SIZE, ScalarArg<T> > > |
---|
95 | { |
---|
96 | typedef const VecBase<T,SIZE,ScalarArg<T> > ExprRef; |
---|
97 | }; |
---|
98 | |
---|
99 | template<typename T, unsigned SIZE> |
---|
100 | struct ExprTraits< VecBase<T, SIZE, DefaultVecTag> > |
---|
101 | { |
---|
102 | typedef const VecBase<T,SIZE,DefaultVecTag>& ExprRef; |
---|
103 | }; |
---|
104 | |
---|
105 | |
---|
106 | // -- Expressions -- // |
---|
107 | /** Binary vector expression. |
---|
108 | * |
---|
109 | * Stores the two vector expressions to process. |
---|
110 | */ |
---|
111 | template <typename EXP1_T, typename EXP2_T, typename OP> |
---|
112 | struct VecBinaryExpr |
---|
113 | { |
---|
114 | typedef typename EXP1_T::DataType DataType; |
---|
115 | |
---|
116 | typename ExprTraits<EXP1_T>::ExprRef Exp1; |
---|
117 | typename ExprTraits<EXP2_T>::ExprRef Exp2; |
---|
118 | |
---|
119 | inline VecBinaryExpr(const EXP1_T& e1, const EXP2_T& e2) : Exp1(e1), Exp2(e2) {;} |
---|
120 | inline DataType operator[](const unsigned i) const |
---|
121 | { return OP::eval(Exp1[i], Exp2[i]); } |
---|
122 | }; |
---|
123 | |
---|
124 | /** Unary vector expression. |
---|
125 | * Holds the vector expression and unary operation to apply to it. |
---|
126 | */ |
---|
127 | template <typename EXP1_T, typename OP> |
---|
128 | struct VecUnaryExpr |
---|
129 | { |
---|
130 | typedef typename EXP1_T::DataType DataType; |
---|
131 | |
---|
132 | typename ExprTraits<EXP1_T>::ExprRef Exp1; |
---|
133 | |
---|
134 | inline VecUnaryExpr(const EXP1_T& e1) : Exp1(e1) {;} |
---|
135 | inline DataType operator[](const unsigned i) const |
---|
136 | { return OP::eval(Exp1[i]); } |
---|
137 | }; |
---|
138 | |
---|
139 | // --- Operations --- // |
---|
140 | /* Binary operations to add two vector expressions. */ |
---|
141 | struct VecPlusBinary |
---|
142 | { |
---|
143 | template <typename T> |
---|
144 | static inline T eval(const T a1, const T a2) |
---|
145 | { return a1+a2; } |
---|
146 | }; |
---|
147 | |
---|
148 | /* Binary operations to subtract two vector expressions. */ |
---|
149 | struct VecMinusBinary |
---|
150 | { |
---|
151 | template <typename T> |
---|
152 | static inline T eval(const T a1, const T a2) |
---|
153 | { return a1-a2; } |
---|
154 | }; |
---|
155 | |
---|
156 | struct VecMultBinary |
---|
157 | { |
---|
158 | template<typename T> |
---|
159 | static inline T eval(const T a1, const T a2) |
---|
160 | { return a1 * a2; } |
---|
161 | }; |
---|
162 | |
---|
163 | struct VecDivBinary |
---|
164 | { |
---|
165 | template<typename T> |
---|
166 | static inline T eval(const T a1, const T a2) |
---|
167 | { return a1/a2; } |
---|
168 | }; |
---|
169 | |
---|
170 | /** Negation of the values. */ |
---|
171 | struct VecNegUnary |
---|
172 | { |
---|
173 | template <typename T> |
---|
174 | static inline T eval(const T a1) |
---|
175 | { return -a1; } |
---|
176 | }; |
---|
177 | |
---|
178 | |
---|
179 | /* |
---|
180 | template<typename T, unsigned SIZE, typename R1, typename R2> |
---|
181 | inline VecBase<T,SIZE, VecBinaryExpr<VecBase<T,SIZE,R1>, VecBase<T,SIZE,R2>, VecPlusBinary> > |
---|
182 | sum(const VecBase<T,SIZE,R1>& v1, const VecBase<T,SIZE,R2>& v2) |
---|
183 | { |
---|
184 | return VecBase<T,SIZE, |
---|
185 | VecBinaryExpr<VecBase<T,SIZE,R1>, |
---|
186 | VecBase<T,SIZE,R2>, |
---|
187 | VecPlusBinary> >( VecBinaryExpr<VecBase<T,SIZE,R1>, |
---|
188 | VecBase<T,SIZE,R2>, |
---|
189 | VecPlusBinary>(v1,v2) ); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | template<typename T, unsigned SIZE, typename R1> |
---|
194 | inline VecBase<T,SIZE, VecBinaryExpr<VecBase<T,SIZE,R1>, VecBase<T,SIZE,ScalarArg<T> >, VecPlusBinary> > |
---|
195 | sum(const VecBase<T,SIZE,R1>& v1, const T& arg) |
---|
196 | { |
---|
197 | return VecBase<T,SIZE, |
---|
198 | VecBinaryExpr<VecBase<T,SIZE,R1>, |
---|
199 | VecBase<T,SIZE,ScalarArg<T> >, |
---|
200 | VecPlusBinary> >( VecBinaryExpr<VecBase<T,SIZE,R1>, |
---|
201 | VecBase<T,SIZE,ScalarArg<T> >, |
---|
202 | VecPlusBinary>(v1,ScalarArg<T>(arg)) ); |
---|
203 | } |
---|
204 | */ |
---|
205 | |
---|
206 | //@} |
---|
207 | |
---|
208 | |
---|
209 | } // namespace meta |
---|
210 | } // end namespace |
---|
211 | |
---|
212 | |
---|
213 | #endif |
---|