* and a {@link MapArea} to both, if it exists
*/
private static void addOverlapBetween(
MapWaySegment line, MapArea area) {
final LineSegmentXZ segmentXZ = line.getLineSegment();
/* check whether the line corresponds to one of the area segments */
for (MapAreaSegment areaSegment : area.getAreaSegments()) {
if (areaSegment.sharesBothNodes(line)) {
MapOverlapWA newOverlap =
new MapOverlapWA(line, area, MapOverlapType.SHARE_SEGMENT,
Collections.<VectorXZ>emptyList(),
Collections.<MapAreaSegment>emptyList());
line.addOverlap(newOverlap);
area.addOverlap(newOverlap);
return;
}
}
/* calculate whether the line contains or intersects the area (or neither) */
boolean contains;
boolean intersects;
{
final PolygonWithHolesXZ polygon = area.getPolygon();
if (!line.isConnectedTo(area)) {
intersects = polygon.intersects(segmentXZ);
contains = !intersects && polygon.contains(segmentXZ);
} else {
/* check whether the line intersects the area somewhere
* else than just at the common node(s).
*/
intersects = false;
double segmentLength = distance(segmentXZ.p1, segmentXZ.p2);
for (VectorXZ pos : polygon.intersectionPositions(segmentXZ)) {
if (distance(pos, segmentXZ.p1) > segmentLength / 100
&& distance(pos, segmentXZ.p2) > segmentLength / 100) {
intersects = true;
break;
}
}
/* check whether the area contains the line's center.
* Unless the line intersects the area outline,
* this means that the area contains the line itself.
*/
contains = !intersects && polygon.contains(segmentXZ.getCenter());
}
}
/* add an overlap if detected */
if (contains || intersects) {
/* find out which area segments intersect the way segment */
List<VectorXZ> intersectionPositions = emptyList();
List<MapAreaSegment> intersectingSegments = emptyList();
if (intersects) {
intersectionPositions = new ArrayList<VectorXZ>();
intersectingSegments = new ArrayList<MapAreaSegment>();
for (MapAreaSegment areaSegment : area.getAreaSegments()) {
VectorXZ intersection = segmentXZ.getIntersection(
areaSegment.getStartNode().getPos(),
areaSegment.getEndNode().getPos());
if (intersection != null) {
intersectionPositions.add(intersection);