2 * $RCSfile: Tuple3f.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 single precision-floating
38 * point x,y,z coordinates.
41 public abstract class Tuple3f implements java.io.Serializable, Cloneable {
43 static final long serialVersionUID=5019834619484343712L;
62 * Constructs and initializes a Tuple3f 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 Tuple3f(float x, float y, float z)
76 * Constructs and initializes a Tuple3f from the array of length 3.
77 * @param t the array of length 3 containing xyz in order
79 public Tuple3f(float[] t)
88 * Constructs and initializes a Tuple3f from the specified Tuple3f.
89 * @param t1 the Tuple3f containing the initialization x y z data
91 public Tuple3f(Tuple3f t1)
100 * Constructs and initializes a Tuple3f from the specified Tuple3d.
101 * @param t1 the Tuple3d containing the initialization x y z data
103 public Tuple3f(Tuple3d t1)
105 this.x = (float) t1.x;
106 this.y = (float) t1.y;
107 this.z = (float) t1.z;
112 * Constructs and initializes a Tuple3f to (0,0,0).
123 * Returns a string that contains the values of this Tuple3f.
124 * The form is (x,y,z).
125 * @return the String representation
127 public String toString() {
128 return "(" + this.x + ", " + this.y + ", " + this.z + ")";
133 * Sets the value of this tuple to the specified xyz coordinates.
134 * @param x the x coordinate
135 * @param y the y coordinate
136 * @param z the z coordinate
138 public final void set(float x, float y, float z)
147 * Sets the value of this tuple to the xyz coordinates specified in
148 * the array of length 3.
149 * @param t the array of length 3 containing xyz in order
151 public final void set(float[] t)
160 * Sets the value of this tuple to the value of tuple t1.
161 * @param t1 the tuple to be copied
163 public final void set(Tuple3f t1)
172 * Sets the value of this tuple to the value of tuple t1.
173 * @param t1 the tuple to be copied
175 public final void set(Tuple3d t1)
177 this.x = (float) t1.x;
178 this.y = (float) t1.y;
179 this.z = (float) t1.z;
184 * Gets the value of this tuple and copies the values into t.
185 * @param t the array of length 3 into which the values are copied
187 public final void get(float[] t)
196 * Gets the value of this tuple and copies the values into t.
197 * @param t the Tuple3f object into which the values of this object are copied
199 public final void get(Tuple3f t)
208 * Sets the value of this tuple to the vector sum of tuples t1 and t2.
209 * @param t1 the first tuple
210 * @param t2 the second tuple
212 public final void add(Tuple3f t1, Tuple3f t2)
214 this.x = t1.x + t2.x;
215 this.y = t1.y + t2.y;
216 this.z = t1.z + t2.z;
221 * Sets the value of this tuple to the vector sum of itself and tuple t1.
222 * @param t1 the other tuple
224 public final void add(Tuple3f t1)
233 * Sets the value of this tuple to the vector difference
234 * of tuples t1 and t2 (this = t1 - t2).
235 * @param t1 the first tuple
236 * @param t2 the second tuple
238 public final void sub(Tuple3f t1, Tuple3f t2)
240 this.x = t1.x - t2.x;
241 this.y = t1.y - t2.y;
242 this.z = t1.z - t2.z;
247 * Sets the value of this tuple to the vector difference of
248 * itself and tuple t1 (this = this - t1) .
249 * @param t1 the other tuple
251 public final void sub(Tuple3f t1)
260 * Sets the value of this tuple to the negation of tuple t1.
261 * @param t1 the source tuple
263 public final void negate(Tuple3f t1)
272 * Negates the value of this tuple in place.
274 public final void negate()
283 * Sets the value of this vector to the scalar multiplication
285 * @param s the scalar value
286 * @param t1 the source tuple
288 public final void scale(float s, Tuple3f t1)
297 * Sets the value of this tuple to the scalar multiplication
298 * of the scale factor with this.
299 * @param s the scalar value
301 public final void scale(float s)
310 * Sets the value of this tuple to the scalar multiplication
311 * of tuple t1 and then adds tuple t2 (this = s*t1 + t2).
312 * @param s the scalar value
313 * @param t1 the tuple to be scaled and added
314 * @param t2 the tuple to be added without a scale
316 public final void scaleAdd(float s, Tuple3f t1, Tuple3f t2)
318 this.x = s*t1.x + t2.x;
319 this.y = s*t1.y + t2.y;
320 this.z = s*t1.z + t2.z;
326 * Sets the value of this tuple to the scalar multiplication
327 * of itself and then adds tuple t1 (this = s*this + t1).
328 * @param s the scalar value
329 * @param t1 the tuple to be added
331 public final void scaleAdd(float s, Tuple3f t1)
333 this.x = s*this.x + t1.x;
334 this.y = s*this.y + t1.y;
335 this.z = s*this.z + t1.z;
340 * Returns true if the Object t1 is of type Tuple3f and all of the
341 * data members of t1 are equal to the corresponding data members in
343 * @param t1 the vector with which the comparison is made
344 * @return true or false
346 public boolean equals(Tuple3f t1)
349 return(this.x == t1.x && this.y == t1.y && this.z == t1.z);
351 catch (NullPointerException e2) {return false;}
354 * Returns true if the Object t1 is of type Tuple3f and all of the
355 * data members of t1 are equal to the corresponding data members in
357 * @param t1 the Object with which the comparison is made
358 * @return true or false
360 public boolean equals(Object t1)
363 Tuple3f t2 = (Tuple3f) t1;
364 return(this.x == t2.x && this.y == t2.y && this.z == t2.z);
366 catch (NullPointerException e2) {return false;}
367 catch (ClassCastException e1) {return false;}
372 * Returns true if the L-infinite distance between this tuple
373 * and tuple t1 is less than or equal to the epsilon parameter,
374 * otherwise returns false. The L-infinite
375 * distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2)].
376 * @param t1 the tuple to be compared to this tuple
377 * @param epsilon the threshold value
378 * @return true or false
380 public boolean epsilonEquals(Tuple3f t1, float epsilon)
385 if(Float.isNaN(diff)) return false;
386 if((diff<0?-diff:diff) > epsilon) return false;
389 if(Float.isNaN(diff)) return false;
390 if((diff<0?-diff:diff) > epsilon) return false;
393 if(Float.isNaN(diff)) return false;
394 if((diff<0?-diff:diff) > epsilon) return false;
402 * Returns a hash code value based on the data values in this
403 * object. Two different Tuple3f objects with identical data values
404 * (i.e., Tuple3f.equals returns true) will return the same hash
405 * code value. Two objects with different data members may return the
406 * same hash value, although this is not likely.
407 * @return the integer hash code value
409 public int hashCode() {
411 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(x);
412 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(y);
413 bits = 31L * bits + (long)VecMathUtil.floatToIntBits(z);
414 return (int) (bits ^ (bits >> 32));
420 * Clamps the tuple parameter to the range [low, high] and
421 * places the values into this tuple.
422 * @param min the lowest value in the tuple after clamping
423 * @param max the highest value in the tuple after clamping
424 * @param t the source tuple, which will not be modified
426 public final void clamp(float min, float max, Tuple3f t)
430 } else if( t.x < min ){
438 } else if( t.y < min ){
446 } else if( t.z < min ){
456 * Clamps the minimum value of the tuple parameter to the min
457 * parameter and places the values into this tuple.
458 * @param min the lowest value in the tuple after clamping
459 * @param t the source tuple, which will not be modified
461 public final void clampMin(float min, Tuple3f t)
485 * Clamps the maximum value of the tuple parameter to the max
486 * parameter and places the values into this tuple.
487 * @param max the highest value in the tuple after clamping
488 * @param t the source tuple, which will not be modified
490 public final void clampMax(float max, Tuple3f t)
514 * Sets each component of the tuple parameter to its absolute
515 * value and places the modified values into this tuple.
516 * @param t the source tuple, which will not be modified
518 public final void absolute(Tuple3f t)
528 * Clamps this tuple to the range [low, high].
529 * @param min the lowest value in this tuple after clamping
530 * @param max the highest value in this tuple after clamping
532 public final void clamp(float min, float max)
536 } else if( x < min ){
542 } else if( y < min ){
548 } else if( z < min ){
556 * Clamps the minimum value of this tuple to the min parameter.
557 * @param min the lowest value in this tuple after clamping
559 public final void clampMin(float min)
569 * Clamps the maximum value of this tuple to the max parameter.
570 * @param max the highest value in the tuple after clamping
572 public final void clampMax(float max)
582 * Sets each component of this tuple to its absolute value.
584 public final void absolute()
594 * Linearly interpolates between tuples t1 and t2 and places the
595 * result into this tuple: this = (1-alpha)*t1 + alpha*t2.
596 * @param t1 the first tuple
597 * @param t2 the second tuple
598 * @param alpha the alpha interpolation parameter
600 public final void interpolate(Tuple3f t1, Tuple3f t2, float alpha)
602 this.x = (1-alpha)*t1.x + alpha*t2.x;
603 this.y = (1-alpha)*t1.y + alpha*t2.y;
604 this.z = (1-alpha)*t1.z + alpha*t2.z;
611 * Linearly interpolates between this tuple and tuple t1 and
612 * places the result into this tuple: this = (1-alpha)*this + alpha*t1.
613 * @param t1 the first tuple
614 * @param alpha the alpha interpolation parameter
616 public final void interpolate(Tuple3f t1, float alpha)
618 this.x = (1-alpha)*this.x + alpha*t1.x;
619 this.y = (1-alpha)*this.y + alpha*t1.y;
620 this.z = (1-alpha)*this.z + alpha*t1.z;
626 * Creates a new object of the same class as this object.
628 * @return a clone of this instance.
629 * @exception OutOfMemoryError if there is not enough memory.
630 * @see java.lang.Cloneable
633 public Object clone() {
634 // Since there are no arrays we can just use Object.clone()
636 return super.clone();
637 } catch (CloneNotSupportedException e) {
638 // this shouldn't happen, since we are Cloneable
639 throw new InternalError();
645 * Get the <i>x</i> coordinate.
647 * @return the <i>x</i> coordinate.
651 public final float getX() {
657 * Set the <i>x</i> coordinate.
659 * @param x value to <i>x</i> coordinate.
663 public final void setX(float x) {
669 * Get the <i>y</i> coordinate.
671 * @return the <i>y</i> coordinate.
675 public final float getY() {
681 * Set the <i>y</i> coordinate.
683 * @param y value to <i>y</i> coordinate.
687 public final void setY(float y) {
692 * Get the <i>z</i> coordinate.
694 * @return the <i>z</i> coordinate
698 public final float getZ() {
704 * Set the <i>Z</i> coordinate.
706 * @param z value to <i>z</i> coordinate.
710 public final void setZ(float z) {