Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / view / BasicOrbitViewAnimator.java
blobadbc7901c728252b6bbd8f12ae5b170ebe6cc6dc
1 /*
2 Copyright (C) 2001, 2006 United States Government as represented by
3 the Administrator of the National Aeronautics and Space Administration.
4 All Rights Reserved.
5 */
6 package gov.nasa.worldwind.view;
8 import gov.nasa.worldwind.geom.*;
9 import gov.nasa.worldwind.util.Logging;
11 import java.lang.reflect.Constructor;
12 import java.util.Arrays;
14 /**
15 * @author dcollins
16 * @version $Id: BasicOrbitViewAnimator.java 2471 2007-07-31 21:50:57Z tgaskins $
18 public class BasicOrbitViewAnimator implements OrbitViewAnimator
20 protected BasicOrbitViewAnimator()
24 public final void doNextState(double interpolant, OrbitView orbitView, BasicOrbitViewStateIterator stateIterator)
26 if (orbitView == null)
28 String message = Logging.getMessage("nullValue.OrbitViewIsNull");
29 Logging.logger().severe(message);
30 throw new IllegalArgumentException(message);
32 if (stateIterator == null)
34 String message = Logging.getMessage("nullValue.OrbitViewStateIteratorIsNull");
35 Logging.logger().severe(message);
36 throw new IllegalArgumentException(message);
39 this.doNextStateImpl(interpolant, orbitView, stateIterator);
42 protected void doNextStateImpl(double interpolant, OrbitView orbitView,
43 BasicOrbitViewStateIterator stateIterator)
47 public final OrbitViewAnimator coalesceWith(OrbitView orbitView, OrbitViewAnimator animator)
49 if (orbitView == null)
51 String message = Logging.getMessage("nullValue.OrbitViewIsNull");
52 Logging.logger().severe(message);
53 throw new IllegalArgumentException(message);
55 if (animator == null)
57 String message = Logging.getMessage("nullValue.OrbitViewAnimatorIsNull");
58 Logging.logger().severe(message);
59 throw new IllegalArgumentException(message);
62 return this.coalesceWithImpl(orbitView, animator);
65 protected OrbitViewAnimator coalesceWithImpl(OrbitView orbitView, OrbitViewAnimator animator)
67 return this;
70 // ============== Implementations ======================= //
71 // ============== Implementations ======================= //
72 // ============== Implementations ======================= //
74 public static class AngleAnimator extends BasicOrbitViewAnimator
76 private final Angle begin;
77 private final Angle end;
78 private final OrbitViewPropertyAccessor.AngleAccessor propertyAccessor;
80 public AngleAnimator(
81 Angle begin, Angle end,
82 OrbitViewPropertyAccessor.AngleAccessor propertyAccessor)
84 if (begin == null || end == null)
86 String message = Logging.getMessage("nullValue.AngleIsNull");
87 Logging.logger().severe(message);
88 throw new IllegalArgumentException(message);
90 if (propertyAccessor == null)
92 String message = Logging.getMessage("nullValue.OrbitViewPropertyAccessorIsNull");
93 Logging.logger().severe(message);
94 throw new IllegalArgumentException(message);
97 this.begin = begin;
98 this.end = end;
99 this.propertyAccessor = propertyAccessor;
102 public final Angle getBegin()
104 return this.begin;
107 public final Angle getEnd()
109 return this.end;
112 public final OrbitViewPropertyAccessor.AngleAccessor getPropertyAccessor()
114 return this.propertyAccessor;
117 protected final void doNextStateImpl(double interpolant, OrbitView orbitView, BasicOrbitViewStateIterator stateIterator)
119 Angle newValue = this.nextAngle(interpolant, orbitView);
120 if (newValue == null)
121 return;
123 boolean success = this.propertyAccessor.setAngle(orbitView, newValue);
124 if (!success)
125 stateIterator.stop();
128 public Angle nextAngle(double interpolant, OrbitView orbitView)
130 return Angle.mix(
131 interpolant,
132 this.begin,
133 this.end);
137 public static class DoubleAnimator extends BasicOrbitViewAnimator
139 private final double begin;
140 private final double end;
141 private final OrbitViewPropertyAccessor.DoubleAccessor propertyAccessor;
143 public DoubleAnimator(
144 double begin, double end,
145 OrbitViewPropertyAccessor.DoubleAccessor propertyAccessor)
147 if (propertyAccessor == null)
149 String message = Logging.getMessage("nullValue.OrbitViewPropertyAccessorIsNull");
150 Logging.logger().severe(message);
151 throw new IllegalArgumentException(message);
154 this.begin = begin;
155 this.end = end;
156 this.propertyAccessor = propertyAccessor;
159 public final Double getBegin()
161 return this.begin;
164 public final Double getEnd()
166 return this.end;
169 public final OrbitViewPropertyAccessor.DoubleAccessor getPropertyAccessor()
171 return this.propertyAccessor;
174 protected final void doNextStateImpl(double interpolant, OrbitView orbitView, BasicOrbitViewStateIterator stateIterator)
176 Double newValue = this.nextDouble(interpolant, orbitView);
177 if (newValue == null)
178 return;
180 boolean success = this.propertyAccessor.setDouble(orbitView, newValue);
181 if (!success)
182 stateIterator.stop();
185 public Double nextDouble(double interpolant, OrbitView orbitView)
187 return mix(
188 interpolant,
189 this.begin,
190 this.end);
193 public static double mix(double amount, double value1, double value2)
195 if (amount < 0)
196 return value1;
197 else if (amount > 1)
198 return value2;
199 return value1 * (1.0 - amount) + value2 * amount;
203 public static class LatLonAnimator extends BasicOrbitViewAnimator
205 private final LatLon begin;
206 private final LatLon end;
207 private final OrbitViewPropertyAccessor.LatLonAccessor propertyAccessor;
209 public LatLonAnimator(
210 LatLon begin,
211 LatLon end,
212 OrbitViewPropertyAccessor.LatLonAccessor propertyAccessor)
214 if (begin == null || end == null)
216 String message = Logging.getMessage("nullValue.LatLonIsNull");
217 Logging.logger().severe(message);
218 throw new IllegalArgumentException(message);
220 if (propertyAccessor == null)
222 String message = Logging.getMessage("nullValue.OrbitViewPropertyAccessorIsNull");
223 Logging.logger().severe(message);
224 throw new IllegalArgumentException(message);
227 this.begin = begin;
228 this.end = end;
229 this.propertyAccessor = propertyAccessor;
232 public final LatLon getBegin()
234 return this.begin;
237 public final LatLon getEnd()
239 return this.end;
242 public final OrbitViewPropertyAccessor.LatLonAccessor getPropertyAccessor()
244 return this.propertyAccessor;
247 protected final void doNextStateImpl(double interpolant, OrbitView orbitView, BasicOrbitViewStateIterator stateIterator)
249 LatLon newValue = this.nextLatLon(interpolant, orbitView);
250 if (newValue == null)
251 return;
253 boolean success = this.propertyAccessor.setLatLon(orbitView, newValue);
254 if (!success)
255 stateIterator.stop();
258 public LatLon nextLatLon(double interpolant, OrbitView orbitView)
260 return LatLon.interpolate(
261 interpolant,
262 this.begin,
263 this.end);
267 public static class QuaternionAnimator extends BasicOrbitViewAnimator
269 private final Quaternion begin;
270 private final Quaternion end;
271 private final OrbitViewPropertyAccessor.QuaternionAccessor propertyAccessor;
273 public QuaternionAnimator(
274 Quaternion begin, Quaternion end,
275 OrbitViewPropertyAccessor.QuaternionAccessor propertyAccessor)
277 if (begin == null || end == null)
279 String message = Logging.getMessage("nullValue.QuaternionIsNull");
280 Logging.logger().severe(message);
281 throw new IllegalArgumentException(message);
283 if (propertyAccessor == null)
285 String message = Logging.getMessage("nullValue.OrbitViewPropertyAccessorIsNull");
286 Logging.logger().severe(message);
287 throw new IllegalArgumentException(message);
290 this.begin = begin;
291 this.end = end;
292 this.propertyAccessor = propertyAccessor;
295 public final Quaternion getBegin()
297 return this.begin;
300 public final Quaternion getEnd()
302 return this.end;
305 public final OrbitViewPropertyAccessor.QuaternionAccessor getPropertyAccessor()
307 return this.propertyAccessor;
310 protected final void doNextStateImpl(double interpolant, OrbitView orbitView, BasicOrbitViewStateIterator stateIterator)
312 Quaternion newValue = this.nextQuaternion(interpolant, orbitView);
313 if (newValue == null)
314 return;
316 boolean success = this.propertyAccessor.setQuaternion(orbitView, newValue);
317 if (!success)
318 stateIterator.stop();
321 public Quaternion nextQuaternion(double interpolant, OrbitView orbitView)
323 return Quaternion.mix(
324 interpolant,
325 this.begin,
326 this.end);
330 public static class CompoundAnimator extends BasicOrbitViewAnimator
332 private OrbitViewAnimator[] animators;
334 public CompoundAnimator(OrbitViewAnimator... animators)
336 if (animators == null)
338 String message = Logging.getMessage("nullValue.ArrayIsNull");
339 Logging.logger().severe(message);
340 throw new IllegalArgumentException(message);
343 int numAnimators = animators.length;
344 this.animators = new OrbitViewAnimator[numAnimators];
345 System.arraycopy(animators, 0, this.animators, 0, numAnimators);
348 private CompoundAnimator newInstance(OrbitViewAnimator... animators)
350 if (animators == null)
352 String message = Logging.getMessage("nullValue.ArrayIsNull");
353 Logging.logger().severe(message);
354 throw new IllegalArgumentException(message);
357 CompoundAnimator newCompoundAnimator;
360 Class<? extends CompoundAnimator> cls = this.getClass();
361 Class<? extends OrbitViewAnimator[]> paramCls = animators.getClass();
362 Constructor<? extends CompoundAnimator> constructor = cls.getConstructor(paramCls);
363 newCompoundAnimator = constructor.newInstance((Object) animators);
365 catch (Exception e)
367 String message = Logging.getMessage(""); // TODO
368 Logging.logger().severe(message);
369 throw new IllegalArgumentException(message);
372 return newCompoundAnimator;
375 public final Iterable<OrbitViewAnimator> getAnimators()
377 return Arrays.asList(this.animators);
380 protected final void doNextStateImpl(double interpolant, OrbitView orbitView,
381 BasicOrbitViewStateIterator stateIterator)
383 for (OrbitViewAnimator a : animators)
385 if (a != null)
387 a.doNextState(interpolant, orbitView, stateIterator);
392 protected final OrbitViewAnimator coalesceWithImpl(OrbitView orbitView, OrbitViewAnimator animator)
394 if (!(animator instanceof CompoundAnimator))
395 return this;
397 // Cannot coalesce with different number of internal animators.
398 CompoundAnimator that = (CompoundAnimator) animator;
399 if (this.animators.length != that.animators.length)
400 return this;
402 int numAnimators = this.animators.length;
403 OrbitViewAnimator[] newAnimators = new OrbitViewAnimator[numAnimators];
404 for (int i = 0; i < numAnimators; i++)
406 if (this.animators[i] != null && that.animators[i] != null)
408 newAnimators[i] = this.animators[i].coalesceWith(orbitView, that.animators[i]);
410 else if (this.animators[i] != null)
412 newAnimators[i] = this.animators[i];
416 CompoundAnimator newCompoundAnimator = this.newInstance(newAnimators);
417 if (newCompoundAnimator == null)
418 return this;
420 return newCompoundAnimator;