View Javadoc

1   package com.insanityengine.ghia.m3;
2   
3   /***
4    *
5    * <P>
6    * </P>
7    *
8    * @author BrianHammond
9    *
10   * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/m3/Mat4.java,v 1.10 2005/03/19 17:50:02 brian Exp $
11   *
12   */
13  
14  public class Mat4 {
15  	/*** 
16  	 * 
17  	 * Constructor
18  	 * 
19  	 */
20  	public Mat4() { 
21  		mat = new float[ 4 ][ 4 ];
22  	}
23  	
24  	/*** 
25  	 * 
26  	 * Constructor
27  	 * 
28  	 * @param d
29  	 * 
30  	 */
31  	public Mat4( float d ) { 
32  		mat = new float[ 4 ][ 4 ];
33  		set( d ); 
34  	}
35  	
36  	/*** 
37  	 * 
38  	 * Constructor
39  	 * 
40  	 * @param that
41  	 * 
42  	 */
43  	public Mat4( Mat4 that ) {
44  		mat = new float[ 4 ][ 4 ];
45  		set( that );
46  	}
47  
48  	/*** 
49  	 * 
50  	 * set
51  	 * 
52  	 * @param that
53  	 * 
54  	 * @return a Mat4
55  	 * 
56  	 */
57  	public Mat4 set( Mat4 that ) {
58  		if ( null == that ) {
59  			set( 0 );
60  			return this;
61  		}
62  		
63  		mat[ 0 ][ 0 ] = that.mat[ 0 ][ 0 ];  mat[ 1 ][ 0 ] = that.mat[ 1 ][ 0 ];
64  		mat[ 0 ][ 1 ] = that.mat[ 0 ][ 1 ];  mat[ 1 ][ 1 ] = that.mat[ 1 ][ 1 ];
65  		mat[ 0 ][ 2 ] = that.mat[ 0 ][ 2 ];  mat[ 1 ][ 2 ] = that.mat[ 1 ][ 2 ];
66  		mat[ 0 ][ 3 ] = that.mat[ 0 ][ 3 ];  mat[ 1 ][ 3 ] = that.mat[ 1 ][ 3 ];
67  		
68  		mat[ 2 ][ 0 ] = that.mat[ 2 ][ 0 ];  mat[ 3 ][ 0 ] = that.mat[ 3 ][ 0 ];
69  		mat[ 2 ][ 1 ] = that.mat[ 2 ][ 1 ];  mat[ 3 ][ 1 ] = that.mat[ 3 ][ 1 ];
70  		mat[ 2 ][ 2 ] = that.mat[ 2 ][ 2 ];  mat[ 3 ][ 2 ] = that.mat[ 3 ][ 2 ];
71  		mat[ 2 ][ 3 ] = that.mat[ 2 ][ 3 ];  mat[ 3 ][ 3 ] = that.mat[ 3 ][ 3 ];
72  
73  		return this;
74  	}
75  
76  	/*** 
77  	 * 
78  	 * set
79  	 * 
80  	 * @param d
81  	 * 
82  	 * @return a Mat4
83  	 * 
84  	 */
85  	public Mat4 set( float d ) {
86  		mat[ 0 ][ 0 ] = mat[ 0 ][ 0 ] = mat[ 0 ][ 0 ] = mat[ 0 ][ 0 ] = 
87  		mat[ 1 ][ 0 ] = mat[ 1 ][ 0 ] = mat[ 1 ][ 0 ] = mat[ 1 ][ 0 ] = 
88  		mat[ 2 ][ 0 ] = mat[ 2 ][ 0 ] = mat[ 2 ][ 0 ] = mat[ 2 ][ 0 ] = 
89  		mat[ 3 ][ 0 ] = mat[ 3 ][ 0 ] = mat[ 3 ][ 0 ] = mat[ 3 ][ 0 ] = d;
90  		return this;
91  	}
92  
93  	/*** 
94  	 * 
95  	 * loadIdentity
96  	 * 
97  	 * @return a Mat4
98  	 * 
99  	 */
100 	public Mat4 loadIdentity() {
101 		mat[ 0 ][ 0 ] = mat[ 0 ][ 1 ] = mat[ 0 ][ 2 ] = mat[ 0 ][ 3 ] = 
102 		mat[ 1 ][ 0 ] = mat[ 1 ][ 1 ] = mat[ 1 ][ 2 ] = mat[ 1 ][ 3 ] = 
103 		mat[ 2 ][ 0 ] = mat[ 2 ][ 1 ] = mat[ 2 ][ 2 ] = mat[ 2 ][ 3 ] = 
104 		mat[ 3 ][ 0 ] = mat[ 3 ][ 1 ] = mat[ 3 ][ 2 ] = mat[ 3 ][ 3 ] = 0;
105 		mat[ 0 ][ 0 ] = mat[ 1 ][ 1 ] = mat[ 2 ][ 2 ] = mat[ 3 ][ 3 ] = 1;
106 		return this;
107 	}
108 	
109 	/*** 
110 	 * 
111 	 * multiply
112 	 * 
113 	 * @param d
114 	 * @param rez
115 	 * 
116 	 * @return a Mat4
117 	 * 
118 	 */
119 	public Mat4 multiply( float d, Mat4 rez ) {
120 		rez.set( this );
121 
122 		rez.mat[ 0 ][ 0 ] *= d;  rez.mat[ 1 ][ 0 ] *= d;
123 		rez.mat[ 0 ][ 1 ] *= d;  rez.mat[ 1 ][ 1 ] *= d;
124 		rez.mat[ 0 ][ 2 ] *= d;  rez.mat[ 1 ][ 2 ] *= d;
125 		rez.mat[ 0 ][ 3 ] *= d;  rez.mat[ 1 ][ 3 ] *= d;
126 		
127 		rez.mat[ 2 ][ 0 ] *= d;  rez.mat[ 3 ][ 0 ] *= d;
128 		rez.mat[ 2 ][ 1 ] *= d;  rez.mat[ 3 ][ 1 ] *= d;
129 		rez.mat[ 2 ][ 2 ] *= d;  rez.mat[ 3 ][ 2 ] *= d;
130 		rez.mat[ 2 ][ 3 ] *= d;  rez.mat[ 3 ][ 3 ] *= d;
131 
132 		return rez;
133 	}
134 	
135 	/*** 
136 	 * 
137 	 * multiply ( result in this )
138 	 * 
139 	 * @param m
140 	 * @param n
141 	 * 
142 	 * @return a Mat4
143 	 * 
144 	 */
145     public Mat4 multiply( Mat4 m, Mat4 n ) {
146 		int i, j;
147 		for ( i = 0 ; i < 4 ; i++ ) {
148 			for ( j = 0 ; j < 4 ; j++ ) {
149 				
150 				mat[ i ][ j ] = (
151 					+ m.mat[ i ][ 0 ] * n.mat[ 0 ][ j ]
152 					+ m.mat[ i ][ 1 ] * n.mat[ 1 ][ j ]
153 					+ m.mat[ i ][ 2 ] * n.mat[ 2 ][ j ]
154 					+ m.mat[ i ][ 3 ] * n.mat[ 3 ][ j ]
155 				);
156 				
157 			}
158 		}
159 		return this;
160 	}
161 
162 	/*** 
163 	 * 
164 	 * print
165 	 * 
166 	 */
167 	public void print() {
168 		for ( int i = 0 ; i < 4 ; i++ ) {
169 			for ( int j = 0 ; j < 4 ; j++ ) {
170 				System.out.print( mat[ i ][ j ] + "   " );
171 			}
172 			System.out.println();
173 		}
174 		System.out.println();
175 	}
176 
177 	/*** 
178 	 * 
179 	 * rotate
180 	 * 
181 	 * @param angle
182 	 * @param workspace1
183 	 * @param workspace2
184 	 * 
185 	 * @return a Mat4
186 	 * 
187 	 */
188 	public Mat4 rotate( Pt3 angle, Mat4 workspace1, Mat4 workspace2 ) {
189 		float cosx, cosy, cosz;
190 		float sinx, siny, sinz;
191 		
192 		cosx = MumpMath.cos( angle.getX() );  sinx = MumpMath.sin( angle.getX() );
193 		cosy = MumpMath.cos( angle.getY() );  siny = MumpMath.sin( angle.getY() );
194 		cosz = MumpMath.cos( angle.getZ() );  sinz = MumpMath.sin( angle.getZ() );
195 		
196 		/*	
197 			X Rotation
198 				1 0       0      0
199 				0 cos(x)  sin(x) 0
200 				0 -sin(x) cos(x) 0
201 				0 0       0      1
202 		*/		
203 		workspace1.loadIdentity();
204 		workspace1.mat[ 1 ][ 1 ] = cosx;
205 		workspace1.mat[ 1 ][ 2 ] = sinx;
206 		workspace1.mat[ 2 ][ 1 ] = -sinx;
207 		workspace1.mat[ 2 ][ 2 ] = cosx;
208 		workspace2.multiply( this, workspace1 );  // now workspace2 holds curr
209 
210 		/*
211 			Y Rotation
212 				cos(y) 0 -sin(y) 0
213 				0      1 0       0
214 				sin(y) 0 cos(y)  0
215 				0      0 0       1
216 		*/
217 		this.loadIdentity();
218 		this.mat[ 0 ][ 0 ] = cosy;
219 		this.mat[ 0 ][ 2 ] = -siny;
220 		this.mat[ 2 ][ 0 ] = siny;
221 		this.mat[ 2 ][ 2 ] = cosy;
222 		workspace1.multiply( workspace2, this );   // now workspace1 holds curr
223 
224 		/*
225 			Z Rotation
226 				1 0       0      0
227 				0 cos(z)  sin(z) 0
228 				0 -sin(z) cos(z) 0
229 				0 0       0      1 
230 		*/
231 		workspace2.loadIdentity();
232 		workspace2.mat[ 1 ][ 1 ] = cosz;
233 		workspace2.mat[ 1 ][ 2 ] = sinz;
234 		workspace2.mat[ 2 ][ 1 ] = -sinz;
235 		workspace2.mat[ 2 ][ 2 ] = cosz;
236 		this.multiply( workspace1, workspace2 ); // now this holds curr... ;)
237 
238 		return this;
239 	}
240 
241 	/*** 
242 	 * 
243 	 * translate
244 	 * 
245 	 * @param displacement
246 	 * @param workspace1
247 	 * @param workspace2
248 	 * 
249 	 * @return a Mat4
250 	 * 
251 	 */
252 	public Mat4 translate( Pt3 displacement, Mat4 workspace1, Mat4 workspace2 ) {
253 		workspace1.loadIdentity();
254 		workspace1.mat[ 0 ][ 3 ] = displacement.getX();
255 		workspace1.mat[ 1 ][ 3 ] = displacement.getY();
256 		workspace1.mat[ 2 ][ 3 ] = displacement.getZ();
257 		workspace2.set( this );
258 		return this.multiply( workspace2, workspace1 );
259 	}
260 
261 	/*** 
262 	 * 
263 	 * scale
264 	 * 
265 	 * @param ratio
266 	 * @param workspace1
267 	 * @param workspace2
268 	 * 
269 	 */
270 	public Mat4 scale( Pt3 ratio, Mat4 workspace1, Mat4 workspace2 ) {
271 		workspace1.loadIdentity();
272 		workspace1.mat[ 0 ][ 0 ] = ratio.getX();
273 		workspace1.mat[ 1 ][ 1 ] = ratio.getY();
274 		workspace1.mat[ 2 ][ 2 ] = ratio.getZ();
275 		workspace1.mat[ 3 ][ 3 ] = 1;
276 		workspace2.set( this );
277 		this.multiply( workspace2, workspace1 );
278 		return this;
279 	}
280 
281 	/*** 
282 	 * 
283 	 * multiply
284 	 * 
285 	 * @param point
286 	 * @param destination
287 	 * 
288 	 * @return a Pt3
289 	 * 
290 	 */
291 	public Pt3 multiply( Pt3 point, Pt3 destination ) {
292 		destination.setX(
293 			point.getX()* mat[ 0 ][ 0 ] +
294 			point.getY()* mat[ 0 ][ 1 ] +
295 			point.getZ()* mat[ 0 ][ 2 ] +
296 			mat[ 0 ][ 3 ] 
297 		);
298 
299 		destination.setY(
300 			point.getX()* mat[ 1 ][ 0 ] +
301 			point.getY()* mat[ 1 ][ 1 ] +
302 			point.getZ()* mat[ 1 ][ 2 ] +
303 			mat[ 1 ][ 3 ] 
304 		);
305 
306 		destination.setZ(
307 			point.getX()* mat[ 2 ][ 0 ] +
308 			point.getY()* mat[ 2 ][ 1 ] +
309 			point.getZ()* mat[ 2 ][ 2 ] +
310 			mat[ 2 ][ 3 ] 
311 		);
312 
313 		return destination;
314 	}
315 
316 	/***
317 	 *
318 	 * This should not be available outside the object...
319 	 *
320 	 */
321 	float mat[][];
322 
323 };
324 
325 /***
326  *
327  * $Log: Mat4.java,v $
328  * Revision 1.10  2005/03/19 17:50:02  brian
329  * repackaging
330  *
331  * Revision 1.9  2005/03/12 04:58:47  brian
332  * call the methods set instead of copy
333  *
334  * Revision 1.8  2005/03/11 23:35:31  brian
335  * major refactoring to add in com.insanityengine.ghia.libograf.State bitz
336  *
337  * Revision 1.7  2005/03/10 13:31:19  brian
338  * introduce com.insanityengine.ghia/libograf/State to pave the way for quaternions based LiboGrafInterface implementation
339  *
340  * Revision 1.6  2004/09/01 01:10:42  brian
341  * fix class level javadoc placement
342  *
343  * Revision 1.5  2004/09/01 00:11:06  brian
344  * author, log and header stuff
345  *
346  *
347  */