I'm trying to create an accurate bounding box of an object, but it appears that if the object isn't aligned with the axis (I think) the box is not aligned with the object.
For example:
The pink and closer orange vertices are the Box3.min, Box3.max of this wall, but you see the red and green and blue are not on that wall. You can ignore the aqua vertices.
This is the code that creates the bounding box (returns Box3):
static getWorldBoundingBox(model, dbId) {
return new Promise(async(resolve, reject)=>{
try{
var fragIds = await ViewerToolkit.getFragIds(
model, dbId);
if(!fragIds.length){
return reject('No geometry, invalid dbId?');
}
var fragList = model.getFragmentList();
var fragbBox = new THREE.Box3();
var nodebBox = new THREE.Box3();
fragIds.forEach(function(fragId) {
fragList.getWorldBounds(fragId, fragbBox);
nodebBox.union(fragbBox);
});
return resolve(nodebBox);
}
catch(ex){
return reject(ex);
}
});
}
And that's how I create the box from the min, max:
let ddd = new THREE.Vector3(min.x, min.y, min.z);
let ddu = new THREE.Vector3(min.x, min.y, max.z);
let dud = new THREE.Vector3(min.x, max.y, min.z);
let udd = new THREE.Vector3(max.x, min.y, min.z);
let duu = new THREE.Vector3(min.x, max.y, max.z);
let uud = new THREE.Vector3(max.x, max.y, min.z);
let udu = new THREE.Vector3(max.x, min.y, max.z);
let uuu = new THREE.Vector3(max.x, max.y, max.z);
this.drawVertices([ddd,ddu,dud,udd,duu,uud,udu,uuu]);
let facesPoints = [
{
BL: ddd.clone(),
UL: ddu.clone(),
UR: udu.clone(),
BR: udd.clone()
},
{
BL: udd.clone(),
UL: udu.clone(),
UR: uuu.clone(),
BR: uud.clone()
},
{
BL: uud.clone(),
UL: uuu.clone(),
UR: duu.clone(),
BR: dud.clone()
},
{
BL: dud.clone(),
UL: duu.clone(),
UR: ddu.clone(),
BR: ddd.clone()
}
];
I want to avoid a brute force approach of sorting all distances of all pairs of vertices and taking the first two.
Is there another data structure will expose 8 points of a cube instead of just 2 that I could give to it polygons to build it just like in the above function?