javauis/lcdui_akn/javalcdui/src.nokialcdui/TMIDTriangleFiller.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Triangle filler.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  Include Files
       
    20 #include <e32std.h>
       
    21 #include "TMIDTriangleFiller.h"
       
    22 
       
    23 
       
    24 //  LOCAL CONSTANTS AND MACROS
       
    25 
       
    26 
       
    27 
       
    28 //  LOCAL FUNCTIONS
       
    29 
       
    30 void TMIDTriangleFiller::Construct(TInt aX1, TInt aY1,
       
    31                                    TInt aX2, TInt aY2,
       
    32                                    TInt aX3, TInt aY3)
       
    33 {
       
    34     iX1 = aX1 << KShift;
       
    35     iY1 = aY1 << KShift;
       
    36     iX2 = aX2 << KShift;
       
    37     iY2 = aY2 << KShift;
       
    38     iX3 = aX3 << KShift;
       
    39     iY3 = aY3 << KShift;
       
    40 
       
    41     if (iY1 > iY2)
       
    42     {
       
    43         // Swap P1 and P2
       
    44         SwapPoints(iX1, iY1, iX2, iY2);
       
    45     }
       
    46     if (iY2 > iY3)
       
    47     {
       
    48         // Swap P2 and P3
       
    49         SwapPoints(iX2, iY2, iX3, iY3);
       
    50     }
       
    51     if (iY1 > iY2)
       
    52     {
       
    53         // Swap P1 and P2
       
    54         SwapPoints(iX1, iY1, iX2, iY2);
       
    55     }
       
    56 
       
    57     iNormalCase = EFalse;
       
    58     if (iY1 == iY2)
       
    59     {
       
    60         if (iY2 == iY3)
       
    61         {
       
    62             ConstructSingleLine();
       
    63         }
       
    64         else
       
    65         {
       
    66             ConstructSpecialCaseOne();
       
    67         }
       
    68     }
       
    69     else if (iY2 == iY3)
       
    70     {
       
    71         ConstructSpecialCaseTwo();
       
    72     }
       
    73     else
       
    74     {
       
    75         ConstructNormalCase();
       
    76     }
       
    77     iY1 = iY1 >> KShift;
       
    78 }
       
    79 
       
    80 
       
    81 
       
    82 void TMIDTriangleFiller::SecondHalf()
       
    83 {
       
    84     if (iLeftDelta == iDx13)
       
    85     {
       
    86         iRightDelta = iDx23;
       
    87     }
       
    88     else
       
    89     {
       
    90         iLeftDelta = iDx23;
       
    91     }
       
    92 
       
    93 
       
    94     iScanlines = (iY3 - iY2) >> KShift;
       
    95     iNormalCase = EFalse;
       
    96 }
       
    97 
       
    98 void TMIDTriangleFiller::ConstructNormalCase()
       
    99 {
       
   100     // The normal case:
       
   101     //      1
       
   102     //     /|
       
   103     //    / |    first half
       
   104     //   /  |                 dx12 < dx13
       
   105     // 2/___|____
       
   106     //  \_  |
       
   107     //    \_|    second half
       
   108     //      3
       
   109 
       
   110     // Calculate step values for edges
       
   111     iNormalCase = ETrue;
       
   112     iDx12 = (FPDIV((iX2 - iX1), (iY2 - iY1)));
       
   113     iDx23 = (FPDIV((iX3 - iX2), (iY3 - iY2)));
       
   114     iDx13 = (FPDIV((iX3 - iX1), (iY3 - iY1)));
       
   115 
       
   116 
       
   117 
       
   118     // Check which edge (13 or 12) is the left side edge
       
   119     if (iDx12 < iDx13)
       
   120     {
       
   121         iLeftDelta = iDx12;
       
   122         iRightDelta = iDx13;
       
   123     }
       
   124     else
       
   125     {
       
   126         iLeftDelta = iDx13;
       
   127         iRightDelta = iDx12;
       
   128     }
       
   129 
       
   130 
       
   131 
       
   132     // Initialize the span
       
   133     iSpanStart = iX1;
       
   134     iSpanStop = iX1;
       
   135 
       
   136     // Do the first half of the polygon ( 1-2 )
       
   137     iScanlines = (iY2 - iY1) >> KShift;
       
   138 }
       
   139 
       
   140 void TMIDTriangleFiller::ConstructSingleLine()
       
   141 {
       
   142     iScanlines = 1;
       
   143     iSpanStart = Min(iX1, iX2);
       
   144     iSpanStart = Min(iSpanStart, iX3);
       
   145 
       
   146     iSpanStop = Max(iX1, iX2);
       
   147     iSpanStop = Max(iSpanStop, iX3);
       
   148 }
       
   149 
       
   150 void TMIDTriangleFiller::ConstructSpecialCaseOne()
       
   151 {
       
   152     // Special case:
       
   153     //    1________2
       
   154     //     \      /
       
   155     //      \    /
       
   156     //       \  /
       
   157     //        \/
       
   158     //         3
       
   159     if (iX1 > iX2)
       
   160     {
       
   161         SwapPoints(iX1, iY1, iX2, iY2);
       
   162     }
       
   163 
       
   164     iDx13 = (FPDIV((iX3 - iX1), (iY3 - iY1)));
       
   165     iDx23 = (FPDIV((iX3 - iX2), (iY3 - iY2)));
       
   166 
       
   167     // check which edge is left edge
       
   168     if (iDx13 < iDx23)
       
   169     {
       
   170         iLeftDelta = iDx23;
       
   171         iRightDelta = iDx13;
       
   172     }
       
   173     else
       
   174     {
       
   175         iLeftDelta = iDx13;
       
   176         iRightDelta = iDx23;
       
   177     }
       
   178 
       
   179     // initialize span
       
   180     iSpanStart = iX1;
       
   181     iSpanStop = iX2;
       
   182 
       
   183     iScanlines = (iY3 - iY1) >> KShift;
       
   184 
       
   185 }
       
   186 void TMIDTriangleFiller::ConstructSpecialCaseTwo()
       
   187 {
       
   188     /*
       
   189     // Special case:
       
   190     //     1
       
   191     //    /\
       
   192     //   /  \
       
   193     //  /    \
       
   194     // /      \
       
   195     //2--------3
       
   196     */
       
   197 
       
   198     // calculate slopes
       
   199     iDx12 = (FPDIV((iX2 - iX1), (iY2 - iY1)));
       
   200     iDx13 = (FPDIV((iX3 - iX1), (iY3 - iY1)));
       
   201 
       
   202     // check which edge is left edge
       
   203     if (iDx12 < iDx13)
       
   204     {
       
   205         iLeftDelta = iDx12;
       
   206         iRightDelta = iDx13;
       
   207     }
       
   208     else
       
   209     {
       
   210         iLeftDelta = iDx13;
       
   211         iRightDelta = iDx12;
       
   212     }
       
   213 
       
   214     // Initialize the span
       
   215     iSpanStart = iX1;// + iLeftDelta;
       
   216     iSpanStop = iX1;// + iRightDelta;
       
   217 
       
   218     iScanlines = (iY2 - iY1) >> KShift;
       
   219 }
       
   220 
       
   221 void TMIDTriangleFiller::GetNextPixelRun(TBool& aExists, TInt& aScanLine,
       
   222         TInt& aStart, TInt& aEnd)
       
   223 {
       
   224     aScanLine = iY1++;
       
   225     aStart = (iSpanStart >> KShift) + 1;
       
   226     aEnd = iSpanStop >> KShift;
       
   227 
       
   228     iSpanStart += iLeftDelta;
       
   229     iSpanStop += iRightDelta;
       
   230     aExists = ETrue;
       
   231     if (--iScanlines < 1)
       
   232     {
       
   233         if (iNormalCase)
       
   234         {
       
   235             SecondHalf();
       
   236         }
       
   237         else
       
   238         {
       
   239             aExists = EFalse;
       
   240         }
       
   241     }
       
   242 }
       
   243 //  End of File
       
   244