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 2: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 8:14 am

Re: direction vector for line intersection

Postby Hooray » Wed Sep 05, 2012 2: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: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: direction vector for line intersection

Postby wolfi08911 » Wed Sep 05, 2012 3: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 8:14 am

Re: direction vector for line intersection

Postby wolfi08911 » Thu Sep 06, 2012 4: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 8:14 am

Re: direction vector for line intersection

Postby Alant » Fri Aug 21, 2015 10: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: 1219
Joined: Wed Jun 23, 2010 6:58 am
Location: Portugal
Callsign: Tarnish99
Version: latest Git
OS: Windows 10/11

Re: direction vector for line intersection

Postby Thorsten » Sat Aug 22, 2015 6: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: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: direction vector for line intersection

Postby Alant » Sat Aug 22, 2015 1: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: 1219
Joined: Wed Jun 23, 2010 6:58 am
Location: Portugal
Callsign: Tarnish99
Version: latest Git
OS: Windows 10/11

Problem with get_cart_ground_intersection()

Postby Amirreza Shakeri » Sat Oct 19, 2019 5:54 pm

Hello.

I tried 'get_cart_ground_intersection()' function in just one direction (z). i expected to obtain the result equal to AGL (height Above Ground Level), which is available in property tree. but there is a small difference which increases as altitude increases.

Does anyone know the reason !!!??

thanks in Advance.
Amirreza Shakeri
 
Posts: 8
Joined: Mon Aug 26, 2019 6:55 pm

Re: direction vector for line intersection

Postby Necolatis » Tue Oct 22, 2019 10:16 am

Z is not downwards except for at one specific place on the earth.

X,Y,Z is an coordinate system that is centered on the planet, its not centered on your aircraft.

So, would you be interested in the solution in Nasal code, or are you only interested in C++?
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2232
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: direction vector for line intersection

Postby Amirreza Shakeri » Fri Oct 25, 2019 11:24 am

Thanks @Necolatis for your response.
I'm interested in nasal coding.
Amirreza Shakeri
 
Posts: 8
Joined: Mon Aug 26, 2019 6:55 pm

Re: direction vector for line intersection

Postby Necolatis » Fri Oct 25, 2019 3:28 pm

Untested, but something like this should work if I understood your question correct:

Code: Select all
var heading = 45;# true heading from aircraft in degrees
var pitch   = -8;# absolute pitch from aircraft in degrees, -89.9999 to 89.9999

var from = geo.aircraft_position();
var to = geo.Coord.new(from);
to.apply_course_distance(heading,1000);# randomly chose 1Km away, don't make too short or maybe run into floating point precision issues.
to.set_alt(to.alt()+1000*math.tan(pitch*D2R));
var xyz = {"x":from.x(),         "y":from.y(),        "z":from.z()};
var dir = {"x":to.x()-from.x(),  "y":to.y()-from.y(), "z":to.z()-from.z()};
var v = get_cart_ground_intersection(me.xyz, me.dir);
if (v != nil) {
    print("lat: "~v.lat);
    print("lon: "~v.lon);
    print("alt: "~v.elevation*M2FT);
} else {
   print("no terrain or terrain not loaded yet at intersection!");
}
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2232
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10


Return to Development

Who is online

Users browsing this forum: No registered users and 4 guests