Skip to content

Commit b9bb776

Browse files
committed
perf(webgraphgen): 120% speed increase
1 parent 0bb8c00 commit b9bb776

File tree

1 file changed

+26
-73
lines changed

1 file changed

+26
-73
lines changed

utils/webgraphgen.simba

Lines changed: 26 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ var
186186
begin
187187
atpa := white.ClusterEx(1).SortBySize(True);
188188
doors := Self.FindDoors(red.ClusterEx(1), map);
189-
190189
SetLength(connectedDoors, Length(doors));
190+
with white.Bounds() do
191+
SetLength(connectionMap, X2, Y2);
191192

192193
for i := 0 to High(atpa) do
193194
begin
@@ -197,15 +198,23 @@ begin
197198
connectA := [];
198199
connectB := [];
199200

200-
for d := High(connectedDoors) downto 0 do
201+
hi := High(connectedDoors);
202+
for d := 0 to hi do
201203
begin
202-
if not connectedDoors[d].After then
203-
if atpa[i].Contains(doors[d].After) then
204-
connectA += d;
204+
if connectedDoors[d].After and connectedDoors[d].Before then
205+
begin
206+
Result.Doors += doors[d];
207+
Delete(doors, d, 1);
208+
Delete(connectedDoors, d, 1);
209+
hi -= 1;
210+
d -= 1;
211+
Continue;
212+
end;
205213

206-
if not connectedDoors[d].Before then
207-
if atpa[i].Contains(doors[d].Before) then
208-
connectB += d;
214+
if not connectedDoors[d].After and atpa[i].Contains(doors[d].After) then
215+
connectA += d;
216+
if not connectedDoors[d].Before and atpa[i].Contains(doors[d].Before) then
217+
connectB += d;
209218
end;
210219

211220
bounds := atpa[i].Bounds();
@@ -231,14 +240,6 @@ begin
231240
Result.Paths[High(Result.Nodes)] := [High(Result.Nodes)-1];
232241
end;
233242

234-
for d := High(doors) downto 0 do
235-
if connectedDoors[d].After and connectedDoors[d].Before then
236-
begin
237-
Delete(connectedDoors, d, 1);
238-
Result.Doors += doors[d];
239-
Delete(doors, d, 1);
240-
end;
241-
242243
Continue;
243244
end;
244245

@@ -266,22 +267,14 @@ begin
266267
Result.Paths[High(Result.Nodes)] := [High(Result.Nodes)-1];
267268
end;
268269

269-
for d := High(doors) downto 0 do
270-
if connectedDoors[d].After and connectedDoors[d].Before then
271-
begin
272-
Delete(connectedDoors, d, 1);
273-
Result.Doors += doors[d];
274-
Delete(doors, d, 1);
275-
end;
276-
277270
Continue;
278271
end;
279272

273+
nodes := [];
274+
280275
for tpa in skeleton.Partition(Self.Spacing) do
281-
begin
282-
if tpa = [] then Continue;
283-
for tpa in tpa.ClusterEx(1.5) do nodes += tpa.Mean();
284-
end;
276+
for tpa in tpa.ClusterEx(1.5) do
277+
nodes += tpa.Mean();
285278

286279
for d in connectA do
287280
begin
@@ -295,18 +288,11 @@ begin
295288
connectedDoors[d].Before := True;
296289
end;
297290

298-
for d := High(doors) downto 0 do
299-
if connectedDoors[d].After and connectedDoors[d].Before then
300-
begin
301-
Delete(connectedDoors, d, 1);
302-
Result.Doors += doors[d];
303-
Delete(doors, d, 1);
304-
end;
305-
306-
SetLength(connectionMap, bounds.X2, bounds.Y2);
307-
308-
skeletonTree.Init(skeleton);
309291
nodesTree.Init(nodes);
292+
len := Length(Result.Nodes);
293+
SetLength(Result.Nodes, len + Length(nodes));
294+
SetLength(Result.Paths, Length(Result.Nodes));
295+
310296
for j := 0 to High(nodesTree.Data) do
311297
begin
312298
p := nodesTree.Data[j].Split;
@@ -316,44 +302,13 @@ begin
316302
begin
317303
if Max(Abs(p.X - q.X), Abs(p.Y - q.Y)) > Self.Spacing * 4 then
318304
Continue;
319-
320-
if Self.WallCrossings then
321-
begin
322-
connectionMap[q.X, q.Y] += j;
305+
if not Self.WallCrossings and map.ColorsInLine(p, q, [$FF, $333333, $0]) then
323306
Continue;
324-
end;
325-
326-
bounds := TBox.Create(p.X, p.Y, q.X, q.Y).Expand(Self.Spacing);
327-
328-
329-
parts := skeletonTree.RangeQuery(bounds).Cluster(1);
330-
331-
for a := 0 to High(parts) do
332-
begin
333-
jInRange := False;
334-
nInRange := False;
335-
336-
for b := 0 to High(parts[a]) do
337-
begin
338-
jInRange := jInRange or (parts[a,b].DistanceTo(p) <= Sqrt(2));
339-
nInRange := nInRange or (parts[a,b].DistanceTo(q) <= Sqrt(2));
340-
341-
if jInRange and nInRange then Break(2);
342-
end;
343-
344-
if jInRange <> nInRange then Break;
345-
end;
346-
347-
if not jInRange or not nInRange then Continue;
348307

349308
connectionMap[q.X, q.Y] += j;
350309
end;
351310
end;
352311

353-
len := Length(Result.Nodes);
354-
SetLength(Result.Nodes, len + Length(nodes));
355-
SetLength(Result.Paths, Length(Result.Nodes));
356-
357312
for j := 0 to High(nodesTree.Data) do
358313
begin
359314
p := nodesTree.Data[j].Split;
@@ -368,8 +323,6 @@ begin
368323
Result.Paths[n+len] += j+len;
369324
end;
370325
end;
371-
372-
nodes := [];
373326
end;
374327

375328
SetLength(Result.Paths, Length(Result.Nodes));

0 commit comments

Comments
 (0)