mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
plane.hpp
Go to the documentation of this file.
1#pragma once
2#include "intersection.hpp"
3#include "ray.hpp"
19namespace mal_math
20{
28 struct Plane
29 {
37 bool Intersect(Intersection &i, Ray const &ray) const
38 {
39 float denom = dot(normal, ray.direction());
40 if (std::abs(denom) <= 1e-6f)
41 {
42 return false;
43 }
44 float t = dot(-ray.origin(), normal) / denom;
45 if (t > i.t || t <= 0)
46 {
47 return false;
48 }
49 i.t = t;
50 // reverse normal if the ray is on the opposite side of the plane
51 i.normal = normal * (denom > 0 ? -1 : 1);
52 i.point = ray.PointAtParameter(t);
53 return true;
54 }
62 void update_plane(vec3 const &first, vec3 const &second)
63 {
64 vec3 temp = -cross(first, second);
65 if (length(temp) == 0)
66 {
67 throw std::invalid_argument("Input vectors can't be collinear!");
68 }
69 v = first;
70 w = second;
71 normal = normalize(temp);
72 }
76 };
77}
Represents a geometric ray in 3D space.
Definition ray.hpp:24
vec3 PointAtParameter(float t) const
Computes a point along the ray based on the given parameter.
Definition ray.hpp:61
constexpr vec3 const & origin() const noexcept
Retrieves the ray's origin (const version).
Definition ray.hpp:38
constexpr vec3 const & direction() const noexcept
Retrieves the ray's direction (const version).
Definition ray.hpp:42
Provides functionalities to represent and handle intersections in 3D space.
Contains mathematical utility functions and classes.
constexpr T length(qua< T > const &q) noexcept
Computes the length (or magnitude) of a quaternion.
constexpr T dot(qua< T > const &left, qua< U > const &right) noexcept
Computes the dot product of two quaternions.
constexpr qua< T > normalize(qua< T > const &q) noexcept
Normalizes a quaternion.
constexpr vec< 3, T > cross(qua< T > const &left, vec< 3, U > const &right) noexcept
Cross product between quaternion and vector.
Contains the definition of the Ray class.
Represents the data of a ray's intersection with an object.
float t
The intersection parameter.
vec3 point
The 3D point of intersection.
vec3 normal
The 3D normal at the point of intersection.
Represents a plane in 3D space defined by its normal and two vectors.
Definition plane.hpp:29
void update_plane(vec3 const &first, vec3 const &second)
Updates the plane's vectors and normal based on two new vectors.
Definition plane.hpp:62
vec3 w
The other vector defining the plane.
Definition plane.hpp:74
vec3 normal
The normal to the plane.
Definition plane.hpp:75
bool Intersect(Intersection &i, Ray const &ray) const
Checks for the intersection of the plane with a given ray.
Definition plane.hpp:37
vec3 v
One of the vectors defining the plane.
Definition plane.hpp:73