forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolygonUtils.test.cc
146 lines (122 loc) · 4.4 KB
/
PolygonUtils.test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orangeinp/detail/PolygonUtils.test.cc
//---------------------------------------------------------------------------//
#include "orange/orangeinp/detail/PolygonUtils.hh"
#include <vector>
#include "corecel/Constants.hh"
#include "corecel/math/ArrayUtils.hh"
#include "corecel/math/SoftEqual.hh"
#include "celeritas_test.hh"
namespace celeritas
{
namespace orangeinp
{
namespace detail
{
namespace test
{
//---------------------------------------------------------------------------//
using Real2 = Array<real_type, 2>;
using VecReal2 = std::vector<Real2>;
using constants::pi;
//---------------------------------------------------------------------------//
// TESTS
//---------------------------------------------------------------------------//
TEST(PolygonUtilsTest, calc_orientation)
{
EXPECT_TRUE(calc_orientation(Real2{0, 0}, Real2{4, 4}, Real2{1, 2})
== Orientation::counterclockwise);
EXPECT_TRUE(calc_orientation(Real2{0, 0}, Real2{4, 4}, Real2{2, 1})
== Orientation::clockwise);
EXPECT_TRUE(calc_orientation(Real2{0, 0}, Real2{4, 4}, Real2{2, 2})
== Orientation::collinear);
}
TEST(PolygonUtilsTest, has_orientation)
{
EXPECT_TRUE(has_orientation(
make_span(VecReal2{{-19, -30}, {-19, 30}, {21, 30}, {21, -30}}),
Orientation::clockwise));
EXPECT_FALSE(has_orientation(
make_span(VecReal2{{-19, -30}, {-19, 30}, {21, 30}, {21, -30}}),
Orientation::counterclockwise));
EXPECT_TRUE(has_orientation(
make_span(VecReal2{{-2, -2}, {0, -2}, {0, 0}, {-2, 0}}),
Orientation::counterclockwise));
}
TEST(PolygonUtilsTest, convexity)
{
VecReal2 cw{{1, 1}, {1, 2}, {2, 2}, {2, 1}};
EXPECT_TRUE(is_convex(make_span(cw)));
Real2 ccw[] = {{1, 1}, {2, 1}, {2, 2}, {1, 2}};
EXPECT_TRUE(is_convex(ccw));
VecReal2 oct{8};
for (size_type i = 0; i < 8; ++i)
{
oct[i] = {std::cos(2 * pi * i / 8), std::sin(2 * pi * i / 8)};
}
EXPECT_TRUE(is_convex(make_span(oct)));
// not properly ordered
Real2 bad[] = {{1, 1}, {2, 2}, {2, 1}, {1, 2}};
EXPECT_FALSE(is_convex(bad));
}
TEST(PolygonUtilsTest, degenerate)
{
// degenerate: all points are colinear
Real2 line[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
EXPECT_FALSE(is_convex(line));
// only three points are collinear
Real2 degen[] = {{1, 1}, {2, 2}, {3, 3}, {2, 4}};
EXPECT_FALSE(is_convex(degen));
EXPECT_TRUE(is_convex(degen, /* degen_ok = */ true));
// degenerate: repeated consecutive points
Real2 repeated[] = {{0, 0}, {1, 0}, {1, 1}, {0.5, 0.5}, {0.5, 0.5}, {0, 1}};
EXPECT_FALSE(is_convex(repeated));
}
TEST(PolygonUtilsTest, self_intersect)
{
Real2 self_int[] = {{0, 0}, {1, 1}, {1, 0}, {0, 1}};
EXPECT_FALSE(is_convex(self_int));
Real2 self_int2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}};
EXPECT_FALSE(is_convex(self_int2));
}
TEST(PolygonUtilsTest, planar)
{
Real3 a{-2, 2, -2}, b{2, 2, -2}, c{2, 2, 2}, d{-2, 2, 2};
EXPECT_TRUE(is_planar(a, b, c, d));
EXPECT_TRUE(is_planar(a, b, d, c)); // proper ordering not required
EXPECT_FALSE(is_planar(a, b, c, Real3{0, 0, 0}));
}
TEST(PolygonUtilsTest, planar_tolerance)
{
real_type const eps_lo = real_type{0.1} * SoftEqual<>{}.rel();
real_type const eps_hi = 2 * SoftEqual<>{}.rel();
Real3 a{-2, 2, -2}, b{2, 2, -2}, c{2, 2, 2}, d{-2, 2, 2};
Real3 dy{0, 1, 0};
// effect on reference corner
Real3 aa{a};
axpy(eps_lo, dy, &aa);
EXPECT_TRUE(is_planar(aa, b, c, d));
axpy(eps_hi, dy, &aa);
EXPECT_FALSE(is_planar(aa, b, c, d));
// effect on non-ref corner
Real3 bb{b};
axpy(eps_lo, dy, &bb);
EXPECT_TRUE(is_planar(a, bb, c, d));
axpy(eps_hi, dy, &bb);
EXPECT_FALSE(is_planar(a, bb, c, d));
// effect on test corner
Real3 dd{d};
axpy(eps_lo, dy, &dd);
EXPECT_TRUE(is_planar(a, b, c, dd));
axpy(eps_hi, dy, &dd);
EXPECT_FALSE(is_planar(a, b, c, dd));
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace detail
} // namespace orangeinp
} // namespace celeritas