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;
46 * <code>Rectangle</code> defines a finite plane within three dimensional space
47 * that is specified via three points (A, B, C). These three points define a
48 * triangle with the forth point defining the rectangle ((B + C) - A.
51 * @author Joshua Slack
54 public class Rectangle implements Serializable, Savable, Cloneable {
55 private static final long serialVersionUID = 1L;
57 private Vector3f a, b, c;
60 * Constructor creates a new <code>Rectangle</code> with no defined corners.
61 * A, B, and C must be set to define a valid rectangle.
71 * Constructor creates a new <code>Rectangle</code> with defined A, B, and C
72 * points that define the area of the rectangle.
75 * the first corner of the rectangle.
77 * the second corner of the rectangle.
79 * the third corner of the rectangle.
81 public Rectangle(Vector3f a, Vector3f b, Vector3f c) {
88 * <code>getA</code> returns the first point of the rectangle.
90 * @return the first point of the rectangle.
92 public Vector3f getA() {
97 * <code>setA</code> sets the first point of the rectangle.
100 * the first point of the rectangle.
102 public void setA(Vector3f a) {
107 * <code>getB</code> returns the second point of the rectangle.
109 * @return the second point of the rectangle.
111 public Vector3f getB() {
116 * <code>setB</code> sets the second point of the rectangle.
119 * the second point of the rectangle.
121 public void setB(Vector3f b) {
126 * <code>getC</code> returns the third point of the rectangle.
128 * @return the third point of the rectangle.
130 public Vector3f getC() {
135 * <code>setC</code> sets the third point of the rectangle.
138 * the third point of the rectangle.
140 public void setC(Vector3f c) {
145 * <code>random</code> returns a random point within the plane defined by:
146 * A, B, C, and (B + C) - A.
148 * @return a random point within the rectangle.
150 public Vector3f random() {
155 * <code>random</code> returns a random point within the plane defined by:
156 * A, B, C, and (B + C) - A.
159 * Vector to store result in
160 * @return a random point within the rectangle.
162 public Vector3f random(Vector3f result) {
163 if (result == null) {
164 result = new Vector3f();
167 float s = FastMath.nextRandomFloat();
168 float t = FastMath.nextRandomFloat();
170 float aMod = 1.0f - s - t;
171 result.set(a.mult(aMod).addLocal(b.mult(s).addLocal(c.mult(t))));
175 public void write(JMEExporter e) throws IOException {
176 OutputCapsule capsule = e.getCapsule(this);
177 capsule.write(a, "a", Vector3f.ZERO);
178 capsule.write(b, "b", Vector3f.ZERO);
179 capsule.write(c, "c", Vector3f.ZERO);
182 public void read(JMEImporter e) throws IOException {
183 InputCapsule capsule = e.getCapsule(this);
184 a = (Vector3f) capsule.readSavable("a", Vector3f.ZERO.clone());
185 b = (Vector3f) capsule.readSavable("b", Vector3f.ZERO.clone());
186 c = (Vector3f) capsule.readSavable("c", Vector3f.ZERO.clone());
189 public Class<? extends Rectangle> getClassTag() {
190 return this.getClass();
194 public Rectangle clone() {
196 Rectangle r = (Rectangle) super.clone();
201 } catch (CloneNotSupportedException e) {
202 throw new AssertionError();