Skip to content

Commit 59e3446

Browse files
committed
Implemented bounds calculator.
1 parent 1c89312 commit 59e3446

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mesh.util;
2+
3+
import java.util.List;
4+
5+
import math.Bounds;
6+
import math.Vector3f;
7+
import mesh.Mesh3D;
8+
9+
public class MeshBoundsCalculator {
10+
11+
/**
12+
* Calculates the axis-aligned bounding box (AABB) for the given 3D mesh.
13+
*
14+
* @param mesh The mesh object whose bounding box needs to be calculated.
15+
* @return A {@link Bounds} object representing the calculated bounding box.
16+
*/
17+
public static Bounds calculateBounds(Mesh3D mesh) {
18+
List<Vector3f> vertices = mesh.getVertices();
19+
if (vertices.isEmpty()) {
20+
return new Bounds(Vector3f.ZERO, Vector3f.ZERO);
21+
}
22+
23+
Vector3f min = new Vector3f(vertices.get(0));
24+
Vector3f max = new Vector3f(vertices.get(0));
25+
26+
for (Vector3f vertex : vertices) {
27+
min.set(
28+
Math.min(min.getX(), vertex.getX()),
29+
Math.min(min.getY(), vertex.getY()),
30+
Math.min(min.getZ(), vertex.getZ()));
31+
max.set(
32+
Math.max(max.getX(), vertex.getX()),
33+
Math.max(max.getY(), vertex.getY()),
34+
Math.max(max.getZ(), vertex.getZ()));
35+
}
36+
37+
return new Bounds(min, max);
38+
}
39+
}

0 commit comments

Comments
 (0)