diff --git a/include/aabb.h b/include/aabb.h index 91e3ae2..e693a40 100644 --- a/include/aabb.h +++ b/include/aabb.h @@ -53,6 +53,7 @@ public: void init(); static AABB Create(const Vector3& minPoint, const Vector3& maxPoint); static AABB Create(const Vector3& corner1, const Vector3& corner2, const Matrix4& modelMatrix); + static AABB Create(const AABB& modelSpaceExtents, const Matrix4& modelMatrix); static AABB Create(); void scale(const Vector3& s); @@ -69,6 +70,7 @@ public: bool intersectsRay(const Vector3& v1, const Vector3& dir) const; bool intersectsSphere(const Vector3& center, float radius) const; void encapsulate(const AABB& b); + void encapsulate(const Vector3& v); bool operator ==(const AABB& b) const; bool operator !=(const AABB& b) const; diff --git a/src/aabb.cpp b/src/aabb.cpp index 72812ed..7bb4101 100644 --- a/src/aabb.cpp +++ b/src/aabb.cpp @@ -91,6 +91,13 @@ AABB AABB::Create(const Vector3& corner1, const Vector3& corner2, const Matrix4& return r; } +AABB AABB::Create(const AABB& modelSpaceExtents, const Matrix4& modelMatrix) +{ + AABB r; + r.init(modelSpaceExtents.min, modelSpaceExtents.max, modelMatrix); + return r; +} + bool AABB::operator ==(const AABB& b) const { return min == b.min && max == b.max; @@ -366,6 +373,17 @@ void AABB::encapsulate(const AABB& b) if (b.max.z > max.z) max.z = b.max.z; } +void AABB::encapsulate(const Vector3& v) +{ + if (v.x < min.x) min.x = v.x; + if (v.y < min.y) min.y = v.y; + if (v.z < min.z) min.z = v.z; + + if (v.x > max.x) max.x = v.x; + if (v.y > max.y) max.y = v.y; + if (v.z > max.z) max.z = v.z; +} + Vector3 AABB::nearestPoint(const Vector3& v) const { return Vector3::Create(KRCLAMP(v.x, min.x, max.x), KRCLAMP(v.y, min.y, max.y), KRCLAMP(v.z, min.z, max.z));