OpenBEM
Open-source framework for electromagnetic simulation with the boundary element method.
Loading...
Searching...
No Matches
triangle.hpp
Go to the documentation of this file.
1// OpenBEM - Copyright (C) 2026 Shashwat Sharma
2
3// This file is part of OpenBEM.
4
5// OpenBEM is free software: you can redistribute it and/or modify it under the terms of the
6// GNU General Public License as published by the Free Software Foundation, either version 3
7// of the License, or (at your option) any later version.
8
9// You should have received a copy of the GNU General Public License along with OpenBEM.
10// If not, see <https://www.gnu.org/licenses/>.
11
12
18#ifndef GEOM_TRIANGLE_H
19#define GEOM_TRIANGLE_H
20
21#include <stdexcept>
22#include <array>
23#include <cassert>
24
25#include "types.hpp"
26#include "constants.hpp"
27
28
29namespace bem
30{
31
32const Float TRIANGLE_DEFAULT_TOL = 1.0e-6;
33
43template <uint8_t dim>
45{
46public:
47
52 {
54 return;
55 };
56
57
71
72
79 std::array<EigColVecN<Float, dim>, 3> v_array,
81 )
82 {
84 for (uint8_t ii = 0; ii < 3; ++ii)
85 v.col(ii) = v_array[ii];
87 return;
88 };
89
90
96 void set_v(
99 );
100
101
107 {
108 return v_;
109 };
110
111
118 {
119 if (idx >= 3)
120 throw std::out_of_range("Triangle::v(): vertex index out of bounds.");
121 return v_.col(idx);
122 };
123
124
130 {
131 return edge_polarities_;
132 };
133
134
141 {
142 if (idx >= 3)
143 throw std::out_of_range("Triangle::edge_polarities(): edge index out of bounds.");
144 return edge_polarities_[idx];
145 };
146
147
151 void reverse()
152 {
153 set_v(v_.rowwise().reverse(), edge_polarities_.reverse());
154 return;
155 };
156
157
162 Float area() const
163 { return area_; };
164
165
171 { return normal_; };
172
173
179 { return centroid_; };
180
181
187 {
188 Float len = 0.0;
189 for (uint8_t ii = 0; ii < 3; ii++)
190 len += (v_.col(ii) - v_.col((ii + 1) % 3)).norm();
191 return len / (Float)3;
192 };
193
194
201 Triangle<2> to_2d() const;
202
203
209 Triangle<3> to_3d(const Float z = 0.0) const;
210
211
218 {
219 return v_.col(0);
220 };
221
222
230
231
244 uint8_t ref_idx = 0
245 ) const;
246
247
253
254
260 {
262 for (uint8_t ii = 0; ii < 3; ii++)
263 lengths[ii] = (v_.col((ii + 1) % 3) - v_.col((ii) % 3)).norm();
264 return lengths;
265 };
266
267
273 {
275 edge_lengths().maxCoeff(&result);
276 assert(result < 3 && "Triangle::longest_edge(): Result out of range.");
277 return result;
278 };
279
280
286 {
288 edge_lengths().minCoeff(&result);
289 assert(result < 3 && "Triangle::shortest_edge(): Result out of range.");
290 return result;
291 };
292
293
299 {
300 return edge_lengths().maxCoeff();
301 };
302
303
309 {
310 return edge_lengths().minCoeff();
311 };
312
313
320
321
329
330
337 bool point_in_triangle(ConstEigRef<EigColVecN<Float, dim>> r, const Float tol = TRIANGLE_DEFAULT_TOL) const;
338
339
348 template <uint8_t dim_out>
352 ) const
353 {
356 tri_out.v(1) * lambda.row(1) +
357 tri_out.v(2) * lambda.row(2);
358 return points_out;
359 }
360
361
368
369
376
377
384 { return v.rowwise().sum() / (Float)3; };
385
386
387protected:
388
390 Float area_;
391 EigColVecN<Float, 3> normal_;
392 EigColVecN<Float, dim> centroid_;
393 EigRowVecN<Float, 3> edge_polarities_ = EigRowVecN<Float, 3>::Constant(1, 3, 1);
394
395};
396
401}
402
403#ifndef BEM_LINKED
405#endif
406
407#endif
Triangle primitive class.
Definition triangle.hpp:45
Triangle< 3 > to_3d(const Float z=0.0) const
Adds a 0-valued third dimension if this Triangle is in 2D, otherwise just returns a copy of this Tria...
Definition triangle.cpp:74
Float shortest_edge_length() const
Length of the shortest edge of this Triangle.
Definition triangle.hpp:308
uint8_t shortest_edge_index() const
Returns the index associated with the shortest edge of this Triangle.
Definition triangle.hpp:285
void get_plane_projection(EigMatNX< Float, dim > &r_proj, EigRowVec< Float > &d, ConstEigRef< EigMatNX< Float, 3 > > r, uint8_t ref_idx=0) const
Returns the projection of given points on to the triangle's plane, along with the normal distance to ...
Definition triangle.cpp:111
Triangle()
Constructs a default reference Triangle object.
Definition triangle.hpp:51
Float mean_edge_length() const
Returns the mean edge length of this Triangle.
Definition triangle.hpp:186
static EigColVecN< Float, dim > centroid(ConstEigRef< EigMatMN< Float, dim, 3 > > v)
Computes the centroid of the triangle formed by the given vertices.
Definition triangle.hpp:383
EigMatMN< Float, dim, dim > local_coordinate_basis() const
Returns the unit vectors of a local coordinate system, defined in the global system,...
Definition triangle.cpp:83
Float area() const
Returns the area of this Triangle.
Definition triangle.hpp:162
EigColVecN< Float, 3 > edge_lengths() const
Returns the lengths of each edge of this Triangle.
Definition triangle.hpp:259
const EigMatMN< Float, dim, 3 > & v() const
Returns the vertices of this Triangle.
Definition triangle.hpp:106
Float longest_edge_length() const
Length of the longest edge of this Triangle.
Definition triangle.hpp:298
Float edge_polarities(const uint8_t idx) const
Returns the edge polarity of this Triangle at a given index.
Definition triangle.hpp:140
EigColVecN< Float, dim_out > map_points(ConstEigRef< EigColVecN< Float, dim > > points, const Triangle< dim_out > &tri_out) const
Performs an affine transform to map points lying in this triangle's plane to the output triangle's pl...
Definition triangle.hpp:349
EigColVecN< Float, 3 > normal() const
Returns the unit normal vector for this Triangle.
Definition triangle.hpp:170
uint8_t longest_edge_index() const
Returns the index associated with the longest edge of this Triangle.
Definition triangle.hpp:272
const EigRowVecN< Float, 3 > & edge_polarities() const
Returns the edge polarities of this Triangle.
Definition triangle.hpp:129
static Triangle< dim > reference_triangle()
Returns a reference triangle in the specified dimension.
Definition triangle.cpp:129
EigColVecN< Float, dim > v(const uint8_t idx) const
Returns the vertex of this Triangle at a given index.
Definition triangle.hpp:117
void set_v(ConstEigRef< EigMatMN< Float, dim, 3 > > v, EigRowVecN< Float, 3 > edge_polarities=EigRowVecN< Float, 3 >::Constant(1, 3, 1))
Sets the vertices of this Triangle.
Definition triangle.cpp:33
EigColVecN< Float, dim > local_origin() const
Returns the local origin of this Triangle in the global coordinate system. The local origin is just t...
Definition triangle.hpp:217
EigMatNX< Float, 3 > barycentric_coords(ConstEigRef< EigMatNX< Float, dim > > p) const
Returns barycentric coordinates of given points lying in the Triangle's plane.
Definition triangle.cpp:141
void reverse()
Reverses the orientation of this Triangle.
Definition triangle.hpp:151
Triangle< 2 > to_2d() const
Returns an equivalent triangle with coordinates in a local 2D system, with the local origin at this t...
Definition triangle.cpp:48
bool point_in_triangle(ConstEigRef< EigColVecN< Float, dim > > r, const Float tol=TRIANGLE_DEFAULT_TOL) const
Checks whether a given point is inside the triangle, inclusive of edges and vertices.
Definition triangle.cpp:241
Triangle(std::array< EigColVecN< Float, dim >, 3 > v_array, EigRowVecN< Float, 3 > edge_polarities=EigRowVecN< Float, 3 >::Constant(1, 3, 1))
Constructs a Triangle with given vertices.
Definition triangle.hpp:78
EigColVecN< Float, dim > centroid() const
Returns the centroid of this Triangle.
Definition triangle.hpp:178
Triangle(ConstEigRef< EigMatMN< Float, dim, 3 > > v, EigRowVecN< Float, 3 > edge_polarities=EigRowVecN< Float, 3 >::Constant(1, 3, 1))
Constructs a Triangle with given vertices.
Definition triangle.hpp:63
EigRowVec< uint8_t > projection_loc(ConstEigRef< EigMatNX< Float, 3 > > r) const
Returns flags that indicate whether the given points project inside or outside the triangle,...
Definition triangle.cpp:172
const Eigen::Ref< const EigObj > ConstEigRef
Read-only reference to an Eigen object.
Definition types.hpp:98
double Float
Floating point number.
Definition types.hpp:47
Eigen::Matrix< T, N, 1 > EigColVecN
Fixed-size column vector of size N containing type T.
Definition types.hpp:86
Eigen::Matrix< T, 1, N > EigRowVecN
Fixed-size row vector of size N containing type T.
Definition types.hpp:94
Primary namespace for the OpenBEM library.
Definition constants.hpp:31