summaryrefslogtreecommitdiff
path: root/lib/geometricPlane.cpp
blob: 71216c12bfdd0a591ccdf82ef35b58c25d0b3648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "geometricPlane.h"
#include "ray.hpp"
#include <glm/geometric.hpp>
#include <glm/gtx/intersect.hpp>

GeometricPlane::PlaneRelation
GeometricPlane::getRelation(glm::vec3 p) const
{
	const auto d = glm::dot(normal, p - origin);
	return d < 0.f ? PlaneRelation::Below : d > 0.f ? PlaneRelation::Above : PlaneRelation::On;
}

bool
GeometricPlane::isIntersect(PlaneRelation a, PlaneRelation b)
{
	return ((a == PlaneRelation::Above && b == PlaneRelation::Below)
			|| (a == PlaneRelation::Below && b == PlaneRelation::Above));
}

std::optional<GeometricPlane::DistAndPosition>
GeometricPlane::getRayIntersectPosition(const Ray & ray) const
{
	float dist {};
	if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) {
		return {};
	}
	return DistAndPosition {dist, ray.start + (ray.direction * dist)};
}