from..legacy.MathsimportSphereas_Spheredef_fix_sphere():defradius(obj):from..unitsimportangstromreturnobj.__old__radius()*angstrom_Sphere.__old__radius=_Sphere.radius_Sphere.radius=radiusdefvolume(obj):from..unitsimportangstrom3returnobj.__old__volume()*angstrom3_Sphere.__old__volume=_Sphere.volume_Sphere.volume=volumedefsurface_area(obj):from..unitsimportangstrom2returnobj.__old__surface_area()*angstrom2try:_Sphere.__old__surface_area=_Sphere.surfaceAreadelattr(_Sphere,"surfaceArea")exceptAttributeError:_Sphere.__old__surface_area=_Sphere.surface_area_Sphere.surface_area=surface_areadeftranslate(obj,delta):from._vectorimportVectorreturnobj.__old__translate(Vector(delta))_Sphere.__old__translate=_Sphere.translate_Sphere.translate=translatedefset_radius(obj,radius):from..unitsimportangstrom# get the default unit of lengthlength_unit=angstrom.get_default()def_is_number(v):returnisinstance(v,int)orisinstance(v,float)if_is_number(radius):radius=radius*length_unitreturnobj.__old__set_radius(radius.to(angstrom))try:_Sphere.__old__set_radius=_Sphere.setRadiusdelattr(_Sphere,"setRadius")exceptAttributeError:_Sphere.__old__set_radius=_Sphere.set_radius_Sphere.set_radius=set_radiusdefintersection_volume(obj,other):from..unitsimportangstrom3returnobj.__old__intersection_volume(other)*angstrom3try:_Sphere.__old__intersection_volume=_Sphere.intersectionVolumedelattr(_Sphere,"intersectionVolume")exceptAttributeError:_Sphere.__old__intersection_volume=_Sphere.intersection_volume_Sphere.intersection_volume=intersection_volume@staticmethoddefcombined_volume(spheres):from..unitsimportangstrom3ifnothasattr(spheres,"__len__"):spheres=[spheres]return_Sphere.__old__combined_volume(spheres)*angstrom3try:_Sphere.__old__combined_volume=_Sphere.combinedVolumedelattr(_Sphere,"combinedVolume")exceptAttributeError:_Sphere.__old__combined_volume=_Sphere.combined_volume_Sphere.combined_volume=combined_volume@staticmethoddefcombined_volume_mc(spheres,nsamples=-1):from..unitsimportangstrom3ifnothasattr(spheres,"__len__"):spheres=[spheres]return_Sphere.__old__combined_volume_mc(spheres,nsamples)*angstrom3try:_Sphere.__old__combined_volume_mc=_Sphere.combinedVolumeMCdelattr(_Sphere,"combinedVolumeMC")exceptAttributeError:_Sphere.__old__combined_volume_mc=_Sphere.combined_volume_mc_Sphere.combined_volume_mc=combined_volume_mcdef__str__(obj):returnf"Sphere( center={str(obj.center())} radius={obj.radius()} )"_Sphere.__str__=__str___Sphere.__repr__=__str__
[docs]classSphere(_Sphere):""" A sphere in 3D space """def__init__(self,*args,**kwargs):from..unitsimportangstrom# get the default unit of lengthlength_unit=angstrom.get_default()def_is_number(v):returnisinstance(v,int)orisinstance(v,float)def_is_vector(v):try:returnlen(v)==3exceptException:returnFalse# mix of doubles and lengths?new_args=[]foriinrange(0,len(args)):if_is_number(args[i]):new_args.append((args[i]*length_unit).to(angstrom))elif_is_vector(args[i]):from._vectorimportVectornew_args.append(Vector(args[i]))elifhasattr(args[i],"to"):new_args.append(args[i].to(angstrom))else:new_args.append(args[i])forkeyinkwargs.keys():if_is_number(kwargs[key]):kwargs[key]=(kwargs[key]*length_unit).to(angstrom)elif_is_vector(kwargs[key]):from._vectorimportVectorkwargs[key]=Vector(args[i])elifhasattr(kwargs[key],"to"):kwargs[key]=kwargs[key].to(angstrom)super().__init__(*new_args,**kwargs)