import java.util.Map;
import net.sf.openrocket.rocketcomponent.Configuration;
-import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.util.Coordinate;
/**
- * A class that is the base of all aerodynamical calculations.
- * <p>
- * A rocket must be assigned to this class before any operations are allowed.
- * This can be done using the constructor or using the
- * {@link #setRocket(Rocket)} method. The default is a
- * <code>null</code> configuration, in which case the calculation
- * methods throw {@link NullPointerException}.
+ * An abstract aerodynamic calculator implementation, that offers basic implementation
+ * of some methods and methods for cache validation and purging.
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
/** The aerodynamic modification ID of the latest rocket */
private int rocketAeroModID = -1;
+ private int stageCount = -1;
-
//////////////// Aerodynamic calculators ////////////////
public abstract Coordinate getCP(Configuration configuration, FlightConditions conditions,
* @param configuration the configuration of the current call
*/
protected final void checkCache(Configuration configuration) {
- if (rocketAeroModID != configuration.getRocket().getAerodynamicModID()) {
+ if (rocketAeroModID != configuration.getRocket().getAerodynamicModID() ||
+ stageCount != configuration.getStageCount()) {
rocketAeroModID = configuration.getRocket().getAerodynamicModID();
+ stageCount = configuration.getStageCount();
voidAerodynamicCache();
}
}
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
-public class Configuration implements Cloneable, ChangeSource, ComponentChangeListener,
+public class Configuration implements Cloneable, ChangeSource, ComponentChangeListener,
Iterable<RocketComponent>, Monitorable {
-
+
private Rocket rocket;
private BitSet stages = new BitSet();
private int boundsModID = -1;
private ArrayList<Coordinate> cachedBounds = new ArrayList<Coordinate>();
private double cachedLength = -1;
-
+
private int refLengthModID = -1;
private double cachedRefLength = -1;
-
+
private int modID = 0;
- private int modIDadd = 0;
/**
setAllStages();
rocket.addComponentChangeListener(this);
}
-
+
public Rocket getRocket() {
return rocket;
}
public void setAllStages() {
stages.clear();
- stages.set(0,rocket.getStageCount());
+ stages.set(0, rocket.getStageCount());
fireChangeEvent();
}
*/
public void setToStage(int stage) {
stages.clear();
- stages.set(0, stage+1, true);
-// stages.set(stage+1, rocket.getStageCount(), false);
+ stages.set(0, stage + 1, true);
+ // stages.set(stage+1, rocket.getStageCount(), false);
fireChangeEvent();
}
public boolean isStageActive(RocketComponent stage) {
if (!(stage instanceof Stage)) {
- throw new IllegalArgumentException("called with component "+stage);
+ throw new IllegalArgumentException("called with component " + stage);
}
return stages.get(stage.getParent().getChildPosition(stage));
}
public int getStageCount() {
return rocket.getStageCount();
}
-
+
public int getActiveStageCount() {
int count = 0;
int s = rocket.getStageCount();
- for (int i=0; i < s; i++) {
+ for (int i = 0; i < s; i++) {
if (stages.get(i))
count++;
}
return count;
}
-
+
public int[] getActiveStages() {
int stageCount = rocket.getStageCount();
List<Integer> active = new ArrayList<Integer>();
int[] ret;
-
- for (int i=0; i < stageCount; i++) {
+
+ for (int i = 0; i < stageCount; i++) {
if (stages.get(i)) {
active.add(i);
}
}
ret = new int[active.size()];
- for (int i=0; i < ret.length; i++) {
+ for (int i = 0; i < ret.length; i++) {
ret[i] = active.get(i);
}
public double getReferenceArea() {
- return Math.PI * MathUtil.pow2(getReferenceLength()/2);
+ return Math.PI * MathUtil.pow2(getReferenceLength() / 2);
}
}
public void setMotorConfigurationID(String id) {
- if ((motorConfiguration == null && id == null) ||
+ if ((motorConfiguration == null && id == null) ||
(id != null && id.equals(motorConfiguration)))
return;
}
-
-
-
+
+
/**
* Removes the listener connection to the rocket and listeners of this object.
* This configuration may not be used after a call to this method!
public void addChangeListener(ChangeListener listener) {
listenerList.add(ChangeListener.class, listener);
}
-
+
@Override
public void removeChangeListener(ChangeListener listener) {
listenerList.remove(ChangeListener.class, listener);
boundsModID = -1;
refLengthModID = -1;
- for (int i = listeners.length-2; i>=0; i-=2) {
+ for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
- ((ChangeListener) listeners[i+1]).stateChanged(e);
+ ((ChangeListener) listeners[i + 1]).stateChanged(e);
}
}
}
-
+
@Override
public void componentChanged(ComponentChangeEvent e) {
* @return true if this configuration has active motor mounts with motors defined to them.
*/
public boolean hasMotors() {
- for (RocketComponent c: this) {
+ for (RocketComponent c : this) {
if (c instanceof MotorMount) {
MotorMount mount = (MotorMount) c;
if (!mount.isMotorMount())
cachedBounds.clear();
double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY;
- for (RocketComponent component: this) {
- for (Coordinate c: component.getComponentBounds()) {
- for (Coordinate coord: component.toAbsolute(c)) {
+ for (RocketComponent component : this) {
+ for (Coordinate c : component.getComponentBounds()) {
+ for (Coordinate coord : component.toAbsolute(c)) {
cachedBounds.add(coord);
if (coord.x < minX)
minX = coord.x;
*/
public double getLength() {
if (rocket.getModID() != boundsModID)
- getBounds(); // Calculates the length
-
+ getBounds(); // Calculates the length
+
return cachedLength;
}
-
+
/**
* Return an iterator that iterates over the currently active components.
rocket.addComponentChangeListener(config);
return config;
} catch (CloneNotSupportedException e) {
- throw new BugException("BUG: clone not supported!",e);
+ throw new BugException("BUG: clone not supported!", e);
}
}
-
-
+
+
@Override
public int getModID() {
return modID + rocket.getModID();
}
-
+
/**
* A class that iterates over all currently active components.
*
public ConfigurationIterator() {
List<Iterator<RocketComponent>> list = new ArrayList<Iterator<RocketComponent>>();
- for (RocketComponent stage: rocket.getChildren()) {
+ for (RocketComponent stage : rocket.getChildren()) {
if (isStageActive(stage)) {
list.add(stage.deepIterator());
}
return current.hasNext();
}
-
+
@Override
public RocketComponent next() {
if (!current.hasNext())
return current.next();
}
-
+
/**
* Get the next iterator that has items. If such an iterator does
* not exist, current is left to an empty iterator.
public MotorIterator() {
this.iterator = iterator();
}
-
+
@Override
public boolean hasNext() {
getNext();
return (next != null);
}
-
+
@Override
public MotorMount next() {
getNext();
next = null;
return ret;
}
-
+
@Override
public void remove() {
throw new UnsupportedOperationException("remove unsupported");
}
}
}
-
+
}