JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / util / V3d.java
1 /*\r
2    Copyright (C) 1997,1998,1999\r
3    Kenji Hiranabe, Eiwa System Management, Inc.\r
4 \r
5    This program is free software.\r
6    Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),\r
7    conforming to the Java(TM) 3D API specification by Sun Microsystems.\r
8 \r
9    Permission to use, copy, modify, distribute and sell this software\r
10    and its documentation for any purpose is hereby granted without fee,\r
11    provided that the above copyright notice appear in all copies and\r
12    that both that copyright notice and this permission notice appear\r
13    in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.\r
14    makes no representations about the suitability of this software for any\r
15    purpose.  It is provided "AS IS" with NO WARRANTY.\r
16 */\r
17 package javajs.util;\r
18 \r
19 \r
20 \r
21 \r
22 /**\r
23  * A 3 element vector that is represented by double precision floating point\r
24  * x,y,z coordinates. If this value represents a normal, then it should be\r
25  * normalized.\r
26  * \r
27  * @version specification 1.1, implementation $Revision: 1.9 $, $Date:\r
28  *          2006/07/28 17:01:32 $\r
29  * @author Kenji hiranabe\r
30  * \r
31  * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012\r
32  * for unique constructor and method names\r
33  * for the optimization of compiled JavaScript using Java2Script\r
34  */\r
35 public class V3d extends T3d {\r
36 \r
37   /**\r
38    * Sets this vector to be the vector cross product of vectors v1 and v2.\r
39    * \r
40    * @param v1\r
41    *        the first vector\r
42    * @param v2\r
43    *        the second vector\r
44    */\r
45   public final void cross(V3d v1, V3d v2) {\r
46     // store on stack once for aliasing-safty\r
47     // i.e. safe when a.cross(a, b)\r
48     set(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y\r
49         - v1.y * v2.x);\r
50   }\r
51 \r
52   /**\r
53    * Normalizes this vector in place.\r
54    */\r
55   public final void normalize() {\r
56     double d = length();\r
57 \r
58     // zero-div may occur.\r
59     x /= d;\r
60     y /= d;\r
61     z /= d;\r
62   }\r
63 \r
64   /**\r
65    * Computes the dot product of the this vector and vector v.\r
66    * \r
67    * @param v\r
68    *        the other vector\r
69    * @return this.dot.v\r
70    */\r
71   public final double dot(V3d v) {\r
72     return x * v.x + y * v.y + z * v.z;\r
73   }\r
74 \r
75   /**\r
76    * Returns the squared length of this vector.\r
77    * \r
78    * @return the squared length of this vector\r
79    */\r
80   public final double lengthSquared() {\r
81     return x * x + y * y + z * z;\r
82   }\r
83 \r
84   /**\r
85    * Returns the length of this vector.\r
86    * \r
87    * @return the length of this vector\r
88    */\r
89   public final double length() {\r
90     return Math.sqrt(lengthSquared());\r
91   }\r
92 \r
93 }\r