Initial commit
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / LaunchLug.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import net.sf.openrocket.util.Coordinate;
7 import net.sf.openrocket.util.MathUtil;
8
9
10 public class LaunchLug extends ExternalComponent {
11
12         private double radius;
13         private double thickness;
14         
15         private double radialDirection = 0;
16         
17         /* These are calculated when the component is first attached to any Rocket */
18         private double shiftY, shiftZ;
19         
20         
21         
22         public LaunchLug() {
23                 super(Position.MIDDLE);
24                 radius = 0.01/2;
25                 thickness = 0.001;
26                 length = 0.03;
27         }
28         
29         
30         public double getRadius() {
31                 return radius;
32         }
33
34         public void setRadius(double radius) {
35                 if (MathUtil.equals(this.radius, radius))
36                         return;
37                 this.radius = radius;
38                 this.thickness = Math.min(this.thickness, this.radius);
39                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
40         }
41
42         public double getInnerRadius() {
43                 return radius-thickness;
44         }
45
46         public void setInnerRadius(double innerRadius) {
47                 setRadius(innerRadius + thickness);
48         }
49
50         public double getThickness() {
51                 return thickness;
52         }
53         
54         public void setThickness(double thickness) {
55                 if (MathUtil.equals(this.thickness, thickness))
56                         return;
57                 this.thickness = MathUtil.clamp(thickness, 0, radius);
58                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
59         }
60
61         
62         public double getRadialDirection() {
63                 return radialDirection;
64         }
65
66         public void setRadialDirection(double direction) {
67                 direction = MathUtil.reduce180(direction);
68                 if (MathUtil.equals(this.radialDirection, direction))
69                         return;
70                 this.radialDirection = direction;
71                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
72         }
73
74
75         
76         public void setLength(double length) {
77                 if (MathUtil.equals(this.length, length))
78                         return;
79                 this.length = length;
80                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
81         }
82         
83         
84         
85
86
87
88         @Override
89         public void setRelativePosition(RocketComponent.Position position) {
90                 super.setRelativePosition(position);
91                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
92         }
93
94         
95         @Override
96         public void setPositionValue(double value) {
97                 super.setPositionValue(value);
98                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
99         }
100
101         
102         
103         @Override
104         public Coordinate[] shiftCoordinates(Coordinate[] array) {
105                 array = super.shiftCoordinates(array);
106                 
107                 for (int i=0; i < array.length; i++) {
108                         array[i] = array[i].add(0, shiftY, shiftZ);
109                 }
110                 
111                 return array;
112         }
113         
114         
115         @Override
116         public void componentChanged(ComponentChangeEvent e) {
117                 super.componentChanged(e);
118                 
119                 /* 
120                  * shiftY and shiftZ must be computed here since calculating them
121                  * in shiftCoordinates() would cause an infinite loop due to .toRelative
122                  */
123                 RocketComponent body;
124                 double parentRadius;
125
126                 for (body = this.getParent(); body != null; body = body.getParent()) {
127                         if (body instanceof SymmetricComponent)
128                                 break;
129                 }
130
131                 if (body == null) {
132                         parentRadius = 0;
133                 } else {
134                         SymmetricComponent s = (SymmetricComponent)body;
135                         double x1, x2;
136                         x1 = this.toRelative(Coordinate.NUL, body)[0].x;
137                         x2 = this.toRelative(new Coordinate(length,0,0), body)[0].x;
138                         x1 = MathUtil.clamp(x1, 0, body.getLength());
139                         x2 = MathUtil.clamp(x2, 0, body.getLength());
140                         parentRadius = Math.max(s.getRadius(x1), s.getRadius(x2));
141                 }
142
143                 shiftY = Math.cos(radialDirection) * (parentRadius + radius);
144                 shiftZ = Math.sin(radialDirection) * (parentRadius + radius);
145
146 //              System.out.println("Computed shift: y="+shiftY+" z="+shiftZ);
147 }
148
149         
150         
151
152         @Override
153         public double getComponentVolume() {
154                 return length * Math.PI * (MathUtil.pow2(radius) - MathUtil.pow2(radius-thickness));
155         }
156
157         @Override
158         public Collection<Coordinate> getComponentBounds() {
159                 ArrayList<Coordinate> set = new ArrayList<Coordinate>();
160                 addBound(set, 0, radius);
161                 addBound(set, length, radius);
162                 return set;
163         }
164
165         @Override
166         public Coordinate getComponentCG() {
167                 return new Coordinate(length/2, 0, 0, getComponentMass());
168         }
169
170         @Override
171         public String getComponentName() {
172                 return "Launch lug";
173         }
174
175         @Override
176         public double getLongitudalUnitInertia() {
177                 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
178                 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getRadius()) +
179                                 MathUtil.pow2(getLength())) / 12;
180         }
181
182         @Override
183         public double getRotationalUnitInertia() {
184                 // 1/2 * (r1^2 + r2^2)
185                 return (MathUtil.pow2(getInnerRadius()) + MathUtil.pow2(getRadius()))/2;
186         }
187
188
189         @Override
190         public boolean isCompatible(Class<? extends RocketComponent> type) {
191                 // Allow nothing to be attached to a LaunchLug
192                 return false;
193         }
194
195 }