javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/M2GSVGMatrix.java
changeset 80 d6dafc5d983f
parent 56 abc41079b313
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.m2g;
       
    20 
       
    21 import org.w3c.dom.svg.*;
       
    22 import org.w3c.dom.DOMException;
       
    23 
       
    24 /**
       
    25  * This interface represents an "SVGMatrix" datatype, identified by an affine transform.
       
    26  * It can be used to read and modify the values of transform attribute as per SVG specification.
       
    27  *
       
    28  * The transformation can be represented using matrix math on a 3x3 array.
       
    29  * Given (x,y), the transformation (x',y') can be found by:
       
    30  * [ x']   [ m00 m01 m02 ] [ x ]   [ m00*x + m01*y + m02 ]
       
    31  * [ y'] = [ m10 m11 m12 ] [ y ] = [ m10*x + m11*y + m12 ]
       
    32  * [ 1 ]   [  0   0   1  ] [ 1 ]   [          1          ]
       
    33  *
       
    34  * The bottom row of the matrix is constant, so a transform can be uniquely
       
    35  * represented by "[[m00, m01, m02], [m10, m11, m12]]".
       
    36  */
       
    37 public class M2GSVGMatrix implements SVGMatrix
       
    38 {
       
    39     //--------------------------------------------------
       
    40     // STATIC CONSTANTS
       
    41     //--------------------------------------------------
       
    42     /* Optimization: static finals changed to local variables
       
    43     static private final String INVALID_INDEX_ESTR =
       
    44     "The index is invalid.";
       
    45     static private final String DET_IS_ZERO_ESTR =
       
    46     "The determinant of this matrix is zero.";
       
    47     */
       
    48     static final int ARRAY_SIZE = 6;
       
    49     // Array index
       
    50     static private final int M00 = 0;
       
    51     static private final int M10 = 1;
       
    52     static private final int M01 = 2;
       
    53     static private final int M11 = 3;
       
    54     static private final int M02 = 4;
       
    55     static private final int M12 = 5;
       
    56 
       
    57     //--------------------------------------------------
       
    58     // VARIABLES
       
    59     //--------------------------------------------------
       
    60     private float iComponents[];
       
    61 
       
    62     //--------------------------------------------------
       
    63     // METHODS
       
    64     //--------------------------------------------------
       
    65     /**
       
    66      * Construct a matrix with the following components:
       
    67      * <pre>
       
    68      * [1 0 0]
       
    69      * [0 1 0]
       
    70      * </pre>
       
    71      */
       
    72     protected M2GSVGMatrix()
       
    73     {
       
    74         iComponents = new float[ARRAY_SIZE];
       
    75         iComponents[M00] = iComponents[M11] = 1;
       
    76     }
       
    77 
       
    78     /**
       
    79     /**
       
    80      * Construct a matrix with the following components:
       
    81      * <pre>
       
    82      * [aM00 aM01 aM02]
       
    83      * [aM10 aM11 aM12]
       
    84      * </pre>
       
    85      * @param aM00 the x scaling component
       
    86      * @param aM10 the y shearing component
       
    87      * @param aM01 the x shearing component
       
    88      * @param aM11 the y scaling component
       
    89      * @param aM02 the x translation component
       
    90      * @param aM12 the y translation component
       
    91      */
       
    92     protected M2GSVGMatrix(float aM00, float aM10, float aM01,
       
    93                            float aM11, float aM02, float aM12)
       
    94     {
       
    95         iComponents = new float[ARRAY_SIZE];
       
    96         iComponents[M00] = aM00;
       
    97         iComponents[M01] = aM01;
       
    98         iComponents[M02] = aM02;
       
    99         iComponents[M10] = aM10;
       
   100         iComponents[M11] = aM11;
       
   101         iComponents[M12] = aM12;
       
   102     }
       
   103 
       
   104     /**
       
   105      * Constructor
       
   106      * Create a new matrix by coping the given one.
       
   107      * @param aMatrix the matrix to copy
       
   108      * @throws NullPointerException if aMatrix is null
       
   109      * @throws DOMException if index sizes are not same
       
   110      */
       
   111     protected M2GSVGMatrix(M2GSVGMatrix aMatrix)
       
   112     {
       
   113         if (aMatrix == null)
       
   114         {
       
   115             throw new NullPointerException();
       
   116         }
       
   117         iComponents = new float[aMatrix.iComponents.length];
       
   118         for (int index = 0; index < iComponents.length; index++)
       
   119         {
       
   120             iComponents[index] = aMatrix.iComponents[index];
       
   121         }
       
   122     }
       
   123 
       
   124     /**
       
   125      * @see org.w3c.dom.svg.SVGMatrix#getComponent()
       
   126      */
       
   127     public float getComponent(int index) throws DOMException
       
   128     {
       
   129         if ((index < 0) || (index >= iComponents.length))
       
   130         {
       
   131             throw new DOMException(
       
   132                 DOMException.INDEX_SIZE_ERR,
       
   133                 /*SF*/"The index is invalid."/*SF*/);
       
   134         }
       
   135         return iComponents[index];
       
   136     }
       
   137 
       
   138     /**
       
   139      * Return the matrix of components used in this transform. The resulting
       
   140      * values are:
       
   141      * <pre>
       
   142      * [array[0] array[2] array[4]]
       
   143      * [array[1] array[3] array[5]]
       
   144      * </pre>
       
   145      * @return array that contains the matrix components.
       
   146      */
       
   147     float[] getComponents()
       
   148     {
       
   149         return iComponents;
       
   150     }
       
   151 
       
   152     /**
       
   153      * Get the matrix of components used in this transform. The resulting
       
   154      * values are:
       
   155      * <pre>
       
   156      * [aComponents[0] aComponents[2] aComponents[4]]
       
   157      * [aComponents[1] aComponents[3] aComponents[5]]
       
   158      * </pre>
       
   159      * @param aComponents Float array for matrix components
       
   160      */
       
   161     void getComponentsToArray(float[] aComponents)
       
   162     {
       
   163         if (aComponents == null)
       
   164         {
       
   165             return;
       
   166         }
       
   167         for (int index = 0; index < aComponents.length; index++)
       
   168         {
       
   169             aComponents[index] = iComponents[index];
       
   170         }
       
   171     }
       
   172 
       
   173     /**
       
   174      * Return the determinant of this transform matrix. If the determinant is
       
   175      * non-zero, the transform is invertible.
       
   176      * The determinant is calculated as:
       
   177      * <pre>
       
   178      * [m00 m01 m02]
       
   179      * [m10 m11 m12] = m00 * m11 - m01 * m10
       
   180      * [ 0   0   1 ]
       
   181      * </pre>
       
   182      * @return the determinant
       
   183      */
       
   184     public float getDeterminant()
       
   185     {
       
   186         return ((iComponents[M00] * iComponents[M11]) -
       
   187                 (iComponents[M01] * iComponents[M10]));
       
   188     }
       
   189 
       
   190     /**
       
   191     * The inverse is calculated as:
       
   192      * <pre>
       
   193      *     [m00 m01 m02]
       
   194      *  M= [m10 m11 m12]
       
   195      *     [ 0   0   1 ]
       
   196      *
       
   197      *              1                 [ m11/det  -m01/det   (m01*m12-m02*m11)/det]
       
   198      * inverse(M)= --- x adjoint(M) = [-m10/det   m00/det   (m10*m02-m00*m12)/det]
       
   199      *             det                [    0         0               1           ]
       
   200      * </pre>
       
   201      * @see org.w3c.dom.svg.SVGMatrix#inverse()
       
   202      */
       
   203     public SVGMatrix inverse() throws SVGException
       
   204     {
       
   205         // The inversion is useful for undoing transformations.
       
   206         float det = getDeterminant();
       
   207         if (det == 0)
       
   208         {
       
   209             throw new SVGException(
       
   210                 SVGException.SVG_MATRIX_NOT_INVERTABLE,
       
   211                 /*SF*/"The determinant of this matrix is zero."/*SF*/);
       
   212         }
       
   213         return new M2GSVGMatrix(
       
   214                    iComponents[M11] / det, // iMtx[M00]
       
   215                    (-iComponents[M10]) / det, // iMtx[M10]
       
   216                    (-iComponents[M01]) / det, // iMtx[M01]
       
   217                    iComponents[M00] / det, // iMtx[M11]
       
   218                    ((iComponents[M01] * iComponents[M12]) - (iComponents[M02] * iComponents[M11])) / det, // iMtx[M02]
       
   219                    ((iComponents[M10] * iComponents[M02]) - (iComponents[M00] * iComponents[M12])) / det); // iMtx[M12]
       
   220     }
       
   221 
       
   222     /**
       
   223      * The multiply is calculated as:
       
   224      * <pre>
       
   225      *       [a00 a01 a02]   [b00 b01 b02]
       
   226      *  this=[a10 a11 a12] B=[b10 b11 b12]
       
   227      *       [ 0   0   1 ]   [ 0   0   1 ]
       
   228      *
       
   229      *                       [(a00*b00+a01*b10) (a00*b01+a01*b11) (a00*b02+a01*b12+a02)]
       
   230      * [this] = [this]x[B] = [(a10*b00+a11*b10) (a10*b01+a11*b11) (a10*b02+a11*b12+a12)]
       
   231      *                       [       0                   0                     1       ]
       
   232      * </pre>
       
   233      * @see org.w3c.dom.svg.SVGMatrix#mMultiply()
       
   234      */
       
   235     public SVGMatrix mMultiply(SVGMatrix secondMatrix)
       
   236     {
       
   237         if (secondMatrix == null)
       
   238         {
       
   239             throw new NullPointerException();
       
   240         }
       
   241         M2GSVGMatrix b = (M2GSVGMatrix)secondMatrix;
       
   242         float a00 = iComponents[M00]; // a
       
   243         float a10 = iComponents[M10]; // b
       
   244         float a01 = iComponents[M01]; // c
       
   245         float a11 = iComponents[M11]; // d
       
   246         float a02 = iComponents[M02]; // e
       
   247         float a12 = iComponents[M12]; // f
       
   248         iComponents[M00] = (a00 * b.iComponents[M00]) + (a01 * b.iComponents[M10]); // a
       
   249         iComponents[M10] = (a10 * b.iComponents[M00]) + (a11 * b.iComponents[M10]); // b
       
   250         iComponents[M01] = (a00 * b.iComponents[M01]) + (a01 * b.iComponents[M11]); // c
       
   251         iComponents[M11] = (a10 * b.iComponents[M01]) + (a11 * b.iComponents[M11]); // d
       
   252         iComponents[M02] = (a00 * b.iComponents[M02]) + (a01 * b.iComponents[M12]) + a02; // e
       
   253         iComponents[M12] = (a10 * b.iComponents[M02]) + (a11 * b.iComponents[M12]) + a12; // f
       
   254         return this;
       
   255     }
       
   256 
       
   257     /**
       
   258      * The rotation is calculated as:
       
   259      * <pre>
       
   260      *          [ cos(angle) -sin(angle) 0 ]
       
   261      * [this] x [ sin(angle)  cos(angle) 0 ]
       
   262      *          [     0           0      1 ]
       
   263      * </pre>
       
   264      * @see org.w3c.dom.svg.SVGMatrix#mRotate()
       
   265      */
       
   266     public SVGMatrix mRotate(float angle)
       
   267     {
       
   268         if (angle % 360 == 0)
       
   269         {
       
   270             return this;
       
   271         }
       
   272         // must convert degrees to radians since java.lang.Math expects radians
       
   273         angle = angle * (float)Math.PI / 180.0f;
       
   274         float c = (float)Math.cos(angle);
       
   275         float s = (float)Math.sin(angle);
       
   276         float m00 = iComponents[M00];
       
   277         float m10 = iComponents[M10];
       
   278         float m01 = iComponents[M01];
       
   279         float m11 = iComponents[M11];
       
   280         iComponents[M00] = m00 * c + m01 * s;
       
   281         iComponents[M10] = m10 * c + m11 * s;
       
   282         iComponents[M01] = m01 * c - m00 * s;
       
   283         iComponents[M11] = m11 * c - m10 * s;
       
   284         return this;
       
   285     }
       
   286 
       
   287     /**
       
   288      * The multiply is calculated as:
       
   289      * <pre>
       
   290      *       [m00 m01 m02]   [scaleFactor      0      0]
       
   291      *  this=[m10 m11 m12] B=[    0       scaleFactor 0]
       
   292      *       [ 0   0   1 ]   [    0            0      1]
       
   293      *
       
   294      *                       [(a00*scaleFactor) (a01*scaleFactor) a02]
       
   295      * [this] = [this]x[B] = [(a10*scaleFactor) (a11*scaleFactor) a12]
       
   296      *                       [       0                   0         1 ]
       
   297      * </pre>
       
   298      * @see org.w3c.dom.svg.SVGMatrix#mScale()
       
   299      */
       
   300     public SVGMatrix mScale(float scaleFactor)
       
   301     {
       
   302         if (scaleFactor == 1)
       
   303         {
       
   304             return this;
       
   305         }
       
   306         iComponents[M00] *= scaleFactor;
       
   307         iComponents[M01] *= scaleFactor;
       
   308         iComponents[M10] *= scaleFactor;
       
   309         iComponents[M11] *= scaleFactor;
       
   310         return this;
       
   311     }
       
   312 
       
   313     /**
       
   314      * @see org.w3c.dom.svg.SVGMatrix#mTranslate()
       
   315      */
       
   316     public SVGMatrix mTranslate(float x, float y)
       
   317     {
       
   318         if (x == 0 && y == 0)
       
   319         {
       
   320             return this;
       
   321         }
       
   322         iComponents[M02] += (iComponents[M00] * x) + (iComponents[M01] * y);
       
   323         iComponents[M12] += (iComponents[M10] * x) + (iComponents[M11] * y);
       
   324         return this;
       
   325     }
       
   326 
       
   327     /**
       
   328      * To string
       
   329      */
       
   330     static String toString(float aComponents[])
       
   331     {
       
   332         if ((aComponents == null) || (M2GSVGMatrix.ARRAY_SIZE != aComponents.length))
       
   333         {
       
   334             return "";
       
   335         }
       
   336         StringBuffer buf = new StringBuffer();
       
   337         buf.append("[00]:").append(aComponents[M00]).append(", [10]:").append(aComponents[M10]);
       
   338         buf.append(", [01]:").append(aComponents[M01]).append(", [11]:").append(aComponents[M11]);
       
   339         buf.append(", [02]:").append(aComponents[M02]).append(", [12]:").append(aComponents[M12]);
       
   340         return buf.toString();
       
   341     }
       
   342 }