summaryrefslogtreecommitdiff
path: root/lib/geometricPlane.h
blob: 3f95d3c28a68d35ca596fb5626555cbf41c21100 (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
#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:
	enum class PlaneRelation { Above, Below, On };

	static bool isIntersect(PlaneRelation a, PlaneRelation b);
};

template<typename PositionType> class GeometricPlaneT : public GeometricPlane {
public:
	struct DistAndPosition {
		PositionType::value_type dist;
		PositionType position;
	};

	PositionType origin;
	Normal3D normal;

	[[nodiscard]] inline PlaneRelation
	getRelation(PositionType 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<PositionType> & ray) const
	{
		float dist {};
		if (!glm::intersectRayPlane(ray.start, ray.direction, origin, normal, dist)) {
			return {};
		}
		return DistAndPosition {dist, ray.start + (ray.direction * dist)};
	}
};