Initial commit
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / FreeformFinSet.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import net.sf.openrocket.util.Coordinate;
7
8
9 public class FreeformFinSet extends FinSet {
10
11         private final List<Coordinate> points = new ArrayList<Coordinate>();
12         
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));
18                 
19                 this.length = 0.05;
20         }
21         
22         
23         public FreeformFinSet(FinSet finset) {
24                 Coordinate[] finpoints = finset.getFinPoints();
25                 this.copyFrom(finset);
26                 
27                 points.clear();
28                 for (Coordinate c: finpoints) {
29                         points.add(c);
30                 }
31                 this.length = points.get(points.size()-1).x - points.get(0).x;
32         }
33         
34         
35         
36         /**
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.
39          * 
40          * @param index   the fin point before which to add the new point.
41          */
42         public void addPoint(int index) {
43                 double x0, y0, x1, y1;
44                 
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;
49                 
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);
53         }
54         
55         
56         /**
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>
59          * if attempted.
60          * 
61          * @param index   the fin point index to remove
62          */
63         public void removePoint(int index) {
64                 if (index == 0  ||  index == points.size()-1) {
65                         throw new IllegalArgumentException("cannot remove first or last point");
66                 }
67                 points.remove(index);
68                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
69         }
70         
71         
72         public int getPointCount() {
73                 return points.size();
74         }
75         
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.");
79                 }
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");
85                                 }
86                         }
87                         if (p[i].z != 0) {
88                                 throw new IllegalArgumentException("z-coordinate not zero");
89                         }
90                 }
91                 
92                 points.clear();
93                 for (Coordinate c: p) {
94                         points.add(c);
95                 }
96                 this.length = p[p.length-1].x;
97                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
98         }
99         
100
101         /**
102          * Set the point at position <code>i</code> to coordinates (x,y).
103          * <p>
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.
108          * <p>
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.
111          * 
112          * @param index the point index to modify.
113          * @param x             the x-coordinate.
114          * @param y             the y-coordinate.
115          */
116         public void setPoint(int index, double x, double y) {
117                 if (y < 0)
118                         y = 0;
119                 
120                 double x0,y0,x1,y1;
121                 
122                 if (index == 0) {
123                         
124                         // Restrict point
125                         x = Math.min(x, points.get(points.size()-1).x);
126                         y = 0;
127                         x0 = Double.NaN;
128                         y0 = Double.NaN;
129                         x1 = points.get(1).x;
130                         y1 = points.get(1).y;
131                         
132                 } else if (index == points.size()-1) {
133                         
134                         // Restrict point
135                         x = Math.max(x, 0);
136                         y = 0;
137                         x0 = points.get(index-1).x;
138                         y0 = points.get(index-1).y;
139                         x1 = Double.NaN;
140                         y1 = Double.NaN;
141                         
142                 } else {
143                         
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;
148                         
149                 }
150                 
151                 
152                 
153                 // Check for intersecting
154                 double px0, py0, px1, py1;
155                 px0 = 0;
156                 py0 = 0;
157                 for (int i=1; i < points.size(); i++) {
158                         px1 = points.get(i).x;
159                         py1 = points.get(i).y;
160                         
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");
164                                 }
165                         }
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");
169                                 }
170                         }
171                         
172                         px0 = px1;
173                         py0 = py1;
174                 }
175                 
176                 if (index == 0) {
177                         
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));
182                         }
183                         
184                 } else {
185                         
186                         points.set(index,new Coordinate(x,y));
187                         
188                 }
189                 if (index == 0 || index == points.size()-1) {
190                         this.length = points.get(points.size()-1).x;
191                 }
192                 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
193         }
194         
195         
196         
197         private boolean intersects(double ax0, double ay0, double ax1, double ay1,
198                         double bx0, double by0, double bx1, double by1) {
199                 
200                 double d = ((by1-by0)*(ax1-ax0) - (bx1-bx0)*(ay1-ay0));
201                 
202                 double ua = ((bx1-bx0)*(ay0-by0) - (by1-by0)*(ax0-bx0)) / d;
203                 double ub = ((ax1-ax0)*(ay0-by0) - (ay1-ay0)*(ax0-bx0)) / d;
204                 
205                 return (ua >= 0) && (ua <= 1) && (ub >= 0) && (ub <= 1);
206         }
207         
208
209         @Override
210         public Coordinate[] getFinPoints() {
211                 return points.toArray(new Coordinate[0]);
212         }
213
214         @Override
215         public double getSpan() {
216                 double max = 0;
217                 for (Coordinate c: points) {
218                         if (c.y > max)
219                                 max = c.y;
220                 }
221                 return max;
222         }
223
224         @Override
225         public String getComponentName() {
226                 return "Freeform fin set";
227         }
228
229 }