-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
72 lines (58 loc) · 2 KB
/
vector.py
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
#%%
import numpy as np
import logging
from pyquaternion import Quaternion
# %% Define logging configurations
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(lineno)s:%(message)s')
file_handler = logging.FileHandler('logs/vector.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
#%%
def get_angle(a,b,deg=True):
a = np.array(a)
b = np.array(b)
mag_a = np.sqrt(np.sum(a**2))
mag_b = np.sqrt(np.sum(b**2))
theta = np.arccos(np.dot(a,b)/(mag_a*mag_b))
if deg:
return theta*180/np.pi
else:
return theta
def direction_cosines(vector):
"""
Calculate the direction cosines of a vector.
Parameters:
vector (numpy array): The vector for which direction cosines are to be calculated.
Returns:
cosines (numpy array): An array containing the direction cosines of the vector.
"""
# Normalize the vector to obtain a unit vector
unit_vector = vector / np.linalg.norm(vector)
# Calculate the direction cosines by taking the dot product with each coordinate axis
cosines = np.array([np.dot(unit_vector, axis) for axis in np.eye(len(vector))])
return cosines
def unit(a):
mag_a = np.sqrt(np.sum(a**2))
return a/mag_a
def rotate_vectors(vectors, axis, angle,deg=True):
"""
Rotate a list of vectors around a specified axis by a given angle.
Parameters:
vectors (list of numpy arrays): List of vectors to be rotated.
axis (numpy array): Axis of rotation. Must be a unit vector.
angle (float): Angle of rotation in radians.
Returns:
rotated_vectors (list of numpy arrays): Rotated vectors.
"""
if deg:
angle_r = angle*np.pi/180
else:
angle_r = angle
# Convert axis to unit vector if it's not already
q = Quaternion(axis=axis,angle=angle_r)
vector_prime = []
for i in vectors:
vector_prime.append(q.rotate(i))
return vector_prime