1 package net.sf.openrocket.rocketcomponent;
3 import net.sf.openrocket.logging.LogHelper;
4 import net.sf.openrocket.startup.Application;
5 import net.sf.openrocket.util.ArrayList;
6 import net.sf.openrocket.util.BugException;
7 import net.sf.openrocket.util.Coordinate;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.List;
14 public class FreeformFinSet extends FinSet {
15 private static final LogHelper log = Application.getLogger();
17 private ArrayList<Coordinate> points = new ArrayList<Coordinate>();
19 public FreeformFinSet() {
20 points.add(Coordinate.NUL);
21 points.add(new Coordinate(0.025, 0.05));
22 points.add(new Coordinate(0.075, 0.05));
23 points.add(new Coordinate(0.05, 0));
29 public FreeformFinSet(Coordinate[] finpoints) throws IllegalFinPointException {
34 public FreeformFinSet(FinSet finset) {
35 Coordinate[] finpoints = finset.getFinPoints();
36 this.copyFrom(finset);
39 for (Coordinate c: finpoints) {
42 this.length = points.get(points.size()-1).x - points.get(0).x;
48 * Convert an existing fin set into a freeform fin set. The specified
49 * fin set is taken out of the rocket tree (if any) and the new component
50 * inserted in its stead.
52 * The specified fin set should not be used after the call!
54 * @param finset the fin set to convert.
55 * @return the new freeform fin set.
57 public static FreeformFinSet convertFinSet(FinSet finset) {
58 log.info("Converting " + finset.getComponentName() + " into freeform fin set");
59 final RocketComponent root = finset.getRoot();
60 FreeformFinSet freeform;
61 List<RocketComponent> toInvalidate = Collections.emptyList();
64 if (root instanceof Rocket) {
65 ((Rocket) root).freeze();
68 // Get fin set position and remove fin set
69 final RocketComponent parent = finset.getParent();
72 position = parent.getChildPosition(finset);
73 parent.removeChild(position);
79 // Create the freeform fin set
80 Coordinate[] finpoints = finset.getFinPoints();
82 freeform = new FreeformFinSet(finpoints);
83 } catch (IllegalFinPointException e) {
84 throw new BugException("Illegal fin points when converting existing fin to " +
85 "freeform fin, fin=" + finset + " points=" + Arrays.toString(finpoints),
89 // Copy component attributes
90 toInvalidate = freeform.copyFrom(finset);
93 final String componentTypeName = finset.getComponentName();
94 final String name = freeform.getName();
96 if (name.startsWith(componentTypeName)) {
97 freeform.setName(freeform.getComponentName() +
98 name.substring(componentTypeName.length()));
101 // Add freeform fin set to parent
102 if (parent != null) {
103 parent.addChild(freeform, position);
107 if (root instanceof Rocket) {
108 ((Rocket) root).thaw();
110 // Invalidate components after events have been fired
111 for (RocketComponent c : toInvalidate) {
121 * Add a fin point between indices <code>index-1</code> and <code>index</code>.
122 * The point is placed at the midpoint of the current segment.
124 * @param index the fin point before which to add the new point.
126 public void addPoint(int index) {
127 double x0, y0, x1, y1;
129 x0 = points.get(index - 1).x;
130 y0 = points.get(index - 1).y;
131 x1 = points.get(index).x;
132 y1 = points.get(index).y;
134 points.add(index, new Coordinate((x0 + x1) / 2, (y0 + y1) / 2));
135 // adding a point within the segment affects neither mass nor aerodynamics
136 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
141 * Remove the fin point with the given index. The first and last fin points
142 * cannot be removed, and will cause an <code>IllegalFinPointException</code>
145 * @param index the fin point index to remove
146 * @throws IllegalFinPointException if removing would result in invalid fin planform
148 public void removePoint(int index) throws IllegalFinPointException {
149 if (index == 0 || index == points.size() - 1) {
150 throw new IllegalFinPointException("cannot remove first or last point");
153 ArrayList<Coordinate> copy = this.points.clone();
158 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
162 public int getPointCount() {
163 return points.size();
166 public void setPoints(Coordinate[] points) throws IllegalFinPointException {
167 ArrayList<Coordinate> list = new ArrayList<Coordinate>(points.length);
168 for (Coordinate p : points) {
174 this.length = points[points.length - 1].x;
175 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
180 * Set the point at position <code>i</code> to coordinates (x,y).
182 * Note that this method enforces basic fin shape restrictions (non-negative y,
183 * first and last point locations) silently, but throws an
184 * <code>IllegalFinPointException</code> if the point causes fin segments to
187 * Moving of the first point in the X-axis is allowed, but this actually moves
188 * all of the other points the corresponding distance back.
190 * @param index the point index to modify.
191 * @param x the x-coordinate.
192 * @param y the y-coordinate.
193 * @throws IllegalFinPointException if the specified fin point would cause intersecting
196 public void setPoint(int index, double x, double y) throws IllegalFinPointException {
200 double x0, y0, x1, y1;
205 x = Math.min(x, points.get(points.size() - 1).x);
209 x1 = points.get(1).x;
210 y1 = points.get(1).y;
212 } else if (index == points.size() - 1) {
217 x0 = points.get(index - 1).x;
218 y0 = points.get(index - 1).y;
224 x0 = points.get(index - 1).x;
225 y0 = points.get(index - 1).y;
226 x1 = points.get(index + 1).x;
227 y1 = points.get(index + 1).y;
233 // Check for intersecting
234 double px0, py0, px1, py1;
237 for (int i = 1; i < points.size(); i++) {
238 px1 = points.get(i).x;
239 py1 = points.get(i).y;
241 if (i != index - 1 && i != index && i != index + 1) {
242 if (intersects(x0, y0, x, y, px0, py0, px1, py1)) {
243 throw new IllegalFinPointException("segments intersect");
246 if (i != index && i != index + 1 && i != index + 2) {
247 if (intersects(x, y, x1, y1, px0, py0, px1, py1)) {
248 throw new IllegalFinPointException("segments intersect");
258 System.out.println("Set point zero to x:" + x);
259 for (int i = 1; i < points.size(); i++) {
260 Coordinate c = points.get(i);
261 points.set(i, c.setX(c.x - x));
266 points.set(index, new Coordinate(x, y));
269 if (index == 0 || index == points.size() - 1) {
270 this.length = points.get(points.size() - 1).x;
272 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
277 private boolean intersects(double ax0, double ay0, double ax1, double ay1,
278 double bx0, double by0, double bx1, double by1) {
280 double d = ((by1 - by0) * (ax1 - ax0) - (bx1 - bx0) * (ay1 - ay0));
282 double ua = ((bx1 - bx0) * (ay0 - by0) - (by1 - by0) * (ax0 - bx0)) / d;
283 double ub = ((ax1 - ax0) * (ay0 - by0) - (ay1 - ay0) * (ax0 - bx0)) / d;
285 return (ua >= 0) && (ua <= 1) && (ub >= 0) && (ub <= 1);
290 public Coordinate[] getFinPoints() {
291 return points.toArray(new Coordinate[0]);
295 public double getSpan() {
297 for (Coordinate c : points) {
305 public String getComponentName() {
306 return "Freeform fin set";
311 protected RocketComponent copyWithOriginalID() {
312 RocketComponent c = super.copyWithOriginalID();
313 ((FreeformFinSet) c).points = this.points.clone();
317 private void validate(ArrayList<Coordinate> points) throws IllegalFinPointException {
318 final int n = points.size();
319 if (points.get(0).x != 0 || points.get(0).y != 0 ||
320 points.get(n - 1).x < 0 || points.get(n - 1).y != 0) {
321 throw new IllegalFinPointException("Start or end point illegal.");
323 for (int i = 0; i < n - 1; i++) {
324 for (int j = i + 2; j < n - 1; j++) {
325 if (intersects(points.get(i).x, points.get(i).y, points.get(i + 1).x, points.get(i + 1).y,
326 points.get(j).x, points.get(j).y, points.get(j + 1).x, points.get(j + 1).y)) {
327 throw new IllegalFinPointException("segments intersect");
330 if (points.get(i).z != 0) {
331 throw new IllegalFinPointException("z-coordinate not zero");