1 package net.sf.openrocket.rocketcomponent;
3 import java.util.Arrays;
4 import java.util.Collections;
7 import net.sf.openrocket.l10n.Translator;
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.startup.Application;
10 import net.sf.openrocket.util.ArrayList;
11 import net.sf.openrocket.util.BugException;
12 import net.sf.openrocket.util.Coordinate;
15 public class FreeformFinSet extends FinSet {
16 private static final LogHelper log = Application.getLogger();
17 private static final Translator trans = Application.getTranslator();
19 private ArrayList<Coordinate> points = new ArrayList<Coordinate>();
21 public FreeformFinSet() {
22 points.add(Coordinate.NUL);
23 points.add(new Coordinate(0.025, 0.05));
24 points.add(new Coordinate(0.075, 0.05));
25 points.add(new Coordinate(0.05, 0));
31 public FreeformFinSet(Coordinate[] finpoints) throws IllegalFinPointException {
36 public FreeformFinSet(FinSet finset) {
37 Coordinate[] finpoints = finset.getFinPoints();
38 this.copyFrom(finset);
41 for (Coordinate c: finpoints) {
44 this.length = points.get(points.size()-1).x - points.get(0).x;
50 * Convert an existing fin set into a freeform fin set. The specified
51 * fin set is taken out of the rocket tree (if any) and the new component
52 * inserted in its stead.
54 * The specified fin set should not be used after the call!
56 * @param finset the fin set to convert.
57 * @return the new freeform fin set.
59 public static FreeformFinSet convertFinSet(FinSet finset) {
60 log.info("Converting " + finset.getComponentName() + " into freeform fin set");
61 final RocketComponent root = finset.getRoot();
62 FreeformFinSet freeform;
63 List<RocketComponent> toInvalidate = Collections.emptyList();
66 if (root instanceof Rocket) {
67 ((Rocket) root).freeze();
70 // Get fin set position and remove fin set
71 final RocketComponent parent = finset.getParent();
74 position = parent.getChildPosition(finset);
75 parent.removeChild(position);
81 // Create the freeform fin set
82 Coordinate[] finpoints = finset.getFinPoints();
84 freeform = new FreeformFinSet(finpoints);
85 } catch (IllegalFinPointException e) {
86 throw new BugException("Illegal fin points when converting existing fin to " +
87 "freeform fin, fin=" + finset + " points=" + Arrays.toString(finpoints),
91 // Copy component attributes
92 toInvalidate = freeform.copyFrom(finset);
95 final String componentTypeName = finset.getComponentName();
96 final String name = freeform.getName();
98 if (name.startsWith(componentTypeName)) {
99 freeform.setName(freeform.getComponentName() +
100 name.substring(componentTypeName.length()));
103 // Add freeform fin set to parent
104 if (parent != null) {
105 parent.addChild(freeform, position);
109 if (root instanceof Rocket) {
110 ((Rocket) root).thaw();
112 // Invalidate components after events have been fired
113 for (RocketComponent c : toInvalidate) {
123 * Add a fin point between indices <code>index-1</code> and <code>index</code>.
124 * The point is placed at the midpoint of the current segment.
126 * @param index the fin point before which to add the new point.
128 public void addPoint(int index) {
129 double x0, y0, x1, y1;
131 x0 = points.get(index - 1).x;
132 y0 = points.get(index - 1).y;
133 x1 = points.get(index).x;
134 y1 = points.get(index).y;
136 points.add(index, new Coordinate((x0 + x1) / 2, (y0 + y1) / 2));
137 // adding a point within the segment affects neither mass nor aerodynamics
138 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
143 * Remove the fin point with the given index. The first and last fin points
144 * cannot be removed, and will cause an <code>IllegalFinPointException</code>
147 * @param index the fin point index to remove
148 * @throws IllegalFinPointException if removing would result in invalid fin planform
150 public void removePoint(int index) throws IllegalFinPointException {
151 if (index == 0 || index == points.size() - 1) {
152 throw new IllegalFinPointException("cannot remove first or last point");
155 ArrayList<Coordinate> copy = this.points.clone();
160 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
164 public int getPointCount() {
165 return points.size();
168 public void setPoints(Coordinate[] points) throws IllegalFinPointException {
169 setPoints(Arrays.asList(points));
172 public void setPoints(List<Coordinate> points) throws IllegalFinPointException {
173 ArrayList<Coordinate> list = new ArrayList<Coordinate>(points);
177 this.length = points.get(points.size() - 1).x;
178 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
183 * Set the point at position <code>i</code> to coordinates (x,y).
185 * Note that this method enforces basic fin shape restrictions (non-negative y,
186 * first and last point locations) silently, but throws an
187 * <code>IllegalFinPointException</code> if the point causes fin segments to
190 * Moving of the first point in the X-axis is allowed, but this actually moves
191 * all of the other points the corresponding distance back.
193 * @param index the point index to modify.
194 * @param x the x-coordinate.
195 * @param y the y-coordinate.
196 * @throws IllegalFinPointException if the specified fin point would cause intersecting
199 public void setPoint(int index, double x, double y) throws IllegalFinPointException {
203 double x0, y0, x1, y1;
208 x = Math.min(x, points.get(points.size() - 1).x);
212 x1 = points.get(1).x;
213 y1 = points.get(1).y;
215 } else if (index == points.size() - 1) {
220 x0 = points.get(index - 1).x;
221 y0 = points.get(index - 1).y;
227 x0 = points.get(index - 1).x;
228 y0 = points.get(index - 1).y;
229 x1 = points.get(index + 1).x;
230 y1 = points.get(index + 1).y;
236 // Check for intersecting
237 double px0, py0, px1, py1;
240 for (int i = 1; i < points.size(); i++) {
241 px1 = points.get(i).x;
242 py1 = points.get(i).y;
244 if (i != index - 1 && i != index && i != index + 1) {
245 if (intersects(x0, y0, x, y, px0, py0, px1, py1)) {
246 throw new IllegalFinPointException("segments intersect");
249 if (i != index && i != index + 1 && i != index + 2) {
250 if (intersects(x, y, x1, y1, px0, py0, px1, py1)) {
251 throw new IllegalFinPointException("segments intersect");
261 System.out.println("Set point zero to x:" + x);
262 for (int i = 1; i < points.size(); i++) {
263 Coordinate c = points.get(i);
264 points.set(i, c.setX(c.x - x));
269 points.set(index, new Coordinate(x, y));
272 if (index == 0 || index == points.size() - 1) {
273 this.length = points.get(points.size() - 1).x;
275 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
280 private boolean intersects(double ax0, double ay0, double ax1, double ay1,
281 double bx0, double by0, double bx1, double by1) {
283 double d = ((by1 - by0) * (ax1 - ax0) - (bx1 - bx0) * (ay1 - ay0));
285 double ua = ((bx1 - bx0) * (ay0 - by0) - (by1 - by0) * (ax0 - bx0)) / d;
286 double ub = ((ax1 - ax0) * (ay0 - by0) - (ay1 - ay0) * (ax0 - bx0)) / d;
288 return (ua >= 0) && (ua <= 1) && (ub >= 0) && (ub <= 1);
293 public Coordinate[] getFinPoints() {
294 return points.toArray(new Coordinate[0]);
298 public double getSpan() {
300 for (Coordinate c : points) {
308 public String getComponentName() {
309 //// Freeform fin set
310 return trans.get("FreeformFinSet.FreeformFinSet");
315 protected RocketComponent copyWithOriginalID() {
316 RocketComponent c = super.copyWithOriginalID();
317 ((FreeformFinSet) c).points = this.points.clone();
321 private void validate(ArrayList<Coordinate> pts) throws IllegalFinPointException {
322 final int n = pts.size();
323 if (pts.get(0).x != 0 || pts.get(0).y != 0 ||
324 pts.get(n - 1).x < 0 || pts.get(n - 1).y != 0) {
325 throw new IllegalFinPointException("Start or end point illegal.");
327 for (int i = 0; i < n - 1; i++) {
328 for (int j = i + 2; j < n - 1; j++) {
329 if (intersects(pts.get(i).x, pts.get(i).y, pts.get(i + 1).x, pts.get(i + 1).y,
330 pts.get(j).x, pts.get(j).y, pts.get(j + 1).x, pts.get(j + 1).y)) {
331 throw new IllegalFinPointException("segments intersect");
334 if (pts.get(i).z != 0) {
335 throw new IllegalFinPointException("z-coordinate not zero");