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