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
|
/*
Copyright (C) 1997,1998,1999
Kenji Hiranabe, Eiwa System Management, Inc.
This program is free software.
Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),
conforming to the Java(TM) 3D API specification by Sun Microsystems.
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.
makes no representations about the suitability of this software for any
purpose. It is provided "AS IS" with NO WARRANTY.
*/
package javajs.util;
import java.io.Serializable;
/**
* A generic 3 element tuple that is represented by double precision floating
* point x,y and z coordinates.
*
* @version specification 1.1, implementation $Revision: 1.9 $, $Date:
* 2006/07/28 17:01:32 $
* @author Kenji hiranabe
*
* additions by Bob Hanson hansonr@stolaf.edu 9/30/2012
* for unique constructor and method names
* for the optimization of compiled JavaScript using Java2Script
*/
public abstract class T3d implements Serializable {
/**
* The x coordinate.
*/
public double x;
/**
* The y coordinate.
*/
public double y;
/**
* The z coordinate.
*/
public double z;
/**
* Constructs and initializes a Tuple3d to (0,0,0).
*/
public T3d() {
}
/**
* 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 from the 3 values specified in the array.
*
* @param t
* the array of length 3 containing xyz in order
*/
public final void setA(double t[]) {
// ArrayIndexOutOfBounds is thrown if t.length < 3
x = t[0];
y = t[1];
z = t[2];
}
/**
* Sets the value of this tuple to the value of the Tuple3d argument.
*
* @param t1
* the tuple to be copied
*/
public final void setT(T3d t1) {
x = t1.x;
y = t1.y;
z = t1.z;
}
/**
* Sets the value of this tuple to the vector sum of tuples t1 and t2.
*
* @param t1
* the first tuple
* @param t2
* the second tuple
*/
public final void add2(T3d t1, T3d t2) {
x = t1.x + t2.x;
y = t1.y + t2.y;
z = t1.z + t2.z;
}
/**
* Sets the value of this tuple to the vector sum of itself and tuple t1.
*
* @param t1
* the other tuple
*/
public final void add(T3d t1) {
x += t1.x;
y += t1.y;
z += t1.z;
}
/**
* Sets the value of this tuple to the vector difference of tuple t1 and t2
* (this = t1 - t2).
*
* @param t1
* the first tuple
* @param t2
* the second tuple
*/
public final void sub2(T3d t1, T3d t2) {
x = t1.x - t2.x;
y = t1.y - t2.y;
z = t1.z - t2.z;
}
/**
* Sets the value of this tuple to the vector difference of itself and tuple
* t1 (this = this - t1).
*
* @param t1
* the other tuple
*/
public final void sub(T3d t1) {
x -= t1.x;
y -= t1.y;
z -= 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) {
x *= s;
y *= s;
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, T3d t1, T3d t2) {
x = s * t1.x + t2.x;
y = s * t1.y + t2.y;
z = s * t1.z + t2.z;
}
/**
* Returns a hash number based on the data values in this object. Two
* different Tuple3d objects with identical data values (ie, returns true for
* equals(Tuple3d) ) will return the same hash number. Two vectors with
* different data members may return the same hash value, although this is not
* likely.
*/
@Override
public int hashCode() {
long xbits = doubleToLongBits0(x);
long ybits = doubleToLongBits0(y);
long zbits = doubleToLongBits0(z);
return (int) (xbits ^ (xbits >> 32) ^ ybits ^ (ybits >> 32) ^ zbits ^ (zbits >> 32));
}
static long doubleToLongBits0(double d) {
// Check for +0 or -0
return (d == 0 ? 0 : Double.doubleToLongBits(d));
}
/**
* Returns true if all of the data members of Tuple3d t1 are equal to the
* corresponding data members in this
*
* @param t1
* the vector with which the comparison is made.
*/
@Override
public boolean equals(Object t1) {
if (!(t1 instanceof T3d))
return false;
T3d t2 = (T3d) t1;
return (this.x == t2.x && this.y == t2.y && this.z == t2.z);
}
/**
* Returns a string that contains the values of this Tuple3d. The form is
* (x,y,z).
*
* @return the String representation
*/
@Override
public String toString() {
return "{" + x + ", " + y + ", " + z + "}";
}
}
|