| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include "math/Plane3.h" |
| 4 |
#include "math/Matrix4.h" |
| 5 |
|
| 6 |
namespace test |
| 7 |
{ |
| 8 |
|
| 9 |
namespace |
| 10 |
{ |
| 11 |
const double EPSILON = 0.0000001; |
| 12 |
const double ONE_OVER_ROOT_TWO = 1.0 / sqrt(2.0); |
| 13 |
} |
| 14 |
|
| 15 |
TEST(MathTest, ConstructPlane) |
| 16 |
{ |
| 17 |
Plane3 plane(1, 0, 0, 4); |
| 18 |
EXPECT_EQ(plane.normal(), Vector3(1, 0, 0)); |
| 19 |
EXPECT_EQ(plane.dist(), 4); |
| 20 |
|
| 21 |
Plane3 plane2(1.5, 56.4, -325, 19.23); |
| 22 |
EXPECT_EQ(plane2.normal(), Vector3(1.5, 56.4, -325)); |
| 23 |
EXPECT_EQ(plane2.dist(), 19.23); |
| 24 |
|
| 25 |
Plane3 copy = plane2; |
| 26 |
EXPECT_EQ(copy.normal(), Vector3(1.5, 56.4, -325)); |
| 27 |
EXPECT_EQ(copy.dist(), 19.23); |
| 28 |
} |
| 29 |
|
| 30 |
TEST(MathTest, TranslatePlaneDirectly) |
| 31 |
{ |
| 32 |
// Basic plane |
| 33 |
Plane3 plane(1, 0, 0, 2.5); |
| 34 |
plane.translate(Vector3(2, 0, 0)); |
| 35 |
|
| 36 |
EXPECT_EQ(plane.normal(), Vector3(1, 0, 0)); |
| 37 |
EXPECT_EQ(plane.dist(), 0.5); |
| 38 |
|
| 39 |
// Inclined plane |
| 40 |
Vector3 normal = Vector3(1.5, 2.6, -0.3).getNormalised(); |
| 41 |
Plane3 inclined(normal, 4.5); |
| 42 |
EXPECT_EQ(inclined.normal(), normal); |
| 43 |
|
| 44 |
inclined.translate(Vector3(1, 1, 0)); |
| 45 |
EXPECT_NEAR(inclined.normal().x(), 0.49724515641972766, EPSILON); |
| 46 |
EXPECT_NEAR(inclined.normal().y(), 0.86189160446086122, EPSILON); |
| 47 |
EXPECT_NEAR(inclined.normal().z(), -0.0994490312834552, EPSILON); |
| 48 |
|
| 49 |
// Check that a plane transformed by a translation matrix matches the |
| 50 |
// plane's own translation function |
| 51 |
Vector3 normal3 = Vector3(0.5, 1.24, 56).getNormalised(); |
| 52 |
Plane3 plane3(normal3, -56.1); |
| 53 |
|
| 54 |
Vector3 transVec(1.5, 45, -2.29); |
| 55 |
Matrix4 transMat = Matrix4::getTranslation(transVec); |
| 56 |
Plane3 p3_matrix = plane3.transformed(transMat); |
| 57 |
|
| 58 |
Plane3 p3_trans = plane3; |
| 59 |
p3_trans.translate(transVec); |
| 60 |
|
| 61 |
EXPECT_NEAR(p3_matrix.normal().x(), p3_trans.normal().x(), EPSILON); |
| 62 |
EXPECT_NEAR(p3_matrix.normal().y(), p3_trans.normal().y(), EPSILON); |
| 63 |
EXPECT_NEAR(p3_matrix.normal().z(), p3_trans.normal().z(), EPSILON); |
| 64 |
} |
| 65 |
|
| 66 |
TEST(MathTest, TranslatePlaneWithMatrix) |
| 67 |
{ |
| 68 |
// Plane for y = 5 |
| 69 |
Plane3 plane(0, 1, 0, 5); |
| 70 |
EXPECT_EQ(plane.normal(), Vector3(0, 1, 0)); |
| 71 |
|
| 72 |
// Translate the plane by 3 in the Y direction |
| 73 |
Matrix4 trans = Matrix4::getTranslation(Vector3(0, 3, 0)); |
| 74 |
Plane3 newPlane = plane.transformed(trans); |
| 75 |
|
| 76 |
// The normal should not have changed but the distance should, although for |
| 77 |
// some reason the translation happens backwards (i.e. negative Y) |
| 78 |
EXPECT_EQ(newPlane.normal(), Vector3(0, 1, 0)); |
| 79 |
EXPECT_EQ(newPlane.dist(), 2); |
| 80 |
|
| 81 |
// Inclined plane |
| 82 |
Plane3 inclined(1, -1, 0, 0); |
| 83 |
EXPECT_EQ(inclined.dist(), 0); |
| 84 |
|
| 85 |
// Again move 3 in the Y direction |
| 86 |
Plane3 newInclined = inclined.transformed(trans); |
| 87 |
|
| 88 |
// Again there should be no normal change, but a distance change |
| 89 |
EXPECT_EQ(newInclined.normal(), Vector3(1, -1, 0)); |
| 90 |
EXPECT_EQ(newInclined.dist(), 3); |
| 91 |
|
| 92 |
// If moved along Z the distance should not change |
| 93 |
Plane3 movedZ = inclined.transformed( |
| 94 |
Matrix4::getTranslation(Vector3(0, 0, 2.3)) |
| 95 |
); |
| 96 |
EXPECT_EQ(movedZ.normal(), Vector3(1, -1, 0)); |
| 97 |
EXPECT_EQ(movedZ.dist(), 0); |
| 98 |
} |
| 99 |
|
| 100 |
TEST(MathTest, RotatePlane) |
| 101 |
{ |
| 102 |
// A plane at 5 in the Y direction |
| 103 |
Plane3 plane(0, 1, 0, 5); |
| 104 |
|
| 105 |
// Rotate 45 degrees around the Z axis |
| 106 |
Matrix4 rot = Matrix4::getRotation( |
| 107 |
Vector3(0, 0, 1), math::PI / 4 |
| 108 |
); |
| 109 |
Plane3 rotated = plane.transformed(rot); |
| 110 |
|
| 111 |
EXPECT_NEAR(rotated.normal().x(), ONE_OVER_ROOT_TWO, EPSILON); |
| 112 |
EXPECT_NEAR(rotated.normal().y(), ONE_OVER_ROOT_TWO, EPSILON); |
| 113 |
EXPECT_EQ(rotated.normal().z(), 0); |
| 114 |
} |
| 115 |
|
| 116 |
TEST(MathTest, ScalePlane) |
| 117 |
{ |
| 118 |
Plane3 plane(1, -1, 0, 3.5); |
| 119 |
|
| 120 |
// Scale the plane by a factor of 2 |
| 121 |
Matrix4 times2 = Matrix4::getScale(Vector3(2, 2, 2)); |
| 122 |
Plane3 scaled = plane.transformed(times2); |
| 123 |
|
| 124 |
EXPECT_EQ(scaled.normal().x(), 2); |
| 125 |
EXPECT_EQ(scaled.normal().y(), -2); |
| 126 |
EXPECT_EQ(scaled.normal().z(), 0); |
| 127 |
|
| 128 |
EXPECT_EQ(scaled.dist(), 28); |
| 129 |
} |
| 130 |
|
| 131 |
TEST(MathTest, TransformPlane) |
| 132 |
{ |
| 133 |
// Check transform with some randomly-generated values with no particular |
| 134 |
// geometric meaning |
| 135 |
Plane3 plane(-2.643101, -47.856364, 17.5173264, -35.589485); |
| 136 |
Plane3 copy = plane; |
| 137 |
|
| 138 |
Matrix4 arbMatrix = Matrix4::byRows( |
| 139 |
-30.1065587, -28.048640, -10.003604, 18.986724, |
| 140 |
18.94792014, -16.186764, -8.7790217, -32.59777, |
| 141 |
12.85452125, -8.8305872, -36.502315, 32.345895, |
| 142 |
0, 0, 0, 1 |
| 143 |
); |
| 144 |
Plane3 transformed = plane.transformed(arbMatrix); |
| 145 |
EXPECT_EQ(plane, copy); // must not be changed by transformed() |
| 146 |
|
| 147 |
// TODO: Improve the maths so that this works more accurately |
| 148 |
static const double ROUGH_EPSILON = 0.001; |
| 149 |
EXPECT_NEAR(transformed.normal().x(), 1246.64431, ROUGH_EPSILON); |
| 150 |
EXPECT_NEAR(transformed.normal().y(), 570.773414, ROUGH_EPSILON); |
| 151 |
EXPECT_NEAR(transformed.normal().z(), -250.79896, ROUGH_EPSILON); |
| 152 |
|
| 153 |
EXPECT_NEAR(transformed.dist(), -69140351.87873, ROUGH_EPSILON); |
| 154 |
} |
| 155 |
|
| 156 |
} |