Component scaling support
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / BodyTube.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6
7 import net.sf.openrocket.l10n.Translator;
8 import net.sf.openrocket.motor.Motor;
9 import net.sf.openrocket.startup.Application;
10 import net.sf.openrocket.util.Coordinate;
11 import net.sf.openrocket.util.MathUtil;
12
13
14 /**
15  * Rocket body tube component.  Has only two parameters, a radius and length.
16  *
17  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18  */
19
20 public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial {
21         private static final Translator trans = Application.getTranslator();
22         
23         private double outerRadius = 0;
24         private boolean autoRadius = false; // Radius chosen automatically based on parent component
25         
26         // When changing the inner radius, thickness is modified
27         
28         private boolean motorMount = false;
29         private HashMap<String, Double> ejectionDelays = new HashMap<String, Double>();
30         private HashMap<String, Motor> motors = new HashMap<String, Motor>();
31         private IgnitionEvent ignitionEvent = IgnitionEvent.AUTOMATIC;
32         private double ignitionDelay = 0;
33         private double overhang = 0;
34         
35         
36
37         public BodyTube() {
38                 super();
39                 this.length = 8 * DEFAULT_RADIUS;
40                 this.outerRadius = DEFAULT_RADIUS;
41                 this.autoRadius = true;
42         }
43         
44         public BodyTube(double length, double radius) {
45                 super();
46                 this.outerRadius = Math.max(radius, 0);
47                 this.length = Math.max(length, 0);
48         }
49         
50         
51         public BodyTube(double length, double radius, boolean filled) {
52                 this(length, radius);
53                 this.filled = filled;
54         }
55         
56         public BodyTube(double length, double radius, double thickness) {
57                 this(length, radius);
58                 this.filled = false;
59                 this.thickness = thickness;
60         }
61         
62         
63         /************  Get/set component parameter methods ************/
64         
65         /**
66          * Return the outer radius of the body tube.
67          *
68          * @return  the outside radius of the tube
69          */
70         @Override
71         public double getOuterRadius() {
72                 if (autoRadius) {
73                         // Return auto radius from front or rear
74                         double r = -1;
75                         SymmetricComponent c = this.getPreviousSymmetricComponent();
76                         if (c != null) {
77                                 r = c.getFrontAutoRadius();
78                         }
79                         if (r < 0) {
80                                 c = this.getNextSymmetricComponent();
81                                 if (c != null) {
82                                         r = c.getRearAutoRadius();
83                                 }
84                         }
85                         if (r < 0)
86                                 r = DEFAULT_RADIUS;
87                         return r;
88                 }
89                 return outerRadius;
90         }
91         
92         
93         /**
94          * Set the outer radius of the body tube.  If the radius is less than the wall thickness,
95          * the wall thickness is decreased accordingly of the value of the radius.
96          * This method sets the automatic radius off.
97          *
98          * @param radius  the outside radius in standard units
99          */
100         @Override
101         public void setOuterRadius(double radius) {
102                 if ((this.outerRadius == radius) && (autoRadius == false))
103                         return;
104                 
105                 this.autoRadius = false;
106                 this.outerRadius = Math.max(radius, 0);
107                 
108                 if (this.thickness > this.outerRadius)
109                         this.thickness = this.outerRadius;
110                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
111         }
112         
113         
114         /**
115          * Returns whether the radius is selected automatically or not.
116          * Returns false also in case automatic radius selection is not possible.
117          */
118         public boolean isOuterRadiusAutomatic() {
119                 return autoRadius;
120         }
121         
122         /**
123          * Sets whether the radius is selected automatically or not.
124          */
125         public void setOuterRadiusAutomatic(boolean auto) {
126                 if (autoRadius == auto)
127                         return;
128                 
129                 autoRadius = auto;
130                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
131         }
132         
133         
134         @Override
135         public double getAftRadius() {
136                 return getOuterRadius();
137         }
138         
139         @Override
140         public double getForeRadius() {
141                 return getOuterRadius();
142         }
143         
144         @Override
145         public boolean isAftRadiusAutomatic() {
146                 return isOuterRadiusAutomatic();
147         }
148         
149         @Override
150         public boolean isForeRadiusAutomatic() {
151                 return isOuterRadiusAutomatic();
152         }
153         
154         
155
156         @Override
157         protected double getFrontAutoRadius() {
158                 if (isOuterRadiusAutomatic()) {
159                         // Search for previous SymmetricComponent
160                         SymmetricComponent c = this.getPreviousSymmetricComponent();
161                         if (c != null) {
162                                 return c.getFrontAutoRadius();
163                         } else {
164                                 return -1;
165                         }
166                 }
167                 return getOuterRadius();
168         }
169         
170         @Override
171         protected double getRearAutoRadius() {
172                 if (isOuterRadiusAutomatic()) {
173                         // Search for next SymmetricComponent
174                         SymmetricComponent c = this.getNextSymmetricComponent();
175                         if (c != null) {
176                                 return c.getRearAutoRadius();
177                         } else {
178                                 return -1;
179                         }
180                 }
181                 return getOuterRadius();
182         }
183         
184         
185
186
187
188         @Override
189         public double getInnerRadius() {
190                 if (filled)
191                         return 0;
192                 return Math.max(getOuterRadius() - thickness, 0);
193         }
194         
195         @Override
196         public void setInnerRadius(double r) {
197                 setThickness(getOuterRadius() - r);
198         }
199         
200         
201
202
203         /**
204          * Return the component name.
205          */
206         @Override
207         public String getComponentName() {
208                 //// Body tube
209                 return trans.get("BodyTube.BodyTube");
210         }
211         
212         
213         /************ Component calculations ***********/
214         
215         // From SymmetricComponent
216         /**
217          * Returns the outer radius at the position x.  This returns the same value as getOuterRadius().
218          */
219         @Override
220         public double getRadius(double x) {
221                 return getOuterRadius();
222         }
223         
224         /**
225          * Returns the inner radius at the position x.  If the tube is filled, returns always zero.
226          */
227         @Override
228         public double getInnerRadius(double x) {
229                 if (filled)
230                         return 0.0;
231                 else
232                         return Math.max(getOuterRadius() - thickness, 0);
233         }
234         
235         
236         /**
237          * Returns the body tube's center of gravity.
238          */
239         @Override
240         public Coordinate getComponentCG() {
241                 return new Coordinate(length / 2, 0, 0, getComponentMass());
242         }
243         
244         /**
245          * Returns the body tube's volume.
246          */
247         @Override
248         public double getComponentVolume() {
249                 double r = getOuterRadius();
250                 if (filled)
251                         return getFilledVolume(r, length);
252                 else
253                         return getFilledVolume(r, length) - getFilledVolume(getInnerRadius(0), length);
254         }
255         
256         
257         @Override
258         public double getLongitudinalUnitInertia() {
259                 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
260                 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getOuterRadius()) + MathUtil.pow2(getLength())) / 12;
261         }
262         
263         @Override
264         public double getRotationalUnitInertia() {
265                 // 1/2 * (r1^2 + r2^2)
266                 return (MathUtil.pow2(getInnerRadius()) + MathUtil.pow2(getOuterRadius())) / 2;
267         }
268         
269         
270
271
272         /**
273          * Helper function for cylinder volume.
274          */
275         private static double getFilledVolume(double r, double l) {
276                 return Math.PI * r * r * l;
277         }
278         
279         
280         /**
281          * Adds bounding coordinates to the given set.  The body tube will fit within the
282          * convex hull of the points.
283          *
284          * Currently the points are simply a rectangular box around the body tube.
285          */
286         @Override
287         public Collection<Coordinate> getComponentBounds() {
288                 Collection<Coordinate> bounds = new ArrayList<Coordinate>(8);
289                 double r = getOuterRadius();
290                 addBound(bounds, 0, r);
291                 addBound(bounds, length, r);
292                 return bounds;
293         }
294         
295         
296
297         /**
298          * Check whether the given type can be added to this component.  BodyTubes allow any
299          * InternalComponents or ExternalComponents, excluding BodyComponents, to be added.
300          *
301          * @param type  The RocketComponent class type to add.
302          * @return      Whether such a component can be added.
303          */
304         @Override
305         public boolean isCompatible(Class<? extends RocketComponent> type) {
306                 if (InternalComponent.class.isAssignableFrom(type))
307                         return true;
308                 if (ExternalComponent.class.isAssignableFrom(type) &&
309                                 !BodyComponent.class.isAssignableFrom(type))
310                         return true;
311                 return false;
312         }
313         
314         ////////////////  Motor mount  /////////////////
315         
316         @Override
317         public boolean isMotorMount() {
318                 return motorMount;
319         }
320         
321         @Override
322         public void setMotorMount(boolean mount) {
323                 if (motorMount == mount)
324                         return;
325                 motorMount = mount;
326                 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
327         }
328         
329         @Override
330         public Motor getMotor(String id) {
331                 if (id == null)
332                         return null;
333                 
334                 // Check whether the id is valid for the current rocket
335                 RocketComponent root = this.getRoot();
336                 if (!(root instanceof Rocket))
337                         return null;
338                 if (!((Rocket) root).isMotorConfigurationID(id))
339                         return null;
340                 
341                 return motors.get(id);
342         }
343         
344         @Override
345         public void setMotor(String id, Motor motor) {
346                 if (id == null) {
347                         if (motor != null) {
348                                 throw new IllegalArgumentException("Cannot set non-null motor for id null");
349                         }
350                 }
351                 Motor current = motors.get(id);
352                 if ((motor == null && current == null) ||
353                                 (motor != null && motor.equals(current)))
354                         return;
355                 motors.put(id, motor);
356                 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
357         }
358         
359         @Override
360         public double getMotorDelay(String id) {
361                 Double delay = ejectionDelays.get(id);
362                 if (delay == null)
363                         return Motor.PLUGGED;
364                 return delay;
365         }
366         
367         @Override
368         public void setMotorDelay(String id, double delay) {
369                 ejectionDelays.put(id, delay);
370                 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
371         }
372         
373         @Override
374         public int getMotorCount() {
375                 return 1;
376         }
377         
378         @Override
379         public double getMotorMountDiameter() {
380                 return getInnerRadius() * 2;
381         }
382         
383         @Override
384         public IgnitionEvent getIgnitionEvent() {
385                 return ignitionEvent;
386         }
387         
388         @Override
389         public void setIgnitionEvent(IgnitionEvent event) {
390                 if (ignitionEvent == event)
391                         return;
392                 ignitionEvent = event;
393                 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
394         }
395         
396         
397         @Override
398         public double getIgnitionDelay() {
399                 return ignitionDelay;
400         }
401         
402         @Override
403         public void setIgnitionDelay(double delay) {
404                 if (MathUtil.equals(delay, ignitionDelay))
405                         return;
406                 ignitionDelay = delay;
407                 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
408         }
409         
410         
411         @Override
412         public double getMotorOverhang() {
413                 return overhang;
414         }
415         
416         @Override
417         public void setMotorOverhang(double overhang) {
418                 if (MathUtil.equals(this.overhang, overhang))
419                         return;
420                 this.overhang = overhang;
421                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
422         }
423         
424         
425         @Override
426         public Coordinate getMotorPosition(String id) {
427                 Motor motor = motors.get(id);
428                 if (motor == null) {
429                         throw new IllegalArgumentException("No motor with id " + id + " defined.");
430                 }
431                 
432                 return new Coordinate(this.getLength() - motor.getLength() + this.getMotorOverhang());
433         }
434         
435         
436
437
438         /*
439          * (non-Javadoc)
440          * Copy the motor and ejection delay HashMaps.
441          *
442          * @see rocketcomponent.RocketComponent#copy()
443          */
444         @SuppressWarnings("unchecked")
445         @Override
446         protected RocketComponent copyWithOriginalID() {
447                 RocketComponent c = super.copyWithOriginalID();
448                 ((BodyTube) c).motors = (HashMap<String, Motor>) motors.clone();
449                 ((BodyTube) c).ejectionDelays = (HashMap<String, Double>) ejectionDelays.clone();
450                 return c;
451         }
452 }