create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / simulation / SimulationStatus.java
1 package net.sf.openrocket.simulation;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Set;
6
7 import net.sf.openrocket.aerodynamics.WarningSet;
8 import net.sf.openrocket.motor.MotorInstanceConfiguration;
9 import net.sf.openrocket.rocketcomponent.Configuration;
10 import net.sf.openrocket.rocketcomponent.RecoveryDevice;
11 import net.sf.openrocket.util.BugException;
12 import net.sf.openrocket.util.Coordinate;
13 import net.sf.openrocket.util.Monitorable;
14 import net.sf.openrocket.util.MonitorableSet;
15 import net.sf.openrocket.util.Quaternion;
16 import net.sf.openrocket.util.WorldCoordinate;
17
18 /**
19  * A holder class for the dynamic status during the rocket's flight.
20  * 
21  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22  */
23 public class SimulationStatus implements Cloneable, Monitorable {
24         
25         /*
26          * NOTE!  All fields must be added to copyFrom() method!!
27          */
28
29         private SimulationConditions simulationConditions;
30         private Configuration configuration;
31         private MotorInstanceConfiguration motorConfiguration;
32         private FlightDataBranch flightData;
33         
34         private double time;
35         
36         private double previousTimeStep;
37         
38         private Coordinate position;
39         private WorldCoordinate worldPosition;
40         private Coordinate velocity;
41         
42         private Quaternion orientation;
43         private Coordinate rotationVelocity;
44         
45         private double effectiveLaunchRodLength;
46         
47
48         /** Nanosecond time when the simulation was started. */
49         private long simulationStartWallTime = Long.MIN_VALUE;
50         
51
52         /** Set to true when a motor has ignited. */
53         private boolean motorIgnited = false;
54         
55         /** Set to true when the rocket has risen from the ground. */
56         private boolean liftoff = false;
57         
58         /** Set to true when the launch rod has been cleared. */
59         private boolean launchRodCleared = false;
60         
61         /** Set to true when apogee has been detected. */
62         private boolean apogeeReached = false;
63         
64         /** Contains a list of deployed recovery devices. */
65         private MonitorableSet<RecoveryDevice> deployedRecoveryDevices = new MonitorableSet<RecoveryDevice>();
66         
67         /** The flight event queue */
68         private final EventQueue eventQueue = new EventQueue();
69         
70         private WarningSet warnings;
71         
72         /** Available for special purposes by the listeners. */
73         private final Map<String, Object> extraData = new HashMap<String, Object>();
74         
75
76         private int modID = 0;
77         private int modIDadd = 0;
78         
79         
80         public void setSimulationTime(double time) {
81                 this.time = time;
82                 this.modID++;
83         }
84         
85         
86         public double getSimulationTime() {
87                 return time;
88         }
89         
90         
91         public void setConfiguration(Configuration configuration) {
92                 if (this.configuration != null)
93                         this.modIDadd += this.configuration.getModID();
94                 this.modID++;
95                 this.configuration = configuration;
96         }
97         
98         
99         public Configuration getConfiguration() {
100                 return configuration;
101         }
102         
103         
104         public void setMotorConfiguration(MotorInstanceConfiguration motorConfiguration) {
105                 if (this.motorConfiguration != null)
106                         this.modIDadd += this.motorConfiguration.getModID();
107                 this.modID++;
108                 this.motorConfiguration = motorConfiguration;
109         }
110         
111         
112         public MotorInstanceConfiguration getMotorConfiguration() {
113                 return motorConfiguration;
114         }
115         
116         
117         public void setFlightData(FlightDataBranch flightData) {
118                 if (this.flightData != null)
119                         this.modIDadd += this.flightData.getModID();
120                 this.modID++;
121                 this.flightData = flightData;
122         }
123         
124         
125         public FlightDataBranch getFlightData() {
126                 return flightData;
127         }
128         
129         
130         public double getPreviousTimeStep() {
131                 return previousTimeStep;
132         }
133         
134         
135         public void setPreviousTimeStep(double previousTimeStep) {
136                 this.previousTimeStep = previousTimeStep;
137                 this.modID++;
138         }
139         
140         
141         public void setRocketPosition(Coordinate position) {
142                 this.position = position;
143                 this.modID++;
144         }
145         
146         
147         public Coordinate getRocketPosition() {
148                 return position;
149         }
150         
151         public void setRocketWorldPosition(WorldCoordinate wc) {
152                 this.worldPosition = wc;
153                 this.modID++;
154         }
155         
156         public WorldCoordinate getRocketWorldPosition() {
157                 return worldPosition;
158         }
159         
160         public void setRocketVelocity(Coordinate velocity) {
161                 this.velocity = velocity;
162                 this.modID++;
163         }
164         
165         
166         public Coordinate getRocketVelocity() {
167                 return velocity;
168         }
169         
170         
171
172
173
174         public Quaternion getRocketOrientationQuaternion() {
175                 return orientation;
176         }
177         
178         
179         public void setRocketOrientationQuaternion(Quaternion orientation) {
180                 this.orientation = orientation;
181                 this.modID++;
182         }
183         
184         
185         public Coordinate getRocketRotationVelocity() {
186                 return rotationVelocity;
187         }
188         
189         
190         public void setRocketRotationVelocity(Coordinate rotation) {
191                 this.rotationVelocity = rotation;
192         }
193         
194         
195         public void setEffectiveLaunchRodLength(double effectiveLaunchRodLength) {
196                 this.effectiveLaunchRodLength = effectiveLaunchRodLength;
197                 this.modID++;
198         }
199         
200         
201         public double getEffectiveLaunchRodLength() {
202                 return effectiveLaunchRodLength;
203         }
204         
205         
206         public void setSimulationStartWallTime(long simulationStartWallTime) {
207                 this.simulationStartWallTime = simulationStartWallTime;
208                 this.modID++;
209         }
210         
211         
212         public long getSimulationStartWallTime() {
213                 return simulationStartWallTime;
214         }
215         
216         
217         public void setMotorIgnited(boolean motorIgnited) {
218                 this.motorIgnited = motorIgnited;
219                 this.modID++;
220         }
221         
222         
223         public boolean isMotorIgnited() {
224                 return motorIgnited;
225         }
226         
227         
228         public void setLiftoff(boolean liftoff) {
229                 this.liftoff = liftoff;
230                 this.modID++;
231         }
232         
233         
234         public boolean isLiftoff() {
235                 return liftoff;
236         }
237         
238         
239         public void setLaunchRodCleared(boolean launchRod) {
240                 this.launchRodCleared = launchRod;
241                 this.modID++;
242         }
243         
244         
245         public boolean isLaunchRodCleared() {
246                 return launchRodCleared;
247         }
248         
249         
250         public void setApogeeReached(boolean apogeeReached) {
251                 this.apogeeReached = apogeeReached;
252                 this.modID++;
253         }
254         
255         
256         public boolean isApogeeReached() {
257                 return apogeeReached;
258         }
259         
260         
261         public Set<RecoveryDevice> getDeployedRecoveryDevices() {
262                 return deployedRecoveryDevices;
263         }
264         
265         
266         public void setWarnings(WarningSet warnings) {
267                 if (this.warnings != null)
268                         this.modIDadd += this.warnings.getModID();
269                 this.modID++;
270                 this.warnings = warnings;
271         }
272         
273         
274         public WarningSet getWarnings() {
275                 return warnings;
276         }
277         
278         
279         public EventQueue getEventQueue() {
280                 return eventQueue;
281         }
282         
283         
284         public void setSimulationConditions(SimulationConditions simulationConditions) {
285                 if (this.simulationConditions != null)
286                         this.modIDadd += this.simulationConditions.getModID();
287                 this.modID++;
288                 this.simulationConditions = simulationConditions;
289         }
290         
291         
292         public SimulationConditions getSimulationConditions() {
293                 return simulationConditions;
294         }
295         
296         
297         /**
298          * Store extra data available for use by simulation listeners.  The data can be retrieved
299          * using {@link #getExtraData(String)}.
300          * 
301          * @param key           the data key
302          * @param value         the value to store
303          */
304         public void putExtraData(String key, Object value) {
305                 extraData.put(key, value);
306         }
307         
308         /**
309          * Retrieve extra data stored by simulation listeners.  This data map is initially empty.
310          * Data can be stored using {@link #putExtraData(String, Object)}.
311          * 
312          * @param key           the data key to retrieve
313          * @return                      the data, or <code>null</code> if nothing has been set for the key
314          */
315         public Object getExtraData(String key) {
316                 return extraData.get(key);
317         }
318         
319         
320         /**
321          * Returns a copy of this object.  The general purpose is that the conditions,
322          * rocket configuration, flight data etc. point to the same objects.  However,
323          * subclasses are allowed to deep-clone specific objects, such as those pertaining
324          * to the current orientation of the rocket.  The purpose is to allow creating intermediate
325          * copies of this object used during step computation.
326          * 
327          * TODO: HIGH: Deep cloning required for branch saving.
328          */
329         @Override
330         public SimulationStatus clone() {
331                 try {
332                         SimulationStatus clone = (SimulationStatus) super.clone();
333                         return clone;
334                 } catch (CloneNotSupportedException e) {
335                         throw new BugException("CloneNotSupportedException?!?", e);
336                 }
337         }
338         
339         
340         /**
341          * Copies the data from the provided object to this object.  Most included object are
342          * deep-cloned, except for the flight data object.
343          * 
344          * @param orig  the object from which to copy
345          */
346         public void copyFrom(SimulationStatus orig) {
347                 this.simulationConditions = orig.simulationConditions.clone();
348                 this.configuration = orig.configuration.clone();
349                 this.motorConfiguration = orig.motorConfiguration.clone();
350                 this.flightData = orig.flightData;
351                 this.time = orig.time;
352                 this.previousTimeStep = orig.previousTimeStep;
353                 this.position = orig.position;
354                 this.worldPosition = orig.worldPosition;
355                 this.velocity = orig.velocity;
356                 this.orientation = orig.orientation;
357                 this.rotationVelocity = orig.rotationVelocity;
358                 this.effectiveLaunchRodLength = orig.effectiveLaunchRodLength;
359                 this.simulationStartWallTime = orig.simulationStartWallTime;
360                 this.motorIgnited = orig.motorIgnited;
361                 this.liftoff = orig.liftoff;
362                 this.launchRodCleared = orig.launchRodCleared;
363                 this.apogeeReached = orig.apogeeReached;
364                 
365                 this.deployedRecoveryDevices.clear();
366                 this.deployedRecoveryDevices.addAll(orig.deployedRecoveryDevices);
367                 
368                 this.eventQueue.clear();
369                 this.eventQueue.addAll(orig.eventQueue);
370                 
371                 this.warnings = orig.warnings;
372                 
373                 this.extraData.clear();
374                 this.extraData.putAll(orig.extraData);
375                 
376                 this.modID = orig.modID;
377                 this.modIDadd = orig.modIDadd;
378         }
379         
380         
381         @Override
382         public int getModID() {
383                 return (modID + modIDadd + simulationConditions.getModID() + configuration.getModID() +
384                                 motorConfiguration.getModID() + flightData.getModID() + deployedRecoveryDevices.getModID() +
385                                 eventQueue.getModID() + warnings.getModID());
386         }
387         
388
389 }