Ticket #3588: optimize-rasterize.patch

File optimize-rasterize.patch, 2.7 KB (added by mimo, 8 years ago)
  • source/simulation2/helpers/Rasterize.cpp

     
    6565        // Find the min/max range of cells that are strictly inside the square+rasterClearance.
    6666        // (Since the square+rasterClearance is a convex shape, we can just test each
    6767        // corner of each cell is inside the shape.)
    68         // (TODO: This potentially does a lot of redundant work.)
     68        // When looping on i, if the previous cell was inside, no need to check again the left corners.
     69        // and we can stop the loop when exiting the shape.
     70        // Futhermore if one of the right corners of a cell is outside, no need to check the following cell
    6971        i16 spanI0 = std::numeric_limits<i16>::max();
    7072        i16 spanI1 = std::numeric_limits<i16>::min();
     73        bool previousInside = false;
     74        bool skipNextCell = false;
    7175        for (i16 i = i0; i < i1; ++i)
    7276        {
    73             if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*i-shape.x, cellSize*j-shape.z),
    74                                                   shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     77            if (skipNextCell)
     78            {
     79                skipNextCell = false;
    7580                continue;
     81            }
    7682
    7783            if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*(i+1)-shape.x, cellSize*j-shape.z),
    78                                                   shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     84                                    shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     85            {
     86                if (previousInside)
     87                    break;
     88                skipNextCell = true;
    7989                continue;
     90            }
    8091
    81             if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*i-shape.x, cellSize*(j+1)-shape.z),
    82                                                   shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
    83                 continue;
    84 
    8592            if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*(i+1)-shape.x, cellSize*(j+1)-shape.z),
    86                                                   shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     93                                    shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     94            {
     95                if (previousInside)
     96                    break;
     97                skipNextCell = true;
    8798                continue;
     99            }
    88100
    89             spanI0 = std::min(spanI0, i);
    90             spanI1 = std::max(spanI1, (i16)(i+1));
     101            if (!previousInside)
     102            {
     103                if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*i-shape.x, cellSize*j-shape.z),
     104                                    shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     105                    continue;
     106
     107                if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*i-shape.x, cellSize*(j+1)-shape.z),
     108                                    shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     109                    continue;
     110
     111                previousInside = true;
     112                spanI0 = i;
     113            }
     114
     115            spanI1 = i+1;
    91116        }
    92117
    93118        // Add non-empty spans onto the list