/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.noding.snapround;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.locationtech.jts.algorithm.LineIntersector;
import org.locationtech.jts.algorithm.RobustLineIntersector;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.noding.InteriorIntersectionFinderAdder;
import org.locationtech.jts.noding.MCIndexNoder;
import org.locationtech.jts.noding.NodedSegmentString;
import org.locationtech.jts.noding.Noder;
import org.locationtech.jts.noding.NodingValidator;
import org.locationtech.jts.noding.SegmentString;
/**
* Uses Snap Rounding to compute a rounded,
* fully noded arrangement from a set of {@link SegmentString}s.
* Implements the Snap Rounding technique described in
* papers by Hobby, Guibas & Marimont, and Goodrich et al.
* Snap Rounding assumes that all vertices lie on a uniform grid;
* hence the precision model of the input must be fixed precision,
* and all the input vertices must be rounded to that precision.
* <p>
* This implementation uses a monotone chains and a spatial index to
* speed up the intersection tests.
* <p>
* This implementation appears to be fully robust using an integer precision model.
* It will function with non-integer precision models, but the
* results are not 100% guaranteed to be correctly noded.
*
* @version 1.7
*/
public class MCIndexSnapRounder
implements Noder
{
private final PrecisionModel pm;
private LineIntersector li;
private final double scaleFactor;
private MCIndexNoder noder;
private MCIndexPointSnapper pointSnapper;
private Collection nodedSegStrings;
public MCIndexSnapRounder(PrecisionModel pm) {
this.pm = pm;
li = new RobustLineIntersector();
li.setPrecisionModel(pm);
scaleFactor = pm.getScale();
}
public Collection getNodedSubstrings()
{
return NodedSegmentString.getNodedSubstrings(nodedSegStrings);
}
public void computeNodes(Collection inputSegmentStrings)
{
this.nodedSegStrings = inputSegmentStrings;
noder = new MCIndexNoder();
pointSnapper = new MCIndexPointSnapper(noder.getIndex());
snapRound(inputSegmentStrings, li);
// testing purposes only - remove in final version
//checkCorrectness(inputSegmentStrings);
}
private void checkCorrectness(Collection inputSegmentStrings)
{
Collection resultSegStrings = NodedSegmentString.getNodedSubstrings(inputSegmentStrings);
NodingValidator nv = new NodingValidator(resultSegStrings);
try {
nv.checkValid();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void snapRound(Collection segStrings, LineIntersector li)
{
List intersections = findInteriorIntersections(segStrings, li);
computeIntersectionSnaps(intersections);
computeVertexSnaps(segStrings);
}
/**
* Computes all interior intersections in the collection of {@link SegmentString}s,
* and returns their {@link Coordinate}s.
*
* Does NOT node the segStrings.
*
* @return a list of Coordinates for the intersections
*/
private List findInteriorIntersections(Collection segStrings, LineIntersector li)
{
InteriorIntersectionFinderAdder intFinderAdder = new InteriorIntersectionFinderAdder(li);
noder.setSegmentIntersector(intFinderAdder);
noder.computeNodes(segStrings);
return intFinderAdder.getInteriorIntersections();
}
/**
* Snaps segments to nodes created by segment intersections.
*/
private void computeIntersectionSnaps(Collection snapPts)
{
for (Iterator it = snapPts.iterator(); it.hasNext(); ) {
Coordinate snapPt = (Coordinate) it.next();
HotPixel hotPixel = new HotPixel(snapPt, scaleFactor, li);
pointSnapper.snap(hotPixel);
}
}
/**
* Snaps segments to all vertices.
*
* @param edges the list of segment strings to snap together
*/
public void computeVertexSnaps(Collection edges)
{
for (Iterator i0 = edges.iterator(); i0.hasNext(); ) {
NodedSegmentString edge0 = (NodedSegmentString) i0.next();
computeVertexSnaps(edge0);
}
}
/**
* Snaps segments to the vertices of a Segment String.
*/
private void computeVertexSnaps(NodedSegmentString e)
{
Coordinate[] pts0 = e.getCoordinates();
for (int i = 0; i < pts0.length ; i++) {
HotPixel hotPixel = new HotPixel(pts0[i], scaleFactor, li);
boolean isNodeAdded = pointSnapper.snap(hotPixel, e, i);
// if a node is created for a vertex, that vertex must be noded too
if (isNodeAdded) {
e.addIntersection(pts0[i], i);
}
}
}
}