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