]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Vector4f.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Vector4f.java
1 /*
2  * $RCSfile: Vector4f.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.6 $
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 4-element vector represented by single-precision floating point x,y,z,w 
38  * coordinates.
39  *
40  */
41 public class Vector4f extends Tuple4f implements java.io.Serializable {
42
43   // Compatible with 1.1
44   static final long serialVersionUID = 8749319902347760659L;
45
46   /**
47    * Constructs and initializes a Vector4f from the specified xyzw coordinates.
48    * @param x the x coordinate
49    * @param y the y coordinate
50    * @param z the z coordinate
51    * @param w the w coordinate
52    */
53   public Vector4f(float x, float y, float z, float w)
54   {
55        super(x,y,z,w);
56   }
57
58
59   /**
60    * Constructs and initializes a Vector4f from the array of length 4. 
61    * @param v the array of length 4 containing xyzw in order
62    */
63   public Vector4f(float[] v)
64   {
65      super(v);
66   }
67
68
69   /**
70    * Constructs and initializes a Vector4f from the specified Vector4f.
71    * @param v1 the Vector4f containing the initialization x y z w data
72    */
73   public Vector4f(Vector4f v1)
74   {
75       super(v1);
76   }
77
78
79   /**
80    * Constructs and initializes a Vector4f from the specified Vector4d.
81    * @param v1 the Vector4d containing the initialization x y z w data
82    */
83   public Vector4f(Vector4d v1)
84   {
85       super(v1);
86   }
87
88
89     /**
90      * Constructs and initializes a Vector4f from the specified Tuple4f.
91      * @param t1 the Tuple4f containing the initialization x y z w data
92      */  
93     public Vector4f(Tuple4f t1) 
94     {
95        super(t1);
96     }
97
98
99     /**
100      * Constructs and initializes a Vector4f from the specified Tuple4d.
101      * @param t1 the Tuple4d containing the initialization x y z w data 
102      */  
103     public Vector4f(Tuple4d t1) 
104     {
105        super(t1); 
106     }
107
108
109     /**
110      * Constructs and initializes a Vector4f from the specified Tuple3f.
111      * The x,y,z components of this vector are set to the corresponding
112      * components of tuple t1.  The w component of this vector
113      * is set to 0.
114      * @param t1 the tuple to be copied
115      *
116      * @since vecmath 1.2
117      */
118     public Vector4f(Tuple3f t1) {
119         super(t1.x, t1.y, t1.z, 0.0f);
120     }
121
122
123   /**
124    * Constructs and initializes a Vector4f to (0,0,0,0).
125    */
126   public Vector4f()
127   {
128       super();
129   }
130
131
132     /**
133      * Sets the x,y,z components of this vector to the corresponding
134      * components of tuple t1.  The w component of this vector
135      * is set to 0.
136      * @param t1 the tuple to be copied
137      *
138      * @since vecmath 1.2
139      */
140     public final void set(Tuple3f t1) {
141         this.x = t1.x;
142         this.y = t1.y;
143         this.z = t1.z;
144         this.w = 0.0f;
145     }
146
147
148  /**
149    * Returns the length of this vector.
150    * @return the length of this vector as a float
151    */
152   public final float length()
153   {
154     return
155       (float) Math.sqrt(this.x*this.x + this.y*this.y +
156                         this.z*this.z + this.w*this.w);
157   }
158
159   /**
160    * Returns the squared length of this vector
161    * @return the squared length of this vector as a float
162    */
163   public final float lengthSquared()
164   {
165     return (this.x*this.x + this.y*this.y +
166             this.z*this.z + this.w*this.w);
167   }
168
169   /**
170    * returns the dot product of this vector and v1
171    * @param v1 the other vector
172    * @return the dot product of this vector and v1
173    */
174   public final float dot(Vector4f v1)
175     {
176       return (this.x*v1.x + this.y*v1.y + this.z*v1.z + this.w*v1.w);
177     }
178
179
180  /**
181    * Sets the value of this vector to the normalization of vector v1.
182    * @param v1 the un-normalized vector
183    */
184   public final void normalize(Vector4f v1)
185   {
186     float norm;
187
188     norm = (float) (1.0/Math.sqrt(v1.x*v1.x + v1.y*v1.y +
189                                   v1.z*v1.z + v1.w*v1.w));
190     this.x = v1.x*norm;
191     this.y = v1.y*norm;
192     this.z = v1.z*norm;
193     this.w = v1.w*norm;
194   }
195
196
197   /**
198    * Normalizes this vector in place.
199    */
200   public final void normalize()
201   {
202     float norm;
203
204     norm = (float) (1.0/Math.sqrt(this.x*this.x + this.y*this.y +
205                                   this.z*this.z + this.w*this.w));
206     this.x *= norm;
207     this.y *= norm;
208     this.z *= norm;
209     this.w *= norm;
210   }
211
212
213   /** 
214     *   Returns the (4-space) angle in radians between this vector and 
215     *   the vector parameter; the return value is constrained to the 
216     *   range [0,PI]. 
217     *   @param v1    the other vector 
218     *   @return   the angle in radians in the range [0,PI] 
219     */   
220    public final float angle(Vector4f v1) 
221    { 
222       double vDot = this.dot(v1) / ( this.length()*v1.length() );
223       if( vDot < -1.0) vDot = -1.0;
224       if( vDot >  1.0) vDot =  1.0;
225       return((float) (Math.acos( vDot )));
226    } 
227
228 }