OrbitePointsTest.java
001 package fr.isae.orbit;
002 
003 import fr.isae.geometry.Point;
004 
005 import org.junit.*;
006 import static org.junit.Assert.*;
007 
008 // those imports are only useful for advanced stuff!
009 import static org.junit.Assume.assumeTrue;
010 import org.junit.experimental.theories.*;
011 import org.junit.runner.RunWith;
012 
013 /**
014  * Unit Test for class OrbitePoints.
015  *
016  * Created: Tue Sep 17 16:17:42 2013
017  *
018  @author <a href="mailto:christophe.garion@isae.fr">Christophe Garion</a>
019  @version 1.0
020  */
021 // @RunWith is only used for advanced stuff!
022 @RunWith(Theories.class)
023 public class OrbitePointsTest {
024 
025     private Orbite gps;
026     private Orbite gpsf;
027     private Orbite exosat;
028     private Orbite exosatf;
029     private OrbitePoints gpsp;
030     private OrbitePoints gpsfp;
031     private OrbitePoints exosatp;
032     private OrbitePoints exosatfp;
033 
034     private static final double EPS = 1E-6;
035     private static final double STEP = 1E-3;
036 
037     // do not bother about the @DataPoint annotation:
038     // - it is advanced stuff
039     // - it is explained in the written solution if you really
040     //   want to understand what it means
041     @DataPoint public static OrbitePoints gpspt =
042         new OrbitePoints(new Orbite(0.026E6), STEP);
043     @DataPoint public static OrbitePoints gpsfpt =
044         new OrbitePoints(new Orbite(0.026E6, new Point(2E6, -5E3)), STEP);
045     @DataPoint public static OrbitePoints exosatpt =
046         new OrbitePoints(new Orbite(0.93100E6), STEP);
047     @DataPoint public static OrbitePoints exosatfpt =
048         new OrbitePoints(new Orbite(0.93100E6, new Point(-50E6, 1E7)), STEP);
049 
050     /**
051      * Setup for the tests.
052      */
053     @Before public void setUp() {
054         this.gps = new Orbite(0.026E6);
055         this.gpsf = new Orbite(0.026E6, new Point(2E6, -5E3));
056         this.exosat = new Orbite(0.93100E6);
057         this.exosatf = new Orbite(0.93100E6, new Point(-50E6, 1E7));
058         this.gpsp = new OrbitePoints(this.gps, STEP);
059         this.gpsfp = new OrbitePoints(this.gpsf, STEP);
060         this.exosatp = new OrbitePoints(this.exosat, STEP);
061         this.exosatfp = new OrbitePoints(this.exosatf, STEP);
062     }
063 
064     /**
065      * Cleanup for the tests.
066      */
067     @After public void tearDown() {
068 
069     }
070 
071     /**
072      * Verifying that the center of orbits is correct.
073      */
074     @Test public void testOrbitCenter() {
075         testCenter(this.gpsp, this.gps);
076         testCenter(this.gpsfp, this.gpsf);
077         testCenter(this.exosatp, this.exosat);
078         testCenter(this.exosatfp, this.exosatf);
079     }
080 
081     private void testCenter(OrbitePoints op, Orbite o) {
082         Point c = o.getFoyer();
083         c.translater(-o.getC()0.0);
084 
085         assertTrue(c.equals(op.getCentre()));
086     }
087 
088     /**
089      * Test if the number of points is correct.
090      */
091     @Test public void testOrbitNbPoints() {
092         testNbPoints(this.gpsp);
093         testNbPoints(this.gpsfp);
094         testNbPoints(this.exosatp);
095         testNbPoints(this.exosatfp);
096     }
097 
098     private void testNbPoints(OrbitePoints o) {
099         assertEquals((int) (2.0 * Math.PI / STEP1, o.getNbPoints());
100     }
101 
102     /**
103      * Test orbit circonference using an approximation
104      * (see http://en.wikipedia.org/wiki/Ellipse#Equations).
105      */
106     @Test public void testOrbitCirconference() {
107         testCirconference(this.gpsp, this.gps);
108         testCirconference(this.gpsfp, this.gpsf);
109         testCirconference(this.exosatp, this.exosat);
110         testCirconference(this.exosatfp, this.exosatf);
111     }
112 
113     private void testCirconference(OrbitePoints op, Orbite o) {
114         double a = o.getA();
115         double b = o.getB();
116         double h = (Math.pow(a - b, 2)) (Math.pow(a + b, 2));
117 
118         double c = Math.PI * (a + b(1.0 (3.0 * h(10 + Math.sqrt(4.0 3.0 * h)));
119 
120         assertEquals(c, op.getCirconference(), o.getA() 1E5);
121     }
122 
123     /**
124      * Test equals method for orbits that should be equal.
125      */
126     @Test public void testOrbitEquals() {
127         testEquals(this.gps);
128         testEquals(this.gpsf);
129         testEquals(this.exosat);
130         testEquals(this.exosatf);
131     }
132 
133     private void testEquals(Orbite o) {
134         OrbitePoints o1 = new OrbitePoints(o, STEP);
135         OrbitePoints o2 = new OrbitePoints(o, STEP);
136 
137         assertTrue(o1.equals(o2));
138         assertTrue(o2.equals(o1));
139     }
140 
141     /**
142      * Test all possible combinations for non-equals orbits.
143      *
144      @param o1 an orbit to use for combinations
145      @param o2 an orbit to use for combinations
146      */
147     @Theory public void testNotEquals(OrbitePoints o1, OrbitePoints o2) {
148         assumeTrue(o1 != o2);
149 
150         assertFalse(o1.equals(o2));
151     }
152 
153     /**
154      * Test clone method. Just verifying that the original ellipse and its clone
155      * are equals, but not the same.
156      */
157     @Test public void testOrbitClone() {
158         testClone(this.gpsp);
159         testClone(this.gpsfp);
160         testClone(this.exosatp);
161         testClone(this.exosatfp);
162     }
163 
164     private void testClone(OrbitePoints o) {
165         OrbitePoints o2 = o.clone();
166 
167         assertTrue(o.equals(o2));
168         assertNotSame(o, o2);
169     }
170 
171     /**
172      * Test tangent vector with a given precision.
173      */
174     @Test public void testOrbitTangent() {
175         testTangent(this.gpsp, this.gps);
176         testTangent(this.gpsfp, this.gpsf);
177         testTangent(this.exosatp, this.exosat);
178         testTangent(this.exosatfp, this.exosatf);
179     }
180 
181     private void testTangent(OrbitePoints op, Orbite o) {
182         Point tangOP = null;
183         Point tangO = null;
184         double det = 0.0;
185 
186         for (int i = 1; i <= op.getNbPoints(); i++) {
187             tangOP = op.getVecteurTangent(i);
188             tangO = o.calculerVecteurTangent((i - 1* STEP);
189 
190             det = tangOP.getX() * tangO.getY() -
191                 tangOP.getY() * tangO.getX();
192 
193             assertEquals(0.0, det, 1E-2);
194         }
195 
196     }
197 
198     /**
199      * Test translater using a (0,0) translation vector.
200      */
201     @Test public void testOrbitTranslaterNull() {
202         testTranslaterNull(this.gpsp);
203         testTranslaterNull(this.gpsfp);
204         testTranslaterNull(this.exosatp);
205         testTranslaterNull(this.exosatfp);
206     }
207 
208     private void testTranslaterNull(OrbitePoints o) {
209         OrbitePoints oc = o.clone();
210 
211         o.translater(0.00.0);
212 
213         assertTrue(o.equals(oc));
214     }
215 
216     /**
217      * Test translater using a (2,-1) translation vector.
218      */
219     @Test public void testOrbitTranslater() {
220         testTranslater(this.gpsp);
221         testTranslater(this.gpsfp);
222         testTranslater(this.exosatp);
223         testTranslater(this.exosatfp);
224     }
225 
226     private void testTranslater(OrbitePoints o) {
227         OrbitePoints oc = o.clone();
228 
229         o.translater(2.0, -1.0);
230 
231         for (int i = 1; i <= o.getNbPoints(); i++) {
232             assertEquals(oc.getPoint(i).getX() 2.0, o.getPoint(i).getX(), EPS);
233             assertEquals(oc.getPoint(i).getY() 1.0, o.getPoint(i).getY(), EPS);
234         }
235 
236         assertEquals(oc.getCentre().getX() 2.0, o.getCentre().getX(), EPS);
237         assertEquals(oc.getCentre().getY() 1.0, o.getCentre().getY(), EPS);
238     }
239 
240     /**
241      * Test translater using a (5,-5) translation vector and then
242      * a (-5,5) translation vector.
243      */
244     @Test public void testOrbitTranslaterBF() {
245         testTranslaterBF(this.gpsp);
246         testTranslaterBF(this.gpsfp);
247         testTranslaterBF(this.exosatp);
248         testTranslaterBF(this.exosatfp);
249     }
250 
251     private void testTranslaterBF(OrbitePoints o) {
252         OrbitePoints oc = o.clone();
253 
254         o.translater(5.0, -5.0);
255         o.translater(-5.05.0);
256 
257         assertTrue(o.equals(oc));
258     }
259 
260     /**
261      * Test homothetie using a simple 2 factor.
262      */
263     @Test public void testOrbitHomothetieTwo() {
264         testHomothetie(this.gpsp, 2.0);
265         testHomothetie(this.gpsfp, 2.0);
266         testHomothetie(this.exosatp, 2.0);
267         testHomothetie(this.exosatfp, 2.0);
268     }
269 
270     /**
271      * Test homothetie using a simple 0.5 factor.
272      */
273     @Test public void testOrbitHomothetieHalf() {
274         testHomothetie(this.gpsp, 0.5);
275         testHomothetie(this.gpsfp, 0.5);
276         testHomothetie(this.exosatp, 0.5);
277         testHomothetie(this.exosatfp, 0.5);
278     }
279 
280     private void testHomothetie(OrbitePoints o, double factor) {
281         OrbitePoints oc = o.clone();
282         o.homothetie(factor);
283         o.translater(-o.getCentre().getX(),
284                      -o.getCentre().getY());
285         oc.translater(-oc.getCentre().getX(),
286                       -oc.getCentre().getY());
287 
288         for (int i = 1; i <= o.getNbPoints(); i++) {
289             assertEquals(oc.getPoint(i).getModule() *  factor, o.getPoint(i).getModule(), EPS);
290         }
291     }
292 }