Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
If the lines are parallel, the return value is [None, None].
Examples
--------
>>>
"""
a, b = l1
c, d = l2
ab = subtract_vectors(b, a)
cd = subtract_vectors(d, c)
n = cross_vectors(ab, cd)
n1 = normalize_vector(cross_vectors(ab, n))
n2 = normalize_vector(cross_vectors(cd, n))
plane_1 = (a, n1)
plane_2 = (c, n2)
i1 = intersection_line_plane(l1, plane_2, tol=tol)
i2 = intersection_line_plane(l2, plane_1, tol=tol)
return i1, i2
>>> axis1 = normalize_vector([-0.043, -0.254, 0.617])
>>> angle1 = 0.1
>>> R = matrix_from_axis_and_angle(axis1, angle1)
>>> axis2, angle2 = axis_and_angle_from_matrix(R)
>>> allclose(axis1, axis2)
True
>>> allclose([angle1], [angle2])
True
"""
if not point:
point = [0.0, 0.0, 0.0]
axis = list(axis)
if length_vector(axis):
axis = normalize_vector(axis)
sina = math.sin(angle)
cosa = math.cos(angle)
R = [[cosa, 0.0, 0.0], [0.0, cosa, 0.0], [0.0, 0.0, cosa]]
outer_product = [[axis[i] * axis[j] *
(1.0 - cosa) for i in range(3)] for j in range(3)]
R = [[R[i][j] + outer_product[i][j]
for i in range(3)] for j in range(3)]
axis = scale_vector(axis, sina)
m = [[0.0, -axis[2], axis[1]],
[axis[2], 0.0, -axis[0]],
[-axis[1], axis[0], 0.0]]
Examples
--------
>>> xaxis = [1, 4, 5]
>>> yaxis = [1, 0, -2]
>>> xaxis, yaxis = correct_axes(xaxis, yaxis)
>>> allclose(xaxis, [0.1543, 0.6172, 0.7715], tol=0.001)
True
>>> allclose(yaxis, [0.6929, 0.4891, -0.5298], tol=0.001)
True
"""
xaxis = normalize_vector(xaxis)
yaxis = normalize_vector(yaxis)
zaxis = cross_vectors(xaxis, yaxis)
if not norm_vector(zaxis):
raise ValueError("Xaxis and yaxis cannot span a plane.")
yaxis = cross_vectors(normalize_vector(zaxis), xaxis)
return xaxis, yaxis
def matrix_from_orthogonal_projection(point, normal):
"""Returns an orthogonal projection matrix to project onto a plane \
defined by point and normal.
Args:
point(:obj:`list` of :obj:`float`)
normal(:obj:`list` of :obj:`float`)
Example:
>>> point = [0, 0, 0]
>>> normal = [0, 0, 1]
>>> P = matrix_from_orthogonal_projection(point, normal)
"""
T = identity_matrix(4)
normal = normalize_vector(normal)
for j in range(3):
for i in range(3):
T[i][j] -= normal[i] * normal[j] # outer_product
T[0][3], T[1][3], T[2][3] = scale_vector(
normal, dot_vectors(point, normal))
return T
Parameters
----------
xaxis : :class:`Vector`
The x-axis of the frame.
yaxis : :class:`Vector`
The y-axis of the frame.
Examples
--------
>>> xaxis = [0.68, 0.68, 0.27]
>>> yaxis = [-0.67, 0.73, -0.15]
>>> R = Rotation.from_basis_vectors(xaxis, yaxis)
"""
xaxis = normalize_vector(list(xaxis))
yaxis = normalize_vector(list(yaxis))
zaxis = cross_vectors(xaxis, yaxis)
yaxis = cross_vectors(zaxis, xaxis) # correction
R = cls()
R.matrix[0][0], R.matrix[1][0], R.matrix[2][0] = xaxis
R.matrix[0][1], R.matrix[1][1], R.matrix[2][1] = yaxis
R.matrix[0][2], R.matrix[1][2], R.matrix[2][2] = zaxis
return R
Parameters
----------
xaxis : :class:`Vector`
The x-axis of the frame.
yaxis : :class:`Vector`
The y-axis of the frame.
Examples
--------
>>> xaxis = [0.68, 0.68, 0.27]
>>> yaxis = [-0.67, 0.73, -0.15]
>>> R = Rotation.from_basis_vectors(xaxis, yaxis)
"""
xaxis = normalize_vector(list(xaxis))
yaxis = normalize_vector(list(yaxis))
zaxis = cross_vectors(xaxis, yaxis)
yaxis = cross_vectors(zaxis, xaxis) # correction
R = cls()
R.matrix[0][0], R.matrix[1][0], R.matrix[2][0] = xaxis
R.matrix[0][1], R.matrix[1][1], R.matrix[2][1] = yaxis
R.matrix[0][2], R.matrix[1][2], R.matrix[2][2] = zaxis
return R
def __init__(self, point, normal):
super(Reflection, self).__init__()
normal = normalize_vector((list(normal)))
for i in range(3):
for j in range(3):
self.matrix[i][j] -= 2.0 * normal[i] * normal[j]
for i in range(3):
self.matrix[i][3] = 2 * dot_vectors(point, normal) *\
normal[i]
as list of 3 numbers.
normal (:obj:`list` of :obj:`float`): The normal of the shear plane
as list of 3 numbers.
Raises:
ValueError: If direction and normal are not orthogonal.
Example:
>>> angle = 0.1
>>> direction = [0.1, 0.2, 0.3]
>>> point = [4, 3, 1]
>>> normal = cross_vectors(direction, [1, 0.3, -0.1])
>>> S = matrix_from_shear(angle, direction, point, normal)
"""
normal = normalize_vector(normal)
direction = normalize_vector(direction)
if math.fabs(dot_vectors(normal, direction)) > _EPS:
raise ValueError('Direction and normal vectors are not orthogonal')
angle = math.tan(angle)
M = [[1. if i == j else 0. for i in range(4)] for j in range(4)]
for j in range(3):
for i in range(3):
M[i][j] += angle * direction[i] * normal[j]
M[0][3], M[1][3], M[2][3] = scale_vector(
direction, -angle * dot_vectors(point, normal))
return M
def matrix_from_basis_vectors(xaxis, yaxis):
"""Creates a rotation matrix from basis vectors (= orthonormal vectors).
Args:
xaxis (:obj:`list` oof :obj:`float`): The x-axis of the frame.
yaxis (:obj:`list` oof :obj:`float`): The y-axis of the frame.
Example:
>>> xaxis = [0.68, 0.68, 0.27]
>>> yaxis = [-0.67, 0.73, -0.15]
>>> R = matrix_from_basis_vectors(xaxis, yaxis)
"""
xaxis = normalize_vector(list(xaxis))
yaxis = normalize_vector(list(yaxis))
zaxis = cross_vectors(xaxis, yaxis)
yaxis = cross_vectors(zaxis, xaxis) # correction
R = identity_matrix(4)
R[0][0], R[1][0], R[2][0] = xaxis
R[0][1], R[1][1], R[2][1] = yaxis
R[0][2], R[1][2], R[2][2] = zaxis
return R
(:obj:`list` of :obj:`list` of :obj:`float`): The matrix.
Example:
>>> axis1 = normalize_vector([-0.043, -0.254, 0.617])
>>> angle1 = 0.1
>>> R = matrix_from_axis_and_angle(axis1, angle1)
>>> axis2, angle2 = axis_and_angle_from_matrix(R)
>>> allclose(axis1, axis2)
True
>>> allclose([angle1], [angle2])
True
"""
axis = list(axis)
if length_vector(axis):
axis = normalize_vector(axis)
sina = math.sin(angle)
cosa = math.cos(angle)
R = [[cosa, 0.0, 0.0], [0.0, cosa, 0.0], [0.0, 0.0, cosa]]
outer_product = [[axis[i] * axis[j] *
(1.0 - cosa) for i in range(3)] for j in range(3)]
R = [[R[i][j] + outer_product[i][j]
for i in range(3)] for j in range(3)]
axis = scale_vector(axis, sina)
m = [[0.0, -axis[2], axis[1]],
[axis[2], 0.0, -axis[0]],
[-axis[1], axis[0], 0.0]]