Overte C++ Documentation
GLMTestUtils.h
1 //
2 // GLMTestUtils.h
3 // tests/physics/src
4 //
5 // Created by Seiji Emery on 6/22/15
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_GLMTestUtils_h
13 #define hifi_GLMTestUtils_h
14 
15 #include <glm/glm.hpp>
16 #include <glm/gtx/quaternion.hpp>
17 #include <QTextStream>
18 
19 // Implements functionality in QTestExtensions.h for glm types
20 
21 // Computes the error value between two quaternions (using glm::dot)
22 float getErrorDifference(const glm::quat& a, const glm::quat& b) {
23  return fabsf(glm::dot(a, b)) - 1.0f;
24 }
25 
26 inline float getErrorDifference(const glm::vec3& a, const glm::vec3& b) {
27  return glm::distance(a, b);
28 }
29 
30 inline float getErrorDifference(const glm::mat4& a, const glm::mat4& b) {
31  float maxDiff = 0;
32  for (int i = 0; i < 4; ++i) {
33  for (int j = 0; j < 4; ++j) {
34  float diff = fabs(a[i][j] - b[i][j]);
35  maxDiff = std::max(diff, maxDiff);
36  }
37  }
38  return maxDiff;
39 }
40 
41 inline QTextStream& operator<<(QTextStream& stream, const glm::vec3& v) {
42  return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }";
43 }
44 
45 QTextStream& operator<<(QTextStream& stream, const glm::quat& q) {
46  return stream << "glm::quat { " << q.x << ", " << q.y << ", " << q.z << ", " << q.w << " }";
47 }
48 
49 inline QTextStream& operator<< (QTextStream& stream, const glm::mat4& matrix) {
50  stream << "[\n\t\t";
51  stream.setFieldWidth(15);
52  for (int r = 0; r < 4; ++r) {
53  for (int c = 0; c < 4; ++c) {
54  stream << matrix[c][r];
55  }
56  stream << "\n\t\t";
57  }
58  stream.setFieldWidth(0);
59  stream << "]\n\t"; // hacky as hell, but this should work...
60  return stream;
61 }
62 
63 #define QCOMPARE_QUATS(rotationA, rotationB, angle) \
64  QVERIFY(fabsf(1.0f - fabsf(glm::dot(rotationA, rotationB))) < 2.0f * sinf(angle))
65 
66 #endif