1 package net.sf.openrocket.rocketcomponent;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
7 import net.sf.openrocket.l10n.Translator;
8 import net.sf.openrocket.motor.Motor;
9 import net.sf.openrocket.preset.ComponentPreset;
10 import net.sf.openrocket.startup.Application;
11 import net.sf.openrocket.util.Coordinate;
12 import net.sf.openrocket.util.MathUtil;
16 * Rocket body tube component. Has only two parameters, a radius and length.
18 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21 public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial {
22 private static final Translator trans = Application.getTranslator();
24 private double outerRadius = 0;
25 private boolean autoRadius = false; // Radius chosen automatically based on parent component
27 // When changing the inner radius, thickness is modified
29 private boolean motorMount = false;
30 private HashMap<String, Double> ejectionDelays = new HashMap<String, Double>();
31 private HashMap<String, Motor> motors = new HashMap<String, Motor>();
32 private IgnitionEvent ignitionEvent = IgnitionEvent.AUTOMATIC;
33 private double ignitionDelay = 0;
34 private double overhang = 0;
40 this.length = 8 * DEFAULT_RADIUS;
41 this.outerRadius = DEFAULT_RADIUS;
42 this.autoRadius = true;
45 public BodyTube(double length, double radius) {
47 this.outerRadius = Math.max(radius, 0);
48 this.length = Math.max(length, 0);
52 public BodyTube(double length, double radius, boolean filled) {
57 public BodyTube(double length, double radius, double thickness) {
60 this.thickness = thickness;
64 /************ Get/set component parameter methods ************/
67 * Return the outer radius of the body tube.
69 * @return the outside radius of the tube
72 public double getOuterRadius() {
74 // Return auto radius from front or rear
76 SymmetricComponent c = this.getPreviousSymmetricComponent();
78 r = c.getFrontAutoRadius();
81 c = this.getNextSymmetricComponent();
83 r = c.getRearAutoRadius();
95 * Set the outer radius of the body tube. If the radius is less than the wall thickness,
96 * the wall thickness is decreased accordingly of the value of the radius.
97 * This method sets the automatic radius off.
99 * @param radius the outside radius in standard units
102 public void setOuterRadius(double radius) {
103 if ((this.outerRadius == radius) && (autoRadius == false))
106 this.autoRadius = false;
107 this.outerRadius = Math.max(radius, 0);
109 if (this.thickness > this.outerRadius)
110 this.thickness = this.outerRadius;
111 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
117 * Returns whether the radius is selected automatically or not.
118 * Returns false also in case automatic radius selection is not possible.
120 public boolean isOuterRadiusAutomatic() {
125 * Sets whether the radius is selected automatically or not.
127 public void setOuterRadiusAutomatic(boolean auto) {
128 if (autoRadius == auto)
132 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
138 protected void loadFromPreset(ComponentPreset preset) {
139 this.autoRadius = false;
140 if ( preset.containsKey(ComponentPreset.OUTER_DIAMETER) ) {
141 double outerDiameter = preset.get(ComponentPreset.OUTER_DIAMETER);
142 this.outerRadius = outerDiameter/2.0;
143 if ( preset.containsKey(ComponentPreset.INNER_DIAMETER) ) {
144 double innerDiameter = preset.get(ComponentPreset.INNER_DIAMETER);
145 this.thickness = (outerDiameter-innerDiameter) / 2.0;
149 super.loadFromPreset(preset);
151 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
155 public double getAftRadius() {
156 return getOuterRadius();
160 public double getForeRadius() {
161 return getOuterRadius();
165 public boolean isAftRadiusAutomatic() {
166 return isOuterRadiusAutomatic();
170 public boolean isForeRadiusAutomatic() {
171 return isOuterRadiusAutomatic();
177 protected double getFrontAutoRadius() {
178 if (isOuterRadiusAutomatic()) {
179 // Search for previous SymmetricComponent
180 SymmetricComponent c = this.getPreviousSymmetricComponent();
182 return c.getFrontAutoRadius();
187 return getOuterRadius();
191 protected double getRearAutoRadius() {
192 if (isOuterRadiusAutomatic()) {
193 // Search for next SymmetricComponent
194 SymmetricComponent c = this.getNextSymmetricComponent();
196 return c.getRearAutoRadius();
201 return getOuterRadius();
209 public double getInnerRadius() {
212 return Math.max(getOuterRadius() - thickness, 0);
216 public void setInnerRadius(double r) {
217 setThickness(getOuterRadius() - r);
224 * Return the component name.
227 public String getComponentName() {
229 return trans.get("BodyTube.BodyTube");
233 /************ Component calculations ***********/
235 // From SymmetricComponent
237 * Returns the outer radius at the position x. This returns the same value as getOuterRadius().
240 public double getRadius(double x) {
241 return getOuterRadius();
245 * Returns the inner radius at the position x. If the tube is filled, returns always zero.
248 public double getInnerRadius(double x) {
252 return Math.max(getOuterRadius() - thickness, 0);
257 * Returns the body tube's center of gravity.
260 public Coordinate getComponentCG() {
261 return new Coordinate(length / 2, 0, 0, getComponentMass());
265 * Returns the body tube's volume.
268 public double getComponentVolume() {
269 double r = getOuterRadius();
271 return getFilledVolume(r, length);
273 return getFilledVolume(r, length) - getFilledVolume(getInnerRadius(0), length);
278 public double getLongitudinalUnitInertia() {
279 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
280 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getOuterRadius()) + MathUtil.pow2(getLength())) / 12;
284 public double getRotationalUnitInertia() {
285 // 1/2 * (r1^2 + r2^2)
286 return (MathUtil.pow2(getInnerRadius()) + MathUtil.pow2(getOuterRadius())) / 2;
293 * Helper function for cylinder volume.
295 private static double getFilledVolume(double r, double l) {
296 return Math.PI * r * r * l;
301 * Adds bounding coordinates to the given set. The body tube will fit within the
302 * convex hull of the points.
304 * Currently the points are simply a rectangular box around the body tube.
307 public Collection<Coordinate> getComponentBounds() {
308 Collection<Coordinate> bounds = new ArrayList<Coordinate>(8);
309 double r = getOuterRadius();
310 addBound(bounds, 0, r);
311 addBound(bounds, length, r);
318 * Check whether the given type can be added to this component. BodyTubes allow any
319 * InternalComponents or ExternalComponents, excluding BodyComponents, to be added.
321 * @param type The RocketComponent class type to add.
322 * @return Whether such a component can be added.
325 public boolean isCompatible(Class<? extends RocketComponent> type) {
326 if (InternalComponent.class.isAssignableFrom(type))
328 if (ExternalComponent.class.isAssignableFrom(type) &&
329 !BodyComponent.class.isAssignableFrom(type))
334 //////////////// Motor mount /////////////////
337 public boolean isMotorMount() {
342 public void setMotorMount(boolean mount) {
343 if (motorMount == mount)
346 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
350 public Motor getMotor(String id) {
354 // Check whether the id is valid for the current rocket
355 RocketComponent root = this.getRoot();
356 if (!(root instanceof Rocket))
358 if (!((Rocket) root).isMotorConfigurationID(id))
361 return motors.get(id);
365 public void setMotor(String id, Motor motor) {
368 throw new IllegalArgumentException("Cannot set non-null motor for id null");
371 Motor current = motors.get(id);
372 if ((motor == null && current == null) ||
373 (motor != null && motor.equals(current)))
375 motors.put(id, motor);
376 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
380 public double getMotorDelay(String id) {
381 Double delay = ejectionDelays.get(id);
383 return Motor.PLUGGED;
388 public void setMotorDelay(String id, double delay) {
389 ejectionDelays.put(id, delay);
390 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
394 public int getMotorCount() {
399 public double getMotorMountDiameter() {
400 return getInnerRadius() * 2;
404 public IgnitionEvent getIgnitionEvent() {
405 return ignitionEvent;
409 public void setIgnitionEvent(IgnitionEvent event) {
410 if (ignitionEvent == event)
412 ignitionEvent = event;
413 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
418 public double getIgnitionDelay() {
419 return ignitionDelay;
423 public void setIgnitionDelay(double delay) {
424 if (MathUtil.equals(delay, ignitionDelay))
426 ignitionDelay = delay;
427 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
432 public double getMotorOverhang() {
437 public void setMotorOverhang(double overhang) {
438 if (MathUtil.equals(this.overhang, overhang))
440 this.overhang = overhang;
441 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
446 public Coordinate getMotorPosition(String id) {
447 Motor motor = motors.get(id);
449 throw new IllegalArgumentException("No motor with id " + id + " defined.");
452 return new Coordinate(this.getLength() - motor.getLength() + this.getMotorOverhang());
460 * Copy the motor and ejection delay HashMaps.
462 * @see rocketcomponent.RocketComponent#copy()
464 @SuppressWarnings("unchecked")
466 protected RocketComponent copyWithOriginalID() {
467 RocketComponent c = super.copyWithOriginalID();
468 ((BodyTube) c).motors = (HashMap<String, Motor>) motors.clone();
469 ((BodyTube) c).ejectionDelays = (HashMap<String, Double>) ejectionDelays.clone();