View Javadoc

1   package com.insanityengine.ghia.m3;
2   
3   /***
4    *
5    * <P>
6    * Why isn't this called P5? I dunno... it is a 5-dimensional point 
7    * three coordinates in the "physical" space and two in texture space.
8    * </P>
9    *
10   * @author BrianHammond
11   *
12   * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/m3/Pt3.java,v 1.13 2005/03/25 13:48:17 brian Exp $
13   *
14   */
15  
16  public class Pt3 {
17  	
18  
19  	public float x, y, z;
20  	public float s, t;
21  
22  	/***
23  	 *
24  	 * Constructor
25  	 *
26  	 */
27  	public Pt3() { 
28  	}
29  	
30  	/***
31  	 *
32  	 * Constructor
33  	 *
34  	 */
35  	public Pt3( float d ) {
36  		set( d );
37  	}
38  	
39  	/***
40  	 *
41  	 * Copy Constructor
42  	 *
43  	 */
44  	public Pt3( Pt3 that ) {
45  		set( that );
46  	}
47  
48  	/***
49  	 *
50  	 * Constructor
51  	 *
52  	 * @param x coordinate
53  	 * @param y coordinate
54  	 * @param z coordinate
55  	 *
56  	 */
57  	public Pt3( float x, float y, float z ) {
58  		set( x, y, z );
59  	}
60  
61  	/***
62  	 *
63  	 * Set (x,y,z) to d
64  	 *
65  	 * @param d value to set to
66  	 *
67  	 * @return this
68  	 *
69  	 */
70  	public final Pt3 set( float d ) {
71  		return set( d, d, d );
72  	}
73  
74  	/***
75  	 *
76  	 * Set the point to (x,y,z)
77  	 *
78  	 * @param x coordinate
79  	 * @param y coordinate
80  	 * @param z coordinate
81  	 *
82  	 * @return this
83  	 *
84  	 */
85  	public final Pt3 set( float x, float y, float z ) {
86  		this.x = x; 
87  		this.y = y; 
88  		this.z = z;
89  		return this;
90  	}
91  
92  	/***	
93  	 *
94  	 * Copy that point
95  	 *
96  	 * @param that
97  	 *
98  	 * @return this
99  	 *
100 	 */
101 	public final Pt3 set( Pt3 that ) {
102 		set( that.x, that.y, that.z );
103 		s = that.s;
104 		t = that.t;
105 		return this;
106 	}
107 
108 	/***	
109 	 *
110 	 * Swap with that point
111 	 *
112 	 * @param that point
113 	 *
114 	 * @return this
115 	 *
116 	 */
117 	public final Pt3 swap( Pt3 that ) {
118 		float a = x;
119 		float b = y;
120 		float c = z;
121 		this.x = that.x;
122 		this.y = that.y;
123 		this.z = that.z;
124 		that.x = a; 
125 		that.y = b;
126 		that.z = c;
127 		a = this.s;
128 		b = this.t;
129 		this.s = that.s;
130 		this.t = that.t;
131 		that.s = a;
132 		that.t = b;
133 		return this;
134 	}
135 
136 	/***	
137 	 *
138 	 * Calculate the dot product with that point
139 	 *
140 	 * @param that point to use
141 	 *
142 	 * @return dot produce
143 	 *
144 	 */
145 	public final float dot( Pt3 that ) {
146 		return (
147 			this.x * that.x + 
148 			this.y * that.y + 
149 			this.z * that.z 
150 		);
151 	}
152 
153 	/***	
154 	 *
155 	 * Calculate the distance squared to that point
156 	 *
157 	 * @param that point to use
158 	 *
159 	 * @return squared distance to that point
160 	 *
161 	 */
162 	public final float distance2( Pt3 that ) {
163 		return (
164 			+ ( that.x - this.x ) * ( that.x - this.x )
165 			+ ( that.y - this.y ) * ( that.y - this.y )
166 			+ ( that.z - this.z ) * ( that.z - this.z )
167 		);
168 	}
169 
170 	/*** 
171 	 * 
172 	 * add
173 	 * 
174 	 * @param d
175 	 * 
176 	 * @return a Pt3
177 	 * 
178 	 */
179 	public final Pt3 add( float d ) {
180 		return add( d, d, d );
181 	}
182 
183 	/*** 
184 	 * 
185 	 * add
186 	 * 
187 	 * @param x
188 	 * @param y
189 	 * @param z
190 	 * 
191 	 * @return a Pt3
192 	 * 
193 	 */
194 	public final Pt3 add( float x, float y, float z ) {
195 		this.x += x;
196 		this.y += y;
197 		this.z += z;
198 		return this;
199 	}
200 
201 	/***	
202 	 *
203 	 * Add that point from this point
204 	 *
205 	 * @param that point to use
206 	 *
207 	 * @return this
208 	 *
209 	 */
210 	public final Pt3 add( Pt3 that ) {
211 		return add( that.x, that.y, that.z );
212 	}
213 	/***	
214 	 *
215 	 * Subtract that point from this point
216 	 *
217 	 * @param that point to use
218 	 *
219 	 * @return this
220 	 *
221 	 */
222 	public final Pt3 subtract( Pt3 that ) {
223 		this.x -= that.x;
224 		this.y -= that.y;
225 		this.z -= that.z;
226 		return this;
227 	}
228 	
229 	/***	
230 	 *
231 	 * Multiply this point by a scalar
232 	 *
233 	 * @param scalar to multiply by
234 	 *
235 	 * @return this
236 	 *
237 	 */
238 	public final Pt3 multiply( float scalar ) {
239 		x *= scalar;
240 		y *= scalar;
241 		z *= scalar;
242 		return this;
243 	}
244 	
245 	/***	
246 	 *
247 	 * Divide this point by a scalar
248 	 *
249 	 * @param scalar to divide by
250 	 *
251 	 * @return this
252 	 *
253 	 */
254 	public final Pt3 divide( float scalar ) {
255 		if ( 0 == scalar ) {
256 			x /= scalar;
257 			y /= scalar;
258 			z /= scalar;
259 		}
260 		return this;
261 	}
262 
263 	/***	
264 	 *
265 	 * Normalize the length of the the point (as a vector) to
266 	 * a unit vector
267 	 *
268 	 * @return this
269 	 *
270 	 */
271 	public final Pt3 normalize() {
272 		float d = ( float ) Math.sqrt( x * x + y * y + z * z );
273 		if ( 0 != d ) {
274 			x /= d;
275 			y /= d;
276 			z /= d;
277 		}
278 		return this;
279 	}
280 
281 	/***	
282 	 *
283 	 * Given three points, calculate the normal vector into
284 	 * this point
285 	 *
286 	 * @param pt1 to use
287 	 * @param pt2 to use
288 	 * @param pt3 to use
289 	 *
290 	 * @return this
291 	 *
292 	 */
293 	public final Pt3 normal( Pt3 pt1, Pt3 pt2, Pt3 pt3 ) {
294 		float ax = pt1.x - pt2.x;
295 		float ay = pt1.y - pt2.y;
296 		float az = pt1.z - pt2.z;
297 
298 		float bx = pt3.x - pt2.x;
299 		float by = pt3.y - pt2.y;
300 		float bz = pt3.z - pt2.z;
301 
302 		x = ( ay * bz - az * by );
303 		y = ( az * bx - ax * bz );
304 		z = ( ax * by - ay * bx );
305 		
306 		ax = ( float ) Math.sqrt( x * x + y * y + z * z );
307 		if ( 0 != ax ) {
308 			x /= ax;
309 			y /= ax;
310 			z /= ax;
311 		}
312 		return this;
313 	}
314 
315 	/***	
316 	 *
317 	 * Subtract pt2 from pt1 and store the result in this point
318 	 *
319 	 * @param pt1 to use
320 	 * @param pt2 to use
321 	 *
322 	 * @return this
323 	 *
324 	 */
325 	public final Pt3 minus( Pt3 pt1, Pt3 pt2 ) {
326 		x = pt1.x - pt2.x;
327 		y = pt1.y - pt2.y;
328 	 	z = pt1.z - pt2.z;
329 		return this;
330 	}
331 	
332 	/***	
333 	 *
334 	 * Add pt1 to pt2 and store the result in this point
335 	 *
336 	 * @param pt1 to use
337 	 * @param pt2 to use
338 	 *
339 	 * @return this
340 	 *
341 	 */
342 	public final Pt3 plus( Pt3 pt1, Pt3 pt2 ) {
343 		x = pt1.x + pt2.x;
344 		y = pt1.y + pt2.y;
345 	 	z = pt1.z + pt2.z;
346 		return this;
347 	}
348 	
349 	/***	
350 	 *
351 	 * Calculate the cross product and store the result in this point
352 	 *
353 	 * @param pt1 to use
354 	 * @param pt2 to use
355 	 *
356 	 * @return this
357 	 *
358 	 */
359 	public final Pt3 cross( Pt3 pt1, Pt3 pt2 ) {
360 		x = ( pt1.y * pt2.z - pt1.z * pt2.y );
361 		y = ( pt1.z * pt2.x - pt1.x * pt2.z );
362 	 	z = ( pt1.x * pt2.y - pt1.y * pt2.x );
363 		return this;
364 	}
365 
366 	/***	
367 	 *
368 	 * Multiply the point by the matrix and store the result in this point
369 	 *
370 	 * @param matrix to multiply
371 	 * @param point to multiply
372 	 *
373 	 * @return this
374 	 *
375 	 */
376 	public final Pt3 multiply( Mat4 matrix, Pt3 point ) {
377 		x = (
378 			point.x * matrix.mat[ 0 ][ 0 ] +
379 			point.y * matrix.mat[ 0 ][ 1 ] +
380 			point.z * matrix.mat[ 0 ][ 2 ] +
381 			      1 * matrix.mat[ 0 ][ 3 ] 
382 		);
383 
384 		y = (
385 			point.x * matrix.mat[ 1 ][ 0 ] +
386 			point.y * matrix.mat[ 1 ][ 1 ] +
387 			point.z * matrix.mat[ 1 ][ 2 ] +
388 			      1 * matrix.mat[ 1 ][ 3 ] 
389 		);
390 
391 		z = (
392 			point.x * matrix.mat[ 2 ][ 0 ] +
393 			point.y * matrix.mat[ 2 ][ 1 ] +
394 			point.z * matrix.mat[ 2 ][ 2 ] +
395 			      1 * matrix.mat[ 2 ][ 3 ] 
396 		);
397 		return this;
398 	}
399 
400 	/***	
401 	 *
402 	 * Convert to string representation
403 	 *
404 	 * @return string representation
405 	 *
406 	 */
407 	public String toString() {
408 		return (
409 			"pt3" + "(" + x + " , " + y + " , " + z + " )"
410 		);
411 	}
412 
413 	/***
414 	 *
415 	 * Get the value of x
416 	 *
417 	 * @return the value of x
418 	 *
419 	 */
420 	public final float getX() {
421 		return x;
422 	}
423 
424 	/***
425 	 *
426 	 * Set the value of x
427 	 *
428 	 * @param newValue of x
429 	 *
430 	 */
431 	public final void setX( float newValue ) {
432 		x = newValue;
433 	}
434 
435 	/***
436 	 *
437 	 * Get the value of y
438 	 *
439 	 * @return the value of y
440 	 *
441 	 */
442 	public final float getY() {
443 		return y;
444 	}
445 
446 	/***
447 	 *
448 	 * Set the value of y
449 	 *
450 	 * @param newValue of y
451 	 *
452 	 */
453 	public final void setY( float newValue ) {
454 		y = newValue;
455 	}
456 
457 	/***
458 	 *
459 	 * Get the value of z
460 	 *
461 	 * @return the value of z
462 	 *
463 	 */
464 	public final float getZ() {
465 		return z;
466 	}
467 
468 	/***
469 	 *
470 	 * Set the value of z
471 	 *
472 	 * @param newValue of z
473 	 *
474 	 */
475 	public final void setZ( float newValue ) {
476 		z = newValue;
477 	}
478 
479 	/***
480 	 *
481 	 * Get the value of s
482 	 *
483 	 * @return the value of s
484 	 *
485 	 */
486 	public final float getS() {
487 		return s;
488 	}
489 
490 	/***
491 	 *
492 	 * Set the value of s
493 	 *
494 	 * @param newValue of s
495 	 *
496 	 */
497 	public final void setS( float newValue ) {
498 		s = newValue;
499 	}
500 
501 	/***
502 	 *
503 	 * Get the value of t
504 	 *
505 	 * @return the value of t
506 	 *
507 	 */
508 	public final float getT() {
509 		return t;
510 	}
511 
512 	/***
513 	 *
514 	 * Set the value of t
515 	 *
516 	 * @param newValue of t
517 	 *
518 	 */
519 	public final void setT( float newValue ) {
520 		t = newValue;
521 	}
522 
523 }
524 
525 /***
526  *
527  * $Log: Pt3.java,v $
528  * Revision 1.13  2005/03/25 13:48:17  brian
529  * make toString less verbose...
530  *
531  * Revision 1.12  2005/03/25 13:08:17  brian
532  * make divide less silly
533  *
534  * Revision 1.11  2005/03/19 17:50:02  brian
535  * repackaging
536  *
537  * Revision 1.10  2005/03/15 05:45:33  brian
538  * reduce code dup
539  *
540  * Revision 1.9  2005/03/12 04:58:47  brian
541  * call the methods set instead of copy
542  *
543  * Revision 1.8  2005/03/11 23:35:31  brian
544  * major refactoring to add in com.insanityengine.ghia.libograf.State bitz
545  *
546  * Revision 1.7  2004/09/01 18:30:00  brian
547  * added the add method I should added before but it is added now...
548  *
549  * Revision 1.6  2004/09/01 01:10:42  brian
550  * fix class level javadoc placement
551  *
552  * Revision 1.5  2004/09/01 00:11:06  brian
553  * author, log and header stuff
554  *
555  *
556  */