aboutsummaryrefslogtreecommitdiff
path: root/js/lib/geom
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/geom')
-rwxr-xr-xjs/lib/geom/brush-stroke.js245
-rwxr-xr-xjs/lib/geom/sub-path.js16
2 files changed, 147 insertions, 114 deletions
diff --git a/js/lib/geom/brush-stroke.js b/js/lib/geom/brush-stroke.js
index 4c42539a..02a39ccd 100755
--- a/js/lib/geom/brush-stroke.js
+++ b/js/lib/geom/brush-stroke.js
@@ -4,9 +4,11 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7// Todo: This entire class should be converted to a module
8var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; 7var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
9var 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;
10
11// Todo: This entire class should be converted to a module
10 12
11/////////////////////////////////////////////////////////////////////// 13///////////////////////////////////////////////////////////////////////
12// Class GLBrushStroke 14// Class GLBrushStroke
@@ -18,14 +20,20 @@ var BrushStroke = function GLBrushStroke() {
18 // Instance variables 20 // Instance variables
19 /////////////////////////////////////////////////// 21 ///////////////////////////////////////////////////
20 this._Points = []; 22 this._Points = [];
23 this._OrigPoints = [];
21 this._BBoxMin = [0, 0, 0]; 24 this._BBoxMin = [0, 0, 0];
22 this._BBoxMax = [0, 0, 0]; 25 this._BBoxMax = [0, 0, 0];
23 this._dirty = true; 26 this._dirty = true;
27 this._addedSamples = false;
28 this._storedOrigPoints = false;
24 29
25 //whether or not to use the canvas drawing to stroke/fill 30 //whether or not to use the canvas drawing to stroke/fill
26 this._useCanvasDrawing = true; 31 this._useCanvasDrawing = true;
27 32
28 //the X and Y location of this subpath's canvas in stage world space of Ninja 33 //the HTML5 canvas that holds this brush stroke
34 this._canvas = null;
35
36 //the X and Y location of this brush stroke canvas in stage world space of Ninja
29 this._canvasX = 0; 37 this._canvasX = 0;
30 this._canvasY = 0; 38 this._canvasY = 0;
31 39
@@ -39,10 +47,7 @@ var BrushStroke = function GLBrushStroke() {
39 this._strokeDoSmoothing = false; 47 this._strokeDoSmoothing = false;
40 this._strokeUseCalligraphic = false; 48 this._strokeUseCalligraphic = false;
41 this._strokeAngle = 0; 49 this._strokeAngle = 0;
42 50 this._strokeAmountSmoothing = 0;
43 //the wetness of the brush (currently this is multiplied to the square of the stroke width, but todo should be changed to not depend on stroke width entirely
44 //smaller value means more samples for the path
45 this._WETNESS_FACTOR = 0.25;
46 51
47 //threshold that tells us whether two samples are too far apart 52 //threshold that tells us whether two samples are too far apart
48 this._MAX_SAMPLE_DISTANCE_THRESHOLD = 5; 53 this._MAX_SAMPLE_DISTANCE_THRESHOLD = 5;
@@ -65,6 +70,10 @@ var BrushStroke = function GLBrushStroke() {
65 ///////////////////////////////////////////////////////// 70 /////////////////////////////////////////////////////////
66 // Property Accessors/Setters 71 // Property Accessors/Setters
67 ///////////////////////////////////////////////////////// 72 /////////////////////////////////////////////////////////
73 this.setCanvas = function(c) {
74 this._canvas = c;
75 }
76
68 this.setWorld = function (world) { 77 this.setWorld = function (world) {
69 this._world = world; 78 this._world = world;
70 }; 79 };
@@ -125,7 +134,7 @@ var BrushStroke = function GLBrushStroke() {
125 //add the point only if it is some epsilon away from the previous point 134 //add the point only if it is some epsilon away from the previous point
126 var numPoints = this._Points.length; 135 var numPoints = this._Points.length;
127 if (numPoints>0) { 136 if (numPoints>0) {
128 var threshold = this._MIN_SAMPLE_DISTANCE_THRESHOLD;//this._WETNESS_FACTOR*this._strokeWidth; 137 var threshold = this._MIN_SAMPLE_DISTANCE_THRESHOLD;
129 var prevPt = this._Points[numPoints-1]; 138 var prevPt = this._Points[numPoints-1];
130 var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]]; 139 var diffPt = [prevPt[0]-pt[0], prevPt[1]-pt[1]];
131 var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]); 140 var diffPtMag = Math.sqrt(diffPt[0]*diffPt[0] + diffPt[1]*diffPt[1]);
@@ -140,7 +149,8 @@ var BrushStroke = function GLBrushStroke() {
140 }; 149 };
141 150
142 this.insertPoint = function(pt, index){ 151 this.insertPoint = function(pt, index){
143 this._Points.splice(index, 0, pt); this._dirty=true; 152 this._Points.splice(index, 0, pt);
153 this._dirty=true;
144 }; 154 };
145 155
146 this.isDirty = function(){ 156 this.isDirty = function(){
@@ -173,7 +183,7 @@ var BrushStroke = function GLBrushStroke() {
173 }; 183 };
174 184
175 this.setStrokeMaterial = function (m) { 185 this.setStrokeMaterial = function (m) {
176 this._strokeMaterial = m; 186 this._strokeMaterial = m; this._dirty = true;
177 }; 187 };
178 188
179 this.getStrokeColor = function () { 189 this.getStrokeColor = function () {
@@ -181,26 +191,64 @@ var BrushStroke = function GLBrushStroke() {
181 }; 191 };
182 192
183 this.setStrokeColor = function (c) { 193 this.setStrokeColor = function (c) {
184 this._strokeColor = c; 194 this._strokeColor = c; this._dirty = true;
185 }; 195 };
186 196
187 this.setSecondStrokeColor = function(c){ 197 this.setSecondStrokeColor = function(c){
188 this._secondStrokeColor=c; 198 this._secondStrokeColor=c; this._dirty = true;
189 } 199 }
190 200
191 this.setStrokeHardness = function(h){ 201 this.setStrokeHardness = function(h){
192 this._strokeHardness=h; 202 if (this._strokeHardness!==h){
203 this._strokeHardness=h;
204 this._dirty = true;
205 }
206 }
207 this.getStrokeHardness = function(){
208 return this._strokeHardness;
193 } 209 }
194 210
195 this.setDoSmoothing = function(s){ 211 this.setDoSmoothing = function(s){
196 this._strokeDoSmoothing = s; 212 if (this._strokeDoSmoothing!==s) {
213 this._strokeDoSmoothing = s;
214 this._dirty = true;
215 }
216 }
217
218 this.getDoSmoothing = function(){
219 return this._strokeDoSmoothing;
220 }
221
222 this.setSmoothingAmount = function(a){
223 if (this._strokeAmountSmoothing!==a) {
224 this._strokeAmountSmoothing = a;
225 this._dirty = true;
226 }
227 }
228
229 this.getSmoothingAmount = function(){
230 return this._strokeAmountSmoothing;
197 } 231 }
198 232
199 this.setStrokeUseCalligraphic = function(c){ 233 this.setStrokeUseCalligraphic = function(c){
200 this._strokeUseCalligraphic = c; 234 if (this._strokeUseCalligraphic!==c){
235 this._strokeUseCalligraphic = c;
236 this._dirty = true;
237 }
201 } 238 }
202 239
203 this.setStrokeAngle = function(a){ 240 this.setStrokeAngle = function(a){
241 if (this._strokeAngle!==a){
242 this._strokeAngle = a;
243 this._dirty = true;
244 };
245 }
246
247 this.getStrokeUseCalligraphic = function(){
248 return this._strokeUseCalligraphic;
249 }
250
251 this.getStrokeAngle = function(){
204 this._strokeAngle = a; 252 this._strokeAngle = a;
205 } 253 }
206 254
@@ -220,133 +268,96 @@ var BrushStroke = function GLBrushStroke() {
220 268
221 };//NO-OP for now 269 };//NO-OP for now
222 270
223
224 //remove and return anchor at specified index, return null on error
225 this.removePoint = function (index) {
226 var retAnchor = null;
227 if (index < this._Points.length) {
228 retPt = this._Points.splice(index, 1);
229 this._dirty=true;
230 }
231 return retPoint;
232 };
233
234 //remove all the points 271 //remove all the points
235 this.clear = function () { 272 this.clear = function () {
236 this._Points = []; 273 this._Points = [];
274 this._OrigPoints = [];
237 this._dirty=true; 275 this._dirty=true;
238 } 276 }
239 277
240 this.translate = function (tx, ty, tz) { 278 /*this.translate = function (tx, ty, tz) {
241 for (var i=0;i<this._Points.length;i++){ 279 for (var i=0;i<this._Points.length;i++){
242 this._Points[i][0]+=tx; 280 this._Points[i][0]+=tx;
243 this._Points[i][1]+=ty; 281 this._Points[i][1]+=ty;
244 this._Points[i][2]+=tz; 282 this._Points[i][2]+=tz;
245 } 283 }
246 }; 284 this._dirty = true;
285 };*/
247 286
248 this.computeMetaGeometry = function() { 287 this.computeMetaGeometry = function() {
249 if (this._dirty) { 288 var numPoints = this._Points.length;
250 var numPoints = this._Points.length; 289 if (this._addedSamples === false){
251 290 //**** add samples to the long sections of the path --- Catmull-Rom spline interpolation *****
252 //**** add samples to the path if needed...linear interpolation for now 291 // instead of the following, may use 4-point subdivision iterations over continuous regions of 'long' segments
253 //if (numPoints>1) { 292 // look at http://www.gvu.gatech.edu/~jarek/Split&Tweak/ for formula
254 if (0){ 293
255 var threshold = this._WETNESS_FACTOR*this._strokeWidth; 294 var numInsertedPoints = 0;
256 var prevPt = this._Points[0]; 295 var newSampledPoints = [];
257 var prevIndex = 0; 296 var threshold = this._MAX_SAMPLE_DISTANCE_THRESHOLD;//this determines whether a segment between two sample is long enough to warrant checking for angle
258 for (var i=1;i<numPoints;i++){ 297 var prevPt = this._Points[0];
259</