Skip to content

Commit

Permalink
mrdoob#27905 Add volume calculator for 3d mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
isalgadof committed Mar 12, 2024
1 parent 72f6c31 commit 94e4c0b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/objects/Mesh.js
Expand Up @@ -49,6 +49,39 @@ class Mesh extends Object3D {

}

volume(precision) {
function calculateVolume({ geometry, precision }) {
let volume = 0;
const vertices = geometry.attributes.position.array;
let indexes = geometry.index ? geometry.index.array : null;
function triangularVolume(p1, p2, p3) {
const v321 = p3.x * p2.y * p1.z;
const v231 = p2.x * p3.y * p1.z;
const v312 = p3.x * p1.y * p2.z;
const v132 = p2.x * p1.y * p3.z;
const v213 = p1.x * p3.y * p2.z;
const v123 = p1.x * p2.y * p3.z;
return (1.0 / 6.0) * (-v321 + v231 + v312 - v132 - v213 + v123);
}
if (!indexes) {
indexes = Array.from({ length: vertices.length / 3 }, (_, i) => i);
}
for (let i = 0; i < indexes.length; i += 3) {
const a = new THREE.Vector3().fromArray(vertices, indexes[i] * 3);
const b = new THREE.Vector3().fromArray(vertices, indexes[i + 1] * 3);
const c = new THREE.Vector3().fromArray(vertices, indexes[i + 2] * 3);
volume += triangularVolume(a, b, c);
}
if (precision) {
return Math.abs(volume);
} else {
return Math.abs(volume / 1000);
}
}
const vol = calculateVolume({ geometry: this.geometry, precision: precision });
return vol;
}

copy( source, recursive ) {

super.copy( source, recursive );
Expand Down
5 changes: 5 additions & 0 deletions test/unit/src/objects/Mesh.tests.js
Expand Up @@ -91,6 +91,11 @@ export default QUnit.module( 'Objects', () => {

} );

QUnit.todo( 'volume', ( assert ) => {

assert.ok( false, 'everything\'s gonna be alright' );

} );
QUnit.todo( 'updateMorphTargets', ( assert ) => {

assert.ok( false, 'everything\'s gonna be alright' );
Expand Down

0 comments on commit 94e4c0b

Please sign in to comment.