From 40f3fc5feae866c99af818a886e8bf9d8cf2b8dd Mon Sep 17 00:00:00 2001 From: Pushkar Joshi Date: Tue, 6 Mar 2012 17:23:58 -0800 Subject: merge Valerio's architecture changes into pen tool --- js/lib/geom/sub-path.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'js/lib/geom') diff --git a/js/lib/geom/sub-path.js b/js/lib/geom/sub-path.js index ab54d1e9..0a65511b 100755 --- a/js/lib/geom/sub-path.js +++ b/js/lib/geom/sub-path.js @@ -584,6 +584,90 @@ GLSubpath.prototype.pickAnchor = function (pickX, pickY, pickZ, radius) { return selAnchorIndex; }; +GLSubpath.prototype.isWithinBBox =function(x,y,z){ + if (this._BBoxMin[0]>x || this._BBoxMin[1]>y || this._BBoxMin[2]>z){ + return false; + } + if (this._BBoxMax[0]=0 && this._selectedAnchorIndex=0 && selAnchorIndex === -1) { + var distSq = this._Anchors[this._selectedAnchorIndex].getPrevDistanceSq(pickX, pickY, pickZ); + if (distSq < minDistance && distSq < radSq){ + selAnchorIndex = this._selectedAnchorIndex; + minDistance = distSq; + } else { + //check the next for this anchor point + distSq = this._Anchors[this._selectedAnchorIndex].getNextDistanceSq(pickX, pickY, pickZ); + if (distSq0) { - var threshold = this._WETNESS_FACTOR*this._strokeWidth; + var threshold = 1;//this._WETNESS_FACTOR*this._strokeWidth; var prevPt = this._Points[numPoints-1]; var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]]; var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); @@ -173,6 +175,14 @@ var BrushStroke = function GLBrushStroke() { this._strokeColor = c; }; + this.setSecondStrokeColor = function(c){ + this._secondStrokeColor=c; + } + + this.setStrokeHardness = function(h){ + this._strokeHardness=h; + } + this.getStrokeStyle = function () { return this._strokeStyle; }; @@ -219,7 +229,8 @@ var BrushStroke = function GLBrushStroke() { var numPoints = this._Points.length; //**** add samples to the path if needed...linear interpolation for now - if (numPoints>1) { + //if (numPoints>1) { + if (0){ var threshold = this._WETNESS_FACTOR*this._strokeWidth; var prevPt = this._Points[0]; var prevIndex = 0; @@ -250,6 +261,44 @@ var BrushStroke = function GLBrushStroke() { } } } + //**** add samples to the long sections of the path --- Catmull-Rom spline interpolation + if (numPoints>1) { + var numInsertedPoints = 0; + var threshold = 5;//0.25*this._strokeWidth; //this determines whether a segment between two sample is too long + var prevPt = this._Points[0]; + for (var i=1;ithreshold){ + //build the control polygon for the Catmull-Rom spline (prev. 2 points and next 2 points) + var prev = (i===1) ? i-1 : i-2; + var next = (i===numPoints-1) ? i : i+1; + var ctrlPts = [this._Points[prev], this._Points[i-1], this._Points[i], this._Points[next]]; + //insert points along the prev. to current point + var numNewPoints = Math.floor(distance/threshold); + for (var j=0;j this._MAX_ALLOWED_SAMPLES){ + console.log("leaving the resampling because numPoints is greater than limit:"+this._MAX_ALLOWED_SAMPLES); + break; + } + } + console.log("Inserted "+numInsertedPoints+" additional CatmullRom points"); + } // *** compute the bounding box ********* this._BBoxMin = [Infinity, Infinity, Infinity]; @@ -412,7 +461,7 @@ var BrushStroke = function GLBrushStroke() { } */ - + /* var R2 = this._strokeWidth; var R = R2*0.5; var hardness = 0; //for a pencil, this is always 1 //TODO get hardness parameter from user interface @@ -440,6 +489,109 @@ var BrushStroke = function GLBrushStroke() { //ctx.globalCompositeOperation = 'source-in'; //ctx.rect(x-R, y-R, R2, R2); } + */ + + /* + //build the stamp for the brush stroke + //todo get this directly from the UI + var t=0; + var numTraces = this._strokeWidth; + var halfNumTraces = numTraces/2; + var startPos = [-this._strokeWidth/2,0]; + var brushStamp = []; + + //build an angled (calligraphic) brush stamp + var deltaDisplacement = [1,1];//[this._strokeWidth/numTraces, 0]; //a horizontal line brush + for (t=0;t1) { + if (this._strokeDoSmoothing && numPoints>1) { var numInsertedPoints = 0; var threshold = 5;//0.25*this._strokeWidth; //this determines whether a segment between two sample is too long var prevPt = this._Points[0]; @@ -507,35 +512,6 @@ var BrushStroke = function GLBrushStroke() { brushStamp.push(brushPt); } - - //make a circular brush stamp - brushStamp=[]; - numTraces = this._strokeWidth*Math.PI; //figure out how to - var radius = this._strokeWidth/2; - for (t=0;t0) { - var threshold = 1;//this._WETNESS_FACTOR*this._strokeWidth; + var threshold = 2;//this._WETNESS_FACTOR*this._strokeWidth; var prevPt = this._Points[numPoints-1]; var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]]; var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); @@ -188,6 +190,14 @@ var BrushStroke = function GLBrushStroke() { this._strokeDoSmoothing = s; } + this.setStrokeUseCalligraphic = function(c){ + this._strokeUseCalligraphic = c; + } + + this.setStrokeAngle = function(a){ + this._strokeAngle = a; + } + this.getStrokeStyle = function () { return this._strokeStyle; }; @@ -266,6 +276,8 @@ var BrushStroke = function GLBrushStroke() { } } } + //todo 4-point subdivision iterations over continuous regions of 'long' segments + // look at http://www.gvu.gatech.edu/~jarek/Split&Tweak/ for formula //**** add samples to the long sections of the path --- Catmull-Rom spline interpolation if (this._strokeDoSmoothing && numPoints>1) { var numInsertedPoints = 0; @@ -496,75 +508,84 @@ var BrushStroke = function GLBrushStroke() { } */ - /* - //build the stamp for the brush stroke - //todo get this directly from the UI - var t=0; - var numTraces = this._strokeWidth; - var halfNumTraces = numTraces/2; - var startPos = [-this._strokeWidth/2,0]; - var brushStamp = []; - - //build an angled (calligraphic) brush stamp - var deltaDisplacement = [1,1];//[this._strokeWidth/numTraces, 0]; //a horizontal line brush - for (t=0;t0) { + alphaVal = 1.0 - distFromOpaqueRegion/maxTransparentRegionHalfWidth; + } + + ctx.save(); + ctx.lineWidth=this._strokeWidth/10;//todo figure out the correct formula for the line width + if (ctx.lineWidth<2) + ctx.lineWidth=2; + if (t===numTraces-1){ + ctx.lineWidth = 1; + } + ctx.lineJoin="bevel"; + ctx.lineCap="butt"; + ctx.globalAlpha = this._strokeColor[3]; + + //if (t0) { - var threshold = 2;//this._WETNESS_FACTOR*this._strokeWidth; + var threshold = this._MIN_SAMPLE_DISTANCE_THRESHOLD;//this._WETNESS_FACTOR*this._strokeWidth; var prevPt = this._Points[numPoints-1]; var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]]; var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); @@ -276,13 +282,16 @@ var BrushStroke = function GLBrushStroke() { } } } + //todo 4-point subdivision iterations over continuous regions of 'long' segments // look at http://www.gvu.gatech.edu/~jarek/Split&Tweak/ for formula //**** add samples to the long sections of the path --- Catmull-Rom spline interpolation if (this._strokeDoSmoothing && numPoints>1) { var numInsertedPoints = 0; - var threshold = 5;//0.25*this._strokeWidth; //this determines whether a segment between two sample is too long + var newPoints = []; + var threshold = this._MAX_SAMPLE_DISTANCE_THRESHOLD;//this determines whether a segment between two sample is long enough to warrant checking for angle var prevPt = this._Points[0]; + newPoints.push(this._Points[0]); for (var i=1;i0) { alphaVal = 1.0 - distFromOpaqueRegion/maxTransparentRegionHalfWidth; + alphaVal *= 1.0/ctx.lineWidth; //factor that accounts for lineWidth !== 1 } ctx.save(); - ctx.lineWidth=this._strokeWidth/10;//todo figure out the correct formula for the line width - if (ctx.lineWidth<2) - ctx.lineWidth=2; - if (t===numTraces-1){ - ctx.lineWidth = 1; - } - ctx.lineJoin="bevel"; - ctx.lineCap="butt"; - ctx.globalAlpha = this._strokeColor[3]; - - //if (t