2 * $RCSfile: Tuple3i.java,v $
4 * Copyright 1999-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 3-element tuple represented by signed integer x,y,z
42 public abstract class Tuple3i implements java.io.Serializable, Cloneable {
44 static final long serialVersionUID = -732740491767276200L;
63 * Constructs and initializes a Tuple3i from the specified
64 * x, y, and z coordinates.
65 * @param x the x coordinate
66 * @param y the y coordinate
67 * @param z the z coordinate
69 public Tuple3i(int x, int y, int z) {
77 * Constructs and initializes a Tuple3i from the array of length 3.
78 * @param t the array of length 3 containing x, y, and z in order.
80 public Tuple3i(int[] t) {
88 * Constructs and initializes a Tuple3i from the specified Tuple3i.
89 * @param t1 the Tuple3i containing the initialization x, y, and z
92 public Tuple3i(Tuple3i t1) {
100 * Constructs and initializes a Tuple3i to (0,0,0).
110 * Sets the value of this tuple to the specified x, y, and z
112 * @param x the x coordinate
113 * @param y the y coordinate
114 * @param z the z coordinate
116 public final void set(int x, int y, int z) {
124 * Sets the value of this tuple to the specified coordinates in the
126 * @param t the array of length 3 containing x, y, and z in order.
128 public final void set(int[] t) {
136 * Sets the value of this tuple to the value of tuple t1.
137 * @param t1 the tuple to be copied
139 public final void set(Tuple3i t1) {
147 * Copies the values of this tuple into the array t.
148 * @param t is the array
150 public final void get(int[] t) {
158 * Copies the values of this tuple into the tuple t.
159 * @param t is the target tuple
161 public final void get(Tuple3i t) {
169 * Sets the value of this tuple to the sum of tuples t1 and t2.
170 * @param t1 the first tuple
171 * @param t2 the second tuple
173 public final void add(Tuple3i t1, Tuple3i t2) {
174 this.x = t1.x + t2.x;
175 this.y = t1.y + t2.y;
176 this.z = t1.z + t2.z;
181 * Sets the value of this tuple to the sum of itself and t1.
182 * @param t1 the other tuple
184 public final void add(Tuple3i t1) {
192 * Sets the value of this tuple to the difference
193 * of tuples t1 and t2 (this = t1 - t2).
194 * @param t1 the first tuple
195 * @param t2 the second tuple
197 public final void sub(Tuple3i t1, Tuple3i t2) {
198 this.x = t1.x - t2.x;
199 this.y = t1.y - t2.y;
200 this.z = t1.z - t2.z;
205 * Sets the value of this tuple to the difference
206 * of itself and t1 (this = this - t1).
207 * @param t1 the other tuple
209 public final void sub(Tuple3i t1) {
217 * Sets the value of this tuple to the negation of tuple t1.
218 * @param t1 the source tuple
220 public final void negate(Tuple3i t1) {
228 * Negates the value of this tuple in place.
230 public final void negate() {
238 * Sets the value of this tuple to the scalar multiplication
240 * @param s the scalar value
241 * @param t1 the source tuple
243 public final void scale(int s, Tuple3i t1) {
251 * Sets the value of this tuple to the scalar multiplication
252 * of the scale factor with this.
253 * @param s the scalar value
255 public final void scale(int s) {
263 * Sets the value of this tuple to the scalar multiplication
264 * of tuple t1 plus tuple t2 (this = s*t1 + t2).
265 * @param s the scalar value
266 * @param t1 the tuple to be multipled
267 * @param t2 the tuple to be added
269 public final void scaleAdd(int s, Tuple3i t1, Tuple3i t2) {
270 this.x = s*t1.x + t2.x;
271 this.y = s*t1.y + t2.y;
272 this.z = s*t1.z + t2.z;
277 * Sets the value of this tuple to the scalar multiplication
278 * of itself and then adds tuple t1 (this = s*this + t1).
279 * @param s the scalar value
280 * @param t1 the tuple to be added
282 public final void scaleAdd(int s, Tuple3i t1) {
283 this.x = s*this.x + t1.x;
284 this.y = s*this.y + t1.y;
285 this.z = s*this.z + t1.z;
290 * Returns a string that contains the values of this Tuple3i.
291 * The form is (x,y,z).
292 * @return the String representation
294 public String toString() {
295 return "(" + this.x + ", " + this.y + ", " + this.z + ")";
300 * Returns true if the Object t1 is of type Tuple3i and all of the
301 * data members of t1 are equal to the corresponding data members in
303 * @param t1 the object with which the comparison is made
305 public boolean equals(Object t1) {
307 Tuple3i t2 = (Tuple3i) t1;
308 return(this.x == t2.x && this.y == t2.y && this.z == t2.z);
310 catch (NullPointerException e2) {
313 catch (ClassCastException e1) {
320 * Returns a hash code value based on the data values in this
321 * object. Two different Tuple3i objects with identical data values
322 * (i.e., Tuple3i.equals returns true) will return the same hash
323 * code value. Two objects with different data members may return the
324 * same hash value, although this is not likely.
325 * @return the integer hash code value
327 public int hashCode() {
329 bits = 31L * bits + (long)x;
330 bits = 31L * bits + (long)y;
331 bits = 31L * bits + (long)z;
332 return (int) (bits ^ (bits >> 32));
337 * Clamps the tuple parameter to the range [low, high] and
338 * places the values into this tuple.
339 * @param min the lowest value in the tuple after clamping
340 * @param max the highest value in the tuple after clamping
341 * @param t the source tuple, which will not be modified
343 public final void clamp(int min, int max, Tuple3i t) {
346 } else if( t.x < min ) {
354 } else if( t.y < min ) {
362 } else if( t.z < min ) {
371 * Clamps the minimum value of the tuple parameter to the min
372 * parameter and places the values into this tuple.
373 * @param min the lowest value in the tuple after clamping
374 * @param t the source tuple, which will not be modified
376 public final void clampMin(int min, Tuple3i t) {
398 * Clamps the maximum value of the tuple parameter to the max
399 * parameter and places the values into this tuple.
400 * @param max the highest value in the tuple after clamping
401 * @param t the source tuple, which will not be modified
403 public final void clampMax(int max, Tuple3i t) {
425 * Sets each component of the tuple parameter to its absolute
426 * value and places the modified values into this tuple.
427 * @param t the source tuple, which will not be modified
429 public final void absolute(Tuple3i t) {
437 * Clamps this tuple to the range [low, high].
438 * @param min the lowest value in this tuple after clamping
439 * @param max the highest value in this tuple after clamping
441 public final void clamp(int min, int max) {
444 } else if( x < min ) {
450 } else if( y < min ) {
456 } else if( z < min ) {
463 * Clamps the minimum value of this tuple to the min parameter.
464 * @param min the lowest value in this tuple after clamping
466 public final void clampMin(int min) {
479 * Clamps the maximum value of this tuple to the max parameter.
480 * @param max the highest value in the tuple after clamping
482 public final void clampMax(int max) {
495 * Sets each component of this tuple to its absolute value.
497 public final void absolute() {
504 * Creates a new object of the same class as this object.
506 * @return a clone of this instance.
507 * @exception OutOfMemoryError if there is not enough memory.
508 * @see java.lang.Cloneable
511 public Object clone() {
512 // Since there are no arrays we can just use Object.clone()
514 return super.clone();
515 } catch (CloneNotSupportedException e) {
516 // this shouldn't happen, since we are Cloneable
517 throw new InternalError();
523 * Get the <i>x</i> coordinate.
525 * @return the <i>x</i> coordinate.
529 public final int getX() {
535 * Set the <i>x</i> coordinate.
537 * @param x value to <i>x</i> coordinate.
541 public final void setX(int x) {
547 * Get the <i>y</i> coordinate.
549 * @return the <i>y</i> coordinate.
553 public final int getY() {
559 * Set the <i>y</i> coordinate.
561 * @param y value to <i>y</i> coordinate.
565 public final void setY(int y) {
570 * Get the <i>z</i> coordinate.
572 * @return the <i>z</i> coordinate.
575 public final int getZ() {
581 * Set the <i>z</i> coordinate.
583 * @param z value to <i>z</i> coordinate.
587 public final void setZ(int z) {