preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
20BOOST_AUTO_TEST_CASE(DirectionalEdges)
21{
23 using Eigen::Vector3d;
24 Vector3d coords1(0.0, 0.0, 0.0);
25 Vector3d coords2(1.0, 0.0, 0.0);
26 Vector3d coords3(1.0, 1.0, 0.0);
27
28 Vertex v1(coords1, 0);
29 Vertex v2(coords2, 1);
30 Vertex v3(coords3, 2);
31
32 Edge e1(v1, v2);
33 Edge e2(v2, v3);
34 Edge e3(v3, v1);
35
36 Triangle triangle(e1, e2, e3);
37
38 Vertex &v1ref = triangle.vertex(0);
39 BOOST_TEST(v1ref.getID() == v1.getID());
40
41 Vertex &v2ref = triangle.vertex(1);
42 BOOST_TEST(v2ref.getID() == v2.getID());
43
44 Vertex &v3ref = triangle.vertex(2);
45 BOOST_TEST(v3ref.getID() == v3.getID());
46
47 Vector3d normal = triangle.computeNormal();
48 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
49 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
50
51 Vector3d center = triangle.getCenter();
52 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
53
54 constexpr double expectedRadius = 0.74535599249993001;
55 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
56
57 constexpr double expectedArea = 0.5;
58 BOOST_TEST(triangle.getArea() == expectedArea);
59}
60
63{
65 using Eigen::Vector3d;
66 Vector3d coords1(0.0, 0.0, 0.0);
67 Vector3d coords2(1.0, 0.0, 0.0);
68 Vector3d coords3(1.0, 1.0, 0.0);
69 Vertex v1(coords1, 0);
70 Vertex v2(coords2, 1);
71 Vertex v3(coords3, 2);
72
73 Edge e1(v1, v2);
74 Edge e2(v3, v2);
75 Edge e3(v3, v1);
76
77 Triangle triangle(e1, e2, e3);
78
79 Vertex &v1ref = triangle.vertex(0);
80 BOOST_TEST(v1ref.getID() == v1.getID());
81
82 Vertex &v2ref = triangle.vertex(1);
83 BOOST_TEST(v2ref.getID() == v2.getID());
84
85 Vertex &v3ref = triangle.vertex(2);
86 BOOST_TEST(v3ref.getID() == v3.getID());
87
88 Vector3d normal = triangle.computeNormal();
89 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
90 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
91
92 Vector3d center = triangle.getCenter();
93 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
94
95 constexpr double expectedRadius = 0.74535599249993001;
96 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
97
98 constexpr double expectedArea = 0.5;
99 BOOST_TEST(triangle.getArea() == expectedArea);
100}
101
102PRECICE_TEST_SETUP(1_rank)
103BOOST_AUTO_TEST_CASE(ReversedFirstFlipped)
104{
105 PRECICE_TEST();
106 using Eigen::Vector3d;
107 Vector3d coords1(0.0, 0.0, 0.0);
108 Vector3d coords2(1.0, 0.0, 0.0);
109 Vector3d coords3(1.0, 1.0, 0.0);
110
111 Vertex v1(coords1, 0);
112 Vertex v2(coords2, 1);
113 Vertex v3(coords3, 2);
114
115 Edge e1(v1, v2);
116 Edge e2(v3, v2);
117 Edge e3(v1, v3);
118
119 Triangle triangle(e1, e2, e3);
120
121 Vertex &v1ref = triangle.vertex(0);
122 BOOST_TEST(v1ref.getID() == v1.getID());
123
124 Vertex &v2ref = triangle.vertex(1);
125 BOOST_TEST(v2ref.getID() == v2.getID());
126
127 Vertex &v3ref = triangle.vertex(2);
128 BOOST_TEST(v3ref.getID() == v3.getID());
129
130 Vector3d normal = triangle.computeNormal();
131 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
132 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
133
134 Vector3d center = triangle.getCenter();
135 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
136
137 constexpr double expectedRadius = 0.74535599249993001;
138 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
139
140 constexpr double expectedArea = 0.5;
141 BOOST_TEST(triangle.getArea() == expectedArea);
142}
143
144PRECICE_TEST_SETUP(1_rank)
145BOOST_AUTO_TEST_CASE(ReversedLastFlipped)
146{
147 PRECICE_TEST();
148 using Eigen::Vector3d;
149 Vector3d coords1(0.0, 0.0, 0.0);
150 Vector3d coords2(1.0, 0.0, 0.0);
151 Vector3d coords3(1.0, 1.0, 0.0);
152
153 Vertex v1(coords1, 0);
154 Vertex v2(coords2, 1);
155 Vertex v3(coords3, 2);
156
157 Edge e1(v1, v2);
158 Edge e2(v3, v2);
159 Edge e3(v3, v1);
160
161 Triangle triangle(e1, e3, e2);
162
163 Vertex &v1ref = triangle.vertex(0);
164 BOOST_TEST(v1ref.getID() == v1.getID());
165
166 Vertex &v2ref = triangle.vertex(1);
167 BOOST_TEST(v2ref.getID() == v2.getID());
168
169 Vertex &v3ref = triangle.vertex(2);
170 BOOST_TEST(v3ref.getID() == v3.getID());
171
172 Vector3d normal = triangle.computeNormal();
173 BOOST_TEST((coords2 - coords1).dot(normal) == 0.0);
174 BOOST_TEST((coords3 - coords1).dot(normal) == 0.0);
175
176 Vector3d center = triangle.getCenter();
177 BOOST_TEST(testing::equals(center, (coords1 + coords2 + coords3) / 3));
178
179 constexpr double expectedRadius = 0.74535599249993001;
180 BOOST_TEST(triangle.getEnclosingRadius() == expectedRadius);
181
182 constexpr double expectedArea = 0.5;
183 BOOST_TEST(triangle.getArea() == expectedArea);
184}
185
186PRECICE_TEST_SETUP(1_rank)
188{
189 PRECICE_TEST();
190 using Eigen::Vector3d;
191 Vector3d coords1(0.0, 0.0, 0.0);
192 Vector3d coords2(1.0, 0.0, 0.0);
193 Vector3d coords3(1.0, 1.0, 0.0);
194
195 Vertex v0(coords1, 0);
196 Vertex v1(coords2, 1);
197 Vertex v2(coords3, 2);
198
199 Edge e0(v0, v1);
200 Edge e1(v1, v2);
201 Edge e2(v2, v0);
202
203 Triangle triangle(e0, e1, e2);
204
205 {
206 // Test begin(), end()
207 auto ibegin = triangle.begin();
208 const auto iend = triangle.end();
209 BOOST_TEST(std::distance(ibegin, iend) == 3);
210 BOOST_TEST(*ibegin == v0.rawCoords());
211 ++ibegin;
212 BOOST_TEST(*ibegin == v1.rawCoords());
213 ++ibegin;
214 BOOST_TEST(*ibegin == v2.rawCoords());
215 ++ibegin;
216 BOOST_TEST((ibegin == iend));
217 }
218 {
219 // Test begin(), end() for const
220 const Triangle &ctriangle = triangle;
221 auto ibegin = ctriangle.begin();
222 const auto iend = ctriangle.end();
223 BOOST_TEST(std::distance(ibegin, iend) == 3);
224 BOOST_TEST(*ibegin == v0.rawCoords());
225 ++ibegin;
226 BOOST_TEST(*ibegin == v1.rawCoords());
227 ++ibegin;
228 BOOST_TEST(*ibegin == v2.rawCoords());
229 ++ibegin;
230 BOOST_TEST((ibegin == iend));
231 }
232 {
233 // Test cbegin(), cend()
234 auto ibegin = triangle.cbegin();
235 const auto iend = triangle.cend();
236 BOOST_TEST(std::distance(ibegin, iend) == 3);
237 BOOST_TEST(*ibegin == v0.rawCoords());
238 ++ibegin;
239 BOOST_TEST(*ibegin == v1.rawCoords());
240 ++ibegin;
241 BOOST_TEST(*ibegin == v2.rawCoords());
242 ++ibegin;
243 BOOST_TEST((ibegin == iend));
244 }
245}
246
247PRECICE_TEST_SETUP(1_rank)
248BOOST_AUTO_TEST_CASE(TriangleEquality)
249{
250 PRECICE_TEST();
251 using Eigen::Vector3d;
252 Vector3d coords1(0.0, 0.0, 0.0);
253 Vector3d coords2(1.0, 0.0, 0.0);
254 Vector3d coords3(1.0, 1.0, 0.0);
255 Vector3d coords4(2.0, 0.0, 0.0);
256
257 Vertex v1(coords1, 0);
258 Vertex v2(coords2, 1);
259 Vertex v3(coords3, 2);
260 Vertex v4(coords4, 0);
261
262 Edge e1(v1, v2);
263 Edge e2(v3, v2);
264 Edge e3(v3, v1);
265 Edge e4(v2, v4);
266 Edge e5(v4, v3);
267
268 // *
269 // * *
270 // ****
271 Triangle triangle1(e1, e3, e2);
272 Triangle triangle2(e1, e2, e3);
273 BOOST_TEST(triangle1 == triangle2);
274 // *
275 // * *
276 // ****
277 Triangle triangle3(e2, e4, e5);
278 Triangle triangle4(e2, e4, e5);
279 BOOST_TEST(triangle1 == triangle2);
280 BOOST_TEST(triangle1 != triangle3);
281 BOOST_TEST(triangle4 == triangle3);
282}
283
284PRECICE_TEST_SETUP(1_rank)
285BOOST_AUTO_TEST_CASE(TriangleWKTPrint)
286{
287 PRECICE_TEST();
288 Vertex v1(Eigen::Vector3d(0., 0., 0.), 0);
289 Vertex v2(Eigen::Vector3d(0., 1., 0.), 0);
290 Vertex v3(Eigen::Vector3d(1., 0., 0.), 0);
291 Edge e1(v1, v2);
292 Edge e2(v2, v3);
293 Edge e3(v3, v1);
294 Triangle t1(e1, e2, e3);
295 std::stringstream stream;
296 stream << t1;
297 std::string t1string("POLYGON ((0 0 0, 0 1 0, 1 0 0, 0 0 0))");
298 BOOST_TEST(t1string == stream.str());
299}
300
301BOOST_AUTO_TEST_SUITE_END() // Triangle
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST()
Definition Testing.hpp:39
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition Testing.hpp:29
BOOST_AUTO_TEST_CASE(DirectionalEdges)
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:15
Triangle of a mesh, defined by three vertices.
Definition Triangle.hpp:24
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:152
const_iterator cbegin() const
Returns a read-only random-access iterator to the begin (0) of the vertex range [0,...
Definition Triangle.hpp:172
iterator end()
Returns a read-only random-access iterator to the end (3) of the vertex range [0,1,...
Definition Triangle.hpp:157
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:140
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:177
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:109
const RawCoords & rawCoords() const
Direct access to the coordinates.
Definition Vertex.hpp:121
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:93
Main namespace of the precice library.
T str(T... args)