/// <summary>
        /// Returns the largest subset of the template shape whose squares are all connected to each other.
        /// </summary>
        /// <param name="template"></param>
        /// <param name="isReserved">defines the maze's reserved areas</param>
        /// <returns></returns>
        public static OutlineShape ConnectedSubset(OutlineShape template, InsideShapeDelegate isReserved)
        {
            ExplicitOutlineShape result = new ExplicitOutlineShape(template, isReserved);

            #region Scan the shape for connected areas.

            byte subsetId        = 1;
            int  largestAreaSize = 0;
            byte largestAreaId   = 0;

            for (int x = 0; x < result.XSize; x++)
            {
                for (int y = 0; y < result.YSize; y++)
                {
                    if (result.squares[x, y] == 1 && subsetId < byte.MaxValue)
                    {
                        int areaSize = result.FillSubset(x, y, ++subsetId);
                        if (areaSize > largestAreaSize)
                        {
                            largestAreaSize = areaSize;
                            largestAreaId   = subsetId;
                        }
                    }
                }
            }

            #endregion

            #region Leave only the largest subset, eliminate all others.

            for (int x = 0; x < result.XSize; x++)
            {
                for (int y = 0; y < result.YSize; y++)
                {
                    result.SetValue(x, y, (result.squares[x, y] == largestAreaId));
                }
            }

            #endregion

            return(result);
        }
        /// <summary>
        /// Returns the template shape, augmented by all totally enclosed areas.
        /// </summary>
        /// <param name="template"></param>
        /// <param name="isReserved">defines the maze's reserved areas</param>
        /// <returns></returns>
        public static OutlineShape Closure(OutlineShape template, InsideShapeDelegate isReserved)
        {
            ExplicitOutlineShape result = new ExplicitOutlineShape(template.Inverse());

            #region Scan and mark the reserved areas.

            if (isReserved != null)
            {
                byte reservedId = 3;

                for (int x = 0; x < result.XSize; x++)
                {
                    for (int y = 0; y < result.YSize; y++)
                    {
                        if (isReserved(x, y))
                        {
                            result.squares[x, y] = reservedId;
                        }
                    }
                }
            }

            #endregion

            #region Scan all outside areas.

            byte outsideId = 2;
            int  x0 = 0, x1 = result.XSize - 1, y0 = 0, y1 = result.YSize - 1;

            for (int x = 0; x < result.XSize; x++)
            {
                if (result.squares[x, y0] == 1)
                {
                    result.FillSubset(x, y0, outsideId);
                }
                if (result.squares[x, y1] == 1)
                {
                    result.FillSubset(x, y1, outsideId);
                }
            }
            for (int y = 0; y < result.YSize; y++)
            {
                if (result.squares[x0, y] == 1)
                {
                    result.FillSubset(x0, y, outsideId);
                }
                if (result.squares[x1, y] == 1)
                {
                    result.FillSubset(x1, y, outsideId);
                }
            }

            #endregion

            #region Add the areas which were not reached.

            for (int x = 0; x < result.XSize; x++)
            {
                for (int y = 0; y < result.YSize; y++)
                {
                    // 0: square is part of the template (not part of its inverse)
                    // 1: square is not part of the template, but was not reached
                    result.SetValue(x, y, (result.squares[x, y] <= 1));
                }
            }

            #endregion

            return(result);
        }