]> gerrit.simantics Code Review - simantics/3d.git/blob - javax.vecmath/src/javax/vecmath/Tuple4i.java
Included old javax.vecmath 1.5.2 to org.simantics.g3d.feature
[simantics/3d.git] / javax.vecmath / src / javax / vecmath / Tuple4i.java
1 /*
2  * $RCSfile: Tuple4i.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 4-element tuple represented by signed integer x,y,z,w
38  * coordinates.
39  *
40  * @since vecmath 1.2
41  */
42 public abstract class Tuple4i implements java.io.Serializable, Cloneable {
43
44     static final long serialVersionUID = 8064614250942616720L;
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      * The w coordinate.
63      */
64     public int w;
65
66
67     /**
68      * Constructs and initializes a Tuple4i from the specified
69      * x, y, z, and w coordinates.
70      * @param x the x coordinate
71      * @param y the y coordinate
72      * @param z the z coordinate
73      * @param w the w coordinate
74      */
75     public Tuple4i(int x, int y, int z, int w) {
76         this.x = x;
77         this.y = y;
78         this.z = z;
79         this.w = w;
80     }
81
82
83     /**
84      * Constructs and initializes a Tuple4i from the array of length 4.
85      * @param t the array of length 4 containing x, y, z, and w in order.
86      */
87     public Tuple4i(int[] t) {
88         this.x = t[0];
89         this.y = t[1];
90         this.z = t[2];
91         this.w = t[3];
92     }
93
94
95     /**
96      * Constructs and initializes a Tuple4i from the specified Tuple4i.
97      * @param t1 the Tuple4i containing the initialization x, y, z,
98      * and w data.
99      */
100     public Tuple4i(Tuple4i t1) {
101         this.x = t1.x;
102         this.y = t1.y;
103         this.z = t1.z;
104         this.w = t1.w;
105     }
106
107
108     /**
109      * Constructs and initializes a Tuple4i to (0,0,0,0).
110      */
111     public Tuple4i() {
112         this.x = 0;
113         this.y = 0;
114         this.z = 0;
115         this.w = 0;
116     }
117
118
119     /**
120      * Sets the value of this tuple to the specified x, y, z, and w
121      * coordinates.
122      * @param x the x coordinate
123      * @param y the y coordinate
124      * @param z the z coordinate
125      * @param w the w coordinate
126      */
127     public final void set(int x, int y, int z, int w) {
128         this.x = x;
129         this.y = y;
130         this.z = z;
131         this.w = w;
132     }
133
134
135     /**
136      * Sets the value of this tuple to the specified coordinates in the
137      * array of length 4.
138      * @param t the array of length 4 containing x, y, z, and w in order.
139      */
140     public final void set(int[] t) {
141         this.x = t[0];
142         this.y = t[1];
143         this.z = t[2];
144         this.w = t[3];
145     }
146
147
148     /**
149      * Sets the value of this tuple to the value of tuple t1.
150      * @param t1 the tuple to be copied
151      */
152     public final void set(Tuple4i t1) {
153         this.x = t1.x;
154         this.y = t1.y;
155         this.z = t1.z;
156         this.w = t1.w;
157     }
158
159
160     /**
161      * Copies the values of this tuple into the array t.
162      * @param t the array
163      */
164     public final void get(int[] t) {
165         t[0] = this.x;
166         t[1] = this.y;
167         t[2] = this.z;
168         t[3] = this.w;
169     }
170
171
172     /**
173      * Copies the values of this tuple into the tuple t.
174      * @param t the target tuple
175      */
176     public final void get(Tuple4i t) {
177         t.x = this.x;
178         t.y = this.y;
179         t.z = this.z;
180         t.w = this.w;
181     }
182
183
184     /**
185      * Sets the value of this tuple to the sum of tuples t1 and t2.
186      * @param t1 the first tuple
187      * @param t2 the second tuple
188      */
189     public final void add(Tuple4i t1, Tuple4i t2) {
190         this.x = t1.x + t2.x;
191         this.y = t1.y + t2.y;
192         this.z = t1.z + t2.z;
193         this.w = t1.w + t2.w;
194     }
195
196
197     /**
198      * Sets the value of this tuple to the sum of itself and t1.
199      * @param t1 the other tuple
200      */
201     public final void add(Tuple4i t1) {
202         this.x += t1.x;
203         this.y += t1.y;
204         this.z += t1.z;
205         this.w += t1.w;
206     }
207
208
209     /**
210      * Sets the value of this tuple to the difference
211      * of tuples t1 and t2 (this = t1 - t2).
212      * @param t1 the first tuple
213      * @param t2 the second tuple
214      */
215     public final void sub(Tuple4i t1, Tuple4i t2) {
216         this.x = t1.x - t2.x;
217         this.y = t1.y - t2.y;
218         this.z = t1.z - t2.z;
219         this.w = t1.w - t2.w;
220     }
221
222
223     /**
224      * Sets the value of this tuple to the difference
225      * of itself and t1 (this = this - t1).
226      * @param t1 the other tuple
227      */
228     public final void sub(Tuple4i t1) {
229         this.x -= t1.x;
230         this.y -= t1.y;
231         this.z -= t1.z;
232         this.w -= t1.w;
233     }
234
235
236     /**
237      * Sets the value of this tuple to the negation of tuple t1.
238      * @param t1 the source tuple
239      */
240     public final void negate(Tuple4i t1) {
241         this.x = -t1.x;
242         this.y = -t1.y;
243         this.z = -t1.z;
244         this.w = -t1.w;
245     }
246
247
248     /**
249      * Negates the value of this tuple in place.
250      */
251     public final void negate() {
252         this.x = -this.x;
253         this.y = -this.y;
254         this.z = -this.z;
255         this.w = -this.w;
256     }
257
258
259     /**
260      * Sets the value of this tuple to the scalar multiplication
261      * of tuple t1.
262      * @param s the scalar value
263      * @param t1 the source tuple
264      */
265     public final void scale(int s, Tuple4i t1) {
266         this.x = s*t1.x;
267         this.y = s*t1.y;
268         this.z = s*t1.z;
269         this.w = s*t1.w;
270     }
271
272
273     /**
274      * Sets the value of this tuple to the scalar multiplication
275      * of the scale factor with this.
276      * @param s the scalar value
277      */
278     public final void scale(int s) {
279         this.x *= s;
280         this.y *= s;
281         this.z *= s;
282         this.w *= s;
283     }
284
285
286     /**
287      * Sets the value of this tuple to the scalar multiplication
288      * of tuple t1 plus tuple t2 (this = s*t1 + t2).
289      * @param s the scalar value
290      * @param t1 the tuple to be multipled
291      * @param t2 the tuple to be added
292      */
293     public final void scaleAdd(int s, Tuple4i t1, Tuple4i t2) {
294         this.x = s*t1.x + t2.x;
295         this.y = s*t1.y + t2.y;
296         this.z = s*t1.z + t2.z;
297         this.w = s*t1.w + t2.w;
298     }
299
300
301     /**
302      * Sets the value of this tuple to the scalar multiplication
303      * of itself and then adds tuple t1 (this = s*this + t1).
304      * @param s the scalar value
305      * @param t1 the tuple to be added
306      */
307     public final void scaleAdd(int s, Tuple4i t1) {
308         this.x = s*this.x + t1.x;
309         this.y = s*this.y + t1.y;
310         this.z = s*this.z + t1.z;
311         this.w = s*this.w + t1.w;
312     }
313
314
315     /**
316      * Returns a string that contains the values of this Tuple4i.
317      * The form is (x,y,z,w).
318      * @return the String representation
319      */
320     public String toString() {
321         return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.w + ")";
322     }
323
324
325     /**
326      * Returns true if the Object t1 is of type Tuple4i and all of the
327      * data members of t1 are equal to the corresponding data members in
328      * this Tuple4i.
329      * @param t1  the object with which the comparison is made
330      * @return  true or false
331      */
332     public boolean equals(Object t1) {
333         try {
334             Tuple4i t2 = (Tuple4i) t1;
335             return(this.x == t2.x && this.y == t2.y &&
336                    this.z == t2.z && this.w == t2.w);
337         }
338         catch (NullPointerException e2) {
339             return false;
340         }
341         catch (ClassCastException e1) {
342             return false;
343         }
344     }
345
346
347     /**
348      * Returns a hash code value based on the data values in this
349      * object.  Two different Tuple4i objects with identical data values
350      * (i.e., Tuple4i.equals returns true) will return the same hash
351      * code value.  Two objects with different data members may return the
352      * same hash value, although this is not likely.
353      * @return the integer hash code value
354      */
355     public int hashCode() {
356         long bits = 1L;
357         bits = 31L * bits + (long)x;
358         bits = 31L * bits + (long)y;
359         bits = 31L * bits + (long)z;
360         bits = 31L * bits + (long)w;
361         return (int) (bits ^ (bits >> 32));
362     }
363
364
365     /**
366      *  Clamps the tuple parameter to the range [low, high] and
367      *  places the values into this tuple.
368      *  @param min   the lowest value in the tuple after clamping
369      *  @param max  the highest value in the tuple after clamping
370      *  @param t   the source tuple, which will not be modified
371      */
372     public final void clamp(int min, int max, Tuple4i t) {
373         if( t.x > max ) {
374             x = max;
375         } else if( t.x < min ) {
376             x = min;
377         } else {
378             x = t.x;
379         }
380
381         if( t.y > max ) {
382             y = max;
383         } else if( t.y < min ) {
384             y = min;
385         } else {
386             y = t.y;
387         }
388
389         if( t.z > max ) {
390             z = max;
391         } else if( t.z < min ) {
392             z = min;
393         } else {
394             z = t.z;
395         }
396
397         if( t.w > max ) {
398             w = max;
399         } else if( t.w < min ) {
400             w = min;
401         } else {
402             w = t.w;
403         }
404     }
405
406
407     /**
408      *  Clamps the minimum value of the tuple parameter to the min
409      *  parameter and places the values into this tuple.
410      *  @param min   the lowest value in the tuple after clamping
411      *  @param t   the source tuple, which will not be modified
412      */
413     public final void clampMin(int min, Tuple4i t) {
414         if( t.x < min ) {
415             x = min;
416         } else {
417             x = t.x;
418         }
419
420         if( t.y < min ) {
421             y = min;
422         } else {
423             y = t.y;
424         }
425
426         if( t.z < min ) {
427             z = min;
428         } else {
429             z = t.z;
430         }
431
432         if( t.w < min ) {
433             w = min;
434         } else {
435             w = t.w;
436         }
437
438
439     }
440
441
442     /**
443      *  Clamps the maximum value of the tuple parameter to the max
444      *  parameter and places the values into this tuple.
445      *  @param max   the highest value in the tuple after clamping
446      *  @param t   the source tuple, which will not be modified
447      */
448     public final void clampMax(int max, Tuple4i t) {
449         if( t.x > max ) {
450             x = max;
451         } else {
452             x = t.x;
453         }
454
455         if( t.y > max ) {
456             y = max;
457         } else {
458             y = t.y;
459         }
460
461         if( t.z > max ) {
462             z = max;
463         } else {
464             z = t.z;
465         }
466
467         if( t.w > max ) {
468             w = max;
469         } else {
470             w = t.z;
471         }
472     }
473
474
475     /**
476      *  Sets each component of the tuple parameter to its absolute
477      *  value and places the modified values into this tuple.
478      *  @param t   the source tuple, which will not be modified
479      */
480     public final void absolute(Tuple4i t) {
481         x = Math.abs(t.x);
482         y = Math.abs(t.y);
483         z = Math.abs(t.z);
484         w = Math.abs(t.w);
485     }
486
487
488     /**
489      *  Clamps this tuple to the range [low, high].
490      *  @param min  the lowest value in this tuple after clamping
491      *  @param max  the highest value in this tuple after clamping
492      */
493     public final void clamp(int min, int max) {
494         if( x > max ) {
495             x = max;
496         } else if( x < min ) {
497             x = min;
498         }
499
500         if( y > max ) {
501             y = max;
502         } else if( y < min ) {
503             y = min;
504         }
505
506         if( z > max ) {
507             z = max;
508         } else if( z < min ) {
509             z = min;
510         }
511
512         if( w > max ) {
513             w = max;
514         } else if( w < min ) {
515             w = min;
516         }
517     }
518
519
520     /**
521      *  Clamps the minimum value of this tuple to the min parameter.
522      *  @param min   the lowest value in this tuple after clamping
523      */
524     public final void clampMin(int min) {
525         if (x < min)
526             x=min;
527
528         if (y < min)
529             y = min;
530
531         if (z < min)
532             z = min;
533
534         if (w < min)
535             w = min;
536     }
537
538
539     /**
540      *  Clamps the maximum value of this tuple to the max parameter.
541      *  @param max   the highest value in the tuple after clamping
542      */
543     public final void clampMax(int max) {
544         if (x > max)
545             x = max;
546
547         if (y > max)
548             y = max;
549
550         if (z > max)
551             z = max;
552
553         if (w > max)
554             w = max;
555     }
556
557
558     /**
559      *  Sets each component of this tuple to its absolute value.
560      */
561     public final void absolute() {
562         x = Math.abs(x);
563         y = Math.abs(y);
564         z = Math.abs(z);
565         w = Math.abs(w);
566     }
567
568     /**
569      * Creates a new object of the same class as this object.
570      *
571      * @return a clone of this instance.
572      * @exception OutOfMemoryError if there is not enough memory.
573      * @see java.lang.Cloneable
574      * @since vecmath 1.3
575      */
576     public Object clone() {
577         // Since there are no arrays we can just use Object.clone()
578         try {
579             return super.clone();
580         } catch (CloneNotSupportedException e) {
581             // this shouldn't happen, since we are Cloneable
582             throw new InternalError();
583         }
584     }
585
586
587
588     /**
589          * Get the <i>x</i> coordinate.
590          * 
591          * @return the <i>x</i> coordinate.
592          * 
593          * @since vecmath 1.5
594          */
595         public final int getX() {
596                 return x;
597         }
598
599
600         /**
601          * Set the <i>x</i> coordinate.
602          * 
603          * @param x  value to <i>x</i> coordinate.
604          * 
605          * @since vecmath 1.5
606          */
607         public final void setX(int x) {
608                 this.x = x;
609         }
610
611
612         /**
613          * Get the <i>y</i> coordinate.
614          * 
615          * @return  the <i>y</i> coordinate.
616          * 
617          * @since vecmath 1.5
618          */
619         public final int getY() {
620                 return y;
621         }
622
623
624         /**
625          * Set the <i>y</i> coordinate.
626          * 
627          * @param y value to <i>y</i> coordinate.
628          * 
629          * @since vecmath 1.5
630          */
631         public final void setY(int y) {
632                 this.y = y;
633         }
634
635         /**
636          * Get the <i>z</i> coordinate.
637          * 
638          * @return the <i>z</i> coordinate.
639          * 
640          * @since vecmath 1.5
641          */
642         public final int getZ() {
643                 return z;
644         }
645
646
647         /**
648          * Set the <i>z</i> coordinate.
649          * 
650          * @param z value to <i>z</i> coordinate.
651          * 
652          * @since vecmath 1.5
653          */
654         public final void setZ(int z) {
655                 this.z = z;
656         }
657
658
659         /**
660          * Get the <i>w</i> coordinate.
661          * @return the  <i>w</i> coordinate.
662          * @since vecmath 1.5
663          */
664         public final int getW() {
665                 return w;
666         }
667
668
669         /**
670          * Set the <i>w</i> coordinate.
671          * 
672          * @param w value to <i>w</i> coordinate.
673          * 
674          * @since vecmath 1.5
675          */
676         public final void setW(int w) {
677                 this.w = w;
678         }
679
680 }