Initial commit
[debian/openrocket] / src / net / sf / openrocket / rocketcomponent / ClusterConfiguration.java
1 package net.sf.openrocket.rocketcomponent;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7
8 /**
9  * Class that defines different cluster configurations available for the InnerTube.
10  * The class is immutable, and all the constructors are private.  Therefore the only
11  * available cluster configurations are those available in the static fields.
12  * 
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public class ClusterConfiguration {
16         // Helper vars
17         private static final double R5 = 1.0/(2*Math.sin(2*Math.PI/10));
18         private static final double SQRT2 = Math.sqrt(2);
19         private static final double SQRT3 = Math.sqrt(3);
20
21         /** A single motor */
22         public static final ClusterConfiguration SINGLE = new ClusterConfiguration("single", 0,0);
23
24         /** Definitions of cluster configurations.  Do not modify array. */
25         public static final ClusterConfiguration[] CONFIGURATIONS = {
26                 // Single row
27                 SINGLE,
28                 new ClusterConfiguration("double", -0.5,0, 0.5,0),
29                 new ClusterConfiguration("3-row", -1.0,0, 0.0,0, 1.0,0),
30                 new ClusterConfiguration("4-row", -1.5,0, -0.5,0, 0.5,0, 1.5,0),
31                 
32                 // Ring of tubes
33                 new ClusterConfiguration("3-ring", -0.5,-1.0/(2*SQRT3),
34                                                                   0.5,-1.0/(2*SQRT3),
35                                                                     0, 1.0/SQRT3),
36                 new ClusterConfiguration("4-ring", -0.5,0.5, 0.5,0.5, 0.5,-0.5, -0.5,-0.5),
37                 new ClusterConfiguration("5-ring", 0,R5,
38                                                                  R5*Math.sin(2*Math.PI/5),R5*Math.cos(2*Math.PI/5),
39                                                                  R5*Math.sin(2*Math.PI*2/5),R5*Math.cos(2*Math.PI*2/5),
40                                                                  R5*Math.sin(2*Math.PI*3/5),R5*Math.cos(2*Math.PI*3/5),
41                                                                  R5*Math.sin(2*Math.PI*4/5),R5*Math.cos(2*Math.PI*4/5)),
42                 new ClusterConfiguration("6-ring", 0,1, SQRT3/2,0.5, SQRT3/2,-0.5,
43                                                                  0,-1, -SQRT3/2,-0.5, -SQRT3/2,0.5),
44                 
45                 // Centered with ring
46                 new ClusterConfiguration("3-star", 0,0, 0,1, SQRT3/2,-0.5, -SQRT3/2,-0.5),
47                 new ClusterConfiguration("4-star", 0,0, -1/SQRT2,1/SQRT2, 1/SQRT2,1/SQRT2,
48                                                                  1/SQRT2,-1/SQRT2, -1/SQRT2,-1/SQRT2),
49                 new ClusterConfiguration("5-star", 0,0, 0,1, 
50                                                                  Math.sin(2*Math.PI/5),Math.cos(2*Math.PI/5),
51                                                                  Math.sin(2*Math.PI*2/5),Math.cos(2*Math.PI*2/5),
52                                                                  Math.sin(2*Math.PI*3/5),Math.cos(2*Math.PI*3/5),
53                                                                  Math.sin(2*Math.PI*4/5),Math.cos(2*Math.PI*4/5)),
54                 new ClusterConfiguration("6-star", 0,0, 0,1, SQRT3/2,0.5, SQRT3/2,-0.5,
55                                                                  0,-1, -SQRT3/2,-0.5, -SQRT3/2,0.5)
56         };
57         
58         
59         private final List<Double> points;
60         private final String xmlName;
61         
62         private ClusterConfiguration(String xmlName, double... points) {
63                 this.xmlName = xmlName;
64                 if (points.length == 0 || points.length%2 == 1) {
65                         throw new IllegalArgumentException("Illegal number of points specified: "+
66                                         points.length);
67                 }
68                 List<Double> l = new ArrayList<Double>(points.length);
69                 for (double d: points)
70                         l.add(d);
71                 
72                 this.points = Collections.unmodifiableList(l);
73         }
74         
75         public String getXMLName() {
76                 return xmlName;
77         }
78         
79         public int getClusterCount() {
80                 return points.size()/2;
81         }
82         
83         public List<Double> getPoints() {
84                 return points;  // Unmodifiable
85         }
86         
87         /**
88          * Return the points rotated by <code>rotation</code> radians.
89          * @param rotation  Rotation amount.
90          */
91         public List<Double> getPoints(double rotation) {
92                 double cos = Math.cos(rotation);
93                 double sin = Math.sin(rotation);
94                 List<Double> ret = new ArrayList<Double>(points.size());
95                 for (int i=0; i<points.size()/2; i++) {
96                         double x = points.get(2*i);
97                         double y = points.get(2*i+1);
98                         ret.add( x*cos + y*sin);
99                         ret.add(-x*sin + y*cos);
100                 }
101                 return ret;
102         }
103 }