2015-08-12 01:41:28 +00:00
|
|
|
#ifndef CAABOX_HPP
|
|
|
|
#define CAABOX_HPP
|
|
|
|
|
|
|
|
#include "CVector3f.hpp"
|
2015-10-25 19:31:41 +00:00
|
|
|
#include "CUnitVector.hpp"
|
2015-08-12 01:41:28 +00:00
|
|
|
#include "CTransform.hpp"
|
2015-08-29 04:49:19 +00:00
|
|
|
#include "CPlane.hpp"
|
2015-10-25 19:31:41 +00:00
|
|
|
#include "CLine.hpp"
|
2015-11-02 18:44:46 +00:00
|
|
|
#include "CSphere.hpp"
|
2015-08-29 04:49:19 +00:00
|
|
|
#include "Math.hpp"
|
2015-11-01 12:04:29 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
2015-08-12 01:41:28 +00:00
|
|
|
#include <Athena/IStreamReader.hpp>
|
2015-11-01 12:04:29 +00:00
|
|
|
#endif
|
2015-08-12 01:41:28 +00:00
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
namespace Zeus
|
|
|
|
{
|
2015-10-25 19:31:41 +00:00
|
|
|
class alignas(16) CAABox
|
2015-08-12 01:41:28 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ZE_DECLARE_ALIGNED_ALLOCATOR();
|
|
|
|
|
2015-10-25 19:31:41 +00:00
|
|
|
enum EBoxEdgeId
|
2015-08-25 06:56:05 +00:00
|
|
|
{
|
2015-10-25 19:31:41 +00:00
|
|
|
UnknownEdge0,
|
|
|
|
UnknownEdge1,
|
|
|
|
UnknownEdge2,
|
|
|
|
UnknownEdge3,
|
|
|
|
UnknownEdge4,
|
|
|
|
UnknownEdge5,
|
|
|
|
UnknownEdge6,
|
|
|
|
UnknownEdge7,
|
|
|
|
UnknownEdge8,
|
|
|
|
UnknownEdge9,
|
|
|
|
UnknownEdge10,
|
|
|
|
UnknownEdge11
|
2015-08-25 06:56:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum EBoxFaceID
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2015-08-12 01:41:28 +00:00
|
|
|
static const CAABox skInvertedBox;
|
|
|
|
|
|
|
|
CVector3f m_min;
|
|
|
|
CVector3f m_max;
|
2015-08-25 06:56:05 +00:00
|
|
|
|
|
|
|
// set default AABox to insane inverse min/max to allow for accumulation
|
2015-08-12 01:41:28 +00:00
|
|
|
inline CAABox()
|
2015-08-25 06:56:05 +00:00
|
|
|
: m_min(1e16f), m_max(-1e16f)
|
2015-08-12 01:41:28 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
CAABox(const CVector3f& min, const CVector3f& max)
|
|
|
|
: m_min(min),
|
|
|
|
m_max(max)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:56:05 +00:00
|
|
|
CAABox(float minX, float minY, float minZ,
|
|
|
|
float maxX, float maxY, float maxZ)
|
2015-08-12 01:41:28 +00:00
|
|
|
: m_min(minX, minY, minZ),
|
|
|
|
m_max(maxX, maxY, maxZ)
|
|
|
|
{
|
|
|
|
}
|
2015-11-01 12:04:29 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
2015-08-25 06:56:05 +00:00
|
|
|
CAABox(Athena::io::IStreamReader& in) {readBoundingBox(in);}
|
2015-08-12 01:41:28 +00:00
|
|
|
|
|
|
|
inline void readBoundingBox(Athena::io::IStreamReader& in)
|
|
|
|
{
|
2015-11-02 18:44:46 +00:00
|
|
|
m_min = CVector3f(in);
|
|
|
|
m_max = CVector3f(in);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
float distanceFromPointSquared(const CVector3f& other) const
|
|
|
|
{
|
|
|
|
float dist = 0;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
if (other[i] < m_min[i])
|
|
|
|
{
|
|
|
|
const float tmp = (m_min[i] - other[i]);
|
|
|
|
dist += tmp * tmp;
|
|
|
|
}
|
|
|
|
else if (other[i] > m_max[i])
|
|
|
|
{
|
|
|
|
const float tmp = (other[i] - m_max[i]);
|
|
|
|
dist += tmp * tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
float distanceFromPoint(const CVector3f &other) const
|
|
|
|
{
|
|
|
|
return Math::sqrtF(distanceFromPointSquared(other));
|
2015-08-12 01:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool intersects(const CAABox& other) const
|
|
|
|
{
|
2015-08-29 04:49:19 +00:00
|
|
|
bool x1 = (m_max[0] < other.m_min[0]);
|
|
|
|
bool x2 = (m_min[0] > other.m_max[0]);
|
|
|
|
bool y1 = (m_max[1] < other.m_min[1]);
|
|
|
|
bool y2 = (m_min[1] > other.m_max[1]);
|
|
|
|
bool z1 = (m_max[2] < other.m_min[2]);
|
|
|
|
bool z2 = (m_min[2] > other.m_max[2]);
|
|
|
|
return x1 && x2 && y1 && y2 && z1 && z2;
|
|
|
|
}
|
2015-11-02 18:44:46 +00:00
|
|
|
bool intersects(const CSphere& other) const
|
|
|
|
{
|
|
|
|
return distanceFromPointSquared(other.position) <= other.radius * other.radius;
|
|
|
|
}
|
2015-08-29 04:49:19 +00:00
|
|
|
|
|
|
|
inline bool inside(const CAABox& other) const
|
|
|
|
{
|
|
|
|
bool x = m_min[0] >= other.m_min[0] && m_max[0] <= other.m_max[0];
|
|
|
|
bool y = m_min[1] >= other.m_min[1] && m_max[1] <= other.m_max[1];
|
|
|
|
bool z = m_min[2] >= other.m_min[2] && m_max[2] <= other.m_max[2];
|
|
|
|
return x && y && z;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool insidePlane(const CPlane& plane) const
|
|
|
|
{
|
|
|
|
CVector3f vmin, vmax;
|
|
|
|
/* X axis */
|
|
|
|
if (plane.a >= 0) {
|
|
|
|
vmin[0] = m_min[0];
|
|
|
|
vmax[0] = m_max[0];
|
|
|
|
} else {
|
|
|
|
vmin[0] = m_max[0];
|
|
|
|
vmax[0] = m_min[0];
|
|
|
|
}
|
|
|
|
/* Y axis */
|
|
|
|
if (plane.b >= 0) {
|
|
|
|
vmin[1] = m_min[1];
|
|
|
|
vmax[1] = m_max[1];
|
|
|
|
} else {
|
|
|
|
vmin[1] = m_max[1];
|
|
|
|
vmax[1] = m_min[1];
|
|
|
|
}
|
|
|
|
/* Z axis */
|
|
|
|
if (plane.c >= 0) {
|
|
|
|
vmin[2] = m_min[2];
|
|
|
|
vmax[2] = m_max[2];
|
|
|
|
} else {
|
|
|
|
vmin[2] = m_max[2];
|
|
|
|
vmax[2] = m_min[2];
|
|
|
|
}
|
|
|
|
float dadot = plane.vec.dot(vmax);
|
|
|
|
if (dadot + plane.d < 0)
|
|
|
|
return false;
|
2015-08-12 01:41:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
CVector3f center() const {return (m_min + m_max) * 0.5f;}
|
|
|
|
|
|
|
|
CVector3f volume() const {return (m_max - m_min) * 0.5f;}
|
2015-08-12 01:41:28 +00:00
|
|
|
|
2015-10-25 19:31:41 +00:00
|
|
|
inline CLine getEdge(EBoxEdgeId id)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case UnknownEdge0:
|
|
|
|
return CLine({m_min.x, m_min.y, m_min.z},
|
|
|
|
CUnitVector3f({m_min.x, m_min.y, m_max.z}));
|
|
|
|
case UnknownEdge1:
|
|
|
|
return CLine({m_max.x, m_min.y, m_min.z},
|
|
|
|
CUnitVector3f({m_min.x, m_min.y, m_min.z}));
|
|
|
|
case UnknownEdge2:
|
|
|
|
return CLine({m_max.x, m_min.y, m_max.z},
|
|
|
|
CUnitVector3f({m_max.x, m_min.y, m_max.z}));
|
|
|
|
case UnknownEdge3:
|
|
|
|
return CLine({m_min.x, m_min.y, m_max.z},
|
|
|
|
CUnitVector3f({m_max.x, m_min.y, m_max.z}));
|
|
|
|
case UnknownEdge4:
|
|
|
|
return CLine({m_max.x, m_max.y, m_min.z},
|
|
|
|
CUnitVector3f({m_max.x, m_max.y, m_max.z}));
|
|
|
|
case UnknownEdge5:
|
|
|
|
return CLine({m_min.x, m_max.y, m_min.z},
|
|
|
|
CUnitVector3f({m_max.x, m_max.y, m_min.z}));
|
|
|
|
case UnknownEdge6:
|
|
|
|
return CLine({m_min.x, m_max.y, m_max.z},
|
|
|
|
CUnitVector3f({m_min.x, m_max.y, m_min.z}));
|
|
|
|
case UnknownEdge7:
|
|
|
|
return CLine({m_max.x, m_max.y, m_max.z},
|
|
|
|
CUnitVector3f({m_min.x, m_max.y, m_max.z}));
|
|
|
|
case UnknownEdge8:
|
|
|
|
return CLine({m_min.x, m_max.y, m_max.z},
|
|
|
|
CUnitVector3f({m_min.x, m_min.y, m_max.z}));
|
|
|
|
case UnknownEdge9:
|
|
|
|
return CLine({m_min.x, m_max.y, m_min.z},
|
|
|
|
CUnitVector3f({m_min.x, m_min.y, m_min.z}));
|
|
|
|
case UnknownEdge10:
|
|
|
|
return CLine({m_max.x, m_max.y, m_min.z},
|
|
|
|
CUnitVector3f({m_max.x, m_min.y, m_min.z}));
|
|
|
|
case UnknownEdge11:
|
|
|
|
return CLine({m_max.x, m_max.y, m_max.z},
|
|
|
|
CUnitVector3f({m_max.x, m_min.y, m_max.z}));
|
|
|
|
default:
|
|
|
|
return CLine({m_min.x, m_min.y, m_min.z},
|
|
|
|
CUnitVector3f({m_min.x, m_min.y, m_max.z}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:56:05 +00:00
|
|
|
inline CAABox getTransformedAABox(const CTransform& xfrm)
|
|
|
|
{
|
|
|
|
CAABox box;
|
2015-10-08 00:21:38 +00:00
|
|
|
CVector3f point = xfrm * getPoint(0);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(1);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(2);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(3);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(4);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(5);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(6);
|
|
|
|
box.accumulateBounds(point);
|
|
|
|
point = xfrm * getPoint(7);
|
|
|
|
box.accumulateBounds(point);
|
2015-08-25 06:56:05 +00:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2015-08-12 01:41:28 +00:00
|
|
|
inline void accumulateBounds(const CVector3f& point)
|
|
|
|
{
|
|
|
|
if (m_min.x > point.x)
|
|
|
|
m_min.x = point.x;
|
|
|
|
if (m_min.y > point.y)
|
|
|
|
m_min.y = point.y;
|
|
|
|
if (m_min.z > point.z)
|
|
|
|
m_min.z = point.z;
|
|
|
|
if (m_max.x < point.x)
|
|
|
|
m_max.x = point.x;
|
|
|
|
if (m_max.y < point.y)
|
|
|
|
m_max.y = point.y;
|
|
|
|
if (m_max.z < point.z)
|
|
|
|
m_max.z = point.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool pointInside(const CVector3f& other) const
|
|
|
|
{
|
|
|
|
return (m_min.x <= other.x && other.x <= m_max.z &&
|
|
|
|
m_min.y <= other.y && other.y <= m_max.z &&
|
|
|
|
m_min.z <= other.z && other.z <= m_max.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CVector3f closestPointAlongVector(const CVector3f& other)
|
|
|
|
{
|
2015-08-29 04:49:19 +00:00
|
|
|
CVector3f center = this->center();
|
|
|
|
return {(other.x < center.x ? m_min.x : m_max.x),
|
|
|
|
(other.y < center.y ? m_min.y : m_max.y),
|
|
|
|
(other.z < center.z ? m_min.z : m_max.z)};
|
2015-08-12 01:41:28 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 22:04:15 +00:00
|
|
|
inline CVector3f furthestPointAlongVector(const CVector3f& other)
|
|
|
|
{
|
2015-08-29 04:49:19 +00:00
|
|
|
CVector3f center = this->center();
|
|
|
|
return {(other.x < center.x ? m_max.x : m_min.x),
|
|
|
|
(other.y < center.y ? m_max.y : m_min.y),
|
|
|
|
(other.z < center.z ? m_max.z : m_min.z)};
|
2015-08-25 22:04:15 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 06:56:05 +00:00
|
|
|
inline CVector3f getPoint(const int point)
|
|
|
|
{
|
|
|
|
int zOff = point & 4;
|
|
|
|
int yOff = (point * 2) & 4;
|
|
|
|
int xOff = (point * 4) & 4;
|
|
|
|
float z = ((float*)(&m_min.x) + zOff)[2];
|
|
|
|
float y = ((float*)(&m_min.x) + yOff)[1];
|
|
|
|
float x = ((float*)(&m_min.x) + xOff)[0];
|
|
|
|
return CVector3f(x, y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CVector3f clampToBox(const CVector3f& vec)
|
|
|
|
{
|
|
|
|
CVector3f ret = vec;
|
2015-10-08 00:21:38 +00:00
|
|
|
Math::clamp(m_min.x, ret.x, m_max.x);
|
|
|
|
Math::clamp(m_min.y, ret.y, m_max.y);
|
|
|
|
Math::clamp(m_min.z, ret.z, m_max.z);
|
2015-08-25 06:56:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-12 01:41:28 +00:00
|
|
|
inline void splitX(CAABox& posX, CAABox& negX) const
|
|
|
|
{
|
2015-08-25 06:56:05 +00:00
|
|
|
float midX = (m_max.x - m_min.x) * .5 + m_min.x;
|
2015-08-12 01:41:28 +00:00
|
|
|
posX.m_max = m_max;
|
|
|
|
posX.m_min = m_min;
|
|
|
|
posX.m_min.x = midX;
|
|
|
|
negX.m_max = m_max;
|
|
|
|
negX.m_max.x = midX;
|
|
|
|
negX.m_min = m_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void splitY(CAABox& posY, CAABox& negY) const
|
|
|
|
{
|
2015-08-25 06:56:05 +00:00
|
|
|
float midY = (m_max.y - m_min.y) * .5 + m_min.y;
|
2015-08-12 01:41:28 +00:00
|
|
|
posY.m_max = m_max;
|
|
|
|
posY.m_min = m_min;
|
|
|
|
posY.m_min.y = midY;
|
|
|
|
negY.m_max = m_max;
|
|
|
|
negY.m_max.y = midY;
|
|
|
|
negY.m_min = m_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void splitZ(CAABox& posZ, CAABox& negZ) const
|
|
|
|
{
|
2015-08-25 06:56:05 +00:00
|
|
|
float midZ = (m_max.z - m_min.z) * .5 + m_min.z;
|
2015-08-12 01:41:28 +00:00
|
|
|
posZ.m_max = m_max;
|
|
|
|
posZ.m_min = m_min;
|
|
|
|
posZ.m_min.z = midZ;
|
|
|
|
negZ.m_max = m_max;
|
|
|
|
negZ.m_max.z = midZ;
|
|
|
|
negZ.m_min = m_min;
|
|
|
|
}
|
2015-08-25 06:56:05 +00:00
|
|
|
|
2015-11-02 18:44:46 +00:00
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
inline bool invalid() {return (m_max.x < m_min.x || m_max.y < m_min.y || m_max.z < m_min.z);}
|
2015-08-12 01:41:28 +00:00
|
|
|
};
|
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
inline bool operator ==(const CAABox& left, const CAABox& right) {return (left.m_min == right.m_min && left.m_max == right.m_max);}
|
|
|
|
inline bool operator !=(const CAABox& left, const CAABox& right) {return (left.m_min != right.m_min || left.m_max != right.m_max);}
|
2015-08-12 01:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CAABOX_HPP
|