Skip to content
Snippets Groups Projects
Commit 6745d5ea authored by Yoel's avatar Yoel
Browse files

Complete implementation of BoundingVolumes

parent 7565f2c8
No related branches found
No related tags found
No related merge requests found
#include "AxisAlignedBoundingBox.h"
#include <algorithm>
#include <limits>
namespace util {
// Constructor
AxisAlignedBoundingBox::AxisAlignedBoundingBox() : min(Vec3(0)), max(Vec3(0)) {
}
AxisAlignedBoundingBox::AxisAlignedBoundingBox(Vec3& min, Vec3& max)
: min(min), max(max) {
}
// Operator
AxisAlignedBoundingBox AxisAlignedBoundingBox::operator+(
AxisAlignedBoundingBox& rhs) const {
Vec3 min(std::min<float>(min.x(), rhs.min.x()),
std::min<float>(min.y(), rhs.min.y()),
std::min<float>(min.z(), rhs.min.z()));
Vec3 max(std::max<float>(max.x(), rhs.max.x()),
std::max<float>(max.y(), rhs.max.y()),
std::max<float>(max.z(), rhs.max.z()));
return AxisAlignedBoundingBox(min, max);
}
// Methods
// https://education.siggraph.org/static/HyperGraph/raytrace/rtinter3.htm
bool AxisAlignedBoundingBox::intersects(cam::Ray& r) const {
float t1x = (min.x() - r.x0.x()) / r.d.x();
float t2x = (max.x() - r.x0.x()) / r.d.x();
float t1y = (min.y() - r.x0.y()) / r.d.y();
float t2y = (max.y() - r.x0.y()) / r.d.y();
float t1z = (min.z() - r.x0.z()) / r.d.z();
float t2z = (max.z() - r.x0.z()) / r.d.z();
float tmin =
std::max<float>({std::min<float>(t1x, t2x), std::min<float>(t1y, t2y),
std::min<float>(t1z, t2z)});
float tmax =
std::min<float>({std::max<float>(t1x, t2x), std::max<float>(t1y, t2y),
std::max<float>(t1z, t2z)});
return tmax >= tmin && tmax > 0;
}
bool AxisAlignedBoundingBox::contains(Vec3& v) const {
bool x = min.x() <= v.x() && v.x() <= max.x();
bool y = min.y() <= v.y() && v.y() <= max.y();
bool z = min.z() <= v.z() && v.z() <= max.z();
return x && y && z;
}
Vec3 AxisAlignedBoundingBox::minBound() const {
return min;
}
Vec3 AxisAlignedBoundingBox::maxBound() const {
return max;
}
} // namespace util
\ No newline at end of file
#pragma once
#include "../camera/Ray.h"
#include "Vec3.h"
namespace util {
class AxisAlignedBoundingBox {
public:
AxisAlignedBoundingBox();
AxisAlignedBoundingBox(Vec3& min, Vec3& max);
// Operator
AxisAlignedBoundingBox operator+(AxisAlignedBoundingBox& rhs) const;
// Methods
bool intersects(cam::Ray& r) const;
bool contains(Vec3& v) const;
Vec3 minBound() const;
Vec3 maxBound() const;
private:
const Vec3 min, max;
};
} // namespace util
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment