Hi everyone,
I have managed to calculate the voule of the stl file I uploaded. I can get the distance between two points using p1.distanceTo(p2). However, I’m unable to get the volume of the geometry. It always return 0. Here is my
code:
var object
var loader = new THREE.STLLoader();
loader.load(box.stl, function(geometry){
var geo = new THREE.Geometry().fromBufferGeometry(geometry);
var material = new THREE.MeshStandardMaterial({color: 0xffff, side: THREE.DoubleSide, wireframe:true});
object = new THREE.Mesh(geo, material);
object.position.set(0,-20,0);
scene.add(object);
// getVolume(geometry);
getVolume(geometry)
console.log(‘sum:’,getVolume(geometry));
});
function signedVolumeOfTriangle(p1, p2, p3){
// return vol = p1.dot(p2.cross(p3))/6.0;
var v321 = p3.xp2.yp1.z;
var v231 = p2.xp3.yp1.z;
var v312 = p3.xp1.yp2.z;
var v132 = p1.xp3.yp2.z;
var v213 = p2.xp1.yp3.z;
var v123 = p1.xp2.yp3.z;
return (1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123);
}
// Get volume
function getVolume(geometry){
if(!geometry.isBufferGeometry){
console.log("‘geometry’ must be an indexed or non-indexed buffer geometry");
return 0;
}
var isIndexed = geometry.index !==null;
let position = geometry.attributes.position;
let p1 = new THREE.Vector3(),
p2= new THREE.Vector3(),
p3 = new THREE.Vector3();
let sum = 0;
if(!isIndexed){
let faces = position.count / 3;
for(let i=0; i<faces; i++){
p1.fromBufferAttribute(position, i*3 + 0);
p2.fromBufferAttribute(position, i * 3 + 1);
p3.fromBufferAttribute(position, i * 3 + 0);
sum += signedVolumeOfTriangle(p1, p2, p3);
}
} else {
let index = geometry.index;
let faces = index.count / 3;
for (let i = 0; i < faces; i++){
p1.fromBufferAttribute(position, index.array[i * 3 + 0]);
p2.fromBufferAttribute(position, index.array[i * 3 + 1]);
p3.fromBufferAttribute(position, index.array[i * 3 + 2]);
sum += signedVolumeOfTriangle(p1, p2, p3);
}
}
return sum;
}
I’m creating a web app where a customer can load and stl file and, the volume and other dimensions can be generated. The generated data will be used in determine the price for 3d printing.
Any help will be much appreciated.
Thanks.