Skip to content

Commit

Permalink
Optimize memory and time overlap detector
Browse files Browse the repository at this point in the history
Do not store all points to new arraylist also do not copy list every
check operation.
  • Loading branch information
praszuk committed Dec 4, 2024
1 parent 076d001 commit 59b8954
Showing 1 changed file with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.openstreetmap.josm.tools.Geometry.nodeInsidePolygon;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.DoubleStream;
import org.openstreetmap.josm.data.coor.LatLon;
Expand Down Expand Up @@ -64,22 +64,24 @@ public static double detect(OsmPrimitive building1, OsmPrimitive building2) {
AtomicInteger b1Counter = new AtomicInteger(); // only nodes in the first building without in both
AtomicInteger b2Counter = new AtomicInteger(); // similar as up, but second building

ArrayList<Node> nodesToCheck = new ArrayList<>();
DoubleStream.iterate(minLat, lat -> lat + freqDegreeStep).limit(latPointCount + 1).forEach(
lat -> DoubleStream.iterate(minLon, lon -> lon + freqDegreeStep).limit(lonPointCount + 1)
.forEach(lon -> nodesToCheck.add(new Node(new LatLon(lat, lon)))));

nodesToCheck.forEach(node -> {
boolean isB1 = nodeInsidePolygon(node, b1.getNodes());
boolean isB2 = nodeInsidePolygon(node, b2.getNodes());
if (isB1 && isB2) {
bothCounter.getAndIncrement();
} else if (isB1) {
b1Counter.getAndIncrement();
} else if (isB2) {
b2Counter.getAndIncrement();
}
});
List<Node> b1Nodes = b1.getNodes();
List<Node> b2Nodes = b2.getNodes();
DoubleStream.iterate(minLat, lat -> lat + freqDegreeStep)
.limit(latPointCount + 1)
.forEach(
lat -> DoubleStream.iterate(minLon, lon -> lon + freqDegreeStep).limit(lonPointCount + 1)
.forEach(lon -> {
Node node = new Node(new LatLon(lat, lon));
boolean isB1 = nodeInsidePolygon(node, b1Nodes);
boolean isB2 = nodeInsidePolygon(node, b2Nodes);
if (isB1 && isB2) {
bothCounter.getAndIncrement();
} else if (isB1) {
b1Counter.getAndIncrement();
} else if (isB2) {
b2Counter.getAndIncrement();
}
}));

// 5 types of intersection

Expand Down

0 comments on commit 59b8954

Please sign in to comment.