-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathClipperConverter.cpp
More file actions
292 lines (252 loc) · 7.42 KB
/
ClipperConverter.cpp
File metadata and controls
292 lines (252 loc) · 7.42 KB
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
283
284
285
286
287
288
289
290
291
292
#include "stdafx.h"
#include "ClipperConverter.h"
#include "ShapefileHelper.h"
// *********************************************************************
// Shape2ClipperPolygon()
// *********************************************************************
// Converts shape clipper polygon
// ClipperLib::Polygons* ClipperConverter::Shape2ClipperPolygon(IShape* shp)
ClipperLib::Paths* ClipperConverter::Shape2ClipperPolygon(IShape* shp)
{
if (!shp)
return NULL;
ShpfileType shpType;
shp->get_ShapeType(&shpType);
if (shpType != SHP_POLYGON && shpType != SHP_POLYGONM && shpType != SHP_POLYGONZ)
return NULL;
long numParts, numPoints;
shp->get_NumParts(&numParts);
shp->get_NumPoints(&numPoints);
if (numPoints == 0 || numParts == 0)
return NULL;
double x, y;
// ClipperLib::Polygons* retval = new ClipperLib::Polygons();
ClipperLib::Paths* retval = new ClipperLib::Paths();
for (int i = 0; i < numParts; i++)
{
// ClipperLib::Polygon polygon;
ClipperLib::Path polygon;
long begin, end;
shp->get_Part(i, &begin);
shp->get_EndOfPart(i, &end);
VARIANT_BOOL vbretval;
// The new Clipper doesn't need to be closed, so skip
// the last point which is the same as the first point
// to not waste memory.
for (int j = begin; j < end; j++ )
{
ClipperLib::IntPoint pnt;
shp->get_XY(j, &x, &y, &vbretval);
if (conversionFactor != 1.0)
{
x *= conversionFactor;
y *= conversionFactor;
}
pnt.X = (ClipperLib::long64)x;
pnt.Y = (ClipperLib::long64)y;
polygon.push_back(pnt);
}
retval->push_back(polygon);
}
if (retval->size() > 0)
{
return retval;
}
else
{
delete retval;
return NULL;
}
}
// ******************************************************************
// ClipperPolygon2Shape()
// ******************************************************************
// Converts clipper polygon to shape
// TODO: perhaps include polygon type as a parameter
// IShape* ClipperConverter::ClipperPolygon2Shape(ClipperLib::Polygons* polygon)
IShape* ClipperConverter::ClipperPolygon2Shape(ClipperLib::Paths* polygon)
{
bool pointsExist = false;
for (long i = 0; i < (long)polygon->size(); i++)
{
// ClipperLib::Polygon* poly = &((*polygon)[i]);
ClipperLib::Path* poly = &((*polygon)[i]);
if (poly->size() > 0)
{
pointsExist = true;
break;
}
}
if (!pointsExist)
return NULL;
IShape* shp = NULL;
ComHelper::CreateShape(&shp);
if (!shp)
return NULL;
VARIANT_BOOL vbretval;
shp->Create(SHP_POLYGON, &vbretval);
if (!vbretval)
{
shp->Release();
return NULL;
}
double x, y;
long cnt = 0;
long part = 0;
for (long i = 0; i < (long)polygon->size(); i++)
{
// ClipperLib::Polygon* poly = &((*polygon)[i]);
ClipperLib::Path* poly = &((*polygon)[i]);
if (poly->size() > 0)
{
shp->InsertPart(cnt, &part, &vbretval);
part++;
int j = poly->size() - 1;
for (; j >= 0; j--)
{
IPoint* pnt = NULL;
ComHelper::CreatePoint(&pnt);
x = (double)(*poly)[j].X;
y = (double)(*poly)[j].Y;
if (this->conversionFactor != 1.0)
{
x /= conversionFactor;
y /= conversionFactor;
}
pnt->put_X(x);
pnt->put_Y(y);
shp->InsertPoint(pnt, &cnt, &vbretval);
pnt->Release();
cnt++;
}
// the first and the last point of the part must be the same
int size = poly->size() - 1;
if (size > 0)
{
if (((*poly)[0]).X != ((*poly)[size]).X ||
((*poly)[0]).Y != ((*poly)[size]).Y)
{
IPoint* pnt = NULL;
ComHelper::CreatePoint(&pnt);
x = (double)(*poly)[size].X; // slightly inoptimal, this point was calculated already
y = (double)(*poly)[size].Y;
if (this->conversionFactor != 1.0)
{
x /= conversionFactor;
y /= conversionFactor;
}
pnt->put_X(x);
pnt->put_Y(y);
shp->InsertPoint(pnt, &cnt, &vbretval);
pnt->Release();
cnt++;
}
}
}
}
return shp;
}
// ******************************************************************
// ClipPolygon()
// ******************************************************************
// ClipperLib::Polygons* ClipperConverter::ClipPolygon(ClipperLib::Polygons* polyClip, ClipperLib::Polygons* polySubject, ClipperLib::ClipType operation)
ClipperLib::Paths* ClipperConverter::ClipPolygon(ClipperLib::Paths* polyClip, ClipperLib::Paths* polySubject, ClipperLib::ClipType operation)
{
if (polyClip && polySubject)
{
if (polyClip->size() > 0 && polySubject->size() > 0)
{
ClipperLib::Clipper* c = new ClipperLib::Clipper();
// ClipperLib::Polygons* solution = new ClipperLib::Polygons();
ClipperLib::Paths* solution = new ClipperLib::Paths();
for (unsigned int i = 0; i < polySubject->size(); i++)
{
// c->AddPolygon((*polySubject)[i], ClipperLib::ptSubject);
c->AddPath((*polySubject)[i], ClipperLib::ptSubject, true);
}
for (unsigned int i = 0; i < polyClip->size(); i++)
{
// c->AddPolygon((*polyClip)[i], ClipperLib::ptClip);
c->AddPath((*polyClip)[i], ClipperLib::ptClip, true);
}
c->Execute(operation, *solution); // ctIntersection
c->Clear();
delete c;
return solution;
}
else
{
return NULL;
}
}
else
{
return NULL;
}
}
// ******************************************************************
// ClipPolygon()
// ******************************************************************
IShape* ClipperConverter::ClipPolygon(IShape* shapeClip, IShape* shapeSubject, PolygonOperation operation)
{
ClipperLib::ClipType operNew;
switch(operation)
{
case DIFFERENCE_OPERATION:
operNew = ClipperLib::ctDifference;
break;
case INTERSECTION_OPERATION:
operNew = ClipperLib::ctIntersection;
break;
case EXCLUSIVEOR_OPERATION:
operNew = ClipperLib::ctXor;
break;
case UNION_OPERATION:
operNew = ClipperLib::ctUnion;
break;
default:
return NULL;
}
//shapeSubject
ClipperConverter ogr;
// ClipperLib::Polygons* poly1 = ogr.Shape2ClipperPolygon(shapeClip);
// ClipperLib::Polygons* poly2 = ogr.Shape2ClipperPolygon(shapeSubject);
ClipperLib::Paths* poly1 = ogr.Shape2ClipperPolygon(shapeClip);
ClipperLib::Paths* poly2 = ogr.Shape2ClipperPolygon(shapeSubject);
if (poly1 && poly2)
{
// ClipperLib::Polygons* result = ClipPolygon(poly1, poly2, operNew);
ClipperLib::Paths* result = ClipPolygon(poly1, poly2, operNew);
if (result)
{
IShape* shp = ogr.ClipperPolygon2Shape(result);
delete result;
return shp;
}
}
return NULL;
}
// ***************************************************
// AddPolygonsToClipper
// ***************************************************
void ClipperConverter::AddPolygons(IShapefile* sf, ClipperLib::Clipper& clp, ClipperLib::PolyType clipType, bool selectedOnly)
{
if (!sf) return;
long numShapes;
sf->get_NumShapes(&numShapes);
ClipperConverter converter(sf);
IShape* shp = NULL;
for (long i = 0; i < numShapes; i++)
{
if (selectedOnly && !ShapefileHelper::ShapeSelected(sf, i))
continue;
sf->get_Shape(i, &shp);
if (shp) {
// ClipperLib::Polygons* polys = converter.Shape2ClipperPolygon(shp);
ClipperLib::Paths* polys = converter.Shape2ClipperPolygon(shp);
// clp.AddPolygons(*polys, clipType);
clp.AddPaths(*polys, clipType, true);
shp->Release();
}
}
}