1 package net.sf.openrocket.rocketcomponent;
3 import net.sf.openrocket.l10n.Translator;
4 import net.sf.openrocket.motor.Motor;
5 import net.sf.openrocket.startup.Application;
6 import net.sf.openrocket.util.Coordinate;
7 import net.sf.openrocket.util.MathUtil;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.HashMap;
15 * Rocket body tube component. Has only two parameters, a radius and length.
17 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20 public class BodyTube extends SymmetricComponent implements MotorMount, Coaxial {
21 private static final Translator trans = Application.getTranslator();
23 private double radius = 0;
24 private boolean autoRadius = false; // Radius chosen automatically based on parent component
26 // When changing the inner radius, thickness is modified
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;
39 this.length = 8 * DEFAULT_RADIUS;
40 this.radius = DEFAULT_RADIUS;
41 this.autoRadius = true;
44 public BodyTube(double length, double radius) {
46 this.radius = Math.max(radius, 0);
47 this.length = Math.max(length, 0);
51 public BodyTube(double length, double radius, boolean filled) {
56 public BodyTube(double length, double radius, double thickness) {
59 this.thickness = thickness;
63 /************ Get/set component parameter methods ************/
66 * Return the outer radius of the body tube.
68 * @return the outside radius of the tube
71 public double getOuterRadius () {
73 // Return auto radius from front or rear
75 SymmetricComponent c = this.getPreviousSymmetricComponent();
77 r = c.getFrontAutoRadius();
80 c = this.getNextSymmetricComponent();
82 r = c.getRearAutoRadius();
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.
98 * @param radius the outside radius in standard units
101 public void setOuterRadius (double radius) {
102 if ((this.radius == radius) && (autoRadius == false))
105 this.autoRadius = false;
106 this.radius = Math.max(radius, 0);
108 if (this.thickness > this.radius)
109 this.thickness = this.radius;
110 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
115 * Returns whether the radius is selected automatically or not.
116 * Returns false also in case automatic radius selection is not possible.
118 public boolean isRadiusAutomatic() {
123 * Sets whether the radius is selected automatically or not.
125 public void setRadiusAutomatic(boolean auto) {
126 if (autoRadius == auto)
130 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
135 public double getAftRadius() { return getOuterRadius(); }
137 public double getForeRadius() { return getOuterRadius(); }
139 public boolean isAftRadiusAutomatic() {
140 return isRadiusAutomatic();
144 public boolean isForeRadiusAutomatic() {
145 return isRadiusAutomatic();
151 protected double getFrontAutoRadius() {
152 if (isRadiusAutomatic()) {
153 // Search for previous SymmetricComponent
154 SymmetricComponent c = this.getPreviousSymmetricComponent();
156 return c.getFrontAutoRadius();
161 return getOuterRadius();
165 protected double getRearAutoRadius() {
166 if (isRadiusAutomatic()) {
167 // Search for next SymmetricComponent
168 SymmetricComponent c = this.getNextSymmetricComponent();
170 return c.getRearAutoRadius();
175 return getOuterRadius();
186 public double getInnerRadius() {
189 return Math.max(getOuterRadius()-thickness, 0);
193 public void setInnerRadius(double r) {
194 setThickness(getOuterRadius()-r);
201 * Return the component name.
204 public String getComponentName() {
206 return trans.get("BodyTube.BodyTube");
210 * Accept a visitor to this BodyTube in the component hierarchy.
212 * @param theVisitor the visitor that will be called back with a reference to this BodyTube
215 public void accept (final ComponentVisitor theVisitor) {
216 theVisitor.visit(this);
220 /************ Component calculations ***********/
222 // From SymmetricComponent
224 * Returns the outer radius at the position x. This returns the same value as getOuterRadius().
227 public double getRadius(double x) {
228 return getOuterRadius();
232 * Returns the inner radius at the position x. If the tube is filled, returns always zero.
235 public double getInnerRadius(double x) {
239 return Math.max(getOuterRadius()-thickness,0);
244 * Returns the body tube's center of gravity.
247 public Coordinate getComponentCG() {
248 return new Coordinate(length / 2, 0, 0, getComponentMass());
252 * Returns the body tube's volume.
255 public double getComponentVolume() {
256 double r = getOuterRadius();
258 return getFilledVolume(r, length);
260 return getFilledVolume(r, length) - getFilledVolume(getInnerRadius(0), length);
265 public double getLongitudinalUnitInertia() {
266 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
267 return (3 * (MathUtil.pow2(getInnerRadius())) + MathUtil.pow2(getOuterRadius()) +
268 MathUtil.pow2(getLength())) / 12;
272 public double getRotationalUnitInertia() {
273 // 1/2 * (r1^2 + r2^2)
274 return (MathUtil.pow2(getInnerRadius()) + MathUtil.pow2(getOuterRadius()))/2;
281 * Helper function for cylinder volume.
283 private static double getFilledVolume(double r, double l) {
284 return Math.PI * r * r * l;
289 * Adds bounding coordinates to the given set. The body tube will fit within the
290 * convex hull of the points.
292 * Currently the points are simply a rectangular box around the body tube.
295 public Collection<Coordinate> getComponentBounds() {
296 Collection<Coordinate> bounds = new ArrayList<Coordinate>(8);
297 double r = getOuterRadius();
298 addBound(bounds, 0, r);
299 addBound(bounds, length, r);
306 * Check whether the given type can be added to this component. BodyTubes allow any
307 * InternalComponents or ExternalComponents, excluding BodyComponents, to be added.
309 * @param type The RocketComponent class type to add.
310 * @return Whether such a component can be added.
313 public boolean isCompatible(Class<? extends RocketComponent> type) {
314 if (InternalComponent.class.isAssignableFrom(type))
316 if (ExternalComponent.class.isAssignableFrom(type) &&
317 !BodyComponent.class.isAssignableFrom(type))
322 //////////////// Motor mount /////////////////
325 public boolean isMotorMount() {
330 public void setMotorMount(boolean mount) {
331 if (motorMount == mount)
334 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
338 public Motor getMotor(String id) {
342 // Check whether the id is valid for the current rocket
343 RocketComponent root = this.getRoot();
344 if (!(root instanceof Rocket))
346 if (!((Rocket) root).isMotorConfigurationID(id))
349 return motors.get(id);
353 public void setMotor(String id, Motor motor) {
356 throw new IllegalArgumentException("Cannot set non-null motor for id null");
359 Motor current = motors.get(id);
360 if ((motor == null && current == null) ||
361 (motor != null && motor.equals(current)))
363 motors.put(id, motor);
364 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
368 public double getMotorDelay(String id) {
369 Double delay = ejectionDelays.get(id);
371 return Motor.PLUGGED;
376 public void setMotorDelay(String id, double delay) {
377 ejectionDelays.put(id, delay);
378 fireComponentChangeEvent(ComponentChangeEvent.MOTOR_CHANGE);
382 public int getMotorCount() {
387 public double getMotorMountDiameter() {
388 return getInnerRadius() * 2;
392 public IgnitionEvent getIgnitionEvent() {
393 return ignitionEvent;
397 public void setIgnitionEvent(IgnitionEvent event) {
398 if (ignitionEvent == event)
400 ignitionEvent = event;
401 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
406 public double getIgnitionDelay() {
407 return ignitionDelay;
411 public void setIgnitionDelay(double delay) {
412 if (MathUtil.equals(delay, ignitionDelay))
414 ignitionDelay = delay;
415 fireComponentChangeEvent(ComponentChangeEvent.EVENT_CHANGE);
420 public double getMotorOverhang() {
425 public void setMotorOverhang(double overhang) {
426 if (MathUtil.equals(this.overhang, overhang))
428 this.overhang = overhang;
429 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
434 public Coordinate getMotorPosition(String id) {
435 Motor motor = motors.get(id);
437 throw new IllegalArgumentException("No motor with id " + id + " defined.");
440 return new Coordinate(this.getLength() - motor.getLength() + this.getMotorOverhang());
448 * Copy the motor and ejection delay HashMaps.
450 * @see rocketcomponent.RocketComponent#copy()
452 @SuppressWarnings("unchecked")
454 protected RocketComponent copyWithOriginalID() {
455 RocketComponent c = super.copyWithOriginalID();
456 ((BodyTube) c).motors = (HashMap<String, Motor>) motors.clone();
457 ((BodyTube) c).ejectionDelays = (HashMap<String, Double>) ejectionDelays.clone();