Skip to content

Commit

Permalink
bugfixes for toxi.Vec2D, toxi.Vec3D, finished toxi.Cone which repairs…
Browse files Browse the repository at this point in the history
… AxisAlignedCylinder's toMesh(). First commit of toxi.processing.ToxiclibsSupport for Processing.js (wip).
  • Loading branch information
hapticdata committed Sep 8, 2011
1 parent 6c6c7c6 commit bc05589
Show file tree
Hide file tree
Showing 13 changed files with 1,419 additions and 973 deletions.
2 changes: 1 addition & 1 deletion build/toxi-color.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

238 changes: 122 additions & 116 deletions build/toxi-core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/toxi-physics2d.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

258 changes: 132 additions & 126 deletions build/toxiclibs.js

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions src/core/geom/AxisAlignedCylinder.js
Expand Up @@ -73,14 +73,20 @@ toxi.AxisAlignedCylinder.prototype = {
* @return mesh instance
*/
toMesh: function(a,b,c) {
if(a === undefined)
{
return new toxi.Cone(this.pos,this.getMajorAxis().getVector(),this.radius,this.radius,this.length).toMesh(null,12,0,true,true);
var opts = {
mesh: undefined,
steps: 12,
thetaOffset: 0
};
if(arguments.length == 1 && typeof arguments[0] == 'object'){ //options object
for(var prop in arguments[0]){
opts[prop] = arguments[0][prop];
}
} else if(arguments.length == 2){
opts.steps = arguments[0];
opts.thetaOffset = arguments[1];
}
else if(a instanceof toxi.TriangleMesh3D)
{
return new toxi.Cone(this.pos,this.getMajorAxis().getVector(),this.radius,this.radius,this.length).toMesh(a,b,c,true,true);
}
return new toxi.Cone(this.pos,this.getMajorAxis().getVector(),this.radius,this.radius,this.length).toMesh(null,a,b,true,true);
var cone = new toxi.Cone(this.pos,this.getMajorAxis().getVector(), this.radius, this.radius, this.length);
return cone.toMesh(opts.mesh,opts.steps,opts.thetaOffset,true,true);
}
};
120 changes: 69 additions & 51 deletions src/core/geom/Cone.js
Expand Up @@ -10,7 +10,7 @@
* @param pos
* centre position
* @param dir
* direction
* direction vector
* @param rNorth
* radius on the side in the forward direction
* @param rSouth
Expand All @@ -21,65 +21,83 @@
toxi.Cone = function(pos,dir,rNorth, rSouth,len) {
toxi.Vec3D.apply(this,[pos]);
this.dir = dir.getNormalized();
this.radiusNorth = rNxiorth;
this.radiusNorth = rNorth;
this.radiusSouth = rSouth;
this.length = len;
};

toxi.extend(toxi.Cone,toxi.Vec3D);

toxi.Cone.prototype.toMesh = function(a,b,c,topClosed,bottomClosed) {
if(topClosed === undefined){
topClosed = true;
toxi.Cone.prototype.toMesh = function(args) {
var opts = {
mesh : undefined,
steps : NaN,
thetaOffset : 0,
topClosed : true,
bottomClosed : true
};


if ( arguments.length == 1) {
if ( typeof arguments[0] == 'object' ) {
//##then it was a javascript option-object
var optionsObject = arguments[0];
for(var prop in optionsObject){
opts[prop] = optionsObject[prop];
}
} else {
opts.steps = arguments[0];
}
}
if(bottomClosed === undefined){
bottomClosed = true;
else if ( arguments.length == 2 ) {
opts.steps = arguments[0];
opts.thetaOffset = arguments[1];
}
if(b === undefined){
var mesh = null;
var steps = a;
var thetaOffset = 0;
} else if( c === undefined){
var mesh = null;
var steps = a;
var thetaOffset = b;
} else {
var mesh = a;
var steps = b;
var thetaOffset = c;
else if ( arguments.length == 5 ) {
opts.mesh = arguments[0];
opts.steps = arguments[1];
opts.thetaOffset = arguments[2];
opts.topClosed = arguments[3];
opts.bottomClosed = arguments[4];
}

var c = this.add(0.01, 0.01, 0.01),
n = c.cross(this.dir.getNormalized()).normalize(),
halfAxis = this.dir.scale(this.length * 0.5),
p = sub(halfAxis),
q = add(halfAxis),
south = new Array(steps),
north = new Array(steps);
phi = MathUtils.TWO_PI / steps;
for (var i = 0; i < steps; i++) {
var theta = i * phi + thetaOffset;
nr = n.getRotatedAroundAxis(this.dir, theta);
south[i] = nr.scale(this.radiusSouth).addSelf(p);
north[i] = nr.scale(this.radiusNorth).addSelf(q);
}
var numV = steps * 2 + 2;
var numF = steps * 2 + (topClosed ? steps : 0) + (bottomClosed ? steps : 0);
if (mesh == null) {
mesh = new toxi.TriangleMesh("cone", numV, numF);
}
for (var i = 0, j = 1; i < steps; i++, j++) {
if (j == steps) {
j = 0;
}
mesh.addFace(south[i], north[i], south[j], null, null, null, null);
mesh.addFace(south[j], north[i], north[j], null, null, null, null);
if (bottomClosed) {
mesh.addFace(p, south[i], south[j], null, null, null, null);
}
if (topClosed) {
mesh.addFace(north[i], q, north[j], null, null, null, null);
}
}
return mesh;
n = c.cross(this.dir.getNormalized()).normalize(),
halfAxis = this.dir.scale(this.length * 0.5),
p = this.sub(halfAxis),
q = this.add(halfAxis),
south = [],
north = [],
phi = (Math.PI*2) / opts.steps;


var i = 0;
for(i=0;i<opts.steps;i++){
var theta = i * phi + opts.thetaOffset;
var nr = n.getRotatedAroundAxis(this.dir,theta);

south[i] = nr.scale(this.radiusSouth).addSelf(p),
north[i] = nr.scale(this.radiusNorth).addSelf(q);
}


var numV = opts.steps * 2 + 2,
numF = opts.steps * 2 + (opts.topClosed ? opts.steps : 0) + (opts.bottomClosed ? opts.steps : 0),
mesh = opts.mesh || new toxi.TriangleMesh("cone",numV,numF);

for(i=0, j=1; i<opts.steps; i++, j++){
if(j == opts.steps){
j = 0;
}
mesh.addFace(south[i],north[i],south[j],undefined,undefined,undefined,undefined);
mesh.addFace(south[j],north[i],north[j],undefined,undefined,undefined,undefined);
if(opts.bottomClosed){
mesh.addFace(p, south[i], south[j], undefined,undefined,undefined,undefined);
}
if(opts.topClosed){
mesh.addFace(north[i], q, north[j], undefined,undefined,undefined,undefined);
}
}

return mesh;
};
4 changes: 0 additions & 4 deletions src/core/geom/Ray2D.js
Expand Up @@ -10,16 +10,12 @@ toxi.Ray2D = function(a,b,d){
toxi.Vec2D.apply(this);
this.dir = toxi.Vec2D.Y_AXIS.copy();
}
console.log(this);
};
toxi.extend(toxi.Ray2D,toxi.Vec2D);

toxi.Ray2D.prototype.getDirection = function() {
return this.dir.copy();
};



/**
* Calculates the distance between the given point and the infinite line
* coinciding with this ray.
Expand Down
4 changes: 2 additions & 2 deletions src/core/geom/Ray3D.js
Expand Up @@ -14,7 +14,7 @@ toxi.Ray3D = function(a,b,c,d){
}
toxi.Vec3D.apply(this,[o]);
this.dir = dir;
}
};

toxi.extend(toxi.Ray3D,toxi.Vec3D);

Expand Down Expand Up @@ -72,7 +72,7 @@ toxi.Ray3D.prototype.setDirection = function(d) {
* @return line segment
*/
toxi.Ray3D.prototype.toLine3DWithPointAtDistance = function(dist) {
return new Line3D(this, this.getPointAtDistance(dist));
return new toxi.Line3D(this, this.getPointAtDistance(dist));
};

toxi.Ray3D.prototype.toString = function() {
Expand Down

0 comments on commit bc05589

Please sign in to comment.