| 1 |
#include <complex> |
| 2 |
#include "gtest/gtest.h" |
| 3 |
|
| 4 |
#include "stk_middle_mesh/point.hpp" |
| 5 |
|
| 6 |
namespace stk { |
| 7 |
namespace middle_mesh { |
| 8 |
namespace impl { |
| 9 |
|
| 10 |
namespace { |
| 11 |
|
| 12 |
using Complex = std::complex<double>; |
| 13 |
using Point = utils::Point; |
| 14 |
using PointC = utils::PointT<Complex>; |
| 15 |
|
| 16 |
void test_float_eq(const Point& p1, const Point& p2) |
| 17 |
{ |
| 18 |
EXPECT_FLOAT_EQ(p1.x, p2.x); |
| 19 |
EXPECT_FLOAT_EQ(p1.y, p2.y); |
| 20 |
EXPECT_FLOAT_EQ(p1.z, p2.z); |
| 21 |
} |
| 22 |
|
| 23 |
void test_float_eq(const PointC& p1, const PointC& p2) |
| 24 |
{ |
| 25 |
for (int i=0; i < 3; ++i) |
| 26 |
{ |
| 27 |
EXPECT_FLOAT_EQ(p1[i].real(), p2[i].real()); |
| 28 |
EXPECT_FLOAT_EQ(p1[i].imag(), p2[i].imag()); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
void expect_near(const Complex& a, const Complex& b, double tol) |
| 33 |
{ |
| 34 |
EXPECT_NEAR(a.real(), b.real(), tol); |
| 35 |
EXPECT_NEAR(a.imag(), b.imag(), tol); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
} // namespace |
| 40 |
|
| 41 |
TEST(Point, Operations) |
| 42 |
{ |
| 43 |
Point p1(1, 2, 3); |
| 44 |
Point p2(4, 5, 6); |
| 45 |
Point p3(6, 5, 4); |
| 46 |
|
| 47 |
test_float_eq(p1 + p2, Point(5, 7, 9)); |
| 48 |
test_float_eq(p1 - p3, Point(-5, -3, -1)); |
| 49 |
|
| 50 |
test_float_eq(p1 * 2, Point(2, 4, 6)); |
| 51 |
test_float_eq(2 * p1, Point(2, 4, 6)); |
| 52 |
|
| 53 |
test_float_eq(p1 / 2, Point(0.5, 1, 1.5)); |
| 54 |
|
| 55 |
EXPECT_FLOAT_EQ(dot(p1, p2), 1 * 4 + 2 * 5 + 3 * 6); |
| 56 |
test_float_eq(cross(p1, p2), Point(-3, 6, -3)); |
| 57 |
|
| 58 |
auto p4 = project(p1, p2); |
| 59 |
EXPECT_FLOAT_EQ(dot(p4, p2), std::sqrt(dot(p4, p4)) * std::sqrt(dot(p2, p2))); |
| 60 |
|
| 61 |
test_float_eq(-p1, Point(-p1.x, -p1.y, -p1.z)); |
| 62 |
|
| 63 |
auto ptmp = p1; |
| 64 |
ptmp += p2; |
| 65 |
test_float_eq(ptmp, p1 + p2); |
| 66 |
|
| 67 |
ptmp = p1; |
| 68 |
ptmp -= p2; |
| 69 |
test_float_eq(ptmp, p1 - p2); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(Point, Addition) |
| 73 |
{ |
| 74 |
Point p_real(1, 2, 3); |
| 75 |
PointC p_complex({4, 5}, {6, 7}, {8, 9}); |
| 76 |
PointC p_complex2({10, 11}, {12, 13}, {14, 15}); |
| 77 |
|
| 78 |
|
| 79 |
static_assert(std::is_same_v<decltype(p_real + p_complex), PointC>); |
| 80 |
test_float_eq(p_real + p_complex, PointC({5, 5}, {8, 7}, {11, 9})); |
| 81 |
|
| 82 |
static_assert(std::is_same_v<decltype(p_complex + p_complex2), PointC>); |
| 83 |
test_float_eq(p_complex + p_complex2, PointC({14, 16}, {18, 20}, {22, 24})); |
| 84 |
} |
| 85 |
|
| 86 |
TEST(Point, Subtraction) |
| 87 |
{ |
| 88 |
Point p_real(1, 2, 3); |
| 89 |
PointC p_complex({4, 5}, {6, 7}, {8, 9}); |
| 90 |
PointC p_complex2({10, 11}, {12, 13}, {14, 15}); |
| 91 |
|
| 92 |
|
| 93 |
static_assert(std::is_same_v<decltype(p_real - p_complex), PointC>); |
| 94 |
test_float_eq(p_real - p_complex, PointC({-3, -5}, {-4, -7}, {-5, -9})); |
| 95 |
|
| 96 |
static_assert(std::is_same_v<decltype(p_complex - p_complex2), PointC>); |
| 97 |
test_float_eq(p_complex - p_complex2, PointC({-6, -6}, {-6, -6}, {-6, -6})); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
TEST(Point, MultiplyByScalar) |
| 102 |
{ |
| 103 |
Point p_real(1, 2, 3); |
| 104 |
PointC p_complex({4, 5}, {6, 7}, {8, 9}); |
| 105 |
|
| 106 |
|
| 107 |
static_assert(std::is_same_v<decltype(p_real * 2.0), Point>); |
| 108 |
test_float_eq(p_real * 2.0, Point(2, 4, 6)); |
| 109 |
|
| 110 |
static_assert(std::is_same_v<decltype(p_real * 2), Point>); |
| 111 |
test_float_eq(p_real * 2, Point(2, 4, 6)); |
| 112 |
|
| 113 |
|
| 114 |
static_assert(std::is_same_v<decltype(p_complex * 2.0), PointC>); |
| 115 |
test_float_eq(p_complex * 2.0, PointC({8, 10}, {12, 14}, {16, 18})); |
| 116 |
|
| 117 |
static_assert(std::is_same_v<decltype(p_complex * 2), PointC>); |
| 118 |
test_float_eq(p_complex * 2, PointC({8, 10}, {12, 14}, {16, 18})); |
| 119 |
} |
| 120 |
|
| 121 |
TEST(Point, Division) |
| 122 |
{ |
| 123 |
Point p_real(1, 2, 3); |
| 124 |
PointC p_complex({4, 5}, {6, 7}, {8, 9}); |
| 125 |
|
| 126 |
static_assert(std::is_same_v<decltype(p_real / 2.0), Point>); |
| 127 |
test_float_eq(p_real / 2.0, Point(0.5, 1, 1.5)); |
| 128 |
|
| 129 |
static_assert(std::is_same_v<decltype(p_real * 2), Point>); |
| 130 |
test_float_eq(p_real / 2, Point(0.5, 1, 1.5)); |
| 131 |
|
| 132 |
|
| 133 |
static_assert(std::is_same_v<decltype(p_complex / 2.0), PointC>); |
| 134 |
test_float_eq(p_complex / 2.0, PointC({2, 2.5}, {3, 3.5}, {4, 4.5})); |
| 135 |
|
| 136 |
static_assert(std::is_same_v<decltype(p_complex / 2), PointC>); |
| 137 |
test_float_eq(p_complex / 2, PointC({2, 2.5}, {3, 3.5}, {4, 4.5})); |
| 138 |
} |
| 139 |
|
| 140 |
TEST(Point, DotProduct) |
| 141 |
{ |
| 142 |
Point p_real(1, 2, 3); |
| 143 |
PointC p_complex({4, 5}, {6, 7}, {8, 9}); |
| 144 |
|
| 145 |
static_assert(std::is_same_v<decltype(dot(p_real, p_complex)), Complex>); |
| 146 |
expect_near(dot(p_real, p_complex), Complex(40, 46), 1e-13); |
| 147 |
|
| 148 |
static_assert(std::is_same_v<decltype(dot(p_complex, p_real)), Complex>); |
| 149 |
expect_near(dot(p_complex, p_real), Complex(40, 46), 1e-13); |
| 150 |
} |
| 151 |
|
| 152 |
TEST(Point, Dot_rev) |
| 153 |
{ |
| 154 |
Point p1(1, 2, 3), p2(4, 6, 9), p1Bar, p2Bar; |
| 155 |
|
| 156 |
dot_rev(p1, p2, p1Bar, p2Bar, 1); |
| 157 |
|
| 158 |
// compute finite difference |
| 159 |
auto v = dot(p1, p2); |
| 160 |
double eps = 1e-7; |
| 161 |
|
| 162 |
p1.x += eps; |
| 163 |
double dvDp1x = (dot(p1, p2) - v) / eps; |
| 164 |
p1.x -= eps; |
| 165 |
|
| 166 |
p1.y += eps; |
| 167 |
double dvDp1y = (dot(p1, p2) - v) / eps; |
| 168 |
p1.y -= eps; |
| 169 |
|
| 170 |
p1.z += eps; |
| 171 |
double dvDp1z = (dot(p1, p2) - v) / eps; |
| 172 |
p1.z -= eps; |
| 173 |
|
| 174 |
p2.x += eps; |
| 175 |
double dvDp2x = (dot(p1, p2) - v) / eps; |
| 176 |
p2.x -= eps; |
| 177 |
|
| 178 |
p2.y += eps; |
| 179 |
double dvDp2y = (dot(p1, p2) - v) / eps; |
| 180 |
p2.y -= eps; |
| 181 |
|
| 182 |
p2.z += eps; |
| 183 |
double dvDp2z = (dot(p1, p2) - v) / eps; |
| 184 |
p2.z -= eps; |
| 185 |
|
| 186 |
EXPECT_NEAR(dvDp1x, p1Bar.x, 1e-6); |
| 187 |
EXPECT_NEAR(dvDp1y, p1Bar.y, 1e-6); |
| 188 |
EXPECT_NEAR(dvDp1z, p1Bar.z, 1e-6); |
| 189 |
EXPECT_NEAR(dvDp2x, p2Bar.x, 1e-6); |
| 190 |
EXPECT_NEAR(dvDp2y, p2Bar.y, 1e-6); |
| 191 |
EXPECT_NEAR(dvDp2z, p2Bar.z, 1e-6); |
| 192 |
} |
| 193 |
|
| 194 |
TEST(Point, CrossProduct) |
| 195 |
{ |
| 196 |
Point p_real(1, 2, 3); |
| 197 |
PointC p_complex({4, 5}, {6, 7}, {8, 9}); |
| 198 |
|
| 199 |
static_assert(std::is_same_v<decltype(cross(p_real, p_complex)), PointC>); |
| 200 |
test_float_eq(cross(p_real, p_complex), PointC({-2, -3}, {4, 6}, {-2, -3})); |
| 201 |
|
| 202 |
static_assert(std::is_same_v<decltype(cross(p_complex, p_real)), PointC>); |
| 203 |
test_float_eq(cross(p_complex, p_real), PointC({2, 3}, {-4, -6}, {2, 3})); |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
} |
| 208 |
} |