//STEP 5-1: 
//Create subsphere
string $sub_figure = $obj + "_sub_figure";
int $isSphere = false;
createSubFigure($obj, $sub_figure, $isSphere);
  
//PROC: Main method to create sub object.
global proc createSubFigure(string $mainObj, string $sub_figure, int $isSphere){
    string $object = $mainObj + "_sub_obj";
    string $sub_obj1 = $mainObj + "_sub_obj1";
    string $sub_obj2 = $mainObj + "_sub_obj2";
    string $sub_obj3 = $mainObj + "_sub_obj3";    
    string $cmdFirst = $object + ".f[112:223]";
    string $cmdSecond = $object + ".f[240:255]";
    
    vector $temp = getFaceCenter($mainObj, 0);
    float $dist = getDistCenterToEdge($mainObj, 0, $temp)-0.2;
    polySphere -r $dist -sx 16 -sy 16 -ax 0 1 0 -tx 1 -ch 1 -n $object;
    select -r $cmdFirst $cmdSecond;
    delete;
  
    ssph_coverVertex($object, $sub_obj1);
    ssph_coverEdge($object, $sub_obj2);
  
    string $cmdThird = $object + ".f[48:111]";
    select -r $cmdThird;
    delete;
    
    ssph_coverRestFaces($object, $sub_obj3, $isSphere);
    
    delete $object;
    select -r $sub_obj1;
    select -tgl $sub_obj2;
    select -tgl $sub_obj3; 
    group -n $sub_figure;    
}
  
//PROC: Return vector information of middle of each face
global proc vector getFaceCenter(string $obj, int $faceId){
    select $obj;
    string $objName = $obj + ".f[" + $faceId +"]";
    //Query vector information for one particular face
    float $results[] = `xform -q -ws -t $objName`;
    int $vertexCount = size($results)/3;
    
    int $i, $j, $h;
    vector $vec[];
    float $newVec[];
    //Average all vector information to get center value.
    for($i=0; $i<3; $i++){
        float $tempNum;
        for($h=0; $h<$vertexCount; $h++){
            $tempNum += $results[$h*3+$i];
        }
        float $val = $tempNum/$vertexCount;
        $newVec[$i] = $val;
    }
    //Return center of the face
    vector $faceCenterPnt = <<$newVec[0], $newVec[1], $newVec[2]>>;
    return $faceCenterPnt;
}
  
//PROC: Return distance value between face center and middle of the edge
global proc float getDistCenterToEdge(string $obj, int $faceId, vector $center){
    select $obj;
    string $objName = $obj + ".f[" + $faceId +"]";
    //Query vector information for one particular face
    float $results[] = `xform -q -ws -t $objName`;
    vector $temp = <<(($results[0]+$results[3])/2), (($results[1]+$results[4])/2), (($results[2]+$results[5])/2)>>;
    vector $result = $center - $temp;
    float $returnValue = mag($result);
    return $returnValue;
}