preCICE v3.1.2
Loading...
Searching...
No Matches
TriangleTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <iterator>
3#include <sstream>
4#include <string>
5#include "logging/Logger.hpp"
6#include "mesh/Edge.hpp"
8#include "mesh/Triangle.hpp"
9#include "mesh/Vertex.hpp"
11#include "testing/Testing.hpp"
12
13using namespace precice;
14using namespace precice::mesh;
15
16BOOST_AUTO_TEST_SUITE(MeshTests)
17BOOST_AUTO_TEST_SUITE(TriangleTests)
18
19BOOST_AUTO_TEST_CASE(DirectionalEdges)
20{
21 PRECICE_TEST(1_rank);
22 using Eigen::Vector3d;
23 Vector3d coords1(0.0, 0.0, 0.0);
24 Vector3d coords2(1.0, 0.0, 0.0);
25 Vector3d coords3(1.0, 1.0, 0.0);
26
27 Vertex v1(coords1, 0);
28 Vertex v2(coords2, 1);
29 Vertex v3(coords3, 2);
30
31 Edge e1(v1, v2);
32 Edge e2(v2, v3);
33 Edge e3(v3, v1);
34
35 Triangle triangle(e1, e2, e3);
36
37 Vertex &v1ref = triangle.vertex(0);
38 BOOST_TEST(v1ref.getID() == v1.getID());
39
40 Vertex &v2ref = triangle.vertex(1);
41 BOOST_TEST(v2ref.getID() == v2.getID());
42
43 Vertex &v3ref = triangle.vertex(2);
44 BOOST_TEST(v3ref.getID() == v3.getID());
45
46 Vector3d normal = triangle.computeNormal();
47 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
48 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
49
50 Vector3d center = triangle.getCenter();
51 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
52
53 constexpr double expectedRadius = 0.74535599249993001;
54 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
55
56 constexpr double expectedArea = 0.5;
57 BOOST_TEST(triangle.getArea() == expectedArea);
58}
59
61{
62 PRECICE_TEST(1_rank);
63 using Eigen::Vector3d;
64 Vector3d coords1(0.0, 0.0, 0.0);
65 Vector3d coords2(1.0, 0.0, 0.0);
66 Vector3d coords3(1.0, 1.0, 0.0);
67 Vertex v1(coords1, 0);
68 Vertex v2(coords2, 1);
69 Vertex v3(coords3, 2);
70
71 Edge e1(v1, v2);
72 Edge e2(v3, v2);
73 Edge e3(v3, v1);
74
75 Triangle triangle(e1, e2, e3);
76
77 Vertex &v1ref = triangle.vertex(0);
78 BOOST_TEST(v1ref.getID() == v1.getID());
79
80 Vertex &v2ref = triangle.vertex(1);
81 BOOST_TEST(v2ref.getID() == v2.getID());
82
83 Vertex &v3ref = triangle.vertex(2);
84 BOOST_TEST(v3ref.getID() == v3.getID());
85
86 Vector3d normal = triangle.computeNormal();
87 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
88 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
89
90 Vector3d center = triangle.getCenter();
91 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
92
93 constexpr double expectedRadius = 0.74535599249993001;
94 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
95
96 constexpr double expectedArea = 0.5;
97 BOOST_TEST(triangle.getArea() == expectedArea);
98}
99
100BOOST_AUTO_TEST_CASE(ReversedFirstFlipped)
101{
102 PRECICE_TEST(1_rank);
103 using Eigen::Vector3d;
104 Vector3d coords1(0.0, 0.0, 0.0);
105 Vector3d coords2(1.0, 0.0, 0.0);
106 Vector3d coords3(1.0, 1.0, 0.0);
107
108 Vertex v1(coords1, 0);
109 Vertex v2(coords2, 1);
110 Vertex v3(coords3, 2);
111
112 Edge e1(v1, v2);
113 Edge e2(v3, v2);
114 Edge e3(v1, v3);
115
116 Triangle triangle(e1, e2, e3);
117
118 Vertex &v1ref = triangle.vertex(0);
119 BOOST_TEST(v1ref.getID() == v1.getID());
120
121 Vertex &v2ref = triangle.vertex(1);
122 BOOST_TEST(v2ref.getID() == v2.getID());
123
124 Vertex &v3ref = triangle.vertex(2);
125 BOOST_TEST(v3ref.getID() == v3.getID());
126
127 Vector3d normal = triangle.computeNormal();
128 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
129 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
130
131 Vector3d center = triangle.getCenter();
132 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
133
134 constexpr double expectedRadius = 0.74535599249993001;
135 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
136
137 constexpr double expectedArea = 0.5;
138 BOOST_TEST(triangle.getArea() == expectedArea);
139}
140
141BOOST_AUTO_TEST_CASE(ReversedLastFlipped)
142{
143 PRECICE_TEST(1_rank);
144 using Eigen::Vector3d;
145 Vector3d coords1(0.0, 0.0, 0.0);
146 Vector3d coords2(1.0, 0.0, 0.0);
147 Vector3d coords3(1.0, 1.0, 0.0);
148
149 Vertex v1(coords1, 0);
150 Vertex v2(coords2, 1);
151 Vertex v3(coords3, 2);
152
153 Edge e1(v1, v2);
154 Edge e2(v3, v2);
155 Edge e3(v3, v1);
156
157 Triangle triangle(e1, e3, e2);
158
159 Vertex &v1ref = triangle.vertex(0);
160 BOOST_TEST(v1ref.getID() == v1.getID());
161
162 Vertex &v2ref = triangle.vertex(1);
163 BOOST_TEST(v2ref.getID() == v2.getID());
164
165 Vertex &v3ref = triangle.vertex(2);
166 BOOST_TEST(v3ref.getID() == v3.getID());
167
168 Vector3d normal = triangle.computeNormal();
169 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
170 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
171
172 Vector3d center = triangle.getCenter();
173 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
174
175 constexpr double expectedRadius = 0.74535599249993001;
176 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
177
178 constexpr double expectedArea = 0.5;
179 BOOST_TEST(triangle.getArea() == expectedArea);
180}
181
183{
184 PRECICE_TEST(1_rank);
185 using Eigen::Vector3d;
186 Vector3d coords1(0.0, 0.0, 0.0);
187 Vector3d coords2(1.0, 0.0, 0.0);
188 Vector3d coords3(1.0, 1.0, 0.0);
189
190 Vertex v0(coords1, 0);
191 Vertex v1(coords2, 1);
192 Vertex v2(coords3, 2);
193
194 Edge e0(v0, v1);
195 Edge e1(v1, v2);
196 Edge e2(v2, v0);
197
198 Triangle triangle(e0, e1, e2);
199
200 {
201 // Test begin(), end()
202 auto ibegin = triangle.begin();
203 const auto iend = triangle.end();
204 BOOST_TEST(std::distance(ibegin, iend) == 3);
205 BOOST_TEST(*ibegin == v0.rawCoords());
206 ++ibegin;
207 BOOST_TEST(*ibegin == v1.rawCoords());
208 ++ibegin;
209 BOOST_TEST(*ibegin == v2.rawCoords());
210 ++ibegin;
211 BOOST_TEST((ibegin == iend));
212 }
213 {
214 // Test begin(), end() for const
215 const Triangle &ctriangle = triangle;
216 auto ibegin = ctriangle.begin();
217 const auto iend = ctriangle.end();
218 BOOST_TEST(std::distance(ibegin, iend) == 3);
219 BOOST_TEST(*ibegin == v0.rawCoords());
220 ++ibegin;
221 BOOST_TEST(*ibegin == v1.rawCoords());
222 ++ibegin;
223 BOOST_TEST(*ibegin == v2.rawCoords());
224 ++ibegin;
225 BOOST_TEST((ibegin == iend));
226 }
227 {
228 // Test cbegin(), cend()
229 auto ibegin = triangle.cbegin();
230 const auto iend = triangle.cend();
231 BOOST_TEST(std::distance(ibegin, iend) == 3);
232 BOOST_TEST(*ibegin == v0.rawCoords());
233 ++ibegin;
234 BOOST_TEST(*ibegin == v1.rawCoords());
235 ++ibegin;
236 BOOST_TEST(*ibegin == v2.rawCoords());
237 ++ibegin;
238 BOOST_TEST((ibegin == iend));
239 }
240}
241
242BOOST_AUTO_TEST_CASE(TriangleEquality)
243{
244 PRECICE_TEST(1_rank);
245 using Eigen::Vector3d;
246 Vector3d coords1(0.0, 0.0, 0.0);
247 Vector3d coords2(1.0, 0.0, 0.0);
248 Vector3d coords3(1.0, 1.0, 0.0);
249 Vector3d coords4(2.0, 0.0, 0.0);
250
251 Vertex v1(coords1, 0);
252 Vertex v2(coords2, 1);
253 Vertex v3(coords3, 2);
254 Vertex v4(coords4, 0);
255
256 Edge e1(v1, v2);
257 Edge e2(v3, v2);
258 Edge e3(v3, v1);
259 Edge e4(v2, v4);
260 Edge e5(v4, v3);
261
262 // *
263 // * *
264 // ****
265 Triangle triangle1(e1, e3, e2);
266 Triangle triangle2(e1, e2, e3);
267 BOOST_TEST(triangle1 == triangle2);
268 // *
269 // * *
270 // ****
271 Triangle triangle3(e2, e4, e5);
272 Triangle triangle4(e2, e4, e5);
273 BOOST_TEST(triangle1 == triangle2);
274 BOOST_TEST(triangle1 != triangle3);
275 BOOST_TEST(triangle4 == triangle3);
276}
277
278BOOST_AUTO_TEST_CASE(TriangleWKTPrint)
279{
280 PRECICE_TEST(1_rank);
281 Vertex v1(Eigen::Vector3d(0., 0., 0.), 0);
282 Vertex v2(Eigen::Vector3d(0., 1., 0.), 0);
283 Vertex v3(Eigen::Vector3d(1., 0., 0.), 0);
284 Edge e1(v1, v2);
285 Edge e2(v2, v3);
286 Edge e3(v3, v1);
287 Triangle t1(e1, e2, e3);
288 std::stringstream stream;
289 stream << t1;
290 std::string t1string("POLYGON ((0 0 0, 0 1 0, 1 0 0, 0 0 0))");
291 BOOST_TEST(t1string == stream.str());
292}
293
294BOOST_AUTO_TEST_SUITE_END() // Triangle
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST(...)
Definition Testing.hpp:27
BOOST_AUTO_TEST_CASE(DirectionalEdges)
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:16
Triangle of a mesh, defined by three vertices.
Definition Triangle.hpp:27
double getArea() const
Returns the surface area of the triangle.
Definition Triangle.cpp:70
iterator begin()
Returns a read-only random-access iterator to the begin (0) of the vertex range [0,...
Definition Triangle.hpp:155
const_iterator cbegin() const
Returns a read-only random-access iterator to the begin (0) of the vertex range [0,...
Definition Triangle.hpp:175
iterator end()
Returns a read-only random-access iterator to the end (3) of the vertex range [0,1,...
Definition Triangle.hpp:160
double getEnclosingRadius() const
Returns the radius of the circle enclosing the triangle.
Definition Triangle.cpp:94
Eigen::VectorXd computeNormal() const
Computes the normal of the triangle.
Definition Triangle.cpp:75
Vertex & vertex(int i)
Returns triangle vertex with index 0, 1 or 2.
Definition Triangle.hpp:143
const Eigen::VectorXd getCenter() const
Returns the barycenter of the triangle.
Definition Triangle.cpp:89
const_iterator cend() const
Returns a read-only random access iterator to the end (3) of the vertex range [0,1,...
Definition Triangle.hpp:180
Vertex of a mesh.
Definition Vertex.hpp:16
VertexID getID() const
Returns the unique (among vertices of one mesh on one processor) ID of the vertex.
Definition Vertex.hpp:111
const RawCoords & rawCoords() const
Direct access to the coordinates.
Definition Vertex.hpp:123
T distance(T... args)
provides Mesh, Data and primitives.
boost::test_tools::predicate_result equals(const std::vector< float > &VectorA, const std::vector< float > &VectorB, float tolerance)
equals to be used in tests. Compares two std::vectors using a given tolerance. Prints both operands o...
Definition Testing.cpp:65
Main namespace of the precice library.
T str(T... args)