uiacceltk/hitchcock/coretoolkit/src/HuiMatrixStack.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:   Implementation of CHuiMatrixStack. CHuiMatrixStack is a stack 
       
    15 *                of 2D transformation matrices.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include "HuiMatrixStack.h"
       
    22 #include "uiacceltk/HuiPanic.h"
       
    23 
       
    24 
       
    25 EXPORT_C CHuiMatrixStack::TMatrix::TMatrix()
       
    26         : iIsIdentity(ETrue)
       
    27     {
       
    28     // First column.
       
    29     iMatrix[0] = 1;
       
    30     iMatrix[1] = 0;
       
    31     iMatrix[2] = 0;
       
    32 
       
    33     // Second column.
       
    34     iMatrix[3] = 0;
       
    35     iMatrix[4] = 1;
       
    36     iMatrix[5] = 0;
       
    37 
       
    38     // Third column.
       
    39     iMatrix[6] = 0;
       
    40     iMatrix[7] = 0;
       
    41     iMatrix[8] = 1;
       
    42     }
       
    43 
       
    44 #define HUI_MATRIX_INDEX(aRow, aColumn) (aColumn*3 + aRow)
       
    45 
       
    46 void CHuiMatrixStack::TMatrix::Multiply(const TMatrix& aOther)
       
    47     {
       
    48     TMatrix result;
       
    49 
       
    50     // Clear the result.
       
    51     Mem::FillZ(result.iMatrix, sizeof(result.iMatrix));
       
    52 
       
    53     // It could still be an identity matrix, but probably not.
       
    54     result.iIsIdentity = EFalse;
       
    55 
       
    56     for(TInt i = 0; i < 3; ++i) // Row.
       
    57         {
       
    58         for(TInt j = 0; j < 3; ++j) // Column.
       
    59             {
       
    60             for(TInt k = 0; k < 3; ++k)
       
    61                 {
       
    62                 result.iMatrix[ HUI_MATRIX_INDEX(i, j) ] +=
       
    63                     iMatrix[ HUI_MATRIX_INDEX(i, k) ] * aOther.iMatrix[ HUI_MATRIX_INDEX(k, j) ];
       
    64 
       
    65                 }
       
    66             }
       
    67         }
       
    68 
       
    69     *this = result;
       
    70     }
       
    71 
       
    72 
       
    73 EXPORT_C void CHuiMatrixStack::TMatrix::Multiply(THuiRealPoint& aPoint) const
       
    74     {
       
    75     if(iIsIdentity)
       
    76         {
       
    77         // No need to do anything.
       
    78         return;
       
    79         }
       
    80 
       
    81     TReal32 wPoint[3] =
       
    82         {
       
    83         aPoint.iX,
       
    84         aPoint.iY,
       
    85         1.0f
       
    86         };
       
    87     TReal32 result[3] =
       
    88         {
       
    89         0, 0, 0
       
    90         };
       
    91 
       
    92     for(TInt i = 0; i < 3; ++i)
       
    93         {
       
    94         for(TInt j = 0; j < 3; ++j)
       
    95             {
       
    96             result[i] += wPoint[j] * iMatrix[ HUI_MATRIX_INDEX(i, j) ];
       
    97             }
       
    98         }
       
    99 
       
   100     if(result[2] != 0)
       
   101         {
       
   102         // Normalize.
       
   103         result[0] /= result[2];
       
   104         result[1] /= result[2];
       
   105         }
       
   106     else
       
   107         {
       
   108         // Completely zero.
       
   109         result[0] = 0;
       
   110         result[1] = 0;
       
   111         }
       
   112 
       
   113     aPoint.iX = result[0];
       
   114     aPoint.iY = result[1];
       
   115     }
       
   116 
       
   117 
       
   118 EXPORT_C CHuiMatrixStack* CHuiMatrixStack::NewL()
       
   119     {
       
   120     CHuiMatrixStack* self = CHuiMatrixStack::NewLC();
       
   121     CleanupStack::Pop(self);
       
   122     return self;
       
   123     }
       
   124 
       
   125     
       
   126 CHuiMatrixStack* CHuiMatrixStack::NewLC()
       
   127     {
       
   128     CHuiMatrixStack* self = new (ELeave) CHuiMatrixStack();
       
   129     CleanupStack::PushL(self);
       
   130     self->ConstructL();
       
   131     return self;
       
   132     }
       
   133     
       
   134     
       
   135 CHuiMatrixStack::~CHuiMatrixStack()
       
   136     {
       
   137     iStack.Close();    
       
   138     }
       
   139     
       
   140 
       
   141 CHuiMatrixStack::CHuiMatrixStack()
       
   142     {
       
   143     }
       
   144     
       
   145     
       
   146 void CHuiMatrixStack::ConstructL()
       
   147     {
       
   148     // Append one identity matrix as the default contents of the stack.
       
   149     User::LeaveIfError( iStack.Append(TMatrix()) );
       
   150     }
       
   151 
       
   152 
       
   153 EXPORT_C TInt CHuiMatrixStack::Count() const
       
   154     {
       
   155     return iStack.Count();
       
   156     }
       
   157 
       
   158 
       
   159 EXPORT_C CHuiMatrixStack::TMatrix& CHuiMatrixStack::Current()
       
   160     {
       
   161     // There is always at least one matrix in the stack.
       
   162     return iStack[iStack.Count() - 1];
       
   163     }
       
   164 
       
   165 
       
   166 EXPORT_C TInt CHuiMatrixStack::Push()
       
   167     {
       
   168     // Make a copy of the topmost item.
       
   169     return iStack.Append(Current());
       
   170     }
       
   171 
       
   172 
       
   173 EXPORT_C void CHuiMatrixStack::Pop()
       
   174     {
       
   175     if(iStack.Count() <= 1)
       
   176         {
       
   177         // Invalid operation.
       
   178         THuiPanic::Panic(THuiPanic::EMatrixStackPopFromEmpty);
       
   179         }
       
   180 
       
   181     iStack.Remove(iStack.Count() - 1);
       
   182     }
       
   183       
       
   184 
       
   185 EXPORT_C void CHuiMatrixStack::LoadIdentity()
       
   186     {
       
   187     Current() = TMatrix();
       
   188     }    
       
   189 
       
   190 
       
   191 EXPORT_C void CHuiMatrixStack::Multiply(const TMatrix& aMatrix)
       
   192     {
       
   193     Current().Multiply(aMatrix);
       
   194     }