create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / dialogs / motor / thrustcurve / MotorClass.java
1 package net.sf.openrocket.gui.dialogs.motor.thrustcurve;
2
3 import net.sf.openrocket.unit.UnitGroup;
4 import net.sf.openrocket.util.BugException;
5
6 /**
7  * NAR approved motor classes (http://www.nar.org/NARmotors.html).
8  * 
9  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
10  */
11 public enum MotorClass {
12         
13         A18("1/8A", 0, 0.3125),
14         A14("1/4A", 0.3125, 0.625),
15         A12("1/2A", 0.625, 1.25),
16         A("A", 1.25, 2.5),
17         B("B", 2.5, 5),
18         C("C", 5, 10),
19         D("D", 10, 20),
20         E("E", 20, 40),
21         F("F", 40, 80),
22         G("G", 80, 160),
23         H("H", 160, 320),
24         I("I", 320, 640),
25         J("J", 640, 1280),
26         K("K", 1280, 2560),
27         L("L", 2560, 5120),
28         M("M", 5120, 10240),
29         N("N", 10240, 20480),
30         O("O", 20480, 40960),
31         OVER("> O", 40960, Double.MAX_VALUE) {
32                 @Override
33                 public String getDescription(double impulse) {
34                         return "Over O";
35                 }
36                 
37                 @Override
38                 public String getClassDescription() {
39                         return "Over O-class (over " + UnitGroup.UNITS_IMPULSE.toStringUnit(40960) + ")";
40                 }
41         };
42         
43         
44         private final String className;
45         private final double min;
46         private final double max;
47         
48         
49         private MotorClass(String className, double min, double max) {
50                 this.className = className;
51                 this.min = min;
52                 this.max = max;
53         }
54         
55         
56         public String getDescription(double impulse) {
57                 double percent = (impulse - min) / (max - min) * 100;
58                 if (percent < 1) {
59                         // 0% looks stupid
60                         percent = 1;
61                 }
62                 return String.format("%d%% %s", Math.round(percent), className);
63         }
64         
65         public String getClassDescription() {
66                 return "Class " + className + " (" + UnitGroup.UNITS_IMPULSE.toStringUnit(min) + " - " + UnitGroup.UNITS_IMPULSE.toStringUnit(max) + ")";
67         }
68         
69         
70         /**
71          * Find the appropriate motor class for the provided impulse.
72          */
73         public static MotorClass getMotorClass(double impulse) {
74                 double epsilon = 0.0000001;
75                 
76                 // Round large values so 640.1 Ns (which is displayed as 640 Ns) is counted as I-class
77                 if (impulse >= 100) {
78                         impulse = Math.rint(impulse);
79                 }
80                 
81                 for (MotorClass m : MotorClass.values()) {
82                         if (impulse <= m.max + epsilon) {
83                                 return m;
84                         }
85                 }
86                 throw new BugException("Could not find motor class for impulse " + impulse);
87         }
88         
89 }