1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Collection of functions for vector operations."""
24
25
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
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
42 u = uniform(0, 1)
43 theta = 2*pi*u
44
45
46 v = uniform(0, 1)
47 phi = acos(2.0*v - 1)
48
49
50 vector[0] = cos(theta) * sin(phi)
51 vector[1] = sin(theta) * sin(phi)
52 vector[2] = cos(phi)
53
54
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
67 point1 = array(point1, float64)
68 point2 = array(point2, float64)
69
70
71 vect = point2 - point1
72
73
74 return vect / norm(vect)
75
76
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
91 vector1 = vector1 / norm(vector1)
92 vector2 = vector2 / norm(vector2)
93
94
95 cp = cross(vector1, vector2)
96
97
98 angle = acos(dot(vector1, vector2))
99 if dot(cp, normal) < 0.0:
100 angle = -angle
101
102
103 return angle
104