2026-02-11 02:52:03 +01:00
|
|
|
/*
|
|
|
|
|
Crafter®.Math
|
|
|
|
|
Copyright (C) 2026 Catcrafts®
|
|
|
|
|
catcrafts.net
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License version 3.0 as published by the Free Software Foundation;
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export module Crafter.Math:Ray;
|
|
|
|
|
import :Vector;
|
|
|
|
|
import std;
|
|
|
|
|
|
|
|
|
|
namespace Crafter {
|
|
|
|
|
export template<typename T>
|
2026-02-11 02:57:20 +01:00
|
|
|
constexpr T IntersectionTestRayTriangle(Vector<T, 3, 0> vert0, Vector<T, 3, 0> vert1, Vector<T, 3, 0> vert2, Vector<T, 3, 0> rayOrigin, Vector<T, 3, 0> rayDir) {
|
2026-02-11 03:26:59 +01:00
|
|
|
constexpr T EPSILON = 0.0000001;
|
2026-02-11 02:52:03 +01:00
|
|
|
Vector<T, 3, 0> edge1 = vert1 - vert0;
|
|
|
|
|
Vector<T, 3, 0> edge2 = vert2 - vert0;
|
|
|
|
|
|
|
|
|
|
Vector<T, 3, 0> h = Vector<T, 3, 0>::Cross(rayDir, edge2);
|
2026-02-11 03:26:59 +01:00
|
|
|
T determinant = Vector<T, 3, 0>::Dot(edge1, h);
|
2026-02-11 02:52:03 +01:00
|
|
|
|
2026-02-11 03:26:59 +01:00
|
|
|
if (determinant <= EPSILON) {
|
|
|
|
|
return std::numeric_limits<T>::max();
|
2026-02-11 02:52:03 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-11 03:26:59 +01:00
|
|
|
T inverse_determinant = T(1) / determinant;
|
2026-02-11 02:52:03 +01:00
|
|
|
|
|
|
|
|
Vector<T, 3, 0> origins_diff_vector = rayOrigin - vert0;
|
2026-02-11 03:26:59 +01:00
|
|
|
T u = Vector<T, 3, 0>::Dot(origins_diff_vector, h) * inverse_determinant;
|
2026-02-11 02:52:03 +01:00
|
|
|
|
|
|
|
|
if (u < 0.0 || u > 1.0)
|
|
|
|
|
{
|
2026-02-11 03:26:59 +01:00
|
|
|
return std::numeric_limits<T>::max();
|
2026-02-11 02:52:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector<T, 3, 0> q = Vector<T, 3, 0>::Cross(origins_diff_vector, edge1);
|
2026-02-11 03:26:59 +01:00
|
|
|
T v = inverse_determinant * Vector<T, 3, 0>::Dot(rayDir, q);
|
2026-02-11 02:52:03 +01:00
|
|
|
|
|
|
|
|
if (v < 0.0 || u + v > 1.0) {
|
2026-02-11 03:26:59 +01:00
|
|
|
return std::numeric_limits<T>::max();
|
2026-02-11 02:52:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inverse_determinant * Vector<T, 3, 0>::Dot(edge2, q);
|
|
|
|
|
}
|
2026-02-11 04:28:54 +01:00
|
|
|
|
|
|
|
|
export template<typename T>
|
|
|
|
|
constexpr T IntersectionTestRaySphere(Vector<T, 3, 0> position, T radius, Vector<T, 3, 0> rayOrigin, Vector<T, 3, 0> rayDir) {
|
|
|
|
|
T a = Vector<T, 3, 0>::Dot(rayDir, rayDir);
|
|
|
|
|
T b = Vector<T, 3, 0>::Dot(rayDir, (T(2) * (rayOrigin - position)));
|
|
|
|
|
T c = Vector<T, 3, 0>::Dot(position, position) + Vector<T, 3, 0>::Dot(rayOrigin, rayOrigin) - T(2) * Vector<T, 3, 0>::Dot(rayOrigin, position) - radius * radius;
|
|
|
|
|
T d = b * b + (T(-4)) * a * c;
|
|
|
|
|
|
|
|
|
|
if (d < 0) {
|
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d = std::sqrt(d);
|
|
|
|
|
|
|
|
|
|
float t = (T(-0.5)) * (b + d) / a;
|
|
|
|
|
if (t > T(0)) {
|
|
|
|
|
return t;
|
|
|
|
|
} else {
|
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-11 02:52:03 +01:00
|
|
|
}
|