1 package net.sf.openrocket.rocketcomponent;
3 import java.util.ArrayList;
6 import net.sf.openrocket.util.Coordinate;
9 public class FreeformFinSet extends FinSet {
11 private final List<Coordinate> points = new ArrayList<Coordinate>();
13 public FreeformFinSet() {
14 points.add(Coordinate.NUL);
15 points.add(new Coordinate(0.025,0.05));
16 points.add(new Coordinate(0.075,0.05));
17 points.add(new Coordinate(0.05,0));
23 public FreeformFinSet(FinSet finset) {
24 Coordinate[] finpoints = finset.getFinPoints();
25 this.copyFrom(finset);
28 for (Coordinate c: finpoints) {
31 this.length = points.get(points.size()-1).x - points.get(0).x;
37 * Add a fin point between indices <code>index-1</code> and <code>index</code>.
38 * The point is placed at the midpoint of the current segment.
40 * @param index the fin point before which to add the new point.
42 public void addPoint(int index) {
43 double x0, y0, x1, y1;
45 x0 = points.get(index-1).x;
46 y0 = points.get(index-1).y;
47 x1 = points.get(index).x;
48 y1 = points.get(index).y;
50 points.add(index, new Coordinate((x0+x1)/2, (y0+y1)/2));
51 // adding a point within the segment affects neither mass nor aerodynamics
52 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
57 * Remove the fin point with the given index. The first and last fin points
58 * cannot be removed, and will cause an <code>IllegalArgumentException</code>
61 * @param index the fin point index to remove
63 public void removePoint(int index) {
64 if (index == 0 || index == points.size()-1) {
65 throw new IllegalArgumentException("cannot remove first or last point");
68 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
72 public int getPointCount() {
76 public void setPoints(Coordinate[] p) {
77 if (p[0].x != 0 || p[0].y != 0 || p[p.length-1].y != 0) {
78 throw new IllegalArgumentException("Start or end point illegal.");
80 for (int i=0; i < p.length-1; i++) {
81 for (int j=i+2; j < p.length-1; j++) {
82 if (intersects(p[i].x, p[i].y, p[i+1].x, p[i+1].y,
83 p[j].x, p[j].y, p[j+1].x, p[j+1].y)) {
84 throw new IllegalArgumentException("segments intersect");
88 throw new IllegalArgumentException("z-coordinate not zero");
93 for (Coordinate c: p) {
96 this.length = p[p.length-1].x;
97 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
102 * Set the point at position <code>i</code> to coordinates (x,y).
104 * Note that this method enforces basic fin shape restrictions (non-negative y,
105 * first and last point locations) silently, but throws an
106 * <code>IllegalArgumentException</code> if the point causes fin segments to
107 * intersect. The calling method should always catch this exception.
109 * Moving of the first point in the X-axis is allowed, but this actually moves
110 * all of the other points the corresponding distance back.
112 * @param index the point index to modify.
113 * @param x the x-coordinate.
114 * @param y the y-coordinate.
116 public void setPoint(int index, double x, double y) {
125 x = Math.min(x, points.get(points.size()-1).x);
129 x1 = points.get(1).x;
130 y1 = points.get(1).y;
132 } else if (index == points.size()-1) {
137 x0 = points.get(index-1).x;
138 y0 = points.get(index-1).y;
144 x0 = points.get(index-1).x;
145 y0 = points.get(index-1).y;
146 x1 = points.get(index+1).x;
147 y1 = points.get(index+1).y;
153 // Check for intersecting
154 double px0, py0, px1, py1;
157 for (int i=1; i < points.size(); i++) {
158 px1 = points.get(i).x;
159 py1 = points.get(i).y;
161 if (i != index-1 && i != index && i != index+1) {
162 if (intersects(x0,y0,x,y,px0,py0,px1,py1)) {
163 throw new IllegalArgumentException("segments intersect");
166 if (i != index && i != index+1 && i != index+2) {
167 if (intersects(x,y,x1,y1,px0,py0,px1,py1)) {
168 throw new IllegalArgumentException("segments intersect");
178 System.out.println("Set point zero to x:"+x);
179 for (int i=1; i < points.size(); i++) {
180 Coordinate c = points.get(i);
181 points.set(i, c.setX(c.x - x));
186 points.set(index,new Coordinate(x,y));
189 if (index == 0 || index == points.size()-1) {
190 this.length = points.get(points.size()-1).x;
192 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
197 private boolean intersects(double ax0, double ay0, double ax1, double ay1,
198 double bx0, double by0, double bx1, double by1) {
200 double d = ((by1-by0)*(ax1-ax0) - (bx1-bx0)*(ay1-ay0));
202 double ua = ((bx1-bx0)*(ay0-by0) - (by1-by0)*(ax0-bx0)) / d;
203 double ub = ((ax1-ax0)*(ay0-by0) - (ay1-ay0)*(ax0-bx0)) / d;
205 return (ua >= 0) && (ua <= 1) && (ub >= 0) && (ub <= 1);
210 public Coordinate[] getFinPoints() {
211 return points.toArray(new Coordinate[0]);
215 public double getSpan() {
217 for (Coordinate c: points) {
225 public String getComponentName() {
226 return "Freeform fin set";