1 package net.sf.openrocket.rocketcomponent;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
8 import net.sf.openrocket.l10n.Translator;
9 import net.sf.openrocket.startup.Application;
10 import net.sf.openrocket.util.Coordinate;
11 import net.sf.openrocket.util.MathUtil;
12 import net.sf.openrocket.util.Transformation;
15 public abstract class FinSet extends ExternalComponent {
16 private static final Translator trans = Application.getTranslator();
19 * Maximum allowed cant of fins.
21 public static final double MAX_CANT = (15.0 * Math.PI / 180);
24 public enum CrossSection {
26 SQUARE(trans.get("FinSet.CrossSection.SQUARE"), 1.00),
28 ROUNDED(trans.get("FinSet.CrossSection.ROUNDED"), 0.99),
30 AIRFOIL(trans.get("FinSet.CrossSection.AIRFOIL"), 0.85);
32 private final String name;
33 private final double volume;
35 CrossSection(String name, double volume) {
40 public double getRelativeVolume() {
45 public String toString() {
50 public enum TabRelativePosition {
51 //// Root chord leading edge
52 FRONT(trans.get("FinSet.TabRelativePosition.FRONT")),
53 //// Root chord midpoint
54 CENTER(trans.get("FinSet.TabRelativePosition.CENTER")),
55 //// Root chord trailing edge
56 END(trans.get("FinSet.TabRelativePosition.END"));
58 private final String name;
60 TabRelativePosition(String name) {
65 public String toString() {
73 protected int fins = 3;
76 * Rotation about the x-axis by 2*PI/fins.
78 protected Transformation finRotation = Transformation.rotate_x(2 * Math.PI / fins);
81 * Rotation angle of the first fin. Zero corresponds to the positive y-axis.
83 protected double rotation = 0;
86 * Rotation about the x-axis by angle this.rotation.
88 protected Transformation baseRotation = Transformation.rotate_x(rotation);
94 protected double cantAngle = 0;
97 private Transformation cantRotation = null;
101 * Thickness of the fins.
103 protected double thickness = 0.003;
107 * The cross-section shape of the fins.
109 protected CrossSection crossSection = CrossSection.SQUARE;
113 * Fin tab properties.
115 private double tabHeight = 0;
116 private double tabLength = 0.05;
117 private double tabShift = 0;
118 private TabRelativePosition tabRelativePosition = TabRelativePosition.CENTER;
121 // Cached fin area & CG. Validity of both must be checked using finArea!
122 // Fin area does not include fin tabs, CG does.
123 private double finArea = -1;
124 private double finCGx = -1;
125 private double finCGy = -1;
129 * New FinSet with given number of fins and given base rotation angle.
130 * Sets the component relative position to POSITION_RELATIVE_BOTTOM,
131 * i.e. fins are positioned at the bottom of the parent component.
134 super(RocketComponent.Position.BOTTOM);
140 * Return the number of fins in the set.
141 * @return The number of fins.
143 public int getFinCount() {
148 * Sets the number of fins in the set.
149 * @param n The number of fins, greater of equal to one.
151 public void setFinCount(int n) {
159 finRotation = Transformation.rotate_x(2 * Math.PI / fins);
160 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
163 public Transformation getFinRotationTransformation() {
168 * Gets the base rotation amount of the first fin.
169 * @return The base rotation amount.
171 public double getBaseRotation() {
176 * Sets the base rotation amount of the first fin.
177 * @param r The base rotation amount.
179 public void setBaseRotation(double r) {
180 r = MathUtil.reduce180(r);
181 if (MathUtil.equals(r, rotation))
184 baseRotation = Transformation.rotate_x(rotation);
185 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
188 public Transformation getBaseRotationTransformation() {
194 public double getCantAngle() {
198 public void setCantAngle(double cant) {
199 cant = MathUtil.clamp(cant, -MAX_CANT, MAX_CANT);
200 if (MathUtil.equals(cant, cantAngle))
202 this.cantAngle = cant;
203 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
207 public Transformation getCantRotation() {
208 if (cantRotation == null) {
209 if (MathUtil.equals(cantAngle, 0)) {
210 cantRotation = Transformation.IDENTITY;
212 Transformation t = new Transformation(-length / 2, 0, 0);
213 t = Transformation.rotate_y(cantAngle).applyTransformation(t);
214 t = new Transformation(length / 2, 0, 0).applyTransformation(t);
223 public double getThickness() {
227 public void setThickness(double r) {
230 thickness = Math.max(r, 0);
231 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
235 public CrossSection getCrossSection() {
239 public void setCrossSection(CrossSection cs) {
240 if (crossSection == cs)
243 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
251 public void setRelativePosition(RocketComponent.Position position) {
252 super.setRelativePosition(position);
253 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
258 public void setPositionValue(double value) {
259 super.setPositionValue(value);
260 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
266 public double getTabHeight() {
270 public void setTabHeight(double height) {
271 height = MathUtil.max(height, 0);
272 if (MathUtil.equals(this.tabHeight, height))
274 this.tabHeight = height;
275 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
279 public double getTabLength() {
283 public void setTabLength(double length) {
284 length = MathUtil.max(length, 0);
285 if (MathUtil.equals(this.tabLength, length))
287 this.tabLength = length;
288 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
292 public double getTabShift() {
296 public void setTabShift(double shift) {
297 this.tabShift = shift;
298 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
302 public TabRelativePosition getTabRelativePosition() {
303 return tabRelativePosition;
306 public void setTabRelativePosition(TabRelativePosition position) {
307 if (this.tabRelativePosition == position)
311 double front = getTabFrontEdge();
314 this.tabShift = front;
318 this.tabShift = front + tabLength / 2 - getLength() / 2;
322 this.tabShift = front + tabLength - getLength();
326 throw new IllegalArgumentException("position=" + position);
328 this.tabRelativePosition = position;
330 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
335 * Return the tab front edge position from the front of the fin.
337 public double getTabFrontEdge() {
338 switch (this.tabRelativePosition) {
343 return getLength() / 2 - tabLength / 2 + tabShift;
346 return getLength() - tabLength + tabShift;
349 throw new IllegalStateException("tabRelativePosition=" + tabRelativePosition);
354 * Return the tab trailing edge position *from the front of the fin*.
356 public double getTabTrailingEdge() {
357 switch (this.tabRelativePosition) {
359 return tabLength + tabShift;
361 return getLength() / 2 + tabLength / 2 + tabShift;
364 return getLength() + tabShift;
367 throw new IllegalStateException("tabRelativePosition=" + tabRelativePosition);
374 /////////// Calculation methods ///////////
377 * Return the area of one side of one fin. This does NOT include the area of
380 * @return the area of one side of one fin.
382 public double getFinArea() {
391 * Return the unweighted CG of a single fin. The X-coordinate is relative to
392 * the root chord trailing edge and the Y-coordinate to the fin root chord.
394 * @return the unweighted CG coordinate of a single fin.
396 public Coordinate getFinCG() {
400 return new Coordinate(finCGx, finCGy, 0);
406 public double getComponentVolume() {
407 return fins * (getFinArea() + tabHeight * tabLength) * thickness *
408 crossSection.getRelativeVolume();
413 public Coordinate getComponentCG() {
417 double mass = getComponentMass(); // safe
420 return baseRotation.transform(
421 new Coordinate(finCGx, finCGy + getBodyRadius(), 0, mass));
423 return new Coordinate(finCGx, 0, 0, mass);
428 private void calculateAreaCG() {
429 Coordinate[] points = this.getFinPoints();
434 for (int i = 0; i < points.length - 1; i++) {
435 final double x0 = points[i].x;
436 final double x1 = points[i + 1].x;
437 final double y0 = points[i].y;
438 final double y1 = points[i + 1].y;
440 double da = (y0 + y1) * (x1 - x0) / 2;
442 if (Math.abs(y0 - y1) < 0.00001) {
443 finCGx += (x0 + x1) / 2 * da;
444 finCGy += y0 / 2 * da;
446 finCGx += (x0 * (2 * y0 + y1) + x1 * (y0 + 2 * y1)) / (3 * (y0 + y1)) * da;
447 finCGy += (y1 + y0 * y0 / (y0 + y1)) / 3 * da;
454 // Add effect of fin tabs to CG
455 double tabArea = tabLength * tabHeight;
456 if (!MathUtil.equals(tabArea, 0)) {
458 double x = (getTabFrontEdge() + getTabTrailingEdge()) / 2;
459 double y = -this.tabHeight / 2;
461 finCGx += x * tabArea;
462 finCGy += y * tabArea;
466 if ((finArea + tabArea) > 0) {
467 finCGx /= (finArea + tabArea);
468 finCGy /= (finArea + tabArea);
470 finCGx = (points[0].x + points[points.length - 1].x) / 2;
477 * Return an approximation of the longitudinal unitary inertia of the fin set.
478 * The process is the following:
480 * 1. Approximate the fin with a rectangular fin
482 * 2. The inertia of one fin is taken as the average of the moments of inertia
483 * through its center perpendicular to the plane, and the inertia through
484 * its center parallel to the plane
486 * 3. If there are multiple fins, the inertia is shifted to the center of the fin
487 * set and multiplied by the number of fins.
490 public double getLongitudinalUnitInertia() {
491 double area = getFinArea();
492 if (MathUtil.equals(area, 0))
495 // Approximate fin with a rectangular fin
496 // w2 and h2 are squares of the fin width and height
497 double w = getLength();
498 double h = getSpan();
501 if (MathUtil.equals(w * h, 0)) {
509 double inertia = (h2 + 2 * w2) / 24;
514 double radius = getBodyRadius();
516 return fins * (inertia + MathUtil.pow2(Math.sqrt(h2) + radius));
521 * Return an approximation of the rotational unitary inertia of the fin set.
522 * The process is the following:
524 * 1. Approximate the fin with a rectangular fin and calculate the inertia of the
525 * rectangular approximate
527 * 2. If there are multiple fins, shift the inertia center to the fin set center
528 * and multiply with the number of fins.
531 public double getRotationalUnitInertia() {
532 double area = getFinArea();
533 if (MathUtil.equals(area, 0))
536 // Approximate fin with a rectangular fin
537 double w = getLength();
538 double h = getSpan();
540 if (MathUtil.equals(w * h, 0)) {
543 h = Math.sqrt(h * area / w);
549 double radius = getBodyRadius();
551 return fins * (h * h / 12 + MathUtil.pow2(h / 2 + radius));
556 * Adds the fin set's bounds to the collection.
559 public Collection<Coordinate> getComponentBounds() {
560 List<Coordinate> bounds = new ArrayList<Coordinate>();
561 double r = getBodyRadius();
563 for (Coordinate point : getFinPoints()) {
564 addFinBound(bounds, point.x, point.y + r);
572 * Adds the 2d-coordinate bound (x,y) to the collection for both z-components and for
575 private void addFinBound(Collection<Coordinate> set, double x, double y) {
579 c = new Coordinate(x, y, thickness / 2);
580 c = baseRotation.transform(c);
582 for (i = 1; i < fins; i++) {
583 c = finRotation.transform(c);
587 c = new Coordinate(x, y, -thickness / 2);
588 c = baseRotation.transform(c);
590 for (i = 1; i < fins; i++) {
591 c = finRotation.transform(c);
599 public void componentChanged(ComponentChangeEvent e) {
600 if (e.isAerodynamicChange()) {
608 * Return the radius of the BodyComponent the fin set is situated on. Currently
609 * only supports SymmetricComponents and returns the radius at the starting point of the
612 * @return radius of the underlying BodyComponent or 0 if none exists.
614 public double getBodyRadius() {
617 s = this.getParent();
619 if (s instanceof SymmetricComponent) {
620 double x = this.toRelative(new Coordinate(0, 0, 0), s)[0].x;
621 return ((SymmetricComponent) s).getRadius(x);
629 public boolean allowsChildren() {
634 * Allows nothing to be attached to a FinSet.
636 * @return <code>false</code>
639 public boolean isCompatible(Class<? extends RocketComponent> type) {
647 * Return a list of coordinates defining the geometry of a single fin.
648 * The coordinates are the XY-coordinates of points defining the shape of a single fin,
649 * where the origin is the leading root edge. Therefore, the first point must be (0,0,0).
650 * All Z-coordinates must be zero, and the last coordinate must have Y=0.
652 * @return List of XY-coordinates.
654 public abstract Coordinate[] getFinPoints();
658 * Return a list of coordinates defining the geometry of a single fin, including a
659 * possible fin tab. The coordinates are the XY-coordinates of points defining the
660 * shape of a single fin, where the origin is the leading root edge. This implementation
661 * calls {@link #getFinPoints()} and adds the necessary points for the fin tab.
662 * The tab coordinates will have a negative y value.
664 * @return List of XY-coordinates.
666 public Coordinate[] getFinPointsWithTab() {
667 Coordinate[] points = getFinPoints();
669 if (MathUtil.equals(getTabHeight(), 0) ||
670 MathUtil.equals(getTabLength(), 0))
673 double x1 = getTabFrontEdge();
674 double x2 = getTabTrailingEdge();
675 double y = -getTabHeight();
677 int n = points.length;
678 points = Arrays.copyOf(points, points.length + 4);
679 points[n] = new Coordinate(x2, 0);
680 points[n + 1] = new Coordinate(x2, y);
681 points[n + 2] = new Coordinate(x1, y);
682 points[n + 3] = new Coordinate(x1, 0);
689 * Get the span of a single fin. That is, the length from the root to the tip of the fin.
690 * @return Span of a single fin.
692 public abstract double getSpan();
696 protected List<RocketComponent> copyFrom(RocketComponent c) {
697 FinSet src = (FinSet) c;
698 this.fins = src.fins;
699 this.finRotation = src.finRotation;
700 this.rotation = src.rotation;
701 this.baseRotation = src.baseRotation;
702 this.cantAngle = src.cantAngle;
703 this.cantRotation = src.cantRotation;
704 this.thickness = src.thickness;
705 this.crossSection = src.crossSection;
706 this.tabHeight = src.tabHeight;
707 this.tabLength = src.tabLength;
708 this.tabRelativePosition = src.tabRelativePosition;
709 this.tabShift = src.tabShift;
711 return super.copyFrom(c);