GEOS 3.14.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
27namespace geos {
28namespace geom {
29class Coordinate;
30class Geometry;
31}
32}
33
34typedef int TriIndex;
35
36namespace geos { // geos.
37namespace triangulate { // geos.triangulate
38namespace tri { // geos.triangulate.tri
39
40
49template<typename TriType>
50class TriList {
54
55private:
56
57 // Members
58 std::deque<TriType> triStore;
59 std::vector<TriType*> tris;
60
61 // Methods
62 TriType* create(const Coordinate& c0, const Coordinate& c1, const Coordinate& c2)
63 {
64 triStore.emplace_back(c0, c1, c2);
65 TriType* newTri = &triStore.back();
66 return newTri;
67 }
68
69
70public:
71
72 TriList() {};
73
74 std::vector<TriType*>& getTris()
75 {
76 return tris;
77 }
78
79 void remove(TriType* tri)
80 {
81 // We can leave triStore untouched, just remove
82 // the pointer from tris.
83 for (auto it = tris.begin(); it != tris.end(); ++it) {
84 if (*it == tri) {
85 tris.erase(it);
86 return;
87 }
88 }
89 }
90
91 void add(const Coordinate& c0, const Coordinate& c1, const Coordinate& c2)
92 {
93 auto* newTri = create(c0, c1, c2);
94 tris.push_back(newTri);
95 };
96
97 void add(std::array<Coordinate, 3>& corner)
98 {
99 add(corner[0], corner[1], corner[2]);
100 };
101
102 double area()
103 {
104 double dArea = 0.0;
105 for (const auto* tri : tris) {
106 dArea += tri->getArea();
107 }
108 return dArea;
109 };
110
111 double length()
112 {
113 double dLength = 0.0;
114 for (const auto* tri : tris) {
115 dLength += tri->getLength();
116 }
117 return dLength;
118 };
119
120 /* public */
121 std::size_t
122 degree(const TriType* tri, TriIndex index)
123 {
124 const Coordinate& v = tri->getCoordinate(index);
125 std::size_t szDegree = 0;
126 for (auto* t : *this) {
127 for (TriIndex i = 0; i < 3; i++) {
128 if (v.equals2D(t->getCoordinate(i)))
129 szDegree++;
130 }
131 }
132 return szDegree;
133 }
134
135 void validate()
136 {
137 for (auto* tri : *this) {
138 tri->validate();
139 }
140 }
141
142 std::unique_ptr<Geometry> toGeometry(
143 const GeometryFactory* geomFact) const
144 {
145 std::vector<std::unique_ptr<Geometry>> geoms;
146 for (auto* tri: tris) {
147 std::unique_ptr<Geometry> geom = tri->toPolygon(geomFact);
148 geoms.emplace_back(geom.release());
149 }
150 return geomFact->createGeometryCollection(std::move(geoms));
151 }
152
153 friend std::ostream& operator << (std::ostream& os, TriList& triList)
154 {
155 os << "TRILIST ";
156 os << "[" << triList.size() << "] (";
157 for (auto* tri: triList) {
158 os << " " << *tri << "," << std::endl;
159 }
160 os << ")";
161 return os;
162 }
163
164 // Support for iterating on TriList
165 typedef typename std::vector<TriType*>::iterator iterator;
166 typedef typename std::vector<TriType*>::const_iterator const_iterator;
167 size_t size() const { return tris.size(); }
168 bool empty() const { return tris.empty(); }
169 iterator begin() { return tris.begin(); }
170 iterator end() { return tris.end(); }
171 const_iterator begin() const { return tris.begin(); }
172 const_iterator end() const { return tris.end(); }
173 TriType* operator [] (std::size_t index) { return tris[index]; }
174
175};
176
177
178} // namespace geos.triangulate.tri
179} // namespace geos.triangulate
180} // namespace geos
181
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:217
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
std::unique_ptr< GeometryCollection > createGeometryCollection() const
Construct an EMPTY GeometryCollection.
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:197
Definition TriList.h:50
Basic namespace for all GEOS functionalities.
Definition geos.h:39