mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
triangle.hpp
Go to the documentation of this file.
1#pragma once
2#include "../math.hpp"
11namespace mal_math
12{
17 struct Triangle
18 {
32 [[nodiscard]] static bool Intersect(vec3 const &p0, vec3 const &p1, vec3 const &p2, float &nearest_t, Ray const &ray)
33 {
34 const float EPSILON = 0.0000001f;
35 vec3 edge1, edge2, h, s, q;
36 float a, f, u, v;
37 edge1 = p1 - p0;
38 edge2 = p2 - p0;
39 h = cross(ray.direction(), edge2);
40 a = dot(edge1, h);
41 if (a > -EPSILON && a < EPSILON)
42 return false; // This ray is parallel to this triangle.
43 f = 1.0f / a;
44 s = ray.origin() - p0;
45 u = f * dot(s, h);
46 if (u < 0.0f || u > 1.0f)
47 return false;
48 q = cross(s, edge1);
49 v = f * dot(ray.direction(), q);
50 if (v < 0.0f || u + v > 1.0f)
51 return false;
52 // At this stage we can compute t to find out where the intersection point is on the line.
53 float t = f * dot(edge2, q);
54 if (t > EPSILON && t < nearest_t) // ray intersection
55 {
56 nearest_t = t;
57 return true;
58 }
59 else // This means that there is a line intersection but not a ray intersection.
60 return false;
61 }
72 [[nodiscard]] static bool Intersect(vec3 const &p0, vec3 const &p1, vec3 const &p2, Intersection &nearest, Ray const &ray)
73 {
74 if(Intersect(p0, p1, p2, nearest.t, ray))
75 {
76 nearest.point = ray.PointAtParameter(nearest.t);
77 return true;
78 }
79 return false;
80 }
88 [[nodiscard]] bool Intersect(float &nearest_t, Ray const &ray) const
89 {
90 return Intersect(a, b, c, nearest_t, ray);
91 }
99 [[nodiscard]] bool Intersect(Intersection &nearest, Ray const &ray) const
100 {
101 return Intersect(a, b, c, nearest, ray);
102 }
103 };
104}
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 various mathematical utilities, vector operations, and custom hash specializations.
Contains mathematical utility functions and classes.
constexpr T dot(qua< T > const &left, qua< U > const &right) noexcept
Computes the dot product of two quaternions.
constexpr vec< 3, T > cross(qua< T > const &left, vec< 3, U > const &right) noexcept
Cross product between quaternion and vector.
Represents the data of a ray's intersection with an object.
float t
The intersection parameter.
vec3 point
The 3D point of intersection.
Represents a triangle defined by three 3D points.
Definition triangle.hpp:18
bool Intersect(float &nearest_t, Ray const &ray) const
Determines if a ray intersects with this triangle.
Definition triangle.hpp:88
bool Intersect(Intersection &nearest, Ray const &ray) const
Determines if a ray intersects with this triangle and updates intersection details.
Definition triangle.hpp:99
vec3 b
Second vertex of the triangle.
Definition triangle.hpp:20
vec3 c
Third vertex of the triangle.
Definition triangle.hpp:21
vec3 a
First vertex of the triangle.
Definition triangle.hpp:19
static bool Intersect(vec3 const &p0, vec3 const &p1, vec3 const &p2, float &nearest_t, Ray const &ray)
Determines if a ray intersects with the triangle using its vertices.
Definition triangle.hpp:32
static bool Intersect(vec3 const &p0, vec3 const &p1, vec3 const &p2, Intersection &nearest, Ray const &ray)
Determines if a ray intersects with the triangle and updates intersection details.
Definition triangle.hpp:72