Skip to content

Commit

Permalink
added .tree property to Trimesh
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Feb 1, 2015
1 parent a063586 commit d2bbd8a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/shapes/Trimesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Vec3 = require('../math/Vec3');
var Quaternion = require('../math/Quaternion');
var Transform = require('../math/Transform');
var AABB = require('../collision/AABB');
var Octree = require('../utils/Octree');

/**
* @class Trimesh
Expand Down Expand Up @@ -61,16 +62,45 @@ function Trimesh(vertices, indices) {
*/
this.edges = null;

this.tree = new Octree();

this.updateEdges();
this.updateNormals();
this.updateAABB();
this.updateBoundingSphereRadius();
this.updateTree();
}
Trimesh.prototype = new Shape();
Trimesh.prototype.constructor = Trimesh;

var computeNormals_n = new Vec3();

/**
* @method updateTree
*/
Trimesh.prototype.updateTree = function(){
var tree = this.tree;

tree.reset();
tree.aabb.copy(this.aabb);

// Insert all triangles
var triangleAABB = new AABB();
var a = new Vec3();
var b = new Vec3();
var c = new Vec3();
var points = [a, b, c];
for (var i = 0; i < this.indices.length / 3; i++) {
this.getTriangleVertices(i, a, b, c);
triangleAABB.setFromPoints(points);
tree.insert(triangleAABB, i);
}
};

Trimesh.prototype.getTrianglesInAABB = function(aabb, result){
return this.tree.aabbQuery(aabb, result);
};

/**
* Compute the normals of the faces. Will save in the .normals array.
* @method updateNormals
Expand Down
38 changes: 32 additions & 6 deletions test/Trimesh.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var Vec3 = require("../src/math/Vec3")
, Quaternion = require("../src/math/Quaternion")
, Plane = require('../src/shapes/Plane')
, Trimesh = require('../src/shapes/Trimesh')
, World = require('../src/world/World')
, Body = require('../src/objects/Body')
var Vec3 = require("../src/math/Vec3");
var Quaternion = require("../src/math/Quaternion");
var Plane = require('../src/shapes/Plane');
var Trimesh = require('../src/shapes/Trimesh');
var World = require('../src/world/World');
var Body = require('../src/objects/Body');
var AABB = require('../src/collision/AABB');

module.exports = {
updateNormals: function(test){
Expand All @@ -22,6 +23,31 @@ module.exports = {
test.done();
},

updateTree: function(test){
var mesh = Trimesh.createTorus();
mesh.updateTree();
test.done();
},

getTrianglesInAABB: function(test){
var mesh = Trimesh.createTorus(1,1,16,16);
var result = [];

// Should get all triangles if we use the full AABB
var aabb = mesh.aabb.clone();
mesh.getTrianglesInAABB(aabb, result);
test.equal(result.length, mesh.indices.length / 3);

// Should get less triangles if we use the half AABB
result.length = 0;
aabb.lowerBound.scale(0.5, aabb.lowerBound);
aabb.upperBound.scale(0.5, aabb.upperBound);
mesh.getTrianglesInAABB(aabb, result);
test.ok(result.length < mesh.indices.length / 3);

test.done();
},

getVertex: function(test){
var mesh = Trimesh.createTorus();
var vertex = new Vec3();
Expand Down

0 comments on commit d2bbd8a

Please sign in to comment.