fd52699b4052967e52b3ce7cf2e142a4632f65e9
[debian/openrocket] / core / src / net / sf / openrocket / rocketcomponent / Transition.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import static java.lang.Math.sin;
4 import static net.sf.openrocket.util.MathUtil.*;
5
6 import java.util.Collection;
7
8 import net.sf.openrocket.l10n.Translator;
9 import net.sf.openrocket.preset.ComponentPreset;
10 import net.sf.openrocket.preset.ComponentPreset.Type;
11 import net.sf.openrocket.startup.Application;
12 import net.sf.openrocket.util.Coordinate;
13 import net.sf.openrocket.util.MathUtil;
14
15
16 public class Transition extends SymmetricComponent {
17         private static final Translator trans = Application.getTranslator();
18         private static final double CLIP_PRECISION = 0.0001;
19         
20
21         private Shape type;
22         private double shapeParameter;
23         private boolean clipped; // Not to be read - use isClipped(), which may be overriden
24         
25         private double radius1, radius2;
26         private boolean autoRadius1, autoRadius2; // Whether the start radius is automatic
27                         
28
29         private double foreShoulderRadius;
30         private double foreShoulderThickness;
31         private double foreShoulderLength;
32         private boolean foreShoulderCapped;
33         private double aftShoulderRadius;
34         private double aftShoulderThickness;
35         private double aftShoulderLength;
36         private boolean aftShoulderCapped;
37         
38
39         // Used to cache the clip length
40         private double clipLength = -1;
41         
42         public Transition() {
43                 super();
44                 
45                 this.radius1 = DEFAULT_RADIUS;
46                 this.radius2 = DEFAULT_RADIUS;
47                 this.length = DEFAULT_RADIUS * 3;
48                 this.autoRadius1 = true;
49                 this.autoRadius2 = true;
50                 
51                 this.type = Shape.CONICAL;
52                 this.shapeParameter = 0;
53                 this.clipped = true;
54         }
55         
56         ////////  Length  ////////
57         @Override
58         public void setLength( double length ) {
59                 if ( this.length == length ) {
60                         return;
61                 }
62                 // Need to clearPreset when length changes.
63                 clearPreset();
64                 super.setLength( length );
65         }
66
67
68         ////////  Fore radius  ////////
69         
70
71         @Override
72         public double getForeRadius() {
73                 if (isForeRadiusAutomatic()) {
74                         // Get the automatic radius from the front
75                         double r = -1;
76                         SymmetricComponent c = this.getPreviousSymmetricComponent();
77                         if (c != null) {
78                                 r = c.getFrontAutoRadius();
79                         }
80                         if (r < 0)
81                                 r = DEFAULT_RADIUS;
82                         return r;
83                 }
84                 return radius1;
85         }
86         
87         public void setForeRadius(double radius) {
88                 if ((this.radius1 == radius) && (autoRadius1 == false))
89                         return;
90                 
91                 this.autoRadius1 = false;
92                 this.radius1 = Math.max(radius, 0);
93                 
94                 if (this.thickness > this.radius1 && this.thickness > this.radius2)
95                         this.thickness = Math.max(this.radius1, this.radius2);
96                 
97                 clearPreset();
98                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
99         }
100         
101         @Override
102         public boolean isForeRadiusAutomatic() {
103                 return autoRadius1;
104         }
105         
106         public void setForeRadiusAutomatic(boolean auto) {
107                 if (autoRadius1 == auto)
108                         return;
109                 
110                 autoRadius1 = auto;
111
112                 clearPreset();
113                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
114         }
115         
116         
117         ////////  Aft radius  /////////
118         
119         @Override
120         public double getAftRadius() {
121                 if (isAftRadiusAutomatic()) {
122                         // Return the auto radius from the rear
123                         double r = -1;
124                         SymmetricComponent c = this.getNextSymmetricComponent();
125                         if (c != null) {
126                                 r = c.getRearAutoRadius();
127                         }
128                         if (r < 0)
129                                 r = DEFAULT_RADIUS;
130                         return r;
131                 }
132                 return radius2;
133         }
134         
135         
136
137         public void setAftRadius(double radius) {
138                 if ((this.radius2 == radius) && (autoRadius2 == false))
139                         return;
140                 
141                 this.autoRadius2 = false;
142                 this.radius2 = Math.max(radius, 0);
143                 
144                 if (this.thickness > this.radius1 && this.thickness > this.radius2)
145                         this.thickness = Math.max(this.radius1, this.radius2);
146
147                 clearPreset();
148                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
149         }
150         
151         @Override
152         public boolean isAftRadiusAutomatic() {
153                 return autoRadius2;
154         }
155         
156         public void setAftRadiusAutomatic(boolean auto) {
157                 if (autoRadius2 == auto)
158                         return;
159                 
160                 autoRadius2 = auto;
161
162                 clearPreset();
163                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
164         }
165         
166         
167
168         //// Radius automatics
169         
170         @Override
171         protected double getFrontAutoRadius() {
172                 if (isAftRadiusAutomatic())
173                         return -1;
174                 return getAftRadius();
175         }
176         
177         
178         @Override
179         protected double getRearAutoRadius() {
180                 if (isForeRadiusAutomatic())
181                         return -1;
182                 return getForeRadius();
183         }
184         
185         
186
187
188         ////////  Type & shape  /////////
189         
190         public Shape getType() {
191                 return type;
192         }
193         
194         public void setType(Shape type) {
195                 if (type == null) {
196                         throw new IllegalArgumentException("setType called with null argument");
197                 }
198                 if (this.type == type)
199                         return;
200                 this.type = type;
201                 this.clipped = type.isClippable();
202                 this.shapeParameter = type.defaultParameter();
203                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
204         }
205         
206         public double getShapeParameter() {
207                 return shapeParameter;
208         }
209         
210         public void setShapeParameter(double n) {
211                 if (shapeParameter == n)
212                         return;
213                 this.shapeParameter = MathUtil.clamp(n, type.minParameter(), type.maxParameter());
214                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
215         }
216         
217         public boolean isClipped() {
218                 if (!type.isClippable())
219                         return false;
220                 return clipped;
221         }
222         
223         public void setClipped(boolean c) {
224                 if (clipped == c)
225                         return;
226                 clipped = c;
227                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
228         }
229         
230         public boolean isClippedEnabled() {
231                 return type.isClippable();
232         }
233         
234         public double getShapeParameterMin() {
235                 return type.minParameter();
236         }
237         
238         public double getShapeParameterMax() {
239                 return type.maxParameter();
240         }
241         
242         
243         ////////  Shoulders  ////////
244         
245         public double getForeShoulderRadius() {
246                 return foreShoulderRadius;
247         }
248         
249         public void setForeShoulderRadius(double foreShoulderRadius) {
250                 if (MathUtil.equals(this.foreShoulderRadius, foreShoulderRadius))
251                         return;
252                 this.foreShoulderRadius = foreShoulderRadius;
253                 clearPreset();
254                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
255         }
256         
257         public double getForeShoulderThickness() {
258                 return foreShoulderThickness;
259         }
260         
261         public void setForeShoulderThickness(double foreShoulderThickness) {
262                 if (MathUtil.equals(this.foreShoulderThickness, foreShoulderThickness))
263                         return;
264                 this.foreShoulderThickness = foreShoulderThickness;
265                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
266         }
267         
268         public double getForeShoulderLength() {
269                 return foreShoulderLength;
270         }
271         
272         public void setForeShoulderLength(double foreShoulderLength) {
273                 if (MathUtil.equals(this.foreShoulderLength, foreShoulderLength))
274                         return;
275                 this.foreShoulderLength = foreShoulderLength;
276                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
277         }
278         
279         public boolean isForeShoulderCapped() {
280                 return foreShoulderCapped;
281         }
282         
283         public void setForeShoulderCapped(boolean capped) {
284                 if (this.foreShoulderCapped == capped)
285                         return;
286                 this.foreShoulderCapped = capped;
287                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
288         }
289         
290         
291
292
293         public double getAftShoulderRadius() {
294                 return aftShoulderRadius;
295         }
296         
297         public void setAftShoulderRadius(double aftShoulderRadius) {
298                 if (MathUtil.equals(this.aftShoulderRadius, aftShoulderRadius))
299                         return;
300                 this.aftShoulderRadius = aftShoulderRadius;
301                 clearPreset();
302                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
303         }
304         
305         public double getAftShoulderThickness() {
306                 return aftShoulderThickness;
307         }
308         
309         public void setAftShoulderThickness(double aftShoulderThickness) {
310                 if (MathUtil.equals(this.aftShoulderThickness, aftShoulderThickness))
311                         return;
312                 this.aftShoulderThickness = aftShoulderThickness;
313                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
314         }
315         
316         public double getAftShoulderLength() {
317                 return aftShoulderLength;
318         }
319         
320         public void setAftShoulderLength(double aftShoulderLength) {
321                 if (MathUtil.equals(this.aftShoulderLength, aftShoulderLength))
322                         return;
323                 this.aftShoulderLength = aftShoulderLength;
324                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
325         }
326         
327         public boolean isAftShoulderCapped() {
328                 return aftShoulderCapped;
329         }
330         
331         public void setAftShoulderCapped(boolean capped) {
332                 if (this.aftShoulderCapped == capped)
333                         return;
334                 this.aftShoulderCapped = capped;
335                 fireComponentChangeEvent(ComponentChangeEvent.MASS_CHANGE);
336         }
337         
338         
339
340
341         ///////////   Shape implementations   ////////////
342         
343
344
345         /**
346          * Return the radius at point x of the transition.
347          */
348         @Override
349         public double getRadius(double x) {
350                 if (x < 0 || x > length)
351                         return 0;
352                 
353                 double r1 = getForeRadius();
354                 double r2 = getAftRadius();
355                 
356                 if (r1 == r2)
357                         return r1;
358                 
359                 if (r1 > r2) {
360                         x = length - x;
361                         double tmp = r1;
362                         r1 = r2;
363                         r2 = tmp;
364                 }
365                 
366                 if (isClipped()) {
367                         // Check clip calculation
368                         if (clipLength < 0)
369                                 calculateClip(r1, r2);
370                         return type.getRadius(clipLength + x, r2, clipLength + length, shapeParameter);
371                 } else {
372                         // Not clipped
373                         return r1 + type.getRadius(x, r2 - r1, length, shapeParameter);
374                 }
375         }
376         
377         /**
378          * Numerically solve clipLength from the equation
379          *     r1 == type.getRadius(clipLength,r2,clipLength+length)
380          * using a binary search.  It assumes getOuterRadius() to be monotonically increasing.
381          */
382         private void calculateClip(double r1, double r2) {
383                 double min = 0, max = length;
384                 
385                 if (r1 >= r2) {
386                         double tmp = r1;
387                         r1 = r2;
388                         r2 = tmp;
389                 }
390                 
391                 if (r1 == 0) {
392                         clipLength = 0;
393                         return;
394                 }
395                 
396                 if (length <= 0) {
397                         clipLength = 0;
398                         return;
399                 }
400                 
401                 // Required:
402                 //    getR(min,min+length,r2) - r1 < 0
403                 //    getR(max,max+length,r2) - r1 > 0
404                 
405                 int n = 0;
406                 while (type.getRadius(max, r2, max + length, shapeParameter) - r1 < 0) {
407                         min = max;
408                         max *= 2;
409                         n++;
410                         if (n > 10)
411                                 break;
412                 }
413                 
414                 while (true) {
415                         clipLength = (min + max) / 2;
416                         if ((max - min) < CLIP_PRECISION)
417                                 return;
418                         double val = type.getRadius(clipLength, r2, clipLength + length, shapeParameter);
419                         if (val - r1 > 0) {
420                                 max = clipLength;
421                         } else {
422                                 min = clipLength;
423                         }
424                 }
425         }
426         
427         
428         @Override
429         public double getInnerRadius(double x) {
430                 return Math.max(getRadius(x) - thickness, 0);
431         }
432         
433         
434
435         @Override
436         public Collection<Coordinate> getComponentBounds() {
437                 Collection<Coordinate> bounds = super.getComponentBounds();
438                 if (foreShoulderLength > 0.001)
439                         addBound(bounds, -foreShoulderLength, foreShoulderRadius);
440                 if (aftShoulderLength > 0.001)
441                         addBound(bounds, getLength() + aftShoulderLength, aftShoulderRadius);
442                 return bounds;
443         }
444         
445         @Override
446         public double getComponentMass() {
447                 double mass = super.getComponentMass();
448                 if (getForeShoulderLength() > 0.001) {
449                         final double or = getForeShoulderRadius();
450                         final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
451                         mass += ringMass(or, ir, getForeShoulderLength(), getMaterial().getDensity());
452                 }
453                 if (isForeShoulderCapped()) {
454                         final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
455                         mass += ringMass(ir, 0, getForeShoulderThickness(), getMaterial().getDensity());
456                 }
457                 
458                 if (getAftShoulderLength() > 0.001) {
459                         final double or = getAftShoulderRadius();
460                         final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
461                         mass += ringMass(or, ir, getAftShoulderLength(), getMaterial().getDensity());
462                 }
463                 if (isAftShoulderCapped()) {
464                         final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
465                         mass += ringMass(ir, 0, getAftShoulderThickness(), getMaterial().getDensity());
466                 }
467                 
468                 return mass;
469         }
470         
471         @Override
472         public Coordinate getComponentCG() {
473                 Coordinate cg = super.getComponentCG();
474                 if (getForeShoulderLength() > 0.001) {
475                         final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
476                         cg = cg.average(ringCG(getForeShoulderRadius(), ir, -getForeShoulderLength(), 0,
477                                         getMaterial().getDensity()));
478                 }
479                 if (isForeShoulderCapped()) {
480                         final double ir = Math.max(getForeShoulderRadius() - getForeShoulderThickness(), 0);
481                         cg = cg.average(ringCG(ir, 0, -getForeShoulderLength(),
482                                         getForeShoulderThickness() - getForeShoulderLength(),
483                                         getMaterial().getDensity()));
484                 }
485                 
486                 if (getAftShoulderLength() > 0.001) {
487                         final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
488                         cg = cg.average(ringCG(getAftShoulderRadius(), ir, getLength(),
489                                         getLength() + getAftShoulderLength(), getMaterial().getDensity()));
490                 }
491                 if (isAftShoulderCapped()) {
492                         final double ir = Math.max(getAftShoulderRadius() - getAftShoulderThickness(), 0);
493                         cg = cg.average(ringCG(ir, 0,
494                                         getLength() + getAftShoulderLength() - getAftShoulderThickness(),
495                                         getLength() + getAftShoulderLength(), getMaterial().getDensity()));
496                 }
497                 return cg;
498         }
499         
500         
501         /*
502          * The moments of inertia are not explicitly corrected for the shoulders.
503          * However, since the mass is corrected, the inertia is automatically corrected
504          * to very nearly the correct value.
505          */
506
507
508
509         /**
510          * Returns the name of the component ("Transition").
511          */
512         @Override
513         public String getComponentName() {
514                 //// Transition
515                 return trans.get("Transition.Transition");
516         }
517         
518         @Override
519         protected void componentChanged(ComponentChangeEvent e) {
520                 super.componentChanged(e);
521                 clipLength = -1;
522         }
523         
524         /**
525          * Check whether the given type can be added to this component.  Transitions allow any
526          * InternalComponents to be added.
527          *
528          * @param ctype  The RocketComponent class type to add.
529          * @return      Whether such a component can be added.
530          */
531         @Override
532         public boolean isCompatible(Class<? extends RocketComponent> ctype) {
533                 if (InternalComponent.class.isAssignableFrom(ctype))
534                         return true;
535                 return false;
536         }
537         
538         @Override
539         public Type getPresetType() {
540                 return ComponentPreset.Type.TRANSITION;
541         }
542
543
544         @Override
545         protected void loadFromPreset(ComponentPreset preset) {
546
547                 if ( preset.has(ComponentPreset.SHAPE) ) {
548                         Shape s = preset.get(ComponentPreset.SHAPE);
549                         this.setType(s);
550                 }
551                 if ( preset.has(ComponentPreset.AFT_OUTER_DIAMETER) )  {
552                         double outerDiameter = preset.get(ComponentPreset.AFT_OUTER_DIAMETER);
553                         this.setAftRadiusAutomatic(false);
554                         this.setAftRadius(outerDiameter/2.0);
555                 }
556                 if ( preset.has(ComponentPreset.AFT_SHOULDER_LENGTH) ) {
557                         double d = preset.get(ComponentPreset.AFT_SHOULDER_LENGTH);
558                         this.setAftShoulderLength(d);
559                 }
560                 if ( preset.has(ComponentPreset.AFT_SHOULDER_DIAMETER) ) {
561                         double d = preset.get(ComponentPreset.AFT_SHOULDER_DIAMETER);
562                         this.setAftShoulderRadius(d/2.0);
563                 }
564                 if ( preset.has(ComponentPreset.FORE_OUTER_DIAMETER) )  {
565                         double outerDiameter = preset.get(ComponentPreset.FORE_OUTER_DIAMETER);
566                         this.setForeRadiusAutomatic(false);
567                         this.setForeRadius(outerDiameter/2.0);
568                 }
569                 if ( preset.has(ComponentPreset.FORE_SHOULDER_LENGTH) ) {
570                         double d = preset.get(ComponentPreset.FORE_SHOULDER_LENGTH);
571                         this.setForeShoulderLength(d);
572                 }
573                 if ( preset.has(ComponentPreset.FORE_SHOULDER_DIAMETER) ) {
574                         double d = preset.get(ComponentPreset.FORE_SHOULDER_DIAMETER);
575                         this.setForeShoulderRadius(d/2.0);
576                 }
577
578                 super.loadFromPreset(preset);
579
580                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
581
582         }
583
584         /**
585          * An enumeration listing the possible shapes of transitions.
586          *
587          * @author Sampo Niskanen <sampo.niskanen@iki.fi>
588          */
589         public static enum Shape {
590                 
591                 /**
592                  * Conical shape.
593                  */
594                 //// Conical
595                 CONICAL(trans.get("Shape.Conical"),
596                                 //// A conical nose cone has a profile of a triangle.
597                                 trans.get("Shape.Conical.desc1"),
598                                 //// A conical transition has straight sides.
599                                 trans.get("Shape.Conical.desc2")) {
600                         @Override
601                         public double getRadius(double x, double radius, double length, double param) {
602                                 assert x >= 0;
603                                 assert x <= length;
604                                 assert radius >= 0;
605                                 return radius * x / length;
606                         }
607                 },
608                 
609                 /**
610                  * Ogive shape.  The shape parameter is the portion of an extended tangent ogive
611                  * that will be used.  That is, for param==1 a tangent ogive will be produced, and
612                  * for smaller values the shape straightens out into a cone at param==0.
613                  */
614                 //// Ogive
615                 OGIVE(trans.get("Shape.Ogive"),
616                                 //// An ogive nose cone has a profile that is a segment of a circle.  The shape parameter value 1 produces a <b>tangent ogive</b>, which has a smooth transition to the body tube, values less than 1 produce <b>secant ogives</b>.
617                                 trans.get("Shape.Ogive.desc1"),
618                                 //// An ogive transition has a profile that is a segment of a circle.   The shape parameter value 1 produces a <b>tangent ogive</b>, which has a smooth transition to the body tube at the aft end, values less than 1 produce <b>secant ogives</b>.
619                                 trans.get("Shape.Ogive.desc2")) {
620                         @Override
621                         public boolean usesParameter() {
622                                 return true; // Range 0...1 is default
623                         }
624                         
625                         @Override
626                         public double defaultParameter() {
627                                 return 1.0; // Tangent ogive by default
628                         }
629                         
630                         @Override
631                         public double getRadius(double x, double radius, double length, double param) {
632                                 assert x >= 0;
633                                 assert x <= length;
634                                 assert radius >= 0;
635                                 assert param >= 0;
636                                 assert param <= 1;
637                                 
638                                 // Impossible to calculate ogive for length < radius, scale instead
639                                 // TODO: LOW: secant ogive could be calculated lower
640                                 if (length < radius) {
641                                         x = x * radius / length;
642                                         length = radius;
643                                 }
644                                 
645                                 if (param < 0.001)
646                                         return CONICAL.getRadius(x, radius, length, param);
647                                 
648                                 // Radius of circle is:
649                                 double R = MathUtil.safeSqrt((pow2(length) + pow2(radius)) *
650                                                 (pow2((2 - param) * length) + pow2(param * radius)) / (4 * pow2(param * radius)));
651                                 double L = length / param;
652                                 //                              double R = (radius + length*length/(radius*param*param))/2;
653                                 double y0 = MathUtil.safeSqrt(R * R - L * L);
654                                 return MathUtil.safeSqrt(R * R - (L - x) * (L - x)) - y0;
655                         }
656                 },
657                 
658                 /**
659                  * Ellipsoidal shape.
660                  */
661                 //// Ellipsoid
662                 ELLIPSOID(trans.get("Shape.Ellipsoid"),
663                                 //// An ellipsoidal nose cone has a profile of a half-ellipse with major axes of lengths 2&times;<i>Length</i> and <i>Diameter</i>.
664                                 trans.get("Shape.Ellipsoid.desc1"),
665                                 //// An ellipsoidal transition has a profile of a half-ellipse with major axes of lengths 2&times;<i>Length</i> and <i>Diameter</i>.  If the transition is not clipped, then the profile is extended at the center by the corresponding radius.         
666                                 trans.get("Shape.Ellipsoid.desc2"), true) {
667                         @Override
668                         public double getRadius(double x, double radius, double length, double param) {
669                                 assert x >= 0;
670                                 assert x <= length;
671                                 assert radius >= 0;
672                                 x = x * radius / length;
673                                 return MathUtil.safeSqrt(2 * radius * x - x * x); // radius/length * sphere
674                         }
675                 },
676                 
677                 //// Power series
678                 POWER(trans.get("Shape.Powerseries"),
679                                 trans.get("Shape.Powerseries.desc1"),
680                                 trans.get("Shape.Powerseries.desc2"), true) {
681                         @Override
682                         public boolean usesParameter() { // Range 0...1
683                                 return true;
684                         }
685                         
686                         @Override
687                         public double defaultParameter() {
688                                 return 0.5;
689                         }
690                         
691                         @Override
692                         public double getRadius(double x, double radius, double length, double param) {
693                                 assert x >= 0;
694                                 assert x <= length;
695                                 assert radius >= 0;
696                                 assert param >= 0;
697                                 assert param <= 1;
698                                 if (param <= 0.00001) {
699                                         if (x <= 0.00001)
700                                                 return 0;
701                                         else
702                                                 return radius;
703                                 }
704                                 return radius * Math.pow(x / length, param);
705                         }
706                         
707                 },
708                 
709                 //// Parabolic series
710                 PARABOLIC(trans.get("Shape.Parabolicseries"),
711                                 ////A parabolic series nose cone has a profile of a parabola.  The shape parameter defines the segment of the parabola to utilize.  The shape parameter 1.0 produces a <b>full parabola</b> which is tangent to the body tube, 0.75 produces a <b>3/4 parabola</b>, 0.5 procudes a <b>1/2 parabola</b> and 0 produces a <b>conical</b> nose cone.
712                                 trans.get("Shape.Parabolicseries.desc1"),
713                                 ////A parabolic series transition has a profile of a parabola.  The shape parameter defines the segment of the parabola to utilize.  The shape parameter 1.0 produces a <b>full parabola</b> which is tangent to the body tube at the aft end, 0.75 produces a <b>3/4 parabola</b>, 0.5 procudes a <b>1/2 parabola</b> and 0 produces a <b>conical</b> transition.
714                                 trans.get("Shape.Parabolicseries.desc2")) {
715                         
716                         // In principle a parabolic transition is clippable, but the difference is
717                         // negligible.
718                         
719                         @Override
720                         public boolean usesParameter() { // Range 0...1
721                                 return true;
722                         }
723                         
724                         @Override
725                         public double defaultParameter() {
726                                 return 1.0;
727                         }
728                         
729                         @Override
730                         public double getRadius(double x, double radius, double length, double param) {
731                                 assert x >= 0;
732                                 assert x <= length;
733                                 assert radius >= 0;
734                                 assert param >= 0;
735                                 assert param <= 1;
736                                 
737                                 return radius * ((2 * x / length - param * pow2(x / length)) / (2 - param));
738                         }
739                 },
740                 
741                 //// Haack series
742                 HAACK(trans.get("Shape.Haackseries"),
743                                 //// The Haack series nose cones are designed to minimize drag.  The shape parameter 0 produces an <b>LD-Haack</b> or <b>Von Karman</b> nose cone, which minimizes drag for fixed length and diameter, while a value of 0.333 produces an <b>LV-Haack</b> nose cone, which minimizes drag for fixed length and volume.
744                                 trans.get("Shape.Haackseries.desc1"),
745                                 //// The Haack series <i>nose cones</i> are designed to minimize drag.  These transition shapes are their equivalents, but do not necessarily produce optimal drag for transitions.  The shape parameter 0 produces an <b>LD-Haack</b> or <b>Von Karman</b> shape, while a value of 0.333 produces an <b>LV-Haack</b> shape.            
746                                 trans.get("Shape.Haackseries.desc2"), true) {
747                         
748                         @Override
749                         public boolean usesParameter() {
750                                 return true;
751                         }
752                         
753                         @Override
754                         public double maxParameter() {
755                                 return 1.0 / 3.0; // Range 0...1/3
756                         }
757                         
758                         @Override
759                         public double getRadius(double x, double radius, double length, double param) {
760                                 assert x >= 0;
761                                 assert x <= length;
762                                 assert radius >= 0;
763                                 assert param >= 0;
764                                 assert param <= 2;
765                                 
766                                 double theta = Math.acos(1 - 2 * x / length);
767                                 if (MathUtil.equals(param, 0)) {
768                                         return radius * MathUtil.safeSqrt((theta - sin(2 * theta) / 2) / Math.PI);
769                                 }
770                                 return radius * MathUtil.safeSqrt((theta - sin(2 * theta) / 2 + param * pow3(sin(theta))) / Math.PI);
771                         }
772                 },
773                 
774                 //              POLYNOMIAL("Smooth polynomial",
775                 //                              "A polynomial is fitted such that the nose cone profile is horizontal "+
776                 //                              "at the aft end of the transition.  The angle at the tip is defined by "+
777                 //                              "the shape parameter.",
778                 //                              "A polynomial is fitted such that the transition profile is horizontal "+
779                 //                              "at the aft end of the transition.  The angle at the fore end is defined "+
780                 //                              "by the shape parameter.") {
781                 //                      @Override
782                 //                      public boolean usesParameter() {
783                 //                              return true;
784                 //                      }
785                 //                      @Override
786                 //                      public double maxParameter() {
787                 //                              return 3.0;   //  Range 0...3
788                 //                      }
789                 //                      @Override
790                 //                      public double defaultParameter() {
791                 //                              return 0.0;
792                 //                      }
793                 //                      public double getRadius(double x, double radius, double length, double param) {
794                 //                              assert x >= 0;
795                 //                              assert x <= length;
796                 //                              assert radius >= 0;
797                 //                              assert param >= 0;
798                 //                              assert param <= 3;
799                 //                              // p(x) = (k-2)x^3 + (3-2k)x^2 + k*x
800                 //                              x = x/length;
801                 //                              return radius*((((param-2)*x + (3-2*param))*x + param)*x);
802                 //                      }
803                 //              }
804                 ;
805                 
806                 // Privete fields of the shapes
807                 private final String name;
808                 private final String transitionDesc;
809                 private final String noseconeDesc;
810                 private final boolean canClip;
811                 
812                 // Non-clippable constructor
813                 Shape(String name, String noseconeDesc, String transitionDesc) {
814                         this(name, noseconeDesc, transitionDesc, false);
815                 }
816                 
817                 // Clippable constructor
818                 Shape(String name, String noseconeDesc, String transitionDesc, boolean canClip) {
819                         this.name = name;
820                         this.canClip = canClip;
821                         this.noseconeDesc = noseconeDesc;
822                         this.transitionDesc = transitionDesc;
823                 }
824                 
825                 
826                 /**
827                  * Return the name of the transition shape name.
828                  */
829                 public String getName() {
830                         return name;
831                 }
832                 
833                 /**
834                  * Get a description of the Transition shape.
835                  */
836                 public String getTransitionDescription() {
837                         return transitionDesc;
838                 }
839                 
840                 /**
841                  * Get a description of the NoseCone shape.
842                  */
843                 public String getNoseConeDescription() {
844                         return noseconeDesc;
845                 }
846                 
847                 /**
848                  * Check whether the shape differs in clipped mode.  The clipping should be
849                  * enabled by default if possible.
850                  */
851                 public boolean isClippable() {
852                         return canClip;
853                 }
854                 
855                 /**
856                  * Return whether the shape uses the shape parameter.  (Default false.)
857                  */
858                 public boolean usesParameter() {
859                         return false;
860                 }
861                 
862                 /**
863                  * Return the minimum value of the shape parameter.  (Default 0.)
864                  */
865                 public double minParameter() {
866                         return 0.0;
867                 }
868                 
869                 /**
870                  * Return the maximum value of the shape parameter.  (Default 1.)
871                  */
872                 public double maxParameter() {
873                         return 1.0;
874                 }
875                 
876                 /**
877                  * Return the default value of the shape parameter.  (Default 0.)
878                  */
879                 public double defaultParameter() {
880                         return 0.0;
881                 }
882                 
883                 /**
884                  * Calculate the basic radius of a transition with the given radius, length and
885                  * shape parameter at the point x from the tip of the component.  It is assumed
886                  * that the fore radius if zero and the aft radius is <code>radius >= 0</code>.
887                  * Boattails are achieved by reversing the component.
888                  *
889                  * @param x      Position from the tip of the component.
890                  * @param radius Aft end radius >= 0.
891                  * @param length Length of the transition >= 0.
892                  * @param param  Valid shape parameter.
893                  * @return       The basic radius at the given position.
894                  */
895                 public abstract double getRadius(double x, double radius, double length, double param);
896                 
897                 
898                 /**
899                  * Returns the name of the shape (same as getName()).
900                  */
901                 @Override
902                 public String toString() {
903                         return name;
904                 }
905         }
906 }