2018-03-28 14:58:38 -07:00
|
|
|
from typing import List
|
2018-03-20 15:10:45 -07:00
|
|
|
import numpy
|
|
|
|
|
from numpy.linalg import norm
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
from . import poly_t, plane_t
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
|
|
|
|
|
def build_tetrahedron(vertices: numpy.ndarray) -> poly_t:
|
|
|
|
|
'''
|
|
|
|
|
Build a Schelegel-representation tetrahedron.
|
|
|
|
|
|
|
|
|
|
:param vertices: 4x3 array, representing the vertex coordinates.
|
|
|
|
|
:return: Tetrahedron in Schelgel representation.
|
|
|
|
|
'''
|
2018-03-20 15:10:45 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
def build_box(box_corners: numpy.ndarray) -> poly_t:
|
|
|
|
|
'''
|
|
|
|
|
Build a Schelegel-representation box.
|
|
|
|
|
|
|
|
|
|
:param box_corners: 2x3 array, representing the vertex coordinates of
|
|
|
|
|
two diagonally-opposite corners of the box.
|
|
|
|
|
:return: Box in Schelgel representation.
|
|
|
|
|
'''
|
2018-03-20 15:10:45 -07:00
|
|
|
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 = [
|
2018-03-28 14:58:38 -07:00
|
|
|
[box_corners[0, 0], box_corners[0, 1], box_corners[0, 2]],
|
|
|
|
|
[box_corners[1, 0], box_corners[0, 1], box_corners[0, 2]],
|
|
|
|
|
[box_corners[1, 0], box_corners[1, 1], box_corners[0, 2]],
|
|
|
|
|
[box_corners[0, 0], box_corners[1, 1], box_corners[0, 2]],
|
|
|
|
|
[box_corners[0, 0], box_corners[0, 1], box_corners[1, 2]],
|
|
|
|
|
[box_corners[1, 0], box_corners[0, 1], box_corners[1, 2]],
|
|
|
|
|
[box_corners[1, 0], box_corners[1, 1], box_corners[1, 2]],
|
|
|
|
|
[box_corners[0, 0], box_corners[1, 1], box_corners[1, 2]],
|
2018-03-20 15:10:45 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'neighbors': numpy.array(neighbors, dtype=int),
|
|
|
|
|
'vertices': numpy.array(vertices, dtype=float),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
def planes_from_box(box_corners: numpy.ndarray) -> List[plane_t]:
|
|
|
|
|
'''
|
|
|
|
|
Construct a list of planes corresponding to the faces of a box.
|
|
|
|
|
|
|
|
|
|
:param box_corners: 2x3 array, representing the vertex coordinates of
|
|
|
|
|
two diagonally-opposite corners of the box.
|
|
|
|
|
:return: List of planes, facing inwards.
|
|
|
|
|
'''
|
2018-04-05 15:09:36 -07:00
|
|
|
box_corners = numpy.array(box_corners)
|
2018-03-20 15:10:45 -07:00
|
|
|
faces = [
|
2018-03-28 14:58:38 -07:00
|
|
|
(( 0, 0, 1), -box_corners[0, 2]),
|
|
|
|
|
(( 0, 0, -1), box_corners[1, 2]),
|
|
|
|
|
(( 0, 1, 0), -box_corners[0, 1]),
|
|
|
|
|
(( 0, -1, 0), box_corners[1, 1]),
|
|
|
|
|
(( 1, 0, 0), -box_corners[0, 0]),
|
|
|
|
|
((-1, 0, 0), box_corners[1, 0]),
|
2018-03-20 15:10:45 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return [(numpy.array(n), d) for n, d in faces]
|
|
|
|
|
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
def planes_from_tetrahedron(vertices: numpy.ndarray) -> List[plane_t]:
|
|
|
|
|
'''
|
|
|
|
|
Construct a list of planes corresponding to the faces of a tetrahedron.
|
|
|
|
|
|
|
|
|
|
:param vertices: 4x3 array, representing the vertex coordinates.
|
|
|
|
|
:return: List of planes, facing inwards.
|
|
|
|
|
'''
|
2018-03-20 15:10:45 -07:00
|
|
|
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)]
|