aboutsummaryrefslogtreecommitdiff
path: root/js/lib/geom/brush-stroke.js
diff options
context:
space:
mode:
authorPushkar Joshi2012-03-23 14:32:46 -0700
committerPushkar Joshi2012-03-23 14:32:46 -0700
commit9b7dac9215fbd7c0fe7a80d3e8f1ff378332fec3 (patch)
treecf0cad815fe98ee8d493cbf42c4d8e13a3e0aaac /js/lib/geom/brush-stroke.js
parent92bc54df4acfec849568ab619150a5da49c087fa (diff)
downloadninja-9b7dac9215fbd7c0fe7a80d3e8f1ff378332fec3.tar.gz
Almost working version of brush tool that uses only local coordinates to store the brush stroke points. Current version does not yet update the width and height of the brush stroke canvas upon changing the brush stroke through the PI. Also, current version does not obtain 3D position of points properly from the drawing tool base (see BrushTool _getUnsnappedPosition)
Diffstat (limited to 'js/lib/geom/brush-stroke.js')
-rwxr-xr-xjs/lib/geom/brush-stroke.js460
1 files changed, 314 insertions, 146 deletions
diff --git a/js/lib/geom/brush-stroke.js b/js/lib/geom/brush-stroke.js
index afeaf6e6..a3a5ed9a 100755
--- a/js/lib/geom/brush-stroke.js
+++ b/js/lib/geom/brush-stroke.js
@@ -7,6 +7,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
7var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; 7var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
8var GeomObj = require("js/lib/geom/geom-obj").GeomObj; 8var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
9var CanvasController = require("js/controllers/elements/canvas-controller").CanvasController; 9var CanvasController = require("js/controllers/elements/canvas-controller").CanvasController;
10var ViewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
10 11
11// Todo: This entire class should be converted to a module 12// Todo: This entire class should be converted to a module
12 13
@@ -19,14 +20,15 @@ var BrushStroke = function GLBrushStroke() {
19 /////////////////////////////////////////////////// 20 ///////////////////////////////////////////////////
20 // Instance variables 21 // Instance variables
21 /////////////////////////////////////////////////// 22 ///////////////////////////////////////////////////
22 this._Points = []; 23 this._Points = []; //current state of points in stage-world space (may be different from input)
23 this._OrigPoints = []; 24 this._OrigPoints = []; //copy of input points without any smoothing
25 this._LocalPoints = []; //_Points in local coordinates...do this before rendering the points in the canvas
26 this._stageWorldCenter = [0,0,0]; //coordinate for the canvas midPoint: a 3D vector in stage world space
24 this._BBoxMin = [0, 0, 0]; 27 this._BBoxMin = [0, 0, 0];
25 this._BBoxMax = [0, 0, 0]; 28 this._BBoxMax = [0, 0, 0];
26 this._dirty = true; 29 this._isDirty = true;
27 this._isInit = false; 30 this._isInit = false;
28 this._addedSamples = false; 31
29 this._storedOrigPoints = false;
30 32
31 //whether or not to use the canvas drawing to stroke/fill 33 //whether or not to use the canvas drawing to stroke/fill
32 this._useCanvasDrawing = true; 34 this._useCanvasDrawing = true;
@@ -57,7 +59,7 @@ var BrushStroke = function GLBrushStroke() {
57 this._MIN_SAMPLE_DISTANCE_THRESHOLD = 2; 59 this._MIN_SAMPLE_DISTANCE_THRESHOLD = 2;
58 60
59 //prevent extremely long paths that can take a long time to render 61 //prevent extremely long paths that can take a long time to render
60 this._MAX_ALLOWED_SAMPLES = 500; 62 this._MAX_ALLOWED_SAMPLES = 5000;
61 63
62 //drawing context 64 //drawing context
63 this._world = null; 65 this._world = null;
@@ -67,6 +69,7 @@ var BrushStroke = function GLBrushStroke() {
67 this._planeMat = null; 69 this._planeMat = null;
68 this._planeMatInv = null; 70 this._planeMatInv = null;
69 this._planeCenter = null; 71 this._planeCenter = null;
72 this._dragPlane = null;
70 73
71 ///////////////////////////////////////////////////////// 74 /////////////////////////////////////////////////////////
72 // Property Accessors/Setters 75 // Property Accessors/Setters
@@ -107,6 +110,10 @@ var BrushStroke = function GLBrushStroke() {
107 this._planeCenter = pc; 110 this._planeCenter = pc;
108 }; 111 };
109 112
113 this.setDragPlane = function(p){
114 this._dragPlane = p;
115 };
116
110 this.getCanvasX = function(){ 117 this.getCanvasX = function(){
111 return this._canvasX; 118 return this._canvasX;
112 }; 119 };
@@ -141,25 +148,32 @@ var BrushStroke = function GLBrushStroke() {
141 var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); 148 var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]);
142 if (diffPtMag>threshold){ 149 if (diffPtMag>threshold){
143 this._Points.push(pt); 150 this._Points.push(pt);
144 this._dirty=true; 151 this._isDirty=true;
152 this._isInit = false;
145 } 153 }
146 } else { 154 } else {
147 this._Points.push(pt); 155 this._Points.push(pt);
148 this._dirty=true; 156 this._isDirty=true;
157 this._isInit = false;
149 } 158 }
150 }; 159 };
151 160
152 this.insertPoint = function(pt, index){ 161 this.insertPoint = function(pt, index){
153 this._Points.splice(index, 0, pt); 162 this._Points.splice(index, 0, pt);
154 this._dirty=true; 163 this._isDirty=true;
164 this._isInit = false;
155 }; 165 };
156 166
157 this.isDirty = function(){ 167 this.isDirty = function(){
158 return this._dirty; 168 return this._isDirty;
159 }; 169 };
160 170
161 this.makeDirty = function(){ 171 this.makeDirty = function(){
162 this._dirty=true; 172 this._isDirty=true;
173 };
174
175 this.getStageWorldCenter = function() {
176 return this._stageWorldCenter;
163 }; 177 };
164 178
165 this.getBBoxMin = function () { 179 this.getBBoxMin = function () {
@@ -176,7 +190,7 @@ var BrushStroke = function GLBrushStroke() {
176 190
177 this.setStrokeWidth = function (w) { 191 this.setStrokeWidth = function (w) {
178 this._strokeWidth = w; 192 this._strokeWidth = w;
179 this._dirty=true; 193 this._isDirty=true;
180 }; 194 };
181 195
182 this.getStrokeMaterial = function () { 196 this.getStrokeMaterial = function () {
@@ -184,7 +198,7 @@ var BrushStroke = function GLBrushStroke() {
184 }; 198 };
185 199
186 this.setStrokeMaterial = function (m) { 200 this.setStrokeMaterial = function (m) {
187 this._strokeMaterial = m; this._dirty = true; 201 this._strokeMaterial = m; this._isDirty = true;
188 }; 202 };
189 203
190 this.getStrokeColor = function () { 204 this.getStrokeColor = function () {
@@ -192,17 +206,17 @@ var BrushStroke = function GLBrushStroke() {
192 }; 206 };
193 207
194 this.setStrokeColor = function (c) { 208 this.setStrokeColor = function (c) {
195 this._strokeColor = c; this._dirty = true; 209 this._strokeColor = c; this._isDirty = true;
196 }; 210 };
197 211
198 this.setSecondStrokeColor = function(c){ 212 this.setSecondStrokeColor = function(c){
199 this._secondStrokeColor=c; this._dirty = true; 213 this._secondStrokeColor=c; this._isDirty = true;
200 } 214 }
201 215
202 this.setStrokeHardness = function(h){ 216 this.setStrokeHardness = function(h){
203 if (this._strokeHardness!==h){ 217 if (this._strokeHardness!==h){
204 this._strokeHardness=h; 218 this._strokeHardness=h;
205 this._dirty = true; 219 this._isDirty = true;
206 } 220 }
207 } 221 }
208 this.getStrokeHardness = function(){ 222 this.getStrokeHardness = function(){
@@ -212,7 +226,7 @@ var BrushStroke = function GLBrushStroke() {
212 this.setDoSmoothing = function(s){ 226 this.setDoSmoothing = function(s){
213 if (this._strokeDoSmoothing!==s) { 227 if (this._strokeDoSmoothing!==s) {
214 this._strokeDoSmoothing = s; 228 this._strokeDoSmoothing = s;
215 this._dirty = true; 229 this._isDirty = true;
216 } 230 }
217 } 231 }
218 232
@@ -223,7 +237,7 @@ var BrushStroke = function GLBrushStroke() {
223 this.setSmoothingAmount = function(a){ 237 this.setSmoothingAmount = function(a){
224 if (this._strokeAmountSmoothing!==a) { 238 if (this._strokeAmountSmoothing!==a) {
225 this._strokeAmountSmoothing = a; 239 this._strokeAmountSmoothing = a;
226 this._dirty = true; 240 this._isDirty = true;
227 } 241 }
228 } 242 }
229 243
@@ -234,14 +248,14 @@ var BrushStroke = function GLBrushStroke() {
234 this.setStrokeUseCalligraphic = function(c){ 248 this.setStrokeUseCalligraphic = function(c){
235 if (this._strokeUseCalligraphic!==c){ 249 if (this._strokeUseCalligraphic!==c){
236 this._strokeUseCalligraphic = c; 250 this._strokeUseCalligraphic = c;
237 this._dirty = true; 251 this._isDirty = true;
238 } 252 }
239 } 253 }
240 254
241 this.setStrokeAngle = function(a){ 255 this.setStrokeAngle = function(a){
242 if (this._strokeAngle!==a){ 256 if (this._strokeAngle!==a){
243 this._strokeAngle = a; 257 this._strokeAngle = a;
244 this._dirty = true; 258 this._isDirty = true;
245 }; 259 };
246 } 260 }
247 261
@@ -269,127 +283,276 @@ var BrushStroke = function GLBrushStroke() {
269 283
270 };//NO-OP for now 284 };//NO-OP for now
271 285
286 this.getWidth = function() {
287 if (this._isDirty){
288 this.update();
289 }
290 return this._BBoxMax[0]-this._BBoxMin[0];
291 };
292
293 this.getHeight = function() {
294 if (this._isDirty){
295 this.update();
296 }
297 return this._BBoxMax[1]-this._BBoxMin[1];
298 };
299
272 //remove all the points 300 //remove all the points
273 this.clear = function () { 301 this.clear = function () {
274 this._Points = []; 302 this._Points = [];
275 this._OrigPoints = []; 303 this._OrigPoints = [];
276 this._dirty=true; 304 this._isDirty=true;
277 } 305 this._isInit = false;
306 };
307
308 this._addSamples = function() {
309 //**** add samples to the long sections of the path --- Catmull-Rom spline interpolation ***