Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized SimpleEleConstraintEnforcer.addConnectors (O(n^2) to O(nlogn)) #188

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -12,6 +13,12 @@
import java.util.Set;

import org.osm2world.core.map_elevation.data.EleConnector;
import org.osm2world.core.math.VectorXZ;
import org.apache.commons.math3.stat.descriptive.rank.Median;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.index.kdtree.KdNode;
import org.locationtech.jts.index.kdtree.KdTree;

/**
* enforcer implementation that ignores many of the constraints,
Expand All @@ -34,23 +41,35 @@ public final class SimpleEleConstraintEnforcer implements EleConstraintEnforcer

@Override
public void addConnectors(Iterable<EleConnector> newConnectors) {

for (EleConnector c : newConnectors) {
connectors.add(c);
}

/* build Kd-tree */
/* ArrayList<EleConnector> is stored in the KdNode */
KdTree kdTree = new KdTree();
for (EleConnector c : connectors) {
Coordinate co = new Coordinate(c.pos.x, c.pos.z);
KdNode node = kdTree.query(co);
if (node == null) {
ArrayList<EleConnector> cs = new ArrayList<EleConnector>();
cs.add(c);
kdTree.insert(co, cs);
} else {
ArrayList<EleConnector> cs = (ArrayList<EleConnector>) node.getData();
cs.add(c);
}
}
/* connect connectors */

for (EleConnector c1 : newConnectors) {
for (EleConnector c2 : connectors) {

Coordinate co = new Coordinate(c1.pos.x, c1.pos.z);
KdNode node = kdTree.query(co);
ArrayList<EleConnector> cs = (ArrayList<EleConnector>) node.getData();
for (EleConnector c2 : cs) {
if (c1 != c2 && c1.connectsTo(c2)) {
requireSameEle(c1, c2);
}

}
}

}

@Override
Expand Down