1 #ifndef _TCUVECTORUTIL_HPP
2 #define _TCUVECTORUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Tester Core
5 * ----------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Vector utility functions.
24 *//*--------------------------------------------------------------------*/
26 #include "tcuDefs.hpp"
27 #include "tcuVector.hpp"
28 #include "deRandom.hpp"
39 static const float PI = 3.141592653589793238f;
41 #if (DE_OS == DE_OS_ANDROID) || (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_QNX) || (DE_OS == DE_OS_WIN32 && DE_COMPILER == DE_COMPILER_CLANG)
42 inline float abs (float f) { return deFloatAbs(f); }
45 template<typename T> inline T add (T a, T b) { return a + b; }
46 template<typename T> inline T sub (T a, T b) { return a - b; }
47 template<typename T> inline T mul (T a, T b) { return a * b; }
48 template<typename T> inline T div (T a, T b) { return a / b; }
50 template<typename T> inline T bitwiseNot (T a) { return ~a; }
51 template<typename T> inline T bitwiseAnd (T a, T b) { return a & b; }
52 template<typename T> inline T bitwiseOr (T a, T b) { return a | b; }
53 template<typename T> inline T bitwiseXor (T a, T b) { return a ^ b; }
55 template<typename T> inline T logicalNot (T a) { return !a; }
56 template<typename T> inline T logicalAnd (T a, T b) { return a && b; }
57 template<typename T> inline T logicalOr (T a, T b) { return a || b; }
59 template<typename T> inline T mod (T a, T b) { return a % b; }
60 template<> inline float mod (float x, float y) { return x - y * deFloatFloor(x / y); }
62 template<typename T> inline T negate (T f) { return -f; }
63 template<> inline deUint32 negate<deUint32> (deUint32 f) { return (deUint32)-(int)f; }
65 inline float radians (float f) { return deFloatRadians(f); }
66 inline float degrees (float f) { return deFloatDegrees(f); }
67 inline float inverseSqrt (float f) { return deFloatRsq(f); }
68 inline float sign (float f) { return (f < 0.0f) ? -1.0f : ((f > 0.0f) ? +1.0f : 0.0f); }
69 inline float fract (float f) { return f - deFloatFloor(f); }
70 inline float mix (float x, float y, float a) { return x * (1.0f - a) + y * a; }
71 inline float step (float edge, float x) { return (x < edge) ? 0.0f : 1.0f; }
72 inline float smoothStep (float edge0, float edge1, float x)
74 if (x <= edge0) return 0.0f;
75 if (x >= edge1) return 1.0f;
76 float t = de::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
77 return t * t * (3.0f - 2.0f * t);
80 inline double mix (double x, double y, double a) { return x * (1.0 - a) + y * a; }
81 inline double step (double edge, double x) { return (x < edge) ? 0.0 : 1.0; }
83 inline float length (float f) { return deFloatAbs(f); }
84 inline float distance (float x, float y) { return deFloatAbs(x - y); }
85 inline float dot (float x, float y) { return (x * y); }
87 inline float normalize (float f) { return sign(f); }
88 inline float faceForward (float n, float i, float ref) { return ((ref * i) < 0.0f) ? n : -n; }
89 inline float reflect (float i, float n) { return i - 2.0f * (n * i) * n; }
90 inline float refract (float i, float n, float eta)
92 float cosAngle = (n * i);
93 float k = 1.0f - eta * eta * (1.0f - cosAngle * cosAngle);
97 return eta * i - (eta * cosAngle + deFloatSqrt(k)) * n;
100 template<typename T> inline bool lessThan (T a, T b) { return (a < b); }
101 template<typename T> inline bool lessThanEqual (T a, T b) { return (a <= b); }
102 template<typename T> inline bool greaterThan (T a, T b) { return (a > b); }
103 template<typename T> inline bool greaterThanEqual (T a, T b) { return (a >= b); }
104 template<typename T> inline bool equal (T a, T b) { return (a == b); }
105 template<typename T> inline bool notEqual (T a, T b) { return (a != b); }
106 template<typename T> inline bool allEqual (T a, T b) { return (a == b); }
107 template<typename T> inline bool anyNotEqual (T a, T b) { return (a != b); }
109 inline bool boolNot (bool a) { return !a; }
111 inline int chopToInt (float a) { return deChopFloatToInt32(a); }
113 inline float roundToEven (float a)
115 float q = deFloatFrac(a);
120 else if (q == 0.5 && (((int)r) % 2 != 0))
126 template <typename T, int Size>
127 inline T dot (const Vector<T, Size>& a, const Vector<T, Size>& b)
130 for (int i = 0; i < Size; i++)
131 res += a.m_data[i] * b.m_data[i];
135 template <typename T, int Size>
136 inline T lengthSquared (const Vector<T, Size>& a)
139 for (int i = 0; i < Size; i++)
140 sqSum += a.m_data[i] * a.m_data[i];
144 template <typename T, int Size>
145 inline typename de::meta::EnableIf<T, de::meta::TypesSame<T, double>::Value>::Type length (const Vector<T, Size>& a)
147 return ::sqrt(lengthSquared(a));
150 template <typename T, int Size>
151 inline typename de::meta::EnableIf<T, de::meta::TypesSame<T, float>::Value>::Type length (const Vector<T, Size>& a)
153 return deFloatSqrt(lengthSquared(a));
156 template <typename T, int Size>
157 inline T distance (const Vector<T, Size>& a, const Vector<T, Size>& b)
159 return length(a - b);
162 template <typename T, int Size>
163 inline Vector<T, Size> cross (const Vector<T, Size>& a, const Vector<T, Size>& b)
165 DE_STATIC_ASSERT(Size == 3);
166 return Vector<T, Size>(
167 a.y() * b.z() - b.y() * a.z(),
168 a.z() * b.x() - b.z() * a.x(),
169 a.x() * b.y() - b.x() * a.y());
172 template <typename T, int Size>
173 inline Vector<T, Size> normalize (const Vector<T, Size>& a)
175 T ooLen = T(1) / length(a);
177 for (int i = 0; i < Size; i++)
178 res.m_data[i] = ooLen * a.m_data[i];
182 template <typename T, int Size>
183 inline Vector<T, Size> faceForward (const Vector<T, Size>& n, const Vector<T, Size>& i, const Vector<T, Size>& ref)
185 return (dot(ref, i) < T(0)) ? n: -n;
188 template <typename T, int Size>
189 inline Vector<T, Size> reflect (const Vector<T, Size>& i, const Vector<T, Size>& n)
191 return i - T(2) * dot(n, i) * n;
194 template <typename T, int Size>
195 inline Vector<T, Size> refract (const Vector<T, Size>& i, const Vector<T, Size>& n, T eta)
197 T cosAngle = dot(n, i);
198 T k = T(1) - eta * eta * (T(1) - cosAngle * cosAngle);
200 return Vector<T, Size>(T(0));
202 return i * eta - n * T(eta * cosAngle + ::sqrt(k));
206 Vector<float, Size> mix (const Vector<float, Size>& x, const Vector<float, Size>& y, float a)
208 Vector<float, Size> res;
209 for (int i = 0; i < Size; i++)
210 res.m_data[i] = deFloatMix(x.m_data[i], y.m_data[i], a);
215 Vector<double, Size> mix (const Vector<double, Size>& x, const Vector<double, Size>& y, double a)
217 Vector<double, Size> res;
218 for (int i = 0; i < Size; i++)
219 res.m_data[i] = deMix(x.m_data[i], y.m_data[i], a);
223 // Piece-wise compare operators.
225 template <typename T, int Size>
226 inline Vector<bool, Size> equal (const Vector<T, Size>& a, const Vector<T, Size>& b)
228 Vector<bool, Size> res;
229 for (int i = 0; i < Size; i++)
230 res.m_data[i] = a.m_data[i] == b.m_data[i];
234 template <typename T, int Size>
235 inline Vector<bool, Size> notEqual (const Vector<T, Size>& a, const Vector<T, Size>& b)
237 Vector<bool, Size> res;
238 for (int i = 0; i < Size; i++)
239 res.m_data[i] = a.m_data[i] != b.m_data[i];
243 template <typename T, int Size>
244 inline Vector<bool, Size> lessThan (const Vector<T, Size>& a, const Vector<T, Size>& b)
246 Vector<bool, Size> res;
247 for (int i = 0; i < Size; i++)
248 res.m_data[i] = a.m_data[i] < b.m_data[i];
252 template <typename T, int Size>
253 inline Vector<bool, Size> lessThanEqual (const Vector<T, Size>& a, const Vector<T, Size>& b)
255 Vector<bool, Size> res;
256 for (int i = 0; i < Size; i++)
257 res.m_data[i] = a.m_data[i] <= b.m_data[i];
261 template <typename T, int Size>
262 inline Vector<bool, Size> greaterThan (const Vector<T, Size>& a, const Vector<T, Size>& b)
264 Vector<bool, Size> res;
265 for (int i = 0; i < Size; i++)
266 res.m_data[i] = a.m_data[i] > b.m_data[i];
270 template <typename T, int Size>
271 inline Vector<bool, Size> greaterThanEqual (const Vector<T, Size>& a, const Vector<T, Size>& b)
273 Vector<bool, Size> res;
274 for (int i = 0; i < Size; i++)
275 res.m_data[i] = a.m_data[i] >= b.m_data[i];
279 // Equality comparison operators.
281 template <typename T, int Size>
282 inline bool allEqual (const Vector<T, Size>& a, const Vector<T, Size>& b)
285 for (int i = 0; i < Size; i++)
286 res = res && a.m_data[i] == b.m_data[i];
290 template <typename T, int Size>
291 inline bool anyNotEqual (const Vector<T, Size>& a, const Vector<T, Size>& b)
294 for (int i = 0; i < Size; i++)
295 res = res || a.m_data[i] != b.m_data[i];
299 // Boolean built-ins.
302 inline Vector<bool, Size> boolNot (const Vector<bool, Size>& a)
304 Vector<bool, Size> res;
305 for (int i = 0; i < Size; i++)
306 res.m_data[i] = !a.m_data[i];
311 inline bool boolAny (const Vector<bool, Size>& a)
313 for (int i = 0; i < Size; i++)
314 if (a.m_data[i] == true)
320 inline bool boolAll (const Vector<bool, Size>& a)
322 for (int i = 0; i < Size; i++)
323 if (a.m_data[i] == false)
329 Vector<int, Size> chopToInt (const Vector<float, Size>& v)
331 Vector<int, Size> res;
332 for (int i = 0; i < Size; i++)
333 res.m_data[i] = chopToInt(v.m_data[i]);
337 // Vector construction using selection based on boolean vector.
339 template <typename T, int Size>
340 inline Vector<T, Size> select (T trueVal, T falseVal, const Vector<bool, Size>& cond)
343 for (int i = 0; i < Size; i++)
344 res[i] = cond[i] ? trueVal : falseVal;
348 // Component-wise selection.
350 template <typename T, int Size>
351 inline Vector<T, Size> select (const Vector<T, Size>& trueVal, const Vector<T, Size>& falseVal, const Vector<bool, Size>& cond)
354 for (int i = 0; i < Size; i++)
355 res[i] = cond[i] ? trueVal[i] : falseVal[i];
359 // Absolute difference (abs(a - b))
361 template<typename T, int Size>
362 static inline Vector<T, Size> absDiff (const Vector<T, Size>& a, const Vector<T, Size>& b)
366 for (int ndx = 0; ndx < Size; ndx++)
367 res[ndx] = (a[ndx] > b[ndx]) ? (a[ndx] - b[ndx]) : (b[ndx] - a[ndx]);
372 template<typename T, int Size>
373 inline tcu::Vector<T, Size> randomVector (de::Random& rnd, const tcu::Vector<T, Size>& minValue, const tcu::Vector<T, Size>& maxValue)
375 tcu::Vector<T, Size> res;
377 for (int ndx = 0; ndx < Size; ndx++)
378 res[ndx] = de::randomScalar<T>(rnd, minValue[ndx], maxValue[ndx]);
383 inline Vector<float, 2> randomVec2 (de::Random& rnd)
385 return randomVector<float, 2>(rnd, tcu::Vector<float, 2>(0.0f), tcu::Vector<float, 2>(1.0f));
388 inline Vector<float, 3> randomVec3 (de::Random& rnd)
390 return randomVector<float, 3>(rnd, tcu::Vector<float, 3>(0.0f), tcu::Vector<float, 3>(1.0f));
393 inline Vector<float, 4> randomVec4 (de::Random& rnd)
395 return randomVector<float, 4>(rnd, tcu::Vector<float, 4>(0.0f), tcu::Vector<float, 4>(1.0f));
398 // Macros for component-wise ops.
400 #define TCU_DECLARE_VECTOR_UNARY_FUNC(FUNC_NAME, OP_NAME) \
401 template <typename T, int Size> \
402 Vector<T, Size> FUNC_NAME (const Vector<T, Size>& v) \
404 Vector<T, Size> res; \
405 for (int i = 0; i < Size; i++) \
406 res.m_data[i] = OP_NAME(v.m_data[i]); \
410 #define TCU_DECLARE_VECTOR_BINARY_FUNC(FUNC_NAME, OP_NAME) \
411 template <typename T, int Size> \
412 Vector<T, Size> FUNC_NAME (const Vector<T, Size>& a, const Vector<T, Size>& b) \
414 Vector<T, Size> res; \
415 for (int i = 0; i < Size; i++) \
416 res.m_data[i] = OP_NAME(a.m_data[i], b.m_data[i]); \
420 #define TCU_DECLARE_VECTOR_TERNARY_FUNC(FUNC_NAME, OP_NAME) \
421 template <typename T, int Size> \
422 Vector<T, Size> FUNC_NAME (const Vector<T, Size>& a, const Vector<T, Size>& b, const Vector<T, Size>& c) \
424 Vector<T, Size> res; \
425 for (int i = 0; i < Size; i++) \
426 res.m_data[i] = OP_NAME(a.m_data[i], b.m_data[i], c.m_data[i]); \
430 // \todo [2011-07-01 pyry] Add some prefix to vector funcs and remove this hack.
438 TCU_DECLARE_VECTOR_UNARY_FUNC(negate, negate)
439 TCU_DECLARE_VECTOR_UNARY_FUNC(bitwiseNot, bitwiseNot)
440 TCU_DECLARE_VECTOR_BINARY_FUNC(add, add)
441 TCU_DECLARE_VECTOR_BINARY_FUNC(sub, sub)
442 TCU_DECLARE_VECTOR_BINARY_FUNC(mul, mul)
443 TCU_DECLARE_VECTOR_BINARY_FUNC(div, div)
444 TCU_DECLARE_VECTOR_BINARY_FUNC(mod, mod)
445 TCU_DECLARE_VECTOR_BINARY_FUNC(bitwiseAnd, bitwiseAnd)
446 TCU_DECLARE_VECTOR_BINARY_FUNC(bitwiseOr, bitwiseOr)
447 TCU_DECLARE_VECTOR_BINARY_FUNC(bitwiseXor, bitwiseXor)
448 TCU_DECLARE_VECTOR_UNARY_FUNC(logicalNot, logicalNot)
449 TCU_DECLARE_VECTOR_BINARY_FUNC(logicalAnd, logicalAnd)
450 TCU_DECLARE_VECTOR_BINARY_FUNC(logicalOr, logicalOr)
452 TCU_DECLARE_VECTOR_UNARY_FUNC(radians, deFloatRadians)
453 TCU_DECLARE_VECTOR_UNARY_FUNC(degrees, deFloatDegrees)
454 TCU_DECLARE_VECTOR_UNARY_FUNC(sin, deFloatSin)
455 TCU_DECLARE_VECTOR_UNARY_FUNC(cos, deFloatCos)
456 TCU_DECLARE_VECTOR_UNARY_FUNC(tan, deFloatTan)
457 TCU_DECLARE_VECTOR_UNARY_FUNC(asin, deFloatAsin)
458 TCU_DECLARE_VECTOR_UNARY_FUNC(acos, deFloatAcos)
459 TCU_DECLARE_VECTOR_UNARY_FUNC(atan, deFloatAtanOver)
460 TCU_DECLARE_VECTOR_BINARY_FUNC(atan2, deFloatAtan2)
461 TCU_DECLARE_VECTOR_UNARY_FUNC(sinh, deFloatSinh)
462 TCU_DECLARE_VECTOR_UNARY_FUNC(cosh, deFloatCosh)
463 TCU_DECLARE_VECTOR_UNARY_FUNC(tanh, deFloatTanh)
464 TCU_DECLARE_VECTOR_UNARY_FUNC(asinh, deFloatAsinh)
465 TCU_DECLARE_VECTOR_UNARY_FUNC(acosh, deFloatAcosh)
466 TCU_DECLARE_VECTOR_UNARY_FUNC(atanh, deFloatAtanh)
468 TCU_DECLARE_VECTOR_BINARY_FUNC(pow, deFloatPow)
469 TCU_DECLARE_VECTOR_UNARY_FUNC(exp, deFloatExp)
470 TCU_DECLARE_VECTOR_UNARY_FUNC(log, deFloatLog)
471 TCU_DECLARE_VECTOR_UNARY_FUNC(exp2, deFloatExp2)
472 TCU_DECLARE_VECTOR_UNARY_FUNC(log2, deFloatLog2)
473 TCU_DECLARE_VECTOR_UNARY_FUNC(sqrt, deFloatSqrt)
474 TCU_DECLARE_VECTOR_UNARY_FUNC(inverseSqrt, deFloatRsq)
476 TCU_DECLARE_VECTOR_UNARY_FUNC(abs, de::abs)
477 TCU_DECLARE_VECTOR_UNARY_FUNC(sign, deFloatSign)
478 TCU_DECLARE_VECTOR_UNARY_FUNC(floor, deFloatFloor)
479 TCU_DECLARE_VECTOR_UNARY_FUNC(trunc, deFloatTrunc)
480 TCU_DECLARE_VECTOR_UNARY_FUNC(roundToEven, roundToEven)
481 TCU_DECLARE_VECTOR_UNARY_FUNC(ceil, deFloatCeil)
482 TCU_DECLARE_VECTOR_UNARY_FUNC(fract, deFloatFrac)
483 TCU_DECLARE_VECTOR_BINARY_FUNC(min, de::min)
484 TCU_DECLARE_VECTOR_BINARY_FUNC(max, de::max)
485 TCU_DECLARE_VECTOR_TERNARY_FUNC(clamp, de::clamp)
486 TCU_DECLARE_VECTOR_TERNARY_FUNC(mix, deFloatMix)
487 TCU_DECLARE_VECTOR_BINARY_FUNC(step, deFloatStep)
488 TCU_DECLARE_VECTOR_TERNARY_FUNC(smoothStep, deFloatSmoothStep)
492 #endif // _TCUVECTORUTIL_HPP