ol-ext/utils/cspline.js
2016-10-16 15:20:10 +02:00

141 lines
4.5 KiB
JavaScript

/** Create a cardinal spline version of this geometry.
* Original https://github.com/epistemex/cardinal-spline-js
* @see https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
*
* @param {} options
* - tension {Number} a [0,1] number / can be interpreted as the "length" of the tangent, default 0.5
* - resolution {Number} size of segment to split
* - pointsPerSeg {Interger} number of points per segment to add if no resolution is provided, default add 10 points per segment
*/
/** Cache cspline calculation
*/
ol.geom.Geometry.prototype.cspline = function(options)
{ // Calculate cspline
if (this.calcCSpline_)
{ if (this.csplineGeometryRevision != this.getRevision()
|| this.csplineOption != JSON.stringify(options))
{ this.csplineGeometry_ = this.calcCSpline_(options)
this.csplineGeometryRevision = this.getRevision();
this.csplineOption = JSON.stringify(options);
}
return this.csplineGeometry_;
}
// Default do nothing
else
{ return this;
}
}
ol.geom.GeometryCollection.prototype.calcCSpline_ = function(options)
{ var g=[], g0=this.getGeometries();
for (var i=0; i<g0.length; i++)
{ g.push(g0[i].cspline());
}
return new ol.geom.GeometryCollection(g);
}
ol.geom.MultiLineString.prototype.calcCSpline_ = function(options)
{ var g=[], g0=this.getLineStrings();
for (var i=0; i<g0.length; i++)
{ g.push(g0[i].cspline().getCoordinates());
}
return new ol.geom.MultiLineString(g);
}
ol.geom.Polygon.prototype.calcCSpline_ = function(options)
{ var g=[], g0=this.getCoordinates();
for (var i=0; i<g0.length; i++)
{ g.push((new ol.geom.LineString(g0[i])).cspline().getCoordinates());
}
return new ol.geom.Polygon(g);
}
ol.geom.MultiPolygon.prototype.calcCSpline_ = function(options)
{ var g=[], g0=this.getPolygons();
for (var i=0; i<g0.length; i++)
{ g.push(g0[i].cspline().getCoordinates());
}
return new ol.geom.MultiPolygon(g);
}
/**
*/
ol.geom.LineString.prototype.calcCSpline_ = function(options)
{ if (!options) options={};
var line = this.getCoordinates();
var tension = typeof options.tension === "number" ? options.tension : 0.5;
var resolution = options.resolution || (this.getLength() / line.length / (options.pointsPerSeg || 10));
var pts, res = [], // clone array
x, y, // our x,y coords
t1x, t2x, t1y, t2y, // tension vectors
c1, c2, c3, c4, // cardinal points
st, t, i; // steps based on num. of segments
// clone array so we don't change the original
//
pts = line.slice(0);
// The algorithm require a previous and next point to the actual point array.
// Check if we will draw closed or open curve.
// If closed, copy end points to beginning and first points to end
// If open, duplicate first points to befinning, end points to end
if (line.length>2 && line[0][0]==line[line.length-1][0] && line[0][1]==line[line.length-1][1])
{ pts.unshift(line[line.length-2]);
pts.push(line[1]);
}
else
{ pts.unshift(line[0]);
pts.push(line[line.length-1]);
}
// ok, lets start..
function dist2d(x1, y1, x2, y2)
{ var dx = x2-x1;
var dy = y2-y1;
return Math.sqrt(dx*dx+dy*dy);
}
// 1. loop goes through point array
// 2. loop goes through each segment between the 2 pts + 1e point before and after
for (i=1; i < (pts.length - 2); i++)
{ var d1 = dist2d (pts[i][0], pts[i][1], pts[i+1][0], pts[i+1][1]);
var numOfSegments = Math.round(d1/resolution);
var d=1;
if (options.normalize)
{ var d1 = dist2d (pts[i+1][0], pts[i+1][1], pts[i-1][0], pts[i-1][1]);
var d2 = dist2d (pts[i+2][0], pts[i+2][1], pts[i][0], pts[i][1]);
if (d1<d2) d = d1/d2;
else d = d2/d1;
}
// calc tension vectors
t1x = (pts[i+1][0] - pts[i-1][0]) * tension *d;
t2x = (pts[i+2][0] - pts[i][0]) * tension *d;
t1y = (pts[i+1][1] - pts[i-1][1]) * tension *d;
t2y = (pts[i+2][1] - pts[i][1]) * tension *d;
for (t=0; t <= numOfSegments; t++)
{ // calc step
st = t / numOfSegments;
// calc cardinals
c1 = 2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1;
c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2);
c3 = Math.pow(st, 3) - 2 * Math.pow(st, 2) + st;
c4 = Math.pow(st, 3) - Math.pow(st, 2);
// calc x and y cords with common control vectors
x = c1 * pts[i][0] + c2 * pts[i+1][0] + c3 * t1x + c4 * t2x;
y = c1 * pts[i][1] + c2 * pts[i+1][1] + c3 * t1y + c4 * t2y;
//store points in array
res.push([x,y]);
}
}
return new ol.geom.LineString(res);
}