2 * $RCSfile: Tuple4d.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 double precision floating point
38 * x,y,z,w coordinates.
41 public abstract class Tuple4d implements java.io.Serializable, Cloneable {
43 static final long serialVersionUID = -4748953690425311052L;
67 * Constructs and initializes a Tuple4d 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 Tuple4d(double x, double y, double z, double w)
83 * Constructs and initializes a Tuple4d from the coordinates contained
85 * @param t the array of length 4 containing xyzw in order
87 public Tuple4d(double[] t)
97 * Constructs and initializes a Tuple4d from the specified Tuple4d.
98 * @param t1 the Tuple4d containing the initialization x y z w data
100 public Tuple4d(Tuple4d t1)
110 * Constructs and initializes a Tuple4d from the specified Tuple4f.
111 * @param t1 the Tuple4f containing the initialization x y z w data
113 public Tuple4d(Tuple4f t1)
123 * Constructs and initializes a Tuple4d to (0,0,0,0).
135 * Sets the value of this tuple to the specified xyzw coordinates.
136 * @param x the x coordinate
137 * @param y the y coordinate
138 * @param z the z coordinate
139 * @param w the w coordinate
141 public final void set(double x, double y, double z, double w)
151 * Sets the value of this tuple to the specified xyzw coordinates.
152 * @param t the array of length 4 containing xyzw in order
154 public final void set(double[] 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(Tuple4d 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(Tuple4f t1)
190 * Gets the value of this tuple and places it into the array t of
191 * length four in x,y,z,w order.
192 * @param t the array of length four
194 public final void get(double[] t)
204 * Gets the value of this tuple and places it into the Tuple4d
206 * length four in x,y,z,w order.
207 * @param t the Tuple into which the values will be copied
209 public final void get(Tuple4d t)
219 * Sets the value of this tuple to the tuple sum of tuples t1 and t2.
220 * @param t1 the first tuple
221 * @param t2 the second tuple
223 public final void add(Tuple4d t1, Tuple4d t2)
225 this.x = t1.x + t2.x;
226 this.y = t1.y + t2.y;
227 this.z = t1.z + t2.z;
228 this.w = t1.w + t2.w;
233 * Sets the value of this tuple to the sum of itself and tuple t1.
234 * @param t1 the other tuple
236 public final void add(Tuple4d t1)
246 * Sets the value of this tuple to the difference
247 * of tuples t1 and t2 (this = t1 - t2).
248 * @param t1 the first tuple
249 * @param t2 the second tuple
251 public final void sub(Tuple4d t1, Tuple4d t2)
253 this.x = t1.x - t2.x;
254 this.y = t1.y - t2.y;
255 this.z = t1.z - t2.z;
256 this.w = t1.w - t2.w;
261 * Sets the value of this tuple to the difference of itself
262 * and tuple t1 (this = this - t1).
263 * @param t1 the other tuple
265 public final void sub(Tuple4d t1)
275 * Sets the value of this tuple to the negation of tuple t1.
276 * @param t1 the source tuple
278 public final void negate(Tuple4d t1)
288 * Negates the value of this tuple in place.
290 public final void negate()
300 * Sets the value of this tuple to the scalar multiplication
301 * of the scale factor with the tuple t1.
302 * @param s the scalar value
303 * @param t1 the source tuple
305 public final void scale(double s, Tuple4d t1)
315 * Sets the value of this tuple to the scalar multiplication
316 * of the scale factor with this.
317 * @param s the scalar value
319 public final void scale(double s)
329 * Sets the value of this tuple to the scalar multiplication by s
330 * of tuple t1 plus tuple t2 (this = s*t1 + t2).
331 * @param s the scalar value
332 * @param t1 the tuple to be multipled
333 * @param t2 the tuple to be added
335 public final void scaleAdd(double s, Tuple4d t1, Tuple4d t2)
337 this.x = s*t1.x + t2.x;
338 this.y = s*t1.y + t2.y;
339 this.z = s*t1.z + t2.z;
340 this.w = s*t1.w + t2.w;
346 * @deprecated Use scaleAdd(double,Tuple4d) instead
348 public final void scaleAdd(float s, Tuple4d t1) {
349 scaleAdd((double)s, t1);
354 * Sets the value of this tuple to the scalar multiplication
355 * of itself and then adds tuple t1 (this = s*this + t1).
356 * @param s the scalar value
357 * @param t1 the tuple to be added
359 public final void scaleAdd(double s, Tuple4d t1) {
360 this.x = s*this.x + t1.x;
361 this.y = s*this.y + t1.y;
362 this.z = s*this.z + t1.z;
363 this.w = s*this.w + t1.w;
369 * Returns a string that contains the values of this Tuple4d.
370 * The form is (x,y,z,w).
371 * @return the String representation
373 public String toString() {
374 return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.w + ")";
379 * Returns true if all of the data members of Tuple4d t1 are
380 * equal to the corresponding data members in this Tuple4d.
381 * @param t1 the tuple with which the comparison is made
382 * @return true or false
384 public boolean equals(Tuple4d t1)
387 return(this.x == t1.x && this.y == t1.y && this.z == t1.z
390 catch (NullPointerException e2) {return false;}
394 * Returns true if the Object t1 is of type Tuple4d and all of the
395 * data members of t1 are equal to the corresponding data members in
397 * @param t1 the object with which the comparison is made
398 * @return true or false
400 public boolean equals(Object t1)
404 Tuple4d t2 = (Tuple4d) t1;
405 return(this.x == t2.x && this.y == t2.y &&
406 this.z == t2.z && this.w == t2.w);
408 catch (NullPointerException e2) {return false;}
409 catch (ClassCastException e1) {return false;}
414 * Returns true if the L-infinite distance between this tuple
415 * and tuple t1 is less than or equal to the epsilon parameter,
416 * otherwise returns false. The L-infinite
417 * distance is equal to
418 * MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)].
419 * @param t1 the tuple to be compared to this tuple
420 * @param epsilon the threshold value
421 * @return true or false
423 public boolean epsilonEquals(Tuple4d t1, double epsilon)
428 if(Double.isNaN(diff)) return false;
429 if((diff<0?-diff:diff) > epsilon) return false;
432 if(Double.isNaN(diff)) return false;
433 if((diff<0?-diff:diff) > epsilon) return false;
436 if(Double.isNaN(diff)) return false;
437 if((diff<0?-diff:diff) > epsilon) return false;
440 if(Double.isNaN(diff)) return false;
441 if((diff<0?-diff:diff) > epsilon) return false;
449 * Returns a hash code value based on the data values in this
450 * object. Two different Tuple4d objects with identical data values
451 * (i.e., Tuple4d.equals returns true) will return the same hash
452 * code value. Two objects with different data members may return the
453 * same hash value, although this is not likely.
454 * @return the integer hash code value
456 public int hashCode() {
458 bits = 31L * bits + VecMathUtil.doubleToLongBits(x);
459 bits = 31L * bits + VecMathUtil.doubleToLongBits(y);
460 bits = 31L * bits + VecMathUtil.doubleToLongBits(z);
461 bits = 31L * bits + VecMathUtil.doubleToLongBits(w);
462 return (int) (bits ^ (bits >> 32));
467 * @deprecated Use clamp(double,double,Tuple4d) instead
469 public final void clamp(float min, float max, Tuple4d t) {
470 clamp((double)min, (double)max, t);
475 * Clamps the tuple parameter to the range [low, high] and
476 * places the values into this tuple.
477 * @param min the lowest value in the tuple after clamping
478 * @param max the highest value in the tuple after clamping
479 * @param t the source tuple, which will not be modified
481 public final void clamp(double min, double max, Tuple4d t) {
484 } else if( t.x < min ){
492 } else if( t.y < min ){
500 } else if( t.z < min ){
508 } else if( t.w < min ){
518 * @deprecated Use clampMin(double,Tuple4d) instead
520 public final void clampMin(float min, Tuple4d t) {
521 clampMin((double)min, t);
526 * Clamps the minimum value of the tuple parameter to the min
527 * parameter and places the values into this tuple.
528 * @param min the lowest value in the tuple after clamping
529 * @param t the source tuple, which will not be modified
531 public final void clampMin(double min, Tuple4d t) {
560 * @deprecated Use clampMax(double,Tuple4d) instead
562 public final void clampMax(float max, Tuple4d t) {
563 clampMax((double)max, t);
568 * Clamps the maximum value of the tuple parameter to the max
569 * parameter and places the values into this tuple.
570 * @param max the highest value in the tuple after clamping
571 * @param t the source tuple, which will not be modified
573 public final void clampMax(double max, Tuple4d t) {
602 * Sets each component of the tuple parameter to its absolute
603 * value and places the modified values into this tuple.
604 * @param t the source tuple, which will not be modified
606 public final void absolute(Tuple4d t)
618 * @deprecated Use clamp(double,double) instead
620 public final void clamp(float min, float max) {
621 clamp((double)min, (double)max);
626 * Clamps this tuple to the range [low, high].
627 * @param min the lowest value in this tuple after clamping
628 * @param max the highest value in this tuple after clamping
630 public final void clamp(double min, double max) {
633 } else if( x < min ){
639 } else if( y < min ){
645 } else if( z < min ){
651 } else if( w < min ){
659 * @deprecated Use clampMin(double) instead
661 public final void clampMin(float min) {
662 clampMin((double)min);
667 * Clamps the minimum value of this tuple to the min parameter.
668 * @param min the lowest value in this tuple after clamping
670 public final void clampMin(double min) {
679 * @deprecated Use clampMax(double) instead
681 public final void clampMax(float max) {
682 clampMax((double)max);
687 * Clamps the maximum value of this tuple to the max parameter.
688 * @param max the highest value in the tuple after clamping
690 public final void clampMax(double max) {
700 * Sets each component of this tuple to its absolute value.
702 public final void absolute()
713 * @deprecated Use interpolate(Tuple4d,Tuple4d,double) instead
715 public void interpolate(Tuple4d t1, Tuple4d t2, float alpha) {
716 interpolate(t1, t2, (double)alpha);
721 * Linearly interpolates between tuples t1 and t2 and places the
722 * result into this tuple: this = (1-alpha)*t1 + alpha*t2.
723 * @param t1 the first tuple
724 * @param t2 the second tuple
725 * @param alpha the alpha interpolation parameter
727 public void interpolate(Tuple4d t1, Tuple4d t2, double alpha) {
728 this.x = (1-alpha)*t1.x + alpha*t2.x;
729 this.y = (1-alpha)*t1.y + alpha*t2.y;
730 this.z = (1-alpha)*t1.z + alpha*t2.z;
731 this.w = (1-alpha)*t1.w + alpha*t2.w;
736 * @deprecated Use interpolate(Tuple4d,double) instead
738 public void interpolate(Tuple4d t1, float alpha) {
739 interpolate(t1, (double)alpha);
744 * Linearly interpolates between this tuple and tuple t1 and
745 * places the result into this tuple: this = (1-alpha)*this + alpha*t1.
746 * @param t1 the first tuple
747 * @param alpha the alpha interpolation parameter
749 public void interpolate(Tuple4d t1, double alpha) {
750 this.x = (1-alpha)*this.x + alpha*t1.x;
751 this.y = (1-alpha)*this.y + alpha*t1.y;
752 this.z = (1-alpha)*this.z + alpha*t1.z;
753 this.w = (1-alpha)*this.w + alpha*t1.w;
757 * Creates a new object of the same class as this object.
759 * @return a clone of this instance.
760 * @exception OutOfMemoryError if there is not enough memory.
761 * @see java.lang.Cloneable
764 public Object clone() {
765 // Since there are no arrays we can just use Object.clone()
767 return super.clone();
768 } catch (CloneNotSupportedException e) {
769 // this shouldn't happen, since we are Cloneable
770 throw new InternalError();
775 * Get the <i>x</i> coordinate.
777 * @return the x coordinate.
781 public final double getX() {
787 * Set the <i>x</i> coordinate.
789 * @param x value to <i>x</i> coordinate.
793 public final void setX(double x) {
799 * Get the <i>y</i> coordinate.
801 * @return the <i>y</i> coordinate.
805 public final double getY() {
811 * Set the <i>y</i> coordinate.
813 * @param y value to <i>y</i> coordinate.
817 public final void setY(double y) {
822 * Get the <i>z</i> coordinate.
824 * @return the <i>z</i> coordinate.
828 public final double getZ() {
834 * Set the <i>z</i> coordinate.
836 * @param z value to <i>z</i> coordinate.
840 public final void setZ(double z) {
846 * Get the <i>w</i> coordinate.
848 * @return the <i>w</i> coordinate.
852 public final double getW() {
858 * Set the <i>w</i> coordinate.
860 * @param w value to <i>w</i> coordinate.
864 public final void setW(double w) {