-
Notifications
You must be signed in to change notification settings - Fork 2
/
Path.mm
233 lines (187 loc) · 5.06 KB
/
Path.mm
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
//
// Path.m
// An Objective-C wrapper for the Paths object from the Clipper library
// (http://sourceforge.net/projects/polyclipping/)
//
// Created by John Swensen on 3/18/14.
// Copyright (c) 2014 John Swensen. All rights reserved.
//
// License:
// Use, modification & distribution is subject to Boost Software License Ver 1.
// http://www.boost.org/LICENSE_1_0.txt
//
#import "Path.h"
#import "Paths.h"
#ifdef HAVE_POLY2TRI
#import "poly2tri.h"
#endif
@implementation Path
@synthesize path=_path;
- (ClipperLib::Path*) pathPtr
{
return &_path;
}
- (Path*) initWithPolygon:(NSMutableArray*)poly
{
self = [super init];
if (self) {
for (int i = 0 ; i < [poly count] ; i++)
{
CGPoint pnt = [[poly objectAtIndex:i] CGPointValue];
_path.push_back(ClipperLib::IntPoint(kClipperScale*pnt.x, kClipperScale*pnt.y));
}
}
return self;
}
- (Path*) initWithPath:(ClipperLib::Path)path
{
self = [super init];
if (self) {
_path = path;
}
return self;
}
- (unsigned long) count
{
return _path.size();
}
- (CGPoint) pointAtIndex:(unsigned long)idx
{
if (idx < [self count])
return CGPointMake(_path[idx].X/kClipperScale, _path[idx].Y/kClipperScale);
else
return CGPointMake(0,0);
}
- (CGPoint) lastPoint
{
int idx = (int)[self count]-1;
if (idx >= 0)
return CGPointMake(_path[idx].X/kClipperScale, _path[idx].Y/kClipperScale);
else
return CGPointMake(0,0);
}
- (void) addPoint:(CGPoint)pnt
{
_path.push_back(ClipperLib::IntPoint(kClipperScale*pnt.x, kClipperScale*pnt.y));
}
- (void) addIntPoint:(ClipperLib::IntPoint)pnt
{
_path.push_back(pnt);
}
- (void) insertPoint:(CGPoint)pnt atIndex:(int)idx
{
ClipperLib::Path::iterator it = _path.begin() + idx;
_path.insert(it, ClipperLib::IntPoint(kClipperScale*pnt.x, kClipperScale*pnt.y));
}
- (float) area
{
return ClipperLib::Area(_path)/kClipperScale/kClipperScale;
}
- (Paths*) simplifyPolygon
{
return [self simplifyPolygonWithFillType:ClipperLib::pftEvenOdd];
}
- (Paths*) simplifyPolygonWithFillType:(ClipperLib::PolyFillType)type
{
Paths* retval = [[Paths alloc] init];
ClipperLib::SimplifyPolygon(_path, *[retval pathsPtr],type);
return retval;
}
- (void) reversePath
{
ClipperLib::ReversePath(_path);
}
- (int) pointInPolygon:(CGPoint)pt;
{
return ClipperLib::PointInPolygon(ClipperLib::IntPoint(kClipperScale*pt.x, kClipperScale*pt.y), _path);
}
- (int) intPointInPolygon:(ClipperLib::IntPoint)pt;
{
return ClipperLib::PointInPolygon(pt, _path);
}
- (BOOL) orientation
{
return ClipperLib::Orientation(_path);
}
- (Paths*) minkowskiSumWithPattern:(Path*)pattern pathIsClosed:(BOOL)pathIsClosed
{
Paths* retval = [[Paths alloc] init];
ClipperLib::MinkowskiSum(pattern.path, _path, *[retval pathsPtr], pathIsClosed);
return retval;
}
- (Paths*) minkowskiDiffWithPoly:(Path*)poly2
{
Paths* retval = [[Paths alloc] init];
ClipperLib::MinkowskiDiff(_path, poly2.path, *[retval pathsPtr]);
return retval;
}
- (Path*) cleanPolygon
{
return [self cleanPolygonWithDistance:kDefaultCleanDistance];
}
- (Path*) cleanPolygonWithDistance:(double)distance
{
Path* retval = [[Path alloc] init];
ClipperLib::CleanPolygon(_path, *[retval pathPtr], distance*kClipperScale);
return retval;
}
- (void) cleanSelf
{
return [self cleanSelfWithDistance:kDefaultCleanDistance];
}
- (void) cleanSelfWithDistance:(double)distance
{
ClipperLib::CleanPolygon(_path, distance*kClipperScale);
}
#ifdef HAVE_POLY2TRI
template <class C> void FreeClear( C & cntr ) {
for ( typename C::iterator it = cntr.begin();
it != cntr.end(); ++it ) {
delete * it;
}
cntr.clear();
}
- (Paths*) triangulate
{
// Use the poly2tri library to split up any concave pieces
std::vector<p2t::Point*> polyline;
for (int j = 0 ; j < _path.size() ; j++)
{
polyline.push_back(new p2t::Point(_path[j].X, _path[j].Y));
}
p2t::CDT* cdt = new p2t::CDT(polyline);
cdt->Triangulate();
std::vector<p2t::Triangle*> triangles;
triangles = cdt->GetTriangles();
Paths* retval = [[Paths alloc] init];
ClipperLib::Paths paths;
for (p2t::Triangle* tri : triangles)
{
p2t::Point a = *(tri->GetPoint(0));
p2t::Point b = *(tri->GetPoint(1));
p2t::Point c = *(tri->GetPoint(2));
Path* p = [[Path alloc] init];
[p addIntPoint:ClipperLib::IntPoint(a.x, a.y)];
[p addIntPoint:ClipperLib::IntPoint(b.x, b.y)];
[p addIntPoint:ClipperLib::IntPoint(c.x, c.y)];
[retval addPath:p];
}
// Cleanup
delete cdt;
// Free points
FreeClear (polyline);
return retval;
}
#endif
- (void) print
{
printf("Orientation: %d (", [self orientation]);
for (int i = 0 ; i < _path.size() ; i++)
{
printf("(%f,%f)", (double)_path[i].X / (double)kClipperScale, (double)_path[i].Y / (double)kClipperScale);
if (i<_path.size()-1)
printf(",");
}
printf(")\n");
}
@end