Package lib :: Package geometry :: Module coord_transform
[hide private]
[frames] | no frames]

Source Code for Module lib.geometry.coord_transform

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2010 Edward d'Auvergne                                        # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Module docstring. 
23  """Module for transforming between different coordinate systems.""" 
24   
25  # Python module imports. 
26  from math import acos, atan2, cos, sin 
27  from numpy import array, float64 
28  from numpy.linalg import norm 
29   
30   
31 -def cartesian_to_spherical(vector):
32 """Convert the Cartesian vector [x, y, z] to spherical coordinates [r, theta, phi]. 33 34 The parameter r is the radial distance, theta is the polar angle, and phi is the azimuth. 35 36 37 @param vector: The Cartesian vector [x, y, z]. 38 @type vector: numpy rank-1, 3D array 39 @return: The spherical coordinate vector [r, theta, phi]. 40 @rtype: numpy rank-1, 3D array 41 """ 42 43 # The radial distance. 44 r = norm(vector) 45 46 # Unit vector. 47 unit = vector / r 48 49 # The polar angle. 50 theta = acos(unit[2]) 51 52 # The azimuth. 53 phi = atan2(unit[1], unit[0]) 54 55 # Return the spherical coordinate vector. 56 return array([r, theta, phi], float64)
57 58
59 -def spherical_to_cartesian(spherical_vect, cart_vect):
60 """Convert the spherical coordinate vector [r, theta, phi] to the Cartesian vector [x, y, z]. 61 62 The parameter r is the radial distance, theta is the polar angle, and phi is the azimuth. 63 64 65 @param spherical_vect: The spherical coordinate vector [r, theta, phi]. 66 @type spherical_vect: 3D array or list 67 @param cart_vect: The Cartesian vector [x, y, z]. 68 @type cart_vect: 3D array or list 69 """ 70 71 # Trig alias. 72 sin_theta = sin(spherical_vect[1]) 73 74 # The vector. 75 cart_vect[0] = spherical_vect[0] * cos(spherical_vect[2]) * sin_theta 76 cart_vect[1] = spherical_vect[0] * sin(spherical_vect[2]) * sin_theta 77 cart_vect[2] = spherical_vect[0] * cos(spherical_vect[1])
78