2 Copyright (C) 1997,1998,1999
3 Kenji Hiranabe, Eiwa System Management, Inc.
5 This program is free software.
6 Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),
7 conforming to the Java(TM) 3D API specification by Sun Microsystems.
9 Permission to use, copy, modify, distribute and sell this software
10 and its documentation for any purpose is hereby granted without fee,
11 provided that the above copyright notice appear in all copies and
12 that both that copyright notice and this permission notice appear
13 in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.
14 makes no representations about the suitability of this software for any
15 purpose. It is provided "AS IS" with NO WARRANTY.
19 import javajs.api.JSONEncodable;
22 * A generic 3 element tuple that is represented by single precision floating
23 * point x,y and z coordinates.
25 * @version specification 1.1, implementation $Revision: 1.10 $, $Date:
26 * 2006/09/08 20:20:20 $
27 * @author Kenji hiranabe
29 * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012
30 * for unique constructor and method names
31 * for the optimization of compiled JavaScript using Java2Script.
34 public abstract class T3 implements JSONEncodable {
39 * Sets the value of this tuple to the specified xyz coordinates.
48 public final void set(float x, float y, float z) {
55 * Sets the value of this tuple from the 3 values specified in the array.
58 * the array of length 3 containing xyz in order
60 public final void setA(float t[]) {
61 // ArrayIndexOutOfBounds is thrown if t.length < 3
68 * Sets the value of this tuple to the value of the Tuple3f argument.
71 * the tuple to be copied
73 public final void setT(T3 t1) {
80 * Sets the value of this tuple to the vector sum of tuples t1 and t2.
87 public final void add2(T3 t1, T3 t2) {
94 * Sets the value of this tuple to the vector sum of itself and tuple t1.
99 public final void add(T3 t1) {
106 * Computes the square of the distance between this point and point p1.
110 * @return the square of distance between these two points as a float
112 public final float distanceSquared(T3 p1) {
113 double dx = x - p1.x;
114 double dy = y - p1.y;
115 double dz = z - p1.z;
116 return (float) (dx * dx + dy * dy + dz * dz);
120 * Returns the distance between this point and point p1.
124 * @return the distance between these two points
126 public final float distance(T3 p1) {
127 return (float) Math.sqrt(distanceSquared(p1));
131 * Sets the value of this tuple to the vector difference of tuple t1 and t2
139 public final void sub2(T3 t1, T3 t2) {
146 * Sets the value of this tuple to the vector difference of itself and tuple
147 * t1 (this = this - t1).
152 public final void sub(T3 t1) {
159 * Sets the value of this tuple to the scalar multiplication of itself.
164 public final void scale(float s) {
177 public final void add3(float a, float b, float c) {
185 * {x*p.x, y*p.y, z*p.z) used for three-way scaling
189 public final void scaleT(T3 p) {
197 * Sets the value of this tuple to the scalar multiplication of tuple t1 and
198 * then adds tuple t2 (this = s*t1 + t2).
203 * the tuple to be multipled
205 * the tuple to be added
207 public final void scaleAdd2(float s, T3 t1, T3 t2) {
215 * average of two tuples
220 public void ave(T3 a, T3 b) {
221 x = (a.x + b.x) / 2f;
222 y = (a.y + b.y) / 2f;
223 z = (a.z + b.z) / 2f;
227 * Vector dot product. Was in Vector3f; more useful here, though.
233 public final float dot(T3 v) {
234 return x * v.x + y * v.y + z * v.z;
238 * Returns the squared length of this vector.
239 * Was in Vector3f; more useful here, though.
241 * @return the squared length of this vector
243 public final float lengthSquared() {
244 return x * x + y * y + z * z;
248 * Returns the length of this vector.
249 * Was in Vector3f; more useful here, though.
251 * @return the length of this vector
253 public final float length() {
254 return (float) Math.sqrt(lengthSquared());
258 * Normalizes this vector in place.
259 * Was in Vector3f; more useful here, though.
261 public final void normalize() {
264 // zero-div may occur.
271 * Sets this tuple to be the vector cross product of vectors v1 and v2.
278 public final void cross(T3 v1, T3 v2) {
279 set(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y
284 * Returns a hash number based on the data values in this object. Two
285 * different Tuple3f objects with identical data values (ie, returns true for
286 * equals(Tuple3f) ) will return the same hash number. Two vectors with
287 * different data members may return the same hash value, although this is not
291 public int hashCode() {
293 bits = 31L * bits + floatToIntBits(x);
294 bits = 31L * bits + floatToIntBits(y);
295 bits = 31L * bits + floatToIntBits(z);
296 return (int) (bits ^ (bits >> 32));
299 static int floatToIntBits(float x) {
300 return (x == 0 ? 0 : Float.floatToIntBits(x));
304 * Returns true if all of the data members of Tuple3f t1 are equal to the
305 * corresponding data members in this
308 * the vector with which the comparison is made.
311 public boolean equals(Object t1) {
312 if (!(t1 instanceof T3))
315 return (x == t2.x && y == t2.y && z == t2.z);
319 * Returns a string that contains the values of this Tuple3f. The form is
322 * @return the String representation
325 public String toString() {
326 return "{" + x + ", " + y + ", " + z + "}";
330 public String toJSON() {
331 return "[" + x + "," + y + "," + z + "]";