943,513 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 920
  • Python RSS
May 2nd, 2009
0

Spherical to Cartesian function

Expand Post »
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.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
May 3rd, 2009
0

Re: Spherical to Cartesian function

I don't know of one. This code requires a radial coordinate.
Python Syntax (Toggle Plain Text)
  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)
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
May 3rd, 2009
0

Re: Spherical to Cartesian function

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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
May 3rd, 2009
0

Re: Spherical to Cartesian function

You are welcome, daviddoria. I added this routine to the geometry library I have been developing for about three years.

-BV
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
May 3rd, 2009
0

Re: Spherical to Cartesian function

Cool - is it available for download?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
May 3rd, 2009
0

Re: Spherical to Cartesian function

Click to Expand / Collapse  Quote originally posted by daviddoria ...
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.
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: passing variables to sqlite
Next Thread in Python Forum Timeline: Understanding functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC