1 package net.sf.openrocket.rocketcomponent;
3 import net.sf.openrocket.motor.Motor;
4 import net.sf.openrocket.util.Coordinate;
5 import net.sf.openrocket.util.MathUtil;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.HashMap;
13 * Rocket body tube component. Has only two parameters, a radius and length.
15 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18 public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial {
20 private double radius = 0;
21 private boolean autoRadius = false; // Radius chosen automatically based on parent component
23 // When changing the inner radius, thickness is modified
25 private boolean motorMount = false;
26 private HashMap<String, Double> ejectionDelays = new HashMap<String, Double>();
27 private HashMap<String, Motor> motors = new HashMap<String, Motor>();
28 private IgnitionEvent ignitionEvent = IgnitionEvent.AUTOMATIC;
29 private double ignitionDelay = 0;
30 private double overhang = 0;
36 this.length = 8 * DEFAULT_RADIUS;
37 this.radius = DEFAULT_RADIUS;
38 this.autoRadius = true;
41 public BodyTube(double length, double radius) {
43 this.radius = Math.max(radius, 0);
44 this.length = Math.max(length, 0);
48 public BodyTube(double length, double radius, boolean filled) {
53 public BodyTube(double length, double radius, double thickness) {
56 this.thickness = thickness;
60 /************ Get/set component parameter methods ************/
63 * Return the outer radius of the body tube.
65 * @return the outside radius of the tube
68 public double getOuterRadius () {
70 // Return auto radius from front or rear
72 SymmetricComponent c = this.getPreviousSymmetricComponent();
74 r = c.getFrontAutoRadius();
77 c = this.getNextSymmetricComponent();
79 r = c.getRearAutoRadius();
91 * Set the outer radius of the body tube. If the radius is less than the wall thickness,
92 * the wall thickness is decreased accordingly of the value of the radius.
93 * This method sets the automatic radius off.
95 * @param radius the outside radius in standard units
98 public void setOuterRadius (double radius) {
99 if ((this.radius == radius) && (autoRadius == false))
102 this.autoRadius = false;
103 this.radius = Math.max(radius, 0);
105 if (this.thickness > this.radius)
106 this.thickness = this.radius;
107 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
112 * Returns whether the radius is selected automatically or not.
113 * Returns false also in case automatic radius selection is not possible.
115 public boolean isRadiusAutomatic() {
120 * Sets whether the radius is selected automatically or not.
122 public void setRadiusAutomatic(boolean auto) {
123 if (autoRadius == auto)
127 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
132 public double getAftRadius() { return getOuterRadius(); }
134 public double getForeRadius() { return getOuterRadius(); }
136 public boolean isAftRadiusAutomatic() {
137 return isRadiusAutomatic();
141 public boolean isForeRadiusAutomatic() {
142 return isRadiusAutomatic();
148 protected double getFrontAutoRadius() {
149 if (isRadiusAutomatic()) {
150 // Search for previous SymmetricComponent
151 SymmetricComponent c = this.getPreviousSymmetricComponent();
153 return c.getFrontAutoRadius();
158 return getOuterRadius();
162 protected double getRearAutoRadius() {
163 if (isRadiusAutomatic()) {
164 // Search for next SymmetricComponent
165 SymmetricComponent c = this.getNextSymmetricComponent();
167 return c.getRearAutoRadius();
172 return getOuterRadius();
183 public double getInnerRadius() {
186 return Math.max(getOuterRadius()-thickness, 0);
190 public void setInnerRadius(double r) {
191 setThickness(getOuterRadius()-r);
198 * Return the component name.
201 public String getComponentName() {
206 /************ Component calculations ***********/
208 // From SymmetricComponent
210 * Returns the outer radius at the position x. This returns the same value as getOuterRadius().
213 public double getRadius(double x) {
214 return getOuterRadius();
218 * Returns the inner radius at the position x. If the tube is filled, returns always zero.
221 public double getInnerRadius(double x) {
225 return Math.max(getOuterRadius()-thickness,0);
230 * Returns the body tube's center of gravity.
233 public Coordinate getComponentCG() {
234 return new Coordinate(length / 2, 0, 0, getComponentMass());
238 * Returns the body tube's volume.
241 public double getComponentVolume() {
242 double r = getOuterRadius();
244 return getFilledVolume(r, length);
246 return getFilledVolume(r, length) - getFilledVolume(getInnerRadius(0), length);
251 public double getLongitudinalUnitInertia() {
252 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
253 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getOuterRadius()) +
254 MathUtil.pow2(getLength())) / 12;
258 public double getRotationalUnitInertia() {
259 // 1/2 * (r1^2 + r2^2)
260 return (MathUtil.pow2(getInnerRadius()) + MathUtil.pow2(getOuterRadius()))/2;
267 * Helper function for cylinder volume.
269 private static double getFilledVolume(double r, double l) {
270 return Math.PI * r * r * l;
275 * Adds bounding coordinates to the given set. The body tube will fit within the
276 * convex hull of the points.
278 * Currently the points are simply a rectangular box around the body tube.
281 public Collection<Coordinate> getComponentBounds() {
282 Collection<Coordinate> bounds = new ArrayList<Coordinate>(8);
283 double r = getOuterRadius();
284 addBound(bounds, 0, r);
285 addBound(bounds, length, r);
292 * Check whether the given type can be added to this component. BodyTubes allow any
293 * InternalComponents or ExternalComponents, excluding BodyComponents, to be added.
295 * @param type The RocketComponent class type to add.
296 * @return Whether such a component can be added.
299 public boolean isCompatible(Class<? extends RocketComponent> type) {
300 if (InternalComponent.class.isAssignableFrom(type))
302 if (ExternalComponent.class.isAssignableFrom(type) &&
303 !BodyComponent.class.isAssignableFrom(type))
308 //////////////// Motor mount /////////////////
311 public boolean isMotorMount() {
316 public void setMotorMount(boolean mount) {
317 if (motorMount == mount)
320 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
324 public Motor getMotor(String id) {
328 // Check whether the id is valid for the current rocket
329 RocketComponent root = this.getRoot();
330 if (!(root instanceof Rocket))
332 if (!((Rocket) root).isMotorConfigurationID(id))
335 return motors.get(id);
339 public void setMotor(String id, Motor motor) {
342 throw new IllegalArgumentException("Cannot set non-null motor for id null");
345 Motor current = motors.get(id);
346 if ((motor == null && current == null) ||
347 (motor != null && motor.equals(current)))
349 motors.put(id, motor);
350 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
354 public double getMotorDelay(String id) {
355 Double delay = ejectionDelays.get(id);
357 return Motor.PLUGGED;
362 public void setMotorDelay(String id, double delay) {
363 ejectionDelays.put(id, delay);
364 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
368 public int getMotorCount() {
373 public double getMotorMountDiameter() {
374 return getInnerRadius() * 2;
378 public IgnitionEvent getIgnitionEvent() {
379 return ignitionEvent;
383 public void setIgnitionEvent(IgnitionEvent event) {
384 if (ignitionEvent == event)
386 ignitionEvent = event;
387 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
392 public double getIgnitionDelay() {
393 return ignitionDelay;
397 public void setIgnitionDelay(double delay) {
398 if (MathUtil.equals(delay, ignitionDelay))
400 ignitionDelay = delay;
401 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
406 public double getMotorOverhang() {
411 public void setMotorOverhang(double overhang) {
412 if (MathUtil.equals(this.overhang, overhang))
414 this.overhang = overhang;
415 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
420 public Coordinate getMotorPosition(String id) {
421 Motor motor = motors.get(id);
423 throw new IllegalArgumentException("No motor with id " + id + " defined.");
426 return new Coordinate(this.getLength() - motor.getLength() + this.getMotorOverhang());
434 * Copy the motor and ejection delay HashMaps.
436 * @see rocketcomponent.RocketComponent#copy()
438 @SuppressWarnings("unchecked")
440 protected RocketComponent copyWithOriginalID() {
441 RocketComponent c = super.copyWithOriginalID();
442 ((BodyTube) c).motors = (HashMap<String, Motor>) motors.clone();
443 ((BodyTube) c).ejectionDelays = (HashMap<String, Double>) ejectionDelays.clone();