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.
35 @SuppressWarnings("serial")
36 public class M3 extends M34 implements Serializable {
39 * Constructs and initializes a Matrix3f from the specified 9 element array.
40 * this.m00 =v[0], this.m01=v[1], etc.
43 * the array of length 9 containing in order
46 public static M3 newA9(float[] v) {
53 * Constructs a new matrix with the same values as the Matrix3f parameter.
59 public static M3 newM3(M3 m1) {
80 * Sets this Matrix3f to a scalar * Identity.
83 public void setScale(float scale) {
85 m00 = m11 = m22 = scale;
89 * Sets the value of this matrix to the double value of the Matrix3f argument.
94 public void setM3(M34 m1) {
98 * Sets the values in this Matrix3f equal to the row-major array parameter
99 * (ie, the first four elements of the array will be copied into the first row
100 * of this matrix, etc.).
104 public void setA(float m[]) {
117 * Sets the specified element of this matrix3d to the value provided.
120 * the row number to be modified (zero indexed)
122 * the column number to be modified (zero indexed)
126 public void setElement(int row, int col, float v) {
131 * Retrieves the value at the specified row and column of this matrix.
134 * the row number to be retrieved (zero indexed)
136 * the column number to be retrieved (zero indexed)
137 * @return the value at the indexed element
139 public float getElement(int row, int col) {
140 return get33(row, col);
144 * Sets the specified row of this matrix3d to the three values provided.
147 * the row number to be modified (zero indexed)
149 * the first column element
151 * the second column element
153 * the third column element
155 public void setRow(int row, float x, float y, float z) {
178 * Sets the specified row of this matrix3d to the Vector provided.
181 * the row number to be modified (zero indexed)
183 * the replacement row
185 public void setRowV(int row, T3 v) {
208 * Sets the specified row of this matrix3d to the four values provided.
211 * the row number to be modified (zero indexed)
213 * the replacement row
215 public void setRowA(int row, float v[]) {
220 * Copies the matrix values in the specified row into the array parameter.
225 * The array into which the matrix row values will be copied
228 public void getRow(int row, float v[]) {
233 * Sets the specified column of this matrix3d to the three values provided.
236 * the column number to be modified (zero indexed)
238 * the first row element
240 * the second row element
242 * the third row element
244 public void setColumn3(int column, float x, float y, float z) {
267 * Sets the specified column of this matrix3d to the vector provided.
270 * the column number to be modified (zero indexed)
272 * the replacement column
274 public void setColumnV(int column, T3 v) {
297 * Copies the matrix values in the specified column into the vector parameter.
302 * The vector into which the matrix row values will be copied
304 public void getColumnV(int column, T3 v) {
327 * Sets the specified column of this matrix3d to the four values provided.
330 * the column number to be modified (zero indexed)
332 * the replacement column
334 public void setColumnA(int column, float v[]) {
335 setColumn33(column, v);
339 * Copies the matrix values in the specified column into the array parameter.
344 * The array into which the matrix row values will be copied
346 public void getColumn(int column, float v[]) {
347 getColumn33(column, v);
351 * Sets the value of this matrix to sum of itself and matrix m1.
356 public void add(M3 m1) {
361 * Sets the value of this matrix to the matrix difference of itself and matrix
362 * m1 (this = this - m1).
367 public void sub(M3 m1) {
372 * Sets the value of this matrix to its transpose.
374 public void transpose() {
379 * Sets the value of this matrix to the transpose of the argument matrix
382 * the matrix to be transposed
384 public void transposeM(M3 m1) {
391 * Sets the value of this matrix to the matrix inverse of the passed matrix
395 * the matrix to be inverted
397 public void invertM(M3 m1) {
403 * Sets the value of this matrix to its inverse.
405 public void invert() {
406 double s = determinant3();
411 set9(m11 * m22 - m12 * m21, m02 * m21 - m01 * m22, m01 * m12 - m02 * m11,
412 m12 * m20 - m10 * m22, m00 * m22 - m02 * m20, m02 * m10 - m00 * m12,
413 m10 * m21 - m11 * m20, m01 * m20 - m00 * m21, m00 * m11 - m01 * m10);
418 * Sets the value of this matrix to a rotation matrix about the x axis by the
422 * the angle to rotate about the X axis in radians
425 public M3 setAsXRotation(float angle) {
431 * Sets the value of this matrix to a rotation matrix about the y axis by the
435 * the angle to rotate about the Y axis in radians
438 public M3 setAsYRotation(float angle) {
444 * Sets the value of this matrix to a rotation matrix about the z axis by the
448 * the angle to rotate about the Z axis in radians
451 public M3 setAsZRotation(float angle) {
457 * Multiplies each element of this matrix by a scalar.
460 * The scalar multiplier.
462 public void scale(float scalar) {
467 * Sets the value of this matrix to the result of multiplying itself with
473 public void mul(M3 m1) {
478 * Sets the value of this matrix to the result of multiplying the two argument
486 public void mul2(M3 m1, M3 m2) {
488 set9(m1.m00 * m2.m00 + m1.m01 * m2.m10 + m1.m02 * m2.m20, m1.m00 * m2.m01
489 + m1.m01 * m2.m11 + m1.m02 * m2.m21, m1.m00 * m2.m02 + m1.m01 * m2.m12
492 m1.m10 * m2.m00 + m1.m11 * m2.m10 + m1.m12 * m2.m20, m1.m10 * m2.m01
493 + m1.m11 * m2.m11 + m1.m12 * m2.m21, m1.m10 * m2.m02 + m1.m11 * m2.m12
496 m1.m20 * m2.m00 + m1.m21 * m2.m10 + m1.m22 * m2.m20, m1.m20 * m2.m01
497 + m1.m21 * m2.m11 + m1.m22 * m2.m21, m1.m20 * m2.m02 + m1.m21 * m2.m12
502 * Returns true if the Object o is of type Matrix3f and all of the data
503 * members of t1 are equal to the corresponding data members in this Matrix3f.
506 * the object with which the comparison is made.
509 public boolean equals(Object o) {
510 if (!(o instanceof M3))
513 return m00 == m.m00 && m01 == m.m01 && m02 == m.m02 && m10 == m.m10
514 && m11 == m.m11 && m12 == m.m12 && m20 == m.m20 && m21 == m.m21
519 * Returns a hash number based on the data values in this object. Two
520 * different Matrix3f objects with identical data values (ie, returns true for
521 * equals(Matrix3f) ) will return the same hash number. Two objects with
522 * different data members may return the same hash value, although this is not
525 * @return the integer hash value
528 public int hashCode() {
529 return T3.floatToIntBits(m00) ^ T3.floatToIntBits(m01)
530 ^ T3.floatToIntBits(m02) ^ T3.floatToIntBits(m10)
531 ^ T3.floatToIntBits(m11) ^ T3.floatToIntBits(m12)
532 ^ T3.floatToIntBits(m20) ^ T3.floatToIntBits(m21)
533 ^ T3.floatToIntBits(m22);
537 * Sets this matrix to all zeros.
539 public void setZero() {
556 private void set9(float m00, float m01, float m02, float m10, float m11,
557 float m12, float m20, float m21, float m22) {
570 * Returns a string that contains the values of this Matrix3f.
572 * @return the String representation
575 public String toString() {
576 return "[\n [" + m00 + "\t" + m01 + "\t" + m02 + "]" + "\n [" + m10
577 + "\t" + m11 + "\t" + m12 + "]" + "\n [" + m20 + "\t" + m21 + "\t"
582 * Sets the value of this matrix to the matrix conversion of the single
583 * precision axis and angle argument.
586 * the axis and angle to be converted
589 public M3 setAA(A4 a) {
595 * 3D ball rotation from dx dy in-plane mouse motion
596 * adapted from Andrew Hanson
597 * Computer Graphics beyond the Third Dimension:
598 * Geometry, Orientation Control, and Rendering for
599 * Graphics in Dimensions Greater than Three
600 * Course Notes for SIGGRAPH ’98
601 * http://www.cse.ohio-state.edu/~hwshen/888_su02/hanson_note.pdf
603 * @param responseFactor Jmol uses 0.02 here
606 * @return true if successful; false if not;
608 public boolean setAsBallRotation(float responseFactor, float dx, float dy) {
609 float r = (float) Math.sqrt(dx * dx + dy * dy);
610 float th = r * responseFactor;
615 float c = (float) Math.cos(th);
616 float s = (float) Math.sin(th);
620 m00 = 1 + c1 * nx * nx;
621 m01 = m10 = c1 * nx * ny;
622 m20 = -(m02 = s * nx);
623 m11 = 1 + c1 * ny * ny;
624 m21 = -(m12 = s * ny);
629 public boolean isRotation() {
630 return (Math.abs(determinant3() - 1) < 0.001f);