How o accuratley calculate the size of Line2 mesh and Boxgeometry?

I currently have one mesh with LineSegmets and another with BoxGeometry since I have a requirement where I need to check the bounding box size of the mesh and return mesh that has a tiny bounding box. basically the small mesh .
The bounding box size and radius of BoxGeometry are greater than those of LineSegment mesh, even if LineSegment mesh appears to be larger when compared to BoxGeometry
Therefore, I fail to see why the LineSegment geometry box and radius size are not larger.

here is my code and link to the CodeSandbox codesandbox

import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/Addons.js";
let box1, scene, camera, renderer, materialLines, geometryLines, boxMesh;

function getBoundingBoxSize(object) {
  const box = new THREE.Box3().setFromObject(object);
  const size = new THREE.Vector3();
  box.getSize(size);
  return size.length();
}

function getBoundingSphere(object) {
  const sphere = new THREE.Sphere();
  object.geometry.computeBoundingSphere();
  sphere.copy(object.geometry.boundingSphere);
  return sphere.radius;
}

function init() {
  scene = new THREE.Scene();
  camera = new THREE.OrthographicCamera();
  camera.position.z = 2;
  renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
  var controls = new OrbitControls(camera, renderer.domElement);

  materialLines = new THREE.LineBasicMaterial({
    color: 0xff0000,
    side: THREE.DoubleSide,
  });

  const w = 0.2;
  const h = 0.5;

  const verticesBox1 = new Float32Array([
    -w / 2,
    h / 2,
    0,
    w / 2,
    h / 2,
    0,

    w / 2,
    h / 2,
    0,
    w / 2,
    -h / 2,
    0,

    w / 2,
    -h / 2,
    0,
    -w / 2,
    -h / 2,
    0,

    -w / 2,
    -h / 2,
    0,
    -w / 2,
    h / 2,
    0,
  ]);

  geometryLines = new THREE.BufferGeometry();
  geometryLines.setAttribute(
    "position",
    new THREE.BufferAttribute(verticesBox1, 3)
  );

  box1 = new THREE.LineSegments(geometryLines, materialLines);
  box1.position.set(-0.2, 0, 0);
  scene.add(box1);

  boxMesh = new THREE.Mesh(new THREE.BoxGeometry(0.05, 0.05));
  boxMesh.position.set(-0.2, 0, 0);
  scene.add(boxMesh);

  console.log("bboxsize line box1", getBoundingBoxSize(box1));
  console.log("bboxsize boxMesh", getBoundingBoxSize(boxMesh));
  console.log("bounding sphere box1", getBoundingSphere(box1));
  console.log("bounding sphere boxMesh", getBoundingSphere(boxMesh));
}

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
}

init();
animate();

If you skip the z-size of a box:

new THREE.Mesh(new THREE.BoxGeometry(0.05, 0.05));

it is assumed to be 1, like this:

new THREE.Mesh(new THREE.BoxGeometry(0.05, 0.05, 1));

This explains why you get large values of the box bounding box/sphere. If you explicitly set 0, I guess, the bounding sizes must be smaller.

Also, for complex objects with subobjects it is better to use .setFromObject(object,true) instead of .setFromObject(object), as it gives more precise results. For simple objects they both return the same.

2 Likes

Thanks a ton man I was really struggling to find out what was the issue this was the case I just added 0 as the depth for the box and now it is returning correct size.

Just one thing what do you suggest to calculate size of meshes which is more precise comparing bounding sphere radius or bounding box size

Thanks Again!!!

If the mesh is globular, bounding sphere is more precise. If the mesh is rentagularish, the bounding box is more precise.

However, both of them are used. For example, in raycasting, first the bounding sphere is tested for intersection. If it indicates possible intersection, then the bounding box is tested. If it indicates possible intersection, then the triangles in the mesh geometry are tested for intersection.

1 Like