diff options
author | 2018-07-12 14:26:54 -0400 | |
---|---|---|
committer | 2018-07-12 20:08:53 +0000 | |
commit | 5bc4fc8f3f26e9e14cccee4d200309e4b500b8d3 (patch) | |
tree | 9e69dcbfcd130f8a18284f7ae8229d5fd0669d61 /platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java | |
parent | 2272ea2a4bcd3cd16547eeb8e0902d9b06ea26b4 (diff) |
Drawing point cloud + planes on Canvas
Java-only version of SkAR: drawing on android Canvas
Bug: skia:
Change-Id: I76285aa85c7523ad9ae213d32a159b671d8aa30f
Reviewed-on: https://skia-review.googlesource.com/141048
Reviewed-by: Mike Reed <reed@google.com>
Diffstat (limited to 'platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java')
-rw-r--r-- | platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java | 156 |
1 files changed, 58 insertions, 98 deletions
diff --git a/platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java b/platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java index fc9333c6df..13c249ce36 100644 --- a/platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java +++ b/platform_tools/android/apps/skar_java/src/main/java/com/google/skar/SkARMatrix.java @@ -1,6 +1,7 @@ package com.google.skar; import android.graphics.Matrix; +import android.graphics.PointF; /** * Provides static methods for matrix manipulation. Input matrices are assumed to be 4x4 @@ -10,70 +11,8 @@ import android.graphics.Matrix; */ public class SkARMatrix { - /** - * Returns an android.graphics.Matrix that can be used on a Canvas to draw a 2D object in - * perspective. Objects will be rotated towards the XZ plane. Undefined behavior when any of - * the matrices are not of size 16, or are null. - * - * @param model 4x4 model matrix of the object to be drawn (global/world) - * @param view 4x4 camera view matrix (brings objects to camera origin and orientation) - * @param projection 4x4 projection matrix - * @param viewport 4x4 viewport matrix - * @return 3x3 matrix that puts a 2D objects in perspective on a Canvas - */ - - public static Matrix createPerspectiveMatrix(float[] model, float[] view, float[] projection, - float[] viewport) { - float[] skiaRotation = createXYtoXZRotationMatrix(); - float[][] matrices = {skiaRotation, model, view, projection, viewport}; - return createMatrixFrom4x4Array(matrices); - } - - /** - * Returns an android.graphics.Matrix that can be used on a Canvas to draw a 2D object in - * perspective. Undefined behavior when any of the matrices are not of size 16, or are null. - * - * @param model 4x4 model matrix of the object to be drawn (global/world) - * @param view 4x4 camera view matrix (brings objects to camera origin and orientation) - * @param projection 4x4 projection matrix - * @param viewport 4x4 viewport matrix - * @param rotatePlane specifies if object should be from the XY plane to the XZ plane - * @return 3x3 matrix that puts a 2D objects in perspective on a Canvas - */ - - public static Matrix createPerspectiveMatrix(float[] model, float[] view, float[] projection, - float[] viewport, boolean rotatePlane) { - if (rotatePlane) { - return createPerspectiveMatrix(model, view, projection, viewport); - } else { - float[][] matrices = {model, view, projection, viewport}; - return createMatrixFrom4x4Array(matrices); - } - } - - /** - * Returns an android.graphics.Matrix that can be used on a Canvas to draw a 2D object in - * perspective. Undefined behavior when any of the matrices are not of size 16, or are null. - * - * @param model 4x4 model matrix of the object to be drawn (global/world) - * @param view 4x4 camera view matrix (brings objects to camera origin and orientation) - * @param projection 4x4 projection matrix - * @param viewPortWidth width of viewport of GLSurfaceView - * @param viewPortHeight height of viewport of GLSurfaceView - * @param rotatePlane specifies if object should be from the XY plane to the XZ plane - * @return 3x3 matrix that puts a 2D objects in perspective on a Canvas - */ - public static Matrix createPerspectiveMatrix(float[] model, float[] view, float[] projection, - float viewPortWidth, float viewPortHeight, boolean rotatePlane) { - if (rotatePlane) { - return createPerspectiveMatrix(model, view, projection, viewPortWidth, viewPortHeight); - } else { - float[] viewPort = createViewportMatrix(viewPortWidth, viewPortHeight); - float[][] matrices = {model, view, projection, viewPort}; - return createMatrixFrom4x4Array(matrices); - } - } + /******************* PUBLIC FUNCTIONS ***********************/ /** * Returns an android.graphics.Matrix that can be used on a Canvas to draw a 2D object in @@ -119,31 +58,19 @@ public class SkARMatrix { * to rotate them from the XY plane to the XZ plane. */ - private static float[] createXYtoXZRotationMatrix() { + public static float[] createXYtoXZRotationMatrix() { float[] rotation = new float[16]; android.opengl.Matrix.setIdentityM(rotation, 0); android.opengl.Matrix.rotateM(rotation, 0, 90, 1, 0, 0); return rotation; } - /** - * Returns an android.graphics.Matrix resulting from a 9-float matrix array in row-major order. - * Undefined behavior when the array is not of size 9 or is null. - * - * @param m3 9-float matrix array in row-major order - */ - - public static Matrix createMatrixFrom3x3(float[] m3) { - Matrix m = new Matrix(); - m.setValues(m3); - return m; - } /** * Returns an android.graphics.Matrix resulting from a 16-float matrix array in column-major order * Undefined behavior when the array is not of size 16 or is null. * - * @param m4 + * @param m4 16-float matrix in column-major order */ public static Matrix createMatrixFrom4x4(float[] m4) { @@ -152,6 +79,46 @@ public class SkARMatrix { } /** + * Returns an android.graphics.PointF resulting from the multiplication of a Vector of 4 floats + * with a 4x4 float Matrix. The return PointF is the (x, y) values of m4 * v4 + * @param m4 16-float matrix in column-major order + * @param v4 4-float vector + * @param perspectiveDivide if true, divide return value by the w-coordinate + * @return PointF resulting from taking the (x, y) values of m4 * v4 + */ + public static PointF multiplyMatrixVector(float[] m4, float[] v4, boolean perspectiveDivide) { + float[] result = new float[4]; + android.opengl.Matrix.multiplyMV(result, 0, m4, 0, v4, 0); + if (perspectiveDivide) { + return new PointF(result[0] / result[3], result[1] / result[3]); + } + + return new PointF(result[0], result[1]); + } + + /** + * Returns a 16-float matrix in column-major order resulting from the multiplication of matrices. + * e.g: m4Array = {m1, m2, m3} --> returns m = m3 * m2 * m1 + * Undefined behavior when the array is empty, null, or contains arrays not of size 9 (or null) + * + * @param m4Array array of 16-float matrices in column-major order + */ + + public static float[] multiplyMatrices4x4(float[][] m4Array) { + float[] result = new float[16]; + android.opengl.Matrix.setIdentityM(result, 0); + float[] rhs = result; + for (int i = 0; i < m4Array.length; i++) { + float[] lhs = m4Array[i]; + android.opengl.Matrix.multiplyMM(result, 0, lhs, 0, rhs, 0); + rhs = result; + } + return result; + } + + /******************* PRIVATE FUNCTIONS ***********************/ + + /** * Returns an android.graphics.Matrix resulting from the concatenation of 16-float matrices * in column-major order from left to right. * e.g: m4Array = {m1, m2, m3} --> returns m = m3 * m2 * m1 @@ -160,12 +127,25 @@ public class SkARMatrix { * @param m4Array array of 16-float matrices in column-major order */ - public static Matrix createMatrixFrom4x4Array(float[][] m4Array) { + private static Matrix createMatrixFrom4x4Array(float[][] m4Array) { float[] result = multiplyMatrices4x4(m4Array); return createMatrixFrom4x4(result); } /** + * Returns an android.graphics.Matrix resulting from a 9-float matrix array in row-major order. + * Undefined behavior when the array is not of size 9 or is null. + * + * @param m3 9-float matrix array in row-major order + */ + + private static Matrix createMatrixFrom3x3(float[] m3) { + Matrix m = new Matrix(); + m.setValues(m3); + return m; + } + + /** * Returns a 9-float matrix in row-major order given a 16-float matrix in column-major order. * This will drop the Z column and row. * Undefined behavior when the array is not of size 9 or is null. @@ -188,24 +168,4 @@ public class SkARMatrix { } return m3; } - - /** - * Returns a 16-float matrix in column-major order resulting from the multiplication of matrices. - * e.g: m4Array = {m1, m2, m3} --> returns m = m3 * m2 * m1 - * Undefined behavior when the array is empty, null, or contains arrays not of size 9 (or null) - * - * @param m4Array array of 16-float matrices in column-major order - */ - - private static float[] multiplyMatrices4x4(float[][] m4Array) { - float[] result = new float[16]; - android.opengl.Matrix.setIdentityM(result, 0); - float[] rhs = result; - for (int i = 0; i < m4Array.length; i++) { - float[] lhs = m4Array[i]; - android.opengl.Matrix.multiplyMM(result, 0, lhs, 0, rhs, 0); - rhs = result; - } - return result; - } } |