2022-10-18 00:44:49 -04:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 17/10/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_2_COLLIDERS_H
|
|
|
|
#define STEP_2_COLLIDERS_H
|
|
|
|
|
|
|
|
#include <math/vectors.h>
|
|
|
|
|
|
|
|
namespace Raytracing {
|
|
|
|
// 3D Axis Aligned Bounding Box
|
|
|
|
class AABB {
|
|
|
|
protected:
|
2022-10-18 23:11:51 -04:00
|
|
|
Vec4 min;
|
|
|
|
Vec4 max;
|
2022-10-18 00:44:49 -04:00
|
|
|
bool empty = false;
|
|
|
|
public:
|
|
|
|
AABB() {
|
|
|
|
empty = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
AABB(PRECISION_TYPE minX, PRECISION_TYPE minY, PRECISION_TYPE minZ, PRECISION_TYPE maxX, PRECISION_TYPE maxY, PRECISION_TYPE maxZ):
|
|
|
|
min{minX, minY, minZ}, max{maxX, maxY, maxZ} {
|
|
|
|
}
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
AABB(const Vec4& min, const Vec4& max): min(min), max(max) {}
|
2022-10-18 00:44:49 -04:00
|
|
|
|
|
|
|
// creates an AABB extending of size centered on x, y, z
|
|
|
|
AABB(PRECISION_TYPE x, PRECISION_TYPE y, PRECISION_TYPE z, PRECISION_TYPE size):
|
|
|
|
min{x - size, y - size, z - size}, max{x + size, y + size, z + size} {
|
|
|
|
}
|
|
|
|
|
|
|
|
// translates the AABB to position x,y,z for world collision detection
|
|
|
|
[[nodiscard]] AABB translate(PRECISION_TYPE x, PRECISION_TYPE y, PRECISION_TYPE z) const {
|
2022-10-18 23:11:51 -04:00
|
|
|
Vec4 pos = {x, y, z};
|
|
|
|
return {min + pos, max + pos};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] AABB translate(const Vec4& vec) const {
|
|
|
|
Vec4 pos = {vec.x(), vec.y(), vec.z()};
|
2022-10-18 00:44:49 -04:00
|
|
|
return {min + pos, max + pos};
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns an expanded version of this AABB is the other AABB is larger then this AABB
|
|
|
|
[[nodiscard]] AABB expand(const AABB& other) const {
|
|
|
|
PRECISION_TYPE minX = std::min(min.x(), other.min.x());
|
|
|
|
PRECISION_TYPE minY = std::min(min.y(), other.min.y());
|
|
|
|
PRECISION_TYPE minZ = std::min(min.z(), other.min.z());
|
|
|
|
PRECISION_TYPE maxX = std::max(max.x(), other.max.x());
|
|
|
|
PRECISION_TYPE maxY = std::max(max.y(), other.max.y());
|
|
|
|
PRECISION_TYPE maxZ = std::max(max.z(), other.max.z());
|
|
|
|
return {minX, minY, minZ, maxX, maxY, maxZ};
|
|
|
|
}
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
[[nodiscard]] inline bool
|
|
|
|
intersects(PRECISION_TYPE minX, PRECISION_TYPE minY, PRECISION_TYPE minZ, PRECISION_TYPE maxX, PRECISION_TYPE maxY,
|
2022-10-18 00:44:49 -04:00
|
|
|
PRECISION_TYPE maxZ) const {
|
2022-10-18 23:11:51 -04:00
|
|
|
return min.x() <= maxX && max.x() >= minX && min.y() <= maxY && max.y() >= minY && min.z() <= maxZ && max.z() >= minZ;
|
2022-10-18 00:44:49 -04:00
|
|
|
}
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
[[nodiscard]] inline bool intersects(const Vec4& minV, const Vec4& maxV) const {
|
2022-10-18 00:44:49 -04:00
|
|
|
return intersects(minV.x(), minV.y(), minV.z(), maxV.x(), maxV.y(), maxV.z());
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] inline bool intersects(const AABB& other) const {
|
|
|
|
return intersects(other.min, other.max);
|
|
|
|
}
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
bool intersects(const Ray& ray, PRECISION_TYPE tmin, PRECISION_TYPE tmax);
|
|
|
|
bool simpleSlabRayAABBMethod(const Ray& ray, PRECISION_TYPE tmin, PRECISION_TYPE tmax);
|
|
|
|
|
2022-10-18 00:44:49 -04:00
|
|
|
[[nodiscard]] inline bool isInside(PRECISION_TYPE x, PRECISION_TYPE y, PRECISION_TYPE z) const {
|
2022-10-18 23:11:51 -04:00
|
|
|
return x >= min.x() && x <= max.x() && y >= min.y() && y <= max.y() && z >= min.z() && z <= max.z();
|
2022-10-18 00:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] inline bool intersectsWithYZ(PRECISION_TYPE y, PRECISION_TYPE z) const {
|
|
|
|
return y >= min.y() && y <= max.y() && z >= min.z() && z <= max.z();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] inline bool intersectsWithXZ(PRECISION_TYPE x, PRECISION_TYPE z) const {
|
|
|
|
return x >= min.x() && x <= max.x() && z >= min.z() && z <= max.z();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] inline bool intersectsWithXY(PRECISION_TYPE x, PRECISION_TYPE y) const {
|
|
|
|
return x >= min.x() && x <= max.x() && y >= min.y() && y <= max.y();
|
|
|
|
}
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
[[nodiscard]] inline Vec4 getCenter() const {
|
2022-10-18 00:44:49 -04:00
|
|
|
return {min.x() + (max.x() - min.x()) * 0.5, min.y() + (max.y() - min.y()) * 0.5, min.z() + (max.z() - min.z()) * 0.5};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] PRECISION_TYPE longestDistanceFromCenter() const;
|
|
|
|
// 0 - x
|
|
|
|
// 1 - y
|
|
|
|
// 2 - z
|
|
|
|
[[nodiscard]] int longestAxis() const;
|
|
|
|
[[nodiscard]] PRECISION_TYPE longestAxisLength() const;
|
2022-10-18 23:11:51 -04:00
|
|
|
[[nodiscard]] std::pair<AABB, AABB> splitByLongestAxis();
|
2022-10-18 00:44:49 -04:00
|
|
|
|
|
|
|
[[nodiscard]] PRECISION_TYPE avgDistanceFromCenter() const;
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
[[nodiscard]] inline bool isEmpty() const { return empty; }
|
|
|
|
|
|
|
|
[[nodiscard]] Vec4 getMin() const { return min; }
|
|
|
|
|
|
|
|
[[nodiscard]] Vec4 getMax() const { return max; }
|
2022-10-18 00:44:49 -04:00
|
|
|
|
|
|
|
};
|
2022-10-18 23:11:51 -04:00
|
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& out, const AABB& v) {
|
|
|
|
return out << "AABB{min{" << v.getMin().x() << ", " << v.getMin().y() << ", " << v.getMin().z() << "}, max{" << v.getMax().x() << ", " << v.getMax().y()
|
|
|
|
<< ", " << v.getMax().z() << "}} ";
|
|
|
|
}
|
2022-10-18 00:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //STEP_2_COLLIDERS_H
|