Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2009-2012 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.renderscript;
     18 
     19 
     20 /**
     21  * Class for exposing the native RenderScript rs_matrix2x2 type back to the Android system.
     22  *
     23  **/
     24 public class Matrix2f {
     25 
     26     /**
     27     * Creates a new identity 2x2 matrix
     28     */
     29     public Matrix2f() {
     30         mMat = new float[4];
     31         loadIdentity();
     32     }
     33 
     34     /**
     35     * Creates a new matrix and sets its values from the given
     36     * parameter
     37     *
     38     * @param dataArray values to set the matrix to, must be 4
     39     *                  floats long
     40     */
     41     public Matrix2f(float[] dataArray) {
     42         mMat = new float[4];
     43         System.arraycopy(dataArray, 0, mMat, 0, mMat.length);
     44     }
     45 
     46     /**
     47     * Return a reference to the internal array representing matrix
     48     * values. Modifying this array will also change the matrix
     49     *
     50     * @return internal array representing the matrix
     51     */
     52     public float[] getArray() {
     53         return mMat;
     54     }
     55 
     56     /**
     57     * Returns the value for a given row and column
     58     *
     59     * @param x column of the value to return
     60     * @param y row of the value to return
     61     *
     62     * @return value in the yth row and xth column
     63     */
     64     public float get(int x, int y) {
     65         return mMat[x*2 + y];
     66     }
     67 
     68     /**
     69     * Sets the value for a given row and column
     70     *
     71     * @param x column of the value to set
     72     * @param y row of the value to set
     73     */
     74     public void set(int x, int y, float v) {
     75         mMat[x*2 + y] = v;
     76     }
     77 
     78     /**
     79     * Sets the matrix values to identity
     80     */
     81     public void loadIdentity() {
     82         mMat[0] = 1;
     83         mMat[1] = 0;
     84 
     85         mMat[2] = 0;
     86         mMat[3] = 1;
     87     }
     88 
     89     /**
     90     * Sets the values of the matrix to those of the parameter
     91     *
     92     * @param src matrix to load the values from
     93     */
     94     public void load(Matrix2f src) {
     95         System.arraycopy(src.getArray(), 0, mMat, 0, mMat.length);
     96     }
     97 
     98     /**
     99     * Sets current values to be a rotation matrix of given angle
    100     *
    101     * @param rot rotation angle
    102     */
    103     public void loadRotate(float rot) {
    104         float c, s;
    105         rot *= (float)(java.lang.Math.PI / 180.0f);
    106         c = (float)java.lang.Math.cos(rot);
    107         s = (float)java.lang.Math.sin(rot);
    108         mMat[0] = c;
    109         mMat[1] = -s;
    110         mMat[2] = s;
    111         mMat[3] = c;
    112     }
    113 
    114     /**
    115     * Sets current values to be a scale matrix of given dimensions
    116     *
    117     * @param x scale component x
    118     * @param y scale component y
    119     */
    120     public void loadScale(float x, float y) {
    121         loadIdentity();
    122         mMat[0] = x;
    123         mMat[3] = y;
    124     }
    125 
    126     /**
    127     * Sets current values to be the result of multiplying two given
    128     * matrices
    129     *
    130     * @param lhs left hand side matrix
    131     * @param rhs right hand side matrix
    132     */
    133     public void loadMultiply(Matrix2f lhs, Matrix2f rhs) {
    134         for (int i=0 ; i<2 ; i++) {
    135             float ri0 = 0;
    136             float ri1 = 0;
    137             for (int j=0 ; j<2 ; j++) {
    138                 float rhs_ij = rhs.get(i,j);
    139                 ri0 += lhs.get(j,0) * rhs_ij;
    140                 ri1 += lhs.get(j,1) * rhs_ij;
    141             }
    142             set(i,0, ri0);
    143             set(i,1, ri1);
    144         }
    145     }
    146 
    147     /**
    148     * Post-multiplies the current matrix by a given parameter
    149     *
    150     * @param rhs right hand side to multiply by
    151     */
    152     public void multiply(Matrix2f rhs) {
    153         Matrix2f tmp = new Matrix2f();
    154         tmp.loadMultiply(this, rhs);
    155         load(tmp);
    156     }
    157     /**
    158     * Modifies the current matrix by post-multiplying it with a
    159     * rotation matrix of given angle
    160     *
    161     * @param rot angle of rotation
    162     */
    163     public void rotate(float rot) {
    164         Matrix2f tmp = new Matrix2f();
    165         tmp.loadRotate(rot);
    166         multiply(tmp);
    167     }
    168     /**
    169     * Modifies the current matrix by post-multiplying it with a
    170     * scale matrix of given dimensions
    171     *
    172     * @param x scale component x
    173     * @param y scale component y
    174     */
    175     public void scale(float x, float y) {
    176         Matrix2f tmp = new Matrix2f();
    177         tmp.loadScale(x, y);
    178         multiply(tmp);
    179     }
    180     /**
    181     * Sets the current matrix to its transpose
    182     */
    183     public void transpose() {
    184         float temp = mMat[1];
    185         mMat[1] = mMat[2];
    186         mMat[2] = temp;
    187     }
    188 
    189     final float[] mMat;
    190 }
    191 
    192 
    193 
    194