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