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.motor.Motor;
8 import net.sf.openrocket.util.Coordinate;
9 import net.sf.openrocket.util.MathUtil;
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 {
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 public double getRadius() {
67 // Return auto radius from front or rear
69 SymmetricComponent c = this.getPreviousSymmetricComponent();
71 r = c.getFrontAutoRadius();
74 c = this.getNextSymmetricComponent();
76 r = c.getRearAutoRadius();
88 * Set the outer radius of the body tube. If the radius is less than the wall thickness,
89 * the wall thickness is decreased accordingly of the value of the radius.
90 * This method sets the automatic radius off.
92 public void setRadius(double radius) {
93 if ((this.radius == radius) && (autoRadius == false))
96 this.autoRadius = false;
97 this.radius = Math.max(radius, 0);
99 if (this.thickness > this.radius)
100 this.thickness = this.radius;
101 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
106 * Returns whether the radius is selected automatically or not.
107 * Returns false also in case automatic radius selection is not possible.
109 public boolean isRadiusAutomatic() {
114 * Sets whether the radius is selected automatically or not.
116 public void setRadiusAutomatic(boolean auto) {
117 if (autoRadius == auto)
121 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
126 public double getAftRadius() {
131 public double getForeRadius() {
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();
162 protected double getRearAutoRadius() {
163 if (isRadiusAutomatic()) {
164 // Search for next SymmetricComponent
165 SymmetricComponent c = this.getNextSymmetricComponent();
167 return c.getRearAutoRadius();
179 public double getInnerRadius() {
182 return Math.max(getRadius() - thickness, 0);
185 public void setInnerRadius(double r) {
186 setThickness(getRadius() - r);
193 * Return the component name.
196 public String getComponentName() {
201 /************ Component calculations ***********/
203 // From SymmetricComponent
205 * Returns the outer radius at the position x. This returns the same value as getRadius().
208 public double getRadius(double x) {
213 * Returns the inner radius at the position x. If the tube is filled, returns always zero.
216 public double getInnerRadius(double x) {
220 return Math.max(getRadius() - thickness, 0);
225 * Returns the body tube's center of gravity.
228 public Coordinate getComponentCG() {
229 return new Coordinate(length / 2, 0, 0, getComponentMass());
233 * Returns the body tube's volume.
236 public double getComponentVolume() {
237 double r = getRadius();
239 return getFilledVolume(r, length);
241 return getFilledVolume(r, length) - getFilledVolume(getInnerRadius(0), length);
246 public double getLongitudalUnitInertia() {
247 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
248 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getRadius()) + MathUtil.pow2(getLength())) / 12;
252 public double getRotationalUnitInertia() {
253 // 1/2 * (r1^2 + r2^2)
254 return (MathUtil.pow2(getInnerRadius()) + MathUtil.pow2(getRadius())) / 2;
261 * Helper function for cylinder volume.
263 private static double getFilledVolume(double r, double l) {
264 return Math.PI * r * r * l;
269 * Adds bounding coordinates to the given set. The body tube will fit within the
270 * convex hull of the points.
272 * Currently the points are simply a rectangular box around the body tube.
275 public Collection<Coordinate> getComponentBounds() {
276 Collection<Coordinate> bounds = new ArrayList<Coordinate>(8);
277 double r = getRadius();
278 addBound(bounds, 0, r);
279 addBound(bounds, length, r);
286 * Check whether the given type can be added to this component. BodyTubes allow any
287 * InternalComponents or ExternalComponents, excluding BodyComponents, to be added.
289 * @param type The RocketComponent class type to add.
290 * @return Whether such a component can be added.
293 public boolean isCompatible(Class<? extends RocketComponent> type) {
294 if (InternalComponent.class.isAssignableFrom(type))
296 if (ExternalComponent.class.isAssignableFrom(type) &&
297 !BodyComponent.class.isAssignableFrom(type))
302 //////////////// Motor mount /////////////////
305 public boolean isMotorMount() {
310 public void setMotorMount(boolean mount) {
311 if (motorMount == mount)
314 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
318 public Motor getMotor(String id) {
322 // Check whether the id is valid for the current rocket
323 RocketComponent root = this.getRoot();
324 if (!(root instanceof Rocket))
326 if (!((Rocket) root).isMotorConfigurationID(id))
329 return motors.get(id);
333 public void setMotor(String id, Motor motor) {
336 throw new IllegalArgumentException("Cannot set non-null motor for id null");
339 Motor current = motors.get(id);
340 if ((motor == null && current == null) ||
341 (motor != null && motor.equals(current)))
343 motors.put(id, motor);
344 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
348 public double getMotorDelay(String id) {
349 Double delay = ejectionDelays.get(id);
351 return Motor.PLUGGED;
356 public void setMotorDelay(String id, double delay) {
357 ejectionDelays.put(id, delay);
358 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
362 public int getMotorCount() {
367 public double getMotorMountDiameter() {
368 return getInnerRadius() * 2;
372 public IgnitionEvent getIgnitionEvent() {
373 return ignitionEvent;
377 public void setIgnitionEvent(IgnitionEvent event) {
378 if (ignitionEvent == event)
380 ignitionEvent = event;
381 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
386 public double getIgnitionDelay() {
387 return ignitionDelay;
391 public void setIgnitionDelay(double delay) {
392 if (MathUtil.equals(delay, ignitionDelay))
394 ignitionDelay = delay;
395 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
400 public double getMotorOverhang() {
405 public void setMotorOverhang(double overhang) {
406 if (MathUtil.equals(this.overhang, overhang))
408 this.overhang = overhang;
409 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
414 public Coordinate getMotorPosition(String id) {
415 Motor motor = motors.get(id);
417 throw new IllegalArgumentException("No motor with id " + id + " defined.");
420 return new Coordinate(this.getLength() - motor.getLength() + this.getMotorOverhang());
428 * Copy the motor and ejection delay HashMaps.
430 * @see rocketcomponent.RocketComponent#copy()
432 @SuppressWarnings("unchecked")
434 protected RocketComponent copyWithOriginalID() {
435 RocketComponent c = super.copyWithOriginalID();
436 ((BodyTube) c).motors = (HashMap<String, Motor>) motors.clone();
437 ((BodyTube) c).ejectionDelays = (HashMap<String, Double>) ejectionDelays.clone();