OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / math / Rectangle.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  * 
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.
49  * 
50  * @author Mark Powell
51  * @author Joshua Slack
52  */
53
54 public class Rectangle implements Serializable, Savable, Cloneable {
55     private static final long serialVersionUID = 1L;
56
57     private Vector3f a, b, c;
58
59     /**
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.
62      * 
63      */
64     public Rectangle() {
65         a = new Vector3f();
66         b = new Vector3f();
67         c = new Vector3f();
68     }
69
70     /**
71      * Constructor creates a new <code>Rectangle</code> with defined A, B, and C
72      * points that define the area of the rectangle.
73      * 
74      * @param a
75      *            the first corner of the rectangle.
76      * @param b
77      *            the second corner of the rectangle.
78      * @param c
79      *            the third corner of the rectangle.
80      */
81     public Rectangle(Vector3f a, Vector3f b, Vector3f c) {
82         this.a = a;
83         this.b = b;
84         this.c = c;
85     }
86
87     /**
88      * <code>getA</code> returns the first point of the rectangle.
89      * 
90      * @return the first point of the rectangle.
91      */
92     public Vector3f getA() {
93         return a;
94     }
95
96     /**
97      * <code>setA</code> sets the first point of the rectangle.
98      * 
99      * @param a
100      *            the first point of the rectangle.
101      */
102     public void setA(Vector3f a) {
103         this.a = a;
104     }
105
106     /**
107      * <code>getB</code> returns the second point of the rectangle.
108      * 
109      * @return the second point of the rectangle.
110      */
111     public Vector3f getB() {
112         return b;
113     }
114
115     /**
116      * <code>setB</code> sets the second point of the rectangle.
117      * 
118      * @param b
119      *            the second point of the rectangle.
120      */
121     public void setB(Vector3f b) {
122         this.b = b;
123     }
124
125     /**
126      * <code>getC</code> returns the third point of the rectangle.
127      * 
128      * @return the third point of the rectangle.
129      */
130     public Vector3f getC() {
131         return c;
132     }
133
134     /**
135      * <code>setC</code> sets the third point of the rectangle.
136      * 
137      * @param c
138      *            the third point of the rectangle.
139      */
140     public void setC(Vector3f c) {
141         this.c = c;
142     }
143
144     /**
145      * <code>random</code> returns a random point within the plane defined by:
146      * A, B, C, and (B + C) - A.
147      * 
148      * @return a random point within the rectangle.
149      */
150     public Vector3f random() {
151         return random(null);
152     }
153
154     /**
155      * <code>random</code> returns a random point within the plane defined by:
156      * A, B, C, and (B + C) - A.
157      * 
158      * @param result
159      *            Vector to store result in
160      * @return a random point within the rectangle.
161      */
162     public Vector3f random(Vector3f result) {
163         if (result == null) {
164             result = new Vector3f();
165         }
166
167         float s = FastMath.nextRandomFloat();
168         float t = FastMath.nextRandomFloat();
169
170         float aMod = 1.0f - s - t;
171         result.set(a.mult(aMod).addLocal(b.mult(s).addLocal(c.mult(t))));
172         return result;
173     }
174
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);
180     }
181
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());
187     }
188
189     public Class<? extends Rectangle> getClassTag() {
190         return this.getClass();
191     }
192     
193     @Override
194     public Rectangle clone() {
195         try {
196             Rectangle r = (Rectangle) super.clone();
197             r.a = a.clone();
198             r.b = b.clone();
199             r.c = c.clone();
200             return r;
201         } catch (CloneNotSupportedException e) {
202             throw new AssertionError();
203         }
204     }
205 }