Board index FlightGear Development

direction vector for line intersection

FlightGear is opensource, so you can be the developer. In the need for help on anything? We are here to help you.
Forum rules
Core development is discussed on the official FlightGear-Devel development mailing list.

Bugs can be reported in the bug tracker.

direction vector for line intersection

Postby wolfi08911 » Wed Sep 05, 2012 1:42 pm

Hi forum,

I'm struggling with an intersection question.

From my ac position I'll try to catch the first terrain intersection
hit. My ray is defined by azimuth/elevation angles.

What I like to use is the handy get_cart_ground_intersection() method
but I found no example to setup the correct direction vector from
my angles.

The example direction is hitting the surface at the correct longitude
but latitude is about 1km off. I need some geo transformation but
can't find the right example.

Thanks
Wolfgang R.

Code: Select all
static int terrain_service_calculate(struct terrain_interrogation_res_t * response)
{
  SGGeod startLoc = SGGeod::fromRadM(response->view_lon_rad, response->view_lat_rad, response->view_altitude_m);
  SGGeod hitpos;
  SGVec3d SGstart = SGVec3d::fromGeod(startLoc);
  SGVec3d direction(0,0,-1);
  SGVec3d nearestHit;
  bool ret;

  SG_LOG(SG_TERRAIN, SG_WARN, "Intersection start     [SGGeod] : " << startLoc);
  SG_LOG(SG_TERRAIN, SG_WARN, "Intersection start     [SGVec3d]: " << SGstart);
  SG_LOG(SG_TERRAIN, SG_WARN, "Intersection direction [SGVec3d]: " << direction);
           
  ret = globals->get_scenery()->get_cart_ground_intersection(SGstart, direction, nearestHit);
 
  if(ret)
  {
    SG_LOG(SG_TERRAIN, SG_WARN, "hit");
    SGGeodesy::SGCartToGeod(nearestHit, hitpos);
    SG_LOG(SG_TERRAIN, SG_WARN, "Intersection nearestHit [SGVec3d]: " << nearestHit);
    SG_LOG(SG_TERRAIN, SG_WARN, "Intersection hitpos     [SGGeod] : " << hitpos);
  }
  else
  {
    SG_LOG(SG_TERRAIN, SG_WARN, "no hit");
  }
  ...
wolfi08911
 
Posts: 11
Joined: Tue Jun 14, 2011 7:14 am

Re: direction vector for line intersection

Postby Hooray » Wed Sep 05, 2012 1:58 pm

I'd suggest to take a look at some of the other places in the source code where similar things are done:

https://gitorious.org/fg/flightgear/blo ... xx#line287

Probably, other instruments should contain more examples, such as the gpws, wxradar - basically anything that needs to sample the terrain and do intersection testing.

More examples can be found by grep'ing the source tree: find . -type f -print0 | xargs -0 grep -n "get_cart_ground_intersection"


./src/Instrumentation/rad_alt.cxx:154: globals->get_scenery()->get_cart_ground_intersection(cartantennapos, uservec, nearestHit);
./src/Instrumentation/agradar.cxx:287: globals->get_scenery()->get_cart_ground_intersection(cartantennapos, uservec, nearestHit);
./src/Scenery/scenery.hxx:101: bool get_cart_ground_intersection(const SGVec3d& start, const SGVec3d& dir,
./src/Scenery/scenery.cxx:301:FGScenery::get_cart_ground_intersection(const SGVec3d& pos, const SGVec3d& dir,
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 11317
Joined: Tue Mar 25, 2008 8:40 am

Re: direction vector for line intersection

Postby wolfi08911 » Wed Sep 05, 2012 2:52 pm

Hi Hooray,

thanks for the quick response.

I came along agradar.cxx and did try to use it. But it was for now beyond
my understanding of the coordinate systems. If this is the most simple
example I'll give it another try. But yes this one should do it for me
somehow.

Thanks
wolfi08911
 
Posts: 11
Joined: Tue Jun 14, 2011 7:14 am

Re: direction vector for line intersection

Postby wolfi08911 » Thu Sep 06, 2012 3:38 pm

I've run the agradar.cxx transformation code stand-alone and got the following result.

Input
altitude 3000m
lat 29deg
lon 17deg
elevation angle -90deg (all others 0deg)

Output
the lat/lon of the hitposition (sea level) is about 15m/15m away from the Input lat/lon

Should I assume that this is related to rounding issues or do I miss something?

Thanks
wolfi08911
 
Posts: 11
Joined: Tue Jun 14, 2011 7:14 am

Re: direction vector for line intersection

Postby Alant » Fri Aug 21, 2015 9:50 pm

An old thread, but I have a similar need so have posted a question on the devel email list.

Did you ever resolve your problem?

I have tried using very high speed submodels and looking for their impact. This seems an unnecessarily complicated way to do things - and the results so far are not that promising.

Alan
Alant
 
Posts: 848
Joined: Wed Jun 23, 2010 5:58 am
Location: Portugal
Callsign: Tarnish99
Version: from Git
OS: Windows 10

Re: direction vector for line intersection

Postby Thorsten » Sat Aug 22, 2015 5:51 am

It's generally tough, and depends a bit on the accuracy you need.

The urban shader has two algorithms for line/heightmap intersection implemented, which could be adapted to a Nasal code sampling terrain mesh.

One of them sub-divides the ray into n intervals, and then uses a linear search to find an intersection and then does a binary subdivision for the first found candidate to refine the answer. I haven't looked much at the other.

You can possibly make it faster by already having information on the terrain, like knowing that the highest elevation above the mean is 1000 m - this would allow you to restrict in which part of the ray an intersection can occur at all. The general problem is that what the best algorithm is really depends on how the terrain looks like.

(The answer for the mesh is sort of trivial inside the fragment shader since it's just fragment depth, but you can't get that back into the property tree unfortunately...)
Thorsten
 
Posts: 10634
Joined: Mon Nov 02, 2009 8:33 am

Re: direction vector for line intersection

Postby Alant » Sat Aug 22, 2015 12:52 pm

I am sure that you are correct, and to me it seems that as I want the real world co-ordinates of a screen pixel, looking inside the video card's private world makes sense.
However,to do this some detailed knowlege of OpenGL, shaders etc might b required an I do not know if my aged brain will be able to absorb this.

Alan
Alant
 
Posts: 848
Joined: Wed Jun 23, 2010 5:58 am
Location: Portugal
Callsign: Tarnish99
Version: from Git
OS: Windows 10


Return to Development

Who is online

Users browsing this forum: No registered users and 1 guest