zeus/src/CAABox.cpp

163 lines
4.7 KiB
C++
Raw Permalink Normal View History

2016-03-04 15:03:26 -08:00
#include "zeus/CAABox.hpp"
2016-04-16 14:49:09 -07:00
#include "zeus/CVector3f.hpp"
2018-12-07 21:23:50 -08:00
namespace zeus {
2019-01-19 22:41:51 -08:00
static const int ProjWindings[6][4] = {
{2, 0, 4, 6}, // -X
{0, 1, 5, 4}, // -Y
{2, 3, 1, 0}, // -Z
{1, 3, 7, 5}, // +X
{3, 2, 6, 7}, // +Y
{4, 5, 7, 6}, // +Z
};
static bool testProjectedLine(const CVector2f& a, const CVector2f& b, const CVector2f& point) {
zeus::CVector2f normal = (b - a).perpendicularVector().normalized();
return point.dot(normal) >= a.dot(normal);
}
bool CAABox::projectedPointTest(const CMatrix4f& mvp, const CVector2f& point) const {
CVector2f projPoints[8];
for (int i = 0; i < 8; ++i)
projPoints[i] = mvp.multiplyOneOverW(getPoint(i)).toVec2f();
for (int i = 0, j; i < 6; ++i) {
for (j = 0; j < 3; ++j) {
if (!testProjectedLine(projPoints[ProjWindings[i][j]], projPoints[ProjWindings[i][j + 1]], point))
break;
}
if (j == 3 && testProjectedLine(projPoints[ProjWindings[i][3]], projPoints[ProjWindings[i][0]], point))
return true;
}
return false;
}
2019-02-07 23:56:15 -08:00
bool CAABox::intersects(const CSphere& other) const {
float dist = 0.f;
int comps = 0;
for (int i = 0; i < 3; i++) {
if (other.position[i] < min[i]) {
if (other.position[i] + other.radius >= min[i]) {
float comp = other.position[i] - min[i];
dist += comp * comp;
comps |= 1 << i;
} else {
return false;
}
} else if (other.position[i] > max[i]) {
if (other.position[i] - other.radius <= max[i]) {
float comp = other.position[i] - max[i];
dist += comp * comp;
comps |= 1 << i;
} else {
return false;
}
}
}
if (comps == 0)
return true;
return dist <= other.radius * other.radius;
}
float CAABox::intersectionRadius(const CSphere& other) const {
float dist = 0.f;
int comps = 0;
for (int i = 0; i < 3; i++) {
if (other.position[i] < min[i]) {
if (other.position[i] + other.radius >= min[i]) {
float comp = other.position[i] - min[i];
dist += comp * comp;
comps |= 1 << i;
} else {
return -1.f;
}
} else if (other.position[i] > max[i]) {
if (other.position[i] - other.radius <= max[i]) {
float comp = other.position[i] - max[i];
dist += comp * comp;
comps |= 1 << i;
} else {
return -1.f;
}
}
}
if (comps == 0)
return dist;
return (dist <= other.radius * other.radius) ? dist : -1.f;
}
CAABox CAABox::booleanIntersection(const CAABox& other) const {
2019-02-23 23:15:32 -08:00
CVector3f minVec;
CVector3f maxVec;
2019-02-07 23:56:15 -08:00
for (int i = 0; i < 3; ++i) {
if (min[i] <= other.min[i] && max[i] >= other.max[i]) {
minVec[i] = other.min[i];
maxVec[i] = other.max[i];
} else if (other.min[i] <= min[i] && other.max[i] >= max[i]) {
minVec[i] = min[i];
maxVec[i] = max[i];
} else if (other.min[i] <= min[i] && other.max[i] >= min[i]) {
minVec[i] = min[i];
maxVec[i] = other.max[i];
} else if (other.min[i] <= max[i] && other.max[i] >= max[i]) {
minVec[i] = other.min[i];
maxVec[i] = max[i];
}
}
return {minVec, maxVec};
}
2019-04-02 21:30:18 -07:00
CAABox::Tri CAABox::getTri(EBoxFaceId face, int windOffset) const {
zeus::CVector3f verts[] = {
{min.x(), min.y(), max.z()},
min,
{max.x(), min.y(), min.z()},
{max.x(), min.y(), max.z()}};
switch (face) {
case EBoxFaceId::YMin:
default:
verts[0].assign(min.x(), min.y(), max.z());
verts[1].assign(max.x(), min.y(), max.z());
verts[2].assign(max.x(), min.y(), min.z());
verts[3] = min;
break;
case EBoxFaceId::YMax:
verts[0] = max;
verts[1].assign(min.x(), max.y(), max.z());
verts[2].assign(min.x(), max.y(), min.z());
verts[3].assign(max.x(), max.y(), min.z());
break;
case EBoxFaceId::XMin:
verts[0].assign(min.x(), max.y(), max.z());
verts[1].assign(min.x(), min.y(), max.z());
verts[2] = min;
verts[3].assign(min.x(), max.y(), min.z());
break;
case EBoxFaceId::XMax:
verts[0].assign(max.x(), min.y(), max.z());
verts[1] = max;
verts[2].assign(max.x(), max.y(), min.z());
verts[3].assign(max.x(), min.y(), min.z());
break;
case EBoxFaceId::ZMax:
verts[0].assign(min.x(), max.y(), max.z());
verts[1] = max;
verts[2].assign(max.x(), min.y(), max.z());
verts[3].assign(min.x(), min.y(), max.z());
break;
case EBoxFaceId::ZMin:
verts[0] = min;
verts[1].assign(max.x(), min.y(), min.z());
verts[2].assign(max.x(), max.y(), min.z());
verts[3].assign(min.x(), max.y(), min.z());
break;
}
return {zeus::CPlane(verts[windOffset % 2], verts[(windOffset + 1) % 2], verts[(windOffset + 2) % 2]),
2019-04-06 22:01:22 -07:00
{verts[windOffset % 2], verts[(windOffset + 1) % 2], verts[(windOffset + 2) % 2]}};
2019-04-02 21:30:18 -07:00
}
2018-12-07 21:23:50 -08:00
} // namespace zeus