1 package net.sf.openrocket.unit;
3 import java.util.Iterator;
5 import net.sf.openrocket.rocketcomponent.Configuration;
6 import net.sf.openrocket.rocketcomponent.Rocket;
7 import net.sf.openrocket.rocketcomponent.RocketComponent;
8 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
9 import net.sf.openrocket.util.BugException;
10 import net.sf.openrocket.util.MathUtil;
13 public class CaliberUnit extends GeneralUnit {
15 public static final double DEFAULT_CALIBER = 0.01;
17 private final Configuration configuration;
18 private final Rocket rocket;
20 private int rocketModId = -1;
21 private int configurationModId = -1;
23 private double caliber = -1;
28 public CaliberUnit(Configuration configuration) {
30 this.configuration = configuration;
32 if (configuration == null) {
35 this.rocket = configuration.getRocket();
39 public CaliberUnit(Rocket rocket) {
41 this.configuration = null;
45 public CaliberUnit(double reference) {
47 this.configuration = null;
49 this.caliber = reference;
52 throw new IllegalArgumentException("Illegal reference = " + reference);
58 public double fromUnit(double value) {
61 return value * caliber;
65 public double toUnit(double value) {
68 return value / caliber;
73 private void checkCaliber() {
74 if (configuration != null && configuration.getModID() != configurationModId) {
76 configurationModId = configuration.getModID();
78 if (rocket != null && rocket.getModID() != rocketModId) {
80 rocketModId = rocket.getModID();
83 if (configuration != null) {
84 caliber = calculateCaliber(configuration);
85 } else if (rocket != null) {
86 caliber = calculateCaliber(rocket);
88 throw new BugException("Both rocket and configuration are null");
95 * Calculate the caliber of a rocket configuration.
97 * @param config the rocket configuration
98 * @return the caliber of the rocket, or the default caliber.
100 public static double calculateCaliber(Configuration config) {
101 return calculateCaliber(config.iterator());
105 * Calculate the caliber of a rocket.
107 * @param rocket the rocket
108 * @return the caliber of the rocket, or the default caliber.
110 public static double calculateCaliber(Rocket rocket) {
111 return calculateCaliber(rocket.iterator());
116 private static double calculateCaliber(Iterator<RocketComponent> iterator) {
119 while (iterator.hasNext()) {
120 RocketComponent c = iterator.next();
121 if (c instanceof SymmetricComponent) {
122 double r1 = ((SymmetricComponent) c).getForeRadius() * 2;
123 double r2 = ((SymmetricComponent) c).getAftRadius() * 2;
124 cal = MathUtil.max(cal, r1, r2);
129 cal = DEFAULT_CALIBER;