/*
* The JTS Topology Suite is a collection of Java classes that
* implement the fundamental operations required to validate a given
* geo-spatial data set to a known topological specification.
*
* Copyright (C) 2001 Vivid Solutions
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, contact:
*
* Vivid Solutions
* Suite #1A
* 2328 Government Street
* Victoria BC V8T 5G5
* Canada
*
* (250)385-6040
* www.vividsolutions.com
*/
package com.revolsys.geometry.simplify;
import com.revolsys.geometry.model.LineString;
import com.revolsys.geometry.model.Point;
import com.revolsys.geometry.model.PointList;
import com.revolsys.geometry.model.coordinates.LineSegmentUtil;
/**
* Simplifies a linestring (sequence of points) using
* the standard Douglas-Peucker algorithm.
*
* @version 1.7
*/
class DouglasPeuckerLineSimplifier {
public static Point[] simplify(final LineString pts, final double distanceTolerance) {
final DouglasPeuckerLineSimplifier simp = new DouglasPeuckerLineSimplifier(pts);
simp.setDistanceTolerance(distanceTolerance);
return simp.simplify();
}
private double distanceTolerance;
private final LineString pts;
private boolean[] usePt;
public DouglasPeuckerLineSimplifier(final LineString pts) {
this.pts = pts;
}
/**
* Sets the distance tolerance for the simplification.
* All vertices in the simplified linestring will be within this
* distance of the original linestring.
*
* @param distanceTolerance the approximation tolerance to use
*/
public void setDistanceTolerance(final double distanceTolerance) {
this.distanceTolerance = distanceTolerance;
}
public Point[] simplify() {
this.usePt = new boolean[this.pts.getVertexCount()];
for (int i = 0; i < this.pts.getVertexCount(); i++) {
this.usePt[i] = true;
}
simplifySection(0, this.pts.getVertexCount() - 1);
final PointList coordList = new PointList();
for (int i = 0; i < this.pts.getVertexCount(); i++) {
if (this.usePt[i]) {
coordList.add(this.pts.getPoint(i));
}
}
return coordList.toPointArray();
}
private void simplifySection(final int i, final int j) {
if (i + 1 == j) {
return;
}
final Point p0 = this.pts.getPoint(i);
final Point p1 = this.pts.getPoint(j);
double maxDistance = -1.0;
int maxIndex = i;
for (int k = i + 1; k < j; k++) {
final double distance = LineSegmentUtil.distanceLinePoint(p0, p1, this.pts.getPoint(k));
if (distance > maxDistance) {
maxDistance = distance;
maxIndex = k;
}
}
if (maxDistance <= this.distanceTolerance) {
for (int k = i + 1; k < j; k++) {
this.usePt[k] = false;
}
} else {
simplifySection(i, maxIndex);
simplifySection(maxIndex, j);
}
}
}