create changelog entry
[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                 clearPreset();
65                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
66         }
67
68
69         @Override
70         public double getInnerRadius() {
71                 return innerRadius;
72         }
73         @Override
74         public void setInnerRadius(double r) {
75                 r = Math.max(r,0);
76                 if (MathUtil.equals(innerRadius, r))
77                         return;
78
79                 innerRadius = r;
80                 innerRadiusAutomatic = false;
81                 if (getOuterRadius() < r) {
82                         outerRadius = r;
83                         outerRadiusAutomatic = false;
84                 }
85
86                 clearPreset();
87                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
88         }
89
90
91         @Override
92         public double getThickness() {
93                 return Math.max(getOuterRadius() - getInnerRadius(), 0);
94         }
95         @Override
96         public void setThickness(double thickness) {
97                 double outer = getOuterRadius();
98
99                 thickness = MathUtil.clamp(thickness, 0, outer);
100                 setInnerRadius(outer - thickness);
101         }
102
103 }