//STEP 5-4: 
//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 $radius = getRadius($mainObj, 0, $temp) - 0.5;
    polySphere -r $radius -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: Cover rest faces with either half-spheres or pyramids.
global proc ssph_coverRestFaces(string $obj, string $sub_obj3, int $isSphere){
    string $mash3[];
    $arrayFace = `polyEvaluate -f $obj`;
    //Calling getFaceCenter method for each face
    int $fId;
    for($fId=0; $fId<$arrayFace[0]; $fId++){
        vector $temp = getFaceCenter($obj, $fId);
        float $radius = getRadius($obj, $fId, $temp);
        
        string $miniSphNm = $obj + "_mini_" + $fId;
        string $name3[];
        if($isSphere){ 
            $name3 = `polySphere -r $radius -sx 16 -sy 16 -ax 0 1 0 -tx 1 -ch 1 -n $miniSphNm`;
            string $cmd1 = $miniSphNm + ".f[0:111]";
            string $cmd2 = $miniSphNm + ".f[224:239]";
            select -r $cmd1 $cmd2;
            delete;
        }else{
            $name3 = `polyPyramid -w ($radius*2) -ns 4 -sh 1 -sc 13 -ax 0 1 0 -cuv 3 -ch 1 -n $miniSphNm`;
        }
        
        $mash3[$fId] = $name3[0];
        
        select $miniSphNm;
        float $a[] = aimY($temp);
        move ($temp.x) ($temp.y) ($temp.z);
        rotate -r $a[0] 0 0;
        rotate -r 0     0 $a[1];
        
        if($fId == ($arrayFace[0]-1)){
            string $cmd0 = $obj + ".vtx[80]";
            vector $vec = `pointPosition -w $cmd0`;
            string $lastOne = $obj + "_mini_" + $fId + "_0";
            string $name4[];
            if($isSphere){ 
                $name4 = `polySphere -r $radius -sx 16 -sy 16 -ax 0 1 0 -tx 1 -ch 1 -n $lastOne`;
                string $cmd1 = $lastOne + ".f[0:111]";
                string $cmd2 = $lastOne + ".f[224:239]";
                select -r $cmd1 $cmd2;
                delete;
            }else{
                $name4 = `polyPyramid -w ($radius*2) -ns 4 -sh 1 -sc 13 -ax 0 1 0 -cuv 3 -ch 1 -n $lastOne`;
            }
            $mash3[$fId+1] = $name4[0];
            
            select $lastOne;
            rotate 180 0 0; 
            move ($vec.x) ($vec.y) ($vec.z);
        }
    }
    
    select -r $mash3[0];
    for($fId=1; $fId<=$arrayFace[0]; $fId++){
        select -tgl $mash3[$fId];    
    }
    group -n $sub_obj3;
  
    select $sub_obj3;
    string $vec = $obj + ".vtx[50]";
    vector $vector = `pointPosition -w $vec`;
    xform -pivots 0  ($vector.y)  0  $sub_obj3;
    rotate 180 0 0;    
}
  
//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 radius value by calulating the distance between center face end vertex on the edge
global proc float getRadius(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[1], $results[2]>>;
    vector $result = $center - $temp;
    return mag($result);
}