2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
35 import java.io.IOException;
36 import java.io.Serializable;
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;
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.
49 * @author Andrzej Kapolka
50 * @author Joshua Slack
53 public class Ring implements Serializable, Savable, Cloneable {
54 private static final long serialVersionUID = 1L;
56 private Vector3f center, up;
57 private float innerRadius, outerRadius;
58 private transient static Vector3f b1 = new Vector3f(), b2 = new Vector3f();
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).
66 center = new Vector3f();
67 up = Vector3f.UNIT_Y.clone();
73 * Constructor creates a new <code>Ring</code> with defined center point,
74 * up vector, and inner and outer radii.
77 * the center of the ring.
79 * the unit up vector defining the ring's orientation.
81 * the ring's inner radius.
83 * the ring's outer radius.
85 public Ring(Vector3f center, Vector3f up, float innerRadius,
89 this.innerRadius = innerRadius;
90 this.outerRadius = outerRadius;
94 * <code>getCenter</code> returns the center of the ring.
96 * @return the center of the ring.
98 public Vector3f getCenter() {
103 * <code>setCenter</code> sets the center of the ring.
106 * the center of the ring.
108 public void setCenter(Vector3f center) {
109 this.center = center;
113 * <code>getUp</code> returns the ring's up vector.
115 * @return the ring's up vector.
117 public Vector3f getUp() {
122 * <code>setUp</code> sets the ring's up vector.
125 * the ring's up vector.
127 public void setUp(Vector3f up) {
132 * <code>getInnerRadius</code> returns the ring's inner radius.
134 * @return the ring's inner radius.
136 public float getInnerRadius() {
141 * <code>setInnerRadius</code> sets the ring's inner radius.
144 * the ring's inner radius.
146 public void setInnerRadius(float innerRadius) {
147 this.innerRadius = innerRadius;
151 * <code>getOuterRadius</code> returns the ring's outer radius.
153 * @return the ring's outer radius.
155 public float getOuterRadius() {
160 * <code>setOuterRadius</code> sets the ring's outer radius.
163 * the ring's outer radius.
165 public void setOuterRadius(float outerRadius) {
166 this.outerRadius = outerRadius;
171 * <code>random</code> returns a random point within the ring.
173 * @return a random point within the ring.
175 public Vector3f random() {
181 * <code>random</code> returns a random point within the ring.
183 * @param result Vector to store result in
184 * @return a random point within the ring.
186 public Vector3f random(Vector3f result) {
187 if (result == null) {
188 result = new Vector3f();
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
197 up.cross(Vector3f.UNIT_X, b1);
198 if (b1.lengthSquared() < FastMath.FLT_EPSILON) {
199 up.cross(Vector3f.UNIT_Y, b1);
203 result.set(b1).multLocal(r * FastMath.cos(theta)).addLocal(center);
204 result.scaleAdd(r * FastMath.sin(theta), b2, result);
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);
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);
226 public Class<? extends Ring> getClassTag() {
227 return this.getClass();
231 public Ring clone() {
233 Ring r = (Ring) super.clone();
234 r.center = center.clone();
237 } catch (CloneNotSupportedException e) {
238 throw new AssertionError();