ec244ee0ecc5f8000a26f53c46d3cbaffa700b8f
[debian/openrocket] / core / src / net / sf / openrocket / rocketcomponent / Sleeve.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 /**
8  * A RingComponent that comes on top of another tube.  It's defined by the inner
9  * radius and thickness.  The inner radius can be automatic, in which case it
10  * takes the radius of the parent component.
11  *  
12  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
13  */
14 public class Sleeve extends RingComponent {
15         
16         protected double innerRadius = 0;
17         protected double thickness = 0;
18         
19         
20         public Sleeve() {
21                 super();
22                 setInnerRadiusAutomatic(true);
23                 setThickness(0.001);
24                 setLength(0.05);
25         }
26         
27         
28         @Override
29         public double getOuterRadius() {
30                 return getInnerRadius() + thickness;
31         }
32         
33         @Override
34         public void setOuterRadius(double r) {
35                 if (MathUtil.equals(getOuterRadius(), r))
36                         return;
37                 
38                 innerRadius = Math.max(r - thickness, 0);
39                 if (thickness > r)
40                         thickness = r;
41                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
42         }
43         
44         
45         @Override
46         public double getInnerRadius() {
47                 // Implement parent inner radius automation
48                 if (isInnerRadiusAutomatic() && getParent() instanceof RadialParent) {
49                         RocketComponent parent = getParent();
50                         double pos1 = this.toRelative(Coordinate.NUL, parent)[0].x;
51                         double pos2 = this.toRelative(new Coordinate(getLength()), parent)[0].x;
52                         pos1 = MathUtil.clamp(pos1, 0, parent.getLength());
53                         pos2 = MathUtil.clamp(pos2, 0, parent.getLength());
54                         innerRadius = Math.max(((RadialParent) parent).getOuterRadius(pos1),
55                                         ((RadialParent) parent).getOuterRadius(pos2));
56                 }
57                 
58                 return innerRadius;
59         }
60         
61         @Override
62         public void setInnerRadius(double r) {
63                 r = Math.max(r, 0);
64                 if (MathUtil.equals(innerRadius, r))
65                         return;
66                 innerRadius = r;
67                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
68         }
69         
70         @Override
71         public double getThickness() {
72                 return thickness;
73         }
74         
75         @Override
76         public void setThickness(double t) {
77                 t = Math.max(t, 0);
78                 if (MathUtil.equals(thickness, t))
79                         return;
80                 thickness = t;
81                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
82         }
83         
84         
85
86
87         @Override
88         public void setInnerRadiusAutomatic(boolean auto) {
89                 super.setOuterRadiusAutomatic(auto);
90         }
91         
92         @Override
93         public String getComponentName() {
94                 return "Sleeve";
95         }
96         
97         @Override
98         public boolean allowsChildren() {
99                 return false;
100         }
101         
102         @Override
103         public boolean isCompatible(Class<? extends RocketComponent> type) {
104                 return false;
105         }
106 }