Add Star grain
[sw/motorsim] / src / com / billkuker / rocketry / motorsim / grain / Star.java
1 package com.billkuker.rocketry.motorsim.grain;\r
2 \r
3 import java.awt.Shape;\r
4 import java.awt.geom.Area;\r
5 import java.awt.geom.Ellipse2D;\r
6 import java.awt.geom.GeneralPath;\r
7 import java.beans.PropertyVetoException;\r
8 \r
9 import javax.measure.quantity.Length;\r
10 import javax.measure.unit.SI;\r
11 \r
12 import org.jscience.physics.amount.Amount;\r
13 \r
14 import com.billkuker.rocketry.motorsim.Validating;\r
15 import com.billkuker.rocketry.motorsim.grain.util.BurningShape;\r
16 import com.billkuker.rocketry.motorsim.grain.util.ExtrudedShapeGrain;\r
17 import com.billkuker.rocketry.motorsim.visual.Editor;\r
18 import com.billkuker.rocketry.motorsim.visual.GrainPanel;\r
19 \r
20 public class Star extends ExtrudedShapeGrain implements Validating {\r
21         private Amount<Length> oD = Amount.valueOf(30, SI.MILLIMETER);\r
22         private Amount<Length> iD = Amount.valueOf(5, SI.MILLIMETER);\r
23         private Amount<Length> pD = Amount.valueOf(15, SI.MILLIMETER);\r
24         private int pointCount = 5;\r
25         \r
26         public Star(){\r
27                 try {\r
28                         setLength(Amount.valueOf(70, SI.MILLIMETER));\r
29                 } catch (PropertyVetoException e) {\r
30                         e.printStackTrace();\r
31                 }\r
32                 generateGeometry();\r
33         }\r
34         \r
35         private void generateGeometry(){\r
36                 double odmm = oD.doubleValue(SI.MILLIMETER);\r
37                 double idmm = iD.doubleValue(SI.MILLIMETER)/2.0;\r
38                 double pdmm = pD.doubleValue(SI.MILLIMETER)/2.0;\r
39                 \r
40                 xsection = new BurningShape();\r
41                 Shape outside = new Ellipse2D.Double(-odmm/2, -odmm/2, odmm, odmm);\r
42                 xsection.add(outside);\r
43                 xsection.inhibit(outside);\r
44                 //xsection.subtract(new Ellipse2D.Double(-idmm/2, -idmm/2, idmm, idmm));\r
45                 webThickness = null;\r
46                 \r
47                 GeneralPath p = new GeneralPath();\r
48                 double theta = 0;\r
49                 double dTheta = (2.0 * Math.PI) / ( pointCount * 2.0 );\r
50                 p.moveTo(0, idmm);\r
51                 for ( int i = 0; i < pointCount; i++ ){\r
52                         theta += dTheta;\r
53                         p.lineTo(pdmm*Math.sin(theta), pdmm*Math.cos(theta));\r
54                         theta += dTheta;\r
55                         p.lineTo(idmm*Math.sin(theta), idmm*Math.cos(theta));\r
56                 }\r
57                 p.closePath();\r
58                 xsection.subtract(new Area(p));\r
59         }\r
60         \r
61         public Amount<Length> getOD() {\r
62                 return oD;\r
63         }\r
64 \r
65         public void setOD(Amount<Length> od) throws PropertyVetoException {\r
66                 if (od.equals(this.oD))\r
67                         return;\r
68                 this.oD = od;\r
69                 generateGeometry();\r
70         }\r
71 \r
72         public Amount<Length> getID() {\r
73                 return iD;\r
74         }\r
75 \r
76         public void setID(Amount<Length> id) throws PropertyVetoException {\r
77                 if (id.equals(this.iD))\r
78                         return;\r
79                 iD = id;\r
80                 generateGeometry();\r
81         }\r
82         \r
83         public Amount<Length> getPointDiameter() {\r
84                 return pD;\r
85         }\r
86 \r
87         public void setPointDiameter(Amount<Length> pd) throws PropertyVetoException {\r
88                 if (pd.equals(this.pD))\r
89                         return;\r
90                 pD = pd;\r
91                 generateGeometry();\r
92         }\r
93         \r
94         public int getPointCount(){\r
95                 return pointCount;\r
96         }\r
97         \r
98         public void setPointCount(final int points){\r
99                 pointCount = points;\r
100                 generateGeometry();\r
101         }\r
102         \r
103         \r
104         public static void main(String args[]) throws Exception {\r
105                 Star e = new Star();\r
106                 new Editor(e).showAsWindow();\r
107                 new GrainPanel(e).showAsWindow();\r
108         }\r
109         \r
110         @Override\r
111         public void validate() throws ValidationException{\r
112                 if ( iD.equals(Amount.ZERO) )\r
113                         throw new ValidationException(this, "Invalid iD");\r
114                 if ( oD.equals(Amount.ZERO) )\r
115                         throw new ValidationException(this, "Invalid oD");\r
116                 if ( getLength().equals(Amount.ZERO) )\r
117                         throw new ValidationException(this, "Invalid Length");\r
118                 if ( iD.isGreaterThan(oD) )\r
119                         throw new ValidationException(this, "iD > oD");         \r
120         }\r
121 }\r