Merge branch 'unstable' into issue-905

This commit is contained in:
SiboVG 2021-07-06 21:24:52 +02:00 committed by GitHub
commit 78eb9bbcda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 10 deletions

View File

@ -0,0 +1,99 @@
package net.sf.openrocket.unit;
import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.util.BugException;
public class PercentageOfLengthUnit extends GeneralUnit {
private final FlightConfiguration configuration;
private final Rocket rocket;
private int rocketModId = -1;
private int configurationModId = -1;
private double referenceLength = -1;
public PercentageOfLengthUnit(FlightConfiguration configuration) {
super(0.01, "%");
this.configuration = configuration;
if (configuration == null) {
this.rocket = null;
} else {
this.rocket = configuration.getRocket();
}
}
public PercentageOfLengthUnit(Rocket rocket) {
super(0.01, "%");
this.configuration = null;
this.rocket = rocket;
}
public PercentageOfLengthUnit(double reference) {
super(0.01, "%");
this.configuration = null;
this.rocket = null;
this.referenceLength = reference;
if (reference <= 0) {
throw new IllegalArgumentException("Illegal reference = " + reference);
}
}
@Override
public double fromUnit(double value) {
checkLength();
return value * referenceLength * multiplier;
}
@Override
public double toUnit(double value) {
checkLength();
return value / referenceLength / multiplier;
}
private void checkLength() {
if (configuration != null && configuration.getModID() != configurationModId) {
referenceLength = -1;
configurationModId = configuration.getModID();
}
if (rocket != null && rocket.getModID() != rocketModId) {
referenceLength = -1;
rocketModId = rocket.getModID();
}
if (referenceLength < 0) {
if (configuration != null) {
referenceLength = getReferenceLength(configuration);
} else if (rocket != null) {
referenceLength = getReferenceLength(rocket);
} else {
throw new BugException("Both rocket and configuration are null");
}
}
}
/**
* Get the reference length of a rocket configuration.
*
* @param config the rocket configuration
* @return the reference length of the rocket
*/
public static double getReferenceLength(FlightConfiguration config) {
return config.getLength();
}
/**
* Get the reference length of a rocket.
*
* @param rocket the rocket
* @return the reference length of the rocket
*/
public static double getReferenceLength(Rocket rocket) {
return getReferenceLength(rocket.getSelectedConfiguration());
}
}

View File

@ -177,13 +177,13 @@ public class UnitGroup {
UNITS_STABILITY.addUnit(new GeneralUnit(1, "m"));
UNITS_STABILITY.addUnit(new GeneralUnit(0.0254, "in"));
UNITS_STABILITY.addUnit(new CaliberUnit((Rocket) null));
UNITS_STABILITY.addUnit(new PercentageOfLengthUnit((Rocket) null));
UNITS_STABILITY.setDefaultUnit(4);
UNITS_STABILITY_CALIBERS = new UnitGroup();
UNITS_STABILITY_CALIBERS.addUnit(new GeneralUnit(1, "cal"));
UNITS_STABILITY_CALIBERS.setDefaultUnit(0);
UNITS_VELOCITY = new UnitGroup();
UNITS_VELOCITY.addUnit(new GeneralUnit(1, "m/s"));
UNITS_VELOCITY.addUnit(new GeneralUnit(1 / 3.6, "km/h"));
@ -708,25 +708,24 @@ public class UnitGroup {
*/
private static class StabilityUnitGroup extends UnitGroup {
public StabilityUnitGroup(double ref) {
this(new CaliberUnit(ref));
}
public StabilityUnitGroup(double ref) { this(new CaliberUnit(ref), new PercentageOfLengthUnit(ref)); }
public StabilityUnitGroup(Rocket rocket) {
this(new CaliberUnit(rocket));
this(new CaliberUnit(rocket), new PercentageOfLengthUnit(rocket));
}
public StabilityUnitGroup(FlightConfiguration config) {
this(new CaliberUnit(config));
}
public StabilityUnitGroup(FlightConfiguration config) { this(new CaliberUnit(config), new PercentageOfLengthUnit(config)); }
private StabilityUnitGroup(CaliberUnit caliberUnit) {
private StabilityUnitGroup(CaliberUnit caliberUnit, PercentageOfLengthUnit percentageOfLengthUnit) {
this.units.addAll(UnitGroup.UNITS_STABILITY.units);
this.defaultUnit = UnitGroup.UNITS_STABILITY.defaultUnit;
for (int i = 0; i < units.size(); i++) {
if (units.get(i) instanceof CaliberUnit) {
units.set(i, caliberUnit);
}
if (units.get(i) instanceof PercentageOfLengthUnit) {
units.set(i, percentageOfLengthUnit);
}
}
}