Cells.cpp
Go to the documentation of this file.
1 //=============================================================================
2 // Copyright (C) 2021-2024 Wageningen University - All Rights Reserved
3 // Author: Gonzalo Mier
4 // BSD-3 License
5 //=============================================================================
6 
7 #include <algorithm>
9 
10 namespace f2c::types {
11 
13  this->data_ = std::shared_ptr<OGRMultiPolygon>(
14  downCast<OGRMultiPolygon*>(
15  OGRGeometryFactory::createGeometry(wkbMultiPolygon)),
16  [](OGRMultiPolygon* f) {OGRGeometryFactory::destroyGeometry(f);});
17 }
18 
19 Cells::Cells(const OGRGeometry* geom) {
20  if (wkbFlatten(geom->getGeometryType()) == OGRwkbGeometryType::wkbPolygon) {
21  this->data_ = std::shared_ptr<OGRMultiPolygon>(downCast<OGRMultiPolygon*>(
22  OGRGeometryFactory::createGeometry(wkbMultiPolygon)),
23  [](OGRMultiPolygon* f) {OGRGeometryFactory::destroyGeometry(f);});
24  this->data_->addGeometry(geom->toPolygon());
25  } else if (wkbFlatten(geom->getGeometryType()) ==
26  OGRwkbGeometryType::wkbMultiPolygon) {
27  this->data_ = std::shared_ptr<OGRMultiPolygon>(
28  downCast<OGRMultiPolygon*>(geom->clone()),
29  [](OGRMultiPolygon* f) {OGRGeometryFactory::destroyGeometry(f);});
30  } else if (wkbFlatten(geom->getGeometryType()) ==
31  OGRwkbGeometryType::wkbGeometryCollection) {
32  this->data_ = std::shared_ptr<OGRMultiPolygon>(
33  downCast<OGRMultiPolygon*>(
34  OGRGeometryFactory::createGeometry(wkbMultiPolygon)),
35  [](OGRMultiPolygon* f) {OGRGeometryFactory::destroyGeometry(f);});
36  auto* gc = downCast<const OGRGeometryCollection*>(geom);
37  for (int i = 0; i < gc->getNumGeometries(); ++i) {
38  auto* g_i = gc->getGeometryRef(i);
39  if (wkbFlatten(g_i->getGeometryType()) ==
40  OGRwkbGeometryType::wkbPolygon) {
41  this->data_->addGeometry(g_i);
42  }
43  }
44  } else {
45  this->data_ = std::shared_ptr<OGRMultiPolygon>(
46  downCast<OGRMultiPolygon*>(
47  OGRGeometryFactory::createGeometry(wkbMultiPolygon)),
48  [](OGRMultiPolygon* f) {OGRGeometryFactory::destroyGeometry(f);});
49  }
50 }
51 
52 
53 Cells::Cells(const Cell& c) {
54  this->data_->addGeometry(c.get());
55 }
56 
57 void Cells::operator*=(double b) {
58  for (auto&& poly : *this) {
59  poly *= b;
60  }
61 }
62 
63 void Cells::getGeometry(size_t i, Cell& cell) {
64  if (i >= this->size()) {
65  throw std::out_of_range(
66  "Geometry does not contain point " + std::to_string(i));
67  }
68  cell = Cell(this->data_->getGeometryRef(i), EmptyDestructor());
69 }
70 
71 void Cells::getGeometry(size_t i, Cell& cell) const {
72  if (i >= this->size()) {
73  throw std::out_of_range(
74  "Geometry does not contain point " + std::to_string(i));
75  }
76  cell = Cell(this->data_->getGeometryRef(i), EmptyDestructor());
77 }
78 
80  if (i >= this->size()) {
81  throw std::out_of_range(
82  "Geometry does not contain point " + std::to_string(i));
83  }
84  return Cell(this->data_->getGeometryRef(i));
85 }
86 
87 const Cell Cells::getGeometry(size_t i) const {
88  if (i >= this->size()) {
89  throw std::out_of_range(
90  "Cells does not contain cell at " + std::to_string(i));
91  }
92  return Cell(this->data_->getGeometryRef(i));
93 }
94 
95 void Cells::setGeometry(size_t i, const Cell& cell) {
96  auto n = this->size();
97  if (i < n) {
98  Cells cells;
99  for (size_t j = 0; j < n; ++j) {
100  cells.addGeometry((i == j) ? cell : this->getGeometry(j));
101  }
102  *this = cells;
103  return;
104  } else if (i != n) {
105  for (size_t j = n; j < i; ++j) {
106  this->addGeometry(Cell());
107  }
108  }
109  this->addGeometry(cell);
110 }
111 
112 const Cell Cells::getCell(size_t i) const {
113  return getGeometry(i);
114 }
115 
116 const LinearRing Cells::getCellBorder(size_t i) const {
117  return LinearRing(
118  downCast<OGRPolygon*>(this->data_->getGeometryRef(i))->getExteriorRing());
119 }
120 
121 const LinearRing Cells::getInteriorRing(size_t i_cell, size_t i_ring) const {
122  return LinearRing(downCast<OGRPolygon*>(this->data_->getGeometryRef(i_cell))
123  ->getInteriorRing(i_ring));
124 }
125 
126 void Cells::addGeometry(const Cell& c) {
127  this->data_->addGeometry(c.get());
128 }
129 
130 void Cells::addRing(size_t i, const LinearRing& ring) {
131  downCast<OGRPolygon*>(this->data_->getGeometryRef(i))->addRing(
132  ring.clone().get());
133 }
134 
135 size_t Cells::size() const {
136  return isEmpty() ? 0 : this->data_->getNumGeometries();
137 }
138 
139 bool Cells::isConvex() const {
140  return (this->size() != 1) ? false :
141  getCell(0).isConvex();
142 }
143 
145  return destroyResGeom<Cell>(this->data_->ConvexHull());
146 }
147 
149  return destroyResGeom<Cells>(cell->Intersection(c.get()));
150 }
151 
152 Cells Cells::intersection(const Cell& c) const {
153  return destroyResGeom<Cells>(c->Intersection(this->get()));
154 }
155 
156 Cells Cells::intersection(const Cells& c) const {
157  return destroyResGeom<Cells>(this->data_->Intersection(c.get()));
158 }
159 
160 Cells Cells::difference(const Cells& c) const {
161  return destroyResGeom<Cells>(this->data_->Difference(c.get()));
162 }
163 
164 Cells Cells::difference(const Cell& c) const {
165  return destroyResGeom<Cells>(this->data_->Difference(c.get()));
166 }
167 
168 Cells Cells::unionOp(const Cells& c) const {
169  return destroyResGeom<Cells>(this->data_->Union(c.get()));
170 }
171 
172 Cells Cells::unionOp(const Cell& c) const {
173  return destroyResGeom<Cells>(this->data_->Union(c.get()));
174 }
175 
177  return destroyResGeom<Cells>(this->data_->UnionCascaded());
178 }
179 
180 Cells Cells::splitByLine(const LineString& line) const {
181  Cells cells = this->difference(this->buffer(line, 1e-8));
182  for (auto&& c : cells) {
183  c = Cell::buffer(c, 1e-8 * 0.5);
184  }
185  return cells;
186 }
187 
189  Cells cells{*this};
190  for (auto&& line : lines) {
191  cells = cells.splitByLine(line);
192  }
193  return cells;
194 }
195 
196 LineString Cells::createSemiLongLine(const Point& point, double angle) const {
197  return LineString({point,
198  point.getPointFromAngle(angle, this->getMinSafeLength())});
199 }
200 
202  const Point& point, double angle) const {
203  return LineString({
204  point.getPointFromAngle(M_PI + angle, this->getMinSafeLength()),
205  point.getPointFromAngle(angle, this->getMinSafeLength())});
206 }
207 
209  return MultiLineString::intersection(line, *this);
210 }
211 
213  return lines.intersection(*this);
214 }
215 
217  return this->intersection(cell);
218 }
219 
220 bool Cells::isPointInBorder(const Point& p) const {
221  return p.touches(*this);
222 }
223 
224 bool Cells::isPointIn(const Point& p) const {
225  return p.within(*this);
226 }
227 
228 const Cell Cells::getCellWherePoint(const Point& p) const {
229  for (auto&& cell : *this) {
230  if (p.touches(cell) || p.within(cell)) {
231  return cell;
232  }
233  }
234  return Cell();
235 }
236 
238  const f2c::types::Point& p, double ang) const {
239  return this->getCellWherePoint(p).createLineUntilBorder(p, ang);
240 }
241 
242 Cells Cells::buffer(double width) const {
243  return destroyResGeom<Cells>(this->OGRBuffer(width));
244 }
245 
247  std::vector<double> dist;
248  std::vector<Point> ps;
249  for (auto&& cell : *this) {
250  ps.emplace_back(cell.closestPointOnBorderTo(p));
251  dist.emplace_back(ps.back().distance(p));
252  }
253  return ps[std::min_element(dist.begin(), dist.end()) - dist.begin()];
254 }
255 
256 
257 } // namespace f2c::types
258 
f2c::types::Cell::buffer
static Cell buffer(const Cell &geom, double width)
Definition: Cell.cpp:99
f2c::types::Geometry::within
bool within(const Geometry< T2, R2 > &geom) const
Check if this geometry is inside another geometry.
Definition: Geometry_impl.hpp:163
f2c::types::Geometry::touches
bool touches(const Geometry< T2, R2 > &geom) const
Check if this and another geometry touch each other.
Definition: Geometry_impl.hpp:157
f2c::types::Cells::unionOp
Cells unionOp(const Cell &c) const
Definition: Cells.cpp:172
1_basic_types.cells
cells
Definition: 1_basic_types.py:93
f2c::types::Cell::isConvex
bool isConvex() const
Check if the Cell is convex.
Definition: Cell.cpp:141
f2c::types
Types used by fields2cover library.
Definition: Cell.h:20
f2c::types::MultiLineString::intersection
MultiLineString intersection(const Geometry< T, R > &g) const
Definition: MultiLineString.h:63
f2c::types::Cells::closestPointOnBorderTo
Point closestPointOnBorderTo(const Point &p) const
Definition: Cells.cpp:246
f2c::types::Cells::convexHull
Cell convexHull() const
Definition: Cells.cpp:144
f2c::types::Cells::Cells
Cells()
Definition: Cells.cpp:12
f2c::types::Cells::intersection
static Cells intersection(const Cell &cell, const Cell &c)
Definition: Cells.cpp:148
f2c::types::Cells::addGeometry
void addGeometry(const Cell &c)
Definition: Cells.cpp:126
1_basic_types.cell
cell
Definition: 1_basic_types.py:88
f2c::types::Cells::getCellBorder
const LinearRing getCellBorder(size_t i) const
Definition: Cells.cpp:116
f2c::types::Cells::isConvex
bool isConvex() const
Definition: Cells.cpp:139
f2c::types::Cells::createSemiLongLine
LineString createSemiLongLine(const Point &point, double angle) const
Definition: Cells.cpp:196
f2c::types::Cells::size
size_t size() const
Definition: Cells.cpp:135
f2c::types::MultiLineString
Definition: MultiLineString.h:18
f2c::types::LinearRing
Definition: LinearRing.h:18
f2c::types::Geometry< OGRMultiPolygon, R >::getMinSafeLength
double getMinSafeLength() const
Definition: Geometry_impl.hpp:130
f2c::types::Geometry< OGRMultiPolygon, R >::OGRBuffer
OGRGeometry * OGRBuffer(double dfDist, int side=0) const
Definition: Geometry_impl.hpp:291
f2c::types::Cell
Definition: Cell.h:32
f2c::types::Cells::splitByLine
Cells splitByLine(const LineString &line) const
Definition: Cells.cpp:180
f2c::types::Geometry< OGRMultiPolygon, R >::isEmpty
bool isEmpty() const
Definition: Geometry_impl.hpp:222
1_basic_types.ring
ring
Definition: 1_basic_types.py:68
2_objective_functions.width
float width
Definition: 2_objective_functions.py:29
f2c::types::Cells::unionCascaded
Cells unionCascaded() const
Definition: Cells.cpp:176
f2c::types::Geometry::get
T * get()
Definition: Geometry_impl.hpp:71
f2c::types::Cells::difference
Cells difference(const Cell &c) const
Definition: Cells.cpp:164
f2c::types::LineString
Definition: LineString.h:19
8_complete_flow.ps
list ps
Definition: 8_complete_flow.py:43
f2c::types::Cells
Definition: Cells.h:21
f2c::types::Cells::getCellsInside
Cells getCellsInside(const Cells &cell) const
Definition: Cells.cpp:216
f2c::types::Point
Definition: Point.h:21
f2c::types::Cells::getCell
const Cell getCell(size_t i) const
Definition: Cells.cpp:112
f2c::types::Cell::createLineUntilBorder
LineString createLineUntilBorder(const Point &p, double ang) const
Generate a line from a point to the border of this cell.
Definition: Cell.cpp:192
Cells.h
f2c::types::Geometry< OGRMultiPolygon, R >::data_
std::shared_ptr< OGRMultiPolygon > data_
Definition: Geometry.h:129
f2c::types::Cells::getLinesInside
MultiLineString getLinesInside(const LineString &line) const
Definition: Cells.cpp:208
f2c::types::Cells::createLineUntilBorder
LineString createLineUntilBorder(const f2c::types::Point &p, double ang) const
Definition: Cells.cpp:237
f2c::types::Cells::operator*=
void operator*=(double b)
Definition: Cells.cpp:57
f2c::types::Cells::setGeometry
void setGeometry(size_t i, const Cell &cell)
Definition: Cells.cpp:95
f2c::types::Cells::getGeometry
void getGeometry(size_t i, Cell &cell)
Definition: Cells.cpp:63
f2c::types::Cells::isPointIn
bool isPointIn(const Point &p) const
Definition: Cells.cpp:224
f2c::types::Cells::addRing
void addRing(size_t i, const LinearRing &ring)
Definition: Cells.cpp:130
f2c::types::EmptyDestructor
Definition: Geometry.h:23
f2c::types::Cells::buffer
static Cells buffer(const Geometry< T, R > &geom, double width, int side=0)
Definition: Cells.h:99
f2c::types::Cells::getCellWherePoint
const Cell getCellWherePoint(const Point &p) const
Definition: Cells.cpp:228
1_basic_types.lines
lines
Definition: 1_basic_types.py:73
f2c::types::Cells::createStraightLongLine
LineString createStraightLongLine(const Point &point, double angle) const
Definition: Cells.cpp:201
f2c::types::to_string
std::string to_string(double d, const int precision=6)
Definition: Path.cpp:274
f2c::types::Point::getPointFromAngle
Point getPointFromAngle(double angle, double dist) const
Definition: Point.cpp:145
f2c::types::Cells::isPointInBorder
bool isPointInBorder(const Point &p) const
Definition: Cells.cpp:220
f2c::types::Cells::getInteriorRing
const LinearRing getInteriorRing(size_t i_cell, size_t i_ring) const
Definition: Cells.cpp:121


fields2cover
Author(s):
autogenerated on Fri Apr 25 2025 02:18:31