Plugins
Loading...
Searching...
No Matches
polygons.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Gesellschaft fuer Anlagen- und Reaktorsicherheit gGmbH
3 * SPDX-License-Identifier: EUPL-1.2
4 * SPDX-FileContributor: Dmitry Logashenko
5 * SPDX-FileContributor: Goethe Universität Frankfurt
6 * SPDX-FileType: SOURCE
7 *
8 * This file is part of d3f++.
9 * d3f++ is an extension for UG4. Licensing information and citation requirements of UG4 are provided in LICENSES/UG4-LGPL_2.1
10 */
11
12
13/*
14 * Definition of surface functions by polygons
15 */
16#ifndef __H__UG__PLUGINS__D3F__POLYGONS__
17#define __H__UG__PLUGINS__D3F__POLYGONS__
18
19#include <vector>
20
21// ug4 headers
22#include "common/common.h"
23#include "common/math/ugmath.h"
24
25namespace ug {
26namespace d3f {
27
34{
35// Description of a polygon
37 {
38 public:
40 int id;
41 std::vector<MathVector<2> > corner;
43
45 t_polygon_item (int the_id, number x, number y, t_polygon_item * the_tl = NULL)
46 : tl (the_tl), id (the_id), corner (1)
47 {
48 corner[0][0] = x; corner[0][1] = y;
49 bb[0] = bb[1] = corner[0]; // a trivial bounding box
50 }
51
53 void append (number x, number y);
54
56 bool is_valid () const {return corner.size () >= 3;}
57
59 bool pnt_inside (number x, number y) const
60 {
61 //---- choose the prefered algorithm ----//
62 return pnt_inside_WN (MathVector<2> (x, y));
63 }
64
65 private:
66 //---- the winding number algorithm
67
69 inline bool pnt_is_on_the_left
70 (
71 const MathVector<2> & pnt_0,
72 const MathVector<2> & pnt_1,
73 const MathVector<2> & pnt
74 ) const;
75
77 bool pnt_inside_WN (const MathVector<2> pnt) const;
78
79 //---- the crossing number algorithm
80
82 bool pnt_inside_CN (const MathVector<2> pnt) const;
83 };
84
85public:
86
89
92 {
94 while (t != NULL)
95 {
96 t_polygon_item * s = t->tl;
97 delete t;
98 t = s;
99 }
100 }
101
103 void append_point (number x, number y, int id)
104 {
105 t_polygon_item * t = polygon_by_id (id);
106 if (t != NULL)
107 t->append (x, y);
108 else
109 m_polygons = new t_polygon_item (id, x, y, m_polygons);
110 }
111
113 bool get_id_by_pnt (number x, number y, int & id) const
114 {
115 for (t_polygon_item * t = m_polygons; t != NULL; t = t->tl)
116 if (t->pnt_inside (x, y))
117 {
118 id = t->id;
119 return true;
120 }
121 return false;
122 }
123
125 int get_id_by_pnt_dv (number x, number y, int dv) const
126 {
127 int id;
128 return (get_id_by_pnt (x, y, id))? id : dv;
129 }
130
132 void load_points_from (const char * file_name);
133
134private:
135
138 {
139 t_polygon_item * t;
140 for (t = m_polygons; t != NULL; t = t->tl)
141 if (t->id == id)
142 break;
143 return t;
144 }
145
147 void load_points_from (std::istream & input);
148
150 bool good () const
151 {
152 for (t_polygon_item * t = m_polygons; t != NULL; t = t->tl)
153 if (! t->is_valid ())
154 return false;
155 return true;
156 }
157
158private:
160};
161
162} // namespace d3f
163} // end namespace ug
164
165#endif // __H__UG__PLUGINS__D3F__POLYGONS__
166
167/* End of File */
parameterString s
Definition Biogas.lua:2
bool pnt_inside_WN(const MathVector< 2 > pnt) const
checks whether a point is in the polygon
Definition polygons.cpp:61
t_polygon_item(int the_id, number x, number y, t_polygon_item *the_tl=NULL)
constructor: we need at least one point!
Definition polygons.h:45
int id
identifier of the polygon
Definition polygons.h:40
std::vector< MathVector< 2 > > corner
corners
Definition polygons.h:41
t_polygon_item * tl
next polygon
Definition polygons.h:39
bool pnt_is_on_the_left(const MathVector< 2 > &pnt_0, const MathVector< 2 > &pnt_1, const MathVector< 2 > &pnt) const
checks the position of a point wrt a line
Definition polygons.cpp:43
bool pnt_inside(number x, number y) const
checks whether a point is in the polygon
Definition polygons.h:59
MathVector< 2 > bb[2]
bounding box
Definition polygons.h:42
void append(number x, number y)
appends a point at the end of the list
Definition polygons.cpp:27
bool is_valid() const
checks whether the points specify a proper polygon
Definition polygons.h:56
bool pnt_inside_CN(const MathVector< 2 > pnt) const
checks whether a point is in the polygon
Definition polygons.cpp:101
Definition polygons.h:34
bool get_id_by_pnt(number x, number y, int &id) const
checks, in which polygon (x, y) is (if in any)
Definition polygons.h:113
void load_points_from(const char *file_name)
loads points and polygon descriptsions from a file
Definition polygons.cpp:169
bool good() const
checks whether the specified polygons are OK
Definition polygons.h:150
t_polygon_item * m_polygons
polygons
Definition polygons.h:159
t_polygon_item * polygon_by_id(int id) const
gets polygon by id
Definition polygons.h:137
~PolygonalRegions()
class destructor
Definition polygons.h:91
PolygonalRegions()
class constructor
Definition polygons.h:88
void append_point(number x, number y, int id)
appends a point to an existing polygon, or creates a new polygon
Definition polygons.h:103
int get_id_by_pnt_dv(number x, number y, int dv) const
returns the polygon id, where (x, y) is, or a given default value
Definition polygons.h:125
double number