1 package net.sf.openrocket.rocketcomponent;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import net.sf.openrocket.util.BugException;
7 import net.sf.openrocket.util.Coordinate;
10 public class FreeformFinSet extends FinSet {
12 private ArrayList<Coordinate> points = new ArrayList<Coordinate>();
14 public FreeformFinSet() {
15 points.add(Coordinate.NUL);
16 points.add(new Coordinate(0.025,0.05));
17 points.add(new Coordinate(0.075,0.05));
18 points.add(new Coordinate(0.05,0));
24 public FreeformFinSet(Coordinate[] finpoints) throws IllegalFinPointException {
29 public FreeformFinSet(FinSet finset) {
30 Coordinate[] finpoints = finset.getFinPoints();
31 this.copyFrom(finset);
34 for (Coordinate c: finpoints) {
37 this.length = points.get(points.size()-1).x - points.get(0).x;
43 * Convert an existing fin set into a freeform fin set. The specified
44 * fin set is taken out of the rocket tree (if any) and the new component
45 * inserted in its stead.
47 * The specified fin set should not be used after the call!
49 * @param finset the fin set to convert.
50 * @return the new freeform fin set.
52 public static FreeformFinSet convertFinSet(FinSet finset) {
53 final RocketComponent root = finset.getRoot();
54 FreeformFinSet freeform;
57 if (root instanceof Rocket) {
58 ((Rocket)root).freeze();
61 // Get fin set position and remove fin set
62 final RocketComponent parent = finset.getParent();
65 position = parent.getChildPosition(finset);
66 parent.removeChild(position);
72 // Create the freeform fin set
73 Coordinate[] finpoints = finset.getFinPoints();
75 freeform = new FreeformFinSet(finpoints);
76 } catch (IllegalFinPointException e) {
77 throw new BugException("Illegal fin points when converting existing fin to " +
78 "freeform fin, fin=" + finset + " points="+Arrays.toString(finpoints),
82 // Copy component attributes
83 freeform.copyFrom(finset);
86 final String componentTypeName = finset.getComponentName();
87 final String name = freeform.getName();
89 if (name.startsWith(componentTypeName)) {
90 freeform.setName(freeform.getComponentName() +
91 name.substring(componentTypeName.length()));
94 // Add freeform fin set to parent
96 parent.addChild(freeform, position);
100 if (root instanceof Rocket) {
101 ((Rocket)root).thaw();
110 * Add a fin point between indices <code>index-1</code> and <code>index</code>.
111 * The point is placed at the midpoint of the current segment.
113 * @param index the fin point before which to add the new point.
115 public void addPoint(int index) {
116 double x0, y0, x1, y1;
118 x0 = points.get(index-1).x;
119 y0 = points.get(index-1).y;
120 x1 = points.get(index).x;
121 y1 = points.get(index).y;
123 points.add(index, new Coordinate((x0+x1)/2, (y0+y1)/2));
124 // adding a point within the segment affects neither mass nor aerodynamics
125 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
130 * Remove the fin point with the given index. The first and last fin points
131 * cannot be removed, and will cause an <code>IllegalFinPointException</code>
134 * @param index the fin point index to remove
135 * @throws IllegalFinPointException if removing the first or last fin point was attempted.
137 public void removePoint(int index) throws IllegalFinPointException {
138 if (index == 0 || index == points.size()-1) {
139 throw new IllegalFinPointException("cannot remove first or last point");
141 // TODO: CRITICAL: Can result in invalid fin points
142 points.remove(index);
143 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
147 public int getPointCount() {
148 return points.size();
151 public void setPoints(Coordinate[] p) throws IllegalFinPointException {
152 if (p[0].x != 0 || p[0].y != 0 || p[p.length-1].x < 0 || p[p.length-1].y != 0) {
153 throw new IllegalFinPointException("Start or end point illegal.");
155 for (int i=0; i < p.length-1; i++) {
156 for (int j=i+2; j < p.length-1; j++) {
157 if (intersects(p[i].x, p[i].y, p[i+1].x, p[i+1].y,
158 p[j].x, p[j].y, p[j+1].x, p[j+1].y)) {
159 throw new IllegalFinPointException("segments intersect");
163 throw new IllegalFinPointException("z-coordinate not zero");
168 for (Coordinate c: p) {
171 this.length = p[p.length-1].x;
172 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
177 * Set the point at position <code>i</code> to coordinates (x,y).
179 * Note that this method enforces basic fin shape restrictions (non-negative y,
180 * first and last point locations) silently, but throws an
181 * <code>IllegalFinPointException</code> if the point causes fin segments to
184 * Moving of the first point in the X-axis is allowed, but this actually moves
185 * all of the other points the corresponding distance back.
187 * @param index the point index to modify.
188 * @param x the x-coordinate.
189 * @param y the y-coordinate.
190 * @throws IllegalFinPointException if the specified fin point would cause intersecting
193 public void setPoint(int index, double x, double y) throws IllegalFinPointException {
202 x = Math.min(x, points.get(points.size()-1).x);
206 x1 = points.get(1).x;
207 y1 = points.get(1).y;
209 } else if (index == points.size()-1) {
214 x0 = points.get(index-1).x;
215 y0 = points.get(index-1).y;
221 x0 = points.get(index-1).x;
222 y0 = points.get(index-1).y;
223 x1 = points.get(index+1).x;
224 y1 = points.get(index+1).y;
230 // Check for intersecting
231 double px0, py0, px1, py1;
234 for (int i=1; i < points.size(); i++) {
235 px1 = points.get(i).x;
236 py1 = points.get(i).y;
238 if (i != index-1 && i != index && i != index+1) {
239 if (intersects(x0,y0,x,y,px0,py0,px1,py1)) {
240 throw new IllegalFinPointException("segments intersect");
243 if (i != index && i != index+1 && i != index+2) {
244 if (intersects(x,y,x1,y1,px0,py0,px1,py1)) {
245 throw new IllegalFinPointException("segments intersect");
255 System.out.println("Set point zero to x:"+x);
256 for (int i=1; i < points.size(); i++) {
257 Coordinate c = points.get(i);
258 points.set(i, c.setX(c.x - x));
263 points.set(index,new Coordinate(x,y));
266 if (index == 0 || index == points.size()-1) {
267 this.length = points.get(points.size()-1).x;
269 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
274 private boolean intersects(double ax0, double ay0, double ax1, double ay1,
275 double bx0, double by0, double bx1, double by1) {
277 double d = ((by1-by0)*(ax1-ax0) - (bx1-bx0)*(ay1-ay0));
279 double ua = ((bx1-bx0)*(ay0-by0) - (by1-by0)*(ax0-bx0)) / d;
280 double ub = ((ax1-ax0)*(ay0-by0) - (ay1-ay0)*(ax0-bx0)) / d;
282 return (ua >= 0) && (ua <= 1) && (ub >= 0) && (ub <= 1);
287 public Coordinate[] getFinPoints() {
288 return points.toArray(new Coordinate[0]);
292 public double getSpan() {
294 for (Coordinate c: points) {
302 public String getComponentName() {
303 return "Freeform fin set";
307 @SuppressWarnings("unchecked")
309 public RocketComponent copy() {
310 RocketComponent c = super.copy();
311 ((FreeformFinSet)c).points = (ArrayList<Coordinate>) this.points.clone();