Skip to content
Snippets Groups Projects
CirclePlane.cpp 919 B
Newer Older
Postea's avatar
Postea committed
#include "CirclePlane.h"

namespace shapes {
Yoel's avatar
Yoel committed
CirclePlane::CirclePlane(float radius,
                         const std::shared_ptr<material::Material>& material)
    : radius(radius), material(material) {
Yoel's avatar
Yoel committed
std::optional<cam::Hit> CirclePlane::intersect(const cam::Ray& r) const {
Yoel's avatar
Yoel committed
	util::Vec3 n(0, 1, 0);
	util::Vec3 x0 = r.x0;
	util::Vec3 d = r.d;
Postea's avatar
Postea committed

Yoel's avatar
Yoel committed
	if (util::dot(d, n) > 0) {
		n = n * -1;
	}
	float a = util::dot(d, n);
	if (a == 0) {
Yoel's avatar
Yoel committed
		return std::nullopt;
Yoel's avatar
Yoel committed
	} else {
		float t = -x0[1] / d[1];
		util::Vec3 t_hitpoint = r(t);
Postea's avatar
Postea committed

Yoel's avatar
Yoel committed
		if (r.in_range(t) && t_hitpoint.length() <= radius) {
Yoel's avatar
Yoel committed
			return std::optional<cam::Hit>({t_hitpoint, n, t, material});
Yoel's avatar
Yoel committed
		} else {
Yoel's avatar
Yoel committed
			return std::nullopt;
Yoel's avatar
Yoel committed
		}
	}
Postea's avatar
Postea committed
}
util::AxisAlignedBoundingBox CirclePlane::bounds() const {
	return util::AxisAlignedBoundingBox(util::Vec3(-radius, 0, -radius),
	                                    util::Vec3(radius, 0, radius));
}
Yoel's avatar
Yoel committed
}  // namespace shapes