Implemented support for bulk heads. Move setting Length from BodyComponent to Rocket...
[debian/openrocket] / core / src / net / sf / openrocket / rocketcomponent / RadiusRingComponent.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import net.sf.openrocket.preset.ComponentPreset;
4 import net.sf.openrocket.util.Coordinate;
5 import net.sf.openrocket.util.MathUtil;
6
7 /**
8  * An inner component that consists of a hollow cylindrical component.  This can be
9  * an inner tube, tube coupler, centering ring, bulkhead etc.
10  *
11  * The properties include the inner and outer radii, length and radial position.
12  *
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public abstract class RadiusRingComponent extends RingComponent implements Coaxial {
16
17         protected double outerRadius = 0;
18         protected double innerRadius = 0;
19
20         @Override
21         protected void loadFromPreset(ComponentPreset preset) {
22                 super.loadFromPreset(preset);
23                 if ( preset.has(ComponentPreset.OUTER_DIAMETER)) {
24                         this.outerRadius = preset.get(ComponentPreset.OUTER_DIAMETER) / 2.0;
25                         this.outerRadiusAutomatic = false;
26                 }
27                 this.innerRadiusAutomatic = false;
28                 if ( preset.has(ComponentPreset.INNER_DIAMETER)) {
29                         this.innerRadius = preset.get(ComponentPreset.INNER_DIAMETER) / 2.0;
30                 }
31
32                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
33
34         }
35
36         @Override
37         public double getOuterRadius() {
38                 if (outerRadiusAutomatic && getParent() instanceof RadialParent) {
39                         RocketComponent parent = getParent();
40                         double pos1 = this.toRelative(Coordinate.NUL, parent)[0].x;
41                         double pos2 = this.toRelative(new Coordinate(getLength()), parent)[0].x;
42                         pos1 = MathUtil.clamp(pos1, 0, parent.getLength());
43                         pos2 = MathUtil.clamp(pos2, 0, parent.getLength());
44                         outerRadius = Math.min(((RadialParent)parent).getInnerRadius(pos1),
45                                         ((RadialParent)parent).getInnerRadius(pos2));
46                 }
47
48                 return outerRadius;
49         }
50
51         @Override
52         public void setOuterRadius(double r) {
53                 r = Math.max(r,0);
54                 if (MathUtil.equals(outerRadius, r) && !isOuterRadiusAutomatic())
55                         return;
56
57                 outerRadius = r;
58                 outerRadiusAutomatic = false;
59                 if (getInnerRadius() > r) {
60                         innerRadius = r;
61                         innerRadiusAutomatic = false;
62                 }
63
64                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
65         }
66
67
68         @Override
69         public double getInnerRadius() {
70                 return innerRadius;
71         }
72         @Override
73         public void setInnerRadius(double r) {
74                 r = Math.max(r,0);
75                 if (MathUtil.equals(innerRadius, r))
76                         return;
77
78                 innerRadius = r;
79                 innerRadiusAutomatic = false;
80                 if (getOuterRadius() < r) {
81                         outerRadius = r;
82                         outerRadiusAutomatic = false;
83                 }
84
85                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
86         }
87
88
89         @Override
90         public double getThickness() {
91                 return Math.max(getOuterRadius() - getInnerRadius(), 0);
92         }
93         @Override
94         public void setThickness(double thickness) {
95                 double outer = getOuterRadius();
96
97                 thickness = MathUtil.clamp(thickness, 0, outer);
98                 setInnerRadius(outer - thickness);
99         }
100
101 }