82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
import numpy
|
|
from numpy.linalg import norm
|
|
|
|
|
|
def build_tetrahedron(vertices: numpy.ndarray):
|
|
poly = {
|
|
'vertices': numpy.array(vertices, dtype=float),
|
|
'neighbors': numpy.array((
|
|
(1, 3, 2),
|
|
(2, 3, 0),
|
|
(0, 3, 1),
|
|
(1, 2, 0)), dtype=int),
|
|
}
|
|
return poly
|
|
|
|
|
|
def build_box(rbounds):
|
|
neighbors = [
|
|
[1, 4, 3],
|
|
[2, 5, 0],
|
|
[3, 6, 1],
|
|
[0, 7, 2],
|
|
[7, 0, 5],
|
|
[4, 1, 6],
|
|
[5, 2, 7],
|
|
[6, 3, 4],
|
|
]
|
|
|
|
vertices = [
|
|
[rbounds[0, 0], rbounds[0, 1], rbounds[0, 2]],
|
|
[rbounds[1, 0], rbounds[0, 1], rbounds[0, 2]],
|
|
[rbounds[1, 0], rbounds[1, 1], rbounds[0, 2]],
|
|
[rbounds[0, 0], rbounds[1, 1], rbounds[0, 2]],
|
|
[rbounds[0, 0], rbounds[0, 1], rbounds[1, 2]],
|
|
[rbounds[1, 0], rbounds[0, 1], rbounds[1, 2]],
|
|
[rbounds[1, 0], rbounds[1, 1], rbounds[1, 2]],
|
|
[rbounds[0, 0], rbounds[1, 1], rbounds[1, 2]],
|
|
]
|
|
|
|
return {
|
|
'neighbors': numpy.array(neighbors, dtype=int),
|
|
'vertices': numpy.array(vertices, dtype=float),
|
|
}
|
|
|
|
|
|
def planes_from_box(rbounds):
|
|
faces = [
|
|
(( 0, 0, 1), -rbounds[0, 2]),
|
|
(( 0, 0, -1), rbounds[1, 2]),
|
|
(( 0, 1, 0), -rbounds[0, 1]),
|
|
(( 0, -1, 0), rbounds[1, 1]),
|
|
(( 1, 0, 0), -rbounds[0, 0]),
|
|
((-1, 0, 0), rbounds[1, 0]),
|
|
]
|
|
|
|
return [(numpy.array(n), d) for n, d in faces]
|
|
|
|
|
|
def planes_from_tetrahedron(vertices: numpy.ndarray):
|
|
verts = numpy.array(vertices, dtype=float)
|
|
vset_inds = numpy.array((
|
|
[3, 1, 2, 1],
|
|
[2, 0, 3, 2],
|
|
[1, 3, 0, 3],
|
|
[0, 2, 1, 0]))
|
|
vertsets = numpy.moveaxis(numpy.dstack(verts[v] for v in vset_inds), 2, 0)
|
|
ab = vertsets[:, 0, :] - vertsets[:, 1, :]
|
|
cd = vertsets[:, 2, :] - vertsets[:, 3, :]
|
|
|
|
n = numpy.cross(ab, cd)
|
|
n /= norm(n, axis=1)[:, None]
|
|
centers = numpy.sum(vertsets[:, :3, :], axis=1) / 3
|
|
d = -numpy.sum(n * centers, axis=1)
|
|
|
|
return [plane for plane in zip(n, d)]
|
|
|
|
|
|
def volume_of_tetrahedron(vertices):
|
|
ad, bd, cd = vertices[0:3] - vertices[3]
|
|
return -ad @ numpy.cross(bd, cd) / 6
|
|
|
|
|