• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 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.jme3.math;
34 
35 import com.jme3.export.*;
36 import java.io.IOException;
37 
38 
39 /**
40  *
41  * <code>Rectangle</code> defines a finite plane within three dimensional space
42  * that is specified via three points (A, B, C). These three points define a
43  * triangle with the forth point defining the rectangle ((B + C) - A.
44  *
45  * @author Mark Powell
46  * @author Joshua Slack
47  */
48 
49 public final class Rectangle implements Savable, Cloneable, java.io.Serializable {
50 
51     static final long serialVersionUID = 1;
52 
53     private Vector3f a, b, c;
54 
55     /**
56      * Constructor creates a new <code>Rectangle</code> with no defined corners.
57      * A, B, and C must be set to define a valid rectangle.
58      *
59      */
Rectangle()60     public Rectangle() {
61         a = new Vector3f();
62         b = new Vector3f();
63         c = new Vector3f();
64     }
65 
66     /**
67      * Constructor creates a new <code>Rectangle</code> with defined A, B, and C
68      * points that define the area of the rectangle.
69      *
70      * @param a
71      *            the first corner of the rectangle.
72      * @param b
73      *            the second corner of the rectangle.
74      * @param c
75      *            the third corner of the rectangle.
76      */
Rectangle(Vector3f a, Vector3f b, Vector3f c)77     public Rectangle(Vector3f a, Vector3f b, Vector3f c) {
78         this.a = a;
79         this.b = b;
80         this.c = c;
81     }
82 
83     /**
84      * <code>getA</code> returns the first point of the rectangle.
85      *
86      * @return the first point of the rectangle.
87      */
getA()88     public Vector3f getA() {
89         return a;
90     }
91 
92     /**
93      * <code>setA</code> sets the first point of the rectangle.
94      *
95      * @param a
96      *            the first point of the rectangle.
97      */
setA(Vector3f a)98     public void setA(Vector3f a) {
99         this.a = a;
100     }
101 
102     /**
103      * <code>getB</code> returns the second point of the rectangle.
104      *
105      * @return the second point of the rectangle.
106      */
getB()107     public Vector3f getB() {
108         return b;
109     }
110 
111     /**
112      * <code>setB</code> sets the second point of the rectangle.
113      *
114      * @param b
115      *            the second point of the rectangle.
116      */
setB(Vector3f b)117     public void setB(Vector3f b) {
118         this.b = b;
119     }
120 
121     /**
122      * <code>getC</code> returns the third point of the rectangle.
123      *
124      * @return the third point of the rectangle.
125      */
getC()126     public Vector3f getC() {
127         return c;
128     }
129 
130     /**
131      * <code>setC</code> sets the third point of the rectangle.
132      *
133      * @param c
134      *            the third point of the rectangle.
135      */
setC(Vector3f c)136     public void setC(Vector3f c) {
137         this.c = c;
138     }
139 
140     /**
141      * <code>random</code> returns a random point within the plane defined by:
142      * A, B, C, and (B + C) - A.
143      *
144      * @return a random point within the rectangle.
145      */
random()146     public Vector3f random() {
147         return random(null);
148     }
149 
150     /**
151      * <code>random</code> returns a random point within the plane defined by:
152      * A, B, C, and (B + C) - A.
153      *
154      * @param result
155      *            Vector to store result in
156      * @return a random point within the rectangle.
157      */
random(Vector3f result)158     public Vector3f random(Vector3f result) {
159         if (result == null) {
160             result = new Vector3f();
161         }
162 
163         float s = FastMath.nextRandomFloat();
164         float t = FastMath.nextRandomFloat();
165 
166         float aMod = 1.0f - s - t;
167         result.set(a.mult(aMod).addLocal(b.mult(s).addLocal(c.mult(t))));
168         return result;
169     }
170 
write(JmeExporter e)171     public void write(JmeExporter e) throws IOException {
172         OutputCapsule capsule = e.getCapsule(this);
173         capsule.write(a, "a", Vector3f.ZERO);
174         capsule.write(b, "b", Vector3f.ZERO);
175         capsule.write(c, "c", Vector3f.ZERO);
176     }
177 
read(JmeImporter e)178     public void read(JmeImporter e) throws IOException {
179         InputCapsule capsule = e.getCapsule(this);
180         a = (Vector3f) capsule.readSavable("a", Vector3f.ZERO.clone());
181         b = (Vector3f) capsule.readSavable("b", Vector3f.ZERO.clone());
182         c = (Vector3f) capsule.readSavable("c", Vector3f.ZERO.clone());
183     }
184 
185     @Override
clone()186     public Rectangle clone() {
187         try {
188             Rectangle r = (Rectangle) super.clone();
189             r.a = a.clone();
190             r.b = b.clone();
191             r.c = c.clone();
192             return r;
193         } catch (CloneNotSupportedException e) {
194             throw new AssertionError();
195         }
196     }
197 }
198