VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
boundingBox.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers 20.12.2025.
5//
6#include "boundingBox.h"
7
8namespace visutwin::canvas
9{
10 void BoundingBox::setFromTransformedAabb(const BoundingBox& aabb, const Matrix4& m, bool ignoreScale)
11 {
12 const Vector3& ac = aabb.center();
13 const Vector3& ar = aabb.halfExtents();
14 const float acx = ac.getX();
15 const float acy = ac.getY();
16 const float acz = ac.getZ();
17 const float arx = ar.getX();
18 const float ary = ar.getY();
19 const float arz = ar.getZ();
20
21 float mx0 = m.getElement(0, 0);
22 float mx1 = m.getElement(1, 0);
23 float mx2 = m.getElement(2, 0);
24 float my0 = m.getElement(0, 1);
25 float my1 = m.getElement(1, 1);
26 float my2 = m.getElement(2, 1);
27 float mz0 = m.getElement(0, 2);
28 float mz1 = m.getElement(1, 2);
29 float mz2 = m.getElement(2, 2);
30
31 // Renormalize axis if scale is to be ignored
32 if (ignoreScale) {
33 float lengthSq = mx0 * mx0 + mx1 * mx1 + mx2 * mx2;
34 if (lengthSq > 0) {
35 const float invLength = 1.0f / std::sqrt(lengthSq);
36 mx0 *= invLength;
37 mx1 *= invLength;
38 mx2 *= invLength;
39 }
40
41 lengthSq = my0 * my0 + my1 * my1 + my2 * my2;
42 if (lengthSq > 0) {
43 const float invLength = 1.0f / std::sqrt(lengthSq);
44 my0 *= invLength;
45 my1 *= invLength;
46 my2 *= invLength;
47 }
48
49 lengthSq = mz0 * mz0 + mz1 * mz1 + mz2 * mz2;
50 if (lengthSq > 0) {
51 const float invLength = 1.0f / std::sqrt(lengthSq);
52 mz0 *= invLength;
53 mz1 *= invLength;
54 mz2 *= invLength;
55 }
56 }
57
58 _center = Vector3(
59 m.getElement(3, 0) + mx0 * acx + mx1 * acy + mx2 * acz,
60 m.getElement(3, 1) + my0 * acx + my1 * acy + my2 * acz,
61 m.getElement(3, 2) + mz0 * acx + mz1 * acy + mz2 * acz
62 );
63 _halfExtents = Vector3(
64 std::abs(mx0) * arx + std::abs(mx1) * ary + std::abs(mx2) * arz,
65 std::abs(my0) * arx + std::abs(my1) * ary + std::abs(my2) * arz,
66 std::abs(mz0) * arx + std::abs(mz1) * ary + std::abs(mz2) * arz
67 );
68 }
69
74 void BoundingBox::add(const BoundingBox& other)
75 {
76 // Cache scalar values for better performance (matching JS pattern)
77 const float tcx = _center.getX();
78 const float tcy = _center.getY();
79 const float tcz = _center.getZ();
80 const float thx = _halfExtents.getX();
81 const float thy = _halfExtents.getY();
82 const float thz = _halfExtents.getZ();
83 float tminx = tcx - thx;
84 float tmaxx = tcx + thx;
85 float tminy = tcy - thy;
86 float tmaxy = tcy + thy;
87 float tminz = tcz - thz;
88 float tmaxz = tcz + thz;
89
90 const float ocx = other.center().getX();
91 const float ocy = other.center().getY();
92 const float ocz = other.center().getZ();
93 const float ohx = other.halfExtents().getX();
94 const float ohy = other.halfExtents().getY();
95 const float ohz = other.halfExtents().getZ();
96 const float ominx = ocx - ohx;
97 const float omaxx = ocx + ohx;
98 const float ominy = ocy - ohy;
99 const float omaxy = ocy + ohy;
100 const float ominz = ocz - ohz;
101 const float omaxz = ocz + ohz;
102
103 if (ominx < tminx) tminx = ominx;
104 if (omaxx > tmaxx) tmaxx = omaxx;
105 if (ominy < tminy) tminy = ominy;
106 if (omaxy > tmaxy) tmaxy = omaxy;
107 if (ominz < tminz) tminz = ominz;
108 if (omaxz > tmaxz) tmaxz = omaxz;
109
110 _center = Vector3((tminx + tmaxx) * 0.5f, (tminy + tmaxy) * 0.5f, (tminz + tmaxz) * 0.5f);
111 _halfExtents = Vector3((tmaxx - tminx) * 0.5f, (tmaxy - tminy) * 0.5f, (tmaxz - tminz) * 0.5f);
112 }
113}
void add(const BoundingBox &other)
void setFromTransformedAabb(const BoundingBox &aabb, const Matrix4 &m, bool ignoreScale=false)
const Vector3 & center() const
Definition boundingBox.h:27
const Vector3 & halfExtents() const
Definition boundingBox.h:35
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31
float getElement(const int col, int row) const
Definition matrix4.h:355
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29