summaryrefslogtreecommitdiff
path: root/lib/geometricPlane.h
blob: d4b803debbc40639acf62b769564d720d27e64ed (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
45
46
47
#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:
	GeometricPlaneT() = default;

	GeometricPlaneT(PositionType origin, Normal3D normal) : origin(std::move(origin)), normal(normal) { }

	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, RelativePosition3D(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)};
	}
};