/
Footprint.cpp
283 lines (227 loc) · 6.14 KB
/
Footprint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "StdAfx.h"
#include "Footprint.h"
Footprint::Footprint(void):_index(-1),_visited(false),_degenerated(false),_altitude(0),_height(0)
{
_polygon = new OGRPolygon();
}
Footprint::~Footprint(void)
{
}
Footprint::Footprint(OGRLinearRing* r):_index(-1),_visited(false),_degenerated(false),_altitude(0),_height(0)
{
if(r->IsEmpty())
std::cerr<<"Footprint constructor error: empty ring"<<std::endl;
// create polygon with clockwise exterior ring
OGRLinearRing * ring = new OGRLinearRing(r);
remove_colinear_edges(ring);
if(ring->getNumPoints()>3)
adjust_winding(ring,ClockWise);
else
_degenerated = true;
_polygon = new OGRPolygon();
_polygon->addRingDirectly(ring);
_polygon = new OGRPolygon();
_polygon->addRing(r);
_polygon->closeRings();
}
Footprint::Footprint(OGRPolygon* ply):_index(-1),_visited(false),_degenerated(false),_altitude(0),_height(0)
{
if(ply->IsEmpty())
std::cerr<<"Footprint constructor error: empty polygon"<<std::endl;
// create polygon with clockwise exterior ring
OGRLinearRing *ring_ex = new OGRLinearRing(ply->getExteriorRing());
remove_colinear_edges(ring_ex);
if(ring_ex->getNumPoints()>3)
adjust_winding(ring_ex,ClockWise);
else
_degenerated = true;
_polygon = new OGRPolygon();
_polygon->addRingDirectly(ring_ex);
// add counterclockwise interior rings
if(int n = ply->getNumInteriorRings() && !_degenerated )
for(int i=0;i<n;i++)
{
OGRLinearRing *ring_in = new OGRLinearRing(ply->getInteriorRing(i));
//remove_colinear_edges(ring_in);
if(ring_in->getNumPoints()>3)
{
adjust_winding(ring_in,CounterClockWise);
_polygon->addRingDirectly(ring_in);
}
}
_polygon = (OGRPolygon*)ply->clone();
_polygon->closeRings();
}
Footprint::Footprint(const Footprint& other)
{
_polygon = (OGRPolygon*)other._polygon->clone();
_visited = other._visited;
_degenerated = other._degenerated;
_index = other._index;
_altitude = other._altitude;
_height = other._height;
}
Footprint& Footprint::operator =(const Footprint & other)
{
_index = other._index;
_visited = other._visited;
_degenerated = other._degenerated;
_height = other._height;
_polygon->empty();
_polygon = (OGRPolygon*)other._polygon->clone();
return *this;
}
bool Footprint::isAdjacent(Footprint *p)
{
if(!p)
return false;
if(!_polygon->Intersects(p->_polygon))
return false;
// intersecting with muliti points is not adjacent
OGRGeometry* pGeom = _polygon->Intersection(p->_polygon);
if( pGeom->getGeometryType() == wkbPoint || pGeom->getGeometryType() == wkbMultiPoint)
return false;
return true;
}
bool Footprint::isCoequal(Footprint *p)
{
if(!p)
return false;
if( _height/p->_height>2 || _height/p->_height<0.5)
return false;
return true;
}
void Footprint::merge(Footprint *p,Footprint& newFp,MergeType type)
{
OGRPolygon* ply = (OGRPolygon*)(_polygon->Union(p->_polygon));
newFp = Footprint(ply);
double a1 = p->_altitude,h1 = p->_height;
switch(type)
{
case Ceil:
newFp._altitude = _altitude > a1? _altitude:a1;
newFp._height = _height > h1? _height:h1;
break;
case Floor:
newFp._altitude = _altitude < a1? _altitude:a1;
newFp._height = _height < h1? _height:h1;
break;
case Average:
newFp._altitude = (_altitude + a1)/2;
newFp._height = (_height + h1)/2;
break;
}
}
int Footprint::getNumVertices() const
{
if( !_polygon->getExteriorRing())
return 0;
if(!_polygon->getNumInteriorRings())
return _polygon->getExteriorRing()->getNumPoints();
int n=_polygon->getNumInteriorRings();
int verts = _polygon->getExteriorRing()->getNumPoints();
for(int i=0;i<n;i++)
verts+= _polygon->getInteriorRing(i)->getNumPoints();
return verts;
}
int Footprint::getNumInteriorRings() const
{
return _polygon->getNumInteriorRings();
}
bool Footprint::getVerticesExteriorRing(std::vector<OGRPoint>& points) const
{
if( !_polygon->getExteriorRing())
return false ;
int n =_polygon->getExteriorRing()->getNumPoints();
for(int i=0;i<n;i++)
{
OGRPoint pt;
_polygon->getExteriorRing()->getPoint(i,&pt);
points.push_back(pt);
}
return true;
}
bool Footprint::getVerticesInteriorRing(int i,std::vector<OGRPoint> & points) const
{
if(i<0 || i>=_polygon->getNumInteriorRings())
return false;
int nPoints = _polygon->getInteriorRing(i)->getNumPoints();
for(int j=0;j< nPoints;j++)
{
OGRPoint pt;
_polygon->getInteriorRing(i)->getPoint(j,&pt);
points.push_back(pt);
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// private functions only called by constructors
void Footprint::remove_colinear_edges(OGRLinearRing* ring)
{
ring->closeRings();
if(ring->getNumPoints()<4)
return;
std::vector<OGRPoint> pts1,pts2; //pts1 contains the original points while pts2 contains the results
int flag =0; // to check if any vertex is removed
int n=ring->getNumPoints();
for(int i=0;i<n;i++)
{
OGRPoint pt;
ring->getPoint(i,&pt);
pts1.push_back(pt);
}
pts2.push_back(pts1[0]);
pts2.push_back(pts1[1]);
for(int i=2;i<n;i++)
{
int j = pts2.size()-1;
OGRPoint* p1 = &pts2[j-1],*p2 = &pts2[j],*p3 = &pts1[i];
OGRPoint v12(p2->getX()-p1->getX(),p2->getY()-p1->getY());
OGRPoint v23(p3->getX()-p2->getX(),p3->getY()-p2->getY());
double cos =( v12.getX() * v23.getX() + v12.getY() * v23.getY() ) / (p2->Distance(p1))*(p3->Distance(p2));
if(cos==1)
{
pts2.pop_back();
flag = 1;
}
pts2.push_back(*p3);
}
if(flag)
{
ring->empty();
ring = new OGRLinearRing();
for(int i=0;i<pts2.size();i++)
ring->addPoint(&pts2[i]);
}
}
void Footprint::adjust_winding(OGRLinearRing *ring,WindingType winding)
{
ring->closeRings();
switch(winding)
{
case ClockWise:
if(!ring->isClockwise())
reverse(ring);
break;
case CounterClockWise:
if(ring->isClockwise())
reverse(ring);
break;
}
}
void Footprint::reverse(OGRLinearRing* ring)
{
std::vector<OGRPoint> pts;
int n=ring->getNumPoints();
for(int i=0;i<n;i++)
{
OGRPoint pt;
ring->getPoint(i,&pt);
pts.push_back(pt);
}
ring->empty();
ring = new OGRLinearRing();
std::vector<OGRPoint>::reverse_iterator rit;
for(rit = pts.rbegin(); rit < pts.rend(); ++rit)
ring->addPoint(&(*rit));
}