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