minor rewrite
This commit is contained in:
parent
8639deac1e
commit
20ecd2ab9d
8 changed files with 173 additions and 279 deletions
62
interfaces/Crafter.Math-Ray.cppm
Executable file
62
interfaces/Crafter.Math-Ray.cppm
Executable file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
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>
|
||||
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) {
|
||||
constexpr float EPSILON = 0.0000001;
|
||||
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);
|
||||
float determinant = Vector<T, 3, 0>::Dot(edge1, h);
|
||||
|
||||
|
||||
if(determinant < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (determinant > -EPSILON && determinant < EPSILON) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
float inverse_determinant = 1.0 / determinant;
|
||||
|
||||
Vector<T, 3, 0> origins_diff_vector = rayOrigin - vert0;
|
||||
float u = Vector<T, 3, 0>::Dot(origins_diff_vector, h) * inverse_determinant;
|
||||
|
||||
if (u < 0.0 || u > 1.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector<T, 3, 0> q = Vector<T, 3, 0>::Cross(origins_diff_vector, edge1);
|
||||
float v = inverse_determinant * Vector<T, 3, 0>::Dot(rayDir, q);
|
||||
|
||||
if (v < 0.0 || u + v > 1.0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return inverse_determinant * Vector<T, 3, 0>::Dot(edge2, q);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue