2016-11-25 14:03:03 +00:00
|
|
|
import math
|
2016-11-25 13:28:39 +00:00
|
|
|
|
|
|
|
class Vector:
|
2017-01-18 13:42:51 +00:00
|
|
|
""" 3D Vector
|
|
|
|
|
|
|
|
Simple class that represents a 3D vector
|
|
|
|
"""
|
|
|
|
|
2016-11-25 13:28:39 +00:00
|
|
|
def __init__(self, x = 0.0, y = 0.0, z = 0.0):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Creates a vector from it's coordinates
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.z = z
|
|
|
|
|
|
|
|
def from_array(self, arr):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Creates a vector from an array
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
self.x = float(arr[0]) if len(arr) > 0 else None
|
|
|
|
self.y = float(arr[1]) if len(arr) > 1 else None
|
|
|
|
self.z = float(arr[2]) if len(arr) > 2 else None
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __add__(self, other):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Sums two vectors
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
|
|
|
|
|
|
|
|
def __mul__(self, other):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Computes the product between a vector and a number
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return Vector(self.x * other, self.y * other, self.z * other)
|
|
|
|
|
|
|
|
def __rmul__(self, other):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Computes the product between a vector and a number
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return self.__mul__(other)
|
|
|
|
|
|
|
|
def norm2(self):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Computes the square of the norm of a vector
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return self.x * self.x + self.y * self.y + self.z * self.z
|
|
|
|
|
|
|
|
def norm(self):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Compute the norm of a vector
|
|
|
|
"""
|
2016-11-25 14:03:03 +00:00
|
|
|
return math.sqrt(self.norm2())
|
2016-11-25 13:28:39 +00:00
|
|
|
|
|
|
|
def normalize(self):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Divides each coordinate of the vector by its norm
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
norm = self.norm()
|
|
|
|
if abs(norm) > 0.0001:
|
|
|
|
self.x /= norm
|
|
|
|
self.y /= norm
|
|
|
|
self.z /= norm
|
|
|
|
|
|
|
|
def cross_product(v1, v2):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Computes the cross product between the two vectors
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return Vector(
|
|
|
|
v1.y * v2.z - v1.z * v2.y,
|
|
|
|
v1.z * v2.x - v1.x * v2.z,
|
|
|
|
v1.x * v2.y - v1.y * v2.x)
|
|
|
|
|
|
|
|
def from_points(v1, v2):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Creates a vector from two points
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return Vector(
|
|
|
|
v2.x - v1.x,
|
|
|
|
v2.y - v1.y,
|
|
|
|
v2.z - v1.z)
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Prints the coordinates of the vector between partheses
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return '(' + ", ".join([str(self.x), str(self.y), str(self.z)]) + ")"
|
|
|
|
|
|
|
|
def dot(self, other):
|
2017-01-18 13:42:51 +00:00
|
|
|
"""
|
|
|
|
Computes the dot product of two vectors
|
|
|
|
"""
|
2016-11-25 13:28:39 +00:00
|
|
|
return self.x * other.x + self.y * other.y + self.z * other.z
|
|
|
|
|
|
|
|
|