TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Polygon_geom_tools.h
1/****************************************************************************
2* Copyright (c) 2025, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef Polygon_geom_tools_included
17#define Polygon_geom_tools_included
18
19#include <cmath>
20#include <cassert>
21
23{
24 double area_ = 0.;
25 double moment_r_ = 0.; // only relevant for axisymmetric geometries
26};
27
28template <typename CoordTab, typename IndexFunctor>
29inline Polygon_geom_data compute_polygon_geom(const CoordTab& coord,
30 int geom_dimension,
31 int nb_vertices,
32 const IndexFunctor& index_of,
33 bool compute_moment = false)
34{
36 if (nb_vertices < 3)
37 return data;
38
39 if (geom_dimension == 2)
40 {
41 double sum_cross = 0.;
42 double sum_r_cross = 0.;
43 for (int i = 0; i < nb_vertices; ++i)
44 {
45 const int next = (i + 1 == nb_vertices) ? 0 : (i + 1);
46 const auto si = index_of(i);
47 const auto sj = index_of(next);
48
49 const double ri = coord(si, 0);
50 const double zi = coord(si, 1);
51 const double rj = coord(sj, 0);
52 const double zj = coord(sj, 1);
53
54 const double cross = ri * zj - rj * zi;
55 sum_cross += cross;
56 if (compute_moment)
57 sum_r_cross += (ri + rj) * cross;
58 }
59
60 data.area_ = 0.5 * std::fabs(sum_cross);
61 if (compute_moment)
62 data.moment_r_ = sum_r_cross / 6.0;
63 }
64 else
65 {
66 assert(geom_dimension == 3);
67 const auto s0 = index_of(0);
68 const double x0 = coord(s0, 0);
69 const double y0 = coord(s0, 1);
70 const double z0 = coord(s0, 2);
71
72 double nx = 0.;
73 double ny = 0.;
74 double nz = 0.;
75 for (int i = 1; i < nb_vertices - 1; ++i)
76 {
77 const auto s1 = index_of(i);
78 const auto s2 = index_of(i + 1);
79
80 const double ax = coord(s1, 0) - x0;
81 const double ay = coord(s1, 1) - y0;
82 const double az = coord(s1, 2) - z0;
83
84 const double bx = coord(s2, 0) - x0;
85 const double by = coord(s2, 1) - y0;
86 const double bz = coord(s2, 2) - z0;
87
88 nx += ay * bz - az * by;
89 ny += az * bx - ax * bz;
90 nz += ax * by - ay * bx;
91 }
92
93 const double norm = std::sqrt(nx * nx + ny * ny + nz * nz);
94 data.area_ = 0.5 * norm;
95 }
96
97 return data;
98}
99
100#endif /* Polygon_geom_tools_included */