2011-04-11 13:27:11 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-04-11 13:27:11 -04:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2014-01-03 19:47:16 -05:00
|
|
|
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-04-11 13:27:11 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name CurveLocation
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class CurveLocation objects describe a location on {@link Curve}
|
|
|
|
* objects, as defined by the curve {@link #parameter}, a value between
|
|
|
|
* {@code 0} (beginning of the curve) and {@code 1} (end of the curve). If
|
|
|
|
* the curve is part of a {@link Path} item, its {@link #index} inside the
|
|
|
|
* {@link Path#curves} array is also provided.
|
2011-07-07 10:10:27 -04:00
|
|
|
*
|
|
|
|
* The class is in use in many places, such as
|
2013-01-28 19:30:28 -05:00
|
|
|
* {@link Path#getLocationAt(offset, isParameter)},
|
2012-12-27 15:08:03 -05:00
|
|
|
* {@link Path#getLocationOf(point)},
|
2012-12-30 13:43:35 -05:00
|
|
|
* {@link Path#getNearestLocation(point),
|
2012-12-27 15:08:03 -05:00
|
|
|
* {@link PathItem#getIntersections(path)},
|
|
|
|
* etc.
|
2011-06-22 18:56:05 -04:00
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
var CurveLocation = Base.extend(/** @lends CurveLocation# */{
|
2014-08-16 13:24:54 -04:00
|
|
|
_class: 'CurveLocation',
|
|
|
|
// Enforce creation of beans, as bean getters have hidden parameters.
|
|
|
|
// See #getSegment() below.
|
|
|
|
beans: true,
|
2014-04-02 14:53:18 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
// DOCS: CurveLocation class description: add these back when the mentioned
|
|
|
|
// functioned have been added: {@link Path#split(location)}
|
|
|
|
/**
|
|
|
|
* Creates a new CurveLocation object.
|
|
|
|
*
|
|
|
|
* @param {Curve} curve
|
|
|
|
* @param {Number} parameter
|
2015-06-16 13:36:22 -04:00
|
|
|
* @param {Point} [point]
|
2014-08-16 13:24:54 -04:00
|
|
|
*/
|
|
|
|
initialize: function CurveLocation(curve, parameter, point, _curve2,
|
|
|
|
_parameter2, _point2, _distance) {
|
|
|
|
// Define this CurveLocation's unique id.
|
2015-05-11 13:39:39 -04:00
|
|
|
// NOTE: We do not use the same pool as the rest of the library here,
|
|
|
|
// since this is only required to be unique at runtime among other
|
|
|
|
// CurveLocation objects.
|
2015-06-16 12:12:40 -04:00
|
|
|
this._id = UID.get(CurveLocation);
|
2015-06-16 13:36:22 -04:00
|
|
|
var path = curve._path;
|
|
|
|
this._version = path ? path._version : 0;
|
2014-08-16 13:24:54 -04:00
|
|
|
this._curve = curve;
|
|
|
|
this._parameter = parameter;
|
2015-06-16 13:36:22 -04:00
|
|
|
this._point = point || curve.getPointAt(parameter, true);
|
2014-08-16 13:24:54 -04:00
|
|
|
this._curve2 = _curve2;
|
|
|
|
this._parameter2 = _parameter2;
|
|
|
|
this._point2 = _point2;
|
|
|
|
this._distance = _distance;
|
2015-06-16 13:36:22 -04:00
|
|
|
// Also store references to segment1 and segment2, in case path
|
|
|
|
// splitting / dividing is going to happen, in which case the segments
|
|
|
|
// can be used to determine the new curves, see #getCurve(true)
|
|
|
|
this._segment1 = curve._segment1;
|
|
|
|
this._segment2 = curve._segment2;
|
2014-08-16 13:24:54 -04:00
|
|
|
},
|
2011-04-11 13:27:11 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The segment of the curve which is closer to the described location.
|
|
|
|
*
|
|
|
|
* @type Segment
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getSegment: function(_preferFirst) {
|
|
|
|
if (!this._segment) {
|
|
|
|
var curve = this.getCurve(),
|
|
|
|
parameter = this.getParameter();
|
|
|
|
if (parameter === 1) {
|
|
|
|
this._segment = curve._segment2;
|
|
|
|
} else if (parameter === 0 || _preferFirst) {
|
|
|
|
this._segment = curve._segment1;
|
|
|
|
} else if (parameter == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// Determine the closest segment by comparing curve lengths
|
|
|
|
this._segment = curve.getPartLength(0, parameter)
|
|
|
|
< curve.getPartLength(parameter, 1)
|
|
|
|
? curve._segment1
|
|
|
|
: curve._segment2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._segment;
|
|
|
|
},
|
2011-04-11 13:27:11 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The curve that this location belongs to.
|
|
|
|
*
|
|
|
|
* @type Curve
|
|
|
|
* @bean
|
|
|
|
*/
|
2015-06-16 13:36:22 -04:00
|
|
|
getCurve: function() {
|
|
|
|
var curve = this._curve,
|
|
|
|
path = curve && curve._path;
|
|
|
|
if (path && path._version !== this._version) {
|
|
|
|
// If the path's segments have changed in the meantime, clear the
|
|
|
|
// internal _parameter value and force refetching of the correct
|
|
|
|
// curve again here.
|
|
|
|
curve = null;
|
|
|
|
this._parameter = null;
|
|
|
|
}
|
|
|
|
if (!curve) {
|
2014-08-16 13:24:54 -04:00
|
|
|
// If we're asked to get the curve uncached, access current curve
|
|
|
|
// objects through segment1 / segment2. Since path splitting or
|
|
|
|
// dividing might have happened in the meantime, try segment1's
|
|
|
|
// curve, and see if _point lies on it still, otherwise assume it's
|
|
|
|
// the curve before segment2.
|
2015-06-16 13:36:22 -04:00
|
|
|
curve = this._segment1.getCurve();
|
|
|
|
if (curve.getParameterOf(this._point) == null)
|
|
|
|
curve = this._segment2.getPrevious().getCurve();
|
|
|
|
this._curve = curve;
|
|
|
|
// Fetch path again as it could be on a new one through split()
|
|
|
|
path = curve._path;
|
|
|
|
this._version = path ? path._version : 0;
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
2015-06-16 13:36:22 -04:00
|
|
|
return curve;
|
2014-08-16 13:24:54 -04:00
|
|
|
},
|
2013-04-30 21:41:26 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The path this curve belongs to, if any.
|
|
|
|
*
|
|
|
|
* @type Item
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getPath: function() {
|
|
|
|
var curve = this.getCurve();
|
|
|
|
return curve && curve._path;
|
|
|
|
},
|
2011-04-26 12:49:54 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The index of the curve within the {@link Path#curves} list, if the
|
|
|
|
* curve is part of a {@link Path} item.
|
|
|
|
*
|
|
|
|
* @type Index
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getIndex: function() {
|
|
|
|
var curve = this.getCurve();
|
|
|
|
return curve && curve.getIndex();
|
|
|
|
},
|
2011-04-11 13:27:11 -04:00
|
|
|
|
2015-06-16 13:36:22 -04:00
|
|
|
/**
|
|
|
|
* The curve parameter, as used by various bezier curve calculations. It is
|
|
|
|
* value between {@code 0} (beginning of the curve) and {@code 1} (end of
|
|
|
|
* the curve).
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getParameter: function() {
|
|
|
|
var curve = this.getCurve(),
|
|
|
|
parameter = this._parameter;
|
|
|
|
return curve && parameter == null
|
|
|
|
? this._parameter = curve.getParameterOf(this._point)
|
|
|
|
: parameter;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The point which is defined by the {@link #curve} and
|
|
|
|
* {@link #parameter}.
|
|
|
|
*
|
|
|
|
* @type Point
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getPoint: function() {
|
|
|
|
return this._point;
|
|
|
|
},
|
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The length of the path from its beginning up to the location described
|
|
|
|
* by this object. If the curve is not part of a path, then the length
|
|
|
|
* within the curve is returned instead.
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getOffset: function() {
|
|
|
|
var path = this.getPath();
|
|
|
|
return path ? path._getOffset(this) : this.getCurveOffset();
|
|
|
|
},
|
2011-04-11 13:27:11 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The length of the curve from its beginning up to the location described
|
|
|
|
* by this object.
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getCurveOffset: function() {
|
|
|
|
var curve = this.getCurve(),
|
|
|
|
parameter = this.getParameter();
|
|
|
|
return parameter != null && curve && curve.getPartLength(0, parameter);
|
|
|
|
},
|
2011-04-11 13:27:11 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
2015-06-16 13:36:22 -04:00
|
|
|
* The curve location on the intersecting curve, if this location is the
|
|
|
|
* result of a call to {@link PathItem#getIntersections(path)} /
|
|
|
|
* {@link Curve#getIntersections(curve)}.
|
2014-08-16 13:24:54 -04:00
|
|
|
*
|
2015-06-16 13:36:22 -04:00
|
|
|
* @type CurveLocation
|
2014-08-16 13:24:54 -04:00
|
|
|
* @bean
|
|
|
|
*/
|
2015-06-16 13:36:22 -04:00
|
|
|
getIntersection: function() {
|
|
|
|
var intersection = this._intersection;
|
|
|
|
if (!intersection && this._curve2) {
|
|
|
|
// If we have the parameter on the other curve use that for
|
|
|
|
// intersection rather than the point.
|
|
|
|
this._intersection = intersection = new CurveLocation(this._curve2,
|
2015-08-23 15:19:19 -04:00
|
|
|
this._parameter2, this._point2 || this._point);
|
|
|
|
intersection._overlap = this._overlap;
|
2015-06-16 13:36:22 -04:00
|
|
|
intersection._intersection = this;
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
2015-06-16 13:36:22 -04:00
|
|
|
return intersection;
|
2014-08-16 13:24:54 -04:00
|
|
|
},
|
2011-04-11 13:27:11 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The tangential vector to the {@link #curve} at the given location.
|
|
|
|
*
|
2014-08-26 04:01:30 -04:00
|
|
|
* @name Item#tangent
|
2014-08-16 13:24:54 -04:00
|
|
|
* @type Point
|
|
|
|
*/
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The normal vector to the {@link #curve} at the given location.
|
|
|
|
*
|
2014-08-26 04:01:30 -04:00
|
|
|
* @name Item#normal
|
2014-08-16 13:24:54 -04:00
|
|
|
* @type Point
|
|
|
|
*/
|
2014-07-25 14:05:44 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The curvature of the {@link #curve} at the given location.
|
|
|
|
*
|
2014-08-26 04:01:30 -04:00
|
|
|
* @name Item#curvature
|
2014-08-16 13:24:54 -04:00
|
|
|
* @type Number
|
|
|
|
*/
|
2011-04-27 14:26:03 -04:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* The distance from the queried point to the returned location.
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
* @bean
|
2015-08-23 15:19:19 -04:00
|
|
|
* @see Curve#getNearestLocation(point)
|
|
|
|
* @see Path#getNearestLocation(point)
|
2014-08-16 13:24:54 -04:00
|
|
|
*/
|
|
|
|
getDistance: function() {
|
|
|
|
return this._distance;
|
|
|
|
},
|
2011-07-06 16:19:01 -04:00
|
|
|
|
2015-06-16 13:36:22 -04:00
|
|
|
// DOCS: divide(), split()
|
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
divide: function() {
|
2015-06-16 13:36:22 -04:00
|
|
|
var curve = this.getCurve();
|
|
|
|
return curve && curve.divide(this.getParameter(), true);
|
2014-08-16 13:24:54 -04:00
|
|
|
},
|
2012-12-31 15:42:55 -05:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
split: function() {
|
2015-06-16 13:36:22 -04:00
|
|
|
var curve = this.getCurve();
|
|
|
|
return curve && curve.split(this.getParameter(), true);
|
2014-08-16 13:24:54 -04:00
|
|
|
},
|
2012-12-30 13:53:09 -05:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* Checks whether tow CurveLocation objects are describing the same location
|
|
|
|
* on a path, by applying the same tolerances as elsewhere when dealing with
|
|
|
|
* curve time parameters.
|
|
|
|
*
|
|
|
|
* @param {CurveLocation} location
|
|
|
|
* @return {Boolean} {@true if the locations are equal}
|
|
|
|
*/
|
|
|
|
equals: function(loc) {
|
2015-01-02 15:20:28 -05:00
|
|
|
var abs = Math.abs,
|
|
|
|
// Use the same tolerance for curve time parameter comparisons as
|
|
|
|
// in Curve.js when considering two locations the same.
|
|
|
|
tolerance = /*#=*/Numerical.TOLERANCE;
|
2014-08-16 13:24:54 -04:00
|
|
|
return this === loc
|
2015-06-16 13:36:22 -04:00
|
|
|
|| loc instanceof CurveLocation
|
|
|
|
// Call getCurve() and getParameter() to keep in sync
|
|
|
|
&& this.getCurve() === loc.getCurve()
|
|
|
|
&& abs(this.getParameter() - loc.getParameter()) < tolerance
|
|
|
|
// _curve2/_parameter2 are only used for Boolean operations
|
|
|
|
// and don't need syncing there.
|
2015-08-26 10:56:28 -04:00
|
|
|
// TODO: That's not quite true though... Rework this!
|
2014-08-16 13:24:54 -04:00
|
|
|
&& this._curve2 === loc._curve2
|
2015-08-26 05:43:39 -04:00
|
|
|
&& abs((this._parameter2 || 0) - (loc._parameter2 || 0))
|
|
|
|
< tolerance
|
2014-08-16 13:24:54 -04:00
|
|
|
|| false;
|
|
|
|
},
|
2014-02-20 09:37:49 -05:00
|
|
|
|
2014-08-16 13:24:54 -04:00
|
|
|
/**
|
|
|
|
* @return {String} a string representation of the curve location
|
|
|
|
*/
|
|
|
|
toString: function() {
|
|
|
|
var parts = [],
|
|
|
|
point = this.getPoint(),
|
|
|
|
f = Formatter.instance;
|
|
|
|
if (point)
|
|
|
|
parts.push('point: ' + point);
|
|
|
|
var index = this.getIndex();
|
|
|
|
if (index != null)
|
|
|
|
parts.push('index: ' + index);
|
|
|
|
var parameter = this.getParameter();
|
|
|
|
if (parameter != null)
|
|
|
|
parts.push('parameter: ' + f.number(parameter));
|
|
|
|
if (this._distance != null)
|
|
|
|
parts.push('distance: ' + f.number(this._distance));
|
|
|
|
return '{ ' + parts.join(', ') + ' }';
|
2015-08-26 10:56:28 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
statics: {
|
|
|
|
sort: function(locations) {
|
2015-08-26 11:06:02 -04:00
|
|
|
var tolerance = /*#=*/Numerical.TOLERANCE;
|
2015-08-26 10:56:28 -04:00
|
|
|
locations.sort(function compare(l1, l2) {
|
|
|
|
var curve1 = l1._curve,
|
|
|
|
curve2 = l2._curve,
|
|
|
|
path1 = curve1._path,
|
|
|
|
path2 = curve2._path;
|
|
|
|
// Sort by path-id, curve, parameter, curve2, parameter2 so we
|
|
|
|
// can easily remove duplicates with calls to equals() after.
|
|
|
|
return path1 === path2
|
|
|
|
? curve1 === curve2
|
|
|
|
? Math.abs(l1._parameter - l2._parameter) < tolerance
|
|
|
|
? l1._curve2 === l2._curve2
|
|
|
|
? l1._parameter2 - l2._parameter2
|
|
|
|
: l1._curve2.getIndex() - l2._curve2.getIndex()
|
|
|
|
: l1._parameter - l2._parameter
|
|
|
|
: curve1.getIndex() - curve2.getIndex()
|
|
|
|
// Sort by path id to group all locs on the same path.
|
|
|
|
: path1._id - path2._id;
|
|
|
|
});
|
|
|
|
}
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
2015-08-19 11:26:52 -04:00
|
|
|
}, Base.each(Curve.evaluateMethods, function(name) {
|
2014-08-16 13:24:54 -04:00
|
|
|
// Produce getters for #getTangent() / #getNormal() / #getCurvature()
|
2015-08-19 11:26:52 -04:00
|
|
|
if (name !== 'getPoint') {
|
|
|
|
var get = name + 'At';
|
|
|
|
this[name] = function() {
|
|
|
|
var parameter = this.getParameter(),
|
|
|
|
curve = this.getCurve();
|
|
|
|
return parameter != null && curve && curve[get](parameter, true);
|
|
|
|
};
|
|
|
|
}
|
2014-09-27 16:03:20 -04:00
|
|
|
}, {}));
|