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