OGS
Surface.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "Surface.h"
5
6#include <list>
7
8// GeoLib
9#include "AABB.h"
10#include "AnalyticalGeometry.h"
11#include "Polygon.h"
12#include "Polyline.h"
13#include "SurfaceGrid.h"
14#include "Triangle.h"
15
16namespace GeoLib
17{
18Surface::Surface(const std::vector<Point*>& pnt_vec)
19 : _sfc_pnts(pnt_vec), _bounding_volume(nullptr), _surface_grid(nullptr)
20{
21}
22
24 : _sfc_pnts(src._sfc_pnts),
26 _surface_grid(nullptr)
27{
28 _sfc_triangles.reserve(src._sfc_triangles.size());
29 std::transform(src._sfc_triangles.cbegin(),
30 src._sfc_triangles.cend(),
31 std::back_inserter(_sfc_triangles),
32 [](Triangle* t) { return new Triangle(*t); });
33}
34
36{
37 for (auto& _sfc_triangle : _sfc_triangles)
38 {
39 delete _sfc_triangle;
40 }
41}
42
43void Surface::addTriangle(std::size_t pnt_a,
44 std::size_t pnt_b,
45 std::size_t pnt_c)
46{
47 assert(pnt_a < _sfc_pnts.size() && pnt_b < _sfc_pnts.size() &&
48 pnt_c < _sfc_pnts.size());
49
50 // Check if two points of the triangle have identical IDs
51 if (pnt_a == pnt_b || pnt_a == pnt_c || pnt_b == pnt_c)
52 {
53 return;
54 }
55
56 // Adding a new triangle invalides the surface grid.
57 _surface_grid.reset();
58
59 _sfc_triangles.push_back(new Triangle(_sfc_pnts, pnt_a, pnt_b, pnt_c));
61 {
62 std::vector<std::size_t> ids(3);
63 ids[0] = pnt_a;
64 ids[1] = pnt_b;
65 ids[2] = pnt_c;
66 _bounding_volume = std::make_unique<GeoLib::AABB>(_sfc_pnts, ids);
67 }
68 else
69 {
70 _bounding_volume->update(*_sfc_pnts[pnt_a]);
71 _bounding_volume->update(*_sfc_pnts[pnt_b]);
72 _bounding_volume->update(*_sfc_pnts[pnt_c]);
73 }
74}
75
77{
78 return _sfc_triangles.size();
79}
80
81const Triangle* Surface::operator[](std::size_t i) const
82{
83 assert(i < _sfc_triangles.size());
84 return _sfc_triangles[i];
85}
86
88 double eps) const
89{
90 return _bounding_volume->containsPoint(pnt, eps);
91}
92
93bool Surface::isPntInSfc(MathLib::Point3d const& pnt, double eps) const
94{
95 // Mutable _surface_grid is constructed if method is called the first time.
96 if (_surface_grid == nullptr)
97 {
98 _surface_grid = std::make_unique<GeoLib::SurfaceGrid>(this);
99 }
100 return _surface_grid->isPointInSurface(pnt, eps);
101}
102
103bool operator==(Surface const& lhs, Surface const& rhs)
104{
105 return &lhs == &rhs;
106}
107
108void resetPointIDs(Surface& surface, std::vector<std::size_t> const& mapping)
109{
110 if (surface.getPointVec()->size() != mapping.size())
111 {
112 OGS_FATAL(
113 "internal error in resetPointIDs(): surface based on point vector "
114 "of size {}, given mapping vector has size {}",
115 surface.getPointVec()->size(), mapping.size());
116 }
117 for (std::size_t i = 0; i < surface.getNumberOfTriangles(); ++i)
118 {
119 auto& triangle = *surface[i];
120 const_cast<std::size_t&>(triangle[0]) = mapping[triangle[0]];
121 const_cast<std::size_t&>(triangle[1]) = mapping[triangle[1]];
122 const_cast<std::size_t&>(triangle[2]) = mapping[triangle[2]];
123 }
124}
125
126void markUsedPoints(Surface const& surface, std::vector<bool>& used_points)
127{
128 if (surface.getPointVec()->size() != used_points.size())
129 {
130 OGS_FATAL(
131 "internal error in markUsedPoints(): surface based on point vector "
132 "of size {}, given used_points has size {}",
133 surface.getPointVec()->size(), used_points.size());
134 }
135 for (std::size_t i = 0; i < surface.getNumberOfTriangles(); ++i)
136 {
137 auto const& triangle = *surface[i];
138 for (std::size_t k = 0; k < 3; ++k)
139 {
140 used_points[triangle[k]] = true;
141 }
142 }
143}
144
145} // namespace GeoLib
#define OGS_FATAL(...)
Definition Error.h:19
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:45
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
std::unique_ptr< AABB > _bounding_volume
std::unique_ptr< SurfaceGrid > _surface_grid
const Triangle * operator[](std::size_t i) const
const access operator for the access to the i-th Triangle of the surface.
Definition Surface.cpp:81
const std::vector< Point * > & _sfc_pnts
std::vector< Triangle * > _sfc_triangles
std::size_t getNumberOfTriangles() const
Definition Surface.cpp:76
~Surface() override
Definition Surface.cpp:35
void addTriangle(std::size_t pnt_a, std::size_t pnt_b, std::size_t pnt_c)
Definition Surface.cpp:43
bool isPntInBoundingVolume(MathLib::Point3d const &pnt, double eps) const
Definition Surface.cpp:87
const std::vector< Point * > * getPointVec() const
Surface(const std::vector< Point * > &pnt_vec)
Definition Surface.cpp:18
bool isPntInSfc(MathLib::Point3d const &pnt, double eps) const
Definition Surface.cpp:93
Class Triangle consists of a reference to a point vector and a vector that stores the indices in the ...
Definition Triangle.h:21
void markUsedPoints(Polyline const &polyline, std::vector< bool > &used_points)
Resets the point IDs of the polyline corresponding to the mapping.
Definition Polyline.cpp:479
void resetPointIDs(Polyline &polyline, std::vector< std::size_t > const &mapping)
Resets the point IDs of the polyline corresponding to the mapping.
Definition Polyline.cpp:464
bool operator==(LineSegment const &s0, LineSegment const &s1)