mostly comments, but broke something
This commit is contained in:
parent
fc82bc255c
commit
3f8b9bef06
1 changed files with 366 additions and 127 deletions
|
|
@ -1,31 +1,65 @@
|
|||
import numpy
|
||||
from numpy import sin, cos
|
||||
|
||||
from . import poly_t, plane_t
|
||||
|
||||
def rotate(poly, theta: float, axis: int):
|
||||
|
||||
def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||
'''
|
||||
Rotate the polyhedron around an axis.
|
||||
|
||||
:param poly: Polyhedron to rotate. Changed in-place.
|
||||
:param theta: Angle to rotate by (radians).
|
||||
:param axis: Axis to rotate around (0-2).
|
||||
:return: Rotated polyhedron.
|
||||
Data is altered in-place but also returned for convenience.
|
||||
'''
|
||||
s, c = sin(theta), cos(theta)
|
||||
v0 = poly['vertices'][:, axis - 2].copy()
|
||||
v1 = poly['vertices'][:, axis - 1].copy()
|
||||
poly['vertices'][:, axis - 2] = c * v0 - s * v1
|
||||
poly['vertices'][:, axis - 1] = s * v1 + c * v0
|
||||
return poly
|
||||
|
||||
|
||||
def translate(poly, shift):
|
||||
def translate(poly: poly_t, shift: numpy.ndarray) -> poly_t:
|
||||
'''
|
||||
Translate the polyhedron by a vector.
|
||||
|
||||
:param poly: Polyhedron to translate. Changed in-place.
|
||||
:param shift: Vector to translate by.
|
||||
:return: Translated polyhedron.
|
||||
Data is altered in-place but also returned for convenience.
|
||||
'''
|
||||
poly['vertices'] += shift
|
||||
return poly
|
||||
|
||||
|
||||
def scale(poly, factor):
|
||||
def scale(poly: poly_t, factor: numpy.ndarray or float) -> poly_t:
|
||||
'''
|
||||
Scale the polyhedron.
|
||||
Can scale by a single value, or scale each axis individually
|
||||
|
||||
:param poly: Polyhedron to scale. Changed in-place.
|
||||
:param factor: Scalar or vector to scale by.
|
||||
:return: Scaled polyhedron.
|
||||
Data is altered in-place but also returned for convenience.
|
||||
'''
|
||||
poly['vertices'] *= factor
|
||||
return poly
|
||||
|
||||
|
||||
def shear(poly, factor: float, axb: int, axs: int):
|
||||
poly['vertices'][:, axb] += factor * poly['vertices'][:, axs]
|
||||
def affine(poly: poly_t, xform: numpy.ndarray) -> poly_t:
|
||||
'''
|
||||
Perform arbitrary affine trasnformation on the polyhedron.
|
||||
|
||||
|
||||
def affine(poly, mat):
|
||||
mat4x3 = mat[:, :3]
|
||||
mat4x1 = mat[:, 3]
|
||||
:param poly: Polyhedron to transform. Changed in-place.
|
||||
:param xform: 4x4 affine transformation matrix.
|
||||
:return: Transformed polyhedron.
|
||||
Data is altered in-place but also returned for convenience.
|
||||
'''
|
||||
mat4x3 = xform[:, :3]
|
||||
mat4x1 = xform[:, 3]
|
||||
vh = mat4x3 @ poly['vertices'].T + mat4x1
|
||||
poly['vertices'] = vh[:3, :] / vh[3, :]
|
||||
|
||||
|
||||
poly['vertices'][:] = (vh[:3, :] / vh[3, :]).T
|
||||
return poly
|
||||
|
|
|
|||
Loading…
Reference in a new issue