Issue at Hand:
The challenge lies in the fact that the bounding box volume is turning out to be smaller than the volume calculated from the mesh.
Attempts So Far:
To begin with, I computed the volume of a bounding box using the following code:
//loaded .obj mesh object:
var sizer = new THREE.Box3().setFromObject(obj);
console.log(sizer.size().x*sizer.size().z*sizer.size().y);
Output: 197112.65382983384
Next, I proceeded to calculate the mesh volume utilizing this method and with the legacy geometry object in "calculateVolume" method included below:
console.log(scope.calculateVolume(child));
The calculateVolume function belongs to a class. Listed below are some of the class methods:
threeDeeAsset.prototype.signedVolumeOfTriangle = function(p1, p2, p3) {
var v321 = p3.x*p2.y*p1.z;
var v231 = p2.x*p3.y*p1.z;
var v312 = p3.x*p1.y*p2.z;
var v132 = p1.x*p3.y*p2.z;
var v213 = p2.x*p1.y*p3.z;
var v123 = p1.x*p2.y*p3.z;
return (-v321 + v231 + v312 - v132 - v213 + v123)/6;
}
threeDeeAsset.prototype.calculateVolume = function(object){
var volumes = 0;
object.legacy_geometry = new THREE.Geometry().fromBufferGeometry(object.geometry);
for(var i = 0; i < object.legacy_geometry.faces.length; i++){
var Pi = object.legacy_geometry.faces[i].a;
var Qi = object.legacy_geometry.faces[i].b;
var Ri = object.legacy_geometry.faces[i].c;
var P = new THREE.Vector3(object.legacy_geometry.vertices[Pi].x, object.legacy_geometry.vertices[Pi].y, object.legacy_geometry.vertices[Pi].z);
var Q = new THREE.Vector3(object.legacy_geometry.vertices[Qi].x, object.legacy_geometry.vertices[Qi].y, object.legacy_geometry.vertices[Qi].z);
var R = new THREE.Vector3(object.legacy_geometry.vertices[Ri].x, object.legacy_geometry.vertices[Ri].y, object.legacy_geometry.vertices[Ri].z);
volumes += this.signedVolumeOfTriangle(P, Q, R);
}
return Math.abs(volumes);
}
Output: 336896.1562770668
Investigating Vertex Sources:
I also experimented with buffergeometry and as expected, it consisted of the same array, albeit in a typed Float32Array which yielded identical results:
var vol = 0;
scope.mesh.traverse(function (child) {
if (child instanceof THREE.Mesh) {
var positions = child.geometry.getAttribute("position").array;
for(var i=0;i<positions.length; i+=9){
var t1 = {};
t1.x = positions[i+0];
t1.y = positions[i+1];
t1.z = positions[i+2];
var t2 = {};
t2.x = positions[i+3];
t2.y = positions[i+4];
t2.z = positions[i+5];
var t3 = {};
t3.x = positions[i+6];
t3.y = positions[i+7];
t3.z = positions[i+8];
vol += scope.signedVolumeOfTriangle(t1,t2,t3);
}
}
});
console.log(vol);
Output: 336896.1562770668
The Query at Hand:
Why is it that my bounding box volume appears to be less than the calculated volume of a closed mesh? It's possible that there might be an offset position that I am overlooking or maybe I am not calculating the bounding box volume accurately. Despite conducting multiple searches on both Google and Stack Overflow, I have yet to find a solution, resulting in me stumbling upon the signedVolumeOfTriangle function mentioned above, which seems to be widely accepted.