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