create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / models / atmosphere / InterpolatingAtmosphericModel.java
1 package net.sf.openrocket.models.atmosphere;
2
3 /**
4  * An abstract atmospheric model that pre-computes the conditions on a number of layers
5  * and later linearly interpolates the values from between these layers.
6  * 
7  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
8  */
9 public abstract class InterpolatingAtmosphericModel implements AtmosphericModel {
10         /** Layer thickness of interpolated altitude. */
11         private static final double DELTA = 500;
12         
13         private AtmosphericConditions[] levels = null;
14         
15         
16         public AtmosphericConditions getConditions(double altitude) {
17                 if (levels == null)
18                         computeLayers();
19                 
20                 if (altitude <= 0)
21                         return levels[0];
22                 if (altitude >= DELTA * (levels.length - 1))
23                         return levels[levels.length - 1];
24                 
25                 int n = (int) (altitude / DELTA);
26                 double d = (altitude - n * DELTA) / DELTA;
27                 AtmosphericConditions c = new AtmosphericConditions();
28                 c.setTemperature(levels[n].getTemperature() * (1 - d) + levels[n + 1].getTemperature() * d);
29                 c.setPressure(levels[n].getPressure() * (1 - d) + levels[n + 1].getPressure() * d);
30                 
31                 return c;
32         }
33         
34         
35         private void computeLayers() {
36                 double max = getMaxAltitude();
37                 int n = (int) (max / DELTA) + 1;
38                 levels = new AtmosphericConditions[n];
39                 for (int i = 0; i < n; i++) {
40                         levels[i] = getExactConditions(i * DELTA);
41                 }
42         }
43         
44         
45         protected abstract double getMaxAltitude();
46         
47         protected abstract AtmosphericConditions getExactConditions(double altitude);
48 }