Introduce private faster Matrix#_transform* methods that lack arguments checks, and use them internally.

This commit is contained in:
Jürg Lehni 2011-05-06 13:28:35 +01:00
parent aed1bb0878
commit c36db3a55c
7 changed files with 40 additions and 32 deletions

View file

@ -292,26 +292,34 @@ var Matrix = this.Matrix = Base.extend({
* @param {number} numPts The number of points to tranform.
*/
transform: function(/* point | */ src, srcOff, dst, dstOff, numPts) {
if (arguments.length == 5) {
var i = srcOff,
j = dstOff,
srcEnd = srcOff + 2 * numPts;
while (i < srcEnd) {
var x = src[i++];
var y = src[i++];
dst[j++] = x * this._m00 + y * this._m01 + this._m02;
dst[j++] = x * this._m10 + y * this._m11 + this._m12;
}
return dst;
} else if (arguments.length > 0) {
var point = Point.read(arguments),
x = point.x, y = point.y;
return Point.create(
x * this._m00 + y * this._m01 + this._m02,
x * this._m10 + y * this._m11 + this._m12
);
return arguments.length < 5
? this._transformPoint(Point.read(arguments))
: this._transformCoordinates(src, srcOff, dst, dstOff, numPts);
},
/**
* A faster version of transform that only takes one point and does not
* attempt to convert it.
*/
_transformPoint: function(point) {
var x = point.x,
y = point.y;
return Point.create(
x * this._m00 + y * this._m01 + this._m02,
x * this._m10 + y * this._m11 + this._m12
);
},
_transformCoordinates: function(src, srcOff, dst, dstOff, numPts) {
var i = srcOff, j = dstOff,
srcEnd = srcOff + 2 * numPts;
while (i < srcEnd) {
var x = src[i++];
var y = src[i++];
dst[j++] = x * this._m00 + y * this._m01 + this._m02;
dst[j++] = x * this._m10 + y * this._m11 + this._m12;
}
return null;
return dst;
},
transformBounds: function(bounds) {
@ -323,7 +331,7 @@ var Matrix = this.Matrix = Base.extend({
br = bounds.getBottomRight(),
bl = bounds.getBottomLeft(),
coords = [ tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y ];
this.transform(coords, 0, coords, 0, 4);
this._transformCoordinates(coords, 0, coords, 0, 4);
// Loop through all x and y coordinates and update min and max values.
// Start with the first coordinate pair for both (coords.slice(0, 2)).
var min = coords.slice(0, 2), max = min.slice(0);

View file

@ -112,7 +112,7 @@ var Point = this.Point = Base.extend({
},
transform: function(matrix) {
return matrix.transform(this);
return matrix._transformPoint(this);
},
/**

View file

@ -78,11 +78,11 @@ var DocumentView = this.DocumentView = Base.extend({
// TODO: getMousePoint
// TODO: artworkToView(rect)
artworkToView: function(point) {
return this.matrix.transform(point);
return this.matrix._transformPoint(point);
},
viewToArtwork: function(point) {
// TODO: cache the inverse matrix:
return this.matrix.createInverse().transform(point);
return this.matrix.createInverse()._transformPoint(point);
}
});

View file

@ -692,7 +692,7 @@ var Item = this.Item = Base.extend({
left, bottom
];
if (matrix)
matrix.transform(coords, 0, coords, 0, 4);
matrix._transformCoordinates(coords, 0, coords, 0, 4);
ctx.beginPath();
for (var i = 0; i < 8; i++)
ctx[i == 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);

View file

@ -185,10 +185,10 @@ var Curve = this.Curve = Base.extend({
transform: function(matrix) {
return new Curve(
matrix.transform(this._segment1._point),
matrix.transform(this._segment1._handleOut),
matrix.transform(this._segment2._handleIn),
matrix.transform(this._segment2._point));
matrix._transformPoint(this._segment1._point),
matrix._transformPoint(this._segment1._handleOut),
matrix._transformPoint(this._segment2._handleIn),
matrix._transformPoint(this._segment2._point));
},
reverse: function() {

View file

@ -124,7 +124,7 @@ var Path = this.Path = PathItem.extend({
if (!matrix.isIdentity()) {
var coords = new Array(6);
for (var i = 0, l = this._segments.length; i < l; i++) {
this._segments[i]._transformCoordinates(matrix, coords, true);
this._segments[i].__transformCoordinates(matrix, coords, true);
}
}
this._changed();
@ -894,11 +894,11 @@ var Path = this.Path = PathItem.extend({
// Make coordinates for first segment available in prevCoords.
if (matrix && matrix.isIdentity())
matrix = null;
first._transformCoordinates(matrix, prevCoords, false);
first.__transformCoordinates(matrix, prevCoords, false);
var min = prevCoords.slice(0, 2),
max = min.slice(0); // clone
function processSegment(segment) {
segment._transformCoordinates(matrix, coords, false);
segment.__transformCoordinates(matrix, coords, false);
for (var i = 0; i < 2; i++) {
var v0 = prevCoords[i], // prev.point

View file

@ -265,7 +265,7 @@ var Segment = this.Segment = Base.extend({
coords[i++] = handleOut._y + y;
}
if (matrix) {
matrix.transform(coords, 0, coords, 0, i / 2);
matrix._transformCoordinates(coords, 0, coords, 0, i / 2);
x = coords[0];
y = coords[1];
if (change) {