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);
54 public List<MotorId> getMotorIDs() {
55 return unmodifiableIds;
58 public MotorInstance getMotorInstance(MotorId id) {
59 return motors.get(indexOf(id));
62 public MotorMount getMotorMount(MotorId id) {
63 return mounts.get(indexOf(id));
66 public Coordinate getMotorPosition(MotorId id) {
67 return positions.get(indexOf(id));
70 public void setMotorPosition(MotorId id, Coordinate position) {
71 positions.set(indexOf(id), position);
75 public double getMotorIgnitionTime(MotorId id) {
76 return ignitionTimes.get(indexOf(id));
79 public void setMotorIgnitionTime(MotorId id, double time) {
80 this.ignitionTimes.set(indexOf(id), time);
86 private int indexOf(MotorId id) {
87 int index = ids.indexOf(id);
89 throw new IllegalArgumentException("MotorInstanceConfiguration does not " +
90 "contain a motor with id " + id);
98 * Step all of the motor instances to the specified time minus their ignition time.
99 * @param time the "global" time
101 public void step(double time, double acceleration, AtmosphericConditions cond) {
102 for (int i = 0; i < motors.size(); i++) {
103 double t = time - ignitionTimes.get(i);
105 motors.get(i).step(t, acceleration, cond);
112 public int getModID() {
114 for (MotorInstance motor : motors) {
115 id += motor.getModID();
121 * Return a copy of this motor instance configuration with independent motor instances
122 * from this instance.
125 public MotorInstanceConfiguration clone() {
126 MotorInstanceConfiguration clone = new MotorInstanceConfiguration();
127 clone.ids.addAll(this.ids);
128 clone.mounts.addAll(this.mounts);
129 clone.positions.addAll(this.positions);
130 clone.ignitionTimes.addAll(this.ignitionTimes);
131 for (MotorInstance motor : this.motors) {
132 clone.motors.add(motor.clone());
134 clone.modID = this.modID;