Merge branch 'Jalview-BH/JAL-3026-JAL-3063-JAXB' of
[jalview.git] / srcjar / javajs / util / V3.java
1 /*
2  Copyright (C) 1997,1998,1999
3  Kenji Hiranabe, Eiwa System Management, Inc.
4
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.
8
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.
16  */
17 package javajs.util;
18
19
20 /**
21  * A 3-element vector that is represented by single precision floating point
22  * x,y,z coordinates. If this value represents a normal, then it should be
23  * normalized.
24  * 
25  * @version specification 1.1, implementation $Revision: 1.10 $, $Date:
26  *          2006/10/03 19:52:30 $
27  * @author Kenji hiranabe
28  * 
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
32  */
33 public class V3 extends T3 {
34
35   public V3() {
36   }
37   
38   public static V3 newV(T3 t) {
39     return V3.new3(t.x, t.y, t.z);
40   }
41
42   public static V3 newVsub(T3 t1, T3 t2) {
43     return V3.new3(t1.x - t2.x, t1.y - t2.y,t1.z - t2.z);
44   }
45
46   public static V3 new3(float x, float y, float z) {
47     V3 v = new V3();
48     v.x = x;
49     v.y = y;
50     v.z = z;
51     return v;
52   }
53
54   /**
55    * Returns the angle in radians between this vector and the vector parameter;
56    * the return value is constrained to the range [0,PI].
57    * 
58    * @param v1
59    *        the other vector
60    * @return the angle in radians in the range [0,PI]
61    */
62   public final float angle(V3 v1) {
63     // return (double)Math.acos(dot(v1)/v1.length()/v.length());
64     // Numerically, near 0 and PI are very bad condition for acos.
65     // In 3-space, |atan2(sin,cos)| is much stable.
66
67     double xx = y * v1.z - z * v1.y;
68     double yy = z * v1.x - x * v1.z;
69     double zz = x * v1.y - y * v1.x;
70     double cross = Math.sqrt(xx * xx + yy * yy + zz * zz);
71
72     return (float) Math.abs(Math.atan2(cross, dot(v1)));
73   }
74 }