Board index FlightGear Development New features

Property rule based road traffic

Discussion and requests for new features. Please note that FlightGear developers are volunteers and may or may not be able to consider these requests.

Re: Property rule based road traffic

Postby Hooray » Mon Jan 29, 2018 9:13 am

I think so, too - that's also why I was asking why property rules are being used, I would not expect Nasal to show up much actually.
To be honest, I believe that the marshaling overhead coming from using the property tree to get/set 1000+ properties may be more significant - unless the property rules directly update the models, too ? (possibly via aliasing)
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Marius_A » Mon Jan 29, 2018 12:53 pm

1190 vehicle computations using property rules: 5.5 .. 6 ms. Models are updated via aliasing.


438 vehicle computations using my Nasal code: 20 ms. Updating 438 models: 10 ms.

Average computation time for one vehicle is 4.3E-05 s:
Code: Select all
table_benchmarking.start();
            foreach (var k; ["lat", "lon", "alt", "hdg"])
               _v[k] = table(_trk.progress, _trk[k], _v.progress);
table_benchmarking.stop();


Code: Select all
var linterp = func (x0, y0, x1, y1, x) {
   return y0 + (y1 - y0) * (x - x0) / (x1 - x0); #linear interpolation
}

var table = func (x_vec, y_vec, x) {
   var i = 0;
   forindex(i; x_vec)
      if (x < x_vec[i])
         break;
   return linterp (x_vec[i-1], y_vec[i-1], x_vec[i], y_vec[i], x);
}



Code: Select all
var benchmark = {
   new: func(path, samples = 30) {
      return {
         parents : [benchmark],
         i       : 0,
         path    : path,
         dt_sum  : 0,
         t0      : nil,
         s       : samples,
      };
   },

   start: func me.t0 = systime(),

   stop: func {
      me.dt_sum += systime() - me.t0;
      if ((me.i += 1) == me.s) {
         setprop(me.path, me.dt_sum / me.i);
         me.dt_sum = 0;
         me.i = 0;
      };
   },
};

var compute_benchmarking = benchmark.new("/aaa/computations-time-sec");
var table_benchmarking = benchmark.new("/aaa/table-time-sec", 10000);
Marius_A
 
Posts: 89
Joined: Wed Dec 04, 2013 2:20 pm

Re: Property rule based road traffic

Postby Hooray » Mon Jan 29, 2018 2:33 pm

Okay, that's looking pretty good actually, just for the sake of completeness: there's a dedicated interpolate() API available - even though I believe that C++ (property rules) and aliasing are hard to beat in terms of reducing property I/O: http://wiki.flightgear.org/Nasal_librar ... late.28.29

At this point, I don't believe that threading out stuff will be worth the hassle, just like Thorsten said.
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Marius_A » Mon Jan 29, 2018 3:05 pm

Using this code, the delay (438 vehicles) is 9 ms:
Code: Select all
var multithreading = 1;

var _computing = 0;
var compute = func {_computing = 1; calc_vehicle_locations(); _computing = 0}

var update = func {foreach (var a; Vehicles) a.update(); update_dt()}

var loop = func {
   compute_benchmarking.start();
   if (!_computing) {
      update();
      if (multithreading)
         thread.newthread( compute );
      else {
         compute();
      }
   }
   compute_benchmarking.stop();
}

var timer = maketimer(0.0, loop);
timer.start();


I think that it's worth trying to use only ~100 models and constantly move them to the field of view. Maybe that would create the impression of the infinite number of cars?
Marius_A
 
Posts: 89
Joined: Wed Dec 04, 2013 2:20 pm

Re: Property rule based road traffic

Postby Hooray » Mon Jan 29, 2018 3:39 pm

Note thought that you must be careful not to have the timer-based loop run more than once - or you may be starting multiple threads, which is going to make things fragile.
You may also want to look at Nasal's support for locking: https://github.com/andyross/nasal/blob/ ... lib.c#L101

One could probably push objects (vehicles) into a few different queues, to prioritize updates according to distance to the view/viewer, and update those first - equally, those object that are hardly visible don't need a fast update either I suppose ?

Basically, this is about a simple LOD-scheme - e.g. traffic far away could even be using Thorsten's Traffic Shader, whereas traffic near-by could possibly even show animated models
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Marius_A » Tue Jan 30, 2018 8:16 pm

This is Nasal based approach (thanks for the recommendations). Number of vehicles: 530+. Main loop time: ~1 ms.


Currently, I can load up to 1300+ vehicles.
Marius_A
 
Posts: 89
Joined: Wed Dec 04, 2013 2:20 pm

Re: Property rule based road traffic

Postby wkitty42 » Tue Jan 30, 2018 8:34 pm

the really fun part is gong to be how to get them to actually go over the bridges and under the overpasses ;)
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 5759
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: Property rule based road traffic

Postby Hooray » Tue Jan 30, 2018 8:59 pm

Hey, that's really impressive !
Not sure if you're ready to share your code yet or not - but we might be able to get out a little more/better performance (or maybe even just more vehicles for the same runtime footprint) - for example, there are some low-level Nasal optimizations that we can do, because the Nasal code generator is pretty dumb actually. But I haven't done any actual profiling/benchmarking yet, so this would be mainly about suggesting different coding constructs and benchmarking those to see if/how they perform better or worse.

I do think that a simple tile/LOD-scheme based on viewer position, orientation, FOV and groundspeed might be worthwhile to explore (maybe in conjunction with relative speed/movement direction of the generated traffic) - I believe Thorsten's Advandced Weather system was/is using a 3x3 grid of "tiles" to create a cache-based system around the position of the current aircraft - something like that should work also fairly well here I suppose: http://wiki.flightgear.org/Weather#Weather_tiles

To be honest, even in the current state it's funny to see that your scripted approach works obviously better than the hard-coded random buildings system in the video, because the latter can be seen adding/removing models at runtime, whereas I didn't spot anything weird going on with the generated road traffic.

Regarding wkitty42's suggestion, I believe that this would be mainly a matter of having better 2D vector data, probably including a 3rd (elevation) component -possibly involving OSM data (the background thread approach would also work for pre-computing such tables asynchronously, e.g. by loading such data from disk).

Anyway, I do think it would be a good idea to structure the whole thing as an addon and get this committed to fgaddon (provided it will be GPL?) I am sure you could get Thorsten involved to help with this, because he's been working on the Traffic Shader anyway - and who knows, maybe we can find a way to integrate these two approaches ?
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Thorsten » Wed Jan 31, 2018 6:37 am

AW still has a quadtree lookup code somewhere in (which is just by-passed now) dating back to the time when clouds were still moved in Nasal - this was moderately tricky to write, so it might help you designing your own code to focus on what's in the field of view. Otherwise the '3x3 tile' concept of AW would work for this problem as well as Hooray says.

If you're interested, there's also traffic density table supplied by FG which is parametrized real-world data as a function of daytime. Also, in the shader itself is code to compute the vehicle speed as function of road type and traffic density (aka, a highway can have a traffic jam) and these rules could easily be adapted for the model solution as well.

I don't think it's feasible to put the vehicle 'on top' of the shader-generated traffic (that would be nice because head and tail lights would come for free), but unfortunately the precise outcome of pseudo-random function seems to depend on GPU architecture - but we can suppress shader-generated road traffic within a certain radius and fade it in beyond that.

Let me know if you need pointers/explanations for any of these.
Thorsten
 
Posts: 11190
Joined: Mon Nov 02, 2009 8:33 am

Re: Property rule based road traffic

Postby Hooray » Wed Jan 31, 2018 11:48 am

To be honest, I would suggest not to over-optimize things at this point, or we will be introducing more and more hard-coded assumptions about the structure of the traffic, which would complicate matters if/when people may want to use other types of traffic/road networks (think OSM data).
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Thorsten » Wed Jan 31, 2018 2:38 pm

It's not about optimizing but about visual continuity - which is going to bite you hard if it isn't there.
Thorsten
 
Posts: 11190
Joined: Mon Nov 02, 2009 8:33 am

Re: Property rule based road traffic

Postby Hooray » Wed Jan 31, 2018 3:29 pm

not disagreeing, but even in its current form, this can only be as good as the underlying road network data (referring to wkitty42's point about bridges and tunnels)

I don't think it's feasible to put the vehicle 'on top' of the shader-generated traffic (that would be nice because head and tail lights would come for free), but unfortunately the precise outcome of pseudo-random function seems to depend on GPU architecture - but we can suppress shader-generated road traffic within a certain radius and fade it in beyond that.

question: would it be possible (aka: make any sense) to texture parts of the vehicles/objects using the traffic light shader (head/tail lights) ?
My assumption here is that we're able to add effects to certain 3D objects, e.g. navigation lights (think aircraft/vehicles), so that this might be a simple first step to start integrating these two systems - at the mere cost of having to specify the surfaces that are to be handled by the effect/shader, what do you think ?
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Marius_A » Mon Feb 05, 2018 2:25 pm

the really fun part is gong to be how to get them to actually go over the bridges and under the overpasses


Golden Gate Bridge:



Roller coasters might be possible too :)


Current version of the code: https://drive.google.com/file/d/1qF5HSIiNwEp2kVynHis47rfZweKskCUf/view?usp=sharing. It will be GPL.

Usage:
Extract and copy folder ground_traffic into flightgear Nasal/ folder. The script should load the traffic if it is available in viewer position.
Currently, I have generated traffic around EYKS airfield and on the Golden Gate Bridge.
Marius_A
 
Posts: 89
Joined: Wed Dec 04, 2013 2:20 pm

Re: Property rule based road traffic

Postby Hooray » Tue Feb 06, 2018 5:51 pm

Your code looks really clean to me - apparently, you are not even using the AI subsystem for any of this but just the model manager, right ?
It would be awesome if we could turn this into an addon that can be added to FGAddons
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: 11375
Joined: Tue Mar 25, 2008 8:40 am

Re: Property rule based road traffic

Postby Marius_A » Tue Feb 06, 2018 7:55 pm

[...] apparently, you are not even using the AI subsystem for any of this but just the model manager, right ?


The models are driven by Nasal. Vehicles (models) are moving along 3d polyline. When a car is at the end of the curve, it is re-positioned to the beginning of this curve. Data points for the polylines are loaded from the disc according to viewer position.
Marius_A
 
Posts: 89
Joined: Wed Dec 04, 2013 2:20 pm

PreviousNext

Return to New features

Who is online

Users browsing this forum: AhrefsBot [Bot] and 1 guest