bool RegionOfRows::GetIntersection(const RegionOfRows& RegionOI,RegionOfRows& Intersection) const
{
    Intersection.Reset();

    // Regions are defined ?

    if (!IsDefined()) return false;
    if (!RegionOI.IsDefined()) return false;

    // Regions are defined, do the have common rows ?

    const ClosedInterval<unsigned int>* Region1Rows = GetRows();
    const ClosedInterval<unsigned int>* Region2Rows = RegionOI.GetRows();
    ClosedInterval<unsigned int> RowsOI;
    if (!Region1Rows->GetOverlap(*Region2Rows,RowsOI)) return false;		// no rows in common
    unsigned int RowStart = RowsOI.GetStart();
    unsigned int RowEnd = RowsOI.GetEnd();

    const ClosedInterval<unsigned int>* Columns1 = GetColumns();
    const ClosedInterval<unsigned int>* Columns2 = RegionOI.GetColumns();

    // Adjust offets

    Columns1 += RowStart - GetFirstRow();
    Columns2 += RowStart - RegionOI.GetFirstRow();

    for (unsigned int Row = RowStart;Row <= RowEnd;Row++)
    {
        ClosedInterval<unsigned int> ColumnsOI;
        if (Columns1->GetOverlap(*Columns2,ColumnsOI))
        {
            // Rows and columns in common => intersection

            Intersection.Add(Row,ColumnsOI.GetStart(),ColumnsOI.GetEnd());
        } else if (Intersection.IsDefined()) break;			// Convex region !

        Columns1++;
        Columns2++;
    }

    // Any intersection ?

    return Intersection.IsDefined();
}
void RegionOfRows::ShiftRegion(int RowDisplacement,int ColumnDisplacement)
{
    if (!IsDefined()) return;

    if (RowDisplacement != 0)
    {

        // Shift rows

        int StartRow = (int) Rows.GetStart();
        StartRow += RowDisplacement;

        if (StartRow < 0)
        {
            // Can't shift => invalidate !

            Reset();
            return;
        }

        int EndRow = (int) Rows.GetEnd();
        EndRow += RowDisplacement;
        Rows.Set((unsigned int)StartRow,(unsigned int)EndRow);
    }

    if (ColumnDisplacement != 0)
    {
        // Shift Columns

        ClosedInterval<unsigned int>* MyColumns = GetMyColumns();
        unsigned int NumberOfRows = Rows.GetSpan() + 1;

        for (unsigned int i=0; i < NumberOfRows;++i)
        {
            int StartColumn = (int) MyColumns->GetStart();
            StartColumn += ColumnDisplacement;

            if (StartColumn < 0)
            {
                // Can't shift => invalidate !

                Reset();
                return;
            }

            int EndColumn = (int) MyColumns->GetEnd();
            EndColumn += ColumnDisplacement;
            MyColumns->Set((unsigned int)StartColumn,(unsigned int)EndColumn);
            MyColumns++;
        }
    }
}