summaryrefslogtreecommitdiffstats
path: root/src/javax/vecmath/Tuple3d.java
blob: 3d1c26b24e8a10d7a12696018a483e3c449a00f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * $RCSfile$
 *
 * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

package javax.vecmath;

import java.lang.Math;

/**
 * A generic 3-element tuple that is represented by double-precision 
 * floating point x,y,z coordinates.
 *
 */
public abstract class Tuple3d implements java.io.Serializable, Cloneable {

    static final long serialVersionUID = 5542096614926168415L;

    /**
     * The x coordinate.
     */
    public	double	x;

    /**
     * The y coordinate.
     */
    public	double	y;

    /**
     * The z coordinate.
     */
    public	double	z;


    /**
     * Constructs and initializes a Tuple3d from the specified xyz coordinates.
     * @param x the x coordinate
     * @param y the y coordinate
     * @param z the z coordinate
     */
    public Tuple3d(double x, double y, double z)
    {
	this.x = x;
	this.y = y;
	this.z = z;
    }

    /**
     * Constructs and initializes a Tuple3d from the array of length 3.
     * @param t the array of length 3 containing xyz in order
     */
    public Tuple3d(double[] t)
    {
	this.x = t[0];
	this.y = t[1];
	this.z = t[2];
    }

    /**
     * Constructs and initializes a Tuple3d from the specified Tuple3d.
     * @param t1 the Tuple3d containing the initialization x y z data
     */
    public Tuple3d(Tuple3d t1)
    {
	this.x = t1.x;
	this.y = t1.y;
	this.z = t1.z;
    }

    /**
     * Constructs and initializes a Tuple3d from the specified Tuple3f.
     * @param t1 the Tuple3f containing the initialization x y z data
     */
    public Tuple3d(Tuple3f t1)
    {
	this.x = (double) t1.x;
	this.y = (double) t1.y;
	this.z = (double) t1.z;
    }

    /**
     * Constructs and initializes a Tuple3d to (0,0,0).
     */
    public Tuple3d()
    {
	this.x = (double) 0.0;
	this.y = (double) 0.0;
	this.z = (double) 0.0;
    }

    /**
     * Sets the value of this tuple to the specified xyz coordinates.
     * @param x the x coordinate
     * @param y the y coordinate
     * @param z the z coordinate
     */
    public final void set(double x, double y, double z)
    {
	this.x = x;
	this.y = y;
	this.z = z;
    }

    /**
     * Sets the value of this tuple to the value of the xyz coordinates
     * located in the array of length 3.
     * @param t the array of length 3 containing xyz in order
     */
    public final void set(double[] t)
    {
	this.x = t[0];
	this.y = t[1];
	this.z = t[2];
    }

    /**
     * Sets the value of this tuple to the value of tuple t1.
     * @param t1 the tuple to be copied
     */
    public final void set(Tuple3d t1)
    {
	this.x = t1.x;
	this.y = t1.y;
	this.z = t1.z;
    }

    /**
     * Sets the value of this tuple to the value of tuple t1.
     * @param t1 the tuple to be copied
     */
    public final void set(Tuple3f t1)
    {
	this.x = (double) t1.x;
	this.y = (double) t1.y;
	this.z = (double) t1.z;
    }

   /**
     * Copies the x,y,z coordinates of this tuple into the array t
     * of length 3.
     * @param t  the target array 
     */
    public final void get(double[] t)
    {
        t[0] = this.x;
        t[1] = this.y;
        t[2] = this.z;
    }


   /**
     * Copies the x,y,z coordinates of this tuple into the tuple t.
     * @param t  the Tuple3d object into which the values of this object are copied
     */
    public final void get(Tuple3d t)
    {
        t.x = this.x;
        t.y = this.y;
        t.z = this.z;
    }


    /**
     * Sets the value of this tuple to the sum of tuples t1 and t2.
     * @param t1 the first tuple
     * @param t2 the second tuple
     */
    public final void add(Tuple3d t1, Tuple3d t2)
    {
	this.x = t1.x + t2.x;
	this.y = t1.y + t2.y;
	this.z = t1.z + t2.z;
    }


    /**  
     * Sets the value of this tuple to the sum of itself and t1.
     * @param t1 the other tuple
     */  
    public final void add(Tuple3d t1)
    { 
        this.x += t1.x;
        this.y += t1.y;
        this.z += t1.z;
    }

    /**
     * Sets the value of this tuple to the difference of tuples
     * t1 and t2 (this = t1 - t2).
     * @param t1 the first tuple
     * @param t2 the second tuple
     */
    public final void sub(Tuple3d t1, Tuple3d t2)
    {
	this.x = t1.x - t2.x;
	this.y = t1.y - t2.y;
	this.z = t1.z - t2.z;
    }
 
    /**  
     * Sets the value of this tuple to the difference
     * of itself and t1 (this = this - t1).
     * @param t1 the other tuple
     */  
    public final void sub(Tuple3d t1)
    { 
        this.x -= t1.x;
        this.y -= t1.y;
        this.z -= t1.z;
    }


    /**
     * Sets the value of this tuple to the negation of tuple t1.
     * @param t1 the source tuple
     */
    public final void negate(Tuple3d t1)
    {
	this.x = -t1.x;
	this.y = -t1.y;
	this.z = -t1.z;
    }


    /**
     * Negates the value of this tuple in place.
     */
    public final void negate()
    {
	this.x = -this.x;
	this.y = -this.y;
	this.z = -this.z;
    }


    /**
     * Sets the value of this tuple to the scalar multiplication
     * of tuple t1.
     * @param s the scalar value
     * @param t1 the source tuple
     */
    public final void scale(double s, Tuple3d t1)
    {
	this.x = s*t1.x;
	this.y = s*t1.y;
	this.z = s*t1.z;
    }


    /**
     * Sets the value of this tuple to the scalar multiplication
     * of itself.
     * @param s the scalar value
     */
    public final void scale(double s)
    {
        this.x *= s;
        this.y *= s;
        this.z *= s;
    }


    /**
     * Sets the value of this tuple to the scalar multiplication
     * of tuple t1 and then adds tuple t2 (this = s*t1 + t2).
     * @param s the scalar value
     * @param t1 the tuple to be multipled
     * @param t2 the tuple to be added
     */
    public final void scaleAdd(double s, Tuple3d t1, Tuple3d t2)
    {
	this.x = s*t1.x + t2.x;
	this.y = s*t1.y + t2.y;
	this.z = s*t1.z + t2.z;
    }


    /**
     * @deprecated Use scaleAdd(double,Tuple3d) instead
     */  
    public final void scaleAdd(double s, Tuple3f t1) {
	scaleAdd(s, new Point3d(t1));
    }


    /**
     * Sets the value of this tuple to the scalar multiplication
     * of itself and then adds tuple t1 (this = s*this + t1).
     * @param s the scalar value
     * @param t1 the tuple to be added
     */  
    public final void scaleAdd(double s, Tuple3d t1) {
        this.x = s*this.x + t1.x;
        this.y = s*this.y + t1.y;
        this.z = s*this.z + t1.z;
    }



   /**
     * Returns a string that contains the values of this Tuple3d.
     * The form is (x,y,z).
     * @return the String representation
     */  
    public String toString() {
        return "(" + this.x + ", " + this.y + ", " + this.z + ")";
    }


    /**
     * Returns a hash code value based on the data values in this
     * object.  Two different Tuple3d objects with identical data values
     * (i.e., Tuple3d.equals returns true) will return the same hash
     * code value.  Two objects with different data members may return the
     * same hash value, although this is not likely.
     * @return the integer hash code value
     */  
    public int hashCode() {
	long bits = 1L;
	bits = 31L * bits + VecMathUtil.doubleToLongBits(x);
	bits = 31L * bits + VecMathUtil.doubleToLongBits(y);
	bits = 31L * bits + VecMathUtil.doubleToLongBits(z);
	return (int) (bits ^ (bits >> 32));
    }


   /**
     * Returns true if all of the data members of Tuple3d t1 are
     * equal to the corresponding data members in this Tuple3d.
     * @param t1  the tuple with which the comparison is made
     * @return  true or false
     */  
    public boolean equals(Tuple3d t1)
    {
      try {
        return(this.x == t1.x && this.y == t1.y && this.z == t1.z);
      }
      catch (NullPointerException e2) {return false;}
    }

   /**
     * Returns true if the Object t1 is of type Tuple3d and all of the
     * data members of t1 are equal to the corresponding data members in
     * this Tuple3d.
     * @param t1  the Object with which the comparison is made
     * @return  true or false
     */  
    public boolean equals(Object t1)
    {
      try {
           Tuple3d t2 = (Tuple3d) t1;
           return(this.x == t2.x && this.y == t2.y && this.z == t2.z);
      }
      catch (ClassCastException   e1) {return false;}
      catch (NullPointerException e2) {return false;}

    }

   /**
     * Returns true if the L-infinite distance between this tuple
     * and tuple t1 is less than or equal to the epsilon parameter, 
     * otherwise returns false.  The L-infinite
     * distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2)].
     * @param t1  the tuple to be compared to this tuple
     * @param epsilon  the threshold value  
     * @return  true or false
     */
    public boolean epsilonEquals(Tuple3d t1, double epsilon)
    {
       double diff;

       diff = x - t1.x;
       if((diff<0?-diff:diff) > epsilon) return false;

       diff = y - t1.y;
       if((diff<0?-diff:diff) > epsilon) return false;

       diff = z - t1.z;
       if((diff<0?-diff:diff) > epsilon) return false;

       return true;

    }


    /**
     * @deprecated Use clamp(double,double,Tuple3d) instead
     */
    public final void clamp(float min, float max, Tuple3d t) {
	clamp((double)min, (double)max, t);
    }


    /**
     *  Clamps the tuple parameter to the range [low, high] and 
     *  places the values into this tuple.  
     *  @param min   the lowest value in the tuple after clamping
     *  @param max  the highest value in the tuple after clamping 
     *  @param t   the source tuple, which will not be modified
     */
    public final void clamp(double min, double max, Tuple3d t) {
        if( t.x > max ) {
          x = max;
        } else if( t.x < min ){
          x = min;
        } else {
          x = t.x;
        }
 
        if( t.y > max ) {
          y = max;
        } else if( t.y < min ){
          y = min;
        } else {
          y = t.y;
        }
 
        if( t.z > max ) {
          z = max;
        } else if( t.z < min ){
          z = min;
        } else {
          z = t.z;
        }

   }


    /** 
     * @deprecated Use clampMin(double,Tuple3d) instead
     */   
    public final void clampMin(float min, Tuple3d t) {
	clampMin((double)min, t);
    }


    /** 
     *  Clamps the minimum value of the tuple parameter to the min 
     *  parameter and places the values into this tuple.
     *  @param min   the lowest value in the tuple after clamping 
     *  @param t   the source tuple, which will not be modified
     */   
    public final void clampMin(double min, Tuple3d t) { 
        if( t.x < min ) {
          x = min;
        } else {
          x = t.x;
        }
 
        if( t.y < min ) {
          y = min;
        } else {
          y = t.y;
        }
 
        if( t.z < min ) {
          z = min;
        } else {
          z = t.z;
        }

   } 


    /** 
     * @deprecated Use clampMax(double,Tuple3d) instead
     */   
    public final void clampMax(float max, Tuple3d t) {
	clampMax((double)max, t);
    }


    /**  
     *  Clamps the maximum value of the tuple parameter to the max 
     *  parameter and places the values into this tuple.
     *  @param max the highest value in the tuple after clamping  
     *  @param t   the source tuple, which will not be modified
     */    
    public final void clampMax(double max, Tuple3d t) {  
        if( t.x > max ) {
          x = max;
        } else {
          x = t.x;
        }
 
        if( t.y > max ) {
          y = max;
        } else {
          y = t.y;
        }
 
        if( t.z > max ) {
          z = max;
        } else {
          z = t.z;
        }

   } 


  /**  
    *  Sets each component of the tuple parameter to its absolute 
    *  value and places the modified values into this tuple.
    *  @param t   the source tuple, which will not be modified
    */    
  public final void absolute(Tuple3d t)
  {
       x = Math.abs(t.x);
       y = Math.abs(t.y);
       z = Math.abs(t.z);

  } 


    /**
     * @deprecated Use clamp(double,double) instead
     */
    public final void clamp(float min, float max) {
	clamp((double)min, (double)max);
    }


    /**
     *  Clamps this tuple to the range [low, high].
     *  @param min  the lowest value in this tuple after clamping
     *  @param max  the highest value in this tuple after clamping
     */
    public final void clamp(double min, double max) {
        if( x > max ) {
          x = max;
        } else if( x < min ){
          x = min;
        }
 
        if( y > max ) {
          y = max;
        } else if( y < min ){
          y = min;
        }
 
        if( z > max ) {
          z = max;
        } else if( z < min ){
          z = min;
        }

   }

 
    /** 
     * @deprecated Use clampMin(double) instead
     */   
    public final void clampMin(float min) {
	clampMin((double)min);
    }


    /**
     *  Clamps the minimum value of this tuple to the min parameter.
     *  @param min   the lowest value in this tuple after clamping
     */
    public final void clampMin(double min) { 
      if( x < min ) x=min;
      if( y < min ) y=min;
      if( z < min ) z=min;

   } 
 
 
    /** 
     * @deprecated Use clampMax(double) instead
     */   
    public final void clampMax(float max) {
	clampMax((double)max);
    }


    /**
     *  Clamps the maximum value of this tuple to the max parameter.
     *  @param max   the highest value in the tuple after clamping
     */
    public final void clampMax(double max) { 
      if( x > max ) x=max;
      if( y > max ) y=max;
      if( z > max ) z=max;
   }


  /**
    *  Sets each component of this tuple to its absolute value.
    */
  public final void absolute()
  {
     x = Math.abs(x);
     y = Math.abs(y);
     z = Math.abs(z);
  }


    /**
     * @deprecated Use interpolate(Tuple3d,Tuple3d,double) instead
     */
    public final void interpolate(Tuple3d t1, Tuple3d t2, float alpha) {
	interpolate(t1, t2, (double)alpha);
    }


    /**
     *  Linearly interpolates between tuples t1 and t2 and places the 
     *  result into this tuple:  this = (1-alpha)*t1 + alpha*t2.
     *  @param t1  the first tuple
     *  @param t2  the second tuple  
     *  @param alpha  the alpha interpolation parameter  
     */   
    public final void interpolate(Tuple3d t1, Tuple3d t2, double alpha) {
	this.x = (1-alpha)*t1.x + alpha*t2.x;
	this.y = (1-alpha)*t1.y + alpha*t2.y;
	this.z = (1-alpha)*t1.z + alpha*t2.z;
    }
 
 
    /**
     * @deprecated Use interpolate(Tuple3d,double) instead
     */
    public final void interpolate(Tuple3d t1, float alpha) {
	interpolate(t1, (double)alpha);
    }


    /**   
    *  Linearly interpolates between this tuple and tuple t1 and 
    *  places the result into this tuple:  this = (1-alpha)*this + alpha*t1. 
    *  @param t1  the first tuple 
    *  @param alpha  the alpha interpolation parameter   
    */    
    public final void interpolate(Tuple3d t1, double alpha) {
	this.x = (1-alpha)*this.x + alpha*t1.x;
	this.y = (1-alpha)*this.y + alpha*t1.y;
	this.z = (1-alpha)*this.z + alpha*t1.z;
    }  
 
    /**
     * Creates a new object of the same class as this object.
     *
     * @return a clone of this instance.
     * @exception OutOfMemoryError if there is not enough memory.
     * @see java.lang.Cloneable
     * @since vecmath 1.3
     */
    public Object clone() {
	// Since there are no arrays we can just use Object.clone()
	try {
	    return super.clone();
	} catch (CloneNotSupportedException e) {
	    // this shouldn't happen, since we are Cloneable
	    throw new InternalError();
	}
    }

}