paper.js/src/path/CurveLocation.js

443 lines
15 KiB
JavaScript
Raw Normal View History

2011-04-11 13:27:11 -04:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
2011-04-11 13:27:11 -04:00
* http://paperjs.org/
*
2014-01-03 19:47:16 -05:00
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
* http://scratchdisk.com/ & http://jonathanpuckey.com/
*
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.
*/
/**
* @name CurveLocation
*
* @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
* {@link Path#getLocationAt(offset, isParameter)},
* {@link Path#getLocationOf(point)},
* {@link Path#getNearestLocation(point),
* {@link PathItem#getIntersections(path)},
* etc.
*/
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-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
* @param {Point} [point]
2014-08-16 13:24:54 -04:00
*/
initialize: function CurveLocation(curve, parameter, point,
_distance, _overlap, _intersection) {
// Merge intersections very close to the end of a curve to the
// beginning of the next curve.
if (parameter >= 1 - /*#=*/Numerical.CURVETIME_EPSILON) {
var next = curve.getNext();
if (next) {
parameter = 0;
curve = next;
}
}
2014-08-16 13:24:54 -04:00
// Define this CurveLocation's unique id.
// 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.
this._id = UID.get(CurveLocation);
this._setCurve(curve);
2014-08-16 13:24:54 -04:00
this._parameter = parameter;
this._point = point || curve.getPointAt(parameter, true);
2014-08-16 13:24:54 -04:00
this._distance = _distance;
this._overlap = _overlap;
this._crossing = null;
this._intersection = _intersection;
if (_intersection) {
_intersection._intersection = this;
// TODO: Remove this once debug logging is removed.
_intersection._other = true;
}
},
_setCurve: function(curve) {
var path = curve._path;
this._version = path ? path._version : 0;
this._curve = curve;
this._segment = null; // To be determined, see #getSegment()
// 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() {
// Request curve first, so _segment gets invalidated if it's out of sync
var curve = this.getCurve(),
segment = this._segment;
if (!segment) {
var parameter = this.getParameter();
if (parameter === 0) {
segment = curve._segment1;
} else if (parameter === 1) {
segment = curve._segment2;
} else if (parameter != null) {
2014-08-16 13:24:54 -04:00
// Determine the closest segment by comparing curve lengths
segment = curve.getPartLength(0, parameter)
2014-08-16 13:24:54 -04:00
< curve.getPartLength(parameter, 1)
? curve._segment1
: curve._segment2;
}
this._segment = segment;
2014-08-16 13:24:54 -04:00
}
return segment;
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 curve that this location belongs to.
*
* @type Curve
* @bean
*/
getCurve: function() {
var curve = this._curve,
path = curve && curve._path,
that = this;
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 = this._parameter = this._curve = null;
}
// If path is out of sync, 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.
function trySegment(segment) {
var curve = segment && segment.getCurve();
if (curve && (that._parameter = curve.getParameterOf(that._point))
!= null) {
// Fetch path again as it could be on a new one through split()
that._setCurve(curve);
that._segment = segment;
return curve;
}
2014-08-16 13:24:54 -04:00
}
return curve
|| trySegment(this._segment)
|| trySegment(this._segment1)
|| trySegment(this._segment2.getPrevious());
2014-08-16 13:24:54 -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;
},
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
/**
* 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
/**
* 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
*
* @type CurveLocation
2014-08-16 13:24:54 -04:00
* @bean
*/
getIntersection: function() {
return this._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.
*
* @name Item#tangent
2014-08-16 13:24:54 -04:00
* @type Point
*/
2014-08-16 13:24:54 -04:00
/**
* The normal vector to the {@link #curve} at the given location.
*
* @name Item#normal
2014-08-16 13:24:54 -04:00
* @type Point
*/
2014-08-16 13:24:54 -04:00
/**
* The curvature of the {@link #curve} at the given location.
*
* @name Item#curvature
2014-08-16 13:24:54 -04:00
* @type Number
*/
2014-08-16 13:24:54 -04:00
/**
* The distance from the queried point to the returned location.
*
* @type Number
* @bean
* @see Curve#getNearestLocation(point)
* @see Path#getNearestLocation(point)
2014-08-16 13:24:54 -04:00
*/
getDistance: function() {
return this._distance;
},
// DOCS: divide(), split()
2014-08-16 13:24:54 -04:00
divide: function() {
var curve = this.getCurve();
return curve && curve.divide(this.getParameter(), true);
2014-08-16 13:24:54 -04:00
},
2014-08-16 13:24:54 -04:00
split: function() {
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
isCrossing: function(report) {
// Implementation based on work by Andy Finnell:
// http://losingfight.com/blog/2011/07/09/how-to-implement-boolean-operations-on-bezier-paths-part-3/
// https://bitbucket.org/andyfinnell/vectorboolean
var intersection = this._intersection,
crossing = this._crossing;
2015-09-15 20:29:17 -04:00
if (crossing != null || !intersection)
return crossing || false;
// TODO: isTangent() ?
// TODO: isAtEndPoint() ?
// -> Return if it's a tangent, or if not at an end point, only end
// point intersections need more checking!
// Values for getTangentAt() that are almost 0 and 1.
// NOTE: Even though getTangentAt() has code to support 0 and 1 instead
// of tMin and tMax, we still need to use this instead, as other issues
// emerge from switching to 0 and 1 in edge cases.
// NOTE: VectorBoolean has code that slowly shifts these points inwards
// until the resulting tangents are not ambiguous. Do we need this too?
var tMin = /*#=*/Numerical.CURVETIME_EPSILON,
tMax = 1 - tMin,
PI = Math.PI,
PI_2 = PI * 2,
// TODO: Make getCurve() sync work in boolean ops after splitting!!!
c2 = this._curve,
c1 = c2.getPrevious(),
c4 = intersection._curve,
c3 = c4.getPrevious();
if (!c1 || !c3)
return this._crossing = false;
if (report) {
new Path.Circle({
center: this.getPoint(),
radius: 10,
strokeColor: 'red'
});
new Path({
segments: [c1.getSegment1(), c1.getSegment2(), c2.getSegment2()],
strokeColor: 'red'
});
new Path({
segments: [c3.getSegment1(), c3.getSegment2(), c4.getSegment2()],
strokeColor: 'orange'
});
console.log(c1.getValues(), c2.getValues(), c3.getValues(), c4.getValues());
}
function isInRange(angle, min, max) {
return min < max
? angle > min && angle < max
// The range wraps around 0:
: angle > min && angle <= PI || angle >= -PI && angle < max;
}
// Calculate angles for all four tangents at the intersection point
var a1 = c1.getTangentAt(tMax, true).negate().getAngleInRadians(),
a2 = c2.getTangentAt(tMin, true).getAngleInRadians(),
a3 = c3.getTangentAt(tMax, true).negate().getAngleInRadians(),
a4 = c4.getTangentAt(tMin, true).getAngleInRadians();
// Count how many times curve2 angles appear between the curve1 angles
// If each pair of angles split the other two, then the edges cross.
return (isInRange(a3, a1, a2) ^ isInRange(a4, a1, a2))
&& (isInRange(a3, a2, a1) ^ isInRange(a4, a2, a1));
},
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, _ignoreIntersection) {
2014-08-16 13:24:54 -04:00
return this === loc
|| loc instanceof CurveLocation
// Call getCurve() and getParameter() to keep in sync
&& this.getCurve() === loc.getCurve()
&& this.getPoint().isClose(loc.getPoint(),
/*#=*/Numerical.GEOMETRIC_EPSILON)
&& (_ignoreIntersection
|| (!this._intersection && !loc._intersection
|| this._intersection && this._intersection.equals(
loc._intersection, true)))
|| false;
2014-08-16 13:24:54 -04:00
},
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(', ') + ' }';
},
statics: {
sort: function(locations) {
locations.sort(function compare(l1, l2) {
var curve1 = l1._curve,
curve2 = l2._curve,
path1 = curve1._path,
path2 = curve2._path,
res;
// Sort by path-id, curve, parameter, curve2, parameter2 so we
// can easily remove duplicates with calls to equals() after.
// NOTE: We don't call getCurve() / getParameter() here, since
// this code is used internally in boolean operations where all
// this information remains valid during processing.
if (path1 === path2) {
if (curve1 === curve2) {
var diff = l1._parameter - l2._parameter;
if (Math.abs(diff) < /*#=*/Numerical.CURVETIME_EPSILON){
var i1 = l1._intersection,
i2 = l2._intersection,
curve21 = i1 && i1._curve,
curve22 = i2 && l2._curve;
res = curve21 === curve22 // equal or both null
? i1 && i2 ? i1._parameter - i2._parameter : 0
: curve21 && curve22
? curve21.getIndex() - curve22.getIndex()
: curve21 ? 1 : -1;
} else {
res = diff;
}
} else {
res = curve1.getIndex() - curve2.getIndex();
}
} else {
// Sort by path id to group all locs on the same path.
res = path1._id - path2._id;
}
return res;
});
}
2014-08-16 13:24:54 -04:00
}
}, Base.each(Curve.evaluateMethods, function(name) {
2014-08-16 13:24:54 -04:00
// Produce getters for #getTangent() / #getNormal() / #getCurvature()
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);
};
}
}, {}));