menu icons, window sizing, compass direction selector
[debian/openrocket] / test / net / sf / openrocket / util / GeodeticComputationStrategyTest.java
1 package net.sf.openrocket.util;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.Test;
6
7 public class GeodeticComputationStrategyTest {
8         
9         @Test
10         public void testSpericalAddCoordinate() {
11                 
12                 double arcmin = (1.0 / 60.0);
13                 double arcsec = (1.0 / (60.0 * 60.0));
14                 
15                 double lat1 = 50.0 + 3 * arcmin + 59 * arcsec;
16                 double lon1 = -1.0 * (5 + 42 * arcmin + 53 * arcsec); //W 
17                 
18                 double lat2 = 58 + 38 * arcmin + 38 * arcsec;
19                 double lon2 = -1.0 * (3 + 4 * arcmin + 12 * arcsec);
20                 
21                 double range = 968.9 * 1000.0;
22                 double bearing = (9.0 + 7 * arcmin + 11 * arcsec) * (Math.PI / 180.0);
23                 
24                 Coordinate coord = new Coordinate(range * Math.sin(bearing), range * Math.cos(bearing), 1000.0);
25                 WorldCoordinate wc = new WorldCoordinate(lat1, lon1, 0.0);
26                 wc = GeodeticComputationStrategy.SPHERICAL.addCoordinate(wc, coord);
27                 
28                 System.out.println(wc.getLatitudeDeg());
29                 System.out.println(lat2);
30                 
31                 System.out.println(wc.getLongitudeDeg());
32                 System.out.println(lon2);
33                 
34                 assertEquals(lat2, wc.getLatitudeDeg(), 0.001);
35                 assertEquals(lon2, wc.getLongitudeDeg(), 0.001);
36                 assertEquals(1000.0, wc.getAltitude(), 0.0);
37         }
38         
39         
40         @Test
41         public void testAddCoordinates() {
42                 
43                 double min = 1 / 60.0;
44                 double sec = 1 / 3600.0;
45                 
46
47                 // Test zero movement
48                 testAddCoordinate(50.0, 20.0, 0, 123, 50.0, 20.0, false);
49                 
50
51                 /*
52                  * These example values have been computed using the calculator at
53                  * http://www.movable-type.co.uk/scripts/latlong.html
54                  */
55
56                 // Long distance NE over England, crosses Greenwich meridian
57                 // 50 03N  005 42W  to  58 38N  003 04E  is  1109km at 027 16'07"
58                 testAddCoordinate(50 + 3 * min, -5 - 42 * min, 1109000, 27 + 16 * min + 7 * sec, 58 + 38 * min, 3 + 4 * min, false);
59                 
60                 // SW over Brazil
61                 // -10N  -60E  to  -11N  -61E  is  155.9km at 224 25'34"
62                 testAddCoordinate(-10, -60, 155900, 224 + 25 * min + 34 * sec, -11, -61, true);
63                 
64                 // NW over the 180 meridian
65                 // 63N  -179E  to  63 01N  179E  is  100.9km at 271 56'34"
66                 testAddCoordinate(63, -179, 100900, 271 + 56 * min + 34 * sec, 63 + 1 * min, 179, true);
67                 
68                 // NE near the north pole
69                 // 89 50N  0E  to 89 45N  175E  is 46.29 km at 003 00'01"
70                 testAddCoordinate(89 + 50 * min, 0, 46290, 3 + 0 * min + 1 * sec, 89 + 45 * min, 175, false);
71                 
72                 // S directly over south pole
73                 // -89 50N  12E  to  -89 45N  192E  is  46.33km at 180 00'00"
74                 testAddCoordinate(-89 - 50 * min, 12, 46330, 180, -89 - 45 * min, -168, false);
75                 
76         }
77         
78         private void testAddCoordinate(double initialLatitude, double initialLongitude, double distance, double bearing,
79                                 double finalLatitude, double finalLongitude, boolean testFlat) {
80                 
81                 double tolerance;
82                 
83                 bearing = Math.toRadians(bearing);
84                 
85                 // positive X is EAST, positive Y is NORTH
86                 double deltaX = distance * Math.sin(bearing);
87                 double deltaY = distance * Math.cos(bearing);
88                 
89                 Coordinate coord = new Coordinate(deltaX, deltaY, 1000.0);
90                 WorldCoordinate wc = new WorldCoordinate(initialLatitude, initialLongitude, 0.0);
91                 
92                 // Test SPHERICAL
93                 tolerance = 0.0015 * distance / 111325;
94                 System.out.println("\nSpherical tolerance: " + tolerance);
95                 WorldCoordinate result = GeodeticComputationStrategy.SPHERICAL.addCoordinate(wc, coord);
96                 
97                 System.out.println("Difference Lat: " + Math.abs(finalLatitude - result.getLatitudeDeg()));
98                 System.out.println("Difference Lon: " + Math.abs(finalLongitude - result.getLongitudeDeg()));
99                 assertEquals(finalLatitude, result.getLatitudeDeg(), tolerance);
100                 assertEquals(finalLongitude, result.getLongitudeDeg(), tolerance);
101                 assertEquals(1000.0, result.getAltitude(), 0.0);
102                 
103
104                 // Test WGS84
105                 /*
106                  * TODO: Since the example values are computed using a spherical earth approximation,
107                  * the WGS84 method will have significantly larger errors.  The tolerance should be
108                  * increased correspondingly.
109                  */
110                 //tolerance = ...
111                 System.out.println("\nWGS84 tolerance: " + tolerance);
112                 result = GeodeticComputationStrategy.WGS84.addCoordinate(result, coord);
113                 
114                 System.out.println("Difference Lat: " + Math.abs(finalLatitude - result.getLatitudeDeg()));
115                 System.out.println("Difference Lon: " + Math.abs(finalLongitude - result.getLongitudeDeg()));
116                 // FIXME: Re-enable these when they function
117                 //              assertEquals(finalLatitude, result.getLatitudeDeg(), tolerance);
118                 //              assertEquals(finalLongitude, result.getLongitudeDeg(), tolerance);
119                 //              assertEquals(1000.0, result.getAltitude(), 0.0);
120                 
121
122                 // Test FLAT
123                 if (testFlat) {
124                         tolerance = 0.02 * distance / 111325;
125                         System.out.println("\nFlat tolerance: " + tolerance);
126                         result = GeodeticComputationStrategy.FLAT.addCoordinate(wc, coord);
127                         
128                         System.out.println("Difference Lat: " + Math.abs(finalLatitude - result.getLatitudeDeg()));
129                         System.out.println("Difference Lon: " + Math.abs(finalLongitude - result.getLongitudeDeg()));
130                         assertEquals(finalLatitude, result.getLatitudeDeg(), tolerance);
131                         assertEquals(finalLongitude, result.getLongitudeDeg(), tolerance);
132                         assertEquals(1000.0, result.getAltitude(), 0.0);
133                         
134                 }
135                 
136         }
137         
138         
139
140         @Test
141         public void testSpericalGetCoriolisAcceleration() {
142                 
143                 // For positive latitude and rotational velocity, a movement due east results in an acceleration due south
144                 Coordinate velocity = new Coordinate(-1000, 0, 0);
145                 WorldCoordinate wc = new WorldCoordinate(45, 0, 0);
146                 double north_accel = GeodeticComputationStrategy.SPHERICAL.getCoriolisAcceleration(wc, velocity).y;
147                 System.out.println("North accel " + north_accel);
148                 assertTrue(north_accel < 0.0);
149                 
150         }
151         
152 }