Object3D.js
Summary
This file defines the class for a 3D Object, which is essentially a set of vertices and an ordered list of faces.
Class Summary
|
Object3D |
Object3D is a class that defines 3D objects to displayed by Scene3D objects. |
dependencies = ['point3D', 'Plane'];
require(dependencies);
function Object3D(vertices, faces) {
this.vertices = new Array();
this.vertices2D = new Array();
this.faces = new Array();
if(vertices instanceof Array) {
this.vertices = vertices;
}
if(faces instanceof Array) {
this.faces = faces;
}
}
Object3D.prototype = {
addFace: function(vertices) {
if(vertices instanceof Array) {
this.faces[this.faces.length] = vertices;
}
return this;
},
toEach: function(func) {
var i = 0;
while(i < this.vertices.length) {
this.vertices[i] = func(this.vertices[i]);
i++;
}
return this;
},
viewIn: function(camera) {
var i = 0;
var T = new AffineTransform(3).newCoordinates(camera.theta, camera.phi);
while(i < this.vertices.length) {
this.vertices2D[i] = T.applyTo(this.vertices[i]);
i++;
}
return this;
},
to2D: function(focus) {
var focus = focus;
var i = 0;
while(i < this.vertices2D.length) {
var point = this.vertices2D[i];
var mult = focus / (focus - point.x);
this.vertices2D[i] = new point2D(mult*point.y, mult*point.z);
i++;
}
return this;
},
draw: function(svg, settings) {
settings.transform = 'scale(1,-1)';
for(i = 0; i < this.faces.length; i++) {
for(j = 0; j < this.faces[i].length - 1; j++) {
var p1 = this.vertices2D[this.faces[i][j]];
var p2 = this.vertices2D[this.faces[i][j+1]];
svg.line(null, p1.x, p1.y, p2.x, p2.y, settings);
}
var p1 = this.vertices2D[this.faces[i][j]];
var p2 = this.vertices2D[this.faces[i][0]];
svg.line(null, p1.x, p1.y, p2.x, p2.y, settings);
}
},
translate: function(tx, ty, tz) {
return this.toEach(function(x) { return x.translate(tx, ty, tz); });
},
toString: function() {
return "vertices: \n" + this.vertices.join("\n") + "\n\nfaces: \n" + this.faces.join("\n");
}
}
Object3D.Cube = function(side, center) {
if(!center) {
var center = point3D.zero;
}
var cx = center.x, cy = center.y, cz = center.z;
var length = side/2;
var verts = new Array(), faces = new Array();
verts[0] = new point3D(length, -length, length);
verts[1] = new point3D(length, length, length);
verts[2] = new point3D(-length, length, length);
verts[3] = new point3D(-length, -length, length);
verts[4] = new point3D(+length, -length, -length);
verts[5] = new point3D(+length, length, -length);
verts[6] = new point3D(-length, length, -length);
verts[7] = new point3D(-length, -length, -length);
faces[0] = [0, 4, 5, 1];
faces[1] = [1, 5, 6, 2];
faces[2] = [2, 6, 7, 3];
faces[3] = [3, 7, 4, 0];
faces[4] = [4, 5, 6, 7];
faces[5] = [0, 1, 2, 3];
return (new Object3D(verts, faces)).translate(cx, cy, cz);
}
Object3D.Sphere = function(rad, cent, segments) {
var o = new Object3D([], []);
var segs = 5;
if(segments) {
segs = segments / 2;
}
var radius = rad;
var center = point3D.zero;
if(center instanceof point3D) {
center = cent;
}
var segmentRad = Math.PI / 2 / (segs + 1);
var numSeparators = 4*segs + 4;
for(i = -segs; i <= segs; i++) {
var r = radius * Math.cos(segmentRad*i);
var y = radius * Math.sin(segmentRad*i);
for(j = 0; j < numSeparators; j++) {
var z = -1*r*Math.sin(segmentRad*j);
var x = r * Math.cos(segmentRad*j);
o.vertices.push(new point3D(x, y, z));
}
}
for(i = 0; i < 2*segs; i++) {
var face = new Array();
for(j = 0; j < numSeparators; j++) {
var i1 = j + i*numSeparators;
var i2 = (j+1) + i*numSeparators;
var i3 = (j+1) + (i+1)*numSeparators;
var i4 = j + (i+1)*numSeparators;
if(j == numSeparators-1) {
i2 = i*numSeparators;
i3 = (i+1)*numSeparators;
}
face = [i1, i2, i3, i4];
o.faces.push(face);
}
}
o.vertices.push(new point3D(0, -radius, 0));
o.vertices.push(new point3D(0, radius, 0));
for(i = 0; i < numSeparators - 1; i++) {
var i1 = i, i2 = i+1;
var i3 = o.vertices.length - 2;
o.faces.push([i1, i2, i3]);
var i1 = i+(2*segs*numSeparators), i2 = (i+1)+(2*segs*numSeparators);
var i3 = o.vertices.length - 1;
o.faces.push([i1, i2, i3]);
}
if(!(cent.equalTo(point3D.zero))) {
o.translate(cent.x, cent.y, cent.z);
}
return o;
}
Object3D.Line = function(points) {
if(points instanceof Array) {
var pts = new Array();
var segments = new Array();
for(var i = 0; i < points.length; i++) {
if(points[i] instanceof point2D) {
points[i] = points[i].make3D();
}
if(!(points[i] instanceof point3D)) {
return false;
}
pts[i] = points[i];
if(i != points.length - 1) {
segments[i] = [i, i+1];
}
}
return new Object3D(pts, segments);
}
return null;
}
Object3D.Xaxis = function(min, max) {
return Object3D.Line([Line.x.pointAt(min), Line.x.pointAt(max)]);
}
Object3D.Yaxis = function(min, max) {
return Object3D.Line([Line.y.pointAt(min), Line.y.pointAt(max)]);
}
Object3D.Zaxis = function(min, max) {
return Object3D.Line([Line.z.pointAt(min), Line.z.pointAt(max)]);
}
Documentation generated by
JSDoc on Mon Aug 11 13:58:31 2008