JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / util / V3.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  * A 3-element vector that is represented by single precision floating point\r
22  * x,y,z coordinates. If this value represents a normal, then it should be\r
23  * normalized.\r
24  * \r
25  * @version specification 1.1, implementation $Revision: 1.10 $, $Date:\r
26  *          2006/10/03 19:52:30 $\r
27  * @author Kenji hiranabe\r
28  * \r
29  * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012\r
30  * for unique constructor and method names\r
31  * for the optimization of compiled JavaScript using Java2Script\r
32  */\r
33 public class V3 extends T3 {\r
34 \r
35   /**\r
36    * @j2sIgnoreSuperConstructor\r
37    */\r
38   public V3() {\r
39     // ignore T3\r
40   }\r
41   public static V3 newV(T3 t) {\r
42     return V3.new3(t.x, t.y, t.z);\r
43   }\r
44 \r
45   public static V3 newVsub(T3 t1, T3 t2) {\r
46     return V3.new3(t1.x - t2.x, t1.y - t2.y,t1.z - t2.z);\r
47   }\r
48 \r
49   public static V3 new3(float x, float y, float z) {\r
50     V3 v = new V3();\r
51     v.x = x;\r
52     v.y = y;\r
53     v.z = z;\r
54     return v;\r
55   }\r
56 \r
57   /**\r
58    * Returns the angle in radians between this vector and the vector parameter;\r
59    * the return value is constrained to the range [0,PI].\r
60    * \r
61    * @param v1\r
62    *        the other vector\r
63    * @return the angle in radians in the range [0,PI]\r
64    */\r
65   public final float angle(V3 v1) {\r
66     // return (double)Math.acos(dot(v1)/v1.length()/v.length());\r
67     // Numerically, near 0 and PI are very bad condition for acos.\r
68     // In 3-space, |atan2(sin,cos)| is much stable.\r
69 \r
70     double xx = y * v1.z - z * v1.y;\r
71     double yy = z * v1.x - x * v1.z;\r
72     double zz = x * v1.y - y * v1.x;\r
73     double cross = Math.sqrt(xx * xx + yy * yy + zz * zz);\r
74 \r
75     return (float) Math.abs(Math.atan2(cross, dot(v1)));\r
76   }\r
77 }\r