summaryrefslogtreecommitdiff
path: root/lib/geometricPlane.h
blob: 5be76ec1e8669545e625d3adfc6a14301985197e (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once

#include "config/types.h"
#include "ray.h"
#include <glm/geometric.hpp>
#include <glm/gtx/intersect.hpp>
#include <glm/vec3.hpp>
#include <optional>

class GeometricPlane {
public:
	struct DistAndPosition {
		float dist;
		Position3D position;
	};
	enum class PlaneRelation { Above, Below, On };

	Position3D origin;
	Normal3D normal;

	[[nodiscard]] inline PlaneRelation
	getRelation(Position3D point) const
	{
		const auto d = glm::dot(normal, point - origin);
		return d < 0.F ? PlaneRelation::Below : d > 0.F ? PlaneRelation::Above : PlaneRelation::On;
	}

	[[nodiscard]] inline std::optional<DistAndPosition>
	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)};
	}

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