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

Source Code for Module lib.geometry.vectors

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2014 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  """Collection of functions for vector operations.""" 
 24   
 25  # Python module imports. 
 26  from math import acos, cos, pi, sin 
 27  from numpy import array, cross, dot, float64 
 28  from numpy.linalg import norm 
 29  from random import uniform 
 30   
 31   
32 -def random_unit_vector(vector):
33 """Generate a random rotation axis. 34 35 Uniform point sampling on a unit sphere is used to generate a random axis orientation. 36 37 @param vector: The 3D rotation axis. 38 @type vector: numpy 3D, rank-1 array 39 """ 40 41 # Random azimuthal angle. 42 u = uniform(0, 1) 43 theta = 2*pi*u 44 45 # Random polar angle. 46 v = uniform(0, 1) 47 phi = acos(2.0*v - 1) 48 49 # Random unit vector. 50 vector[0] = cos(theta) * sin(phi) 51 vector[1] = sin(theta) * sin(phi) 52 vector[2] = cos(phi)
53 54
55 -def unit_vector_from_2point(point1, point2):
56 """Generate the unit vector connecting point 1 to point 2. 57 58 @param point1: The first point. 59 @type point1: list of float or numpy array 60 @param point2: The second point. 61 @type point2: list of float or numpy array 62 @return: The unit vector. 63 @rtype: numpy float64 array 64 """ 65 66 # Convert to numpy data structures. 67 point1 = array(point1, float64) 68 point2 = array(point2, float64) 69 70 # The vector. 71 vect = point2 - point1 72 73 # Return the unit vector. 74 return vect / norm(vect)
75 76
77 -def vector_angle(vector1, vector2, normal):
78 """Calculate the directional angle between two N-dimensional vectors. 79 80 @param vector1: The first vector. 81 @type vector1: numpy rank-1 array 82 @param vector2: The second vector. 83 @type vector2: numpy rank-1 array 84 @param normal: The vector defining the plane, to determine the sign. 85 @type normal: numpy rank-1 array 86 @return: The angle between -pi and pi. 87 @rtype: float 88 """ 89 90 # Normalise the vectors (without changing the original vectors). 91 vector1 = vector1 / norm(vector1) 92 vector2 = vector2 / norm(vector2) 93 94 # The cross product. 95 cp = cross(vector1, vector2) 96 97 # The angle. 98 angle = acos(dot(vector1, vector2)) 99 if dot(cp, normal) < 0.0: 100 angle = -angle 101 102 # Return the signed angle. 103 return angle
104