1 package net.sf.openrocket.rocketcomponent;
4 import java.util.ArrayDeque;
5 import java.util.Collection;
6 import java.util.Deque;
7 import java.util.Iterator;
9 import java.util.NoSuchElementException;
11 import javax.swing.event.ChangeListener;
13 import net.sf.openrocket.l10n.Translator;
14 import net.sf.openrocket.logging.LogHelper;
15 import net.sf.openrocket.startup.Application;
16 import net.sf.openrocket.util.ArrayList;
17 import net.sf.openrocket.util.BugException;
18 import net.sf.openrocket.util.ChangeSource;
19 import net.sf.openrocket.util.Coordinate;
20 import net.sf.openrocket.util.Invalidator;
21 import net.sf.openrocket.util.LineStyle;
22 import net.sf.openrocket.util.MathUtil;
23 import net.sf.openrocket.util.SafetyMutex;
24 import net.sf.openrocket.util.UniqueID;
27 public abstract class RocketComponent implements ChangeSource, Cloneable, Iterable<RocketComponent> {
28 private static final LogHelper log = Application.getLogger();
29 private static final Translator trans = Application.getTranslator();
32 * Text is suitable to the form
33 * Position relative to: <title>
35 public enum Position {
36 /** Position relative to the top of the parent component. */
37 //// Top of the parent component
38 TOP(trans.get("RocketComponent.Position.TOP")),
39 /** Position relative to the middle of the parent component. */
40 //// Middle of the parent component
41 MIDDLE(trans.get("RocketComponent.Position.MIDDLE")),
42 /** Position relative to the bottom of the parent component. */
43 //// Bottom of the parent component
44 BOTTOM(trans.get("RocketComponent.Position.BOTTOM")),
45 /** Position after the parent component (for body components). */
46 //// After the parent component
47 AFTER(trans.get("RocketComponent.Position.AFTER")),
48 /** Specify an absolute X-coordinate position. */
49 //// Tip of the nose cone
50 ABSOLUTE(trans.get("RocketComponent.Position.ABSOLUTE"));
54 Position(String title) {
59 public String toString() {
65 * A safety mutex that can be used to prevent concurrent access to this component.
67 protected SafetyMutex mutex = SafetyMutex.newInstance();
69 //////// Parent/child trees
71 * Parent component of the current component, or null if none exists.
73 private RocketComponent parent = null;
76 * List of child components of this component.
78 private ArrayList<RocketComponent> children = new ArrayList<RocketComponent>();
81 //////// Parameters common to all components:
84 * Characteristic length of the component. This is used in calculating the coordinate
85 * transformations and positions of other components in reference to this component.
86 * This may and should be used as the "true" length of the component, where applicable.
87 * By default it is zero, i.e. no translation.
89 protected double length = 0;
92 * Positioning of this component relative to the parent component.
94 protected Position relativePosition;
97 * Offset of the position of this component relative to the normal position given by
98 * relativePosition. By default zero, i.e. no position change.
100 protected double position = 0;
103 // Color of the component, null means to use the default color
104 private Color color = null;
105 private LineStyle lineStyle = null;
109 private double overrideMass = 0;
110 private boolean massOverriden = false;
111 private double overrideCGX = 0;
112 private boolean cgOverriden = false;
114 private boolean overrideSubcomponents = false;
117 // User-given name of the component
118 private String name = null;
120 // User-specified comment
121 private String comment = "";
123 // Unique ID of the component
124 private String id = null;
127 * Used to invalidate the component after calling {@link #copyFrom(RocketComponent)}.
129 private Invalidator invalidator = new Invalidator(this);
132 //// NOTE !!! All fields must be copied in the method copyFrom()! ////
137 * Default constructor. Sets the name of the component to the component's static name
138 * and the relative position of the component.
140 public RocketComponent(Position relativePosition) {
141 // These must not fire any events, due to Rocket undo system initialization
142 this.name = getComponentName();
143 this.relativePosition = relativePosition;
147 //////////// Methods that must be implemented ////////////
151 * Static component name. The name may not vary of the parameters, it must be static.
153 public abstract String getComponentName(); // Static component type name
156 * Return the component mass (regardless of mass overriding).
158 public abstract double getComponentMass(); // Mass of non-overridden component
161 * Return the component CG and mass (regardless of CG or mass overriding).
163 public abstract Coordinate getComponentCG(); // CG of non-overridden component
167 * Return the longitudinal (around the y- or z-axis) unitary moment of inertia.
168 * The unitary moment of inertia is the moment of inertia with the assumption that
169 * the mass of the component is one kilogram. The inertia is measured in
170 * respect to the non-overridden CG.
172 * @return the longitudinal unitary moment of inertia of this component.
174 public abstract double getLongitudinalUnitInertia();
178 * Return the rotational (around the x-axis) unitary moment of inertia.
179 * The unitary moment of inertia is the moment of inertia with the assumption that
180 * the mass of the component is one kilogram. The inertia is measured in
181 * respect to the non-overridden CG.
183 * @return the rotational unitary moment of inertia of this component.
185 public abstract double getRotationalUnitInertia();
189 * Test whether this component allows any children components. This method must
190 * return true if and only if {@link #isCompatible(Class)} returns true for any
191 * rocket component class.
193 * @return <code>true</code> if children can be attached to this component, <code>false</code> otherwise.
195 public abstract boolean allowsChildren();
198 * Test whether the given component type can be added to this component. This type safety
199 * is enforced by the <code>addChild()</code> methods. The return value of this method
200 * may change to reflect the current state of this component (e.g. two components of some
201 * type cannot be placed as children).
203 * @param type The RocketComponent class type to add.
204 * @return Whether such a component can be added.
206 public abstract boolean isCompatible(Class<? extends RocketComponent> type);
209 /* Non-abstract helper method */
211 * Test whether the given component can be added to this component. This is equivalent
212 * to calling <code>isCompatible(c.getClass())</code>.
214 * @param c Component to test.
215 * @return Whether the component can be added.
216 * @see #isCompatible(Class)
218 public final boolean isCompatible(RocketComponent c) {
220 return isCompatible(c.getClass());
226 * Return a collection of bounding coordinates. The coordinates must be such that
227 * the component is fully enclosed in their convex hull.
229 * @return a collection of coordinates that bound the component.
231 public abstract Collection<Coordinate> getComponentBounds();
234 * Return true if the component may have an aerodynamic effect on the rocket.
236 public abstract boolean isAerodynamic();
239 * Return true if the component may have an effect on the rocket's mass.
241 public abstract boolean isMassive();
247 //////////// Methods that may be overridden ////////////
251 * Shift the coordinates in the array corresponding to radial movement. A component
252 * that has a radial position must shift the coordinates in this array suitably.
253 * If the component is clustered, then a new array must be returned with a
254 * coordinate for each cluster.
256 * The default implementation simply returns the array, and thus produces no shift.
258 * @param c an array of coordinates to shift.
259 * @return an array of shifted coordinates. The method may modify the contents
260 * of the passed array and return the array itself.
262 public Coordinate[] shiftCoordinates(Coordinate[] c) {
269 * Called when any component in the tree fires a ComponentChangeEvent. This is by
270 * default a no-op, but subclasses may override this method to e.g. invalidate
271 * cached data. The overriding method *must* call
272 * <code>super.componentChanged(e)</code> at some point.
274 * @param e The event fired
276 protected void componentChanged(ComponentChangeEvent e) {
285 * Return the user-provided name of the component, or the component base
286 * name if the user-provided name is empty. This can be used in the UI.
288 * @return A string describing the component.
291 public final String toString() {
293 if (name.length() == 0)
294 return getComponentName();
301 * Create a string describing the basic component structure from this component downwards.
302 * @return a string containing the rocket structure
304 public final String toDebugString() {
305 mutex.lock("toDebugString");
307 StringBuilder sb = new StringBuilder();
309 return sb.toString();
311 mutex.unlock("toDebugString");
315 private void toDebugString(StringBuilder sb) {
316 sb.append(this.getClass().getSimpleName()).append('@').append(System.identityHashCode(this));
317 sb.append("[\"").append(this.getName()).append('"');
318 for (RocketComponent c : this.children) {
327 * Make a deep copy of the rocket component tree structure from this component
328 * downwards for copying purposes. Each component in the copy will be assigned
329 * a new component ID, making it a safe copy. This method does not fire any events.
331 * @return A deep copy of the structure.
333 public final RocketComponent copy() {
334 RocketComponent clone = copyWithOriginalID();
336 Iterator<RocketComponent> iterator = clone.iterator(true);
337 while (iterator.hasNext()) {
338 iterator.next().newID();
346 * Make a deep copy of the rocket component tree structure from this component
347 * downwards while maintaining the component ID's. The purpose of this method is
348 * to allow copies to be created with the original ID's for the purpose of the
349 * undo/redo mechanism. This method should not be used for other purposes,
350 * such as copy/paste. This method does not fire any events.
352 * This method must be overridden by any component that refers to mutable objects,
353 * or if some fields should not be copied. This should be performed by
354 * <code>RocketComponent c = super.copyWithOriginalID();</code> and then cloning/modifying
355 * the appropriate fields.
357 * This is not performed as serializing/deserializing for performance reasons.
359 * @return A deep copy of the structure.
361 protected RocketComponent copyWithOriginalID() {
362 mutex.lock("copyWithOriginalID");
365 RocketComponent clone;
367 clone = (RocketComponent) this.clone();
368 } catch (CloneNotSupportedException e) {
369 throw new BugException("CloneNotSupportedException encountered, report a bug!", e);
373 clone.mutex = SafetyMutex.newInstance();
375 // Reset all parent/child information
377 clone.children = new ArrayList<RocketComponent>();
379 // Add copied children to the structure without firing events.
380 for (RocketComponent child : this.children) {
381 RocketComponent childCopy = child.copyWithOriginalID();
382 // Don't use add method since it fires events
383 clone.children.add(childCopy);
384 childCopy.parent = clone;
387 this.checkComponentStructure();
388 clone.checkComponentStructure();
392 mutex.unlock("copyWithOriginalID");
397 ////////////// Methods that may not be overridden ////////////
401 ////////// Common parameter setting/getting //////////
404 * Return the color of the object to use in 2D figures, or <code>null</code>
405 * to use the default color.
407 public final Color getColor() {
413 * Set the color of the object to use in 2D figures.
415 public final void setColor(Color c) {
416 if ((color == null && c == null) ||
417 (color != null && color.equals(c)))
422 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
426 public final LineStyle getLineStyle() {
431 public final void setLineStyle(LineStyle style) {
432 if (this.lineStyle == style)
435 this.lineStyle = style;
436 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
443 * Get the current override mass. The mass is not necessarily in use
446 * @return the override mass
448 public final double getOverrideMass() {
454 * Set the current override mass. The mass is not set to use by this
457 * @param m the override mass
459 public final void setOverrideMass(double m) {
460 if (MathUtil.equals(m, overrideMass))
463 overrideMass = Math.max(m, 0);
465 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
469 * Return whether mass override is active for this component. This does NOT
470 * take into account whether a parent component is overriding the mass.
472 * @return whether the mass is overridden
474 public final boolean isMassOverridden() {
476 return massOverriden;
480 * Set whether the mass is currently overridden.
482 * @param o whether the mass is overridden
484 public final void setMassOverridden(boolean o) {
485 if (massOverriden == o) {
490 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
498 * Return the current override CG. The CG is not necessarily overridden.
500 * @return the override CG
502 public final Coordinate getOverrideCG() {
504 return getComponentCG().setX(overrideCGX);
508 * Return the x-coordinate of the current override CG.
510 * @return the x-coordinate of the override CG.
512 public final double getOverrideCGX() {
518 * Set the current override CG to (x,0,0).
520 * @param x the x-coordinate of the override CG to set.
522 public final void setOverrideCGX(double x) {
523 if (MathUtil.equals(overrideCGX, x))
526 this.overrideCGX = x;
527 if (isCGOverridden())
528 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
530 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
534 * Return whether the CG is currently overridden.
536 * @return whether the CG is overridden
538 public final boolean isCGOverridden() {
544 * Set whether the CG is currently overridden.
546 * @param o whether the CG is overridden
548 public final void setCGOverridden(boolean o) {
549 if (cgOverriden == o) {
554 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
560 * Return whether the mass and/or CG override overrides all subcomponent values
561 * as well. The default implementation is a normal getter/setter implementation,
562 * however, subclasses are allowed to override this behavior if some subclass
563 * always or never overrides subcomponents. In this case the subclass should
564 * also override {@link #isOverrideSubcomponentsEnabled()} to return
565 * <code>false</code>.
567 * @return whether the current mass and/or CG override overrides subcomponents as well.
569 public boolean getOverrideSubcomponents() {
571 return overrideSubcomponents;
576 * Set whether the mass and/or CG override overrides all subcomponent values
577 * as well. See {@link #getOverrideSubcomponents()} for details.
579 * @param override whether the mass and/or CG override overrides all subcomponent.
581 public void setOverrideSubcomponents(boolean override) {
582 if (overrideSubcomponents == override) {
586 overrideSubcomponents = override;
587 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
591 * Return whether the option to override all subcomponents is enabled or not.
592 * The default implementation returns <code>false</code> if neither mass nor
593 * CG is overridden, <code>true</code> otherwise.
595 * This method may be overridden if the setting of overriding subcomponents
598 * @return whether the option to override subcomponents is currently enabled.
600 public boolean isOverrideSubcomponentsEnabled() {
602 return isCGOverridden() || isMassOverridden();
609 * Get the user-defined name of the component.
611 public final String getName() {
617 * Set the user-defined name of the component. If name==null, sets the name to
618 * the default name, currently the component name.
620 public final void setName(String name) {
621 if (this.name.equals(name)) {
625 if (name == null || name.matches("^\\s*$"))
626 this.name = getComponentName();
629 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
634 * Return the comment of the component. The component may contain multiple lines
635 * using \n as a newline separator.
637 * @return the comment of the component.
639 public final String getComment() {
645 * Set the comment of the component.
647 * @param comment the comment of the component.
649 public final void setComment(String comment) {
650 if (this.comment.equals(comment))
656 this.comment = comment;
657 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
663 * Returns the unique ID of the component.
665 * @return the ID of the component.
667 public final String getID() {
672 * Generate a new ID for this component.
674 private final void newID() {
676 this.id = UniqueID.uuid();
683 * Get the characteristic length of the component, for example the length of a body tube
684 * of the length of the root chord of a fin. This is used in positioning the component
685 * relative to its parent.
687 * If the length of a component is settable, the class must define the setter method
690 public final double getLength() {
696 * Get the positioning of the component relative to its parent component.
697 * This is one of the enums of {@link Position}. A setter method is not provided,
698 * but can be provided by a subclass.
700 public final Position getRelativePosition() {
702 return relativePosition;
707 * Set the positioning of the component relative to its parent component.
708 * The actual position of the component is maintained to the best ability.
710 * The default implementation is of protected visibility, since many components
711 * do not support setting the relative position. A component that does support
712 * it should override this with a public method that simply calls this
713 * supermethod AND fire a suitable ComponentChangeEvent.
715 * @param position the relative positioning.
717 protected void setRelativePosition(RocketComponent.Position position) {
718 if (this.relativePosition == position)
722 // Update position so as not to move the component
723 if (this.parent != null) {
724 double thisPos = this.toRelative(Coordinate.NUL, this.parent)[0].x;
728 this.position = this.toAbsolute(Coordinate.NUL)[0].x;
732 this.position = thisPos;
736 this.position = thisPos - (this.parent.length - this.length) / 2;
740 this.position = thisPos - (this.parent.length - this.length);
744 throw new BugException("Unknown position type: " + position);
748 this.relativePosition = position;
749 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
756 * Get the position value of the component. The exact meaning of the value is
757 * dependent on the current relative positioning.
759 * @return the positional value.
761 public final double getPositionValue() {
768 * Set the position value of the component. The exact meaning of the value
769 * depends on the current relative positioning.
771 * The default implementation is of protected visibility, since many components
772 * do not support setting the relative position. A component that does support
773 * it should override this with a public method that simply calls this
774 * supermethod AND fire a suitable ComponentChangeEvent.
776 * @param value the position value of the component.
778 public void setPositionValue(double value) {
779 if (MathUtil.equals(this.position, value))
782 this.position = value;
787 /////////// Coordinate changes ///////////
790 * Returns coordinate c in absolute coordinates. Equivalent to toComponent(c,null).
792 public Coordinate[] toAbsolute(Coordinate c) {
794 return toRelative(c, null);
799 * Return coordinate <code>c</code> described in the coordinate system of
800 * <code>dest</code>. If <code>dest</code> is <code>null</code> returns
801 * absolute coordinates.
803 * This method returns an array of coordinates, each of which represents a
804 * position of the coordinate in clustered cases. The array is guaranteed
805 * to contain at least one element.
807 * The current implementation does not support rotating components.
809 * @param c Coordinate in the component's coordinate system.
810 * @param dest Destination component coordinate system.
811 * @return an array of coordinates describing <code>c</code> in coordinates
812 * relative to <code>dest</code>.
814 public final Coordinate[] toRelative(Coordinate c, RocketComponent dest) {
816 mutex.lock("toRelative");
818 double absoluteX = Double.NaN;
819 RocketComponent search = dest;
820 Coordinate[] array = new Coordinate[1];
823 RocketComponent component = this;
824 while ((component != search) && (component.parent != null)) {
826 array = component.shiftCoordinates(array);
828 switch (component.relativePosition) {
830 for (int i = 0; i < array.length; i++) {
831 array[i] = array[i].add(component.position, 0, 0);
836 for (int i = 0; i < array.length; i++) {
837 array[i] = array[i].add(component.position +
838 (component.parent.length - component.length) / 2, 0, 0);
843 for (int i = 0; i < array.length; i++) {
844 array[i] = array[i].add(component.position +
845 (component.parent.length - component.length), 0, 0);
850 // Add length of all previous brother-components with POSITION_RELATIVE_AFTER
851 int index = component.parent.children.indexOf(component);
853 for (index--; index >= 0; index--) {
854 RocketComponent comp = component.parent.children.get(index);
855 double componentLength = comp.getTotalLength();
856 for (int i = 0; i < array.length; i++) {
857 array[i] = array[i].add(componentLength, 0, 0);
860 for (int i = 0; i < array.length; i++) {
861 array[i] = array[i].add(component.position + component.parent.length, 0, 0);
866 search = null; // Requires back-search if dest!=null
867 if (Double.isNaN(absoluteX)) {
868 absoluteX = component.position;
873 throw new BugException("Unknown relative positioning type of component" +
874 component + ": " + component.relativePosition);
877 component = component.parent; // parent != null
880 if (!Double.isNaN(absoluteX)) {
881 for (int i = 0; i < array.length; i++) {
882 array[i] = array[i].setX(absoluteX + c.x);
886 // Check whether destination has been found or whether to backtrack
887 // TODO: LOW: Backtracking into clustered components uses only one component
888 if ((dest != null) && (component != dest)) {
889 Coordinate[] origin = dest.toAbsolute(Coordinate.NUL);
890 for (int i = 0; i < array.length; i++) {
891 array[i] = array[i].sub(origin[0]);
897 mutex.unlock("toRelative");
903 * Recursively sum the lengths of all subcomponents that have position
906 * @return Sum of the lengths.
908 private final double getTotalLength() {
910 this.checkComponentStructure();
911 mutex.lock("getTotalLength");
914 if (relativePosition == Position.AFTER)
916 for (int i = 0; i < children.size(); i++)
917 l += children.get(i).getTotalLength();
920 mutex.unlock("getTotalLength");
926 /////////// Total mass and CG calculation ////////////
929 * Return the (possibly overridden) mass of component.
931 * @return The mass of the component or the given override mass.
933 public final double getMass() {
937 return getComponentMass();
941 * Return the (possibly overridden) center of gravity and mass.
943 * Returns the CG with the weight of the coordinate set to the weight of the component.
944 * Both CG and mass may be separately overridden.
946 * @return The CG of the component or the given override CG.
948 public final Coordinate getCG() {
951 return getOverrideCG().setWeight(getMass());
954 return getComponentCG().setWeight(getMass());
956 return getComponentCG();
961 * Return the longitudinal (around the y- or z-axis) moment of inertia of this component.
962 * The moment of inertia is scaled in reference to the (possibly overridden) mass
963 * and is relative to the non-overridden CG.
965 * @return the longitudinal moment of inertia of this component.
967 public final double getLongitudinalInertia() {
969 return getLongitudinalUnitInertia() * getMass();
973 * Return the rotational (around the y- or z-axis) moment of inertia of this component.
974 * The moment of inertia is scaled in reference to the (possibly overridden) mass
975 * and is relative to the non-overridden CG.
977 * @return the rotational moment of inertia of this component.
979 public final double getRotationalInertia() {
981 return getRotationalUnitInertia() * getMass();
986 /////////// Children handling ///////////
990 * Adds a child to the rocket component tree. The component is added to the end
991 * of the component's child list. This is a helper method that calls
992 * {@link #addChild(RocketComponent,int)}.
994 * @param component The component to add.
995 * @throws IllegalArgumentException if the component is already part of some
997 * @see #addChild(RocketComponent,int)
999 public final void addChild(RocketComponent component) {
1001 addChild(component, children.size());
1006 * Adds a child to the rocket component tree. The component is added to
1007 * the given position of the component's child list.
1009 * This method may be overridden to enforce more strict component addition rules.
1010 * The tests should be performed first and then this method called.
1012 * @param component The component to add.
1013 * @param index Position to add component to.
1014 * @throws IllegalArgumentException If the component is already part of
1015 * some component tree.
1017 public void addChild(RocketComponent component, int index) {
1019 if (component.parent != null) {
1020 throw new IllegalArgumentException("component " + component.getComponentName() +
1021 " is already in a tree");
1023 if (!isCompatible(component)) {
1024 throw new IllegalStateException("Component " + component.getComponentName() +
1025 " not currently compatible with component " + getComponentName());
1028 children.add(index, component);
1029 component.parent = this;
1031 this.checkComponentStructure();
1032 component.checkComponentStructure();
1034 fireAddRemoveEvent(component);
1039 * Removes a child from the rocket component tree.
1041 * @param n remove the n'th child.
1042 * @throws IndexOutOfBoundsException if n is out of bounds
1044 public final void removeChild(int n) {
1046 RocketComponent component = children.remove(n);
1047 component.parent = null;
1049 this.checkComponentStructure();
1050 component.checkComponentStructure();
1052 fireAddRemoveEvent(component);
1056 * Removes a child from the rocket component tree. Does nothing if the component
1057 * is not present as a child.
1059 * @param component the component to remove
1060 * @return whether the component was a child
1062 public final boolean removeChild(RocketComponent component) {
1065 component.checkComponentStructure();
1067 if (children.remove(component)) {
1068 component.parent = null;
1070 this.checkComponentStructure();
1071 component.checkComponentStructure();
1073 fireAddRemoveEvent(component);
1083 * Move a child to another position.
1085 * @param component the component to move
1086 * @param index the component's new position
1087 * @throws IllegalArgumentException If an illegal placement was attempted.
1089 public final void moveChild(RocketComponent component, int index) {
1091 if (children.remove(component)) {
1092 children.add(index, component);
1094 this.checkComponentStructure();
1095 component.checkComponentStructure();
1097 fireAddRemoveEvent(component);
1103 * Fires an AERODYNAMIC_CHANGE, MASS_CHANGE or OTHER_CHANGE event depending on the
1104 * type of component removed.
1106 private void fireAddRemoveEvent(RocketComponent component) {
1107 Iterator<RocketComponent> iter = component.iterator(true);
1108 int type = ComponentChangeEvent.TREE_CHANGE;
1109 while (iter.hasNext()) {
1110 RocketComponent c = iter.next();
1111 if (c.isAerodynamic())
1112 type |= ComponentChangeEvent.AERODYNAMIC_CHANGE;
1114 type |= ComponentChangeEvent.MASS_CHANGE;
1117 fireComponentChangeEvent(type);
1121 public final int getChildCount() {
1123 this.checkComponentStructure();
1124 return children.size();
1127 public final RocketComponent getChild(int n) {
1129 this.checkComponentStructure();
1130 return children.get(n);
1133 public final List<RocketComponent> getChildren() {
1135 this.checkComponentStructure();
1136 return children.clone();
1141 * Returns the position of the child in this components child list, or -1 if the
1142 * component is not a child of this component.
1144 * @param child The child to search for.
1145 * @return Position in the list or -1 if not found.
1147 public final int getChildPosition(RocketComponent child) {
1149 this.checkComponentStructure();
1150 return children.indexOf(child);
1154 * Get the parent component of this component. Returns <code>null</code> if the component
1157 * @return The parent of this component or <code>null</code>.
1159 public final RocketComponent getParent() {
1165 * Get the root component of the component tree.
1167 * @return The root component of the component tree.
1169 public final RocketComponent getRoot() {
1171 RocketComponent gp = this;
1172 while (gp.parent != null)
1178 * Returns the root Rocket component of this component tree. Throws an
1179 * IllegalStateException if the root component is not a Rocket.
1181 * @return The root Rocket component of the component tree.
1182 * @throws IllegalStateException If the root component is not a Rocket.
1184 public final Rocket getRocket() {
1186 RocketComponent r = getRoot();
1187 if (r instanceof Rocket)
1189 throw new IllegalStateException("getRocket() called with root component "
1190 + r.getComponentName());
1195 * Return the Stage component that this component belongs to. Throws an
1196 * IllegalStateException if a Stage is not in the parentage of this component.
1198 * @return The Stage component this component belongs to.
1199 * @throws IllegalStateException if a Stage component is not in the parentage.
1201 public final Stage getStage() {
1203 RocketComponent c = this;
1205 if (c instanceof Stage)
1209 throw new IllegalStateException("getStage() called without Stage as a parent.");
1213 * Return the stage number of the stage this component belongs to. The stages
1214 * are numbered from zero upwards.
1216 * @return the stage number this component belongs to.
1218 public final int getStageNumber() {
1220 if (parent == null) {
1221 throw new IllegalArgumentException("getStageNumber() called for root component");
1224 RocketComponent stage = this;
1225 while (!(stage instanceof Stage)) {
1226 stage = stage.parent;
1227 if (stage == null || stage.parent == null) {
1228 throw new IllegalStateException("getStageNumber() could not find parent " +
1232 return stage.parent.getChildPosition(stage);
1237 * Find a component with the given ID. The component tree is searched from this component
1238 * down (including this component) for the ID and the corresponding component is returned,
1239 * or null if not found.
1241 * @param idToFind ID to search for.
1242 * @return The component with the ID, or null if not found.
1244 public final RocketComponent findComponent(String idToFind) {
1246 Iterator<RocketComponent> iter = this.iterator(true);
1247 while (iter.hasNext()) {
1248 RocketComponent c = iter.next();
1249 if (c.getID().equals(idToFind))
1256 // TODO: Move these methods elsewhere (used only in SymmetricComponent)
1257 public final RocketComponent getPreviousComponent() {
1259 this.checkComponentStructure();
1262 int pos = parent.getChildPosition(this);
1264 StringBuffer sb = new StringBuffer();
1265 sb.append("Inconsistent internal state: ");
1266 sb.append("this=").append(this).append('[')
1267 .append(System.identityHashCode(this)).append(']');
1268 sb.append(" parent.children=[");
1269 for (int i = 0; i < parent.children.size(); i++) {
1270 RocketComponent c = parent.children.get(i);
1271 sb.append(c).append('[').append(System.identityHashCode(c)).append(']');
1272 if (i < parent.children.size() - 1)
1276 throw new IllegalStateException(sb.toString());
1281 RocketComponent c = parent.getChild(pos - 1);
1282 while (c.getChildCount() > 0)
1283 c = c.getChild(c.getChildCount() - 1);
1287 // TODO: Move these methods elsewhere (used only in SymmetricComponent)
1288 public final RocketComponent getNextComponent() {
1290 if (getChildCount() > 0)
1293 RocketComponent current = this;
1294 RocketComponent nextParent = this.parent;
1296 while (nextParent != null) {
1297 int pos = nextParent.getChildPosition(current);
1298 if (pos < nextParent.getChildCount() - 1)
1299 return nextParent.getChild(pos + 1);
1301 current = nextParent;
1302 nextParent = current.parent;
1308 /////////// Event handling //////////
1310 // Listener lists are provided by the root Rocket component,
1311 // a single listener list for the whole rocket.
1315 * Adds a ComponentChangeListener to the rocket tree. The listener is added to the root
1316 * component, which must be of type Rocket (which overrides this method). Events of all
1317 * subcomponents are sent to all listeners.
1319 * @throws IllegalStateException - if the root component is not a Rocket
1321 public void addComponentChangeListener(ComponentChangeListener l) {
1323 getRocket().addComponentChangeListener(l);
1327 * Removes a ComponentChangeListener from the rocket tree. The listener is removed from
1328 * the root component, which must be of type Rocket (which overrides this method).
1329 * Does nothing if the root component is not a Rocket. (The asymmetry is so
1330 * that listeners can always be removed just in case.)
1332 * @param l Listener to remove
1334 public void removeComponentChangeListener(ComponentChangeListener l) {
1335 if (parent != null) {
1336 getRoot().removeComponentChangeListener(l);
1342 * Adds a <code>ChangeListener</code> to the rocket tree. This is identical to
1343 * <code>addComponentChangeListener()</code> except that it uses a
1344 * <code>ChangeListener</code>. The same events are dispatched to the
1345 * <code>ChangeListener</code>, as <code>ComponentChangeEvent</code> is a subclass
1346 * of <code>ChangeEvent</code>.
1348 * @throws IllegalStateException - if the root component is not a <code>Rocket</code>
1351 public void addChangeListener(ChangeListener l) {
1353 getRocket().addChangeListener(l);
1357 * Removes a ChangeListener from the rocket tree. This is identical to
1358 * removeComponentChangeListener() except it uses a ChangeListener.
1359 * Does nothing if the root component is not a Rocket. (The asymmetry is so
1360 * that listeners can always be removed just in case.)
1362 * @param l Listener to remove
1365 public void removeChangeListener(ChangeListener l) {
1366 if (this.parent != null) {
1367 getRoot().removeChangeListener(l);
1373 * Fires a ComponentChangeEvent on the rocket structure. The call is passed to the
1374 * root component, which must be of type Rocket (which overrides this method).
1375 * Events of all subcomponents are sent to all listeners.
1377 * If the component tree root is not a Rocket, the event is ignored. This is the
1378 * case when constructing components not in any Rocket tree. In this case it
1379 * would be impossible for the component to have listeners in any case.
1381 * @param e Event to send
1383 protected void fireComponentChangeEvent(ComponentChangeEvent e) {
1385 if (parent == null) {
1386 /* Ignore if root invalid. */
1387 log.debug("Attempted firing event " + e + " with root " + this.getComponentName() + ", ignoring event");
1390 getRoot().fireComponentChangeEvent(e);
1395 * Fires a ComponentChangeEvent of the given type. The source of the event is set to
1398 * @param type Type of event
1399 * @see #fireComponentChangeEvent(ComponentChangeEvent)
1401 protected void fireComponentChangeEvent(int type) {
1402 fireComponentChangeEvent(new ComponentChangeEvent(this, type));
1407 * Checks whether this component has been invalidated and should no longer be used.
1408 * This is a safety check that in-place replaced components are no longer used.
1409 * All non-trivial methods (with the exception of methods simply getting a property)
1410 * should call this method before changing or computing anything.
1412 * @throws BugException if this component has been invalidated by {@link #copyFrom(RocketComponent)}.
1414 protected void checkState() {
1415 invalidator.check(true);
1421 * Check that the local component structure is correct. This can be called after changing
1422 * the component structure in order to verify the integrity.
1424 * TODO: Remove this after the "inconsistent internal state" bug has been corrected
1426 public void checkComponentStructure() {
1427 if (this.parent != null) {
1428 // Test that this component is found in parent's children with == operator
1429 if (!containsExact(this.parent.children, this)) {
1430 throw new BugException("Inconsistent component structure detected, parent does not contain this " +
1431 "component as a child, parent=" + parent.toDebugString() + " this=" + this.toDebugString());
1434 for (RocketComponent child : this.children) {
1435 if (child.parent != this) {
1436 throw new BugException("Inconsistent component structure detected, child does not have this component " +
1437 "as the parent, this=" + this.toDebugString() + " child=" + child.toDebugString() +
1438 " child.parent=" + (child.parent == null ? "null" : child.parent.toDebugString()));
1443 // Check whether the list contains exactly the searched-for component (with == operator)
1444 private boolean containsExact(List<RocketComponent> haystack, RocketComponent needle) {
1445 for (RocketComponent c : haystack) {
1454 /////////// Iterators //////////
1457 * Returns an iterator that iterates over all children and sub-children.
1459 * The iterator iterates through all children below this object, including itself if
1460 * <code>returnSelf</code> is true. The order of the iteration is not specified
1461 * (it may be specified in the future).
1463 * If an iterator iterating over only the direct children of the component is required,
1464 * use <code>component.getChildren().iterator()</code>.
1466 * TODO: HIGH: Remove this after merges have been done
1468 * @param returnSelf boolean value specifying whether the component itself should be
1470 * @return An iterator for the children and sub-children.
1471 * @deprecated Use {@link #iterator(boolean)} instead
1474 public final Iterator<RocketComponent> deepIterator(boolean returnSelf) {
1475 return iterator(returnSelf);
1480 * Returns an iterator that iterates over all children and sub-children, including itself.
1482 * This method is equivalent to <code>deepIterator(true)</code>.
1484 * TODO: HIGH: Remove this after merges have been done
1486 * @return An iterator for this component, its children and sub-children.
1487 * @deprecated Use {@link #iterator()} instead
1490 public final Iterator<RocketComponent> deepIterator() {
1497 * Returns an iterator that iterates over all children and sub-children.
1499 * The iterator iterates through all children below this object, including itself if
1500 * <code>returnSelf</code> is true. The order of the iteration is not specified
1501 * (it may be specified in the future).
1503 * If an iterator iterating over only the direct children of the component is required,
1504 * use <code>component.getChildren().iterator()</code>.
1506 * @param returnSelf boolean value specifying whether the component itself should be
1508 * @return An iterator for the children and sub-children.
1510 public final Iterator<RocketComponent> iterator(boolean returnSelf) {
1512 return new RocketComponentIterator(this, returnSelf);
1517 * Returns an iterator that iterates over this component, its children and sub-children.
1519 * This method is equivalent to <code>iterator(true)</code>.
1521 * @return An iterator for this component, its children and sub-children.
1524 public final Iterator<RocketComponent> iterator() {
1525 return iterator(true);
1533 * Compare component equality based on the ID of this component. Only the
1534 * ID and class type is used for a basis of comparison.
1537 public boolean equals(Object obj) {
1542 if (this.getClass() != obj.getClass())
1544 RocketComponent other = (RocketComponent) obj;
1545 return this.id.equals(other.id);
1551 public int hashCode() {
1552 return id.hashCode();
1557 //////////// Helper methods for subclasses
1563 * Helper method to add rotationally symmetric bounds at the specified coordinates.
1564 * The X-axis value is <code>x</code> and the radius at the specified position is
1567 protected static final void addBound(Collection<Coordinate> bounds, double x, double r) {
1568 bounds.add(new Coordinate(x, -r, -r));
1569 bounds.add(new Coordinate(x, r, -r));
1570 bounds.add(new Coordinate(x, r, r));
1571 bounds.add(new Coordinate(x, -r, r));
1575 protected static final Coordinate ringCG(double outerRadius, double innerRadius,
1576 double x1, double x2, double density) {
1577 return new Coordinate((x1 + x2) / 2, 0, 0,
1578 ringMass(outerRadius, innerRadius, x2 - x1, density));
1581 protected static final double ringMass(double outerRadius, double innerRadius,
1582 double length, double density) {
1583 return Math.PI * (MathUtil.pow2(outerRadius) - MathUtil.pow2(innerRadius)) *
1587 protected static final double ringLongitudinalUnitInertia(double outerRadius,
1588 double innerRadius, double length) {
1589 // 1/12 * (3 * (r1^2 + r2^2) + h^2)
1590 return (3 * (MathUtil.pow2(innerRadius) + MathUtil.pow2(outerRadius)) + MathUtil.pow2(length)) / 12;
1593 protected static final double ringRotationalUnitInertia(double outerRadius,
1594 double innerRadius) {
1595 // 1/2 * (r1^2 + r2^2)
1596 return (MathUtil.pow2(innerRadius) + MathUtil.pow2(outerRadius)) / 2;
1605 * Loads the RocketComponent fields from the given component. This method is meant
1606 * for in-place replacement of a component. It is used with the undo/redo
1607 * mechanism and when converting a finset into a freeform fin set.
1608 * This component must not have a parent, otherwise this method will fail.
1610 * The child components in the source tree are copied into the current tree, however,
1611 * the original components should not be used since they represent old copies of the
1612 * components. It is recommended to invalidate them by calling {@link #invalidate()}.
1614 * This method returns a list of components that should be invalidated after references
1615 * to them have been removed (for example by firing appropriate events). The list contains
1616 * all children and sub-children of the current component and the entire component
1617 * tree of <code>src</code>.
1619 * @return a list of components that should not be used after this call.
1621 protected List<RocketComponent> copyFrom(RocketComponent src) {
1623 List<RocketComponent> toInvalidate = new ArrayList<RocketComponent>();
1625 if (this.parent != null) {
1626 throw new UnsupportedOperationException("copyFrom called for non-root component, parent=" +
1627 this.parent.toDebugString() + ", this=" + this.toDebugString());
1630 // Add current structure to be invalidated
1631 Iterator<RocketComponent> iterator = this.iterator(false);
1632 while (iterator.hasNext()) {
1633 toInvalidate.add(iterator.next());
1636 // Remove previous components
1637 for (RocketComponent child : this.children) {
1638 child.parent = null;
1640 this.children.clear();
1642 // Copy new children to this component
1643 for (RocketComponent c : src.children) {
1644 RocketComponent copy = c.copyWithOriginalID();
1645 this.children.add(copy);
1649 this.checkComponentStructure();
1650 src.checkComponentStructure();
1652 // Set all parameters
1653 this.length = src.length;
1654 this.relativePosition = src.relativePosition;
1655 this.position = src.position;
1656 this.color = src.color;
1657 this.lineStyle = src.lineStyle;
1658 this.overrideMass = src.overrideMass;
1659 this.massOverriden = src.massOverriden;
1660 this.overrideCGX = src.overrideCGX;
1661 this.cgOverriden = src.cgOverriden;
1662 this.overrideSubcomponents = src.overrideSubcomponents;
1663 this.name = src.name;
1664 this.comment = src.comment;
1667 // Add source components to invalidation tree
1668 for (RocketComponent c : src) {
1669 toInvalidate.add(c);
1672 return toInvalidate;
1675 protected void invalidate() {
1676 invalidator.invalidate();
1680 ////////// Iterator implementation ///////////
1683 * Private inner class to implement the Iterator.
1685 * This iterator is fail-fast if the root of the structure is a Rocket.
1687 private static class RocketComponentIterator implements Iterator<RocketComponent> {
1688 // Stack holds iterators which still have some components left.
1689 private final Deque<Iterator<RocketComponent>> iteratorStack = new ArrayDeque<Iterator<RocketComponent>>();
1691 private final Rocket root;
1692 private final int treeModID;
1694 private final RocketComponent original;
1695 private boolean returnSelf = false;
1697 // Construct iterator with component's child's iterator, if it has elements
1698 public RocketComponentIterator(RocketComponent c, boolean returnSelf) {
1700 RocketComponent gp = c.getRoot();
1701 if (gp instanceof Rocket) {
1703 treeModID = root.getTreeModID();
1709 Iterator<RocketComponent> i = c.children.iterator();
1711 iteratorStack.push(i);
1714 this.returnSelf = returnSelf;
1718 public boolean hasNext() {
1722 return !iteratorStack.isEmpty(); // Elements remain if stack is not empty
1726 public RocketComponent next() {
1727 Iterator<RocketComponent> i;
1731 // Return original component first
1737 // Peek first iterator from stack, throw exception if empty
1738 i = iteratorStack.peek();
1740 throw new NoSuchElementException("No further elements in RocketComponent iterator");
1743 // Retrieve next component of the iterator, remove iterator from stack if empty
1744 RocketComponent c = i.next();
1746 iteratorStack.pop();
1748 // Add iterator of component children to stack if it has children
1749 i = c.children.iterator();
1751 iteratorStack.push(i);
1756 private void checkID() {
1758 if (root.getTreeModID() != treeModID) {
1759 throw new IllegalStateException("Rocket modified while being iterated");
1765 public void remove() {
1766 throw new UnsupportedOperationException("remove() not supported by " +
1767 "RocketComponent iterator");