/* 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:Intersection; import :Vector; import std; namespace Crafter { export template constexpr T IntersectionTestRayTriangle(Vector vert0, Vector vert1, Vector vert2, Vector rayOrigin, Vector rayDir) { Vector edge1 = vert1 - vert0; Vector edge2 = vert2 - vert0; Vector h = Vector::Cross(rayDir, edge2); T determinant = Vector::Dot(edge1, h); if (determinant <= std::numeric_limits::epsilon()) { return std::numeric_limits::max(); } T inverse_determinant = T(1) / determinant; Vector origins_diff_vector = rayOrigin - vert0; T u = Vector::Dot(origins_diff_vector, h) * inverse_determinant; if (u < 0.0 || u > 1.0) { return std::numeric_limits::max(); } Vector q = Vector::Cross(origins_diff_vector, edge1); T v = inverse_determinant * Vector::Dot(rayDir, q); if (v < 0.0 || u + v > 1.0) { return std::numeric_limits::max(); } return inverse_determinant * Vector::Dot(edge2, q); } export template constexpr T IntersectionTestRaySphere(Vector position, T radius, Vector rayOrigin, Vector rayDir) { T a = Vector::Dot(rayDir, rayDir); T b = Vector::Dot(rayDir, (T(2) * (rayOrigin - position))); T c = Vector::Dot(position, position) + Vector::Dot(rayOrigin, rayOrigin) - T(2) * Vector::Dot(rayOrigin, position) - radius * radius; T d = b * b + (T(-4)) * a * c; if (d < 0) { return std::numeric_limits::max(); } d = std::sqrt(d); float t = (T(-0.5)) * (b + d) / a; if (t > T(0)) { return t; } else { return std::numeric_limits::max(); } } export template constexpr T IntersectionTestRayOrientedBox(Vector boxPosition, Vector boxSize, Vector boxRotation, Vector rayOrigin, Vector rayDir) { Vector invRot( -boxRotation.x, -boxRotation.y, -boxRotation.z, boxRotation.w ); Vector localOrigin = Vector::Rotate(rayOrigin - boxPosition, invRot); Vector localDir = Vector::Rotate(rayDir, invRot); Vector halfExtents = boxSize * T(0.5); T tMin = T(0); T tMax = std::numeric_limits::max(); for (std::uint32_t i = 0; i < 3; ++i) { if (std::abs(localDir.v[i]) < std::numeric_limits::epsilon()) { if (localOrigin.v[i] < -halfExtents.v[i] || localOrigin.v[i] > halfExtents.v[i]) { return std::numeric_limits::max(); } } else { T invD = T(1) / localDir.v[i]; T t1 = (-halfExtents.v[i] - localOrigin.v[i]) * invD; T t2 = ( halfExtents.v[i] - localOrigin.v[i]) * invD; if (t1 > t2) { std::swap(t1, t2); } tMin = std::max(tMin, t1); tMax = std::min(tMax, t2); if (tMin > tMax) { return std::numeric_limits::max(); } } } return (tMin >= T(0)) ? tMin : tMax; } }