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