2 * $RCSfile: Tuple4f.java,v $
4 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 * $Date: 2008/02/28 20:18:51 $
32 package javax.vecmath;
34 import java.lang.Math;
37 * A 4-element tuple represented by single-precision floating point x,y,z,w
41 public abstract class Tuple4f implements java.io.Serializable, Cloneable {
43 static final long serialVersionUID = 7068460319248845763L;
67 * Constructs and initializes a Tuple4f from the specified xyzw coordinates.
68 * @param x the x coordinate
69 * @param y the y coordinate
70 * @param z the z coordinate
71 * @param w the w coordinate
73 public Tuple4f(float x, float y, float z, float w)
83 * Constructs and initializes a Tuple4f from the array of length 4.
84 * @param t the array of length 4 containing xyzw in order
86 public Tuple4f(float[] t)
96 * Constructs and initializes a Tuple4f from the specified Tuple4f.
97 * @param t1 the Tuple4f containing the initialization x y z w data
99 public Tuple4f(Tuple4f t1)
109 * Constructs and initializes a Tuple4f from the specified Tuple4d.
110 * @param t1 the Tuple4d containing the initialization x y z w data
112 public Tuple4f(Tuple4d t1)
114 this.x = (float) t1.x;
115 this.y = (float) t1.y;
116 this.z = (float) t1.z;
117 this.w = (float) t1.w;
122 * Constructs and initializes a Tuple4f to (0,0,0,0).
134 * Sets the value of this tuple to the specified xyzw coordinates.
135 * @param x the x coordinate
136 * @param y the y coordinate
137 * @param z the z coordinate
138 * @param w the w coordinate
140 public final void set(float x, float y, float z, float w)
150 * Sets the value of this tuple to the specified coordinates in the
152 * @param t the array of length 4 containing xyzw in order
154 public final void set(float[] t)
164 * Sets the value of this tuple to the value of tuple t1.
165 * @param t1 the tuple to be copied
167 public final void set(Tuple4f t1)
177 * Sets the value of this tuple to the value of tuple t1.
178 * @param t1 the tuple to be copied
180 public final void set(Tuple4d t1)
182 this.x = (float) t1.x;
183 this.y = (float) t1.y;
184 this.z = (float) t1.z;
185 this.w = (float) t1.w;
190 * Copies the values of this tuple into the array t.
193 public final void get(float[] t)
203 * Copies the values of this tuple into the tuple t.
204 * @param t the target tuple
206 public final void get(Tuple4f t)
216 * Sets the value of this tuple to the sum of tuples t1 and t2.
217 * @param t1 the first tuple
218 * @param t2 the second tuple
220 public final void add(Tuple4f t1, Tuple4f t2)
222 this.x = t1.x + t2.x;
223 this.y = t1.y + t2.y;
224 this.z = t1.z + t2.z;
225 this.w = t1.w + t2.w;
230 * Sets the value of this tuple to the sum of itself and t1.
231 * @param t1 the other tuple
233 public final void add(Tuple4f t1)
243 * Sets the value of this tuple to the difference
244 * of tuples t1 and t2 (this = t1 - t2).
245 * @param t1 the first tuple
246 * @param t2 the second tuple
248 public final void sub(Tuple4f t1, Tuple4f t2)
250 this.x = t1.x - t2.x;
251 this.y = t1.y - t2.y;
252 this.z = t1.z - t2.z;
253 this.w = t1.w - t2.w;
258 * Sets the value of this tuple to the difference
259 * of itself and t1 (this = this - t1).
260 * @param t1 the other tuple
262 public final void sub(Tuple4f t1)
272 * Sets the value of this tuple to the negation of tuple t1.
273 * @param t1 the source tuple
275 public final void negate(Tuple4f t1)
285 * Negates the value of this tuple in place.
287 public final void negate()
297 * Sets the value of this tuple to the scalar multiplication
299 * @param s the scalar value
300 * @param t1 the source tuple
302 public final void scale(float s, Tuple4f t1)
312 * Sets the value of this tuple to the scalar multiplication
313 * of the scale factor with this.
314 * @param s the scalar value
316 public final void scale(float s)
326 * Sets the value of this tuple to the scalar multiplication
327 * of tuple t1 plus tuple t2 (this = s*t1 + t2).
328 * @param s the scalar value
329 * @param t1 the tuple to be multipled
330 * @param t2 the tuple to be added
332 public final void scaleAdd(float s, Tuple4f t1, Tuple4f t2)
334 this.x = s*t1.x + t2.x;
335 this.y = s*t1.y + t2.y;
336 this.z = s*t1.z + t2.z;
337 this.w = s*t1.w + t2.w;
342 * Sets the value of this tuple to the scalar multiplication
343 * of itself and then adds tuple t1 (this = s*this + t1).
344 * @param s the scalar value
345 * @param t1 the tuple to be added
347 public final void scaleAdd(float s, Tuple4f t1)
349 this.x = s*this.x + t1.x;
350 this.y = s*this.y + t1.y;
351 this.z = s*this.z + t1.z;
352 this.w = s*this.w + t1.w;
358 * Returns a string that contains the values of this Tuple4f.
359 * The form is (x,y,z,w).
360 * @return the String representation
362 public String toString() {
363 return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.w + ")";
367 * Returns true if all of the data members of Tuple4f t1 are
368 * equal to the corresponding data members in this Tuple4f.
369 * @param t1 the vector with which the comparison is made
370 * @return true or false
372 public boolean equals(Tuple4f t1)
375 return(this.x == t1.x && this.y == t1.y && this.z == t1.z
378 catch (NullPointerException e2) {return false;}
382 * Returns true if the Object t1 is of type Tuple4f and all of the
383 * data members of t1 are equal to the corresponding data members in
385 * @param t1 the object with which the comparison is made
386 * @return true or false
388 public boolean equals(Object t1)
391 Tuple4f t2 = (Tuple4f) t1;
392 return(this.x == t2.x && this.y == t2.y &&
393 this.z == t2.z && this.w == t2.w);
395 catch (NullPointerException e2) {return false;}
396 catch (ClassCastException e1) {return false;}
401 * Returns true if the L-infinite distance between this tuple
402 * and tuple t1 is less than or equal to the epsilon parameter,
403 * otherwise returns false. The L-infinite
404 * distance is equal to
405 * MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)].
406 * @param t1 the tuple to be compared to this tuple
407 * @param epsilon the threshold value
408 * @return true or false
410 public boolean epsilonEquals(Tuple4f t1, float epsilon)
415 if(Float.isNaN(diff)) return false;
416 if((diff<0?-diff:diff) > epsilon) return false;
419 if(Float.isNaN(diff)) return false;
420 if((diff<0?-diff:diff) > epsilon) return false;
423 if(Float.isNaN(diff)) return false;
424 if((diff<0?-diff:diff) > epsilon) return false;
427 if(Float.isNaN(diff)) return false;
428 if((diff<0?-diff:diff) > epsilon) return false;
435 * Returns a hash code value based on the data values in this
436 * object. Two different Tuple4f objects with identical data values
437 * (i.e., Tuple4f.equals returns true) will return the same hash
438 * code value. Two objects with different data members may return the
439 * same hash value, although this is not likely.
440 * @return the integer hash code value
442 public int hashCode() {
444 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(x);
445 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(y);
446 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(z);
447 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(w);
448 return (int) (bits ^ (bits >> 32));
453 * Clamps the tuple parameter to the range [low, high] and
454 * places the values into this tuple.
455 * @param min the lowest value in the tuple after clamping
456 * @param max the highest value in the tuple after clamping
457 * @param t the source tuple, which will not be modified
459 public final void clamp(float min, float max, Tuple4f t)
463 } else if( t.x < min ){
471 } else if( t.y < min ){
479 } else if( t.z < min ){
487 } else if( t.w < min ){
497 * Clamps the minimum value of the tuple parameter to the min
498 * parameter and places the values into this tuple.
499 * @param min the lowest value in the tuple after clamping
500 * @param t the source tuple, which will not be modified
502 public final void clampMin(float min, Tuple4f t)
533 * Clamps the maximum value of the tuple parameter to the max
534 * parameter and places the values into this tuple.
535 * @param max the highest value in the tuple after clamping
536 * @param t the source tuple, which will not be modified
538 public final void clampMax(float max, Tuple4f t)
568 * Sets each component of the tuple parameter to its absolute
569 * value and places the modified values into this tuple.
570 * @param t the source tuple, which will not be modified
572 public final void absolute(Tuple4f t)
582 * Clamps this tuple to the range [low, high].
583 * @param min the lowest value in this tuple after clamping
584 * @param max the highest value in this tuple after clamping
586 public final void clamp(float min, float max)
590 } else if( x < min ){
596 } else if( y < min ){
602 } else if( z < min ){
608 } else if( w < min ){
616 * Clamps the minimum value of this tuple to the min parameter.
617 * @param min the lowest value in this tuple after clamping
619 public final void clampMin(float min)
630 * Clamps the maximum value of this tuple to the max parameter.
631 * @param max the highest value in the tuple after clamping
633 public final void clampMax(float max)
644 * Sets each component of this tuple to its absolute value.
646 public final void absolute()
656 * Linearly interpolates between tuples t1 and t2 and places the
657 * result into this tuple: this = (1-alpha)*t1 + alpha*t2.
658 * @param t1 the first tuple
659 * @param t2 the second tuple
660 * @param alpha the alpha interpolation parameter
662 public void interpolate(Tuple4f t1, Tuple4f t2, float alpha)
664 this.x = (1-alpha)*t1.x + alpha*t2.x;
665 this.y = (1-alpha)*t1.y + alpha*t2.y;
666 this.z = (1-alpha)*t1.z + alpha*t2.z;
667 this.w = (1-alpha)*t1.w + alpha*t2.w;
673 * Linearly interpolates between this tuple and tuple t1 and
674 * places the result into this tuple: this = (1-alpha)*this + alpha*t1.
675 * @param t1 the first tuple
676 * @param alpha the alpha interpolation parameter
678 public void interpolate(Tuple4f t1, float alpha)
680 this.x = (1-alpha)*this.x + alpha*t1.x;
681 this.y = (1-alpha)*this.y + alpha*t1.y;
682 this.z = (1-alpha)*this.z + alpha*t1.z;
683 this.w = (1-alpha)*this.w + alpha*t1.w;
688 * Creates a new object of the same class as this object.
690 * @return a clone of this instance.
691 * @exception OutOfMemoryError if there is not enough memory.
692 * @see java.lang.Cloneable
695 public Object clone() {
696 // Since there are no arrays we can just use Object.clone()
698 return super.clone();
699 } catch (CloneNotSupportedException e) {
700 // this shouldn't happen, since we are Cloneable
701 throw new InternalError();
706 * Get the <i>x</i> coordinate.
708 * @return the <i>x</i> coordinate.
712 public final float getX() {
718 * Set the <i>x</i> coordinate.
720 * @param x value to <i>x</i> coordinate.
724 public final void setX(float x) {
730 * Get the <i>y</i> coordinate.
732 * @return the <i>y</i> coordinate.
736 public final float getY() {
742 * Set the <i>y</i> coordinate.
744 * @param y value to <i>y</i> coordinate.
748 public final void setY(float y) {
753 * Get the <i>z</i> coordinate.
755 * @return the <i>z</i> coordinate.
759 public final float getZ() {
765 * Set the <i>z</i> coordinate.
767 * @param z value to <i>z</i> coordinate.
771 public final void setZ(float z) {
777 * Get the <i>w</i> coordinate.
779 * @return the <i>w</i> coordinate.
783 public final float getW() {
789 * Set the <i>w</i> coordinate.
791 * @param w value to <i>w</i> coordinate.
795 public final void setW(float w) {