Fixed bug that prevents adding stages to a rocket

This commit is contained in:
Sampo Niskanen 2010-09-05 12:34:03 +00:00
parent f535b8caf0
commit b9a0800ca5
4 changed files with 56 additions and 55 deletions

View File

@ -1,3 +1,7 @@
2010-09-05 Sampo Niskanen
* [BUG] Fixed bug that prevents adding stages to a rocket
2010-09-04 Sampo Niskanen 2010-09-04 Sampo Niskanen
* Added launch rod velocity to FlightData * Added launch rod velocity to FlightData

View File

@ -3,19 +3,13 @@ package net.sf.openrocket.aerodynamics;
import java.util.Map; import java.util.Map;
import net.sf.openrocket.rocketcomponent.Configuration; import net.sf.openrocket.rocketcomponent.Configuration;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent; import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.util.Coordinate; import net.sf.openrocket.util.Coordinate;
/** /**
* A class that is the base of all aerodynamical calculations. * An abstract aerodynamic calculator implementation, that offers basic implementation
* <p> * of some methods and methods for cache validation and purging.
* 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}.
* *
* @author Sampo Niskanen <sampo.niskanen@iki.fi> * @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/ */
@ -33,11 +27,11 @@ public abstract class AbstractAerodynamicCalculator implements AerodynamicCalcul
/** The aerodynamic modification ID of the latest rocket */ /** The aerodynamic modification ID of the latest rocket */
private int rocketAeroModID = -1; private int rocketAeroModID = -1;
private int stageCount = -1;
//////////////// Aerodynamic calculators //////////////// //////////////// Aerodynamic calculators ////////////////
public abstract Coordinate getCP(Configuration configuration, FlightConditions conditions, public abstract Coordinate getCP(Configuration configuration, FlightConditions conditions,
@ -89,8 +83,10 @@ public abstract class AbstractAerodynamicCalculator implements AerodynamicCalcul
* @param configuration the configuration of the current call * @param configuration the configuration of the current call
*/ */
protected final void checkCache(Configuration configuration) { protected final void checkCache(Configuration configuration) {
if (rocketAeroModID != configuration.getRocket().getAerodynamicModID()) { if (rocketAeroModID != configuration.getRocket().getAerodynamicModID() ||
stageCount != configuration.getStageCount()) {
rocketAeroModID = configuration.getRocket().getAerodynamicModID(); rocketAeroModID = configuration.getRocket().getAerodynamicModID();
stageCount = configuration.getStageCount();
voidAerodynamicCache(); voidAerodynamicCache();
} }
} }

View File

@ -10,6 +10,7 @@ import net.sf.openrocket.rocketcomponent.Configuration;
public abstract class AbstractMassCalculator implements MassCalculator { public abstract class AbstractMassCalculator implements MassCalculator {
private int rocketMassModID = -1; private int rocketMassModID = -1;
private int stageCount = -1;
/** /**
@ -24,8 +25,10 @@ public abstract class AbstractMassCalculator implements MassCalculator {
* @param configuration the configuration of the current call * @param configuration the configuration of the current call
*/ */
protected final void checkCache(Configuration configuration) { protected final void checkCache(Configuration configuration) {
if (rocketMassModID != configuration.getRocket().getMassModID()) { if (rocketMassModID != configuration.getRocket().getMassModID() ||
stageCount != configuration.getStageCount()) {
rocketMassModID = configuration.getRocket().getMassModID(); rocketMassModID = configuration.getRocket().getMassModID();
stageCount = configuration.getStageCount();
voidMassCache(); voidMassCache();
} }
} }

View File

@ -26,9 +26,9 @@ import net.sf.openrocket.util.Monitorable;
* *
* @author Sampo Niskanen <sampo.niskanen@iki.fi> * @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/ */
public class Configuration implements Cloneable, ChangeSource, ComponentChangeListener, public class Configuration implements Cloneable, ChangeSource, ComponentChangeListener,
Iterable<RocketComponent>, Monitorable { Iterable<RocketComponent>, Monitorable {
private Rocket rocket; private Rocket rocket;
private BitSet stages = new BitSet(); private BitSet stages = new BitSet();
@ -41,13 +41,12 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
private int boundsModID = -1; private int boundsModID = -1;
private ArrayList<Coordinate> cachedBounds = new ArrayList<Coordinate>(); private ArrayList<Coordinate> cachedBounds = new ArrayList<Coordinate>();
private double cachedLength = -1; private double cachedLength = -1;
private int refLengthModID = -1; private int refLengthModID = -1;
private double cachedRefLength = -1; private double cachedRefLength = -1;
private int modID = 0; private int modID = 0;
private int modIDadd = 0;
/** /**
@ -61,9 +60,9 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
setAllStages(); setAllStages();
rocket.addComponentChangeListener(this); rocket.addComponentChangeListener(this);
} }
public Rocket getRocket() { public Rocket getRocket() {
return rocket; return rocket;
} }
@ -71,7 +70,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public void setAllStages() { public void setAllStages() {
stages.clear(); stages.clear();
stages.set(0,rocket.getStageCount()); stages.set(0, rocket.getStageCount());
fireChangeEvent(); fireChangeEvent();
} }
@ -84,8 +83,8 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
*/ */
public void setToStage(int stage) { public void setToStage(int stage) {
stages.clear(); stages.clear();
stages.set(0, stage+1, true); stages.set(0, stage + 1, true);
// stages.set(stage+1, rocket.getStageCount(), false); // stages.set(stage+1, rocket.getStageCount(), false);
fireChangeEvent(); fireChangeEvent();
} }
@ -101,7 +100,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public boolean isStageActive(RocketComponent stage) { public boolean isStageActive(RocketComponent stage) {
if (!(stage instanceof 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)); return stages.get(stage.getParent().getChildPosition(stage));
} }
@ -116,31 +115,31 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public int getStageCount() { public int getStageCount() {
return rocket.getStageCount(); return rocket.getStageCount();
} }
public int getActiveStageCount() { public int getActiveStageCount() {
int count = 0; int count = 0;
int s = rocket.getStageCount(); int s = rocket.getStageCount();
for (int i=0; i < s; i++) { for (int i = 0; i < s; i++) {
if (stages.get(i)) if (stages.get(i))
count++; count++;
} }
return count; return count;
} }
public int[] getActiveStages() { public int[] getActiveStages() {
int stageCount = rocket.getStageCount(); int stageCount = rocket.getStageCount();
List<Integer> active = new ArrayList<Integer>(); List<Integer> active = new ArrayList<Integer>();
int[] ret; int[] ret;
for (int i=0; i < stageCount; i++) { for (int i = 0; i < stageCount; i++) {
if (stages.get(i)) { if (stages.get(i)) {
active.add(i); active.add(i);
} }
} }
ret = new int[active.size()]; 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); ret[i] = active.get(i);
} }
@ -164,7 +163,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public double getReferenceArea() { public double getReferenceArea() {
return Math.PI * MathUtil.pow2(getReferenceLength()/2); return Math.PI * MathUtil.pow2(getReferenceLength() / 2);
} }
@ -173,7 +172,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
} }
public void setMotorConfigurationID(String id) { public void setMotorConfigurationID(String id) {
if ((motorConfiguration == null && id == null) || if ((motorConfiguration == null && id == null) ||
(id != null && id.equals(motorConfiguration))) (id != null && id.equals(motorConfiguration)))
return; return;
@ -186,10 +185,9 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
} }
/** /**
* Removes the listener connection to the rocket and listeners of this object. * Removes the listener connection to the rocket and listeners of this object.
* This configuration may not be used after a call to this method! * This configuration may not be used after a call to this method!
@ -207,7 +205,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public void addChangeListener(ChangeListener listener) { public void addChangeListener(ChangeListener listener) {
listenerList.add(ChangeListener.class, listener); listenerList.add(ChangeListener.class, listener);
} }
@Override @Override
public void removeChangeListener(ChangeListener listener) { public void removeChangeListener(ChangeListener listener) {
listenerList.remove(ChangeListener.class, listener); listenerList.remove(ChangeListener.class, listener);
@ -221,13 +219,13 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
boundsModID = -1; boundsModID = -1;
refLengthModID = -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) { if (listeners[i] == ChangeListener.class) {
((ChangeListener) listeners[i+1]).stateChanged(e); ((ChangeListener) listeners[i + 1]).stateChanged(e);
} }
} }
} }
@Override @Override
public void componentChanged(ComponentChangeEvent e) { public void componentChanged(ComponentChangeEvent e) {
@ -243,7 +241,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
* @return true if this configuration has active motor mounts with motors defined to them. * @return true if this configuration has active motor mounts with motors defined to them.
*/ */
public boolean hasMotors() { public boolean hasMotors() {
for (RocketComponent c: this) { for (RocketComponent c : this) {
if (c instanceof MotorMount) { if (c instanceof MotorMount) {
MotorMount mount = (MotorMount) c; MotorMount mount = (MotorMount) c;
if (!mount.isMotorMount()) if (!mount.isMotorMount())
@ -269,9 +267,9 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
cachedBounds.clear(); cachedBounds.clear();
double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY; double minX = Double.POSITIVE_INFINITY, maxX = Double.NEGATIVE_INFINITY;
for (RocketComponent component: this) { for (RocketComponent component : this) {
for (Coordinate c: component.getComponentBounds()) { for (Coordinate c : component.getComponentBounds()) {
for (Coordinate coord: component.toAbsolute(c)) { for (Coordinate coord : component.toAbsolute(c)) {
cachedBounds.add(coord); cachedBounds.add(coord);
if (coord.x < minX) if (coord.x < minX)
minX = coord.x; minX = coord.x;
@ -299,13 +297,13 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
*/ */
public double getLength() { public double getLength() {
if (rocket.getModID() != boundsModID) if (rocket.getModID() != boundsModID)
getBounds(); // Calculates the length getBounds(); // Calculates the length
return cachedLength; return cachedLength;
} }
/** /**
* Return an iterator that iterates over the currently active components. * Return an iterator that iterates over the currently active components.
@ -345,17 +343,17 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
rocket.addComponentChangeListener(config); rocket.addComponentChangeListener(config);
return config; return config;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new BugException("BUG: clone not supported!",e); throw new BugException("BUG: clone not supported!", e);
} }
} }
@Override @Override
public int getModID() { public int getModID() {
return modID + rocket.getModID(); return modID + rocket.getModID();
} }
/** /**
* A class that iterates over all currently active components. * A class that iterates over all currently active components.
* *
@ -368,7 +366,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public ConfigurationIterator() { public ConfigurationIterator() {
List<Iterator<RocketComponent>> list = new ArrayList<Iterator<RocketComponent>>(); List<Iterator<RocketComponent>> list = new ArrayList<Iterator<RocketComponent>>();
for (RocketComponent stage: rocket.getChildren()) { for (RocketComponent stage : rocket.getChildren()) {
if (isStageActive(stage)) { if (isStageActive(stage)) {
list.add(stage.deepIterator()); list.add(stage.deepIterator());
} }
@ -392,7 +390,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
return current.hasNext(); return current.hasNext();
} }
@Override @Override
public RocketComponent next() { public RocketComponent next() {
if (!current.hasNext()) if (!current.hasNext())
@ -400,7 +398,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
return current.next(); return current.next();
} }
/** /**
* Get the next iterator that has items. If such an iterator does * Get the next iterator that has items. If such an iterator does
* not exist, current is left to an empty iterator. * not exist, current is left to an empty iterator.
@ -424,13 +422,13 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
public MotorIterator() { public MotorIterator() {
this.iterator = iterator(); this.iterator = iterator();
} }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
getNext(); getNext();
return (next != null); return (next != null);
} }
@Override @Override
public MotorMount next() { public MotorMount next() {
getNext(); getNext();
@ -442,7 +440,7 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
next = null; next = null;
return ret; return ret;
} }
@Override @Override
public void remove() { public void remove() {
throw new UnsupportedOperationException("remove unsupported"); throw new UnsupportedOperationException("remove unsupported");
@ -463,5 +461,5 @@ public class Configuration implements Cloneable, ChangeSource, ComponentChangeLi
} }
} }
} }
} }