pioneerspacesim/pioneer — pull request #1476 by Ae-2222 (2012-09-03 12:46:25+00:00)
Title: Region based terrain control

This pull request implements region based terrain control. As described in detail in issue #924 and outlined in one of the posts in the [wiki](https://github.com/pioneerspacesim/pioneer/wiki/Discussion.Cities-and-Terrain) (including extensions). Closes #7.
* Development of large city buildings of any size (many of km) possible without risk of portions being buried.
* Starports on hilly terrain will no longer be buried preventing crashes.

**Algorithm:**
* A fast dot product is used to identify a measure of proximity to the city center (starport location).This encloses a roughly circular area around the starport.
* Once identified, the measure of distance and the region-less height, combined with region implementation data consisting of the target height, statistical measure of height variance in the neighbourhood, and inner and outer limits of the region, is used to calculate a new height.
* The new height,currently, describes a height region whose dynamic range is compressed using an s-curve with a compression parameter based on the neighbourhood height variation.
* Height is blended between the compressed and normal height as the outer limit approaches.
* Each terrain class steps through the child system bodies of the planet and builds region data. This is done in the constructor using Terrain::InitCityRegionType. Regions are re-innited if a starport is moved in Space::MakeFrameFor.

**Considerations:**
* The colour is partly based on the normals, so for flat terrains near cities it is usually a slightly darker/lighter shade. The blending at the edges makes it fairly seamless.

* Currently there is some variation in height to make it look natural, so small buildings in particular might be slightly buried.

**Notes:**
* There will likely be multiple exchanges in communication between city and terrain code in the future. Currently the city code uses pristine terrain followed by the terrain code modifying itself based on city stages. Cities will need to be initialised to some extent during body generation as some city parameters of all on demand.

**Future extensions:**
* As described in #924. New region types and effects can be added trivially. Colour regions can also be used. It's possible to have flat or texture based height/colour. Spatial partitioning will allow a lot of regions, one for each building if needed..

Phobos: Note blending causes a raised area down-slope and a caved in area up-slope.
![](http://i.imgur.com/OQMAs.png)

Earth:
![](http://i.imgur.com/mqylB.png)

Further images of earlier code showing terrain with and without regions: http://imgur.com/a/EoFJT