GEOS  3.13.0dev
TriList.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  **********************************************************************/
14 
15 #pragma once
16 
17 #include <geos/geom/GeometryFactory.h>
18 
19 #include <geos/export.h>
20 #include <iostream>
21 #include <iterator>
22 #include <deque>
23 #include <array>
24 #include <vector>
25 
26 // Forward declarations
27 namespace geos {
28 namespace geom {
29 class Coordinate;
30 class Geometry;
31 }
32 }
33 
37 
38 typedef int TriIndex;
39 
40 namespace geos { // geos.
41 namespace triangulate { // geos.triangulate
42 namespace tri { // geos.triangulate.tri
43 
44 
53 template<typename TriType>
54 class TriList {
55 
56 private:
57 
58  // Members
59  std::deque<TriType> triStore;
60  std::vector<TriType*> tris;
61 
62  // Methods
63  TriType* create(const Coordinate& c0, const Coordinate& c1, const Coordinate& c2)
64  {
65  triStore.emplace_back(c0, c1, c2);
66  TriType* newTri = &triStore.back();
67  return newTri;
68  }
69 
70 
71 public:
72 
73  TriList() {};
74 
75  std::vector<TriType*>& getTris()
76  {
77  return tris;
78  }
79 
80  void remove(TriType* tri)
81  {
82  // We can leave triStore untouched, just remove
83  // the pointer from tris.
84  for (auto it = tris.begin(); it != tris.end(); ++it) {
85  if (*it == tri) {
86  tris.erase(it);
87  return;
88  }
89  }
90  }
91 
92  void add(const Coordinate& c0, const Coordinate& c1, const Coordinate& c2)
93  {
94  auto* newTri = create(c0, c1, c2);
95  tris.push_back(newTri);
96  };
97 
98  void add(std::array<Coordinate, 3>& corner)
99  {
100  add(corner[0], corner[1], corner[2]);
101  };
102 
103  double area()
104  {
105  double dArea = 0.0;
106  for (const auto* tri : tris) {
107  dArea += tri->getArea();
108  }
109  return dArea;
110  };
111 
112  double length()
113  {
114  double dLength = 0.0;
115  for (const auto* tri : tris) {
116  dLength += tri->getLength();
117  }
118  return dLength;
119  };
120 
121  /* public */
122  std::size_t
123  degree(const TriType* tri, TriIndex index)
124  {
125  const Coordinate& v = tri->getCoordinate(index);
126  std::size_t szDegree = 0;
127  for (auto* t : *this) {
128  for (TriIndex i = 0; i < 3; i++) {
129  if (v.equals2D(t->getCoordinate(i)))
130  szDegree++;
131  }
132  }
133  return szDegree;
134  }
135 
136  void validate()
137  {
138  for (auto* tri : *this) {
139  tri->validate();
140  }
141  }
142 
143  std::unique_ptr<Geometry> toGeometry(
144  const GeometryFactory* geomFact) const
145  {
146  std::vector<std::unique_ptr<Geometry>> geoms;
147  for (auto* tri: tris) {
148  std::unique_ptr<Geometry> geom = tri->toPolygon(geomFact);
149  geoms.emplace_back(geom.release());
150  }
151  return geomFact->createGeometryCollection(std::move(geoms));
152  }
153 
154  friend std::ostream& operator << (std::ostream& os, TriList& triList)
155  {
156  os << "TRILIST ";
157  os << "[" << triList.size() << "] (";
158  for (auto* tri: triList) {
159  os << " " << *tri << "," << std::endl;
160  }
161  os << ")";
162  return os;
163  }
164 
165  // Support for iterating on TriList
166  typedef typename std::vector<TriType*>::iterator iterator;
167  typedef typename std::vector<TriType*>::const_iterator const_iterator;
168  size_t size() const { return tris.size(); }
169  bool empty() const { return tris.empty(); }
170  iterator begin() { return tris.begin(); }
171  iterator end() { return tris.end(); }
172  const_iterator begin() const { return tris.begin(); }
173  const_iterator end() const { return tris.end(); }
174  TriType* operator [] (std::size_t index) { return tris[index]; }
175 
176 };
177 
178 
179 } // namespace geos.triangulate.tri
180 } // namespace geos.triangulate
181 } // namespace geos
182 
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:216
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:65
std::unique_ptr< GeometryCollection > createGeometryCollection() const
Construct an EMPTY GeometryCollection.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition: Geometry.h:186
Definition: TriList.h:54
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25