| 1 |
#include "gtest/gtest.h" |
| 2 |
|
| 3 |
#include "stk_middle_mesh/change_of_basis.hpp" |
| 4 |
|
| 5 |
namespace stk { |
| 6 |
namespace middle_mesh { |
| 7 |
namespace impl { |
| 8 |
|
| 9 |
using namespace utils::impl; |
| 10 |
|
| 11 |
namespace { |
| 12 |
void expect_float_eq(const utils::Point& pt1, const utils::Point& pt2) |
| 13 |
{ |
| 14 |
EXPECT_FLOAT_EQ(pt1.x, pt2.x); |
| 15 |
EXPECT_FLOAT_EQ(pt1.y, pt2.y); |
| 16 |
EXPECT_FLOAT_EQ(pt1.z, pt2.z); |
| 17 |
} |
| 18 |
|
| 19 |
// tests that the basis vectors are orthonormal |
| 20 |
void test_orthonormal(std::array<utils::Point, 3> pts) |
| 21 |
{ |
| 22 |
for (int i = 0; i < 3; ++i) |
| 23 |
for (int j = 0; j < 3; ++j) |
| 24 |
if (i == j) |
| 25 |
EXPECT_FLOAT_EQ(dot(pts[i], pts[j]), 1); |
| 26 |
else |
| 27 |
EXPECT_FLOAT_EQ(dot(pts[i], pts[j]), 0); |
| 28 |
|
| 29 |
// also test right handed |
| 30 |
for (int i = 0; i < 3; ++i) |
| 31 |
{ |
| 32 |
int j = (i + 1) % 3; |
| 33 |
int k = (i + 2) % 3; |
| 34 |
expect_float_eq(cross(pts[i], pts[j]), pts[k]); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
void test_parallel(const utils::Point& pt1, const utils::Point& pt2) |
| 39 |
{ |
| 40 |
EXPECT_FLOAT_EQ(dot(pt1, pt2) / (std::sqrt(dot(pt1, pt1) * std::sqrt(dot(pt2, pt2)))), 1); |
| 41 |
} |
| 42 |
} // namespace |
| 43 |
|
| 44 |
TEST(ChangeOfBasis, singleAxis) |
| 45 |
{ |
| 46 |
// test cases where the normal vector is along one of the coordinate axes |
| 47 |
utils::Point norm, p0; |
| 48 |
std::array<utils::Point, 3> basis; |
| 49 |
|
| 50 |
norm = utils::Point(1, 0, 0) * 2; |
| 51 |
p0 = utils::Point(1, 1, 1); |
| 52 |
basis = compute_basis(norm); |
| 53 |
expect_float_eq(basis[0], utils::Point(0, 1, 0)); |
| 54 |
expect_float_eq(basis[1], utils::Point(0, 0, 1)); |
| 55 |
expect_float_eq(basis[2], utils::Point(1, 0, 0)); |
| 56 |
|
| 57 |
norm = utils::Point(-1, 0, 0) * 2; |
| 58 |
p0 = utils::Point(1, 1, 1); |
| 59 |
basis = compute_basis(norm); |
| 60 |
expect_float_eq(basis[0], utils::Point(0, 0, 1)); |
| 61 |
expect_float_eq(basis[1], utils::Point(0, 1, 0)); |
| 62 |
expect_float_eq(basis[2], utils::Point(-1, 0, 0)); |
| 63 |
|
| 64 |
norm = utils::Point(0, 1, 0) * 2; |
| 65 |
p0 = utils::Point(1, 1, 1); |
| 66 |
basis = compute_basis(norm); |
| 67 |
expect_float_eq(basis[0], utils::Point(0, 0, 1)); |
| 68 |
expect_float_eq(basis[1], utils::Point(1, 0, 0)); |
| 69 |
expect_float_eq(basis[2], utils::Point(0, 1, 0)); |
| 70 |
|
| 71 |
norm = utils::Point(0, -1, 0) * 2; |
| 72 |
p0 = utils::Point(1, 1, 1); |
| 73 |
basis = compute_basis(norm); |
| 74 |
expect_float_eq(basis[0], utils::Point(1, 0, 0)); |
| 75 |
expect_float_eq(basis[1], utils::Point(0, 0, 1)); |
| 76 |
expect_float_eq(basis[2], utils::Point(0, -1, 0)); |
| 77 |
|
| 78 |
norm = utils::Point(0, 0, 1) * 2; |
| 79 |
p0 = utils::Point(1, 1, 1); |
| 80 |
basis = compute_basis(norm); |
| 81 |
expect_float_eq(basis[0], utils::Point(1, 0, 0)); |
| 82 |
expect_float_eq(basis[1], utils::Point(0, 1, 0)); |
| 83 |
expect_float_eq(basis[2], utils::Point(0, 0, 1)); |
| 84 |
|
| 85 |
norm = utils::Point(0, 0, -1) * 2; |
| 86 |
p0 = utils::Point(1, 1, 1); |
| 87 |
basis = compute_basis(norm); |
| 88 |
expect_float_eq(basis[0], utils::Point(0, 1, 0)); |
| 89 |
expect_float_eq(basis[1], utils::Point(1, 0, 0)); |
| 90 |
expect_float_eq(basis[2], utils::Point(0, 0, -1)); |
| 91 |
} |
| 92 |
|
| 93 |
TEST(ChangeOfBasis, twoAxis) |
| 94 |
{ |
| 95 |
// test cases where the normal vector is along two coordinate axes |
| 96 |
utils::Point norm; |
| 97 |
std::array<utils::Point, 3> basis; |
| 98 |
|
| 99 |
// xy |
| 100 |
norm = utils::Point(1, 1, 0) * 2; |
| 101 |
basis = compute_basis(norm); |
| 102 |
test_orthonormal(basis); |
| 103 |
test_parallel(norm, basis[2]); |
| 104 |
|
| 105 |
norm = utils::Point(1, -1, 0) * 2; |
| 106 |
basis = compute_basis(norm); |
| 107 |
test_orthonormal(basis); |
| 108 |
test_parallel(norm, basis[2]); |
| 109 |
|
| 110 |
norm = utils::Point(-1, 1, 0) * 2; |
| 111 |
basis = compute_basis(norm); |
| 112 |
test_orthonormal(basis); |
| 113 |
test_parallel(norm, basis[2]); |
| 114 |
|
| 115 |
norm = utils::Point(-1, -1, 0) * 2; |
| 116 |
basis = compute_basis(norm); |
| 117 |
test_orthonormal(basis); |
| 118 |
test_parallel(norm, basis[2]); |
| 119 |
|
| 120 |
// yz |
| 121 |
norm = utils::Point(0, 1, 1) * 2; |
| 122 |
basis = compute_basis(norm); |
| 123 |
test_orthonormal(basis); |
| 124 |
test_parallel(norm, basis[2]); |
| 125 |
|
| 126 |
norm = utils::Point(0, 1, -1) * 2; |
| 127 |
basis = compute_basis(norm); |
| 128 |
test_orthonormal(basis); |
| 129 |
test_parallel(norm, basis[2]); |
| 130 |
|
| 131 |
norm = utils::Point(0, -1, 1) * 2; |
| 132 |
basis = compute_basis(norm); |
| 133 |
test_orthonormal(basis); |
| 134 |
test_parallel(norm, basis[2]); |
| 135 |
|
| 136 |
norm = utils::Point(0, -1, -1) * 2; |
| 137 |
basis = compute_basis(norm); |
| 138 |
test_orthonormal(basis); |
| 139 |
test_parallel(norm, basis[2]); |
| 140 |
|
| 141 |
// xz |
| 142 |
norm = utils::Point(1, 0, 1) * 2; |
| 143 |
basis = compute_basis(norm); |
| 144 |
test_orthonormal(basis); |
| 145 |
test_parallel(norm, basis[2]); |
| 146 |
|
| 147 |
norm = utils::Point(1, 0, -1) * 2; |
| 148 |
basis = compute_basis(norm); |
| 149 |
test_orthonormal(basis); |
| 150 |
test_parallel(norm, basis[2]); |
| 151 |
|
| 152 |
norm = utils::Point(-1, 0, 1) * 2; |
| 153 |
basis = compute_basis(norm); |
| 154 |
test_orthonormal(basis); |
| 155 |
test_parallel(norm, basis[2]); |
| 156 |
|
| 157 |
norm = utils::Point(-1, 0, -1) * 2; |
| 158 |
basis = compute_basis(norm); |
| 159 |
test_orthonormal(basis); |
| 160 |
test_parallel(norm, basis[2]); |
| 161 |
} |
| 162 |
|
| 163 |
TEST(ChangeOfBasis, threeAxis) |
| 164 |
{ |
| 165 |
// general case |
| 166 |
std::vector<utils::Point> pts{utils::Point(1, 1, 1) * 2, utils::Point(-1, 1, 1) * 2, utils::Point(1, -1, 1) * 2, |
| 167 |
utils::Point(-1, -1, 1) * 2, utils::Point(1, 1, -1) * 2, utils::Point(1, 1, -1) * 2, |
| 168 |
utils::Point(1, -1, 1) * 2, utils::Point(-1, -1, -1) * 2}; |
| 169 |
|
| 170 |
for (auto& norm : pts) |
| 171 |
{ |
| 172 |
auto basis = compute_basis(norm); |
| 173 |
test_orthonormal(basis); |
| 174 |
test_parallel(norm, basis[2]); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
TEST(ChangeOfBasis, projection) |
| 179 |
{ |
| 180 |
// general case |
| 181 |
std::vector<utils::Point> normals{ |
| 182 |
utils::Point(1, 1, 1) * 2, utils::Point(-1, 1, 1) * 2, utils::Point(1, -1, 1) * 2, utils::Point(-1, -1, 1) * 2, |
| 183 |
utils::Point(1, 1, -1) * 2, utils::Point(1, 1, -1) * 2, utils::Point(1, -1, 1) * 2, utils::Point(-1, -1, -1) * 2}; |
| 184 |
|
| 185 |
utils::Point pt(2, 3, 4); |
| 186 |
for (auto& norm : normals) |
| 187 |
{ |
| 188 |
auto basis = compute_basis(norm); |
| 189 |
utils::impl::ChangeOfBasis proj(basis); |
| 190 |
auto ptPrime = proj.project_forward(pt); |
| 191 |
auto pt2 = proj.project_back(ptPrime); |
| 192 |
|
| 193 |
expect_float_eq(pt, pt2); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
} // namespace impl |
| 198 |
} // namespace middle_mesh |
| 199 |
} // namespace stk |