/**
  * Populate the children of "candidate" by expanding the given number of
  * levels from the given cell. Returns the number of children that were marked
  * "terminal".
  */
 private function expandChildren(Candidate $candidate, S2Cell $cell, $numLevels)
 {
     $numLevels--;
     $childCells = array();
     for ($i = 0; $i < 4; ++$i) {
         $childCells[$i] = new S2Cell();
     }
     $cell->subdivide($childCells);
     $numTerminals = 0;
     for ($i = 0; $i < 4; ++$i) {
         if ($numLevels > 0) {
             if ($this->region->mayIntersect($childCells[$i])) {
                 $numTerminals += $this->expandChildren($candidate, $childCells[$i], $numLevels);
             }
             continue;
         }
         $child = $this->newCandidate($childCells[$i]);
         //        echo "child for " . $childCells[$i] . " is " . $child . "\n";
         if ($child != null) {
             $candidate->children[$candidate->numChildren++] = $child;
             if ($child->isTerminal) {
                 ++$numTerminals;
             }
         }
     }
     return $numTerminals;
 }