1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
package my3d; public class _math3d { public static final double DEG = Math.PI / 180; private static double[][] m_buf1 = new double[3][3], m_buf2 = new double[3][3]; public static double[][] copy_matrix(double[][] m, double[][] mo) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) mo[i][j] = m[i][j]; return mo; } public static double[] copy_vector(double[] v, double[] vo) { for (int i = 0; i < 3; i++) vo[i] = v[i]; return vo; } public static double[] set_vector( double x, double y, double z, double[] vo ) { vo[0] = x; vo[1] = y; vo[2] = z; return vo; } public static class _transform { public double[][] ori = new double[3][3]; // 姿勢 public double[] pos = new double[3]; // 位置 public _transform( double ax, double ay, double az, double x, double y, double z ) { eular(ax, ay, az, ori); set_vector(x, y, z, pos); } } public static double[][] rot_axis(int axis, double a, double[][] mo) { a *= DEG; double sa = Math.sin(a), ca = Math.cos(a); int x = (axis + 1) % 3, y = (axis + 2) % 3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) mo[i][j] = ((i == axis) ? 1 : ca); else if (i == x && j == y) mo[i][j] = sa; else if (i == y && j == x) mo[i][j] = -sa; else mo[i][j] = 0; } } return mo; } public static double[][] rot_x(double a, double[][] mo) { return rot_axis(0, a, mo); } public static double[][] rot_y(double a, double[][] mo) { return rot_axis(1, a, mo); } public static double[][] rot_z(double a, double[][] mo) { return rot_axis(2, a, mo); } public static double[][] eular( double ax, double ay, double az, double[][] mo ) { rot_z(az, mo); rot_y(ay, m_buf2); mul(m_buf2, mo, mo); rot_x(ax, m_buf2); mul(m_buf2, mo, mo); return mo; } public static _transform mul( _transform tf1, _transform tf2, _transform tfo ) { mul(tf1.ori, tf2.ori, tfo.ori); mul(tf1, tf2.pos, tfo.pos); return tfo; } public static double[] mul( _transform tf, double[] v, double[] vo ) { mul(tf.ori, v, vo); return add(tf.pos, vo, vo); } public static double[][] mul( double[][] m1, double[][] m2, double[][] mo ) { for (int i = 0; i < 3; i++) mul(m1, m2[i], m_buf1[i]); return copy_matrix(m_buf1, mo); } public static double[] mul( double[][] m, double[] v, double[] vo ) { double x = v[0], y = v[1], z = v[2]; for (int i = 0; i < 3; i++) vo[i] = m[0][i] * x + m[1][i] * y + m[2][i] * z; return vo; } public static double[] add(double[] v1, double[] v2, double[] vo) { vo[0] = v1[0] + v2[0]; vo[1] = v1[1] + v2[1]; vo[2] = v1[2] + v2[2]; return vo; } public static double[] sub(double[] v1, double[] v2, double[] vo) { vo[0] = v1[0] - v2[0]; vo[1] = v1[1] - v2[1]; vo[2] = v1[2] - v2[2]; return vo; } public static double[] normalize(double[] v, double[] vo) { double len = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); vo[0] = v[0] / len; vo[1] = v[1] / len; vo[2] = v[2] / len; return vo; } public static double dot(double[] v1, double[] v2) { return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; } public static double[] cross(double[] v1, double[] v2, double[] vo) { double x, y; x = v1[1] * v2[2] - v1[2] * v2[1]; y = v1[2] * v2[0] - v1[0] * v2[2]; vo[2] = v1[0] * v2[1] - v1[1] * v2[0]; vo[0] = x; vo[1] = y; return vo; } public static double[] lerp(double[] v1, double[] v2, double t1, double[] vo) { double t2 = (1 - t1); vo[0] = v1[0] * t1 + v2[0] * t2; vo[1] = v1[1] * t1 + v2[1] * t2; vo[2] = v1[2] * t1 + v2[2] * t2; return vo; } }