Spherical to Cartesian function

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Spherical to Cartesian function

 
0
  #1
May 2nd, 2009
Is there a library that has a function that will take (theta,phi) and return (x,y,z)?

Thanks,

Dave
Last edited by daviddoria; May 2nd, 2009 at 6:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Spherical to Cartesian function

 
0
  #2
May 3rd, 2009
I don't know of one. This code requires a radial coordinate.
  1. class Pt(object):
  2. def __init__(self, x=0.0, y=0.0, z=0.0):
  3. self.x = x
  4. self.y = y
  5. self.z = z
  6.  
  7. def __str__(self):
  8. return '(%0.4f, %0.4f, %0.4f)' % (self.x, self.y, self.z)
  9.  
  10. def __repr__(self):
  11. return 'Pt(%f, %f, %f)' % (self.x, self.y, self.z)
  12.  
  13. def __add__(self, other):
  14. return Point(self.x+other.x, self.y+other.y, self.z+other.z)
  15.  
  16. def __sub__(self, other):
  17. return Point(self.x-other.x, self.y-other.y, self.z-other.z)
  18.  
  19. def __mul__(self, f):
  20. return Point(self.x*f, self.y*f, self.z*f)
  21.  
  22. def dist(self, other):
  23. p = self-other
  24. return (p.x**2 + p.y**2 + p.z**2)**0.5
  25.  
  26. def toSpherical(self):
  27. r = mag(self)
  28. theta = atan2(sqrt(self.x**2+self.y**2), self.z)
  29. phi = atan2(self.y, self.x)
  30. return SphericalPt(r, theta, phi)
  31.  
  32. class SphericalPt(object):
  33. def __init__(self, r, theta, phi):
  34. # radial coordinate, zenith angle, azimuth angle
  35. self.r = r
  36. self.theta = theta
  37. self.phi = phi
  38.  
  39. def __str__(self):
  40. return '(%0.4f, %0.4f, %0.4f)' % (self.r, self.theta, self.phi)
  41.  
  42. def __repr__(self):
  43. return 'SphericalPt(%f, %f, %f)' % (self.r, self.theta, self.phi)
  44.  
  45. def toCartesian(self):
  46. x = self.r*cos(self.phi)*sin(self.theta)
  47. y = self.r*sin(self.phi)*sin(self.theta)
  48. z = self.r*cos(self.theta)
  49. return Pt(x,y,z)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Spherical to Cartesian function

 
0
  #3
May 3rd, 2009
Thanks, I haven't used classes in python yet so this is a good example for me.. For future readers, there is a small typo. In the Pt class, the return statements say Point(...) when they should say Pt(...).

It's interesting this isn't in a library anywhere though, it seems like deg2rad, cart2sphere, etc type functions would be pretty handy.

Dave
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Spherical to Cartesian function

 
0
  #4
May 3rd, 2009
You are welcome, daviddoria. I added this routine to the geometry library I have been developing for about three years.

-BV
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Spherical to Cartesian function

 
0
  #5
May 3rd, 2009
Cool - is it available for download?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Spherical to Cartesian function

 
0
  #6
May 3rd, 2009
Originally Posted by daviddoria View Post
Cool - is it available for download?
No. The work I have done interfaces with SDS/2 software. SDS/2 has a built-in interpreter, and we run scripts inside the SDS/2 3D model to automate tasks, extract and manipulate information, etc. Some of the code can be viewed here, but I have not updated it in several months.

What type of geometry work are you interested in? Following are some of my geometry applications:
LineLineIntersect3D (two point pairs)
Plane3D (three points)
three point circle
rotate a point about an arbitrary axis
DistancePointLine3D
LinePlaneIntersect3D
DistancePointPlane3D
PlanePlaneIntersect3D
BasisTransToGlobal - translate a point in a defined orthonormal basis to a point in the standard basis set
BasisTransToLocal - translate a point in the standard basis set to a point in a defined orthonormal basis
CircleCircleIntersect3D

I have no special knowledge nor am I an expert in geometry. Everything I have developed was from research done at various sites on the internet, particularly here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC