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