| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Collision/Common.h>
- namespace anki {
- /// @addtogroup collision
- /// @{
- /// Axis align bounding box collision shape.
- class Aabb
- {
- public:
- static constexpr CollisionShapeType kClassType = CollisionShapeType::kAabb;
- /// Will not initialize any memory, nothing.
- Aabb()
- {
- }
- Aabb(const Vec4& min, const Vec4& max)
- : m_min(min)
- , m_max(max)
- {
- check();
- }
- Aabb(const Vec3& min, const Vec3& max)
- : Aabb(Vec4(min, 0.0f), Vec4(max, 0.0f))
- {
- check();
- }
- /// Set from point cloud.
- Aabb(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
- {
- setFromPointCloud(pointBuffer, pointCount, pointStride, buffSize);
- }
- /// Copy.
- Aabb(const Aabb& b)
- {
- operator=(b);
- }
- /// Copy.
- Aabb& operator=(const Aabb& b)
- {
- b.check();
- m_min = b.m_min;
- m_max = b.m_max;
- return *this;
- }
- const Vec4& getMin() const
- {
- check();
- return m_min;
- }
- void setMin(const Vec4& x)
- {
- ANKI_ASSERT(x.w == 0.0f);
- m_min = x;
- }
- void setMin(const Vec3& x)
- {
- setMin(Vec4(x, 0.0f));
- }
- const Vec4& getMax() const
- {
- check();
- return m_max;
- }
- void setMax(const Vec4& x)
- {
- ANKI_ASSERT(x.w == 0.0f);
- m_max = x;
- }
- void setMax(const Vec3& x)
- {
- setMax(Vec4(x, 0.0f));
- }
- /// Compute the GJK support.
- [[nodiscard]] Vec4 computeSupport(const Vec4& dir) const;
- /// It uses a nice trick to avoid unwanted calculations
- Aabb getTransformed(const Transform& transform) const;
- /// Get a collision shape that includes this and the given. Its not very accurate
- Aabb getCompoundShape(const Aabb& b) const;
- /// Calculate from a set of points
- void setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize);
- private:
- Vec4 m_min
- #if ANKI_ASSERTIONS_ENABLED
- = Vec4(kMaxF32)
- #endif
- ;
- Vec4 m_max
- #if ANKI_ASSERTIONS_ENABLED
- = Vec4(kMinF32)
- #endif
- ;
- void check() const
- {
- ANKI_ASSERT(m_min.xyz < m_max.xyz);
- ANKI_ASSERT(m_min.w == 0.0f);
- ANKI_ASSERT(m_max.w == 0.0f);
- }
- };
- /// @}
- } // end namespace anki
|