1 // Copyright (c) 2017-2021, Lawrence Livermore National Security, LLC and
2 // other Axom Project Developers. See the top-level LICENSE file for details.
3 //
4 // SPDX-License-Identifier: (BSD-3-Clause)
5
6 #include "gtest/gtest.h"
7
8 #include "axom/slic/interface/slic.hpp"
9
10 #include "axom/primal/operators/detail/intersect_impl.hpp"
11
12 using namespace axom;
13
TEST(primal_intersection_impl,fuzzy_comparisons)14 TEST(primal_intersection_impl, fuzzy_comparisons)
15 {
16 const double eps = 0.1;
17
18 SLIC_INFO("This test demonstrates the fuzzy comparison"
19 << " operators used in quest's intersection tests"
20 << " using a large epsilon (" << eps << ")");
21
22 // Testing 'greater than' function
23 // Note: Excludes the eps window
24 EXPECT_TRUE(primal::detail::isGt(1.15, 1.0, eps));
25 // --
26 EXPECT_FALSE(primal::detail::isGt(1.05, 1.0, eps));
27 EXPECT_FALSE(primal::detail::isGt(1.0, 1.0, eps));
28 EXPECT_FALSE(primal::detail::isGt(0.95, 1.0, eps));
29 EXPECT_FALSE(primal::detail::isGt(0.85, 1.0, eps));
30
31 // Testing 'less than or equal' function
32 // Note: Includes eps window
33 // Note: isLeq() has opposite value of isGt()
34 EXPECT_FALSE(primal::detail::isLeq(1.15, 1.0, eps));
35 // --
36 EXPECT_TRUE(primal::detail::isLeq(1.05, 1.0, eps));
37 EXPECT_TRUE(primal::detail::isLeq(1.0, 1.0, eps));
38 EXPECT_TRUE(primal::detail::isLeq(0.95, 1.0, eps));
39 EXPECT_TRUE(primal::detail::isLeq(0.85, 1.0, eps));
40
41 // Testing 'less than' function
42 // Note: Excludes eps window
43 EXPECT_FALSE(primal::detail::isLt(1.15, 1.0, eps));
44 EXPECT_FALSE(primal::detail::isLt(1.05, 1.0, eps));
45 EXPECT_FALSE(primal::detail::isLt(1.0, 1.0, eps));
46 EXPECT_FALSE(primal::detail::isLt(0.95, 1.0, eps));
47 // --
48 EXPECT_TRUE(primal::detail::isLt(0.85, 1.0, eps));
49
50 // Testing 'greater than or equal' function
51 // Note: Includes eps window
52 // Note: isGeq() has opposite value of isLt()
53 EXPECT_TRUE(primal::detail::isGeq(1.15, 1.0, eps));
54 EXPECT_TRUE(primal::detail::isGeq(1.05, 1.0, eps));
55 EXPECT_TRUE(primal::detail::isGeq(1.0, 1.0, eps));
56 EXPECT_TRUE(primal::detail::isGeq(0.95, 1.0, eps));
57 // --
58 EXPECT_FALSE(primal::detail::isGeq(0.85, 1.0, eps));
59
60 EXPECT_FALSE(primal::detail::isLpeq(1.15, 1.0, true, eps));
61 // --
62 EXPECT_TRUE(primal::detail::isLpeq(1.05, 1.0, true, eps));
63 EXPECT_TRUE(primal::detail::isLpeq(1.0, 1.0, true, eps));
64 EXPECT_TRUE(primal::detail::isLpeq(0.95, 1.0, true, eps));
65 EXPECT_TRUE(primal::detail::isLpeq(0.85, 1.0, true, eps));
66
67 EXPECT_TRUE(primal::detail::isGpeq(1.15, 1.0, true, eps));
68 EXPECT_TRUE(primal::detail::isGpeq(1.05, 1.0, true, eps));
69 EXPECT_TRUE(primal::detail::isGpeq(1.0, 1.0, true, eps));
70 EXPECT_TRUE(primal::detail::isGpeq(0.95, 1.0, true, eps));
71 // --
72 EXPECT_FALSE(primal::detail::isGpeq(0.85, 1.0, true, eps));
73
74 EXPECT_FALSE(primal::detail::isLpeq(1.15, 1.0, false, eps));
75 EXPECT_FALSE(primal::detail::isLpeq(1.05, 1.0, false, eps));
76 EXPECT_FALSE(primal::detail::isLpeq(1.0, 1.0, false, eps));
77 EXPECT_FALSE(primal::detail::isLpeq(0.95, 1.0, false, eps));
78 // --
79 EXPECT_TRUE(primal::detail::isLpeq(0.85, 1.0, false, eps));
80
81 EXPECT_TRUE(primal::detail::isGpeq(1.15, 1.0, false, eps));
82 // --
83 EXPECT_FALSE(primal::detail::isGpeq(1.05, 1.0, false, eps));
84 EXPECT_FALSE(primal::detail::isGpeq(1.0, 1.0, false, eps));
85 EXPECT_FALSE(primal::detail::isGpeq(0.95, 1.0, false, eps));
86 EXPECT_FALSE(primal::detail::isGpeq(0.85, 1.0, false, eps));
87 }
88
TEST(primal_intersection_impl,zero_count)89 TEST(primal_intersection_impl, zero_count)
90 {
91 const double eps = 0.1;
92
93 int expectedCount = 0;
94
95 EXPECT_EQ(primal::detail::countZeros(1, 1, 1, eps), expectedCount);
96 EXPECT_EQ(primal::detail::countZeros(-1, 1, -5, eps), expectedCount);
97 EXPECT_EQ(primal::detail::countZeros(0.15, -0.15, 1.3, eps), expectedCount);
98
99 expectedCount = 1;
100
101 EXPECT_EQ(primal::detail::countZeros(-1, 1, 0, eps), expectedCount);
102 EXPECT_EQ(primal::detail::countZeros(-0.2, -0.05, 3, eps), expectedCount);
103 EXPECT_EQ(primal::detail::countZeros(0.07, -0.5, 3, eps), expectedCount);
104
105 expectedCount = 2;
106
107 EXPECT_EQ(primal::detail::countZeros(1, 0.05, 0, eps), expectedCount);
108 EXPECT_EQ(primal::detail::countZeros(0.05, -0.02, -0.12, eps), expectedCount);
109 EXPECT_EQ(primal::detail::countZeros(0.07, -0.5, 0, eps), expectedCount);
110
111 expectedCount = 3;
112
113 EXPECT_EQ(primal::detail::countZeros(0.01, 0.05, 0, eps), expectedCount);
114 EXPECT_EQ(primal::detail::countZeros(-0.01, -0.02, 0.05, eps), expectedCount);
115
116 EXPECT_TRUE(primal::detail::oneZeroOthersMatch(0, 1, 0.2, eps));
117 EXPECT_TRUE(primal::detail::oneZeroOthersMatch(-1, 0.02, -9, eps));
118 EXPECT_TRUE(primal::detail::oneZeroOthersMatch(-1, -0.9, -0.05, eps));
119 EXPECT_FALSE(primal::detail::oneZeroOthersMatch(0, -0.2, 0.2, eps));
120 EXPECT_FALSE(primal::detail::oneZeroOthersMatch(-1, 0.02, 5, eps));
121 EXPECT_FALSE(primal::detail::oneZeroOthersMatch(-1, 0.02, 0.05, eps));
122 EXPECT_FALSE(primal::detail::oneZeroOthersMatch(-1, 2, 5, eps));
123 EXPECT_FALSE(primal::detail::oneZeroOthersMatch(-0.01, 0.05, 0, eps));
124
125 EXPECT_TRUE(primal::detail::twoZeros(0, 0, 1, eps));
126 EXPECT_TRUE(primal::detail::twoZeros(0.05, -0.05, 1, eps));
127 EXPECT_TRUE(primal::detail::twoZeros(-0.05, -1.5, 0.05, eps));
128 EXPECT_TRUE(primal::detail::twoZeros(1, 0.05, 0.01, eps));
129 EXPECT_FALSE(primal::detail::twoZeros(0, 0.05, 0.01, eps));
130 EXPECT_FALSE(primal::detail::twoZeros(0, 0.05, 0.01, eps));
131 EXPECT_FALSE(primal::detail::twoZeros(1, 0.05, 0.15, eps));
132 EXPECT_FALSE(primal::detail::twoZeros(1, 5, 0.15, eps));
133 }
134
135 //------------------------------------------------------------------------------
136 //------------------------------------------------------------------------------
137 #include "axom/slic/core/SimpleLogger.hpp"
138 using axom::slic::SimpleLogger;
139
main(int argc,char * argv[])140 int main(int argc, char* argv[])
141 {
142 int result = 0;
143
144 ::testing::InitGoogleTest(&argc, argv);
145
146 // create & initialize test logger, finalized when exiting main scope
147 SimpleLogger logger;
148
149 result = RUN_ALL_TESTS();
150
151 return result;
152 }
153