From 0e87c02e74e08c7bf156373b0d2459563e17ecd6 Mon Sep 17 00:00:00 2001 From: Pushkar Joshi Date: Wed, 1 Feb 2012 14:38:15 -0800 Subject: make GLAnchorPoint functions as part of its prototype --- js/helper-classes/RDGE/GLAnchorPoint.js | 230 ++++++++++++++++---------------- js/helper-classes/RDGE/GLSubpath.js | 10 +- 2 files changed, 124 insertions(+), 116 deletions(-) (limited to 'js/helper-classes/RDGE') diff --git a/js/helper-classes/RDGE/GLAnchorPoint.js b/js/helper-classes/RDGE/GLAnchorPoint.js index 496b6f60..6b4af072 100644 --- a/js/helper-classes/RDGE/GLAnchorPoint.js +++ b/js/helper-classes/RDGE/GLAnchorPoint.js @@ -26,125 +26,125 @@ function GLAnchorPoint() { this._nextX = 0.0; this._nextY = 0.0; this._nextZ = 0.0; - +} // *********** setters ************ - this.setPos = function (x, y, z) { this._x = x; this._y = y; this._z = z; } - this.setPrevPos = function (x, y, z) { this._prevX = x; this._prevY = y; this._prevZ = z; } - this.setNextPos = function (x, y, z) { this._nextX = x; this._nextY = y; this._nextZ = z; } - - this.setPrevFromNext = function () { - //set the previous control point by reflecting the next control point - var dispX = this._nextX - this._x; - var dispY = this._nextY - this._y; - var dispZ = this._nextZ - this._z; - - this._prevX = this._x - dispX; - this._prevY = this._y - dispY; - this._prevZ = this._z - dispZ; - } - this.setNextFromPrev = function () { - //set the previous control point by reflecting the next control point - var dispX = this._prevX - this._x; - var dispY = this._prevY - this._y; - var dispZ = this._prevZ - this._z; - - this._nextX = this._x - dispX; - this._nextY = this._y - dispY; - this._nextZ = this._z - dispZ; - } +GLAnchorPoint.prototype.setPos = function (x, y, z) { this._x = x; this._y = y; this._z = z; } +GLAnchorPoint.prototype.setPrevPos = function (x, y, z) { this._prevX = x; this._prevY = y; this._prevZ = z; } +GLAnchorPoint.prototype.setNextPos = function (x, y, z) { this._nextX = x; this._nextY = y; this._nextZ = z; } - //translate the next point from the translation that was applied to the prev. point - this.translateNextFromPrev = function (tx, ty, tz) { - // *** compute the rotation of the prev vector *** - var oldP = Vector.create([this._prevX + tx - this._x, this._prevY + ty - this._y, this._prevZ + tz - this._z]); - var newP = Vector.create([this._prevX - this._x, this._prevY - this._y, this._prevZ - this._z]); - //compute angle between the two vectors - var axis = Vector.create([0, 0, 0]); - var angle = MathUtils.getAxisAngleBetween3DVectors(oldP, newP, axis); - if (angle === 0) - return; - - // *** compute the vector from anchor to next - var oldN = Vector.create([this._nextX - this._x, this._nextY - this._y, this._nextZ - this._z]); - var rotMat = Matrix.Rotation(-angle, axis); - var newN = MathUtils.transformVector(oldN, rotMat); - - //TEMP for some situations the axis angle computation returns NaNs - if (isNaN(newN[0]) || isNaN(newN[1]) || isNaN(newN[2])) { - return; - } - //end TEMP - this._nextX = this._x + newN[0]; - this._nextY = this._y + newN[1]; - this._nextZ = this._z + newN[2]; - } - //translate the next point from the translation that was applied to the prev. point - this.translatePrevFromNext = function (tx, ty, tz) { - // *** compute the rotation of the next vector *** - var oldN = Vector.create([this._nextX + tx - this._x, this._nextY + ty - this._y, this._nextZ + tz - this._z]); - var newN = Vector.create([this._nextX - this._x, this._nextY - this._y, this._nextZ - this._z]); - //compute angle between the two vectors - var axis = Vector.create([0, 0, 0]); - var angle = MathUtils.getAxisAngleBetween3DVectors(oldN, newN, axis); - if (angle === 0) - return; - - // *** compute the vector from anchor to prev - var oldP = Vector.create([this._prevX - this._x, this._prevY - this._y, this._prevZ - this._z]); - var rotMat = Matrix.Rotation(-angle, axis); - var newP = MathUtils.transformVector(oldP, rotMat); - - //TEMP for some situations the axis angle computation returns NaNs - if (isNaN(newP[0]) || isNaN(newP[1]) || isNaN(newP[2])) { - return; - } - //end TEMP - this._prevX = this._x + newP[0]; - this._prevY = this._y + newP[1]; - this._prevZ = this._z + newP[2]; - } +GLAnchorPoint.prototype.setPrevFromNext = function () { + //set the previous control point by reflecting the next control point + var dispX = this._nextX - this._x; + var dispY = this._nextY - this._y; + var dispZ = this._nextZ - this._z; + this._prevX = this._x - dispX; + this._prevY = this._y - dispY; + this._prevZ = this._z - dispZ; +} +GLAnchorPoint.prototype.setNextFromPrev = function () { + //set the previous control point by reflecting the next control point + var dispX = this._prevX - this._x; + var dispY = this._prevY - this._y; + var dispZ = this._prevZ - this._z; - // ******* modifiers ******* - this.translatePrev = function (x, y, z) { - this._prevX += x; this._prevY += y; this._prevZ += z; - } - this.translateNext = function (x, y, z) { - this._nextX += x; this._nextY += y; this._nextZ += z; - } - this.translate = function (x, y, z) { - this._x += x; this._y += y; this._z += z; - } - this.translateAll = function (x, y, z) { - this.translate(x, y, z); - this.translatePrev(x, y, z); - this.translateNext(x, y, z); - } - - - // ********* getters ********** - this.getPosX = function () { return this._x; } - this.getPosY = function () { return this._y; } - this.getPosZ = function () { return this._z; } - this.getPrevX = function () { return this._prevX; } - this.getPrevY = function () { return this._prevY; } - this.getPrevZ = function () { return this._prevZ; } - this.getNextX = function () { return this._nextX; } - this.getNextY = function () { return this._nextY; } - this.getNextZ = function () { return this._nextZ; } - this.getPos = function() { return Vector.create([this._x, this._y, this._z]);} - this.getPrev = function() { return Vector.create([this._prevX, this._prevY, this._prevZ]);} - this.getNext = function() { return Vector.create([this._nextX, this._nextY, this._nextZ]);} - //return the square of distance from passed in point to the anchor position - this.getDistanceSq = function (x, y, z) { - return (this._x - x) * (this._x - x) + (this._y - y) * (this._y - y) + (this._z - z) * (this._z - z); - } - //return sq. of distance to prev. - this.getPrevDistanceSq = function (x, y, z) { - return (this._prevX - x) * (this._prevX - x) + (this._prevY - y) * (this._prevY - y) + (this._prevZ - z) * (this._prevZ - z); + this._nextX = this._x - dispX; + this._nextY = this._y - dispY; + this._nextZ = this._z - dispZ; +} + +//translate the next point from the translation that was applied to the prev. point +GLAnchorPoint.prototype.translateNextFromPrev = function (tx, ty, tz) { + // *** compute the rotation of the prev vector *** + var oldP = Vector.create([this._prevX + tx - this._x, this._prevY + ty - this._y, this._prevZ + tz - this._z]); + var newP = Vector.create([this._prevX - this._x, this._prevY - this._y, this._prevZ - this._z]); + //compute angle between the two vectors + var axis = Vector.create([0, 0, 0]); + var angle = MathUtils.getAxisAngleBetween3DVectors(oldP, newP, axis); + if (angle === 0) + return; + + // *** compute the vector from anchor to next + var oldN = Vector.create([this._nextX - this._x, this._nextY - this._y, this._nextZ - this._z]); + var rotMat = Matrix.Rotation(-angle, axis); + var newN = MathUtils.transformVector(oldN, rotMat); + + //TEMP for some situations the axis angle computation returns NaNs + if (isNaN(newN[0]) || isNaN(newN[1]) || isNaN(newN[2])) { + return; } - //return sq. of distance to next - this.getNextDistanceSq = function (x, y, z) { - return (this._nextX - x) * (this._nextX - x) + (this._nextY - y) * (this._nextY - y) + (this._nextZ - z) * (this._nextZ - z); + //end TEMP + this._nextX = this._x + newN[0]; + this._nextY = this._y + newN[1]; + this._nextZ = this._z + newN[2]; +} +//translate the next point from the translation that was applied to the prev. point +GLAnchorPoint.prototype.translatePrevFromNext = function (tx, ty, tz) { + // *** compute the rotation of the next vector *** + var oldN = Vector.create([this._nextX + tx - this._x, this._nextY + ty - this._y, this._nextZ + tz - this._z]); + var newN = Vector.create([this._nextX - this._x, this._nextY - this._y, this._nextZ - this._z]); + //compute angle between the two vectors + var axis = Vector.create([0, 0, 0]); + var angle = MathUtils.getAxisAngleBetween3DVectors(oldN, newN, axis); + if (angle === 0) + return; + + // *** compute the vector from anchor to prev + var oldP = Vector.create([this._prevX - this._x, this._prevY - this._y, this._prevZ - this._z]); + var rotMat = Matrix.Rotation(-angle, axis); + var newP = MathUtils.transformVector(oldP, rotMat); + + //TEMP for some situations the axis angle computation returns NaNs + if (isNaN(newP[0]) || isNaN(newP[1]) || isNaN(newP[2])) { + return; } + //end TEMP + this._prevX = this._x + newP[0]; + this._prevY = this._y + newP[1]; + this._prevZ = this._z + newP[2]; +} + + +// ******* modifiers ******* +GLAnchorPoint.prototype.translatePrev = function (x, y, z) { + this._prevX += x; this._prevY += y; this._prevZ += z; +} +GLAnchorPoint.prototype.translateNext = function (x, y, z) { + this._nextX += x; this._nextY += y; this._nextZ += z; +} +GLAnchorPoint.prototype.translate = function (x, y, z) { + this._x += x; this._y += y; this._z += z; } +GLAnchorPoint.prototype.translateAll = function (x, y, z) { + this.translate(x, y, z); + this.translatePrev(x, y, z); + this.translateNext(x, y, z); +} + + +// ********* getters ********** +GLAnchorPoint.prototype.getPosX = function () { return this._x; } +GLAnchorPoint.prototype.getPosY = function () { return this._y; } +GLAnchorPoint.prototype.getPosZ = function () { return this._z; } +GLAnchorPoint.prototype.getPrevX = function () { return this._prevX; } +GLAnchorPoint.prototype.getPrevY = function () { return this._prevY; } +GLAnchorPoint.prototype.getPrevZ = function () { return this._prevZ; } +GLAnchorPoint.prototype.getNextX = function () { return this._nextX; } +GLAnchorPoint.prototype.getNextY = function () { return this._nextY; } +GLAnchorPoint.prototype.getNextZ = function () { return this._nextZ; } +GLAnchorPoint.prototype.getPos = function() { return Vector.create([this._x, this._y, this._z]);} +GLAnchorPoint.prototype.getPrev = function() { return Vector.create([this._prevX, this._prevY, this._prevZ]);} +GLAnchorPoint.prototype.getNext = function() { return Vector.create([this._nextX, this._nextY, this._nextZ]);} +//return the square of distance from passed in point to the anchor position +GLAnchorPoint.prototype.getDistanceSq = function (x, y, z) { + return (this._x - x) * (this._x - x) + (this._y - y) * (this._y - y) + (this._z - z) * (this._z - z); +} +//return sq. of distance to prev. +GLAnchorPoint.prototype.getPrevDistanceSq = function (x, y, z) { + return (this._prevX - x) * (this._prevX - x) + (this._prevY - y) * (this._prevY - y) + (this._prevZ - z) * (this._prevZ - z); +} +//return sq. of distance to next +GLAnchorPoint.prototype.getNextDistanceSq = function (x, y, z) { + return (this._nextX - x) * (this._nextX - x) + (this._nextY - y) * (this._nextY - y) + (this._nextZ - z) * (this._nextZ - z); +} + diff --git a/js/helper-classes/RDGE/GLSubpath.js b/js/helper-classes/RDGE/GLSubpath.js index 52699f85..fd2f3560 100644 --- a/js/helper-classes/RDGE/GLSubpath.js +++ b/js/helper-classes/RDGE/GLSubpath.js @@ -141,6 +141,7 @@ function GLSubpath() { var lineCap = ['butt','round','square']; ctx.lineCap = lineCap[1]; ctx.beginPath(); + var prevAnchor = this.getAnchor(0); ctx.moveTo(prevAnchor.getPosX()-bboxMin[0],prevAnchor.getPosY()-bboxMin[1]); for (var i = 1; i < numAnchors; i++) { @@ -156,8 +157,15 @@ function GLSubpath() { if (this._isClosed){ ctx.fill(); } - ctx.stroke(); + /* + var numPoints = this._samples.length/3; + ctx.moveTo(this._samples[0],this._samples[1]); + for (var i=0;i