wayBounds[pos] = getNodesBounds(newNodes[pos]);
changedWays[pos] = false;
}
//iterate over all way pairs and introduce the intersections
Comparator<Node> coordsComparator = new NodePositionComparator();
for (int seg1Way = 0; seg1Way < n; seg1Way ++) {
for (int seg2Way = seg1Way; seg2Way < n; seg2Way ++) {
//do not waste time on bounds that do not intersect
if (!wayBounds[seg1Way].intersects(wayBounds[seg2Way])) {
continue;
}
List<Node> way1Nodes = newNodes[seg1Way];
List<Node> way2Nodes = newNodes[seg2Way];
//iterate over primary segmemt
for (int seg1Pos = 0; seg1Pos + 1 < way1Nodes.size(); seg1Pos ++) {
//iterate over secondary segment
int seg2Start = seg1Way != seg2Way ? 0: seg1Pos + 2;//skip the adjacent segment
for (int seg2Pos = seg2Start; seg2Pos + 1< way2Nodes.size(); seg2Pos ++) {
//need to get them again every time, because other segments may be changed
Node seg1Node1 = way1Nodes.get(seg1Pos);
Node seg1Node2 = way1Nodes.get(seg1Pos + 1);
Node seg2Node1 = way2Nodes.get(seg2Pos);
Node seg2Node2 = way2Nodes.get(seg2Pos + 1);
int commonCount = 0;
//test if we have common nodes to add.
if (seg1Node1 == seg2Node1 || seg1Node1 == seg2Node2) {
commonCount ++;
if (seg1Way == seg2Way &&
seg1Pos == 0 &&
seg2Pos == way2Nodes.size() -2) {
//do not add - this is first and last segment of the same way.
} else {
intersectionNodes.add(seg1Node1);
}
}
if (seg1Node2 == seg2Node1 || seg1Node2 == seg2Node2) {
commonCount ++;
intersectionNodes.add(seg1Node2);
}
//no common nodes - find intersection
if (commonCount == 0) {
EastNorth intersection = getSegmentSegmentIntersection(
seg1Node1.getEastNorth(), seg1Node2.getEastNorth(),
seg2Node1.getEastNorth(), seg2Node2.getEastNorth());
if (intersection != null) {
if (test) {
intersectionNodes.add(seg2Node1);
return intersectionNodes;
}
Node newNode = new Node(Main.getProjection().eastNorth2latlon(intersection));
Node intNode = newNode;
boolean insertInSeg1 = false;
boolean insertInSeg2 = false;
//find if the intersection point is at end point of one of the segments, if so use that point
//segment 1
if (coordsComparator.compare(newNode, seg1Node1) == 0) {
intNode = seg1Node1;
} else if (coordsComparator.compare(newNode, seg1Node2) == 0) {
intNode = seg1Node2;
} else {
insertInSeg1 = true;
}
//segment 2
if (coordsComparator.compare(newNode, seg2Node1) == 0) {
intNode = seg2Node1;
} else if (coordsComparator.compare(newNode, seg2Node2) == 0) {
intNode = seg2Node2;
} else {
insertInSeg2 = true;
}