1 package net.sf.openrocket.motor;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import net.sf.openrocket.models.atmosphere.AtmosphericConditions;
8 import net.sf.openrocket.rocketcomponent.MotorMount;
9 import net.sf.openrocket.util.Coordinate;
10 import net.sf.openrocket.util.Monitorable;
13 * A configuration of motor instances identified by a string id. Each motor instance has
14 * an individual position, ingition time etc.
16 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18 public final class MotorInstanceConfiguration implements Monitorable, Cloneable {
20 private final List<MotorId> ids = new ArrayList<MotorId>();
21 private final List<MotorId> unmodifiableIds = Collections.unmodifiableList(ids);
22 private final List<MotorInstance> motors = new ArrayList<MotorInstance>();
23 private final List<MotorMount> mounts = new ArrayList<MotorMount>();
24 private final List<Coordinate> positions = new ArrayList<Coordinate>();
25 private final List<Double> ignitionTimes = new ArrayList<Double>();
28 private int modID = 0;
32 * Add a motor instance to this configuration. The motor is placed at
33 * the specified position and with an infinite ignition time (never ignited).
35 * @param id the ID of this motor instance.
36 * @param motor the motor instance.
37 * @param mount the motor mount containing this motor
38 * @param position the position of the motor in absolute coordinates.
39 * @throws IllegalArgumentException if a motor with the specified ID already exists.
41 public void addMotor(MotorId id, MotorInstance motor, MotorMount mount, Coordinate position) {
42 if (this.ids.contains(id)) {
43 throw new IllegalArgumentException("MotorInstanceConfiguration already " +
44 "contains a motor with id " + id);
47 this.motors.add(motor);
48 this.mounts.add(mount);
49 this.positions.add(position);
50 this.ignitionTimes.add(Double.POSITIVE_INFINITY);
55 * Return a list of all motor IDs in this configuration (not only ones in active stages).
57 public List<MotorId> getMotorIDs() {
58 return unmodifiableIds;
61 public MotorInstance getMotorInstance(MotorId id) {
62 return motors.get(indexOf(id));
65 public MotorMount getMotorMount(MotorId id) {
66 return mounts.get(indexOf(id));
69 public Coordinate getMotorPosition(MotorId id) {
70 return positions.get(indexOf(id));
73 public void setMotorPosition(MotorId id, Coordinate position) {
74 positions.set(indexOf(id), position);
78 public double getMotorIgnitionTime(MotorId id) {
79 return ignitionTimes.get(indexOf(id));
82 public void setMotorIgnitionTime(MotorId id, double time) {
83 this.ignitionTimes.set(indexOf(id), time);
89 private int indexOf(MotorId id) {
90 int index = ids.indexOf(id);
92 throw new IllegalArgumentException("MotorInstanceConfiguration does not " +
93 "contain a motor with id " + id);
101 * Step all of the motor instances to the specified time minus their ignition time.
102 * @param time the "global" time
104 public void step(double time, double acceleration, AtmosphericConditions cond) {
105 for (int i = 0; i < motors.size(); i++) {
106 double t = time - ignitionTimes.get(i);
108 motors.get(i).step(t, acceleration, cond);
115 public int getModID() {
117 for (MotorInstance motor : motors) {
118 id += motor.getModID();
124 * Return a copy of this motor instance configuration with independent motor instances
125 * from this instance.
128 public MotorInstanceConfiguration clone() {
129 MotorInstanceConfiguration clone = new MotorInstanceConfiguration();
130 clone.ids.addAll(this.ids);
131 clone.mounts.addAll(this.mounts);
132 clone.positions.addAll(this.positions);
133 clone.ignitionTimes.addAll(this.ignitionTimes);
134 for (MotorInstance motor : this.motors) {
135 clone.motors.add(motor.clone());
137 clone.modID = this.modID;