Various fixes and implementations

This commit is contained in:
Phillip Stephens 2016-04-26 03:36:44 -07:00
parent 7af2c491da
commit ac98ebf428
11 changed files with 130 additions and 97 deletions

View File

@ -47,7 +47,7 @@ add_library(zeus
include/zeus/CFrustum.hpp include/zeus/CFrustum.hpp
include/zeus/CAABox.hpp include/zeus/CAABox.hpp
include/zeus/COBBox.hpp include/zeus/COBBox.hpp
include/zeus/CLine.hpp include/zeus/CLineSeg.hpp
include/zeus/CSphere.hpp include/zeus/CSphere.hpp
include/zeus/CUnitVector.hpp include/zeus/CUnitVector.hpp
include/zeus/CMRay.hpp) include/zeus/CMRay.hpp)

View File

@ -2,11 +2,10 @@
#define CAABOX_HPP #define CAABOX_HPP
#include "zeus/CVector3f.hpp" #include "zeus/CVector3f.hpp"
#include "CUnitVector.hpp"
#include "zeus/CTransform.hpp" #include "zeus/CTransform.hpp"
#include "zeus/CPlane.hpp" #include "zeus/CPlane.hpp"
#include "CLine.hpp" #include "zeus/CLineSeg.hpp"
#include "CSphere.hpp" #include "zeus/CSphere.hpp"
#include "zeus/Math.hpp" #include "zeus/Math.hpp"
#if ZE_ATHENA_TYPES #if ZE_ATHENA_TYPES
#include <athena/IStreamReader.hpp> #include <athena/IStreamReader.hpp>
@ -155,49 +154,49 @@ public:
CVector3f volume() const {return (m_max - m_min) * 0.5f;} CVector3f volume() const {return (m_max - m_min) * 0.5f;}
inline CLine getEdge(EBoxEdgeId id) inline CLineSeg getEdge(EBoxEdgeId id)
{ {
switch (id) switch (id)
{ {
case EBoxEdgeId::UnknownEdge0: case EBoxEdgeId::UnknownEdge0:
return CLine({m_min.x, m_min.y, m_min.z}, return CLineSeg({m_min.x, m_min.y, m_min.z},
CUnitVector3f({m_min.x, m_min.y, m_max.z})); {m_min.x, m_min.y, m_max.z});
case EBoxEdgeId::UnknownEdge1: case EBoxEdgeId::UnknownEdge1:
return CLine({m_max.x, m_min.y, m_min.z}, return CLineSeg({m_max.x, m_min.y, m_min.z},
CUnitVector3f({m_min.x, m_min.y, m_min.z})); {m_min.x, m_min.y, m_min.z});
case EBoxEdgeId::UnknownEdge2: case EBoxEdgeId::UnknownEdge2:
return CLine({m_max.x, m_min.y, m_max.z}, return CLineSeg({m_max.x, m_min.y, m_max.z},
CUnitVector3f({m_max.x, m_min.y, m_max.z})); {m_max.x, m_min.y, m_max.z});
case EBoxEdgeId::UnknownEdge3: case EBoxEdgeId::UnknownEdge3:
return CLine({m_min.x, m_min.y, m_max.z}, return CLineSeg({m_min.x, m_min.y, m_max.z},
CUnitVector3f({m_max.x, m_min.y, m_max.z})); {m_max.x, m_min.y, m_max.z});
case EBoxEdgeId::UnknownEdge4: case EBoxEdgeId::UnknownEdge4:
return CLine({m_max.x, m_max.y, m_min.z}, return CLineSeg({m_max.x, m_max.y, m_min.z},
CUnitVector3f({m_max.x, m_max.y, m_max.z})); {m_max.x, m_max.y, m_max.z});
case EBoxEdgeId::UnknownEdge5: case EBoxEdgeId::UnknownEdge5:
return CLine({m_min.x, m_max.y, m_min.z}, return CLineSeg({m_min.x, m_max.y, m_min.z},
CUnitVector3f({m_max.x, m_max.y, m_min.z})); {m_max.x, m_max.y, m_min.z});
case EBoxEdgeId::UnknownEdge6: case EBoxEdgeId::UnknownEdge6:
return CLine({m_min.x, m_max.y, m_max.z}, return CLineSeg({m_min.x, m_max.y, m_max.z},
CUnitVector3f({m_min.x, m_max.y, m_min.z})); {m_min.x, m_max.y, m_min.z});
case EBoxEdgeId::UnknownEdge7: case EBoxEdgeId::UnknownEdge7:
return CLine({m_max.x, m_max.y, m_max.z}, return CLineSeg({m_max.x, m_max.y, m_max.z},
CUnitVector3f({m_min.x, m_max.y, m_max.z})); {m_min.x, m_max.y, m_max.z});
case EBoxEdgeId::UnknownEdge8: case EBoxEdgeId::UnknownEdge8:
return CLine({m_min.x, m_max.y, m_max.z}, return CLineSeg({m_min.x, m_max.y, m_max.z},
CUnitVector3f({m_min.x, m_min.y, m_max.z})); {m_min.x, m_min.y, m_max.z});
case EBoxEdgeId::UnknownEdge9: case EBoxEdgeId::UnknownEdge9:
return CLine({m_min.x, m_max.y, m_min.z}, return CLineSeg({m_min.x, m_max.y, m_min.z},
CUnitVector3f({m_min.x, m_min.y, m_min.z})); {m_min.x, m_min.y, m_min.z});
case EBoxEdgeId::UnknownEdge10: case EBoxEdgeId::UnknownEdge10:
return CLine({m_max.x, m_max.y, m_min.z}, return CLineSeg({m_max.x, m_max.y, m_min.z},
CUnitVector3f({m_max.x, m_min.y, m_min.z})); {m_max.x, m_min.y, m_min.z});
case EBoxEdgeId::UnknownEdge11: case EBoxEdgeId::UnknownEdge11:
return CLine({m_max.x, m_max.y, m_max.z}, return CLineSeg({m_max.x, m_max.y, m_max.z},
CUnitVector3f({m_max.x, m_min.y, m_max.z})); {m_max.x, m_min.y, m_max.z});
default: default:
return CLine({m_min.x, m_min.y, m_min.z}, return CLineSeg({m_min.x, m_min.y, m_min.z},
CUnitVector3f({m_min.x, m_min.y, m_max.z})); {m_min.x, m_min.y, m_max.z});
} }
} }

View File

@ -1,31 +0,0 @@
#ifndef CLINE_HPP
#define CLINE_HPP
#include "Global.hpp"
#include "zeus/CVector3f.hpp"
#include "CUnitVector.hpp"
namespace zeus
{
class alignas(16) CLine
{
public:
CLine(const CVector3f& a, const CUnitVector3f& b)
{
CVector3f ab = (b - a).normalized();
start = a;
if (ab.x != 0.0f || ab.y != 0.0f || ab.z != 0.0f)
normal = ab;
end = b;
}
CVector3f start;
CVector3f normal;
CUnitVector3f end;
};
}
#endif

30
include/zeus/CLineSeg.hpp Normal file
View File

@ -0,0 +1,30 @@
#ifndef CLINE_HPP
#define CLINE_HPP
#include "Global.hpp"
#include "zeus/CVector3f.hpp"
namespace zeus
{
class CLineSeg
{
public:
CLineSeg(const CVector3f& start, const CVector3f& end)
: start(start),
end(end)
{
CVector3f tmp = (end - start).normalized();
if (tmp.x != 0 || tmp.y != 0 || tmp.z != 0)
normal = tmp.normalized();
else
normal = CVector3f::skZero;
}
CVector3f normal;
CVector3f start;
CVector3f end;
};
}
#endif

View File

@ -11,6 +11,21 @@ class alignas(16) COBBox
{ {
public: public:
ZE_DECLARE_ALIGNED_ALLOCATOR(); ZE_DECLARE_ALIGNED_ALLOCATOR();
#if ZE_ATHENA_TYPES
void readBig(athena::io::IStreamReader& in)
{
m_transform.read34RowMajor(in);
m_extents.readBig(in);
}
static COBBox ReadBig(athena::io::IStreamReader& in)
{
COBBox out;
out.readBig(in);
return out;
}
#endif
CTransform m_transform; CTransform m_transform;
CVector3f m_extents; CVector3f m_extents;
@ -23,6 +38,11 @@ public:
m_transform.m_origin = aabb.center(); m_transform.m_origin = aabb.center();
} }
COBBox(const CTransform& xf, const CVector3f& point)
{
}
CAABox calculateAABox(const CTransform& transform = CTransform()) CAABox calculateAABox(const CTransform& transform = CTransform())
{ {
CAABox ret = CAABox::skInvertedBox; CAABox ret = CAABox::skInvertedBox;
@ -58,6 +78,13 @@ public:
return ret; return ret;
} }
static COBBox FromAABox(const CAABox& box, const CTransform& xf)
{
const CVector3f extents = xf.toMatrix4f().transposed().vec[1].toVec3f() - box.center();
const CTransform newXf = CTransform::Translate(box.center()) * xf;
return COBBox(newXf, extents);
}
}; };
} }

View File

@ -329,20 +329,21 @@ public:
} }
static CVector2f slerp(const CVector2f& a, const CVector2f& b, float t); static CVector2f slerp(const CVector2f& a, const CVector2f& b, float t);
inline bool canBeNormalized() const
{
const float epsilon = 1.1920929e-7f;
if (std::fabs(x) >= epsilon || std::fabs(y) >= epsilon)
return true;
return false;
}
inline bool isNormalized() const inline bool isNormalized() const
{ return !canBeNormalized(); } { return std::fabs(1.f - magSquared()) < 0.01f; }
inline bool canBeNormalized() const
{ return !isNormalized(); }
inline bool isZero() const inline bool isZero() const
{ return magSquared() <= 1.1920929e-7f; } { return magSquared() <= 1.1920929e-7f; }
inline bool isEqu(const CVector2f& other, float epsilon=1.1920929e-7f)
{
const CVector2f diffVec = other - *this;
return (diffVec.x <= epsilon && diffVec.y <= epsilon);
}
inline float& operator[](size_t idx) {return (&x)[idx];} inline float& operator[](size_t idx) {return (&x)[idx];}
inline const float& operator[](size_t idx) const {return (&x)[idx];} inline const float& operator[](size_t idx) const {return (&x)[idx];}

View File

@ -269,6 +269,7 @@ public:
return (x * rhs.x) + (y * rhs.y) + (z * rhs.z); return (x * rhs.x) + (y * rhs.y) + (z * rhs.z);
#endif #endif
} }
inline float magSquared() const inline float magSquared() const
{ {
#if __SSE__ #if __SSE__
@ -287,6 +288,7 @@ public:
return x*x + y*y + z*z; return x*x + y*y + z*z;
#endif #endif
} }
inline float magnitude() const inline float magnitude() const
{ return sqrtF(magSquared()); } { return sqrtF(magSquared()); }
@ -316,18 +318,12 @@ public:
static inline CVector3f nlerp(const CVector3f& a, const CVector3f& b, float t) static inline CVector3f nlerp(const CVector3f& a, const CVector3f& b, float t)
{ return lerp(a, b, t).normalized(); } { return lerp(a, b, t).normalized(); }
static CVector3f slerp(const CVector3f& a, const CVector3f& b, float t); static CVector3f slerp(const CVector3f& a, const CVector3f& b, float t);
//static CVector3f slerp(const CVector3f& a, const CVector3f& b, const CRelAngle& angle);
inline bool canBeNormalized() const
{
const float epsilon = 1.1920929e-7f;
if (std::fabs(x) >= epsilon || std::fabs(y) >= epsilon || std::fabs(z) >= epsilon)
return true;
return false;
}
inline bool isNormalized() const inline bool isNormalized() const
{ return !canBeNormalized(); } { return std::fabs(1.f - magSquared()) < 0.01f; }
inline bool canBeNormalized() const
{ return !isNormalized(); }
inline bool isZero() const inline bool isZero() const
{ return magSquared() <= 1.1920929e-7f; } { return magSquared() <= 1.1920929e-7f; }
@ -355,7 +351,7 @@ public:
inline bool isEqu(const CVector3f& other, float epsilon=1.1920929e-7f) inline bool isEqu(const CVector3f& other, float epsilon=1.1920929e-7f)
{ {
CVector3f diffVec = other - *this; const CVector3f diffVec = other - *this;
return (diffVec.x <= epsilon && diffVec.y <= epsilon && diffVec.z <= epsilon); return (diffVec.x <= epsilon && diffVec.y <= epsilon && diffVec.z <= epsilon);
} }
@ -409,6 +405,11 @@ static inline CVector3f operator/(float lhs, const CVector3f& rhs)
} }
extern const CVector3f kUpVec; extern const CVector3f kUpVec;
extern const CVector3f kDownVec;
extern const CVector3f kRightVec;
extern const CVector3f kLeftVec;
extern const CVector3f kForwardVec;
extern const CVector3f kBackVec;
extern const CVector3f kRadToDegVec; extern const CVector3f kRadToDegVec;
extern const CVector3f kDegToRadVec; extern const CVector3f kDegToRadVec;
inline CVector3f radToDeg(const CVector3f& rad) {return rad * kRadToDegVec;} inline CVector3f radToDeg(const CVector3f& rad) {return rad * kRadToDegVec;}

View File

@ -357,16 +357,17 @@ public:
return lerp(a, b, t).normalized(); return lerp(a, b, t).normalized();
} }
inline bool canBeNormalized() const
{
const float epsilon = 1.1920929e-7f;
if (std::fabs(x) >= epsilon || std::fabs(y) >= epsilon || std::fabs(z) >= epsilon || std::fabs(w) >= epsilon)
return true;
return false;
}
inline bool isNormalized() const inline bool isNormalized() const
{ return !canBeNormalized(); } { return std::fabs(1.f - magSquared()) < 0.01f; }
inline bool canBeNormalized() const
{ return !isNormalized(); }
inline bool isEqu(const CVector4f& other, float epsilon=1.1920929e-7f)
{
const CVector4f diffVec = other - *this;
return (diffVec.x <= epsilon && diffVec.y <= epsilon && diffVec.z <= epsilon && diffVec.w <= epsilon);
}
inline float& operator[](size_t idx) {return (&x)[idx];} inline float& operator[](size_t idx) {return (&x)[idx];}
inline const float& operator[](size_t idx) const {return (&x)[idx];} inline const float& operator[](size_t idx) const {return (&x)[idx];}

View File

@ -15,7 +15,7 @@
#include "zeus/CUnitVector.hpp" #include "zeus/CUnitVector.hpp"
#include "zeus/CRectangle.hpp" #include "zeus/CRectangle.hpp"
#include "zeus/CPlane.hpp" #include "zeus/CPlane.hpp"
#include "zeus/CLine.hpp" #include "zeus/CLineSeg.hpp"
#include "zeus/CAABox.hpp" #include "zeus/CAABox.hpp"
#include "zeus/COBBox.hpp" #include "zeus/COBBox.hpp"
#include "zeus/CSphere.hpp" #include "zeus/CSphere.hpp"

View File

@ -6,11 +6,16 @@
namespace zeus namespace zeus
{ {
const CVector3f CVector3f::skOne = CVector3f(1.0); const CVector3f CVector3f::skOne = CVector3f(1.f);
const CVector3f CVector3f::skNegOne = CVector3f(-1.0); const CVector3f CVector3f::skNegOne = CVector3f(-1.f);
const CVector3f CVector3f::skZero; const CVector3f CVector3f::skZero;
const CVector3f kUpVec(0.0, 0.0, 1.0); const CVector3f kUpVec(0.f, 0.f, 1.f);
const CVector3f kDownVec(0.f, 0.f, -1.f);
const CVector3f kForwardVec(0.f, 1.f, 0.f);
const CVector3f kBackVec(0.f, -1.f, 0.f);
const CVector3f kRightVec(1.f, 0.f, 0.f);
const CVector3f kLeftVector(-1.f, 0.f, 0.f);
const CVector3f kRadToDegVec(180.0f / M_PIF); const CVector3f kRadToDegVec(180.0f / M_PIF);
const CVector3f kDegToRadVec(M_PIF / 180.0f); const CVector3f kDegToRadVec(M_PIF / 180.0f);

View File

@ -16,7 +16,7 @@ int main()
zeus::detectCPU(); zeus::detectCPU();
assert(!CAABox({100, 100, 100}, {100, 100, 100}).invalid()); assert(!CAABox({100, 100, 100}, {100, 100, 100}).invalid());
assert(CAABox().invalid()); assert(CAABox().invalid());
CVector3f vec{320, 632162.f, 800.f}; CVector3f vec{320.f, 1.f, 0.66568f};
assert(vec.canBeNormalized()); assert(vec.canBeNormalized());
assert(!vec.isZero()); assert(!vec.isZero());
assert(CVector3f().isZero()); assert(CVector3f().isZero());
@ -55,7 +55,7 @@ int main()
std::cout << " Test 1 " << ( aabb.intersects(s1) ? "succeeded" : "failed" ) << std::endl; std::cout << " Test 1 " << ( aabb.intersects(s1) ? "succeeded" : "failed" ) << std::endl;
std::cout << " Test 2 " << ( aabb.intersects(s2) ? "succeeded" : "failed" ) << std::endl; std::cout << " Test 2 " << ( aabb.intersects(s2) ? "succeeded" : "failed" ) << std::endl;
std::cout << " Test 3 " << ( aabb.intersects(s3) ? "succeeded" : "failed" ) << std::endl; std::cout << " Test 3 " << ( aabb.intersects(s3) ? "succeeded" : "failed" ) << std::endl;
CLine line({-89.120926f, 59.328712f, 3.265882f}, CUnitVector3f({-90.120926f, 59.328712f, 3.265882f})); CLineSeg line({-89.120926f, 59.328712f, 3.265882f}, {-90.120926f, 59.328712f, 3.265882f});
CColor ctest1; CColor ctest1;
ctest1.fromHSV(0, 255/255.f, .5); ctest1.fromHSV(0, 255/255.f, .5);