Pick Rays can squeeze between triangles - simple repro

I’m working on a project that uses JME and I’ve run into an issue that has come up in the forums before  (See, e.g., here and here).  Occasionally, when I try to pick, I get TrianglePickResults back where some of the results have a distance of Infinity.  It seems that the problem manifests itself when a collision is found with the Bounds of a TriMesh but none of the triangles within it return true in the pick ray is intersected with them.  This can happen even if the TriMesh has no holes.



Anyhow, I was able to come up with a simple repro for the source of the problem:



import com.jme.math.Ray;
import com.jme.math.Vector3f;


public class TestRayIntersect {

   /**
    * @param args
    */
   public static void main(String[] args) {
      Ray ray = new Ray(new Vector3f(0,0,3), new Vector3f(0,0,-1));      
      
      Vector3f v0 = new Vector3f(-1f,-1f,-1f);
      Vector3f v1 = new Vector3f(+1f,-1f,-1f);
      Vector3f v2 = new Vector3f(+1f,+1f,-1f);
      Vector3f v3 = new Vector3f(-1f,+1f,-1f);
      
      Vector3f loc = new Vector3f();
      
      boolean intersect;

// Looking along pick ray (marked by 'x')      
//    3


2
//    |    |
//    |    |
//    |  x  |
//    |    |
//    |    |
//    0
1
//
      intersect = ray.intersectWhere(v0, v1, v3, loc);
      System.out.println( intersect ? "Intersect At "+loc : "No Intersection");

      intersect = ray.intersectWhere(v2, v3, v1, loc);
      System.out.println( intersect ? "Intersect At "+loc : "No Intersection");

// Looking along pick ray (marked by 'x')      
//    3
2
//    |    /|
//    |   / |
//    |  x  |
//    | /   |
//    |/    |
//    0
1

      intersect = ray.intersectWhere(v0, v1, v2, loc);
      System.out.println( intersect ? "Intersect At "+loc : "No Intersection");

      intersect = ray.intersectWhere(v2, v3, v0, loc);
      System.out.println( intersect ? "Intersect At "+loc : "No Intersection");      
      
// Output on my Mac      
//      Intersect At com.jme.math.Vector3f [X=0.0, Y=0.0, Z=-1.0]
//      Intersect At com.jme.math.Vector3f [X=0.0, Y=0.0, Z=-1.0]
//      No Intersection
//      No Intersection      
   }

}



I'll start looking into the Ray.intersect(...) code but I wanted to make sure that this behavior isn't expected and that I haven't misunderstood how Ray.intersect(...) should be working.

Is there a "well known" solution to this problem (other than tweaking the pick ray origin by small amounts)?

Looking a little more carefully, it seems that the ray fails to register the intersection is with the returning edge of a triangle.  That is, if the ray intersects the edge of the triangle running from the third corner back to the first, Ray.intersect(…) returns false.

Interesting bug. Please fix it if you can and submit the patch to the contribution board along with the test.