]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Vector3f.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Vector3f.java
1 /*
2  * $RCSfile: Vector3f.java,v $
3  *
4  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 only, as
9  * published by the Free Software Foundation.  Sun designates this
10  * particular file as subject to the "Classpath" exception as provided
11  * by Sun in the LICENSE file that accompanied this code.
12  *
13  * This code is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * version 2 for more details (a copy is included in the LICENSE file that
17  * accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License version
20  * 2 along with this work; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  *
27  * $Revision: 1.5 $
28  * $Date: 2008/02/28 20:18:51 $
29  * $State: Exp $
30  */
31
32 package javax.vecmath;
33
34 import java.lang.Math;
35
36 /**
37  * A 3-element vector that is represented by single-precision floating point 
38  * x,y,z coordinates.  If this value represents a normal, then it should
39  * be normalized.
40  *
41  */
42 public class Vector3f extends Tuple3f implements java.io.Serializable {
43
44     // Combatible with 1.1
45     static final long serialVersionUID = -7031930069184524614L;
46
47     /**
48      * Constructs and initializes a Vector3f from the specified xyz coordinates.
49      * @param x the x coordinate
50      * @param y the y coordinate
51      * @param z the z coordinate
52      */
53     public Vector3f(float x, float y, float z)
54     {
55         super(x,y,z);
56     }
57
58
59     /**
60      * Constructs and initializes a Vector3f from the array of length 3.
61      * @param v the array of length 3 containing xyz in order
62      */
63     public Vector3f(float[] v)
64     {
65        super(v);
66     }
67
68
69     /**
70      * Constructs and initializes a Vector3f from the specified Vector3f.
71      * @param v1 the Vector3f containing the initialization x y z data
72      */
73     public Vector3f(Vector3f v1)
74     {
75        super(v1);
76     }
77
78
79     /**
80      * Constructs and initializes a Vector3f from the specified Vector3d.
81      * @param v1 the Vector3d containing the initialization x y z data
82      */
83     public Vector3f(Vector3d v1)
84     {
85        super(v1);
86     }
87
88
89     /**
90      * Constructs and initializes a Vector3f from the specified Tuple3f.
91      * @param t1 the Tuple3f containing the initialization x y z data
92      */  
93     public Vector3f(Tuple3f t1) {
94        super(t1);
95     }
96
97
98     /**
99      * Constructs and initializes a Vector3f from the specified Tuple3d.
100      * @param t1 the Tuple3d containing the initialization x y z data
101      */  
102     public Vector3f(Tuple3d t1) {
103        super(t1);
104     }
105
106
107     /**
108      * Constructs and initializes a Vector3f to (0,0,0).
109      */
110     public Vector3f()
111     {
112         super();
113     }
114
115
116    /**
117      * Returns the squared length of this vector.
118      * @return the squared length of this vector
119      */
120     public final float lengthSquared()
121     {
122         return (this.x*this.x + this.y*this.y + this.z*this.z);
123     }
124
125     /**
126      * Returns the length of this vector.
127      * @return the length of this vector
128      */
129     public final float length()
130     {
131         return (float)
132              Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
133     }
134
135
136   /**
137      * Sets this vector to be the vector cross product of vectors v1 and v2.
138      * @param v1 the first vector
139      * @param v2 the second vector
140      */
141     public final void cross(Vector3f v1, Vector3f v2)
142     {
143         float x,y;
144
145         x = v1.y*v2.z - v1.z*v2.y;
146         y = v2.x*v1.z - v2.z*v1.x;
147         this.z = v1.x*v2.y - v1.y*v2.x;
148         this.x = x;
149         this.y = y;
150     }
151
152  /**
153    * Computes the dot product of this vector and vector v1.
154    * @param v1 the other vector
155    * @return the dot product of this vector and v1
156    */
157   public final float dot(Vector3f v1)
158     {
159       return (this.x*v1.x + this.y*v1.y + this.z*v1.z);
160     }
161
162    /**
163      * Sets the value of this vector to the normalization of vector v1.
164      * @param v1 the un-normalized vector
165      */
166     public final void normalize(Vector3f v1)
167     {
168         float norm;
169
170         norm = (float) (1.0/Math.sqrt(v1.x*v1.x + v1.y*v1.y + v1.z*v1.z));
171         this.x = v1.x*norm;
172         this.y = v1.y*norm;
173         this.z = v1.z*norm;
174     }
175
176     /**
177      * Normalizes this vector in place.
178      */
179     public final void normalize()
180     {
181         float norm;
182
183         norm = (float)
184                (1.0/Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z));
185         this.x *= norm;
186         this.y *= norm;
187         this.z *= norm;
188     }
189
190
191   /** 
192     *   Returns the angle in radians between this vector and the vector
193     *   parameter; the return value is constrained to the range [0,PI]. 
194     *   @param v1    the other vector 
195     *   @return   the angle in radians in the range [0,PI] 
196     */   
197    public final float angle(Vector3f v1) 
198    { 
199       double vDot = this.dot(v1) / ( this.length()*v1.length() );
200       if( vDot < -1.0) vDot = -1.0;
201       if( vDot >  1.0) vDot =  1.0;
202       return((float) (Math.acos( vDot )));
203    } 
204
205 }