2 Copyright (C) 1997,1998,1999
\r
3 Kenji Hiranabe, Eiwa System Management, Inc.
\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
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
17 package javajs.util;
\r
19 import java.io.Serializable;
\r
24 * A single precision floating point 3 by 3 matrix.
\r
26 * @author Kenji hiranabe
\r
28 * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique
\r
29 * constructor and method names for the optimization of compiled
\r
30 * JavaScript using Java2Script
\r
34 public class M3 extends M34 implements Serializable {
\r
37 * Constructs and initializes a Matrix3f to all zeros.
\r
44 * Constructs and initializes a Matrix3f from the specified 9 element array.
\r
45 * this.m00 =v[0], this.m01=v[1], etc.
\r
48 * the array of length 9 containing in order
\r
51 public static M3 newA9(float[] v) {
\r
58 * Constructs a new matrix with the same values as the Matrix3f parameter.
\r
61 * The source matrix.
\r
64 public static M3 newM3(M3 m1) {
\r
85 * Sets this Matrix3f to a scalar * Identity.
\r
88 public void setScale(float scale) {
\r
90 m00 = m11 = m22 = scale;
\r
94 * Sets the value of this matrix to the double value of the Matrix3f argument.
\r
99 public void setM3(M34 m1) {
\r
103 * Sets the values in this Matrix3f equal to the row-major array parameter
\r
104 * (ie, the first four elements of the array will be copied into the first row
\r
105 * of this matrix, etc.).
\r
109 public void setA(float m[]) {
\r
122 * Sets the specified element of this matrix3d to the value provided.
\r
125 * the row number to be modified (zero indexed)
\r
127 * the column number to be modified (zero indexed)
\r
131 public void setElement(int row, int col, float v) {
\r
132 set33(row, col, v);
\r
136 * Retrieves the value at the specified row and column of this matrix.
\r
139 * the row number to be retrieved (zero indexed)
\r
141 * the column number to be retrieved (zero indexed)
\r
142 * @return the value at the indexed element
\r
144 public float getElement(int row, int col) {
\r
145 return get33(row, col);
\r
149 * Sets the specified row of this matrix3d to the three values provided.
\r
152 * the row number to be modified (zero indexed)
\r
154 * the first column element
\r
156 * the second column element
\r
158 * the third column element
\r
160 public void setRow(int row, float x, float y, float z) {
\r
183 * Sets the specified row of this matrix3d to the Vector provided.
\r
186 * the row number to be modified (zero indexed)
\r
188 * the replacement row
\r
190 public void setRowV(int row, T3 v) {
\r
213 * Sets the specified row of this matrix3d to the four values provided.
\r
216 * the row number to be modified (zero indexed)
\r
218 * the replacement row
\r
220 public void setRowA(int row, float v[]) {
\r
225 * Copies the matrix values in the specified row into the array parameter.
\r
230 * The array into which the matrix row values will be copied
\r
233 public void getRow(int row, float v[]) {
\r
238 * Sets the specified column of this matrix3d to the three values provided.
\r
241 * the column number to be modified (zero indexed)
\r
243 * the first row element
\r
245 * the second row element
\r
247 * the third row element
\r
249 public void setColumn3(int column, float x, float y, float z) {
\r
272 * Sets the specified column of this matrix3d to the vector provided.
\r
275 * the column number to be modified (zero indexed)
\r
277 * the replacement column
\r
279 public void setColumnV(int column, T3 v) {
\r
302 * Copies the matrix values in the specified column into the vector parameter.
\r
305 * the matrix column
\r
307 * The vector into which the matrix row values will be copied
\r
309 public void getColumnV(int column, T3 v) {
\r
332 * Sets the specified column of this matrix3d to the four values provided.
\r
335 * the column number to be modified (zero indexed)
\r
337 * the replacement column
\r
339 public void setColumnA(int column, float v[]) {
\r
340 setColumn33(column, v);
\r
344 * Copies the matrix values in the specified column into the array parameter.
\r
347 * the matrix column
\r
349 * The array into which the matrix row values will be copied
\r
351 public void getColumn(int column, float v[]) {
\r
352 getColumn33(column, v);
\r
356 * Sets the value of this matrix to sum of itself and matrix m1.
\r
361 public void add(M3 m1) {
\r
366 * Sets the value of this matrix to the matrix difference of itself and matrix
\r
367 * m1 (this = this - m1).
\r
372 public void sub(M3 m1) {
\r
377 * Sets the value of this matrix to its transpose.
\r
379 public void transpose() {
\r
384 * Sets the value of this matrix to the transpose of the argument matrix
\r
387 * the matrix to be transposed
\r
389 public void transposeM(M3 m1) {
\r
396 * Sets the value of this matrix to the matrix inverse of the passed matrix
\r
400 * the matrix to be inverted
\r
402 public void invertM(M3 m1) {
\r
408 * Sets the value of this matrix to its inverse.
\r
410 public void invert() {
\r
411 double s = determinant3();
\r
416 set9(m11 * m22 - m12 * m21, m02 * m21 - m01 * m22, m01 * m12 - m02 * m11,
\r
417 m12 * m20 - m10 * m22, m00 * m22 - m02 * m20, m02 * m10 - m00 * m12,
\r
418 m10 * m21 - m11 * m20, m01 * m20 - m00 * m21, m00 * m11 - m01 * m10);
\r
423 * Sets the value of this matrix to a rotation matrix about the x axis by the
\r
427 * the angle to rotate about the X axis in radians
\r
430 public M3 setAsXRotation(float angle) {
\r
436 * Sets the value of this matrix to a rotation matrix about the y axis by the
\r
440 * the angle to rotate about the Y axis in radians
\r
443 public M3 setAsYRotation(float angle) {
\r
449 * Sets the value of this matrix to a rotation matrix about the z axis by the
\r
453 * the angle to rotate about the Z axis in radians
\r
456 public M3 setAsZRotation(float angle) {
\r
462 * Multiplies each element of this matrix by a scalar.
\r
465 * The scalar multiplier.
\r
467 public void scale(float scalar) {
\r
472 * Sets the value of this matrix to the result of multiplying itself with
\r
478 public void mul(M3 m1) {
\r
483 * Sets the value of this matrix to the result of multiplying the two argument
\r
484 * matrices together.
\r
489 * the second matrix
\r
491 public void mul2(M3 m1, M3 m2) {
\r
493 set9(m1.m00 * m2.m00 + m1.m01 * m2.m10 + m1.m02 * m2.m20, m1.m00 * m2.m01
\r
494 + m1.m01 * m2.m11 + m1.m02 * m2.m21, m1.m00 * m2.m02 + m1.m01 * m2.m12
\r
497 m1.m10 * m2.m00 + m1.m11 * m2.m10 + m1.m12 * m2.m20, m1.m10 * m2.m01
\r
498 + m1.m11 * m2.m11 + m1.m12 * m2.m21, m1.m10 * m2.m02 + m1.m11 * m2.m12
\r
501 m1.m20 * m2.m00 + m1.m21 * m2.m10 + m1.m22 * m2.m20, m1.m20 * m2.m01
\r
502 + m1.m21 * m2.m11 + m1.m22 * m2.m21, m1.m20 * m2.m02 + m1.m21 * m2.m12
\r
503 + m1.m22 * m2.m22);
\r
507 * Returns true if the Object o is of type Matrix3f and all of the data
\r
508 * members of t1 are equal to the corresponding data members in this Matrix3f.
\r
511 * the object with which the comparison is made.
\r
514 public boolean equals(Object o) {
\r
515 if (!(o instanceof M3))
\r
518 return m00 == m.m00 && m01 == m.m01 && m02 == m.m02 && m10 == m.m10
\r
519 && m11 == m.m11 && m12 == m.m12 && m20 == m.m20 && m21 == m.m21
\r
524 * Returns a hash number based on the data values in this object. Two
\r
525 * different Matrix3f objects with identical data values (ie, returns true for
\r
526 * equals(Matrix3f) ) will return the same hash number. Two objects with
\r
527 * different data members may return the same hash value, although this is not
\r
530 * @return the integer hash value
\r
533 public int hashCode() {
\r
534 return T3.floatToIntBits0(m00) ^ T3.floatToIntBits0(m01)
\r
535 ^ T3.floatToIntBits0(m02) ^ T3.floatToIntBits0(m10)
\r
536 ^ T3.floatToIntBits0(m11) ^ T3.floatToIntBits0(m12)
\r
537 ^ T3.floatToIntBits0(m20) ^ T3.floatToIntBits0(m21)
\r
538 ^ T3.floatToIntBits0(m22);
\r
542 * Sets this matrix to all zeros.
\r
544 public void setZero() {
\r
561 private void set9(float m00, float m01, float m02, float m10, float m11,
\r
562 float m12, float m20, float m21, float m22) {
\r
575 * Returns a string that contains the values of this Matrix3f.
\r
577 * @return the String representation
\r
580 public String toString() {
\r
581 return "[\n [" + m00 + "\t" + m01 + "\t" + m02 + "]" + "\n [" + m10
\r
582 + "\t" + m11 + "\t" + m12 + "]" + "\n [" + m20 + "\t" + m21 + "\t"
\r
587 * Sets the value of this matrix to the matrix conversion of the single
\r
588 * precision axis and angle argument.
\r
591 * the axis and angle to be converted
\r
594 public M3 setAA(A4 a) {
\r
600 * 3D ball rotation from dx dy in-plane mouse motion
\r
601 * adapted from Andrew Hanson
\r
602 * Computer Graphics beyond the Third Dimension:
\r
603 * Geometry, Orientation Control, and Rendering for
\r
604 * Graphics in Dimensions Greater than Three
\r
605 * Course Notes for SIGGRAPH ’98
\r
606 * http://www.cse.ohio-state.edu/~hwshen/888_su02/hanson_note.pdf
\r
608 * @param responseFactor Jmol uses 0.02 here
\r
611 * @return true if successful; false if not;
\r
613 public boolean setAsBallRotation(float responseFactor, float dx, float dy) {
\r
614 float r = (float) Math.sqrt(dx * dx + dy * dy);
\r
615 float th = r * responseFactor;
\r
620 float c = (float) Math.cos(th);
\r
621 float s = (float) Math.sin(th);
\r
622 float nx = -dy / r;
\r
625 m00 = 1 + c1 * nx * nx;
\r
626 m01 = m10 = c1 * nx * ny;
\r
627 m20 = -(m02 = s * nx);
\r
628 m11 = 1 + c1 * ny * ny;
\r
629 m21 = -(m12 = s * ny);
\r
634 public boolean isRotation() {
\r
635 return (Math.abs(determinant3() - 1) < 0.001f);
\r