View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class VecMath3D
3    * 
4    * Copyright (c), 1994 - 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:07 $
11   * 
12   * $Id: VecMath3D.java,v 1.3 2004/03/03 11:53:07 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.vecmath;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import javax.vecmath.*;
27  
28  /*====================================================================
29                    Implementation of class VecMath3D                   
30  ====================================================================*/
31  /***
32   * VecMath3D defines many 3D related Vector/Matrix methods.
33   * 
34   * @version $Revision: 1.3 $
35   * @author Masahiro Takatsuka (masa@jbeans.net)
36   */
37  
38  public class VecMath3D {
39  	private VecMath3D() {
40  		super();
41  	}
42  
43  	/***
44  	* Convert the specified Vector3{fd}'s coordinate from
45  	* Spherical to Orthogonal.
46  	*/
47  	public Vector3f sphericalToOrthogonal(Vector3f sp, Vector3f or) {
48  		if (or == null) {
49  			or = new Vector3f();
50  		}
51  		float r = sp.x;
52  		float p = sp.y;
53  		float t = sp.z;
54  		or.x = (float) (r * Math.sin((double)p) * Math.cos((double)t));
55  		or.y = (float) (r * Math.sin((double)p) * Math.sin((double)t));
56  		or.z = (float) (r * Math.cos((double)p));
57  		
58  		return or;
59  	}
60  
61  	public Vector3d sphericalToOrthogonal(Vector3d sp, Vector3d or) {
62  		if (or == null) {
63  			or = new Vector3d();
64  		}
65  		double r = sp.x;
66  		double p = sp.y;
67  		double t = sp.z;
68  		or.x = r * Math.sin(p) * Math.cos(t);
69  		or.y = r * Math.sin(p) * Math.sin(t);
70  		or.z = r * Math.cos(p);
71  		
72  		return or;
73  	}
74  	
75  	/***
76  	 * Convert the specified Vector3{fd}'s coordinate from
77  	 * Orthogonal to Spherical.
78  	 */
79  	public Vector3f orthogonalToSpherical(Vector3f or, Vector3f sp) {
80  		if (sp == null) {
81  			sp = new Vector3f();
82  		}
83  		float x = or.x;
84  		float y = or.y;
85  		float z = or.z;
86  		float r = (float) Math.sqrt((double)(x*x + y*y + z*z)); /* >= 0 */
87  		float p = (r == 0.0f) ? 0.0f : (float) Math.acos((double)(z/r)); /* 0 <= p <= GPZ_PI */
88  		float t = (float) Math.atan2((double)y, (double)x); /* -GPZ_PI <= t <= GPZ_PI */
89  		if (t < 0) {
90  			t += 2 * Math.PI;	/* 0<= t <= PI */
91  		}
92  		sp.x = r;
93  		sp.y = p;
94  		sp.z = t;
95  		
96  		return sp;
97  	}
98  
99  	/***
100 	 * Convert the specified Vector3{fd}'s coordinate from
101 	 * Orthogonal to Spherical.
102 	 */
103 	public Vector3d orthogonalToSpherical(Vector3d or, Vector3d sp) {
104 		if (sp == null) {
105 			sp = new Vector3d();
106 		}
107 		double x = or.x;
108 		double y = or.y;
109 		double z = or.z;
110 		double r = Math.sqrt(x*x + y*y + z*z); /* >= 0 */
111 		double p = (r == 0.0f) ? 0.0f : Math.acos(z/r); /* 0 <= p <= GPZ_PI */
112 		double t = Math.atan2(y, x); /* -GPZ_PI <= t <= GPZ_PI */
113 		if (t < 0) {
114 			t += 2 * Math.PI;	/* 0<= t <= PI */
115 		}
116 		sp.x = r;
117 		sp.y = p;
118 		sp.z = t;
119 		
120 		return sp;
121 	}
122 	
123 	/***
124 	 * Computes a rotation matrix to align the specified Vector to
125 	 * the Y-axis.  The vector is in the Orthogonal coordinate system.
126 	 */
127 	public Matrix3f rotationMatrixToAlignVector(Vector3f v, Matrix3f ra) {
128 		if (ra == null) {
129 			ra = new Matrix3f();
130 		}
131 		float p = v.y;
132 		float t = v.z; 
133 		
134 		float ct = (float) Math.cos(-t);
135 		float st = (float) Math.sin(-t);
136 		float cp = (float) Math.cos(-p);
137 		float sp = (float) Math.sin(-p);
138 
139 		ra.m00 = ct * cp;	ra.m01 = st;	ra.m02 = -ct*sp;
140 		ra.m10 = -st*cp;	ra.m11 = ct;	ra.m12 = st*sp;
141 		ra.m20 = sp;	ra.m21 = 0;	ra.m22 = cp;
142 		
143 		return ra;
144 	}
145 
146 	/***
147 	 * Computes a rotation matrix to align the specified Vector to
148 	 * the Y-axis.  The vector is in the Orthogonal coordinate system.
149 	 */
150 	public Matrix3d rotationMatrixToAlignVector(Vector3d v, Matrix3d ra) {
151 		if (ra == null) {
152 			ra = new Matrix3d();
153 		}
154 		double p = v.y;
155 		double t = v.z; 
156 		
157 		double ct = Math.cos(-t);
158 		double st = Math.sin(-t);
159 		double cp = Math.cos(-p);
160 		double sp = Math.sin(-p);
161 
162 		ra.m00 = ct * cp;	ra.m01 = st;	ra.m02 = -ct*sp;
163 		ra.m10 = -st*cp;	ra.m11 = ct;	ra.m12 = st*sp;
164 		ra.m20 = sp;	ra.m21 = 0;	ra.m22 = cp;
165 		
166 		return ra;
167 	}
168 }
169 
170 
171