From: Bill Kuker Date: Sun, 22 Jan 2012 15:04:57 +0000 (+0000) Subject: Move out the genuinely GPL tainted code, while I make a decision. X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=946644035687f5577b81efe37bd7d9d604880678;p=sw%2Fmotorsim Move out the genuinely GPL tainted code, while I make a decision. --- diff --git a/.classpath b/.classpath index 0b0564b..cb44e71 100644 --- a/.classpath +++ b/.classpath @@ -4,6 +4,7 @@ + diff --git a/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/OneMotorDatabase.java b/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/OneMotorDatabase.java new file mode 100644 index 0000000..09d1f72 --- /dev/null +++ b/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/OneMotorDatabase.java @@ -0,0 +1,332 @@ +package com.billkuker.rocketry.motorsim.visual.openRocket; +import java.util.List; +import java.util.Vector; + +import javax.measure.quantity.Duration; +import javax.measure.unit.SI; + +import net.sf.openrocket.database.MotorDatabase; +import net.sf.openrocket.models.atmosphere.AtmosphericConditions; +import net.sf.openrocket.motor.Motor; +import net.sf.openrocket.motor.MotorInstance; +import net.sf.openrocket.util.BugException; +import net.sf.openrocket.util.Coordinate; +import net.sf.openrocket.util.Inertia; +import net.sf.openrocket.util.MathUtil; + +import org.jscience.physics.amount.Amount; + +import com.billkuker.rocketry.motorsim.Burn; +import com.billkuker.rocketry.motorsim.Burn.Interval; +import com.billkuker.rocketry.motorsim.BurnSummary; +import com.billkuker.rocketry.motorsim.ICylindricalChamber; +import com.billkuker.rocketry.motorsim.RocketScience; + +public class OneMotorDatabase implements MotorDatabase { + + static Burn burn; + static BurnSummary bs; + static Coordinate cg[]; + static double[] time; + static double[] thrust; + + static Motor motor = new Motor() { + + @Override + public Type getMotorType() { + return Type.SINGLE; + } + + @Override + public String getDesignation() { + return bs.getRating(); + } + + @Override + public String getDesignation(double delay) { + return bs.getRating() + "-" + ((int) delay); + } + + @Override + public String getDescription() { + if ( burn == null ) + return "NO MOTOR YET"; + return burn.getMotor().getName(); + } + + @Override + public double getDiameter() { + return ((ICylindricalChamber) burn.getMotor().getChamber()).getOD() + .doubleValue(SI.METER); + } + + @Override + public double getLength() { + return ((ICylindricalChamber) burn.getMotor().getChamber()) + .getLength().doubleValue(SI.METER); + } + + @Override + public String getDigest() { + return "WTF"; + } + + @Override + public MotorInstance getInstance() { + + return new MotorInstance() { + + private int position; + + // Previous time step value + private double prevTime; + + // Average thrust during previous step + private double stepThrust; + // Instantaneous thrust at current time point + private double instThrust; + + // Average CG during previous step + private Coordinate stepCG; + // Instantaneous CG at current time point + private Coordinate instCG; + + private final double unitRotationalInertia; + private final double unitLongitudinalInertia; + + private int modID = 0; + + { + position = 0; + prevTime = 0; + instThrust = 0; + stepThrust = 0; + instCG = cg[0]; + stepCG = cg[0]; + unitRotationalInertia = Inertia + .filledCylinderRotational(getDiameter() / 2); + unitLongitudinalInertia = Inertia + .filledCylinderLongitudinal(getDiameter() / 2, + getLength()); + } + + @Override + public double getTime() { + return prevTime; + } + + @Override + public Coordinate getCG() { + return stepCG; + } + + @Override + public double getLongitudinalInertia() { + return unitLongitudinalInertia * stepCG.weight; + } + + @Override + public double getRotationalInertia() { + return unitRotationalInertia * stepCG.weight; + } + + @Override + public double getThrust() { + return stepThrust; + } + + @Override + public boolean isActive() { + return prevTime < time[time.length - 1]; + } + + @Override + public void step(double nextTime, double acceleration, + AtmosphericConditions cond) { + + if (!(nextTime >= prevTime)) { + // Also catches NaN + throw new IllegalArgumentException( + "Stepping backwards in time, current=" + + prevTime + " new=" + nextTime); + } + if (MathUtil.equals(prevTime, nextTime)) { + return; + } + + modID++; + + if (position >= time.length - 1) { + // Thrust has ended + prevTime = nextTime; + stepThrust = 0; + instThrust = 0; + stepCG = cg[cg.length - 1]; + return; + } + + // Compute average & instantaneous thrust + if (nextTime < time[position + 1]) { + + // Time step between time points + double nextF = MathUtil.map(nextTime, time[position], + time[position + 1], thrust[position], + thrust[position + 1]); + stepThrust = (instThrust + nextF) / 2; + instThrust = nextF; + + } else { + + // Portion of previous step + stepThrust = (instThrust + thrust[position + 1]) / 2 + * (time[position + 1] - prevTime); + + // Whole steps + position++; + while ((position < time.length - 1) + && (nextTime >= time[position + 1])) { + stepThrust += (thrust[position] + thrust[position + 1]) + / 2 * (time[position + 1] - time[position]); + position++; + } + + // End step + if (position < time.length - 1) { + instThrust = MathUtil.map(nextTime, time[position], + time[position + 1], thrust[position], + thrust[position + 1]); + stepThrust += (thrust[position] + instThrust) / 2 + * (nextTime - time[position]); + } else { + // Thrust ended during this step + instThrust = 0; + } + + stepThrust /= (nextTime - prevTime); + + } + + // Compute average and instantaneous CG (simple average + // between points) + Coordinate nextCG; + if (position < time.length - 1) { + nextCG = MathUtil.map(nextTime, time[position], + time[position + 1], cg[position], + cg[position + 1]); + } else { + nextCG = cg[cg.length - 1]; + } + stepCG = instCG.add(nextCG).multiply(0.5); + instCG = nextCG; + + // Update time + prevTime = nextTime; + } + + @Override + public MotorInstance clone() { + try { + return (MotorInstance) super.clone(); + } catch (CloneNotSupportedException e) { + throw new BugException("CloneNotSupportedException", e); + } + } + + @Override + public int getModID() { + return modID; + } + }; + + } + + @Override + public Coordinate getLaunchCG() { + return new Coordinate(); + } + + @Override + public Coordinate getEmptyCG() { + return new Coordinate(); + } + + @Override + public double getBurnTimeEstimate() { + return bs.thrustTime().doubleValue(SI.SECOND); + } + + @Override + public double getAverageThrustEstimate() { + return bs.averageThrust().doubleValue(SI.NEWTON); + } + + @Override + public double getMaxThrustEstimate() { + return bs.maxThrust().doubleValue(SI.NEWTON); + } + + @Override + public double getTotalImpulseEstimate() { + return bs.totalImpulse().doubleValue(RocketScience.NEWTON_SECOND); + } + + @Override + public double[] getTimePoints() { + return time; + } + + @Override + public double[] getThrustPoints() { + return thrust; + } + + @Override + public Coordinate[] getCGPoints() { + return cg; + } + + }; + + static void setBurn(Burn b) { + burn = b; + bs = new BurnSummary(b); + + cg = new Coordinate[burn.getData().size()]; + time = new double[burn.getData().size()]; + thrust = new double[burn.getData().size()]; + + Coordinate c = new Coordinate(); + + for (int i = 0; i < cg.length; i++) { + cg[i] = c; + } + + int i = 0; + for (Amount t : burn.getData().keySet()) { + time[i++] = t.doubleValue(SI.SECOND); + } + + i = 0; + for (Interval d : burn.getData().values()) { + double t = d.thrust.doubleValue(SI.NEWTON); + t = Math.max(t, 0.0001); + thrust[i++] = t; + } + thrust[0] = 0; + thrust[thrust.length - 1] = 0; + } + + public OneMotorDatabase() { + super(); + } + + public List findMotors(Motor.Type type, + String manufacturer, String designation, double diameter, + double length) { + List ret = new Vector(); + System.err.println("Returning " + motor.getDescription()); + ret.add(motor); + return ret; + } + +} diff --git a/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/RocketSimTable.java b/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/RocketSimTable.java new file mode 100644 index 0000000..b6155f3 --- /dev/null +++ b/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/RocketSimTable.java @@ -0,0 +1,228 @@ +package com.billkuker.rocketry.motorsim.visual.openRocket; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.io.File; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + +import javax.measure.unit.SI; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.SwingUtilities; +import javax.swing.table.AbstractTableModel; + +import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.document.Simulation; +import net.sf.openrocket.file.RocketLoadException; +import net.sf.openrocket.file.openrocket.OpenRocketLoader; +import net.sf.openrocket.gui.plot.PlotConfiguration; +import net.sf.openrocket.gui.plot.SimulationPlotDialog; +import net.sf.openrocket.gui.util.SwingPreferences; +import net.sf.openrocket.l10n.ResourceBundleTranslator; +import net.sf.openrocket.rocketcomponent.MotorMount; +import net.sf.openrocket.rocketcomponent.RocketComponent; +import net.sf.openrocket.simulation.exception.SimulationException; +import net.sf.openrocket.startup.Application; +import net.sf.openrocket.unit.UnitGroup; + +import org.jscience.physics.amount.Amount; + +import com.billkuker.rocketry.motorsim.Burn; +import com.billkuker.rocketry.motorsim.Motor; +import com.billkuker.rocketry.motorsim.RocketScience; +import com.billkuker.rocketry.motorsim.RocketScience.UnitPreference; +import com.billkuker.rocketry.motorsim.visual.workbench.BurnWatcher; + +public class RocketSimTable extends JScrollPane implements BurnWatcher, + RocketScience.UnitPreferenceListener { + static final long serialVersionUID = 1L; + + static { + Application.setBaseTranslator(new ResourceBundleTranslator( + "l10n.messages")); + Application.setMotorSetDatabase(new OneMotorDatabase()); + Application.setPreferences(new SwingPreferences()); + + } + + class Entry { + Entry(Motor m, Simulation s, Burn b) { + this.m = m; + this.s = s; + this.b = b; + } + + boolean ready = false; + Simulation s; + Motor m; + Burn b; + } + + List entries = new Vector(); + + class TM extends AbstractTableModel { + private static final long serialVersionUID = 1L; + + @Override + public String getColumnName(int col) { + switch (col) { + case 0: + return "Motor"; + case 1: + return "Apogee"; + case 2: + return "Vmax"; + case 3: + return "Vdeploy"; + case 4: + return "Vground"; + case 5: + return "Tapogee"; + case 6: + return "Tflight"; + } + return "??"; + } + + @Override + public int getColumnCount() { + return 7; + } + + @Override + public int getRowCount() { + return entries.size(); + } + + @Override + public Object getValueAt(int row, int col) { + Entry e = entries.get(row); + if (e == null) + return "???"; + if (e.ready == false) + return "..."; + switch (col) { + case 0: + return e.m.getName(); + case 1: + return RocketScience.ammountToRoundedString(Amount.valueOf(e.s + .getSimulatedData().getMaxAltitude(), SI.METER)); + case 2: + return RocketScience.ammountToRoundedString(Amount.valueOf(e.s + .getSimulatedData().getMaxVelocity(), + SI.METERS_PER_SECOND)); + case 3: + return RocketScience.ammountToRoundedString(Amount.valueOf(e.s + .getSimulatedData().getDeploymentVelocity(), + SI.METERS_PER_SECOND)); + case 4: + return RocketScience.ammountToRoundedString(Amount.valueOf(e.s + .getSimulatedData().getGroundHitVelocity(), + SI.METERS_PER_SECOND)); + case 5: + return RocketScience.ammountToRoundedString(Amount.valueOf(e.s + .getSimulatedData().getTimeToApogee(), SI.SECOND)); + case 6: + return RocketScience.ammountToRoundedString(Amount.valueOf(e.s + .getSimulatedData().getFlightTime(), SI.SECOND)); + } + return "??"; + } + + } + + private TM tm = new TM(); + private JTable table = new JTable(tm); + private OpenRocketDocument doc; + + public RocketSimTable() { + setViewportView(table); + RocketScience.addUnitPreferenceListener(this); + table.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + Entry entry = entries.get(table.getSelectedRow()); + SimulationPlotDialog.showPlot(SwingUtilities + .getWindowAncestor(RocketSimTable.this), entry.s, + PlotConfiguration.DEFAULT_CONFIGURATIONS[0] + .resetUnits()); + } + } + }); + preferredUnitsChanged(); + } + + public void openRocket(File f) throws RocketLoadException { + this.doc = new OpenRocketLoader().load(f); + } + + private Entry toEntry(Burn b) { + final Entry e = new Entry(b.getMotor(), doc.getSimulation(0).copy(), b); + OneMotorDatabase.setBurn(b); + + e.s.getConfiguration().getMotorConfigurationID(); + Iterator iterator = doc.getRocket().iterator(); + while (iterator.hasNext()) { + RocketComponent c = iterator.next(); + if (c instanceof MotorMount) { + ((MotorMount) c).setMotorDelay(e.s.getConfiguration() + .getMotorConfigurationID(), b.getMotor() + .getEjectionDelay().doubleValue(SI.SECOND)); + } + } + + tm.fireTableDataChanged(); + new Thread() { + @Override + public void run() { + try { + e.s.simulate(); + e.ready = true; + tm.fireTableDataChanged(); + } catch (SimulationException e1) { + e1.printStackTrace(); + } + + } + }.start(); + + return e; + } + + @Override + public void replace(Burn oldBurn, Burn newBurn) { + if (doc == null) { + return; // TODO, deal with changing rockets + } + + if (oldBurn != null) { + for (int i = 0; i < entries.size(); i++) { + if (entries.get(i).b == oldBurn) { + if (newBurn != null) { + entries.set(i, toEntry(newBurn)); + } else { + entries.remove(i); + } + break; + } + } + } else { + entries.add(toEntry(newBurn)); + } + tm.fireTableDataChanged(); + } + + @Override + public void preferredUnitsChanged() { + tm.fireTableDataChanged(); + if (UnitPreference.getUnitPreference() == UnitPreference.NONSI) { + System.err.println("NONSI"); + UnitGroup.setDefaultImperialUnits(); + } else { + System.err.println("SI"); + UnitGroup.setDefaultMetricUnits(); + } + } +} diff --git a/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/Test.java b/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/Test.java new file mode 100644 index 0000000..56fe81b --- /dev/null +++ b/gpl/com/billkuker/rocketry/motorsim/visual/openRocket/Test.java @@ -0,0 +1,36 @@ +package com.billkuker.rocketry.motorsim.visual.openRocket; + +import java.io.File; + +import javax.swing.JFrame; + +import com.billkuker.rocketry.motorsim.Burn; +import com.billkuker.rocketry.motorsim.RocketScience.UnitPreference; +import com.billkuker.rocketry.motorsim.motors.kuker.PVC9; + +public class Test { + + /** + * @param args + */ + public static void main(String[] args) throws Exception { + + UnitPreference.setUnitPreference(UnitPreference.NONSI); + + JFrame frame = new JFrame(); + frame.setSize(1024, 768); + + RocketSimTable st = new RocketSimTable(); + st.openRocket(new File("simple.ork")); + + com.billkuker.rocketry.motorsim.Motor m = new PVC9(); + Burn b = new Burn(m); + b.burn(); + st.replace(null, b); + + frame.setContentPane(st); + frame.setVisible(true); + + } + +} diff --git a/gui/com/billkuker/rocketry/motorsim/visual/openRocket/OneMotorDatabase.java b/gui/com/billkuker/rocketry/motorsim/visual/openRocket/OneMotorDatabase.java deleted file mode 100644 index 09d1f72..0000000 --- a/gui/com/billkuker/rocketry/motorsim/visual/openRocket/OneMotorDatabase.java +++ /dev/null @@ -1,332 +0,0 @@ -package com.billkuker.rocketry.motorsim.visual.openRocket; -import java.util.List; -import java.util.Vector; - -import javax.measure.quantity.Duration; -import javax.measure.unit.SI; - -import net.sf.openrocket.database.MotorDatabase; -import net.sf.openrocket.models.atmosphere.AtmosphericConditions; -import net.sf.openrocket.motor.Motor; -import net.sf.openrocket.motor.MotorInstance; -import net.sf.openrocket.util.BugException; -import net.sf.openrocket.util.Coordinate; -import net.sf.openrocket.util.Inertia; -import net.sf.openrocket.util.MathUtil; - -import org.jscience.physics.amount.Amount; - -import com.billkuker.rocketry.motorsim.Burn; -import com.billkuker.rocketry.motorsim.Burn.Interval; -import com.billkuker.rocketry.motorsim.BurnSummary; -import com.billkuker.rocketry.motorsim.ICylindricalChamber; -import com.billkuker.rocketry.motorsim.RocketScience; - -public class OneMotorDatabase implements MotorDatabase { - - static Burn burn; - static BurnSummary bs; - static Coordinate cg[]; - static double[] time; - static double[] thrust; - - static Motor motor = new Motor() { - - @Override - public Type getMotorType() { - return Type.SINGLE; - } - - @Override - public String getDesignation() { - return bs.getRating(); - } - - @Override - public String getDesignation(double delay) { - return bs.getRating() + "-" + ((int) delay); - } - - @Override - public String getDescription() { - if ( burn == null ) - return "NO MOTOR YET"; - return burn.getMotor().getName(); - } - - @Override - public double getDiameter() { - return ((ICylindricalChamber) burn.getMotor().getChamber()).getOD() - .doubleValue(SI.METER); - } - - @Override - public double getLength() { - return ((ICylindricalChamber) burn.getMotor().getChamber()) - .getLength().doubleValue(SI.METER); - } - - @Override - public String getDigest() { - return "WTF"; - } - - @Override - public MotorInstance getInstance() { - - return new MotorInstance() { - - private int position; - - // Previous time step value - private double prevTime; - - // Average thrust during previous step - private double stepThrust; - // Instantaneous thrust at current time point - private double instThrust; - - // Average CG during previous step - private Coordinate stepCG; - // Instantaneous CG at current time point - private Coordinate instCG; - - private final double unitRotationalInertia; - private final double unitLongitudinalInertia; - - private int modID = 0; - - { - position = 0; - prevTime = 0; - instThrust = 0; - stepThrust = 0; - instCG = cg[0]; - stepCG = cg[0]; - unitRotationalInertia = Inertia - .filledCylinderRotational(getDiameter() / 2); - unitLongitudinalInertia = Inertia - .filledCylinderLongitudinal(getDiameter() / 2, - getLength()); - } - - @Override - public double getTime() { - return prevTime; - } - - @Override - public Coordinate getCG() { - return stepCG; - } - - @Override - public double getLongitudinalInertia() { - return unitLongitudinalInertia * stepCG.weight; - } - - @Override - public double getRotationalInertia() { - return unitRotationalInertia * stepCG.weight; - } - - @Override - public double getThrust() { - return stepThrust; - } - - @Override - public boolean isActive() { - return prevTime < time[time.length - 1]; - } - - @Override - public void step(double nextTime, double acceleration, - AtmosphericConditions cond) { - - if (!(nextTime >= prevTime)) { - // Also catches NaN - throw new IllegalArgumentException( - "Stepping backwards in time, current=" - + prevTime + " new=" + nextTime); - } - if (MathUtil.equals(prevTime, nextTime)) { - return; - } - - modID++; - - if (position >= time.length - 1) { - // Thrust has ended - prevTime = nextTime; - stepThrust = 0; - instThrust = 0; - stepCG = cg[cg.length - 1]; - return; - } - - // Compute average & instantaneous thrust - if (nextTime < time[position + 1]) { - - // Time step between time points - double nextF = MathUtil.map(nextTime, time[position], - time[position + 1], thrust[position], - thrust[position + 1]); - stepThrust = (instThrust + nextF) / 2; - instThrust = nextF; - - } else { - - // Portion of previous step - stepThrust = (instThrust + thrust[position + 1]) / 2 - * (time[position + 1] - prevTime); - - // Whole steps - position++; - while ((position < time.length - 1) - && (nextTime >= time[position + 1])) { - stepThrust += (thrust[position] + thrust[position + 1]) - / 2 * (time[position + 1] - time[position]); - position++; - } - - // End step - if (position < time.length - 1) { - instThrust = MathUtil.map(nextTime, time[position], - time[position + 1], thrust[position], - thrust[position + 1]); - stepThrust += (thrust[position] + instThrust) / 2 - * (nextTime - time[position]); - } else { - // Thrust ended during this step - instThrust = 0; - } - - stepThrust /= (nextTime - prevTime); - - } - - // Compute average and instantaneous CG (simple average - // between points) - Coordinate nextCG; - if (position < time.length - 1) { - nextCG = MathUtil.map(nextTime, time[position], - time[position + 1], cg[position], - cg[position + 1]); - } else { - nextCG = cg[cg.length - 1]; - } - stepCG = instCG.add(nextCG).multiply(0.5); - instCG = nextCG; - - // Update time - prevTime = nextTime; - } - - @Override - public MotorInstance clone() { - try { - return (MotorInstance) super.clone(); - } catch (CloneNotSupportedException e) { - throw new BugException("CloneNotSupportedException", e); - } - } - - @Override - public int getModID() { - return modID; - } - }; - - } - - @Override - public Coordinate getLaunchCG() { - return new Coordinate(); - } - - @Override - public Coordinate getEmptyCG() { - return new Coordinate(); - } - - @Override - public double getBurnTimeEstimate() { - return bs.thrustTime().doubleValue(SI.SECOND); - } - - @Override - public double getAverageThrustEstimate() { - return bs.averageThrust().doubleValue(SI.NEWTON); - } - - @Override - public double getMaxThrustEstimate() { - return bs.maxThrust().doubleValue(SI.NEWTON); - } - - @Override - public double getTotalImpulseEstimate() { - return bs.totalImpulse().doubleValue(RocketScience.NEWTON_SECOND); - } - - @Override - public double[] getTimePoints() { - return time; - } - - @Override - public double[] getThrustPoints() { - return thrust; - } - - @Override - public Coordinate[] getCGPoints() { - return cg; - } - - }; - - static void setBurn(Burn b) { - burn = b; - bs = new BurnSummary(b); - - cg = new Coordinate[burn.getData().size()]; - time = new double[burn.getData().size()]; - thrust = new double[burn.getData().size()]; - - Coordinate c = new Coordinate(); - - for (int i = 0; i < cg.length; i++) { - cg[i] = c; - } - - int i = 0; - for (Amount t : burn.getData().keySet()) { - time[i++] = t.doubleValue(SI.SECOND); - } - - i = 0; - for (Interval d : burn.getData().values()) { - double t = d.thrust.doubleValue(SI.NEWTON); - t = Math.max(t, 0.0001); - thrust[i++] = t; - } - thrust[0] = 0; - thrust[thrust.length - 1] = 0; - } - - public OneMotorDatabase() { - super(); - } - - public List findMotors(Motor.Type type, - String manufacturer, String designation, double diameter, - double length) { - List ret = new Vector(); - System.err.println("Returning " + motor.getDescription()); - ret.add(motor); - return ret; - } - -} diff --git a/gui/com/billkuker/rocketry/motorsim/visual/openRocket/RocketSimTable.java b/gui/com/billkuker/rocketry/motorsim/visual/openRocket/RocketSimTable.java deleted file mode 100644 index b6155f3..0000000 --- a/gui/com/billkuker/rocketry/motorsim/visual/openRocket/RocketSimTable.java +++ /dev/null @@ -1,228 +0,0 @@ -package com.billkuker.rocketry.motorsim.visual.openRocket; - -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.io.File; -import java.util.Iterator; -import java.util.List; -import java.util.Vector; - -import javax.measure.unit.SI; -import javax.swing.JScrollPane; -import javax.swing.JTable; -import javax.swing.SwingUtilities; -import javax.swing.table.AbstractTableModel; - -import net.sf.openrocket.document.OpenRocketDocument; -import net.sf.openrocket.document.Simulation; -import net.sf.openrocket.file.RocketLoadException; -import net.sf.openrocket.file.openrocket.OpenRocketLoader; -import net.sf.openrocket.gui.plot.PlotConfiguration; -import net.sf.openrocket.gui.plot.SimulationPlotDialog; -import net.sf.openrocket.gui.util.SwingPreferences; -import net.sf.openrocket.l10n.ResourceBundleTranslator; -import net.sf.openrocket.rocketcomponent.MotorMount; -import net.sf.openrocket.rocketcomponent.RocketComponent; -import net.sf.openrocket.simulation.exception.SimulationException; -import net.sf.openrocket.startup.Application; -import net.sf.openrocket.unit.UnitGroup; - -import org.jscience.physics.amount.Amount; - -import com.billkuker.rocketry.motorsim.Burn; -import com.billkuker.rocketry.motorsim.Motor; -import com.billkuker.rocketry.motorsim.RocketScience; -import com.billkuker.rocketry.motorsim.RocketScience.UnitPreference; -import com.billkuker.rocketry.motorsim.visual.workbench.BurnWatcher; - -public class RocketSimTable extends JScrollPane implements BurnWatcher, - RocketScience.UnitPreferenceListener { - static final long serialVersionUID = 1L; - - static { - Application.setBaseTranslator(new ResourceBundleTranslator( - "l10n.messages")); - Application.setMotorSetDatabase(new OneMotorDatabase()); - Application.setPreferences(new SwingPreferences()); - - } - - class Entry { - Entry(Motor m, Simulation s, Burn b) { - this.m = m; - this.s = s; - this.b = b; - } - - boolean ready = false; - Simulation s; - Motor m; - Burn b; - } - - List entries = new Vector(); - - class TM extends AbstractTableModel { - private static final long serialVersionUID = 1L; - - @Override - public String getColumnName(int col) { - switch (col) { - case 0: - return "Motor"; - case 1: - return "Apogee"; - case 2: - return "Vmax"; - case 3: - return "Vdeploy"; - case 4: - return "Vground"; - case 5: - return "Tapogee"; - case 6: - return "Tflight"; - } - return "??"; - } - - @Override - public int getColumnCount() { - return 7; - } - - @Override - public int getRowCount() { - return entries.size(); - } - - @Override - public Object getValueAt(int row, int col) { - Entry e = entries.get(row); - if (e == null) - return "???"; - if (e.ready == false) - return "..."; - switch (col) { - case 0: - return e.m.getName(); - case 1: - return RocketScience.ammountToRoundedString(Amount.valueOf(e.s - .getSimulatedData().getMaxAltitude(), SI.METER)); - case 2: - return RocketScience.ammountToRoundedString(Amount.valueOf(e.s - .getSimulatedData().getMaxVelocity(), - SI.METERS_PER_SECOND)); - case 3: - return RocketScience.ammountToRoundedString(Amount.valueOf(e.s - .getSimulatedData().getDeploymentVelocity(), - SI.METERS_PER_SECOND)); - case 4: - return RocketScience.ammountToRoundedString(Amount.valueOf(e.s - .getSimulatedData().getGroundHitVelocity(), - SI.METERS_PER_SECOND)); - case 5: - return RocketScience.ammountToRoundedString(Amount.valueOf(e.s - .getSimulatedData().getTimeToApogee(), SI.SECOND)); - case 6: - return RocketScience.ammountToRoundedString(Amount.valueOf(e.s - .getSimulatedData().getFlightTime(), SI.SECOND)); - } - return "??"; - } - - } - - private TM tm = new TM(); - private JTable table = new JTable(tm); - private OpenRocketDocument doc; - - public RocketSimTable() { - setViewportView(table); - RocketScience.addUnitPreferenceListener(this); - table.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - Entry entry = entries.get(table.getSelectedRow()); - SimulationPlotDialog.showPlot(SwingUtilities - .getWindowAncestor(RocketSimTable.this), entry.s, - PlotConfiguration.DEFAULT_CONFIGURATIONS[0] - .resetUnits()); - } - } - }); - preferredUnitsChanged(); - } - - public void openRocket(File f) throws RocketLoadException { - this.doc = new OpenRocketLoader().load(f); - } - - private Entry toEntry(Burn b) { - final Entry e = new Entry(b.getMotor(), doc.getSimulation(0).copy(), b); - OneMotorDatabase.setBurn(b); - - e.s.getConfiguration().getMotorConfigurationID(); - Iterator iterator = doc.getRocket().iterator(); - while (iterator.hasNext()) { - RocketComponent c = iterator.next(); - if (c instanceof MotorMount) { - ((MotorMount) c).setMotorDelay(e.s.getConfiguration() - .getMotorConfigurationID(), b.getMotor() - .getEjectionDelay().doubleValue(SI.SECOND)); - } - } - - tm.fireTableDataChanged(); - new Thread() { - @Override - public void run() { - try { - e.s.simulate(); - e.ready = true; - tm.fireTableDataChanged(); - } catch (SimulationException e1) { - e1.printStackTrace(); - } - - } - }.start(); - - return e; - } - - @Override - public void replace(Burn oldBurn, Burn newBurn) { - if (doc == null) { - return; // TODO, deal with changing rockets - } - - if (oldBurn != null) { - for (int i = 0; i < entries.size(); i++) { - if (entries.get(i).b == oldBurn) { - if (newBurn != null) { - entries.set(i, toEntry(newBurn)); - } else { - entries.remove(i); - } - break; - } - } - } else { - entries.add(toEntry(newBurn)); - } - tm.fireTableDataChanged(); - } - - @Override - public void preferredUnitsChanged() { - tm.fireTableDataChanged(); - if (UnitPreference.getUnitPreference() == UnitPreference.NONSI) { - System.err.println("NONSI"); - UnitGroup.setDefaultImperialUnits(); - } else { - System.err.println("SI"); - UnitGroup.setDefaultMetricUnits(); - } - } -} diff --git a/gui/com/billkuker/rocketry/motorsim/visual/openRocket/Test.java b/gui/com/billkuker/rocketry/motorsim/visual/openRocket/Test.java deleted file mode 100644 index 56fe81b..0000000 --- a/gui/com/billkuker/rocketry/motorsim/visual/openRocket/Test.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.billkuker.rocketry.motorsim.visual.openRocket; - -import java.io.File; - -import javax.swing.JFrame; - -import com.billkuker.rocketry.motorsim.Burn; -import com.billkuker.rocketry.motorsim.RocketScience.UnitPreference; -import com.billkuker.rocketry.motorsim.motors.kuker.PVC9; - -public class Test { - - /** - * @param args - */ - public static void main(String[] args) throws Exception { - - UnitPreference.setUnitPreference(UnitPreference.NONSI); - - JFrame frame = new JFrame(); - frame.setSize(1024, 768); - - RocketSimTable st = new RocketSimTable(); - st.openRocket(new File("simple.ork")); - - com.billkuker.rocketry.motorsim.Motor m = new PVC9(); - Burn b = new Burn(m); - b.burn(); - st.replace(null, b); - - frame.setContentPane(st); - frame.setVisible(true); - - } - -}