mostly comments, but broke something
This commit is contained in:
parent
fc82bc255c
commit
3f8b9bef06
1 changed files with 366 additions and 127 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from typing import Dict, Tuple
|
||||
import numpy
|
||||
|
||||
from . import poly_t, plane_t
|
||||
from .util import find_faces
|
||||
|
||||
try:
|
||||
|
|
@ -11,56 +12,88 @@ except NameError:
|
|||
return f
|
||||
|
||||
|
||||
def number_of_moments(order):
|
||||
def number_of_moments(order: int) -> int:
|
||||
'''
|
||||
Returns the number of moments with a given order or below.
|
||||
|
||||
:param order: Maximum polynomial order for the moments.
|
||||
:return: Number of moments with that order or fewer.
|
||||
'''
|
||||
return ((order + 1) * (order + 2) * (order + 3)) // 6
|
||||
|
||||
|
||||
def get_volume(poly):
|
||||
def get_volume(poly: poly_t) -> float:
|
||||
'''
|
||||
Calculates the volume of a polyhedron (0th moment).
|
||||
|
||||
:param poly: Polyhedron to evaluate, in Schlegel representation.
|
||||
:return: Volume of the polyhedron.
|
||||
'''
|
||||
return get_moments(poly, 0)[0]
|
||||
|
||||
|
||||
@profile
|
||||
def get_moments(poly, poly_order):
|
||||
def get_moments(poly: poly_t, order: int) -> numpy.ndarray:
|
||||
'''
|
||||
Calculates the moments of a polyhedron, up to the specified polynomial order.
|
||||
|
||||
:param poly: Polyhedron to evaluate, in Schlegel representation.
|
||||
:param order: Maximum polynomial order for moments.
|
||||
:return: Moments, increasing polynomial order:
|
||||
[1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...]
|
||||
'''
|
||||
vertices = poly['vertices']
|
||||
neighbors = poly['neighbors']
|
||||
num_verts = vertices.shape[0]
|
||||
|
||||
moments = numpy.zeros(number_of_moments(poly_order), dtype=float)
|
||||
moments = numpy.zeros(number_of_moments(order), dtype=float)
|
||||
|
||||
if num_verts == 0:
|
||||
return moments
|
||||
|
||||
'''
|
||||
Decompose polyhedron into triangle fans
|
||||
'''
|
||||
faces = find_faces(neighbors)
|
||||
|
||||
triangle_fans = []
|
||||
max_face_len = max(len(face) for face in faces)
|
||||
triangle_inds = numpy.empty((max_face_len - 2, 3), dtype=int)
|
||||
for face in faces:
|
||||
# Build a triangle fan from each face
|
||||
ilen = len(face) - 2
|
||||
triangle_inds[:ilen, 0] = face[0] # this one broadcasts
|
||||
triangle_inds[:ilen, 0] = face[0] # this assignment is a broadcast
|
||||
triangle_inds[:ilen, 1] = face[1:-1]
|
||||
triangle_inds[:ilen, 2] = face[2:]
|
||||
|
||||
triverts = vertices[triangle_inds[:ilen]]
|
||||
triangle_fans.append(triverts)
|
||||
|
||||
'''
|
||||
Calculate the 0th-order moment
|
||||
'''
|
||||
triangles = numpy.vstack(triangle_fans)
|
||||
volumes = -numpy.linalg.det(triangles)
|
||||
moments[0] = volumes.sum() / 6
|
||||
|
||||
if order == 0:
|
||||
return moments
|
||||
|
||||
'''
|
||||
Iteratively calculate higher-order moments, as per Koehl (2012).
|
||||
'''
|
||||
prevlayer, curlayer = 0, 1
|
||||
S = numpy.zeros((3, poly_order + 1, poly_order + 1, 2), dtype=float)
|
||||
inds = numpy.arange(poly_order + 1)
|
||||
S = numpy.zeros((3, order + 1, order + 1, 2), dtype=float)
|
||||
inds = numpy.arange(order + 1)
|
||||
mx, my = numpy.meshgrid(inds, inds, indexing='ij')
|
||||
valid = numpy.zeros((2, poly_order + 1, poly_order + 1), dtype=bool)
|
||||
cropped = numpy.zeros((poly_order + 1, poly_order + 1), dtype=bool)
|
||||
valid = numpy.zeros((2, order + 1, order + 1), dtype=bool)
|
||||
cropped = numpy.zeros((order + 1, order + 1), dtype=bool)
|
||||
|
||||
for i, vecs in enumerate(triangles):
|
||||
S[:, 0, 0, prevlayer] = 1
|
||||
valid[prevlayer, :, :] = False
|
||||
valid[prevlayer, 0, 0] = True
|
||||
m = 1
|
||||
for c in range(1, poly_order+1):
|
||||
for c in range(1, order + 1):
|
||||
valid[curlayer] = c >= mx + my
|
||||
num_valid = (c + 1) * (c + 2) // 2 # always an int, see number_of_moments()
|
||||
|
||||
|
|
@ -87,7 +120,7 @@ def get_moments(poly, poly_order):
|
|||
m = 1
|
||||
valid[prevlayer, :, :] = False
|
||||
valid[prevlayer, 0, 0] = True
|
||||
for c in range(1, poly_order+1):
|
||||
for c in range(1, order + 1):
|
||||
valid[curlayer] = c >= mx + my
|
||||
num_valid = (c + 1) * (c + 2) // 2 # always an int, see number_of_moments()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue