More re-implementations

This commit is contained in:
Phillip Stephens 2015-08-24 23:56:05 -07:00
parent 5270d232ab
commit d5c8fe162f
4 changed files with 84 additions and 47 deletions

View File

@ -1,3 +1,6 @@
cmake_minimum_required(VERSION 3.0)
project(zeus)
if (NOT DEFINED ATHENA_INCLUDE_DIR)
set(ATHENA_INCLUDE_DIR ../Athena/include)
endif()
@ -42,3 +45,5 @@ add_library(Math
include/CAABox.hpp
include/COBBox.hpp)
add_subdirectory(test)

View File

@ -5,23 +5,27 @@
#include "CTransform.hpp"
#include <Athena/IStreamReader.hpp>
inline float fsel(float a, float b, float c)
{
return (a >= -0.0f) ? b : c;
}
class ZE_ALIGN(16) CAABox
{
public:
ZE_DECLARE_ALIGNED_ALLOCATOR();
enum EBoxEdgeID
{
};
enum EBoxFaceID
{
};
static const CAABox skInvertedBox;
CVector3f m_min;
CVector3f m_max;
// set default AABox to insane inverse min/max to allow for accumulation
inline CAABox()
: m_min(10000000000000000.f), m_max(-10000000000000000.f)
: m_min(1e16f), m_max(-1e16f)
{}
CAABox(const CVector3f& min, const CVector3f& max)
@ -30,13 +34,15 @@ public:
{
}
CAABox(float minX,const float minY,const float& minZ,
float maxX,const float& maxY,const float& maxZ)
CAABox(float minX, float minY, float minZ,
float maxX, float maxY, float maxZ)
: m_min(minX, minY, minZ),
m_max(maxX, maxY, maxZ)
{
}
CAABox(Athena::io::IStreamReader& in) {readBoundingBox(in);}
inline void readBoundingBox(Athena::io::IStreamReader& in)
{
m_min[0] = in.readFloat();
@ -46,7 +52,6 @@ public:
m_max[1] = in.readFloat();
m_max[2] = in.readFloat();
}
CAABox(Athena::io::IStreamReader& in) {readBoundingBox(in);}
inline bool intersects(const CAABox& other) const
{
@ -68,6 +73,20 @@ public:
return (m_max - m_min) * 0.5f;
}
inline CAABox getTransformedAABox(const CTransform& xfrm)
{
CAABox box;
for (int i = 0; i < 8; i+=2)
{
CVector3f point = xfrm * getPoint(i);
box.accumulateBounds(point);
point = xfrm * getPoint(i+1);
box.accumulateBounds(point);
}
return box;
}
inline void accumulateBounds(const CVector3f& point)
{
if (m_min.x > point.x)
@ -83,15 +102,6 @@ public:
m_max.y = point.y;
if (m_max.z < point.z)
m_max.z = point.z;
/*
for (unsigned i = 0; i < 3; i++)
{
if (m_min[i] > point[i])
m_min[i] = point[i];
if (m_max[i] < point[i])
m_max[i] = point[i];
}*/
}
inline bool pointInside(const CVector3f& other) const
@ -103,14 +113,43 @@ public:
inline CVector3f closestPointAlongVector(const CVector3f& other)
{
return {fsel(other.x, m_min.x, m_max.x),
return {(other.x >= -0.f) ? m_min.x : m_max.x,
(other.y >= -0.f) ? m_min.y : m_max.y,
(other.z >= -0.f) ? m_min.z : m_max.z};
}
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;
if (ret.x < m_min.x)
ret.x = m_min.x;
if (ret.y < m_min.y)
ret.y = m_min.y;
if (ret.z < m_min.z)
ret.z = m_min.z;
if (ret.x > m_max.x)
ret.x = m_max.x;
if (ret.y > m_max.y)
ret.y = m_max.y;
if (ret.z > m_max.z)
ret.z = m_max.z;
return ret;
}
inline void splitX(CAABox& posX, CAABox& negX) const
{
float midX = (m_max.x - m_min.x) / 2.0 + m_min.x;
float midX = (m_max.x - m_min.x) * .5 + m_min.x;
posX.m_max = m_max;
posX.m_min = m_min;
posX.m_min.x = midX;
@ -121,7 +160,7 @@ public:
inline void splitY(CAABox& posY, CAABox& negY) const
{
float midY = (m_max.y - m_min.y) / 2.0 + m_min.y;
float midY = (m_max.y - m_min.y) * .5 + m_min.y;
posY.m_max = m_max;
posY.m_min = m_min;
posY.m_min.y = midY;
@ -132,7 +171,7 @@ public:
inline void splitZ(CAABox& posZ, CAABox& negZ) const
{
float midZ = (m_max.z - m_min.z) / 2.0 + m_min.z;
float midZ = (m_max.z - m_min.z) * .5 + m_min.z;
posZ.m_max = m_max;
posZ.m_min = m_min;
posZ.m_min.z = midZ;
@ -140,6 +179,11 @@ public:
negZ.m_max.z = midZ;
negZ.m_min = m_min;
}
inline bool invalid()
{
return (m_max.x < m_min.x || m_max.y < m_min.y || m_max.z < m_min.z);
}
};
inline bool operator ==(const CAABox& left, const CAABox& right)
@ -152,10 +196,4 @@ inline bool operator !=(const CAABox& left, const CAABox& right)
}
inline CAABox operator*(const CTransform& left, const CAABox& right)
{
return {left * (right.m_min - right.m_max), left * (right.m_max - right.m_min)};
}
#endif // CAABOX_HPP

View File

@ -5,9 +5,10 @@
#include "CVector3f.hpp"
#include "CAABox.hpp"
class COBBox
class ZE_ALIGN(16) COBBox
{
public:
ZE_DECLARE_ALIGNED_ALLOCATOR();
CTransform m_transform;
CVector3f m_extents;
@ -15,7 +16,10 @@ public:
{}
COBBox(const CAABox& aabb)
{ createFromAABox(aabb); }
: m_extents(aabb.extents())
{
m_transform.m_origin = aabb.center();
}
CAABox calculateAABox(const CTransform& transform = CTransform())
{
@ -33,27 +37,16 @@ public:
{-1.0, 1.0, 1.0}
};
for (int i = 0; i < 8; i++)
for (int i = 0; i < 8; i+=2)
{
CVector3f p = (m_extents * basis[i]);
ret.accumulateBounds(trans * p);
p = m_extents * basis[i + 1];
ret.accumulateBounds(trans * p);
}
return ret;
}
void createFromAABox(const CAABox& box)
{
m_extents = box.extents();
m_transform.m_origin = box.center();
}
static inline COBBox fromAABox(const CAABox& box)
{
COBBox ret;
ret.createFromAABox(box);
return box;
}
};
#endif

View File

@ -29,9 +29,10 @@
inline void* operator new[](size_t sizeInBytes) { return zeAlloc(sizeInBytes,16); } \
inline void operator delete[](void* ptr) { zeFree(ptr); } \
inline void* operator new[](size_t, void* ptr) { return ptr; } \
inline void operator delete[](void*, void*) { }
inline void operator delete[](void*, void*) { } \
void __unused__()
#else
# define ZE_DECLARE_ALIGNED_ALLOCATOR()
# define ZE_DECLARE_ALIGNED_ALLOCATOR() void __unused__()
#endif
#if __SSE__