Initial commit
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / ThicknessRingComponent.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 ThicknessRingComponent extends RingComponent {
15
16         protected double outerRadius = 0;
17         protected double thickness = 0;
18         
19         
20         @Override
21         public double getOuterRadius() {
22                 if (isOuterRadiusAutomatic() && getParent() instanceof RadialParent) {
23                         RocketComponent parent = getParent();
24                         double pos1 = this.toRelative(Coordinate.NUL, parent)[0].x;
25                         double pos2 = this.toRelative(new Coordinate(getLength()), parent)[0].x;
26                         pos1 = MathUtil.clamp(pos1, 0, parent.getLength());
27                         pos2 = MathUtil.clamp(pos2, 0, parent.getLength());
28                         outerRadius = Math.min(((RadialParent)parent).getInnerRadius(pos1),
29                                         ((RadialParent)parent).getInnerRadius(pos2));
30                 }
31                                 
32                 return outerRadius;
33         }
34
35         
36         @Override
37         public void setOuterRadius(double r) {
38                 r = Math.max(r,0);
39                 if (MathUtil.equals(outerRadius, r) && !isOuterRadiusAutomatic())
40                         return;
41                 
42                 outerRadius = r;
43                 outerRadiusAutomatic = false;
44
45                 if (thickness > outerRadius)
46                         thickness = outerRadius;
47                 
48                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
49         }
50         
51         
52
53         @Override
54         public double getThickness() {
55                 return Math.min(thickness, getOuterRadius());
56         }
57         @Override
58         public void setThickness(double thickness) {
59                 double outer = getOuterRadius();
60                 
61                 thickness = MathUtil.clamp(thickness, 0, outer);
62                 if (MathUtil.equals(getThickness(), thickness))
63                         return;
64                 
65                 this.thickness = thickness;
66                 
67                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
68         }
69
70         
71         @Override
72         public double getInnerRadius() {
73                 return Math.max(getOuterRadius()-thickness, 0);
74         }
75         @Override
76         public void setInnerRadius(double r) {
77                 r = Math.max(r,0);
78                 setThickness(getOuterRadius() - r);
79         }
80         
81         
82 }