Revert return strategy of boolean operators for simpler code, simplify operator logic and inline functions.

This commit is contained in:
Jürg Lehni 2013-05-03 16:52:37 -07:00
parent ee8a79449b
commit c11967c81c

View file

@ -33,51 +33,49 @@
PathItem.inject({ PathItem.inject({
/** // A boolean operator is a binary operator function of the form
* A boolean operator is a binary operator function of the form // function(isPath1, isInPath1, isInPath2)
* f(isPath1:boolean, isInsidePath1:Boolean, isInsidePath2:Boolean) :Boolean //
* // Operators return true if a curve in the operands is to be removed,
* Boolean operator determines whether a curve segment in the operands is part // and they aare called for each curve segment in the graph after all the
* of the boolean result, and will be called for each curve segment in the graph after // intersections between the operands are calculated and curves in the
* all the intersections between the operands are calculated and curves in the operands // operands were split at intersections.
* are split at intersections. //
* // These functions should have a name ("union", "subtraction" etc. below),
* These functions should have a name ("union", "subtraction" etc. below), if we need to // if we need to do operator specific operations on paths inside the
* do operator specific operations on paths inside the computeBoolean function. // computeBoolean function.
* for example: if the name of the operator is "subtraction" then we need to reverse the second // For example: If the name of the operator is "subtraction" then we need to
* operand. Subtraction is neither associative nor commutative. // reverse the second operand. Subtraction is neither associative nor
* // commutative.
* The boolean operator should return a Boolean value indicating whether to keep the curve or not. //
* return true - keep the curve // The boolean operator return a Boolean value indicating whether to
* return false - discard the curve // keep the curve or not.
*/ // return true - discard the curve
// return false - keep the curve
unite: function(path, _cache) { unite: function(path, _cache) {
var unionOp = function union(isPath1, isInsidePath1, isInsidePath2) { return this._computeBoolean(this, path,
return (isInsidePath1 || isInsidePath2)? false : true; function union(isPath1, isInPath1, isInPath2) {
}; return isInPath1 || isInPath2;
return this._computeBoolean(this, path, unionOp, _cache); }, _cache);
}, },
intersect: function(path, _cache) { intersect: function(path, _cache) {
var intersectionOp = function intersection(isPath1, isInsidePath1, isInsidePath2) { return this._computeBoolean(this, path,
return (!isInsidePath1 && !isInsidePath2)? false : true; function intersection(isPath1, isInPath1, isInPath2) {
}; return !(isInPath1 || isInPath2);
return this._computeBoolean(this, path, intersectionOp, _cache); }, _cache);
}, },
subtract: function(path, _cache) { subtract: function(path, _cache) {
var subtractionOp = function subtraction(isPath1, isInsidePath1, isInsidePath2) { return this._computeBoolean(this, path,
return ((isPath1 && isInsidePath2) || (!isPath1 && !isInsidePath1))? false : true; function subtraction(isPath1, isInPath1, isInPath2) {
}; return isPath1 && isInPath2 || !isPath1 && !isInPath1;
return this._computeBoolean(this, path, subtractionOp, _cache); }, _cache);
}, },
/* // Compound boolean operators combine the basic boolean operations such as
* Compound boolean operators combine the basic boolean operations such as union, intersection, // union, intersection, subtract etc.
* subtract etc. // TODO: cache the split objects and find a way to properly clone them!
*
* TODO: cache the split objects and find a way to properly clone them!
*/
// a.k.a. eXclusiveOR // a.k.a. eXclusiveOR
exclude: function(path) { exclude: function(path) {
var res1 = this.subtract(path); var res1 = this.subtract(path);
@ -293,7 +291,7 @@ PathItem.inject({
insidePath2 = (thisWinding === path2Clockwise)? contains : insidePath2 = (thisWinding === path2Clockwise)? contains :
contains && !this._testOnCurve(_path2, midPoint); contains && !this._testOnCurve(_path2, midPoint);
} }
if (!operator(thisId === path1Id, insidePath1, insidePath2)) { if (operator(thisId === path1Id, insidePath1, insidePath2)) {
crv._INVALID = true; crv._INVALID = true;
// markPoint(midPoint, '+'); // markPoint(midPoint, '+');
} }