added svn:ignores
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / LaunchLug.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import net.sf.openrocket.util.Coordinate;
4 import net.sf.openrocket.util.MathUtil;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8
9
10
11 public class LaunchLug extends ExternalComponent implements Coaxial {
12
13         private double radius;
14         private double thickness;
15
16         private double radialDirection = 0;
17
18         /* These are calculated when the component is first attached to any Rocket */
19         private double shiftY, shiftZ;
20
21
22
23         public LaunchLug() {
24                 super(Position.MIDDLE);
25                 radius = 0.01 / 2;
26                 thickness = 0.001;
27                 length = 0.03;
28         }
29
30
31         public double getOuterRadius () {
32                 return radius;
33         }
34
35         public void setOuterRadius (double radius) {
36                 if (MathUtil.equals(this.radius, radius))
37                         return;
38                 this.radius = radius;
39                 this.thickness = Math.min(this.thickness, this.radius);
40                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
41         }
42
43         public double getInnerRadius() {
44                 return radius - thickness;
45         }
46
47         public void setInnerRadius(double innerRadius) {
48                 setOuterRadius(innerRadius + thickness);
49         }
50
51         public double getThickness() {
52                 return thickness;
53         }
54
55         public void setThickness(double thickness) {
56                 if (MathUtil.equals(this.thickness, thickness))
57                         return;
58                 this.thickness = MathUtil.clamp(thickness, 0, radius);
59                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
60         }
61
62
63         public double getRadialDirection() {
64                 return radialDirection;
65         }
66
67         public void setRadialDirection(double direction) {
68                 direction = MathUtil.reduce180(direction);
69                 if (MathUtil.equals(this.radialDirection, direction))
70                         return;
71                 this.radialDirection = direction;
72                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
73         }
74
75
76
77         public void setLength(double length) {
78                 if (MathUtil.equals(this.length, length))
79                         return;
80                 this.length = length;
81                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
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 getLongitudinalUnitInertia() {
177                 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
178                 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getOuterRadius()) +
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(getOuterRadius()))/2;
186         }
187
188         @Override
189         public boolean allowsChildren() {
190                 return false;
191         }
192
193         @Override
194         public boolean isCompatible(Class<? extends RocketComponent> type) {
195                 // Allow nothing to be attached to a LaunchLug
196                 return false;
197         }
198
199 }