OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / math / Ring.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
17  *   may be used to endorse or promote products derived from this software 
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 package com.jme.math;
34
35 import java.io.IOException;
36 import java.io.Serializable;
37
38 import com.jme.util.export.InputCapsule;
39 import com.jme.util.export.JMEExporter;
40 import com.jme.util.export.JMEImporter;
41 import com.jme.util.export.OutputCapsule;
42 import com.jme.util.export.Savable;
43
44 /**
45  * <code>Ring</code> defines a flat ring or disk within three dimensional
46  * space that is specified via the ring's center point, an up vector, an inner
47  * radius, and an outer radius.
48  * 
49  * @author Andrzej Kapolka
50  * @author Joshua Slack
51  */
52
53 public class Ring implements Serializable, Savable, Cloneable {
54     private static final long serialVersionUID = 1L;
55     
56     private Vector3f center, up;
57     private float innerRadius, outerRadius;
58     private transient static Vector3f b1 = new Vector3f(), b2 = new Vector3f();
59
60     /**
61      * Constructor creates a new <code>Ring</code> lying on the XZ plane,
62      * centered at the origin, with an inner radius of zero and an outer radius
63      * of one (a unit disk).
64      */
65     public Ring() {
66         center = new Vector3f();
67         up = Vector3f.UNIT_Y.clone();
68         innerRadius = 0f;
69         outerRadius = 1f;
70     }
71
72     /**
73      * Constructor creates a new <code>Ring</code> with defined center point,
74      * up vector, and inner and outer radii.
75      * 
76      * @param center
77      *            the center of the ring.
78      * @param up
79      *            the unit up vector defining the ring's orientation.
80      * @param innerRadius
81      *            the ring's inner radius.
82      * @param outerRadius
83      *            the ring's outer radius.
84      */
85     public Ring(Vector3f center, Vector3f up, float innerRadius,
86             float outerRadius) {
87         this.center = center;
88         this.up = up;
89         this.innerRadius = innerRadius;
90         this.outerRadius = outerRadius;
91     }
92
93     /**
94      * <code>getCenter</code> returns the center of the ring.
95      * 
96      * @return the center of the ring.
97      */
98     public Vector3f getCenter() {
99         return center;
100     }
101
102     /**
103      * <code>setCenter</code> sets the center of the ring.
104      * 
105      * @param center
106      *            the center of the ring.
107      */
108     public void setCenter(Vector3f center) {
109         this.center = center;
110     }
111
112     /**
113      * <code>getUp</code> returns the ring's up vector.
114      * 
115      * @return the ring's up vector.
116      */
117     public Vector3f getUp() {
118         return up;
119     }
120
121     /**
122      * <code>setUp</code> sets the ring's up vector.
123      * 
124      * @param up
125      *            the ring's up vector.
126      */
127     public void setUp(Vector3f up) {
128         this.up = up;
129     }
130
131     /**
132      * <code>getInnerRadius</code> returns the ring's inner radius.
133      * 
134      * @return the ring's inner radius.
135      */
136     public float getInnerRadius() {
137         return innerRadius;
138     }
139
140     /**
141      * <code>setInnerRadius</code> sets the ring's inner radius.
142      * 
143      * @param innerRadius
144      *            the ring's inner radius.
145      */
146     public void setInnerRadius(float innerRadius) {
147         this.innerRadius = innerRadius;
148     }
149
150     /**
151      * <code>getOuterRadius</code> returns the ring's outer radius.
152      * 
153      * @return the ring's outer radius.
154      */
155     public float getOuterRadius() {
156         return outerRadius;
157     }
158
159     /**
160      * <code>setOuterRadius</code> sets the ring's outer radius.
161      * 
162      * @param outerRadius
163      *            the ring's outer radius.
164      */
165     public void setOuterRadius(float outerRadius) {
166         this.outerRadius = outerRadius;
167     }
168
169     /**
170      * 
171      * <code>random</code> returns a random point within the ring.
172      * 
173      * @return a random point within the ring.
174      */
175     public Vector3f random() {
176         return random(null);
177     }
178
179     /**
180      * 
181      * <code>random</code> returns a random point within the ring.
182      * 
183      * @param result Vector to store result in
184      * @return a random point within the ring.
185      */
186     public Vector3f random(Vector3f result) {
187         if (result == null) {
188             result = new Vector3f();
189         }
190         
191         // compute a random radius according to the ring area distribution
192         float inner2 = innerRadius * innerRadius, outer2 = outerRadius
193                 * outerRadius, r = FastMath.sqrt(inner2
194                 + FastMath.nextRandomFloat() * (outer2 - inner2)), theta = FastMath
195                 .nextRandomFloat()
196                 * FastMath.TWO_PI;
197         up.cross(Vector3f.UNIT_X, b1);
198         if (b1.lengthSquared() < FastMath.FLT_EPSILON) {
199             up.cross(Vector3f.UNIT_Y, b1);
200         }
201         b1.normalizeLocal();
202         up.cross(b1, b2);
203         result.set(b1).multLocal(r * FastMath.cos(theta)).addLocal(center);
204         result.scaleAdd(r * FastMath.sin(theta), b2, result);
205         return result;
206     }
207
208     public void write(JMEExporter e) throws IOException {
209         OutputCapsule capsule = e.getCapsule(this);
210         capsule.write(center, "center", Vector3f.ZERO);
211         capsule.write(up, "up", Vector3f.UNIT_Z);
212         capsule.write(innerRadius, "innerRadius", 0f);
213         capsule.write(outerRadius, "outerRadius", 1f);
214     }
215
216     public void read(JMEImporter e) throws IOException {
217         InputCapsule capsule = e.getCapsule(this);
218         center = (Vector3f) capsule.readSavable("center",
219                 Vector3f.ZERO.clone());
220         up = (Vector3f) capsule
221                 .readSavable("up", Vector3f.UNIT_Z.clone());
222         innerRadius = capsule.readFloat("innerRadius", 0f);
223         outerRadius = capsule.readFloat("outerRadius", 1f);
224     }
225
226     public Class<? extends Ring> getClassTag() {
227         return this.getClass();
228     }
229
230     @Override
231     public Ring clone() {
232         try {
233             Ring r = (Ring) super.clone();
234             r.center = center.clone();
235             r.up = up.clone();
236             return r;
237         } catch (CloneNotSupportedException e) {
238             throw new AssertionError();
239         }
240     }
241 }