-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridInPolygon.pas
330 lines (295 loc) · 8.54 KB
/
GridInPolygon.pas
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
unit GridInPolygon;
interface
uses
SysUtils, Geometry;
type
P2dByteArray = ^T2dByteArray;
T2dByteArray = array [0..MaxInt div SizeOf(PByteArray) - 1] of PByteArray;
function GridInPolygon(Polygon: PPolygon; X, Y: PFloatArray; CountX,
CountY: Integer; Grid: P2dByteArray; bBounded: Boolean): Boolean;
implementation
uses
Math;
type
PSegmentNode = ^TSegmentNode;
TSegmentNode = record
left: PVertex;
right: PVertex;
Next: PSegmentNode;
Less: PSegmentNode;
More: PSegmentNode;
end;
PPSegmentArray = ^PSegmentArray;
PSegmentArray = array [0..MaxInt div SizeOf(PSegmentNode) - 1] of PSegmentNode;
PCrossNode = ^TCrossNode;
TCrossNode = record
y: Float;
Next: PCrossNode;
end;
function GetLeft(v0, v1: PVertex): PVertex;
begin
if IsLs(v0.x, v1.x, EpsFloat) then
Result := v0
else
if IsLs(v1.x, v0.x, EpsFloat) then
Result := v1
else
if IsLs(v0.y, v1.y, EpsFloat) then
Result := v0
else
Result := v1;
end;
function GetRight(v0, v1: PVertex): PVertex;
begin
if IsGt(v0.x, v1.x, EpsFloat) then
Result := v0
else
if IsGt(v1.x, v0.x, EpsFloat) then
Result := v1
else
if IsGt(v0.y, v1.y, EpsFloat) then
Result := v0
else
Result := v1;
end;
function GetDy(v0, v1: PVertex): Float;
var
left, right: PVertex;
begin
left := GetLeft(v0, v1);
right := GetRight(v0, v1);
if IsEq(left.x, right.x, EpsFloat) then
if IsLs(right.y - left.y, 0.0, EpsFloat) then
Result := -MaxFloat
else
Result := MaxFloat
else
Result := (right.y - left.y) / (right.x - left.x);
end;
{ SegmentNode functions }
procedure InsertSegmentToTree(var Root: PSegmentNode; left, right: PVertex);
begin
if Root = nil then
begin
Root := AllocMem(SizeOf(TSegmentNode));
Root.left := left;
Root.right := right;
end
else if IsLs(left.x, Root.left.x, EpsFloat) then
InsertSegmentToTree(Root.Less, left, right)
else if IsGt(left.x, Root.left.x, EpsFloat) then
InsertSegmentToTree(Root.More, left, right)
else if IsLs(left.y, Root.left.y, EpsFloat) then
InsertSegmentToTree(Root.Less, left, right)
else if IsGt(left.y, Root.left.y, EpsFloat) then
InsertSegmentToTree(Root.More, left, right)
else if IsLs(GetDy(left, right), GetDy(Root.left, Root.right), EpsFloat) then
InsertSegmentToTree(Root.Less, left, right)
else
InsertSegmentToTree(Root.More, left, right);
end;
procedure InsertSegmentToList(var Root: PSegmentNode; Node: PSegmentNode);
begin
if Node = nil then
Exit;
Node.Next := Root;
Root := Node;
end;
procedure RemoveSegmentFromList(var Root: PSegmentNode; Prev, Node: PSegmentNode);
begin
if (Root = nil) or (Node = nil) then
Exit;
if Prev = nil then
Root := Node.Next
else
Prev.Next := Node.Next;
Node.Next := nil;
end;
procedure GetSegmentCount(Root: PSegmentNode; var Count: Integer);
begin
if Root.Less <> nil then GetSegmentCount(Root.Less, Count);
Inc(Count);
if Root.More <> nil then GetSegmentCount(Root.More, Count);
end;
procedure GetSegmentArray(Root: PSegmentNode; P: PPSegmentArray; var Count: Integer);
begin
if Root.Less <> nil then GetSegmentArray(Root.Less, P, Count);
P[Count] := Root;
Inc(Count);
if Root.More <> nil then GetSegmentArray(Root.More, P, Count);
end;
procedure ResetSegments(var Root: PSegmentNode);
begin
if Root <> nil then
begin
ResetSegments(Root.Less);
ResetSegments(Root.More);
Geometry.Free(Pointer(Root));
end;
end;
{ CrossNode functions }
procedure InsertCross(var Root: PCrossNode; y: Float);
var
ExistingNode: PCrossNode;
begin
if Root = nil then
begin
Root := AllocMem(SizeOf(TCrossNode));
Root.y := y;
end
else if IsLs(y, Root.y, EpsFloat) then
begin
ExistingNode := Root;
Root := AllocMem(SizeOf(TCrossNode));
Root.y := y;
Root.Next := ExistingNode;
end
else
InsertCross(Root.Next, y);
end;
function GetCrossCount(Root: PCrossNode): Integer;
begin
Result := 0;
while Root <> nil do
begin
Inc(Result);
Root := Root.Next;
end;
end;
procedure ResetCross(var Root: PCrossNode);
var
Next: PCrossNode;
begin
while Root <> nil do
begin
Next := Root.Next;
Geometry.Free(Pointer(Root));
Root := Next;
end;
end;
{ Implement functions }
function GetSegments(Polygon: PPolygon): PSegmentNode;
var
I, J: Integer;
v0, v1: PVertex;
begin
Result := nil;
if Polygon <> nil then
for I := 0 to Polygon.Count - 1 do
for J := 0 to Polygon.Contours[I].Count - 1 do
begin
v0 := @Polygon.Contours[I].Vertices[J];
v1 := @Polygon.Contours[I].Vertices[GetNext(J, Polygon.Contours[I].Count)];
if not PntsEqu(v0, v1) then
InsertSegmentToTree(Result, GetLeft(v0, v1), GetRight(v0, v1));
end;
end;
procedure ResetGrid(Grid: P2dByteArray; CountX, CountY: Integer);
var
I, J: Integer;
begin
for I := 0 to CountX - 1 do
for J := 0 to CountY - 1 do
Grid[I][J] := 0;
end;
function GetCrosses(var Segments: PSegmentNode; x: Float): PCrossNode;
var
Prev, Curr, Next: PSegmentNode;
t: Float;
begin
Result := nil;
Prev := nil;
Curr := Segments;
while Curr <> nil do
begin
Next := Curr.Next;
if IsGe(x, Curr.left.x, EpsFloat) and
IsLs(x, Curr.right.x, EpsFloat) then
begin
t := (x - Curr.left.x) / (Curr.right.x - Curr.left.x);
InsertCross(Result, Curr.left.y + t * (Curr.right.y - Curr.left.y));
Prev := Curr;
end
else
RemoveSegmentFromList(Segments, Prev, Curr);
Curr := Next;
end;
end;
function GridInPolygon(Polygon: PPolygon; X, Y: PFloatArray; CountX,
CountY: Integer; Grid: P2dByteArray; bBounded: Boolean): Boolean;
var
I, J: Integer;
Segments: PSegmentNode;
SegmentCount: Integer;
SegmentArray: PPSegmentArray;
TmpSegments: PSegmentNode;
Crosses: PCrossNode;
CrossNode: PCrossNode;
CrossIndex: Integer;
begin
Result := False;
if (Polygon = nil) or (X = nil) or (Y = nil) or (CountX = 0) or (CountY = 0) or (Grid = nil) then Exit;
Segments := GetSegments(Polygon);
if Segments <> nil then
try
SegmentCount := 0;
GetSegmentCount(Segments, SegmentCount);
if SegmentCount > 0 then
begin
SegmentArray := AllocMem(SegmentCount * SizeOf(PSegmentNode));
SegmentCount := 0;
GetSegmentArray(Segments, SegmentArray, SegmentCount);
try
ResetGrid(Grid, CountX, CountY);
TmpSegments := nil;
J := 0;
for I := 0 to CountX - 1 do
begin
if IsLs(X[I], SegmentArray[0].left.x, EpsFloat) then
Continue
else if IsGt(X[I], SegmentArray[SegmentCount - 1].left.x, EpsFloat) and
(TmpSegments = nil) then
Break;
while J < SegmentCount do
begin
if IsGe(X[I], SegmentArray[J].left.x, EpsFloat) and
IsLs(X[I], SegmentArray[J].right.x, EpsFloat) then
InsertSegmentToList(TmpSegments, SegmentArray[J])
else
if IsLs(X[I], SegmentArray[J].left.x, EpsFloat) then
Break;
Inc(J);
end;
Crosses := GetCrosses(TmpSegments, X[I]);
if Crosses <> nil then
try
CrossNode := Crosses;
CrossIndex := 0;
while (CrossNode <> nil) and (CrossNode.Next <> nil) do
begin
while CrossIndex < CountY do
begin
if (IsGt(Y[CrossIndex], CrossNode.Next.y, EpsFloat) and bBounded) or
(IsGe(Y[CrossIndex], CrossNode.Next.y, EpsFloat) and (not bBounded)) then
Break;
if (IsGe(Y[CrossIndex], CrossNode.y, EpsFloat) and bBounded) or
(IsGt(Y[CrossIndex], CrossNode.y, EpsFloat) and (not bBounded)) then
Grid[I][CrossIndex] := 1;
Inc(CrossIndex);
end;
CrossNode := CrossNode.Next.Next;
end;
finally
ResetCross(Crosses);
end;
end;
Result := True;
finally
Geometry.Free(Pointer(SegmentArray));
end;
end;
finally
ResetSegments(Segments);
end;
end;
end.