]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/netradiant-src/libs/math/expression.cpp
Move all other sources in a separate subfolder
[voretournament/voretournament.git] / misc / mediasource / netradiant-src / libs / math / expression.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "expression.h"
23
24 Vector3 testAdded1(const Vector3& a, const Vector3& b)
25 {
26   return vector3_added(a, vector3_added(a, b));
27 }
28
29 Vector3 testAdded2(const Vector3& a, const Vector3& b)
30 {
31   return vector3_for_expression( vector_added( vector3_identity(a), vector_added( vector3_identity(a), vector3_identity(b) ) ) );
32 }
33
34 Vector3 testMultiplied1(const Vector3& a, const Vector3& b)
35 {
36   return vector3_scaled(a, b);
37 }
38
39 Vector3 testMultiplied2(const Vector3& a, const Vector3& b)
40 {
41   return vector3_for_expression( vector_multiplied( vector3_identity(a), vector3_identity(b) ) );
42 }
43
44 Vector3 testCross1(const Vector3& a, const Vector3& b)
45 {
46   return vector3_cross(a, b);
47 }
48
49 Vector3 testCross2(const Vector3& a, const Vector3& b)
50 {
51   return vector3_for_expression( vector_cross( vector3_identity(a), vector3_identity(b) ) );
52 }
53
54 double testDot1(const Vector3& a, const Vector3& b)
55 {
56   return vector3_dot(a, b);
57 }
58
59 double testDot2(const Vector3& a, const Vector3& b)
60 {
61   return float_for_expression( vector_dot( vector3_identity(a), vector3_identity(b) ) );
62 }
63
64 double testLength1(const Vector3& a)
65 {
66   return vector3_length(a);
67 }
68
69 double testLength2(const Vector3& a)
70 {
71   return float_for_expression( vector_length( vector3_identity(a) ) );
72 }
73
74 Vector3 testNormalised1(const Vector3& a)
75 {
76   return vector3_normalised(a);
77 }
78
79 Vector3 testNormalised2(const Vector3& a)
80 {
81   return vector3_for_expression( vector_normalised( vector3_identity(a) ) );
82 }
83
84 Vector3 testNegated1(const Vector3& a)
85 {
86   return vector3_negated(a);
87 }
88
89 Vector3 testNegated2(const Vector3& a)
90 {
91   return vector3_for_expression( vector_negated( vector3_identity(a) ) );
92 }
93
94 Vector3 testScaled1(const Vector3& a, const double& b)
95 {
96   return vector3_scaled(a, b);
97 }
98
99 Vector3 testScaled2(const Vector3& a, const double& b)
100 {
101   return vector3_for_expression( vector_scaled( vector3_identity(a), float_literal(b) ) );
102 }
103
104 Vector3 testMatrixMultiplied1(const Vector3& a, const Matrix4& b)
105 {
106   return matrix4_transformed_point(b, vector3_added(a, Vector3(1, 0, 0)));
107 }
108
109 Vector3 testMatrixMultiplied2(const Vector3& a, const Matrix4& b)
110 {
111   return vector3_for_expression(
112     point_multiplied(
113       vector_added(
114         vector3_identity(a),
115         vector3_literal(Vector3(1, 0, 0))
116       ),
117       matrix4_identity(b)
118     )
119   );
120 }
121
122 Matrix4 testMatrix4Multiplied1(const Matrix4& a, const Matrix4& b)
123 {
124   return matrix4_multiplied_by_matrix4(a, matrix4_multiplied_by_matrix4(a, b));
125 }
126
127 Matrix4 testMatrix4Multiplied2(const Matrix4& a, const Matrix4& b)
128 {
129   return matrix4_for_expression(
130     matrix4_multiplied(
131       matrix4_identity(a),
132       matrix4_identity(b)
133     )
134   );
135 }
136
137 Matrix4 testMatrix4AffineMultiplied1(const Matrix4& a, const Matrix4& b)
138 {
139   return matrix4_affine_multiplied_by_matrix4(a, b);
140 }
141
142 Matrix4 testMatrix4AffineMultiplied2(const Matrix4& a, const Matrix4& b)
143 {
144   return matrix4_affine_for_expression(
145     matrix4_multiplied(
146       matrix4_identity(a),
147       matrix4_identity(b)
148     )
149   );
150 }
151
152 Matrix4 testMatrix4MultipliedConstant1(const Matrix4& a)
153 {
154   return matrix4_multiplied_by_matrix4(a, g_matrix4_identity);
155 }
156
157 Matrix4 testMatrix4MultipliedConstant2(const Matrix4& a)
158 {
159   return matrix4_for_expression(
160     matrix4_multiplied(
161       matrix4_identity(a),
162       matrix4_identity(g_matrix4_identity)
163     )
164   );
165 }
166 Matrix4 testMatrix4Transposed1(const Matrix4& a)
167 {
168   return matrix4_transposed(a);
169 }
170
171 Matrix4 testMatrix4Transposed2(const Matrix4& a)
172 {
173   return matrix4_for_expression( matrix_transposed( matrix4_identity(a) ) );
174 }
175
176 Vector3 testMulti1(const Matrix4& a, const Vector3& b, const Vector3& c)
177 {
178   return vector3_added(matrix4_transformed_point(matrix4_transposed(a), b), c);
179 }
180
181 Vector3 testMulti2(const Matrix4& a, const Vector3& b, const Vector3& c)
182 {
183   return vector3_for_expression(
184     vector_added(
185       point_multiplied(
186         vector3_identity(b),
187         matrix_transposed(matrix4_identity(a))
188       ),
189       vector3_identity(c)
190     )
191   );
192 }
193
194 template<typename Value, typename First, typename Second>
195 class TestBinaryFunction
196 {
197   typedef Value(*Function)(const First&, const Second&);
198   Function m_function;
199 public:
200
201   TestBinaryFunction(Function function) : m_function(function)
202   {
203   }
204   Value run(const First& first, const Second& second) const
205   {
206     return m_function(first, second);
207   }
208 };
209
210 template<typename Value, typename First>
211 class TestUnaryFunction
212 {
213   typedef Value(*Function)(const First&);
214   Function m_function;
215 public:
216
217   TestUnaryFunction(Function function) : m_function(function)
218   {
219   }
220   Value run(const First& first) const
221   {
222     return m_function(first);
223   }
224 };
225
226 class TestAll
227 {
228 public:
229   TestAll()
230   {
231     Vector3 result1 = TestBinaryFunction<Vector3, Vector3, Vector3>(testAdded1).run(Vector3(0, 0, 0), Vector3(1, 1, 1));
232     Vector3 result2 = TestBinaryFunction<Vector3, Vector3, Vector3>(testAdded2).run(Vector3(0, 0, 0), Vector3(1, 1, 1));
233     Vector3 result3 = TestBinaryFunction<Vector3, Vector3, Vector3>(testMultiplied1).run(Vector3(1, 2, 3), Vector3(2, 1, 0.5f));
234     Vector3 result4 = TestBinaryFunction<Vector3, Vector3, Vector3>(testMultiplied2).run(Vector3(1, 2, 3), Vector3(2, 1, 0.5f));
235     Vector3 result5 = TestBinaryFunction<Vector3, Vector3, double>(testScaled1).run(Vector3(1, 2, 3), 2.0);
236     Vector3 result6 = TestBinaryFunction<Vector3, Vector3, double>(testScaled2).run(Vector3(1, 2, 3), 2.0);
237     Vector3 result7 = TestBinaryFunction<Vector3, Vector3, Matrix4>(testMatrixMultiplied1).run(Vector3(1, 2, 3), matrix4_rotation_for_x_degrees(90));
238     Vector3 result8 = TestBinaryFunction<Vector3, Vector3, Matrix4>(testMatrixMultiplied2).run(Vector3(1, 2, 3), matrix4_rotation_for_x_degrees(90));
239     Vector3 result9 = TestUnaryFunction<Vector3, Vector3>(testNormalised1).run(Vector3(1, 2, 3));
240     Vector3 result10 = TestUnaryFunction<Vector3, Vector3>(testNormalised2).run(Vector3(1, 2, 3));
241   }
242 } g_testAll;
243