2 * $RCSfile: Tuple3d.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 generic 3-element tuple that is represented by double-precision
38 * floating point x,y,z coordinates.
41 public abstract class Tuple3d implements java.io.Serializable, Cloneable {
43 static final long serialVersionUID = 5542096614926168415L;
62 * Constructs and initializes a Tuple3d from the specified xyz coordinates.
63 * @param x the x coordinate
64 * @param y the y coordinate
65 * @param z the z coordinate
67 public Tuple3d(double x, double y, double z)
75 * Constructs and initializes a Tuple3d from the array of length 3.
76 * @param t the array of length 3 containing xyz in order
78 public Tuple3d(double[] t)
86 * Constructs and initializes a Tuple3d from the specified Tuple3d.
87 * @param t1 the Tuple3d containing the initialization x y z data
89 public Tuple3d(Tuple3d t1)
97 * Constructs and initializes a Tuple3d from the specified Tuple3f.
98 * @param t1 the Tuple3f containing the initialization x y z data
100 public Tuple3d(Tuple3f t1)
102 this.x = (double) t1.x;
103 this.y = (double) t1.y;
104 this.z = (double) t1.z;
108 * Constructs and initializes a Tuple3d to (0,0,0).
112 this.x = (double) 0.0;
113 this.y = (double) 0.0;
114 this.z = (double) 0.0;
118 * Sets the value of this tuple to the specified xyz coordinates.
119 * @param x the x coordinate
120 * @param y the y coordinate
121 * @param z the z coordinate
123 public final void set(double x, double y, double z)
131 * Sets the value of this tuple to the value of the xyz coordinates
132 * located in the array of length 3.
133 * @param t the array of length 3 containing xyz in order
135 public final void set(double[] t)
143 * Sets the value of this tuple to the value of tuple t1.
144 * @param t1 the tuple to be copied
146 public final void set(Tuple3d t1)
154 * Sets the value of this tuple to the value of tuple t1.
155 * @param t1 the tuple to be copied
157 public final void set(Tuple3f t1)
159 this.x = (double) t1.x;
160 this.y = (double) t1.y;
161 this.z = (double) t1.z;
165 * Copies the x,y,z coordinates of this tuple into the array t
167 * @param t the target array
169 public final void get(double[] t)
178 * Copies the x,y,z coordinates of this tuple into the tuple t.
179 * @param t the Tuple3d object into which the values of this object are copied
181 public final void get(Tuple3d t)
190 * Sets the value of this tuple to the sum of tuples t1 and t2.
191 * @param t1 the first tuple
192 * @param t2 the second tuple
194 public final void add(Tuple3d t1, Tuple3d t2)
196 this.x = t1.x + t2.x;
197 this.y = t1.y + t2.y;
198 this.z = t1.z + t2.z;
203 * Sets the value of this tuple to the sum of itself and t1.
204 * @param t1 the other tuple
206 public final void add(Tuple3d t1)
214 * Sets the value of this tuple to the difference of tuples
215 * t1 and t2 (this = t1 - t2).
216 * @param t1 the first tuple
217 * @param t2 the second tuple
219 public final void sub(Tuple3d t1, Tuple3d t2)
221 this.x = t1.x - t2.x;
222 this.y = t1.y - t2.y;
223 this.z = t1.z - t2.z;
227 * Sets the value of this tuple to the difference
228 * of itself and t1 (this = this - t1).
229 * @param t1 the other tuple
231 public final void sub(Tuple3d t1)
240 * Sets the value of this tuple to the negation of tuple t1.
241 * @param t1 the source tuple
243 public final void negate(Tuple3d t1)
252 * Negates the value of this tuple in place.
254 public final void negate()
263 * Sets the value of this tuple to the scalar multiplication
265 * @param s the scalar value
266 * @param t1 the source tuple
268 public final void scale(double s, Tuple3d t1)
277 * Sets the value of this tuple to the scalar multiplication
279 * @param s the scalar value
281 public final void scale(double s)
290 * Sets the value of this tuple to the scalar multiplication
291 * of tuple t1 and then adds tuple t2 (this = s*t1 + t2).
292 * @param s the scalar value
293 * @param t1 the tuple to be multipled
294 * @param t2 the tuple to be added
296 public final void scaleAdd(double s, Tuple3d t1, Tuple3d t2)
298 this.x = s*t1.x + t2.x;
299 this.y = s*t1.y + t2.y;
300 this.z = s*t1.z + t2.z;
305 * @deprecated Use scaleAdd(double,Tuple3d) instead
307 public final void scaleAdd(double s, Tuple3f t1) {
308 scaleAdd(s, new Point3d(t1));
313 * Sets the value of this tuple to the scalar multiplication
314 * of itself and then adds tuple t1 (this = s*this + t1).
315 * @param s the scalar value
316 * @param t1 the tuple to be added
318 public final void scaleAdd(double s, Tuple3d t1) {
319 this.x = s*this.x + t1.x;
320 this.y = s*this.y + t1.y;
321 this.z = s*this.z + t1.z;
327 * Returns a string that contains the values of this Tuple3d.
328 * The form is (x,y,z).
329 * @return the String representation
331 public String toString() {
332 return "(" + this.x + ", " + this.y + ", " + this.z + ")";
337 * Returns a hash code value based on the data values in this
338 * object. Two different Tuple3d objects with identical data values
339 * (i.e., Tuple3d.equals returns true) will return the same hash
340 * code value. Two objects with different data members may return the
341 * same hash value, although this is not likely.
342 * @return the integer hash code value
344 public int hashCode() {
346 bits = 31L * bits + VecMathUtil.doubleToLongBits(x);
347 bits = 31L * bits + VecMathUtil.doubleToLongBits(y);
348 bits = 31L * bits + VecMathUtil.doubleToLongBits(z);
349 return (int) (bits ^ (bits >> 32));
354 * Returns true if all of the data members of Tuple3d t1 are
355 * equal to the corresponding data members in this Tuple3d.
356 * @param t1 the tuple with which the comparison is made
357 * @return true or false
359 public boolean equals(Tuple3d t1)
362 return(this.x == t1.x && this.y == t1.y && this.z == t1.z);
364 catch (NullPointerException e2) {return false;}
368 * Returns true if the Object t1 is of type Tuple3d and all of the
369 * data members of t1 are equal to the corresponding data members in
371 * @param t1 the Object with which the comparison is made
372 * @return true or false
374 public boolean equals(Object t1)
377 Tuple3d t2 = (Tuple3d) t1;
378 return(this.x == t2.x && this.y == t2.y && this.z == t2.z);
380 catch (ClassCastException e1) {return false;}
381 catch (NullPointerException e2) {return false;}
386 * Returns true if the L-infinite distance between this tuple
387 * and tuple t1 is less than or equal to the epsilon parameter,
388 * otherwise returns false. The L-infinite
389 * distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2)].
390 * @param t1 the tuple to be compared to this tuple
391 * @param epsilon the threshold value
392 * @return true or false
394 public boolean epsilonEquals(Tuple3d t1, double epsilon)
399 if(Double.isNaN(diff)) return false;
400 if((diff<0?-diff:diff) > epsilon) return false;
403 if(Double.isNaN(diff)) return false;
404 if((diff<0?-diff:diff) > epsilon) return false;
407 if(Double.isNaN(diff)) return false;
408 if((diff<0?-diff:diff) > epsilon) return false;
416 * @deprecated Use clamp(double,double,Tuple3d) instead
418 public final void clamp(float min, float max, Tuple3d t) {
419 clamp((double)min, (double)max, t);
424 * Clamps the tuple parameter to the range [low, high] and
425 * places the values into this tuple.
426 * @param min the lowest value in the tuple after clamping
427 * @param max the highest value in the tuple after clamping
428 * @param t the source tuple, which will not be modified
430 public final void clamp(double min, double max, Tuple3d t) {
433 } else if( t.x < min ){
441 } else if( t.y < min ){
449 } else if( t.z < min ){
459 * @deprecated Use clampMin(double,Tuple3d) instead
461 public final void clampMin(float min, Tuple3d t) {
462 clampMin((double)min, t);
467 * Clamps the minimum value of the tuple parameter to the min
468 * parameter and places the values into this tuple.
469 * @param min the lowest value in the tuple after clamping
470 * @param t the source tuple, which will not be modified
472 public final void clampMin(double min, Tuple3d t) {
495 * @deprecated Use clampMax(double,Tuple3d) instead
497 public final void clampMax(float max, Tuple3d t) {
498 clampMax((double)max, t);
503 * Clamps the maximum value of the tuple parameter to the max
504 * parameter and places the values into this tuple.
505 * @param max the highest value in the tuple after clamping
506 * @param t the source tuple, which will not be modified
508 public final void clampMax(double max, Tuple3d t) {
531 * Sets each component of the tuple parameter to its absolute
532 * value and places the modified values into this tuple.
533 * @param t the source tuple, which will not be modified
535 public final void absolute(Tuple3d t)
545 * @deprecated Use clamp(double,double) instead
547 public final void clamp(float min, float max) {
548 clamp((double)min, (double)max);
553 * Clamps this tuple to the range [low, high].
554 * @param min the lowest value in this tuple after clamping
555 * @param max the highest value in this tuple after clamping
557 public final void clamp(double min, double max) {
560 } else if( x < min ){
566 } else if( y < min ){
572 } else if( z < min ){
580 * @deprecated Use clampMin(double) instead
582 public final void clampMin(float min) {
583 clampMin((double)min);
588 * Clamps the minimum value of this tuple to the min parameter.
589 * @param min the lowest value in this tuple after clamping
591 public final void clampMin(double min) {
600 * @deprecated Use clampMax(double) instead
602 public final void clampMax(float max) {
603 clampMax((double)max);
608 * Clamps the maximum value of this tuple to the max parameter.
609 * @param max the highest value in the tuple after clamping
611 public final void clampMax(double max) {
619 * Sets each component of this tuple to its absolute value.
621 public final void absolute()
630 * @deprecated Use interpolate(Tuple3d,Tuple3d,double) instead
632 public final void interpolate(Tuple3d t1, Tuple3d t2, float alpha) {
633 interpolate(t1, t2, (double)alpha);
638 * Linearly interpolates between tuples t1 and t2 and places the
639 * result into this tuple: this = (1-alpha)*t1 + alpha*t2.
640 * @param t1 the first tuple
641 * @param t2 the second tuple
642 * @param alpha the alpha interpolation parameter
644 public final void interpolate(Tuple3d t1, Tuple3d t2, double alpha) {
645 this.x = (1-alpha)*t1.x + alpha*t2.x;
646 this.y = (1-alpha)*t1.y + alpha*t2.y;
647 this.z = (1-alpha)*t1.z + alpha*t2.z;
652 * @deprecated Use interpolate(Tuple3d,double) instead
654 public final void interpolate(Tuple3d t1, float alpha) {
655 interpolate(t1, (double)alpha);
660 * Linearly interpolates between this tuple and tuple t1 and
661 * places the result into this tuple: this = (1-alpha)*this + alpha*t1.
662 * @param t1 the first tuple
663 * @param alpha the alpha interpolation parameter
665 public final void interpolate(Tuple3d t1, double alpha) {
666 this.x = (1-alpha)*this.x + alpha*t1.x;
667 this.y = (1-alpha)*this.y + alpha*t1.y;
668 this.z = (1-alpha)*this.z + alpha*t1.z;
672 * Creates a new object of the same class as this object.
674 * @return a clone of this instance.
675 * @exception OutOfMemoryError if there is not enough memory.
676 * @see java.lang.Cloneable
679 public Object clone() {
680 // Since there are no arrays we can just use Object.clone()
682 return super.clone();
683 } catch (CloneNotSupportedException e) {
684 // this shouldn't happen, since we are Cloneable
685 throw new InternalError();
690 * Get the <i>x</i> coordinate.
692 * @return the <i>x</i> coordinate.
696 public final double getX() {
702 * Set the <i>x</i> coordinate.
704 * @param x value to <i>x</i> coordinate.
708 public final void setX(double x) {
714 * Get the <i>y</i> coordinate.
716 * @return the <i>y</i> coordinate.
720 public final double getY() {
726 * Set the <i>y</i> coordinate.
728 * @param y value to <i>y</i> coordinate.
732 public final void setY(double y) {
737 * Get the <i>z</i> coordinate.
739 * @return the <i>z</i> coordinate.
743 public final double getZ() {
749 * Set the <i>z</i> coordinate.
751 * @param z value to <i>z</i> coordinate.
755 public final void setZ(double z) {