diff --git a/modules/androidkit/src/Matrix.cpp b/modules/androidkit/src/Matrix.cpp index c1579b5e74..fc991fa739 100644 --- a/modules/androidkit/src/Matrix.cpp +++ b/modules/androidkit/src/Matrix.cpp @@ -20,18 +20,11 @@ static jlong Matrix_Create(JNIEnv* env, jobject, jfloat m0, jfloat m4, jfloat m8 m2, m6, m10, m14, m3, m7, m11, m15)); } + static void Matrix_Release(JNIEnv* env, jobject, jlong native_matrix) { delete reinterpret_cast(native_matrix); } -static void Matrix_PostConcat(JNIEnv* env, jobject, jlong native_matrixA, jlong native_matrixB) { - if (auto* mA = reinterpret_cast(native_matrixA), - * mB = reinterpret_cast(native_matrixB); mA && mB) { - mA->postConcat(*mB); - } - -} - static void Matrix_PreConcat(JNIEnv* env, jobject, jlong native_matrixA, jlong native_matrixB) { if (auto* mA = reinterpret_cast(native_matrixA), * mB = reinterpret_cast(native_matrixB); mA && mB) { @@ -52,7 +45,6 @@ int register_androidkit_Matrix(JNIEnv* env) { static const JNINativeMethod methods[] = { {"nCreate" , "(FFFFFFFFFFFFFFFF)J" , reinterpret_cast(Matrix_Create)}, {"nRelease" , "(J)V" , reinterpret_cast(Matrix_Release)}, - {"nPostConcat" , "(JJ)V" , reinterpret_cast(Matrix_PostConcat)}, {"nPreConcat" , "(JJ)V" , reinterpret_cast(Matrix_PreConcat)}, {"nConcat" , "(JJ)J" , reinterpret_cast(Matrix_Concat)}, }; diff --git a/platform_tools/android/apps/AndroidKit/src/main/java/org/skia/androidkit/Matrix.java b/platform_tools/android/apps/AndroidKit/src/main/java/org/skia/androidkit/Matrix.java index 023f5bc161..e27033ac58 100644 --- a/platform_tools/android/apps/AndroidKit/src/main/java/org/skia/androidkit/Matrix.java +++ b/platform_tools/android/apps/AndroidKit/src/main/java/org/skia/androidkit/Matrix.java @@ -41,7 +41,9 @@ public class Matrix { } /* - * Concat A * B, return new matrix as result + * A: this Matrix + * B: Matrix passed in + * Concat A * B, return new Matrix C as result */ public static Matrix Concat(Matrix a, Matrix b) { long nativeA = a.mNativeInstance; @@ -51,15 +53,8 @@ public class Matrix { } /* - * Concat A * B, store result in Matrix A - */ - public void postConcat(Matrix b) { - long nativeA = this.mNativeInstance; - long nativeB = b.mNativeInstance; - nPostConcat(nativeA, nativeB); - } - - /* + * A: this Matrix + * B: Matrix passed in * Concat B * A, store result in Matrix A */ public void preConcat(Matrix b) { @@ -68,6 +63,78 @@ public class Matrix { nPreConcat(nativeA, nativeB); } + /* + * Translates this Matrix by x, y, z + * Store result in caller Matrix + * returns reference to this Matrix for operation chaining + * + * TODO: optimize calls to JNI + */ + public Matrix translate(float x, float y, float z) { + Matrix t = new Matrix(1, 0, 0, x, + 0, 1, 0, y, + 0, 0, 1, z, + 0, 0, 0, 1); + this.preConcat(t); + return this; + } + + /* + * Scales this Matrix by x, y, z + * Store result in caller Matrix + * returns reference to this Matrix for operation chaining + */ + public Matrix scale(float x, float y, float z) { + Matrix s = new Matrix(x, 0, 0, 0, + 0, y, 0, 0, + 0, 0, z, 0, + 0, 0, 0, 1); + this.preConcat(s); + return this; + } + + /* + * Rotates this Matrix along the x-axis by rad radians + * Store result in caller Matrix + * returns reference to this Matrix for operation chaining + */ + public Matrix rotateX(float rad) { + Matrix r = new Matrix(1, 0, 0, 0, + 0, (float) Math.cos(rad), (float) -Math.sin(rad), 0, + 0, (float) Math.sin(rad), (float) Math.cos(rad), 0, + 0, 0, 0, 1); + this.preConcat(r); + return this; + } + + /* + * Rotates this Matrix along the y-axis by rad radians + * Store result in caller Matrix + * returns reference to this Matrix for operation chaining + */ + public Matrix rotateY(float rad) { + Matrix r = new Matrix((float) Math.cos(rad), 0, (float) Math.sin(rad), 0, + 0, 1, 0, 0, + (float) -Math.sin(rad), 0, (float) Math.cos(rad), 0, + 0, 0, 0, 1); + this.preConcat(r); + return this; + } + + /* + * Rotates this Matrix along the z-axis by rad radians + * Store result in caller Matrix + * returns reference to this Matrix for operation chaining + */ + public Matrix rotateZ(float rad) { + Matrix r = new Matrix((float) Math.cos(rad), (float) -Math.sin(rad), 0, 0, + (float) Math.sin(rad), (float) Math.cos(rad), 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); + this.preConcat(r); + return this; + } + /** * Releases any resources associated with this Matrix. */ @@ -87,7 +154,6 @@ public class Matrix { float m3, float m7, float m11, float m15); private static native void nRelease(long nativeInstance); - private static native void nPostConcat(long mNativeInstanceA, long mNativeInstanceB); private static native void nPreConcat(long mNativeInstanceA, long mNativeInstanceB); private static native long nConcat(long mNativeInstanceA, long mNativeInstanceB); }