gpt4 book ai didi

java - WorldWind 球线相交错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:23 26 4
gpt4 key购买 nike

我在 WorldWind 的 Sphere 中看到了看似矛盾的行为-线相交逻辑。我创建了一个 Sphere 和 Line,它们相交但随后交集返回 null(扫描代码以获取评论://*** 这就是它变得古怪的地方)。

这是视觉上发生的事情(线是灰色的,但很难看到):Sphere-Line Intersecting

public class WWTest extends ApplicationTemplate {

public static class VisualizationFrame extends ApplicationTemplate.AppFrame {

public VisualizationFrame() {
super(new Dimension(1200, 1024));
final Globe globe = getWwd().getModel().getGlobe();

//Create a sphere at 0,0 on the surface of the Earth wtih a 60 NMi radius
final Vec4 sphereCenter = globe.computePointFromLocation(LatLon.ZERO);
final Sphere sphere = new Sphere(sphereCenter, 111120);
// Draw the sphere
final RenderableLayer sphereLayer = new RenderableLayer();
sphereLayer.addRenderable(sphere);

final RenderableLayer pathLayer = new RenderableLayer();
// Create a line at 10k feet (3048 meters) that starts outside the sphere at (2,-2) and proceeds into the sphere at (0.5, 0.5)
final Position lineStart = Position.fromDegrees(2, -2, 3048);
final Position lineEnds = Position.fromDegrees(0.5, 0.5, 3048);
final Path asPath = new Path(lineStart, lineEnds);
pathLayer.addRenderable(asPath);

// Now that we've visualized the line, let's do some intersection math
final Vec4 lineStartsAsVec = globe.computePointFromPosition(lineStart);
final Vec4 lineEndsAsVec = globe.computePointFromPosition(lineEnds);
final Line asLine = Line.fromSegment(lineStartsAsVec, lineEndsAsVec);

// *** This is where it gets whacky - true, but no intersection?
final boolean doesIntersect = sphere.intersects(asLine);
final Intersection[] intersection = sphere.intersect(asLine);
//outputs: Intersection found: null
System.out.println(doesIntersect ? "Intersection found: " + Arrays.toString(intersection) : "No intersection, Why Not!?!?");

insertBeforeCompass(getWwd(), sphereLayer);
insertBeforeCompass(getWwd(), pathLayer);
getWwd().getView().setEyePosition(Position.fromDegrees(0, 0, 500_000));
getLayerPanel().update(getWwd());
}
}

public static void main(String[] args) {
ApplicationTemplate.start("World Wind Sphere-Line Intersection", VisualizationFrame.class);

}
}

下面是我声明的将 WorldWind 引入我的 maven 项目的依赖项(我也尝试过“2.0.0-986”版本,但似乎没有帮助):

<dependency>
<groupId>gov.nasa</groupId>
<artifactId>worldwind</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>gov.nasa</groupId>
<artifactId>worldwindx</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.jogamp.gluegen</groupId>
<artifactId>gluegen-rt-main</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.jogamp.jogl</groupId>
<artifactId>jogl-all-main</artifactId>
<version>2.2.4</version>
</dependency>

为了完全彻底,这里是代码导入:

import gov.nasa.worldwind.geom.Intersection;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Line;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Sphere;
import gov.nasa.worldwind.geom.Vec4;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.render.Path;
import gov.nasa.worldwindx.examples.ApplicationTemplate;
import static gov.nasa.worldwindx.examples.ApplicationTemplate.insertBeforeCompass;
import java.awt.Dimension;
import java.util.Arrays;

最佳答案

如果您查看 Sphere#intersect() 的实现,您会发现坐标线以球体原点(而非地球原点)为中心,这几乎可以肯定是一个错误。你应该能够做到:

final Vec4 pa = lineStartsAsVec.subtract3(sphereCenter);
final Vec4 pb = lineEndsAsVec.subtract3(sphereCenter);
final Line asLine2 = Line.fromSegment(pa, pb);
final Intersection[] intersection = sphere.intersect(asLine2);

请记住,返回的交点仍在以球体原点为中心的笛卡尔坐标系中,因此要将它们转换回世界风笛卡尔坐标系,您需要执行以下操作:

final Vec4 intersectionPos = intersection[0].getIntersectionPoint().add3(sphereCenter);

该实现还认为直线是无限长的,因此它将返回两个点,而不是一个。

实现您自己的 intersect() 版本会非常直接,该版本在正常坐标下工作并考虑到线的长度,请参阅 here .

关于java - WorldWind 球线相交错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35392853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com