2 Copyright (C) 1997,1998,1999
3 Kenji Hiranabe, Eiwa System Management, Inc.
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.
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.
19 import java.io.Serializable;
24 * A single precision floating point 3 by 3 matrix.
26 * @author Kenji hiranabe
28 * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012 for unique
29 * constructor and method names for the optimization of compiled
30 * JavaScript using Java2Script
34 public class M3 extends M34 implements Serializable {
37 * Constructs and initializes a Matrix3f to all zeros.
43 * Constructs and initializes a Matrix3f from the specified 9 element array.
44 * this.m00 =v[0], this.m01=v[1], etc.
47 * the array of length 9 containing in order
50 public static M3 newA9(float[] v) {
57 * Constructs a new matrix with the same values as the Matrix3f parameter.
63 public static M3 newM3(M3 m1) {
84 * Sets this Matrix3f to a scalar * Identity.
87 public void setScale(float scale) {
89 m00 = m11 = m22 = scale;
93 * Sets the value of this matrix to the double value of the Matrix3f argument.
98 public void setM3(M34 m1) {
102 * Sets the values in this Matrix3f equal to the row-major array parameter
103 * (ie, the first four elements of the array will be copied into the first row
104 * of this matrix, etc.).
108 public void setA(float m[]) {
121 * Sets the specified element of this matrix3d to the value provided.
124 * the row number to be modified (zero indexed)
126 * the column number to be modified (zero indexed)
130 public void setElement(int row, int col, float v) {
135 * Retrieves the value at the specified row and column of this matrix.
138 * the row number to be retrieved (zero indexed)
140 * the column number to be retrieved (zero indexed)
141 * @return the value at the indexed element
143 public float getElement(int row, int col) {
144 return get33(row, col);
148 * Sets the specified row of this matrix3d to the three values provided.
151 * the row number to be modified (zero indexed)
153 * the first column element
155 * the second column element
157 * the third column element
159 public void setRow(int row, float x, float y, float z) {
182 * Sets the specified row of this matrix3d to the Vector provided.
185 * the row number to be modified (zero indexed)
187 * the replacement row
189 public void setRowV(int row, T3 v) {
212 * Sets the specified row of this matrix3d to the four values provided.
215 * the row number to be modified (zero indexed)
217 * the replacement row
219 public void setRowA(int row, float v[]) {
224 * Copies the matrix values in the specified row into the array parameter.
229 * The array into which the matrix row values will be copied
232 public void getRow(int row, float v[]) {
237 * Sets the specified column of this matrix3d to the three values provided.
240 * the column number to be modified (zero indexed)
242 * the first row element
244 * the second row element
246 * the third row element
248 public void setColumn3(int column, float x, float y, float z) {
271 * Sets the specified column of this matrix3d to the vector provided.
274 * the column number to be modified (zero indexed)
276 * the replacement column
278 public void setColumnV(int column, T3 v) {
301 * Copies the matrix values in the specified column into the vector parameter.
306 * The vector into which the matrix row values will be copied
308 public void getColumnV(int column, T3 v) {
331 * Sets the specified column of this matrix3d to the four values provided.
334 * the column number to be modified (zero indexed)
336 * the replacement column
338 public void setColumnA(int column, float v[]) {
339 setColumn33(column, v);
343 * Copies the matrix values in the specified column into the array parameter.
348 * The array into which the matrix row values will be copied
350 public void getColumn(int column, float v[]) {
351 getColumn33(column, v);
355 * Sets the value of this matrix to sum of itself and matrix m1.
360 public void add(M3 m1) {
365 * Sets the value of this matrix to the matrix difference of itself and matrix
366 * m1 (this = this - m1).
371 public void sub(M3 m1) {
376 * Sets the value of this matrix to its transpose.
378 public void transpose() {
383 * Sets the value of this matrix to the transpose of the argument matrix
386 * the matrix to be transposed
388 public void transposeM(M3 m1) {
395 * Sets the value of this matrix to the matrix inverse of the passed matrix
399 * the matrix to be inverted
401 public void invertM(M3 m1) {
407 * Sets the value of this matrix to its inverse.
409 public void invert() {
410 double s = determinant3();
415 set9(m11 * m22 - m12 * m21, m02 * m21 - m01 * m22, m01 * m12 - m02 * m11,
416 m12 * m20 - m10 * m22, m00 * m22 - m02 * m20, m02 * m10 - m00 * m12,
417 m10 * m21 - m11 * m20, m01 * m20 - m00 * m21, m00 * m11 - m01 * m10);
422 * Sets the value of this matrix to a rotation matrix about the x axis by the
426 * the angle to rotate about the X axis in radians
429 public M3 setAsXRotation(float angle) {
435 * Sets the value of this matrix to a rotation matrix about the y axis by the
439 * the angle to rotate about the Y axis in radians
442 public M3 setAsYRotation(float angle) {
448 * Sets the value of this matrix to a rotation matrix about the z axis by the
452 * the angle to rotate about the Z axis in radians
455 public M3 setAsZRotation(float angle) {
461 * Multiplies each element of this matrix by a scalar.
464 * The scalar multiplier.
466 public void scale(float scalar) {
471 * Sets the value of this matrix to the result of multiplying itself with
477 public void mul(M3 m1) {
482 * Sets the value of this matrix to the result of multiplying the two argument
490 public void mul2(M3 m1, M3 m2) {
492 set9(m1.m00 * m2.m00 + m1.m01 * m2.m10 + m1.m02 * m2.m20, m1.m00 * m2.m01
493 + m1.m01 * m2.m11 + m1.m02 * m2.m21, m1.m00 * m2.m02 + m1.m01 * m2.m12
496 m1.m10 * m2.m00 + m1.m11 * m2.m10 + m1.m12 * m2.m20, m1.m10 * m2.m01
497 + m1.m11 * m2.m11 + m1.m12 * m2.m21, m1.m10 * m2.m02 + m1.m11 * m2.m12
500 m1.m20 * m2.m00 + m1.m21 * m2.m10 + m1.m22 * m2.m20, m1.m20 * m2.m01
501 + m1.m21 * m2.m11 + m1.m22 * m2.m21, m1.m20 * m2.m02 + m1.m21 * m2.m12
506 * Returns true if the Object o is of type Matrix3f and all of the data
507 * members of t1 are equal to the corresponding data members in this Matrix3f.
510 * the object with which the comparison is made.
513 public boolean equals(Object o) {
514 if (!(o instanceof M3))
517 return m00 == m.m00 && m01 == m.m01 && m02 == m.m02 && m10 == m.m10
518 && m11 == m.m11 && m12 == m.m12 && m20 == m.m20 && m21 == m.m21
523 * Returns a hash number based on the data values in this object. Two
524 * different Matrix3f objects with identical data values (ie, returns true for
525 * equals(Matrix3f) ) will return the same hash number. Two objects with
526 * different data members may return the same hash value, although this is not
529 * @return the integer hash value
532 public int hashCode() {
533 return T3.floatToIntBits(m00) ^ T3.floatToIntBits(m01)
534 ^ T3.floatToIntBits(m02) ^ T3.floatToIntBits(m10)
535 ^ T3.floatToIntBits(m11) ^ T3.floatToIntBits(m12)
536 ^ T3.floatToIntBits(m20) ^ T3.floatToIntBits(m21)
537 ^ T3.floatToIntBits(m22);
541 * Sets this matrix to all zeros.
543 public void setZero() {
560 private void set9(float m00, float m01, float m02, float m10, float m11,
561 float m12, float m20, float m21, float m22) {
574 * Returns a string that contains the values of this Matrix3f.
576 * @return the String representation
579 public String toString() {
580 return "[\n [" + m00 + "\t" + m01 + "\t" + m02 + "]" + "\n [" + m10
581 + "\t" + m11 + "\t" + m12 + "]" + "\n [" + m20 + "\t" + m21 + "\t"
586 * Sets the value of this matrix to the matrix conversion of the single
587 * precision axis and angle argument.
590 * the axis and angle to be converted
593 public M3 setAA(A4 a) {
599 * 3D ball rotation from dx dy in-plane mouse motion
600 * adapted from Andrew Hanson
601 * Computer Graphics beyond the Third Dimension:
602 * Geometry, Orientation Control, and Rendering for
603 * Graphics in Dimensions Greater than Three
604 * Course Notes for SIGGRAPH ’98
605 * http://www.cse.ohio-state.edu/~hwshen/888_su02/hanson_note.pdf
607 * @param responseFactor Jmol uses 0.02 here
610 * @return true if successful; false if not;
612 public boolean setAsBallRotation(float responseFactor, float dx, float dy) {
613 float r = (float) Math.sqrt(dx * dx + dy * dy);
614 float th = r * responseFactor;
619 float c = (float) Math.cos(th);
620 float s = (float) Math.sin(th);
624 m00 = 1 + c1 * nx * nx;
625 m01 = m10 = c1 * nx * ny;
626 m20 = -(m02 = s * nx);
627 m11 = 1 + c1 * ny * ny;
628 m21 = -(m12 = s * ny);
633 public boolean isRotation() {
634 return (Math.abs(determinant3() - 1) < 0.001f);