It is a common issue to be able to find out where in the world does a line intersect the terrain surface. The
WorldWind Java SDK allows to resolve the intersection from the eye point of view using the
pick process - based on drawing objects in unique colors, but that does not help much when you need to compute such intersections from another perspective, like for line of sight calculations.
Line of sight calculation from the top of Mount Jackson (3064m),
Glacier National Parc, Montana, USA.
Here is a helper class to answer such questions, along with an example application that will display areas from where you can see a central point.
Download
RayCastingSupport.java and
LineOfSight.java - remove the .txt extensions, save the first one in 'util' and the application in 'examples'. You may also need the
crosshair layer.
Update april 18: there appears to be two dependencies on the not yet published SDK version: centerPosition calculation and PatternFactory.blur() - see this post comments. You can simply comment out the blur, or replace it with a
ConvolveOp, and change the center position line with:
Position centerPosition =
RayCastingSupport.intersectRayWithTerrain(globe,
view.getEyePoint(), view.getForwardVector(), 30, 3);
Ray casting - figure 1
The ray casting code samples points at regular intervals along the 'ray' until it finds one which elevation is below ground - figure 1.1. It then recursively resamples the last segment every tenth of the previous sample length - fig 1.2, until that sampling step is smaller then or equal to the required precision.
The longer the initial sample length, the quicker an intersection will be found - 1, and the sub sampling process will start narrowing onto the terrain - 2. However, the larger the value the less accuracy you will get since the ray will be able to go through some surface features in just one step and completely miss them. The default values for sampling length and precision are 100 meters and 10 meters and can be overrided in the methods arguments.
Note that the example application also shows how to 'paint' both on the terrain and on the screen using BufferedImage, Java 2D, SurfaceImage and ScreenAnnotation classes.