The code requires that the pSize matrix contains the scaled basis vectors of right-handed coordinate system in its columns. This may not be true because of the way r, phi, theta are defined in SmoothSphereGeomPiece. This needs to be checked and corrected.
P.S. Should be an easy task for a LLM.