Skip to content
Snippets Groups Projects
Sphere.cpp 1.4 KiB
Newer Older
Yoel's avatar
Yoel committed
#define _USE_MATH_DEFINES

#include "Sphere.h"
Yoel's avatar
Yoel committed

Yoel's avatar
Yoel committed
#include "math.h"

namespace shapes {
Yoel's avatar
Yoel committed
Sphere::Sphere(float radius,
               const std::shared_ptr<material::Material>& material)
    : radius(radius), material(material) {
Yoel's avatar
Yoel committed
}
Yoel's avatar
Yoel committed
std::optional<cam::Hit> Sphere::intersect(const cam::Ray& r) const {
Yoel's avatar
Yoel committed
	util::Vec3 d = r.d;
	util::Vec3 x0 = r.x0;
	float a = util::dot(d, d);
	float b = 2 * util::dot(x0, d);
	float c = util::dot(x0, x0) - (radius * radius);
	float discrim = b * b - 4 * a * c;
Yoel's avatar
Yoel committed

Yoel's avatar
Yoel committed
	if (discrim >= 0) {
		float t1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
Yoel's avatar
Yoel committed

Yoel's avatar
Yoel committed
		if (r.in_range(t1)) {
			util::Vec3 t1HitPoint = r(t1);
			float theta = acos(t1HitPoint.y() / radius);
			float phi = M_PI + atan2(t1HitPoint.x(), t1HitPoint.z());
Yoel's avatar
Yoel committed
			return std::optional<cam::Hit>(
Yoel's avatar
Yoel committed
			    cam::Hit(t1HitPoint, t1HitPoint, t1, material));
		} else {
			float t2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
Yoel's avatar
Yoel committed
			if (r.in_range(t2)) {
				util::Vec3 t2HitPoint = r(t2);
				float theta = acos(t2HitPoint.y() / radius);
				float phi = M_PI + atan2(t2HitPoint.x(), t2HitPoint.z());
Yoel's avatar
Yoel committed
				return std::optional<cam::Hit>(
Yoel's avatar
Yoel committed
				    cam::Hit(t2HitPoint, t2HitPoint, t2, material));
			} else {
Yoel's avatar
Yoel committed
				return std::nullopt;
Yoel's avatar
Yoel committed
			}
		}
	} else {
Yoel's avatar
Yoel committed
		return std::nullopt;
Yoel's avatar
Yoel committed
	}
Yoel's avatar
Yoel committed
}
Yoel's avatar
Yoel committed
util::AxisAlignedBoundingBox Sphere::bounds() const {
	return util::AxisAlignedBoundingBox(util::Vec3(-radius),
	                                    util::Vec3(radius));
}
Yoel's avatar
Yoel committed
}  // namespace shapes