9f96bbdc9d740282e2f2834a8954af3b31e7be69
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / ExternalComponent.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import net.sf.openrocket.material.Material;
4 import net.sf.openrocket.unit.UnitGroup;
5 import net.sf.openrocket.util.Prefs;
6
7 /**
8  * Class of components with well-defined physical appearance and which have an effect on
9  * aerodynamic simulation.  They have material defined for them, and the mass of the component 
10  * is calculated using the component's volume.
11  * 
12  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
13  */
14
15 public abstract class ExternalComponent extends RocketComponent {
16         
17         public enum Finish {
18                 ROUGH("Rough", 500e-6),
19                 UNFINISHED("Unfinished", 150e-6),
20                 NORMAL("Regular paint", 60e-6),
21                 SMOOTH("Smooth paint", 20e-6),
22                 POLISHED("Polished", 2e-6);
23                 
24                 private final String name;
25                 private final double roughnessSize;
26                 
27                 Finish(String name, double roughness) {
28                         this.name = name;
29                         this.roughnessSize = roughness;
30                 }
31                 
32                 public double getRoughnessSize() {
33                         return roughnessSize;
34                 }
35                 
36                 @Override
37                 public String toString() {
38                         return name + " (" + UnitGroup.UNITS_ROUGHNESS.toStringUnit(roughnessSize) + ")";
39                 }
40         }
41         
42
43         /**
44          * The material of the component.
45          */
46         protected Material material=null;
47         
48         protected Finish finish = Finish.NORMAL;
49         
50         
51         
52         /**
53          * Constructor that sets the relative position of the component.
54          */
55         public ExternalComponent(RocketComponent.Position relativePosition) {
56                 super(relativePosition);
57                 this.material = Prefs.getDefaultComponentMaterial(this.getClass(), Material.Type.BULK);
58         }
59
60         /**
61          * Returns the volume of the component.  This value is used in calculating the mass
62          * of the object.
63          */
64         public abstract double getComponentVolume();
65
66         /**
67          * Calculates the mass of the component as the product of the volume and interior density.
68          */
69         @Override
70         public double getComponentMass() {
71                 return material.getDensity() * getComponentVolume();
72         }
73
74         /**
75          * ExternalComponent has aerodynamic effect, so return true.
76          */
77         @Override
78         public boolean isAerodynamic() {
79                 return true;
80         }
81         
82         /**
83          * ExternalComponent has effect on the mass, so return true.
84          */
85         @Override
86         public boolean isMassive() {
87                 return true;
88         }
89         
90         
91         public Material getMaterial() {
92                 return material;
93         }
94         
95         public void setMaterial(Material mat) {
96                 if (mat.getType() != Material.Type.BULK) {
97                         throw new IllegalArgumentException("ExternalComponent requires a bulk material" +
98                                         " type="+mat.getType());
99                 }
100
101                 if (material.equals(mat))
102                         return;
103                 material = mat;
104                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
105         }
106         
107         public Finish getFinish() {
108                 return finish;
109         }
110         
111         public void setFinish(Finish finish) {
112                 if (this.finish == finish)
113                         return;
114                 this.finish = finish;
115                 fireComponentChangeEvent(ComponentChangeEvent.AERODYNAMIC_CHANGE);
116         }
117         
118 }