ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
MatrixUtils.java
Go to the documentation of this file.
1 /*
2  * Taken from android-gl, Making OpenGL Programming in Android Easier
3  * URL: https://code.google.com/p/android-gl/source/browse/trunk/AndroidGL/src/edu/union/
4  *
5  * License: GNU Lesser General Public License
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package com.mission_base.arviewer_android.viewer.utilities;
22 
23 
24 public class MatrixUtils
25 {
26 
35  public static void transpose(float[][] m, float[][] result)
36  {
37  for (int i = 0; i < 4; i++)
38  for (int j = 0; j < 4; j++)
39  result[j][i] = m[i][j];
40  }
41 
42  public static float[][] matrix(float[] v)
43  {
44  float[][] result = new float[][] { //
45  { //
46  v[0], v[4], v[8], v[12] }, //
47  { v[1], v[5], v[9], v[13] }, //
48  { v[2], v[6], v[10], v[14] }, //
49  { v[3], v[7], v[11], v[15] } //
50  };
51  return result;
52  }
53 
54  public static void inverse(float[][] in, float[][] out)
55  {
56  int st_vrs = 4, st_stolp = 4;
57  float[][] old = new float[st_vrs][st_stolp * 2];
58  float[][] tmp = new float[st_vrs][st_stolp * 2];
59 
60  for (int v = 0; v < st_vrs; v++)
61  {// ones vector
62  for (int s = 0; s < st_stolp * 2; s++)
63  {
64  if (s - v == st_vrs)
65  old[v][s] = 1;
66  if (s < st_stolp)
67  old[v][s] = in[v][s];
68  }
69  }
70  // zeros below the diagonal
71  for (int v = 0; v < st_vrs; v++)
72  {
73  for (int v1 = 0; v1 < st_vrs; v1++)
74  {
75  for (int s = 0; s < st_stolp * 2; s++)
76  {
77  if (v == v1)
78  tmp[v][s] = old[v][s] / old[v][v];
79  else
80  tmp[v1][s] = old[v1][s];
81  }
82  }
83  old = prepisi(tmp);
84  for (int v1 = v + 1; v1 < st_vrs; v1++)
85  {
86  for (int s = 0; s < st_stolp * 2; s++)
87  {
88  tmp[v1][s] = old[v1][s] - old[v][s] * old[v1][v];
89  }
90  }
91  old = prepisi(tmp);
92  }
93  // zeros above the diagonal
94  for (int s = st_stolp - 1; s > 0; s--)
95  {
96  for (int v = s - 1; v >= 0; v--)
97  {
98  for (int s1 = 0; s1 < st_stolp * 2; s1++)
99  {
100  tmp[v][s1] = old[v][s1] - old[s][s1] * old[v][s];
101  }
102  }
103  old = prepisi(tmp);
104  }
105  for (int v = 0; v < st_vrs; v++)
106  {// right part of matrix is inverse
107  for (int s = st_stolp; s < st_stolp * 2; s++)
108  {
109  out[v][s - st_stolp] = tmp[v][s];
110  }
111  }
112  }
113 
114  private static float[][] prepisi(float[][] in)
115  {
116  float[][] out = new float[in.length][in[0].length];
117  for (int v = 0; v < in.length; v++)
118  {
119  for (int s = 0; s < in[0].length; s++)
120  {
121  out[v][s] = in[v][s];
122  }
123  }
124  return out;
125  }
126 
127  public static void transpose(float[] m, float[] result)
128  {
129  for (int i = 0; i < 4; i++)
130  for (int j = 0; j < 4; j++)
131  result[j * 4 + i] = m[i * 4 + j];
132  }
133 
141  public static void normalize(float[] vector)
142  {
143  scalarMultiply(vector, 1 / magnitude(vector));
144  }
145 
153  public static void normalize(int[] vector)
154  {
155  scalarMultiply(vector, 1 / magnitude(vector));
156  }
157 
166  public static void copy(float[] from, float[] to)
167  {
168  for (int i = 0; i < from.length; i++)
169  {
170  to[i] = from[i];
171  }
172  }
173 
185  public static void multiply(float[][] m1, float[][] m2, float[][] result)
186  {
187  for (int i = 0; i < 4; i++)
188  {
189  for (int j = 0; j < 4; j++)
190  {
191  result[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j] + m1[i][3] * m2[3][j];
192  }
193  }
194  }
195 
204  public static void scalarMultiply(float[] vector, float scalar)
205  {
206  for (int i = 0; i < vector.length; i++)
207  vector[i] *= scalar;
208  }
209 
218  public static void scalarMultiply(int[] vector, int scalar)
219  {
220  for (int i = 0; i < vector.length; i++)
221  vector[i] = FixedPointUtils.multiply(vector[i], scalar);
222  }
223 
230  public static void identity(float[][] matrix)
231  {
232  for (int i = 0; i < 4; i++)
233  for (int j = 0; j < 4; j++)
234  matrix[i][j] = (i == j) ? 1 : 0;
235  }
236 
246  public static float dot(float[] v1, float[] v2)
247  {
248  float res = 0;
249  for (int i = 0; i < v1.length; i++)
250  res += v1[i] * v2[i];
251  return res;
252  }
253 
264  public static void cross(float[] p1, float[] p2, float[] result)
265  {
266  result[0] = p1[1] * p2[2] - p2[1] * p1[2];
267  result[1] = p1[2] * p2[0] - p2[2] * p1[0];
268  result[2] = p1[0] * p2[1] - p2[0] * p1[1];
269  }
270 
281  public static void cross(int[] p1, int[] p2, int[] result)
282  {
283  result[0] = FixedPointUtils.multiply(p1[1], p2[2]) - FixedPointUtils.multiply(p2[1], p1[2]);
284  result[1] = FixedPointUtils.multiply(p1[2], p2[0]) - FixedPointUtils.multiply(p2[2], p1[0]);
285  result[2] = FixedPointUtils.multiply(p1[0], p2[1]) - FixedPointUtils.multiply(p2[0], p1[1]);
286  }
287 
295  public static float magnitude(float[] vector)
296  {
297  return (float) Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
298  }
299 
307  public static int magnitude(int[] vector)
308  {
309  return FixedPointUtils.sqrt(FixedPointUtils.multiply(vector[0], vector[0]) + FixedPointUtils.multiply(vector[1], vector[1])
310  + FixedPointUtils.multiply(vector[2], vector[2]));
311  }
312 
313  public static void rotateZ(float[] v, float angle, float[] out)
314  {
315  float[][] R = new float[4][4];
316  R[0][0] = (float) Math.cos(angle);
317  R[0][1] = (float) -Math.sin(angle);
318  R[1][0] = (float) Math.sin(angle);
319  R[1][1] = (float) Math.cos(angle);
320  R[2][2] = R[3][3] = 1;
321 
322  multiply(R, v, out);
323  }
324 
325  public static void rotateY(float[] v, float angle, float[] out)
326  {
327  float[][] R = new float[4][4];
328  R[0][0] = (float) Math.cos(angle);
329  R[0][2] = (float) -Math.sin(angle);
330  R[1][0] = (float) Math.sin(angle);
331  R[1][2] = (float) Math.cos(angle);
332  R[1][1] = R[3][3] = 1;
333 
334  multiply(R, v, out);
335  }
336 
347  public static void multiply(float[][] matrix, float[] vector, float[] res)
348  {
349  for (int i = 0; i < 4; i++)
350  {
351  res[i] = matrix[i][0] * vector[0] + matrix[i][1] * vector[1] + matrix[i][2] * vector[2] + matrix[i][3] * vector[3];
352  }
353  }
354 
361  public static void printMatrix(float[][] matrix)
362  {
363  for (int i = 0; i < 4; i++)
364  {
365  for (int j = 0; j < 4; j++)
366  System.out.print(matrix[i][j] + "\t");
367  System.out.println();
368  }
369  }
370 
377  public static void homogenize(float[] pt)
378  {
379  scalarMultiply(pt, 1 / pt[3]);
380  }
381 
388  public static void printVector(float[] vec)
389  {
390  for (int i = 0; i < vec.length; i++)
391  System.out.println(vec[i]);
392  }
393 
404  public static void minus(float[] a, float[] b, float[] result)
405  {
406  float[] res = (result == null) ? a : result;
407  for (int i = 0; i < Math.min(a.length, b.length); i++)
408  res[i] = a[i] - b[i];
409  }
410 
421  public static void minus(int[] a, int[] b, int[] result)
422  {
423  int[] res = (result == null) ? a : result;
424  for (int i = 0; i < Math.min(a.length, b.length); i++)
425  res[i] = a[i] - b[i];
426  }
427 
438  public static void plus(float[] a, float[] b, float[] result)
439  {
440  float[] res = (result == null) ? a : result;
441  for (int i = 0; i < a.length; i++)
442  res[i] = a[i] + b[i];
443  }
444 
455  public static void plus(int[] a, int[] b, int[] result)
456  {
457  int[] res = (result == null) ? a : result;
458  for (int i = 0; i < a.length; i++)
459  res[i] = a[i] + b[i];
460  }
461 }