void OBB::SetFrom(const AABB &aabb) { pos = aabb.CenterPoint(); r = aabb.HalfSize(); axis[0] = float3(1,0,0); axis[1] = float3(0,1,0); axis[2] = float3(0,0,1); }
void OBBSetFrom(OBB &obb, const AABB &aabb, const Matrix &m) { obb.pos = m.MulPos(aabb.CenterPoint()); float3 size = aabb.HalfSize(); obb.axis[0] = m.MulDir(float3(size.x, 0, 0)); obb.axis[1] = m.MulDir(float3(0, size.y, 0)); obb.axis[2] = m.MulDir(float3(0, 0, size.z)); obb.r.x = obb.axis[0].Normalize(); obb.r.y = obb.axis[1].Normalize(); obb.r.z = obb.axis[2].Normalize(); }
void AABBTransformAsAABB(AABB &aabb, Matrix &m) { float3 newCenter = m.MulPos(aabb.CenterPoint()); float3 newDir; float3 h = aabb.HalfSize(); // The following is equal to taking the absolute value of the whole matrix m. newDir.x = ABSDOT3(m[0], h); newDir.y = ABSDOT3(m[1], h); newDir.z = ABSDOT3(m[2], h); aabb.minPoint = newCenter - newDir; aabb.maxPoint = newCenter + newDir; }
void OBBSetFrom(OBB &obb, const AABB &aabb, const Matrix &m) { assume(m.IsColOrthogonal()); // We cannot convert transform an AABB to OBB if it gets sheared in the process. assume(m.HasUniformScale()); // Nonuniform scale will produce shear as well. obb.pos = m.MulPos(aabb.CenterPoint()); obb.r = aabb.HalfSize(); obb.axis[0] = DIR_VEC(m.Col(0)); obb.axis[1] = DIR_VEC(m.Col(1)); obb.axis[2] = DIR_VEC(m.Col(2)); // If the matrix m contains scaling, propagate the scaling from the axis vectors to the half-length vectors, // since we want to keep the axis vectors always normalized in our representation. float matrixScale = obb.axis[0].LengthSq(); matrixScale = Sqrt(matrixScale); obb.r *= matrixScale; matrixScale = 1.f / matrixScale; obb.axis[0] *= matrixScale; obb.axis[1] *= matrixScale; obb.axis[2] *= matrixScale; // mathassert(vec::AreOrthogonal(obb.axis[0], obb.axis[1], obb.axis[2])); // mathassert(vec::AreOrthonormal(obb.axis[0], obb.axis[1], obb.axis[2])); ///@todo Would like to simply do the above, but instead numerical stability requires to do the following: vec::Orthonormalize(obb.axis[0], obb.axis[1], obb.axis[2]); }