1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.tools.datatypes 18 19 import android.tools.FloatFormatter 20 import android.tools.withCache 21 22 /** 23 * Representation of a matrix 3x3 used for layer transforms 24 * 25 * ``` 26 * |dsdx dsdy tx| 27 * matrix = |dtdx dtdy ty| 28 * |0 0 1 | 29 * ``` 30 */ 31 class Matrix33 32 private constructor( 33 val dsdx: Float, 34 val dtdx: Float, 35 val tx: Float = 0F, 36 val dsdy: Float, 37 val dtdy: Float, 38 val ty: Float = 0F 39 ) : DataType() { 40 override val isEmpty = 41 dsdx == 0f && dtdx == 0f && tx == 0f && dsdy == 0f && dtdy == 0f && ty == 0f 42 doPrintValuenull43 override fun doPrintValue() = buildString { 44 append("dsdx:${FloatFormatter.format(dsdx)} ") 45 append("dtdx:${FloatFormatter.format(dtdx)} ") 46 append("dsdy:${FloatFormatter.format(dsdy)} ") 47 append("dtdy:${FloatFormatter.format(dtdy)} ") 48 append("tx:${FloatFormatter.format(tx)} ") 49 append("ty:${FloatFormatter.format(ty)}") 50 } 51 52 companion object { 53 val EMPTY: Matrix33 <lambda>null54 get() = withCache { from(dsdx = 0f, dtdx = 0f, tx = 0f, dsdy = 0f, dtdy = 0f, ty = 0f) } 55 <lambda>null56 fun identity(x: Float, y: Float): Matrix33 = withCache { 57 from(dsdx = 1f, dtdx = 0f, x, dsdy = 0f, dtdy = 1f, y) 58 } 59 <lambda>null60 fun rot270(x: Float, y: Float): Matrix33 = withCache { 61 from(dsdx = 0f, dtdx = -1f, x, dsdy = 1f, dtdy = 0f, y) 62 } 63 <lambda>null64 fun rot180(x: Float, y: Float): Matrix33 = withCache { 65 from(dsdx = -1f, dtdx = 0f, x, dsdy = 0f, dtdy = -1f, y) 66 } 67 <lambda>null68 fun rot90(x: Float, y: Float): Matrix33 = withCache { 69 from(dsdx = 0f, dtdx = 1f, x, dsdy = -1f, dtdy = 0f, y) 70 } 71 fromnull72 fun from( 73 dsdx: Float, 74 dtdx: Float, 75 tx: Float, 76 dsdy: Float, 77 dtdy: Float, 78 ty: Float 79 ): Matrix33 = withCache { Matrix33(dsdx, dtdx, tx, dsdy, dtdy, ty) } 80 } 81 } 82