1 package net.sf.openrocket.rocketcomponent;
3 import static java.lang.Math.*;
4 import static net.sf.openrocket.util.Chars.*;
5 import static net.sf.openrocket.util.MathUtil.*;
7 import java.util.Collection;
9 import net.sf.openrocket.util.Coordinate;
10 import net.sf.openrocket.util.MathUtil;
13 public class Transition extends SymmetricComponent {
14 private static final double CLIP_PRECISION = 0.0001;
18 private double shapeParameter;
19 private boolean clipped; // Not to be read - use isClipped(), which may be overriden
21 private double radius1, radius2;
22 private boolean autoRadius1, autoRadius2; // Whether the start radius is automatic
25 private double foreShoulderRadius;
26 private double foreShoulderThickness;
27 private double foreShoulderLength;
28 private boolean foreShoulderCapped;
29 private double aftShoulderRadius;
30 private double aftShoulderThickness;
31 private double aftShoulderLength;
32 private boolean aftShoulderCapped;
35 // Used to cache the clip length
36 private double clipLength=-1;
41 this.radius1 = DEFAULT_RADIUS;
42 this.radius2 = DEFAULT_RADIUS;
43 this.length = DEFAULT_RADIUS * 3;
44 this.autoRadius1 = true;
45 this.autoRadius2 = true;
47 this.type = Shape.CONICAL;
48 this.shapeParameter = 0;
55 //////// Fore radius ////////
59 public double getForeRadius() {
60 if (isForeRadiusAutomatic()) {
61 // Get the automatic radius from the front
63 SymmetricComponent c = this.getPreviousSymmetricComponent();
65 r = c.getFrontAutoRadius();
74 public void setForeRadius(double radius) {
75 if ((this.radius1 == radius) && (autoRadius1 == false))
78 this.autoRadius1 = false;
79 this.radius1 = Math.max(radius,0);
81 if (this.thickness > this.radius1 && this.thickness > this.radius2)
82 this.thickness = Math.max(this.radius1, this.radius2);
83 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
87 public boolean isForeRadiusAutomatic() {
91 public void setForeRadiusAutomatic(boolean auto) {
92 if (autoRadius1 == auto)
96 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
100 //////// Aft radius /////////
103 public double getAftRadius() {
104 if (isAftRadiusAutomatic()) {
105 // Return the auto radius from the rear
107 SymmetricComponent c = this.getNextSymmetricComponent();
109 r = c.getRearAutoRadius();
120 public void setAftRadius(double radius) {
121 if ((this.radius2 == radius) && (autoRadius2 == false))
124 this.autoRadius2 = false;
125 this.radius2 = Math.max(radius,0);
127 if (this.thickness > this.radius1 && this.thickness > this.radius2)
128 this.thickness = Math.max(this.radius1, this.radius2);
129 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
133 public boolean isAftRadiusAutomatic() {
137 public void setAftRadiusAutomatic(boolean auto) {
138 if (autoRadius2 == auto)
142 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
147 //// Radius automatics
150 protected double getFrontAutoRadius() {
151 if (isAftRadiusAutomatic())
153 return getAftRadius();
158 protected double getRearAutoRadius() {
159 if (isForeRadiusAutomatic())
161 return getForeRadius();
167 //////// Type & shape /////////
169 public Shape getType() {
173 public void setType(Shape type) {
174 if (this.type == type)
177 this.clipped = type.isClippable();
178 this.shapeParameter = type.defaultParameter();
179 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
182 public double getShapeParameter() {
183 return shapeParameter;
186 public void setShapeParameter(double n) {
187 if (shapeParameter == n)
189 this.shapeParameter = MathUtil.clamp(n, type.minParameter(), type.maxParameter());
190 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
193 public boolean isClipped() {
194 if (!type.isClippable())
199 public void setClipped(boolean c) {
203 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
206 public boolean isClippedEnabled() {
207 return type.isClippable();
210 public double getShapeParameterMin() {
211 return type.minParameter();
214 public double getShapeParameterMax() {
215 return type.maxParameter();
219 //////// Shoulders ////////
221 public double getForeShoulderRadius() {
222 return foreShoulderRadius;
225 public void setForeShoulderRadius(double foreShoulderRadius) {
226 if (MathUtil.equals(this.foreShoulderRadius, foreShoulderRadius))
228 this.foreShoulderRadius = foreShoulderRadius;
229 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
232 public double getForeShoulderThickness() {
233 return foreShoulderThickness;
236 public void setForeShoulderThickness(double foreShoulderThickness) {
237 if (MathUtil.equals(this.foreShoulderThickness, foreShoulderThickness))
239 this.foreShoulderThickness = foreShoulderThickness;
240 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
243 public double getForeShoulderLength() {
244 return foreShoulderLength;
247 public void setForeShoulderLength(double foreShoulderLength) {
248 if (MathUtil.equals(this.foreShoulderLength, foreShoulderLength))
250 this.foreShoulderLength = foreShoulderLength;
251 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
254 public boolean isForeShoulderCapped() {
255 return foreShoulderCapped;
258 public void setForeShoulderCapped(boolean capped) {
259 if (this.foreShoulderCapped == capped)
261 this.foreShoulderCapped = capped;
262 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
268 public double getAftShoulderRadius() {
269 return aftShoulderRadius;
272 public void setAftShoulderRadius(double aftShoulderRadius) {
273 if (MathUtil.equals(this.aftShoulderRadius, aftShoulderRadius))
275 this.aftShoulderRadius = aftShoulderRadius;
276 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
279 public double getAftShoulderThickness() {
280 return aftShoulderThickness;
283 public void setAftShoulderThickness(double aftShoulderThickness) {
284 if (MathUtil.equals(this.aftShoulderThickness, aftShoulderThickness))
286 this.aftShoulderThickness = aftShoulderThickness;
287 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
290 public double getAftShoulderLength() {
291 return aftShoulderLength;
294 public void setAftShoulderLength(double aftShoulderLength) {
295 if (MathUtil.equals(this.aftShoulderLength, aftShoulderLength))
297 this.aftShoulderLength = aftShoulderLength;
298 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
301 public boolean isAftShoulderCapped() {
302 return aftShoulderCapped;
305 public void setAftShoulderCapped(boolean capped) {
306 if (this.aftShoulderCapped == capped)
308 this.aftShoulderCapped = capped;
309 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
315 /////////// Shape implementations ////////////
320 * Return the radius at point x of the transition.
323 public double getRadius(double x) {
327 double r1=getForeRadius();
328 double r2=getAftRadius();
341 // Check clip calculation
343 calculateClip(r1,r2);
344 return type.getRadius(clipLength+x, r2, clipLength+length, shapeParameter);
347 return r1 + type.getRadius(x, r2-r1, length, shapeParameter);
352 * Numerically solve clipLength from the equation
353 * r1 == type.getRadius(clipLength,r2,clipLength+length)
354 * using a binary search. It assumes getRadius() to be monotonically increasing.
356 private void calculateClip(double r1, double r2) {
357 double min=0, max=length;
376 // getR(min,min+length,r2) - r1 < 0
377 // getR(max,max+length,r2) - r1 > 0
380 while (type.getRadius(max, r2, max+length, shapeParameter) - r1 < 0) {
389 clipLength = (min+max)/2;
390 if ((max-min)<CLIP_PRECISION)
392 double val = type.getRadius(clipLength, r2, clipLength+length, shapeParameter);
403 public double getInnerRadius(double x) {
404 return Math.max(getRadius(x)-thickness,0);
410 public Collection<Coordinate> getComponentBounds() {
411 Collection<Coordinate> bounds = super.getComponentBounds();
412 if (foreShoulderLength > 0.001)
413 addBound(bounds, -foreShoulderLength, foreShoulderRadius);
414 if (aftShoulderLength > 0.001)
415 addBound(bounds, getLength() + aftShoulderLength, aftShoulderRadius);
420 public double getComponentMass() {
421 double mass = super.getComponentMass();
422 if (getForeShoulderLength() > 0.001) {
423 final double or = getForeShoulderRadius();
424 final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
425 mass += ringMass(or, ir, getForeShoulderLength(), getMaterial().getDensity());
427 if (isForeShoulderCapped()) {
428 final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
429 mass += ringMass(ir, 0, getForeShoulderThickness(), getMaterial().getDensity());
432 if (getAftShoulderLength() > 0.001) {
433 final double or = getAftShoulderRadius();
434 final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
435 mass += ringMass(or, ir, getAftShoulderLength(), getMaterial().getDensity());
437 if (isAftShoulderCapped()) {
438 final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
439 mass += ringMass(ir, 0, getAftShoulderThickness(), getMaterial().getDensity());
446 public Coordinate getComponentCG() {
447 Coordinate cg = super.getComponentCG();
448 if (getForeShoulderLength() > 0.001) {
449 final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
450 cg = cg.average(ringCG(getForeShoulderRadius(), ir, -getForeShoulderLength(), 0,
451 getMaterial().getDensity()));
453 if (isForeShoulderCapped()) {
454 final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
455 cg = cg.average(ringCG(ir, 0, -getForeShoulderLength(),
456 getForeShoulderThickness()-getForeShoulderLength(),
457 getMaterial().getDensity()));
460 if (getAftShoulderLength() > 0.001) {
461 final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
462 cg = cg.average(ringCG(getAftShoulderRadius(), ir, getLength(),
463 getLength()+getAftShoulderLength(), getMaterial().getDensity()));
465 if (isAftShoulderCapped()) {
466 final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
467 cg = cg.average(ringCG(ir, 0,
468 getLength()+getAftShoulderLength()-getAftShoulderThickness(),
469 getLength()+getAftShoulderLength(), getMaterial().getDensity()));
476 * The moments of inertia are not explicitly corrected for the shoulders.
477 * However, since the mass is corrected, the inertia is automatically corrected
478 * to very nearly the correct value.
484 * Returns the name of the component ("Transition").
487 public String getComponentName() {
492 protected void componentChanged(ComponentChangeEvent e) {
493 super.componentChanged(e);
500 * An enumeration listing the possible shapes of transitions.
502 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
504 public static enum Shape {
510 "A conical nose cone has a profile of a triangle.",
511 "A conical transition has straight sides.") {
513 public double getRadius(double x, double radius, double length, double param) {
517 return radius*x/length;
522 * Ogive shape. The shape parameter is the portion of an extended tangent ogive
523 * that will be used. That is, for param==1 a tangent ogive will be produced, and
524 * for smaller values the shape straightens out into a cone at param==0.
527 "An ogive nose cone has a profile that is a segment of a circle. " +
528 "The shape parameter value 1 produces a <b>tangent ogive</b>, which has " +
529 "a smooth transition to the body tube, values less than 1 produce "+
530 "<b>secant ogives</b>.",
531 "An ogive transition has a profile that is a segment of a circle. " +
532 "The shape parameter value 1 produces a <b>tangent ogive</b>, which has " +
533 "a smooth transition to the body tube at the aft end, values less than 1 " +
534 "produce <b>secant ogives</b>.") {
536 public boolean usesParameter() {
537 return true; // Range 0...1 is default
540 public double defaultParameter() {
541 return 1.0; // Tangent ogive by default
544 public double getRadius(double x, double radius, double length, double param) {
551 // Impossible to calculate ogive for length < radius, scale instead
552 // TODO: LOW: secant ogive could be calculated lower
553 if (length < radius) {
554 x = x * radius / length;
559 return CONICAL.getRadius(x, radius, length, param);
561 // Radius of circle is:
562 double R = sqrt((pow2(length)+pow2(radius)) *
563 (pow2((2-param)*length) + pow2(param*radius))/(4*pow2(param*radius)));
564 double L = length/param;
565 // double R = (radius + length*length/(radius*param*param))/2;
566 double y0 = sqrt(R*R - L*L);
567 return sqrt(R*R - (L-x)*(L-x)) - y0;
574 ELLIPSOID("Ellipsoid",
575 "An ellipsoidal nose cone has a profile of a half-ellipse "+
576 "with major axes of lengths 2×<i>Length</i> and <i>Diameter</i>.",
577 "An ellipsoidal transition has a profile of a half-ellipse "+
578 "with major axes of lengths 2×<i>Length</i> and <i>Diameter</i>. If the "+
579 "transition is not clipped, then the profile is extended at the center by the "+
580 "corresponding radius.",true) {
582 public double getRadius(double x, double radius, double length, double param) {
587 return sqrt(2*radius*x-x*x); // radius/length * sphere
591 POWER("Power series",
592 "A power series nose cone has a profile of "+
593 "<i>Radius</i> × (<i>x</i> / <i>Length</i>)" +
594 "<sup><i>k</i></sup> "+
595 "where <i>k</i> is the shape parameter. For <i>k</i>=0.5 this is a "+
596 "<b>" + FRAC12 +"-power</b> or <b>parabolic</b> nose cone, for <i>k</i>=0.75 a "+
597 "<b>" + FRAC34 +"-power</b>, and for <i>k</i>=1 a <b>conical</b> nose cone.",
598 "A power series transition has a profile of "+
599 "<i>Radius</i> × (<i>x</i> / <i>Length</i>)" +
600 "<sup><i>k</i></sup> "+
601 "where <i>k</i> is the shape parameter. For <i>k</i>=0.5 the transition is "+
602 "<b>" + FRAC12 + "-power</b> or <b>parabolic</b>, for <i>k</i>=0.75 a " +
603 "<b>" + FRAC34 + "-power</b>, and for <i>k</i>=1 <b>conical</b>.",true) {
605 public boolean usesParameter() { // Range 0...1
609 public double defaultParameter() {
613 public double getRadius(double x, double radius, double length, double param) {
619 if (param<=0.00001) {
625 return radius*Math.pow(x/length, param);
630 PARABOLIC("Parabolic series",
631 "A parabolic series nose cone has a profile of a parabola. The shape "+
632 "parameter defines the segment of the parabola to utilize. The shape " +
633 "parameter 1.0 produces a <b>full parabola</b> which is tangent to the body " +
634 "tube, 0.75 produces a <b>3/4 parabola</b>, 0.5 procudes a " +
635 "<b>1/2 parabola</b> and 0 produces a <b>conical</b> nose cone.",
636 "A parabolic series transition has a profile of a parabola. The shape "+
637 "parameter defines the segment of the parabola to utilize. The shape " +
638 "parameter 1.0 produces a <b>full parabola</b> which is tangent to the body " +
639 "tube at the aft end, 0.75 produces a <b>3/4 parabola</b>, 0.5 procudes a " +
640 "<b>1/2 parabola</b> and 0 produces a <b>conical</b> transition.") {
642 // In principle a parabolic transition is clippable, but the difference is
646 public boolean usesParameter() { // Range 0...1
650 public double defaultParameter() {
654 public double getRadius(double x, double radius, double length, double param) {
661 return radius * ((2*x/length - param*pow2(x/length))/(2-param));
667 HAACK("Haack series",
668 "The Haack series nose cones are designed to minimize drag. The shape parameter " +
669 "0 produces an <b>LD-Haack</b> or <b>Von Karman</b> nose cone, which minimizes " +
670 "drag for fixed length and diameter, while a value of 0.333 produces an " +
671 "<b>LV-Haack</b> nose cone, which minimizes drag for fixed length and volume.",
672 "The Haack series <i>nose cones</i> are designed to minimize drag. " +
673 "These transition shapes are their equivalents, but do not necessarily produce " +
674 "optimal drag for transitions. " +
675 "The shape parameter 0 produces an <b>LD-Haack</b> or <b>Von Karman</b> shape, " +
676 "while a value of 0.333 produces an <b>LV-Haack</b> shape.",true) {
678 public boolean usesParameter() {
682 public double maxParameter() {
683 return 1.0/3.0; // Range 0...1/3
686 public double getRadius(double x, double radius, double length, double param) {
693 double theta = Math.acos(1-2*x/length);
695 return radius*sqrt((theta-sin(2*theta)/2)/Math.PI);
697 return radius*sqrt((theta-sin(2*theta)/2+param*pow3(sin(theta)))/Math.PI);
701 // POLYNOMIAL("Smooth polynomial",
702 // "A polynomial is fitted such that the nose cone profile is horizontal "+
703 // "at the aft end of the transition. The angle at the tip is defined by "+
704 // "the shape parameter.",
705 // "A polynomial is fitted such that the transition profile is horizontal "+
706 // "at the aft end of the transition. The angle at the fore end is defined "+
707 // "by the shape parameter.") {
709 // public boolean usesParameter() {
713 // public double maxParameter() {
714 // return 3.0; // Range 0...3
717 // public double defaultParameter() {
720 // public double getRadius(double x, double radius, double length, double param) {
722 // assert x <= length;
723 // assert radius >= 0;
724 // assert param >= 0;
725 // assert param <= 3;
726 // // p(x) = (k-2)x^3 + (3-2k)x^2 + k*x
728 // return radius*((((param-2)*x + (3-2*param))*x + param)*x);
733 // Privete fields of the shapes
734 private final String name;
735 private final String transitionDesc;
736 private final String noseconeDesc;
737 private final boolean canClip;
739 // Non-clippable constructor
740 Shape(String name, String noseconeDesc, String transitionDesc) {
741 this(name,noseconeDesc,transitionDesc,false);
744 // Clippable constructor
745 Shape(String name, String noseconeDesc, String transitionDesc, boolean canClip) {
747 this.canClip = canClip;
748 this.noseconeDesc = noseconeDesc;
749 this.transitionDesc = transitionDesc;
754 * Return the name of the transition shape name.
756 public String getName() {
761 * Get a description of the Transition shape.
763 public String getTransitionDescription() {
764 return transitionDesc;
768 * Get a description of the NoseCone shape.
770 public String getNoseConeDescription() {
775 * Check whether the shape differs in clipped mode. The clipping should be
776 * enabled by default if possible.
778 public boolean isClippable() {
783 * Return whether the shape uses the shape parameter. (Default false.)
785 public boolean usesParameter() {
790 * Return the minimum value of the shape parameter. (Default 0.)
792 public double minParameter() {
797 * Return the maximum value of the shape parameter. (Default 1.)
799 public double maxParameter() {
804 * Return the default value of the shape parameter. (Default 0.)
806 public double defaultParameter() {
811 * Calculate the basic radius of a transition with the given radius, length and
812 * shape parameter at the point x from the tip of the component. It is assumed
813 * that the fore radius if zero and the aft radius is <code>radius >= 0</code>.
814 * Boattails are achieved by reversing the component.
816 * @param x Position from the tip of the component.
817 * @param radius Aft end radius >= 0.
818 * @param length Length of the transition >= 0.
819 * @param param Valid shape parameter.
820 * @return The basic radius at the given position.
822 public abstract double getRadius(double x, double radius, double length, double param);
826 * Returns the name of the shape (same as getName()).
829 public String toString() {