Rik*_*Rik 8 3d javafx intersection javafx-3d
在JavaFX 8 3D场景中是否可以沿着光线(例如PickRay)找到点,从3D空间中的任意点开始,使用一些3D方向向量,其中光线与网格中的三角形相交(MeshView内的TriangleMesh)?
我知道这是在Camera/MouseHandler中完成的,用于鼠标拾取,但我无法看到任何方式为任意光线做这件事.
Jos*_*eda 10
正如@ jdub1581所暗示的那样,光线只是一个几何向量,所以为了找到与这个向量相交的三角形列表,我们需要解决"线相交平面"和"线与三角形边界内的平面相交"的问题.
假设我们有一个TriangleMesh,我们有一个顶点列表和一个面列表.每个顶点有3个坐标,每个顶面有3个顶点(不考虑纹理,法线,......).为简单起见,我们使用两个列表Point3D来存储它们.
在此链接中,有几种3D形状可供使用.我们抓一个CuboidMesh.
CuboidMesh cuboid = new CuboidMesh(10f,12f,4f,4);
Run Code Online (Sandbox Code Playgroud)
这将给我们这个3D形状:

现在,如果我们看一下网格,我们可以创建两个包含顶点和面的列表:
List<Point3D> vertices=Arrays.asList(new Point3D(5.0, 6.0, 2.0),
new Point3D(5.0, 6.0, 2.0), new Point3D(5.0, -6.0, 2.0), ...,
new Point3D(-1.875, -2.25, -2.0), new Point3D(-1.875, -1.5, -2.0));
List<Point3D> faces=Arrays.asList(new Point3D(0, 386, 388),
new Point3D(98, 387, 386.0), new Point3D(100, 388, 387), ...,
new Point3D(383, 1535, 1537), new Point3D(1536, 1537, 1535));
Run Code Online (Sandbox Code Playgroud)
让我们在场景中添加一些3D点,一个原点和一个目标,都在全局坐标中,并定义矢量的方向,规范化:
Point3D gloOrigin=new Point3D(4,-7,-4);
Point3D gloTarget=new Point3D(2,3,2);
Point3D direction=gloTarget.subtract(gloOrigin).normalize(); // -0.154,0.771,0.617
Run Code Online (Sandbox Code Playgroud)
射线方程式将是这样的:
r(t) = (4,-7,-4)+t*(-0.154,0.771,0.617)
Run Code Online (Sandbox Code Playgroud)
如果我们在这两个点之间添加一个细长的圆柱体,我们就可以直观地表示我们的射线:

边界框交叉口
第一步是检查光线是否与我们形状的边界框相交.在形状的局部坐标中,我们有6个面部由它们的法线给出,它们有6个中心:
Bounds locBounds = cuboid.getBoundsInLocal();
List<Point3D> normals=Arrays.asList(new Point3D(-1,0,0),new Point3D(1,0,0),
new Point3D(0,-1,0), new Point3D(0,1,0), new Point3D(0,0,-1), new Point3D(0,0,1));
List<Point3D> positions=Arrays.asList(new Point3D(locBounds.getMinX(),0,0),
new Point3D(locBounds.getMaxX(),0,0), new Point3D(0,locBounds.getMinY(),0),
new Point3D(0,locBounds.getMaxY(),0), new Point3D(0,0,locBounds.getMinZ()),
new Point3D(0,0,locBounds.getMaxZ()));
Run Code Online (Sandbox Code Playgroud)
由于我们将在本地系统上工作,因此我们需要在此坐标中使用原点:
Point3D gloOriginInLoc = cuboid.sceneToLocal(gloOrigin); // 4,-7,-4 since the box is centered in 0,0,0
Run Code Online (Sandbox Code Playgroud)
现在,对于六个面中的任何一个,我们得到t该链接后到飞机的距离.然后我们可以检查该点是否属于该框.
AtomicInteger counter = new AtomicInteger();
IntStream.range(0, 6).forEach(i->{
double d=-normals.get(i).dotProduct(positions.get(i));
double t=-(gloOriginInLoc.dotProduct(normals.get(i))+d)/
(gloDirection.dotProduct(normals.get(i)));
Point3D locInter=gloOriginInLoc.add(gloDirection.multiply(t));
if(locBounds.contains(locInter)){
counter.getAndIncrement();
}
});
Run Code Online (Sandbox Code Playgroud)
如果counter.get()>0那时我们在光线和形状之间有一些交叉点,我们可以继续使用三角形.在这个例子中,这些将是交叉点:(3.5,-4.5,-2)和(2.5,0.5,2).
三角形交叉点
有几种算法可以查找光线是否与网格的任何三角形相交,因此我们不需要重新发明轮子.
我使用过的是来自TomasMöller和Ben Trumbore.它将提供t从原点到平面的距离,以及u,v给定交叉点的三角形内部的坐标.
一旦我们在形状的局部坐标中得到原点,并且我们知道光线的方向,这个算法的实现是这样的:
private final float EPS = 0.000001f;
public List<Point3D> getIntersections(Point3D origin, Point3D direction,
List<Point3D> points, List<Point3D> faces){
return faces.parallelStream().filter(f->{
// vertices indices
int p0=(int)f.getX();
int p1=(int)f.getY();
int p2=(int)f.getZ();
// vertices 3D coordinates
Point3D a = points.get(p0);
Point3D b = points.get(p1);
Point3D c = points.get(p2);
Point3D edge1 = b.substract(a);
Point3D edge2 = c.substract(a);
Point3D pvec=direction.crossProduct(edge2);
float det=edge1.dotProduct(pvec);
if(det<=-EPS || det>=EPS){
float inv_det=1f/det;
Point3D tvec=origin.substract(a);
float u = tvec.dotProduct(pvec)*inv_det;
if(u>=0f && u<=1f){
Point3D qvec=tvec.crossProduct(edge1);
float v = direction.dotProduct(qvec)*inv_det;
if(v>=0 && u+v<=1f){
float t = c.dotProduct(qvec)*inv_det;
System.out.println("t: "+t+", u: "+u+", v: "+v);
return true;
}
}
}
return false;
}).collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
在这个样本中,我们找到了由这些顶点给出的三个面:(85,1245,1274),(85,1274,1266)和(351,1476,1479).
如果我们绘制那些面将看到交叉点:

请注意,通过在形状的局部坐标系中执行所有操作,我们保存了将每个三角形转换为全局系统的操作.
这个算法非常快.我在不到40毫秒的时间内测试了最多3M三角形.
此测试的所有代码均可在此处获得.
好吧,我几乎把它宰了,所以我将提供一个非常容易理解的教程.写得很好,必须承认我也学到了很多东西!
我将把数学留给文章,因为要覆盖很多(转换点和使用矩阵)
总结一下:
光线上的任何点都是距离原点的距离的函数
Ray(t) = Origin + Direction(t)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
编辑:
在Jose的好榜样之后,我冒昧地创建了一个Ray类和一个SimpleRayTest示例,以显示光线在距离上的路径(将光线视为射弹).虽然它不包括三角形交叉点,但它应该有助于可视化光线的工作方式.
在Jose提供的图书馆链接中也提供了来源.