Browse files

SurfaceMeshBuilder bugfix and minor mesh-related changes

  • Loading branch information...
1 parent 99f3c80 commit 1e6b8827bb0bf1aeb25cb789f46e13c27872c50a @hapticdata hapticdata committed Jul 26, 2011
View
4 src/core/geom/mesh/SphericalHarmonics.js
@@ -4,11 +4,11 @@
*/
toxi.SphericalHarmonics = function(m) {
this.m = m;
-}
+};
toxi.SphericalHarmonics.prototype = {
- // FIXME check where flipped vertex order is coming from sometimes
+ // toxiclibs - FIXME check where flipped vertex order is coming from sometimes
computeVertexFor: function(p,phi,theta) {
var r = 0;
r += Math.pow(toxi.MathUtils.sin(this.m[0] * theta), this.m[1]);
View
22 src/core/geom/mesh/SurfaceMeshBuilder.js
@@ -10,28 +10,22 @@ toxi.SurfaceMeshBuilder = function(func) {
toxi.SurfaceMeshBuilder.prototype = {
createMesh: function(mesh_or_res,_res,size,isClosed) {
- if(mesh_or_res == null)mesh_or_res = undefined; //this is for in case people use it in toxi's examples for p5
+ if(mesh_or_res == null){
+ mesh_or_res = undefined; //this is for in case people use it in toxi's examples for p5
+ }
var mesh;
var res;
- if(_res === undefined)
- {
+ if(_res === undefined){
res = mesh_or_res;
mesh = new toxi.TriangleMesh();
- }
- else {
+ } else {
mesh = mesh_or_res;
res = _res;
- }
- if(mesh === undefined)
- {
+ } if(mesh === undefined){
mesh = new toxi.TriangleMesh();
- }
- if(size === undefined)
- {
+ } if(size === undefined){
size = 1;
- }
- if(isClosed === undefined)
- {
+ } if(isClosed === undefined) {
isClosed = true;
}
var a = new toxi.Vec3D(),
View
49 src/core/geom/mesh/TriangleMesh.js
@@ -4,6 +4,7 @@ toxi.TriangleMesh = function(name,numV,numF){
if(numV === undefined)numV = toxi.TriangleMesh.DEFAULT_NUM_VERTICES;
if(numF === undefined)numF = toxi.TriangleMesh.DEFAULT_NUM_FACES;
this.setName(name);
+ this.matrix = new toxi.Matrix4x4();
this.vertices = [];
this.faces = [];
this.numVertices = 0;
@@ -21,35 +22,28 @@ toxi.TriangleMesh.DEFAULT_STRIDE = 4;
toxi.TriangleMesh.prototype = {
addFace: function(a,b,c,n,uvA,uvB,uvC){
- if(uvC === undefined) //then it wasnt the 7 param method
- {
- if(uvB === undefined)//then it wasnt the 6 param method
- {
+ if(uvC === undefined){ //then it wasnt the 7 param method
+ if(uvB === undefined){ //then it wasnt the 6 param method
//its either the 3 or 4 param method
- if(uvA === undefined)
- {
+ if(uvA === undefined){
//3 param method
n = undefined;
uvA = undefined;
uvB = undefined;
uvC = undefined;
- }
- else
- {
+ } else {
//4 param method
uvA = undefined;
uvB = undefined;
uvC = undefined;
}
- }
- else {
+ } else {
//6 param method
//pass down the chain
uvC = uvB;
uvB = uvA;
uvA = n;
}
-
}
//7 param method
var va = this.checkVertex(a);
@@ -58,8 +52,7 @@ toxi.TriangleMesh.prototype = {
if(va.id == vb.id || va.id == vc.id || vb.id == vc.id){
console.log("ignoring invalid face: "+a + ", " +b+ ", "+c);
- }
- else {
+ } else {
if(n != undefined){
var nc = va.sub(vc).crossSelf(va.sub(vb));
if(n.dot(nc)<0){
@@ -119,17 +112,15 @@ toxi.TriangleMesh.prototype = {
computeCentroid: function(){
this.centroid.clear();
var l = this.vertices.length;
- for(var i=0;i<l;i++)
- {
+ for(var i=0;i<l;i++){
this.centroid.addSelf(this.vertices[i]);
}
return this.centroid.scaleSelf(1.0/this.numVertices).copy();
},
computeFaceNormals: function(){
var l = this.faces.length;
- for(var i=0;i<l;i++)
- {
+ for(var i=0;i<l;i++){
this.faces[i].computeNormal();
}
},
@@ -170,8 +161,7 @@ toxi.TriangleMesh.prototype = {
faceOutwards: function(){
this.computeCentroid();
var l = this.faces.length;
- for(var i=0;i<l;i++)
- {
+ for(var i=0;i<l;i++){
var f = this.faces[i];
var n = f.getCentroid().sub(this.centroid);
var dot = n.dot(f.normal);
@@ -372,11 +362,12 @@ toxi.TriangleMesh.prototype = {
* @return array of xyz vertex coords
*/
getMeshAsVertexArray: function(verts, offset, stride) {
- if(verts ===undefined)
- {
+ if(verts ===undefined){
verts = undefined;
}
- if(offset === undefined){ offset = 0;}
+ if(offset === undefined){
+ offset = 0;
+ }
if(stride === undefined){
stride = toxi.TriangleMesh.DEFAULT_STRIDE
}
@@ -594,23 +585,21 @@ toxi.TriangleMesh.prototype = {
* @return itself
*/
pointTowards: function(dir) {
-
-
return this.transform( toxi.Quaternion.getAlignmentQuat(dir, toxi.Vec3D.Z_AXIS).toMatrix4x4(), true);
},
removeFace: function(f) {
var index = -1;
var l = this.faces.length;
- for(var i=0;i<l;i++)
- {
- if(this.faces[i] == f)
- {
+ for(var i=0;i<l;i++){
+ if(this.faces[i] == f){
index = i;
break;
}
}
- if(index > -1)this.faces.splice(index,1);
+ if(index > -1){
+ this.faces.splice(index,1);
+ }
},
View
34 src/core/math/MathUtils.js
@@ -106,11 +106,11 @@ toxi.MathUtils.ceilPowerOf2 = function(x) {
pow2 <<= 1;
}
return pow2;
-}
+};
toxi.MathUtils.clip = function(a, _min, _max) {
return a < _min ? _min : (a > _max ? _max : a);
-}
+};
/**
* Clips the value to the 0.0 .. 1.0 interval.
*
@@ -125,13 +125,13 @@ toxi.MathUtils.clipNormalized = function(a) {
return 1;
}
return a;
-}
+};
toxi.MathUtils.cos = Math.cos;
toxi.MathUtils.degrees = function(radians) {
return radians * this.RAD2DEG;
-}
+};
/**
* Fast cosine approximation.
@@ -142,7 +142,7 @@ toxi.MathUtils.degrees = function(radians) {
*/
toxi.MathUtils.fastCos = function(x) {
return toxi.MathUtils.fastSin(x + ((x > toxi.MathUtils.HALF_PI) ? -toxi.MathUtils.THREE_HALVES_PI : toxi.MathUtils.HALF_PI));
-}
+};
/**
* Fast sine approximation.
@@ -154,11 +154,11 @@ toxi.MathUtils.fastCos = function(x) {
toxi.MathUtils.fastSin = function(x) {
x = toxi.MathUtils.SIN_B * x + toxi.MathUtils.SIN_A * x * Math.abs(x);
return toxi.MathUtils.SIN_P * (x * Math.abs(x) - x) + x;
-}
+};
toxi.MathUtils.flipCoin = function(rnd) {
return Math.random() < 0.5;
-}
+};
/**
* This method is a *lot* faster than using (int)Math.floor(x).
@@ -174,7 +174,7 @@ toxi.MathUtils.floor = function(x) {
y--;
}
return y;
-}
+};
/**
* Rounds down the value to the nearest lower power^2 value.
@@ -184,17 +184,17 @@ toxi.MathUtils.floor = function(x) {
*/
toxi.MathUtils.floorPowerOf2 = function(x) {
return parseInt( Math.pow(2, parseInt((Math.log(x) / toxi.MathUtils.LOG2))));
-}
+};
toxi.MathUtils.max = function(a, b, c) {
if(!c) return Math.max(a,b);
return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
-}
+};
toxi.MathUtils.min = function(a, b, c) {
if(!c)return Math.min(a,b);
return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
-}
+};
/**
* Returns a random number in the interval -1 .. +1.
@@ -203,11 +203,11 @@ toxi.MathUtils.min = function(a, b, c) {
*/
toxi.MathUtils.normalizedRandom = function() {
return Math.random() * 2 - 1;
-}
+};
toxi.MathUtils.radians = function(degrees) {
return degrees * toxi.MathUtils.DEG2RAD;
-}
+};
toxi.MathUtils.random = function(min,max) {
if(!min && !max)return Math.random();
@@ -216,7 +216,7 @@ toxi.MathUtils.random = function(min,max) {
return Math.random()*max;
}
return Math.random() * (max - min) + min;
-}
+};
toxi.MathUtils.reduceAngle = function(theta) {
@@ -228,17 +228,17 @@ toxi.MathUtils.reduceAngle = function(theta) {
theta = toxi.MathUtils.PI - theta;
}
return theta;
-}
+};
toxi.MathUtils.sign = function(x) {
return x < 0 ? -1 : (x > 0 ? 1 : 0);
-}
+};
toxi.MathUtils.sin = function(theta) {
theta = toxi.MathUtils.reduceAngle(theta);
if (Math.abs(theta) <= toxi.MathUtils.QUARTER_PI) {
return toxi.MathUtils.fastSin(theta);
}
return toxi.MathUtils.fastCos(toxi.MathUtils.HALF_PI - theta);
-}
+};

0 comments on commit 1e6b882

Please sign in to comment.