-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector_methods.py
More file actions
125 lines (96 loc) · 2.97 KB
/
vector_methods.py
File metadata and controls
125 lines (96 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Methods to compute properties of vectors.
Author: Raul A. Flores
Development Notes:
TEMP
"""
# | - Import Modules
import os
import numpy as np
# __|
def radians_to_degrees(angle):
"""
"""
# | - radians_to_degrees
angle_degrees = angle * 180 / np.pi
return(angle_degrees)
# __|
def degrees_to_radians(angle):
"""
"""
# | - radians_to_degrees
angle_radians = angle * (180 / np.pi) ** -1
return(angle_radians)
# __|
def unit_vector(vector):
""" Returns the unit vector of the vector. """
# | - unit_vector
return vector / np.linalg.norm(vector)
# __|
def angle_between_vectors(v1, v2, return_radians=True):
""" Returns the angle in radians between vectors 'v1' and 'v2'::
>>> angle_between((1, 0, 0), (0, 1, 0))
1.5707963267948966
>>> angle_between((1, 0, 0), (1, 0, 0))
0.0
>>> angle_between((1, 0, 0), (-1, 0, 0))
3.141592653589793
"""
# | - angle_between_vectors
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
angle = np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
if not return_radians:
angle = radians_to_degrees(angle)
return(angle)
# __|
def get_vector_magnitude(v1):
# | - get_vector_magnitude
magnitude = np.linalg.norm(v1)
return(magnitude)
# __|
def get_rotation_matrix(theta, axis):
"""
https://en.wikipedia.org/wiki/Rotation_matrix
"""
# | - get_get_rotation_matrix
rotation_matrix_x = np.array([
[1., 0., 0., ],
[0., np.cos(theta), -np.sin(theta), ],
[0., np.sin(theta), np.cos(theta), ],
])
rotation_matrix_y = np.array([
[ np.cos(theta), 0., np.sin(theta), ],
[ 0., 1., 0., ],
[-np.sin(theta), 0., np.cos(theta), ],
])
rotation_matrix_z = np.array([
[np.cos(theta), -np.sin(theta), 0., ],
[np.sin(theta), np.cos(theta), 0., ],
[0., 0., 1., ],
])
if axis == "x":
rotation_matrix = rotation_matrix_x
if axis == "y":
rotation_matrix = rotation_matrix_y
if axis == "z":
rotation_matrix = rotation_matrix_z
return(rotation_matrix)
# __|
# import numpy as np
import math
def rotation_matrix(axis, theta):
"""
Return the rotation matrix associated with counterclockwise rotation about
the given axis by theta radians.
"""
#| - rotation_matrix
axis = np.asarray(axis)
axis = axis / math.sqrt(np.dot(axis, axis))
a = math.cos(theta / 2.0)
b, c, d = -axis * math.sin(theta / 2.0)
aa, bb, cc, dd = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
[2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
[2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
#__|