calculateMinimumBoundingRectangle() private method

Given a node object, calculate the node Minimum Bounding Rectangle from it's entries. Used in consistency checking
private calculateMinimumBoundingRectangle ( ) : Rectangle
return Rectangle
Exemplo n.º 1
0
        private bool checkConsistency(NodeBase n, int expectedLevel, Rectangle?expectedMBR)
        {
            // go through the tree, and check that the internal data structures of the tree are not corrupted.

            if (n == null)
            {
                throw new UnexpectedException("Error: Could not read node " + this);
            }

            // if tree is empty, then there should be exactly one node, at level 1
            // TODO: also check the MBR is as for a new node
            if (n == rootNode && Count == 0)
            {
                if (n.level != 1)
                {
                    throw new UnexpectedException("Error: tree is empty but root node is not at level 1");
                }
            }

            if (n.level != expectedLevel)
            {
                throw new UnexpectedException("Error: Node " + this + ", expected level " + expectedLevel + ", actual level " + n.level);
            }

            Rectangle calculatedMBR = n.calculateMinimumBoundingRectangle();
            Rectangle actualMBR     = n.minimumBoundingRectangle;

            if (!actualMBR.Equals(calculatedMBR))
            {
                if (actualMBR.MinX != n.minimumBoundingRectangle.MinX)
                {
                    throw new UnexpectedException("  actualMinX=" + actualMBR.MinX + ", calc=" + calculatedMBR.MinX);
                }
                if (actualMBR.MinY != n.minimumBoundingRectangle.MinY)
                {
                    throw new UnexpectedException("  actualMinY=" + actualMBR.MinY + ", calc=" + calculatedMBR.MinY);
                }
                if (actualMBR.MaxX != n.minimumBoundingRectangle.MaxX)
                {
                    throw new UnexpectedException("  actualMaxX=" + actualMBR.MaxX + ", calc=" + calculatedMBR.MaxX);
                }
                if (actualMBR.MaxY != n.minimumBoundingRectangle.MaxY)
                {
                    throw new UnexpectedException("  actualMaxY=" + actualMBR.MaxY + ", calc=" + calculatedMBR.MaxY);
                }
                throw new UnexpectedException("Error: Node " + this + ", calculated MBR does not equal stored MBR");
            }

            if (expectedMBR != null && !actualMBR.Equals(expectedMBR))
            {
                throw new UnexpectedException("Error: Node " + this + ", expected MBR (from parent) does not equal stored MBR");
            }

            for (int i = 0; i < n.entryCount; i++)
            {
                if (n.level > 1) // if not a leaf
                {
                    NodeInternal nodeInternal = n as NodeInternal;
                    if (nodeInternal.childNodes[i] == null)
                    {
                        throw new UnexpectedException("Error: Node " + this + ", Entry " + i + " is null");
                    }
                    if (!checkConsistency(nodeInternal.childNodes[i], n.level - 1, n.entries[i]))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
    private bool checkConsistency(NodeBase n, int expectedLevel, Rectangle? expectedMBR)
    {
      // go through the tree, and check that the internal data structures of the tree are not corrupted.        

      if (n == null)
        throw new UnexpectedException("Error: Could not read node " + this);

      // if tree is empty, then there should be exactly one node, at level 1
      // TODO: also check the MBR is as for a new node
      if (n == rootNode && Count == 0)
      {
        if (n.level != 1)
          throw new UnexpectedException("Error: tree is empty but root node is not at level 1");
      }

      if (n.level != expectedLevel)
        throw new UnexpectedException("Error: Node " + this + ", expected level " + expectedLevel + ", actual level " + n.level);

      Rectangle calculatedMBR = n.calculateMinimumBoundingRectangle();
      Rectangle actualMBR = n.minimumBoundingRectangle;
      if (!actualMBR.Equals(calculatedMBR))
      {
        if (actualMBR.MinX != n.minimumBoundingRectangle.MinX)
          throw new UnexpectedException("  actualMinX=" + actualMBR.MinX + ", calc=" + calculatedMBR.MinX);
        if (actualMBR.MinY != n.minimumBoundingRectangle.MinY)
          throw new UnexpectedException("  actualMinY=" + actualMBR.MinY + ", calc=" + calculatedMBR.MinY);
        if (actualMBR.MaxX != n.minimumBoundingRectangle.MaxX)
          throw new UnexpectedException("  actualMaxX=" + actualMBR.MaxX + ", calc=" + calculatedMBR.MaxX);
        if (actualMBR.MaxY != n.minimumBoundingRectangle.MaxY)
          throw new UnexpectedException("  actualMaxY=" + actualMBR.MaxY + ", calc=" + calculatedMBR.MaxY);
        throw new UnexpectedException("Error: Node " + this + ", calculated MBR does not equal stored MBR");
      }

      if (expectedMBR != null && !actualMBR.Equals(expectedMBR))
        throw new UnexpectedException("Error: Node " + this + ", expected MBR (from parent) does not equal stored MBR");

      for (int i = 0; i < n.entryCount; i++)
      {
        if (n.level > 1) // if not a leaf
        {
          NodeInternal nodeInternal = n as NodeInternal;
          if (nodeInternal.childNodes[i] == null)
            throw new UnexpectedException("Error: Node " + this + ", Entry " + i + " is null");
          if (!checkConsistency(nodeInternal.childNodes[i], n.level - 1, n.entries[i]))
          {
            return false;
          }
        }
      }
      return true;
    }