using Float64Array for vector3D, but this turns out to be too slow

This commit is contained in:
Joost Nieuwenhuijse 2012-02-21 16:30:21 +01:00
parent e91a2f9de3
commit 998abea367

169
csg.js
View file

@ -513,17 +513,13 @@ CSG.prototype = {
var bounds = polygon.boundingBox(); var bounds = polygon.boundingBox();
if(i == 0) if(i == 0)
{ {
minpoint = bounds[0].clone(); minpoint = bounds[0];
maxpoint = bounds[1].clone(); maxpoint = bounds[1];
} }
else else
{ {
minpoint.x = Math.min(minpoint.x, bounds[0].x); minpoint = minpoint.min(bounds[0]);
minpoint.y = Math.min(minpoint.y, bounds[0].y); maxpoint = maxpoint.max(bounds[1]);
minpoint.z = Math.min(minpoint.z, bounds[0].z);
maxpoint.x = Math.max(maxpoint.x, bounds[1].x);
maxpoint.y = Math.max(maxpoint.y, bounds[1].y);
maxpoint.z = Math.max(maxpoint.z, bounds[1].z);
} }
} }
this.cachedBoundingBox = [minpoint, maxpoint]; this.cachedBoundingBox = [minpoint, maxpoint];
@ -534,6 +530,7 @@ CSG.prototype = {
// returns true if there is a possibility that the two solids overlap // returns true if there is a possibility that the two solids overlap
// returns false if we can be sure that they do not overlap // returns false if we can be sure that they do not overlap
mayOverlap: function(csg) { mayOverlap: function(csg) {
return true;
if( (this.polygons.length == 0) || (csg.polygons.length == 0) ) if( (this.polygons.length == 0) || (csg.polygons.length == 0) )
{ {
return false; return false;
@ -1135,77 +1132,109 @@ CSG.roundedCube = function(options) {
// new CSG.Vector3D({ x: 1, y: 2, z: 3 }); // new CSG.Vector3D({ x: 1, y: 2, z: 3 });
CSG.Vector3D = function(x, y, z) { CSG.Vector3D = function(x, y, z) {
var ok = true; var value = []; //new Float64Array(3);
if (arguments.length == 1) if (arguments.length == 3)
{ {
if(typeof(x) == "object") // this.x = Number(x);
// this.y = Number(y);
// this.z = Number(z);
value[0] = x;
value[1] = y;
value[2] = z;
}
else
{
var ok = true;
if (arguments.length == 1)
{ {
if(x instanceof Array) if(typeof(x) == "object")
{ {
this.x = x[0]; if(x instanceof CSG.Vector3D)
this.y = x[1]; {
this.z = x[2]; value = x.value;
} }
else if( ('x' in x) && ('y' in x) && ('z' in x) ) else if(x instanceof Array)
{ {
this.x = x.x; value[0] = x[0];
this.y = x.y; value[1] = x[1];
this.z = x.z; value[2] = x[2];
}
else if( ('x' in x) && ('y' in x) && ('z' in x) )
{
value[0] = x.x;
value[1] = x.y;
value[2] = x.z;
}
else ok = false;
}
else
{
var v = Number(x);
value[0] = v;
value[1] = v;
value[2] = v;
} }
else ok = false;
} }
else else ok = false;
if(!ok)
{ {
var v = Number(x); throw new Error("wrong arguments");
this.x = v;
this.y = v;
this.z = v;
} }
} }
else if (arguments.length == 3) this.value = value;
{
this.x = Number(x);
this.y = Number(y);
this.z = Number(z);
}
else ok = false;
if(!ok)
{
throw new Error("wrong arguments");
}
}; };
CSG.Vector3D.prototype = { CSG.Vector3D.prototype = {
get x() {
return this.value[0];
},
get y() {
return this.value[1];
},
get z() {
return this.value[2];
},
set x(v) {
throw new Error("Vector3D is immutable");
},
set y(v) {
throw new Error("Vector3D is immutable");
},
set z(v) {
throw new Error("Vector3D is immutable");
},
clone: function() { clone: function() {
return new CSG.Vector3D(this.x, this.y, this.z); return new CSG.Vector3D(this);
}, },
negated: function() { negated: function() {
return new CSG.Vector3D(-this.x, -this.y, -this.z); return new CSG.Vector3D(-this.value[0], -this.value[1], -this.value[2]);
}, },
abs: function() { abs: function() {
return new CSG.Vector3D(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z)); return new CSG.Vector3D(Math.abs(this.value[0]), Math.abs(this.value[1]), Math.abs(this.value[2]));
}, },
plus: function(a) { plus: function(a) {
return new CSG.Vector3D(this.x + a.x, this.y + a.y, this.z + a.z); return new CSG.Vector3D(this.value[0] + a.value[0], this.value[1] + a.value[1], this.value[2] + a.value[2]);
}, },
minus: function(a) { minus: function(a) {
return new CSG.Vector3D(this.x - a.x, this.y - a.y, this.z - a.z); return new CSG.Vector3D(this.value[0] - a.value[0], this.value[1] - a.value[1], this.value[2] - a.value[2]);
}, },
times: function(a) { times: function(a) {
return new CSG.Vector3D(this.x * a, this.y * a, this.z * a); return new CSG.Vector3D(this.value[0] * a, this.value[1] * a, this.value[2] * a);
}, },
dividedBy: function(a) { dividedBy: function(a) {
return new CSG.Vector3D(this.x / a, this.y / a, this.z / a); return new CSG.Vector3D(this.value[0] / a, this.value[1] / a, this.value[2] / a);
}, },
dot: function(a) { dot: function(a) {
return this.x * a.x + this.y * a.y + this.z * a.z; return this.value[0] * a.value[0] + this.value[1] * a.value[1] + this.value[2] * a.value[2];
}, },
lerp: function(a, t) { lerp: function(a, t) {
@ -1226,9 +1255,9 @@ CSG.Vector3D.prototype = {
cross: function(a) { cross: function(a) {
return new CSG.Vector3D( return new CSG.Vector3D(
this.y * a.z - this.z * a.y, this.value[1] * a.value[2] - this.value[2] * a.value[1],
this.z * a.x - this.x * a.z, this.value[2] * a.value[0] - this.value[0] * a.value[2],
this.x * a.y - this.y * a.x this.value[0] * a.value[1] - this.value[1] * a.value[0]
); );
}, },
@ -1241,7 +1270,7 @@ CSG.Vector3D.prototype = {
}, },
equals: function(a) { equals: function(a) {
return (this.x == a.x) && (this.y == a.y) && (this.z == a.z); return (this.value[0] == a.value[0]) && (this.value[1] == a.value[1]) && (this.value[2] == a.value[2]);
}, },
// Right multiply by a 4x4 matrix (the vector is interpreted as a row vector) // Right multiply by a 4x4 matrix (the vector is interpreted as a row vector)
@ -1255,21 +1284,21 @@ CSG.Vector3D.prototype = {
}, },
toStlString: function() { toStlString: function() {
return this.x+" "+this.y+" "+this.z; return this.value[0]+" "+this.value[1]+" "+this.value[2];
}, },
toString: function() { toString: function() {
return "("+this.x+", "+this.y+", "+this.z+")"; return "("+this.value[0]+", "+this.value[1]+", "+this.value[2]+")";
}, },
// find a vector that is somewhat perpendicular to this one // find a vector that is somewhat perpendicular to this one
randomNonParallelVector: function() { randomNonParallelVector: function() {
var abs = this.abs(); var abs = this.abs();
if( (abs.x <= abs.y) && (abs.x <= abs.z) ) if( (abs.value[0] <= abs.value[1]) && (abs.value[0] <= abs.value[2]) )
{ {
return new CSG.Vector3D(1,0,0); return new CSG.Vector3D(1,0,0);
} }
else if( (abs.y <= abs.x) && (abs.y <= abs.z) ) else if( (abs.value[1] <= abs.value[0]) && (abs.value[1] <= abs.value[2]) )
{ {
return new CSG.Vector3D(0,1,0); return new CSG.Vector3D(0,1,0);
} }
@ -1279,6 +1308,21 @@ CSG.Vector3D.prototype = {
} }
}, },
min: function(p) {
return new CSG.Vector3D(
Math.min(this.value[0], p.value[0]),
Math.min(this.value[1], p.value[1]),
Math.min(this.value[2], p.value[2])
);
},
max: function(p) {
return new CSG.Vector3D(
Math.max(this.value[0], p.value[0]),
Math.max(this.value[1], p.value[1]),
Math.max(this.value[2], p.value[2])
);
},
}; };
// # class Vertex // # class Vertex
@ -1743,22 +1787,17 @@ CSG.Polygon.prototype = {
if(numvertices == 0) if(numvertices == 0)
{ {
minpoint=new CSG.Vector3D(0,0,0); minpoint=new CSG.Vector3D(0,0,0);
maxpoint=new CSG.Vector3D(0,0,0);
} }
else else
{ {
minpoint=vertices[0].pos.clone(); minpoint=vertices[0].pos;
maxpoint=vertices[0].pos.clone();
} }
maxpoint=minpoint;
for(var i=1; i < numvertices; i++) for(var i=1; i < numvertices; i++)
{ {
var point = vertices[i].pos; var point = vertices[i].pos;
minpoint.x = Math.min(minpoint.x, point.x); minpoint = minpoint.min(point);
minpoint.y = Math.min(minpoint.y, point.y); maxpoint = maxpoint.max(point);
minpoint.z = Math.min(minpoint.z, point.z);
maxpoint.x = Math.max(maxpoint.x, point.x);
maxpoint.y = Math.max(maxpoint.y, point.y);
maxpoint.z = Math.max(maxpoint.z, point.z);
} }
this.cachedBoundingBox = [minpoint, maxpoint]; this.cachedBoundingBox = [minpoint, maxpoint];
} }
@ -2029,10 +2068,10 @@ CSG.PolygonTreeNode.prototype = {
{ {
var bound = polygon.boundingSphere(); var bound = polygon.boundingSphere();
var sphereradius = bound[1] + 1e-4; var sphereradius = bound[1] + 1e-4;
// var d = plane.normal.dot(bound[0]) - plane.w;
var planenormal = plane.normal; var planenormal = plane.normal;
var spherecenter = bound[0]; var spherecenter = bound[0];
var d = planenormal.x*spherecenter.x + planenormal.y*spherecenter.y + planenormal.z*spherecenter.z - plane.w; var d = planenormal.dot(spherecenter) - plane.w;
// var d = planenormal.x*spherecenter.x + planenormal.y*spherecenter.y + planenormal.z*spherecenter.z - plane.w;
if(d > sphereradius) if(d > sphereradius)
{ {
frontnodes.push(this); frontnodes.push(this);