Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Use trumbore moller algorithm for tri inter
Browse files Browse the repository at this point in the history
  • Loading branch information
stephomi committed Jan 8, 2014
1 parent f03b599 commit 2231fb0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
41 changes: 25 additions & 16 deletions math3d/geometry.js
Expand Up @@ -20,27 +20,36 @@ Geometry.mouseOnUnitSphere = function (mouseXY)
};

/** Compute intersection vertex between a ray and a triangle. Returne false if it doesn't intersect. */
Geometry.intersectionRayTriangle = function (s1, s2, v1, v2, v3, normal, vertInter)
Geometry.intersectionRayTriangle = function (orig, dir, v1, v2, v3, vertInter)
{
var temp = [0, 0, 0];
var dist1 = vec3.dot(vec3.sub(temp, s1, v1), normal);
var dist2 = vec3.dot(vec3.sub(temp, s2, v1), normal);
//ray copplanar to triangle
if ((dist1 * dist2) >= 0)
// moller trumbore intersection algorithm
// http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-9-ray-triangle-intersection/m-ller-trumbore-algorithm/
var EPSILON = 0.000001;

var edge1 = [0.0, 0.0, 0.0];
vec3.sub(edge1, v2, v1);
var edge2 = [0.0, 0.0, 0.0];
vec3.sub(edge2, v3, v1);
var pvec = [0.0, 0.0, 0.0];
vec3.cross(pvec, dir, edge2);
var det = vec3.dot(edge1, pvec);
if (det > -EPSILON && det < EPSILON)
return false;
//intersection between ray and triangle
var val = -dist1 / (dist2 - dist1);
vec3.scaleAndAdd(vertInter, s1, vec3.sub(temp, s2, s1), val);
var cross = [0, 0, 0];
vec3.cross(cross, normal, vec3.sub(temp, v2, v1));
if (vec3.dot(cross, vec3.sub(temp, vertInter, v1)) < 0)
var invDet = 1.0 / det;
var tvec = [0.0, 0.0, 0.0];
vec3.sub(tvec, orig, v1);
var u = vec3.dot(tvec, pvec) * invDet;
if (u < 0.0 || u > 1.0)
return false;
vec3.cross(cross, normal, vec3.sub(temp, v3, v2));
if (vec3.dot(cross, vec3.sub(temp, vertInter, v2)) < 0)
var qvec = [0.0, 0.0, 0.0];
vec3.cross(qvec, tvec, edge1);
var v = vec3.dot(dir, qvec) * invDet;
if (v < 0.0 || u + v > 1.0)
return false;
vec3.cross(cross, normal, vec3.sub(temp, v1, v3));
if (vec3.dot(cross, vec3.sub(temp, vertInter, v1)) < 0)
var t = vec3.dot(edge2, qvec) * invDet;
if (t < 0.0)
return false;
vec3.scaleAndAdd(vertInter, orig, dir, t);
return true;
};

Expand Down
6 changes: 2 additions & 4 deletions math3d/picking.js
Expand Up @@ -51,16 +51,14 @@ Picking.prototype = {
var vertInter = [0, 0, 0];
for (var i = 0; i < nbTrisCandidates; ++i)
{
var indTri = iTrisCandidates[i];
var t = triangles[indTri];
indTri *= 3;
var indTri = iTrisCandidates[i] * 3;
var ind1 = iAr[indTri] * 3,
ind2 = iAr[indTri + 1] * 3,
ind3 = iAr[indTri + 2] * 3;
var v1 = [vAr[ind1], vAr[ind1 + 1], vAr[ind1 + 2]],
v2 = [vAr[ind2], vAr[ind2 + 1], vAr[ind2 + 2]],
v3 = [vAr[ind3], vAr[ind3 + 1], vAr[ind3 + 2]];
if (Geometry.intersectionRayTriangle(vNear, vFar, v1, v2, v3, t.normal_, vertInter))
if (Geometry.intersectionRayTriangle(vNear, ray, v1, v2, v3, vertInter))
{
var testDistance = vec3.sqrDist(vNear, vertInter);
{
Expand Down

0 comments on commit 2231fb0

Please sign in to comment.