]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Tuple3i.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Tuple3i.java
1 /*
2  * $RCSfile: Tuple3i.java,v $
3  *
4  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
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.
12  *
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).
18  *
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.
22  *
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
25  * have any questions.
26  *
27  * $Revision: 1.7 $
28  * $Date: 2008/02/28 20:18:51 $
29  * $State: Exp $
30  */
31
32 package javax.vecmath;
33
34 import java.lang.Math;
35
36 /**
37  * A 3-element tuple represented by signed integer x,y,z
38  * coordinates.
39  *
40  * @since vecmath 1.2
41  */
42 public abstract class Tuple3i implements java.io.Serializable, Cloneable {
43
44     static final long serialVersionUID = -732740491767276200L;
45
46     /**
47      * The x coordinate.
48      */
49     public int x;
50
51     /**
52      * The y coordinate.
53      */
54     public int y;
55
56     /**
57      * The z coordinate.
58      */
59     public int z;
60
61
62     /**
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
68      */
69     public Tuple3i(int x, int y, int z) {
70         this.x = x;
71         this.y = y;
72         this.z = z;
73     }
74
75
76     /**
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.
79      */
80     public Tuple3i(int[] t) {
81         this.x = t[0];
82         this.y = t[1];
83         this.z = t[2];
84     }
85
86
87     /**
88      * Constructs and initializes a Tuple3i from the specified Tuple3i.
89      * @param t1 the Tuple3i containing the initialization x, y, and z
90      * data.
91      */
92     public Tuple3i(Tuple3i t1) {
93         this.x = t1.x;
94         this.y = t1.y;
95         this.z = t1.z;
96     }
97
98
99     /**
100      * Constructs and initializes a Tuple3i to (0,0,0).
101      */
102     public Tuple3i() {
103         this.x = 0;
104         this.y = 0;
105         this.z = 0;
106     }
107
108
109     /**
110      * Sets the value of this tuple to the specified x, y, and z
111      * coordinates.
112      * @param x the x coordinate
113      * @param y the y coordinate
114      * @param z the z coordinate
115      */
116     public final void set(int x, int y, int z) {
117         this.x = x;
118         this.y = y;
119         this.z = z;
120     }
121
122
123     /**
124      * Sets the value of this tuple to the specified coordinates in the
125      * array of length 3.
126      * @param t the array of length 3 containing x, y, and z in order.
127      */
128     public final void set(int[] t) {
129         this.x = t[0];
130         this.y = t[1];
131         this.z = t[2];
132     }
133
134
135     /**
136      * Sets the value of this tuple to the value of tuple t1.
137      * @param t1 the tuple to be copied
138      */
139     public final void set(Tuple3i t1) {
140         this.x = t1.x;
141         this.y = t1.y;
142         this.z = t1.z;
143     }
144
145
146     /**
147      * Copies the values of this tuple into the array t.
148      * @param t is the array
149      */
150     public final void get(int[] t) {
151         t[0] = this.x;
152         t[1] = this.y;
153         t[2] = this.z;
154     }
155
156
157     /**
158      * Copies the values of this tuple into the tuple t.
159      * @param t is the target tuple
160      */
161     public final void get(Tuple3i t) {
162         t.x = this.x;
163         t.y = this.y;
164         t.z = this.z;
165     }
166
167
168     /**
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
172      */
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;
177     }
178
179
180     /**
181      * Sets the value of this tuple to the sum of itself and t1.
182      * @param t1 the other tuple
183      */
184     public final void add(Tuple3i t1) {
185         this.x += t1.x;
186         this.y += t1.y;
187         this.z += t1.z;
188     }
189
190
191     /**
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
196      */
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;
201     }
202
203
204     /**
205      * Sets the value of this tuple to the difference
206      * of itself and t1 (this = this - t1).
207      * @param t1 the other tuple
208      */
209     public final void sub(Tuple3i t1) {
210         this.x -= t1.x;
211         this.y -= t1.y;
212         this.z -= t1.z;
213     }
214
215
216     /**
217      * Sets the value of this tuple to the negation of tuple t1.
218      * @param t1 the source tuple
219      */
220     public final void negate(Tuple3i t1) {
221         this.x = -t1.x;
222         this.y = -t1.y;
223         this.z = -t1.z;
224     }
225
226
227     /**
228      * Negates the value of this tuple in place.
229      */
230     public final void negate() {
231         this.x = -this.x;
232         this.y = -this.y;
233         this.z = -this.z;
234     }
235
236
237     /**
238      * Sets the value of this tuple to the scalar multiplication
239      * of tuple t1.
240      * @param s the scalar value
241      * @param t1 the source tuple
242      */
243     public final void scale(int s, Tuple3i t1) {
244         this.x = s*t1.x;
245         this.y = s*t1.y;
246         this.z = s*t1.z;
247     }
248
249
250     /**
251      * Sets the value of this tuple to the scalar multiplication
252      * of the scale factor with this.
253      * @param s the scalar value
254      */
255     public final void scale(int s) {
256         this.x *= s;
257         this.y *= s;
258         this.z *= s;
259     }
260
261
262     /**
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
268      */
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;
273     }
274
275
276     /**
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
281      */
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;
286     }
287
288
289     /**
290      * Returns a string that contains the values of this Tuple3i.
291      * The form is (x,y,z).
292      * @return the String representation
293      */
294     public String toString() {
295         return "(" + this.x + ", " + this.y + ", " + this.z + ")";
296     }
297
298
299     /**
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
302      * this Tuple3i.
303      * @param t1  the object with which the comparison is made
304      */
305     public boolean equals(Object t1) {
306         try {
307             Tuple3i t2 = (Tuple3i) t1;
308             return(this.x == t2.x && this.y == t2.y && this.z == t2.z);
309         }
310         catch (NullPointerException e2) {
311             return false;
312         }
313         catch (ClassCastException e1) {
314             return false;
315         }
316     }
317
318
319     /**
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
326      */
327     public int hashCode() {
328         long bits = 1L;
329         bits = 31L * bits + (long)x;
330         bits = 31L * bits + (long)y;
331         bits = 31L * bits + (long)z;
332         return (int) (bits ^ (bits >> 32));
333     }
334
335
336     /**
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
342      */
343     public final void clamp(int min, int max, Tuple3i t) {
344         if( t.x > max ) {
345             x = max;
346         } else if( t.x < min ) {
347             x = min;
348         } else {
349             x = t.x;
350         }
351
352         if( t.y > max ) {
353             y = max;
354         } else if( t.y < min ) {
355             y = min;
356         } else {
357             y = t.y;
358         }
359
360         if( t.z > max ) {
361             z = max;
362         } else if( t.z < min ) {
363             z = min;
364         } else {
365             z = t.z;
366         }
367     }
368
369
370     /**
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
375      */
376     public final void clampMin(int min, Tuple3i t) {
377         if( t.x < min ) {
378             x = min;
379         } else {
380             x = t.x;
381         }
382
383         if( t.y < min ) {
384             y = min;
385         } else {
386             y = t.y;
387         }
388
389         if( t.z < min ) {
390             z = min;
391         } else {
392             z = t.z;
393         }
394     }
395
396
397     /**
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
402      */
403     public final void clampMax(int max, Tuple3i t) {
404         if( t.x > max ) {
405             x = max;
406         } else {
407             x = t.x;
408         }
409
410         if( t.y > max ) {
411             y = max;
412         } else {
413             y = t.y;
414         }
415
416         if( t.z > max ) {
417             z = max;
418         } else {
419             z = t.z;
420         }
421     }
422
423
424     /**
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
428      */
429     public final void absolute(Tuple3i t) {
430         x = Math.abs(t.x);
431         y = Math.abs(t.y);
432         z = Math.abs(t.z);
433     }
434
435
436     /**
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
440      */
441     public final void clamp(int min, int max) {
442         if( x > max ) {
443             x = max;
444         } else if( x < min ) {
445             x = min;
446         }
447
448         if( y > max ) {
449             y = max;
450         } else if( y < min ) {
451             y = min;
452         }
453
454         if( z > max ) {
455             z = max;
456         } else if( z < min ) {
457             z = min;
458         }
459     }
460
461
462     /**
463      *  Clamps the minimum value of this tuple to the min parameter.
464      *  @param min   the lowest value in this tuple after clamping
465      */
466     public final void clampMin(int min) {
467         if (x < min)
468             x=min;
469
470         if (y < min)
471             y = min;
472
473         if (z < min)
474             z = min;
475     }
476
477
478     /**
479      *  Clamps the maximum value of this tuple to the max parameter.
480      *  @param max   the highest value in the tuple after clamping
481      */
482     public final void clampMax(int max) {
483         if (x > max)
484             x = max;
485
486         if (y > max)
487             y = max;
488
489         if (z > max)
490             z = max;
491     }
492
493
494     /**
495      *  Sets each component of this tuple to its absolute value.
496      */
497     public final void absolute() {
498         x = Math.abs(x);
499         y = Math.abs(y);
500         z = Math.abs(z);
501     }
502
503     /**
504      * Creates a new object of the same class as this object.
505      *
506      * @return a clone of this instance.
507      * @exception OutOfMemoryError if there is not enough memory.
508      * @see java.lang.Cloneable
509      * @since vecmath 1.3
510      */
511     public Object clone() {
512         // Since there are no arrays we can just use Object.clone()
513         try {
514             return super.clone();
515         } catch (CloneNotSupportedException e) {
516             // this shouldn't happen, since we are Cloneable
517             throw new InternalError();
518         }
519     }
520
521
522     /**
523          * Get the <i>x</i> coordinate.
524          * 
525          * @return  the <i>x</i> coordinate.
526          * 
527          * @since vecmath 1.5
528          */
529         public final int getX() {
530                 return x;
531         }
532
533
534         /**
535          * Set the <i>x</i> coordinate.
536          * 
537          * @param x  value to <i>x</i> coordinate.
538          * 
539          * @since vecmath 1.5
540          */
541         public final void setX(int x) {
542                 this.x = x;
543         }
544
545
546         /**
547          * Get the <i>y</i> coordinate.
548          * 
549          * @return  the <i>y</i> coordinate.
550          * 
551          * @since vecmath 1.5
552          */
553         public final int getY() {
554                 return y;
555         }
556
557
558         /**
559          * Set the <i>y</i> coordinate.
560          * 
561          * @param y value to <i>y</i> coordinate.
562          * 
563          * @since vecmath 1.5
564          */
565         public final void setY(int y) {
566                 this.y = y;
567         }
568
569         /**
570          * Get the <i>z</i> coordinate.
571          * 
572          * @return the <i>z</i> coordinate.
573          * @since vecmath 1.5
574          */
575         public final int getZ() {
576                 return z;
577         }
578
579
580         /**
581          * Set the <i>z</i> coordinate.
582          * 
583          * @param z value to <i>z</i> coordinate.
584          * 
585          * @since vecmath 1.5
586          */
587         public final void setZ(int z) {
588                 this.z = z;
589         }
590 }