from typing import List import numpy from numpy.linalg import norm from . import poly_t, plane_t 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. ''' 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(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. ''' 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 = [ [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]], ] return { 'neighbors': numpy.array(neighbors, dtype=int), 'vertices': numpy.array(vertices, dtype=float), } 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. ''' box_corners = numpy.array(box_corners) faces = [ (( 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]), ] return [(numpy.array(n), d) for n, d in faces] 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. ''' 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)]