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.BugException;
6 import net.sf.openrocket.util.Coordinate;
8 import java.util.ArrayList;
9 import java.util.Arrays;
12 public class FreeformFinSet extends FinSet {
13 private static final LogHelper log = Application.getLogger();
15 private ArrayList<Coordinate> points = new ArrayList<Coordinate>();
17 public FreeformFinSet() {
18 points.add(Coordinate.NUL);
19 points.add(new Coordinate(0.025, 0.05));
20 points.add(new Coordinate(0.075, 0.05));
21 points.add(new Coordinate(0.05, 0));
27 public FreeformFinSet(Coordinate[] finpoints) throws IllegalFinPointException {
32 public FreeformFinSet(FinSet finset) {
33 Coordinate[] finpoints = finset.getFinPoints();
34 this.copyFrom(finset);
37 for (Coordinate c: finpoints) {
40 this.length = points.get(points.size()-1).x - points.get(0).x;
46 * Convert an existing fin set into a freeform fin set. The specified
47 * fin set is taken out of the rocket tree (if any) and the new component
48 * inserted in its stead.
50 * The specified fin set should not be used after the call!
52 * @param finset the fin set to convert.
53 * @return the new freeform fin set.
55 public static FreeformFinSet convertFinSet(FinSet finset) {
56 log.info("Converting " + finset.getComponentName() + " into freeform fin set");
57 final RocketComponent root = finset.getRoot();
58 FreeformFinSet freeform;
61 if (root instanceof Rocket) {
62 ((Rocket) root).freeze();
65 // Get fin set position and remove fin set
66 final RocketComponent parent = finset.getParent();
69 position = parent.getChildPosition(finset);
70 parent.removeChild(position);
76 // Create the freeform fin set
77 Coordinate[] finpoints = finset.getFinPoints();
79 freeform = new FreeformFinSet(finpoints);
80 } catch (IllegalFinPointException e) {
81 throw new BugException("Illegal fin points when converting existing fin to " +
82 "freeform fin, fin=" + finset + " points=" + Arrays.toString(finpoints),
86 // Copy component attributes
87 freeform.copyFrom(finset);
90 final String componentTypeName = finset.getComponentName();
91 final String name = freeform.getName();
93 if (name.startsWith(componentTypeName)) {
94 freeform.setName(freeform.getComponentName() +
95 name.substring(componentTypeName.length()));
98 // Add freeform fin set to parent
100 parent.addChild(freeform, position);
104 if (root instanceof Rocket) {
105 ((Rocket) root).thaw();
114 * Add a fin point between indices <code>index-1</code> and <code>index</code>.
115 * The point is placed at the midpoint of the current segment.
117 * @param index the fin point before which to add the new point.
119 public void addPoint(int index) {
120 double x0, y0, x1, y1;
122 x0 = points.get(index - 1).x;
123 y0 = points.get(index - 1).y;
124 x1 = points.get(index).x;
125 y1 = points.get(index).y;
127 points.add(index, new Coordinate((x0 + x1) / 2, (y0 + y1) / 2));
128 // adding a point within the segment affects neither mass nor aerodynamics
129 fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
134 * Remove the fin point with the given index. The first and last fin points
135 * cannot be removed, and will cause an <code>IllegalFinPointException</code>
138 * @param index the fin point index to remove
139 * @throws IllegalFinPointException if removing would result in invalid fin planform
141 @SuppressWarnings("unchecked")
142 public void removePoint(int index) throws IllegalFinPointException {
143 if (index == 0 || index == points.size() - 1) {
144 throw new IllegalFinPointException("cannot remove first or last point");
147 ArrayList<Coordinate> copy = (ArrayList<Coordinate>) this.points.clone();
152 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
156 public int getPointCount() {
157 return points.size();
160 public void setPoints(Coordinate[] points) throws IllegalFinPointException {
161 ArrayList<Coordinate> list = new ArrayList<Coordinate>(points.length);
162 for (Coordinate p : points) {
168 this.length = points[points.length - 1].x;
169 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
174 * Set the point at position <code>i</code> to coordinates (x,y).
176 * Note that this method enforces basic fin shape restrictions (non-negative y,
177 * first and last point locations) silently, but throws an
178 * <code>IllegalFinPointException</code> if the point causes fin segments to
181 * Moving of the first point in the X-axis is allowed, but this actually moves
182 * all of the other points the corresponding distance back.
184 * @param index the point index to modify.
185 * @param x the x-coordinate.
186 * @param y the y-coordinate.
187 * @throws IllegalFinPointException if the specified fin point would cause intersecting
190 public void setPoint(int index, double x, double y) throws IllegalFinPointException {
194 double x0, y0, x1, y1;
199 x = Math.min(x, points.get(points.size() - 1).x);
203 x1 = points.get(1).x;
204 y1 = points.get(1).y;
206 } else if (index == points.size() - 1) {
211 x0 = points.get(index - 1).x;
212 y0 = points.get(index - 1).y;
218 x0 = points.get(index - 1).x;
219 y0 = points.get(index - 1).y;
220 x1 = points.get(index + 1).x;
221 y1 = points.get(index + 1).y;
227 // Check for intersecting
228 double px0, py0, px1, py1;
231 for (int i = 1; i < points.size(); i++) {
232 px1 = points.get(i).x;
233 py1 = points.get(i).y;
235 if (i != index - 1 && i != index && i != index + 1) {
236 if (intersects(x0, y0, x, y, px0, py0, px1, py1)) {
237 throw new IllegalFinPointException("segments intersect");
240 if (i != index && i != index + 1 && i != index + 2) {
241 if (intersects(x, y, x1, y1, px0, py0, px1, py1)) {
242 throw new IllegalFinPointException("segments intersect");
252 System.out.println("Set point zero to x:" + x);
253 for (int i = 1; i < points.size(); i++) {
254 Coordinate c = points.get(i);
255 points.set(i, c.setX(c.x - x));
260 points.set(index, new Coordinate(x, y));
263 if (index == 0 || index == points.size() - 1) {
264 this.length = points.get(points.size() - 1).x;
266 fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
271 private boolean intersects(double ax0, double ay0, double ax1, double ay1,
272 double bx0, double by0, double bx1, double by1) {
274 double d = ((by1 - by0) * (ax1 - ax0) - (bx1 - bx0) * (ay1 - ay0));
276 double ua = ((bx1 - bx0) * (ay0 - by0) - (by1 - by0) * (ax0 - bx0)) / d;
277 double ub = ((ax1 - ax0) * (ay0 - by0) - (ay1 - ay0) * (ax0 - bx0)) / d;
279 return (ua >= 0) && (ua <= 1) && (ub >= 0) && (ub <= 1);
284 public Coordinate[] getFinPoints() {
285 return points.toArray(new Coordinate[0]);
289 public double getSpan() {
291 for (Coordinate c : points) {
299 public String getComponentName() {
300 return "Freeform fin set";
304 @SuppressWarnings("unchecked")
306 protected RocketComponent copyWithOriginalID() {
307 RocketComponent c = super.copyWithOriginalID();
308 ((FreeformFinSet) c).points = (ArrayList<Coordinate>) this.points.clone();
313 * Accept a visitor to this FreeformFinSet in the component hierarchy.
315 * @param theVisitor the visitor that will be called back with a reference to this FreeformFinSet
318 public void accept(ComponentVisitor theVisitor) {
319 theVisitor.visit(this);
322 private void validate(ArrayList<Coordinate> points) throws IllegalFinPointException {
323 final int n = points.size();
324 if (points.get(0).x != 0 || points.get(0).y != 0 ||
325 points.get(n - 1).x < 0 || points.get(n - 1).y != 0) {
326 throw new IllegalFinPointException("Start or end point illegal.");
328 for (int i = 0; i < n - 1; i++) {
329 for (int j = i + 2; j < n - 1; j++) {
330 if (intersects(points.get(i).x, points.get(i).y, points.get(i + 1).x, points.get(i + 1).y,
331 points.get(j).x, points.get(j).y, points.get(j + 1).x, points.get(j + 1).y)) {
332 throw new IllegalFinPointException("segments intersect");
335 if (points.get(i).z != 0) {
336 throw new IllegalFinPointException("z-coordinate not zero");