mpacts.geometrygenerators. polyhedron

In order to be able to use this module import it like this:

import mpacts.geometrygenerators.polyhedron
#or assign it to a shorter name
import mpacts.geometrygenerators.polyhedron as pol

Connectivity

class mpacts.geometrygenerators.polyhedron.Connectivity(polyhedron)

Bases: object

Caculates the connectivity of a polyhedron, mainly in its members

Members:
-cornerEdges edges coming together in a corner
-cornerTriangles
 triangles coming together in a corner
-edgeCorners corners defining an edge (same as polyhedron.edges)
-edgeTriangles triangles coming together in an edge
-triangleCorners
 corners defining a triangle (same as polyhedron.triangles)
-triangleEdges edges defining a triangle
Connectivity(name, parent, **kwargs)
cylinder_to_nodes(cylinder_data)
isBoundaryCorner(iCorner)

a corner is on the boundary if it belongs to a boundary edge

isBoundaryEdge(iEdge)

an edge is on the boundary if it belongs to only one triangle

isConvexEdge(iEdge)

test whether this edge is convex or not. An edge is convex if the neighbouring triangles form a strictly convex surface (so, if the neighbouring triangles are in the same plane, isConvexEdge returns False.

nCorners()
nEdges()
nTriangles()
triangle_to_nodes(triangle_data)

Polyhedron

class mpacts.geometrygenerators.polyhedron.Polyhedron(defaultRoundingRadius=0.0, tolerance=2.220446049250313e-12, name=None)

Bases: object

Object describing and manipulating triangulated polyhedra.

Geometry elements:
-vertices list of tuples of node positions in 3D
-triangles list of triplets of integers referring to a triangle between three vertices
-edges list of pairs of integers referring to an edge between two vertices (optional)
-corners list of integers referring to a vertex (optional)

-roundingRadius list of floats referring to the radius of a vertex (optional)

Polyhedra are iterable - and iterate by default over the vertices. By modifying the ‘“iterateOver” member, it is also possible, to iterate over edges or triangles...

Polyhedron(name, parent, **kwargs)
aabbox()

compute axis aligned bounding box returns: ([xmin,ymin,zmin],[xmax,ymax,zmax])

addCorner(iVertex)

associate corner with vertex iVertex

addEdge(iVertex0, iVertex1=None)

associate edge with vertices iVertex0 and iVertex1 (if iVertex1 is None iVertex0 is assumed to be a list/tuple of two integers)

addTriangle(iVertex0, iVertex1=None, iVertex2=None, normal=None, tag1=None, tag2=None, safe=True)

associate triangle with vertices iVertex0, iVertex1 and iVertex2 (if iVertex1 is None iVertex0 is assumed to be a list or tuple of three integers) If normal is not None it is assumed that normal is the normal vector of the plane and the order of the vertices may be changed.

addVertex(x, y=None, z=None, roundingRadius=None, scale=1, noDuplicates=True)

add a vertex (if y is None x is assumed to be a list of three floats) a rounding radius and a scale factor can be specified if noDuplicates==True it is checked that the vertex does not already exist. if the vertex exists, the index of the existing vertex is returned and the vertex is NOT added.

alignAlongInertia(density=1.0, center=True)

Calculates the inertia tensor, diagonalizes it, and aligns the closed, homogeneous, and triangulated polyhedron along its major axis of inertia. It returns the eigenvalues of the inertia tensor (equivalent to the diagonals of the new aligned inertia tensor).

alignAlongInertiaAndReturnOrientation(density=1.0, center=True)

Calculates the inertia tensor, diagonalizes it, and aligns the closed, homogeneous, and triangulated polyhedron along its major axis of inertia. It returns the eigenvalues of the inertia tensor (equivalent to the diagonals of the new aligned inertia tensor).

autoAddCornersAndEdges(mode='all')

Construct the list of corners and the list of edges from the corners and the edges in the existing triangles.

  • mode = ‘all’ adds all corners/edges
  • mode = ‘convex’ adds only edges for which the neighbouring triangles form a
    convex shape. Corners are added if they belong to at least one convex edge (this includes saddle points). If the polyhedron represents a surface rather than a solid, there are boundary corners and edges. These boundary corners and edges are are also added.
center()

compute the center of mass of this polyhedron as signed volumes of tetrahedra formed by its triangles and return it make vertex coordinates relative to this center of mass.

cleanLists()

make list of tuples from list of lists of the vertices, corners, triangles, and edges.

crop_to_beam(sides, center, verbose=True)

remove all vertices (and associated edges and triangles) which do not fit in a given rectangular beam with sides (sidex,sidey,sidez) and center (x0,y0.z0) returns (and optionally reports) the number of deleted vertices

displace_to(xyz)

displace the polyhedron object to xyz

edge_length(what='all')

returning the lengths of the edges of the polyhedron. If only max, min, or mean are required, you can specify this in the parameter ‘what’. Parameters:

what lets the user decide, which (simple) statstics of the
edges is required: can be ‘all’ (default), ‘mean’, ‘min’, or ‘max’
findVertex(x)
find_triangle_edges(list_edges)
flip_normals()
hasFixedRoundingRadius()
inertiaTensor(density=1.0, center=True)

Calculates the inertia tensor with respect to the centroid assuming a homogeneous, closed, and triangulated polyhedron.

isValidTriangleId(i)
isValidVertexId(i)
maximum_triangle_diameter()
mayavi(representation='surface', color=None, scalars=None, show=True, scale=None)

imports mayavi.mlab and draws the triangular polyhedron. possible values for representation: ‘surface’, ‘wireframe’, ‘mesh’, ‘fancymesh’, or ‘points’

mayavi2(representation='surface', color=None, scalars=None)
minimal_edge_length()
minimum_triangle_diameter()
nCorners()
nEdges()
nTriangles()
nVertices()
normal(triangle)

return the normal to the triangle (as a numpy array). if triangle is an int it refers to the corresponding entry in self.triangles if triangle is a tuple of 3 ints, the ints refers to entries in self.vertices

normal_area(triangle)

return the normal to the triangle (as a numpy array), and area. if triangle is an int it refers to the corresponding entry in self.triangles if triangle is a tuple of 3 ints, the ints refers to entries in self.vertices

oneParticlePerPrimitive()

create a list of polyhedra with the same primitives, but with a separate particle for each primitive this is practical for analysing forces on particles

optimize_bandwidth()

Uses reverse Cuthill-McKee to optimize the ‘bandwidth’ w.r.t. the edges of the polyhedron.

removeEdge(e)

remove an edge; usage see removeTriangle

removeEdgesWithVertexIndex(idx)

remove all edges which contain a given vertex index. no resorting or re-indexing will be made so this function should be safe for loops over vertices.

removeTriangle(t, strict=False)

remove a triangle removeTriangle([i,j,k]), removeTriangle((i,j,k)): remove triangle ijk

note that ijk and ikj are different triangles (opposite normals) but ijk and jki are the same

removeTriangle(i): remove i-th triangle if strict, the triangle will NOT be rotated, and only the triangle with exactly the given indices in the given order will be removed

removeTriangleDuplicates()
removeTrianglesWithVertexIndex(idx)

remove all triangles which contain a given vertex index. no resorting or re-indexing will be made so this function should be safe for loops over vertices.

removeUnusedVertices()

remove all vertices that are not used in a triangle, edge or corner. Note that this implies a renumbering of the vertices and therefor the contents of triangles/edges/corners can be modified by this function.

removeVertex(v)

remove a all instances of a given vertex, and all associated triangles, corners and edges which will be broken as a result. Warning: reasonably safe but horribly slow for large polyhedra!

removeVertexDuplicates(tolerance=2.220446049250313e-12)

Vertices (a list of 3D coordinates) and triangles (a list of 3-tuples of ints referencing vertices) are assumed to define a triangulated surface. This function removes all copies of vertices and renumbers the vertex numbers in triangles

removeVerticesLargerThan(axis, value)

remove all vertices (and associated edges and triangles) LARGER than a given value on a given axis. Warning: reasonably safe but horribly slow for large polyhedra

removeVerticesSmallerThan(axis, value)

remove all vertices (and associated edges and triangles) SMALLER than a given value on a given axis. Warning: reasonably safe but horribly slow for large polyhedra

rotate(axis, angle_degrees)

rotate all the vertices about the axis (represented as a unit vector) over the angle (degrees)

roundingRadius()

returns the roundingRadius as a single value if fixed, or as a list if variable.

scaleVertices(scale)
scaleVerticesXYZ(scalex, scaley, scalez)
setCenter(center)

set the center of gravity to <center> and recompute the vertex coordinates relative to <center>. Note that this does NOT change the absolute position of the vertices, only of the center of gravity. If one wants to keep the current relative vertex coordinates but change the center (i.e. put this polyhedron at a different position in space), one must use

polyhedron._center = [newX,newY,newZ]

or polyhedron.displace_to([newX,newY,newZ])

setVertex(i, x, y=None, z=None, roundingRadius=None, scale=1)

modify i-th vertex

set_normal(triangle, n)
simplify_mesh()
sort_vertices_along_axis(axis)

changes the order of the indices to be sorted along the given axis (0,1,2 or ‘x’,’y’,’z’) as well as the corresponding triangles’ and edges’ lists.

subdivide(n)
surface_area()

Calculates the surface area as the sum of all triangles’ areas.

triangleToPlane(iTriangle)

compute the plane of a triangle as the (point,normal) tuple

triangle_centers()
volume()

Calculates the volume of a _closed_ polyhedron according to Gauss’ theorem

mpacts.geometrygenerators.polyhedron.barycenter_radius(v0, v1, v2)
mpacts.geometrygenerators.polyhedron.calc_bandwidth(edges)
mpacts.geometrygenerators.polyhedron.correct_triangle_order(x_, triangles)

Defines all triangles in such a way that their normal vectors always point OUTWARDS

Parameters:
-x_ positions of vertices -triangles faces (triangles - 3D numpy arrays pointing to x, dtype=int)
Returns:
triangles
mpacts.geometrygenerators.polyhedron.edgeStatistics(vertices, triangles)

Vertices (a list of 3D coordinates) and triangles (a list of 3-tuples of ints referencing vertices) are assumed to define a triangulated surface. returns minimal, maximal, and average edge length as well as the stddeviation of the average

mpacts.geometrygenerators.polyhedron.get_oriented_triangle_edges(triangles)

Returns a numpy array of the edges of a given list of oriented triangles.

mpacts.geometrygenerators.polyhedron.isParallel(u0, u1, tolerance)
mpacts.geometrygenerators.polyhedron.isSamePlane(p0, n0, p1, n1, tolerance)
mpacts.geometrygenerators.polyhedron.isSmall(value, tolerance)
mpacts.geometrygenerators.polyhedron.listPlanes(vertices, triangles=None, normals=None, tolerance=1e-09, verbose=False)

make a list of planes occurring in the mesh sorted by decreasing frequency of triangles each plane is returned as a list containing a point in the plane, the normal of the plane, and its frequency of occurrence. Two planes are considered equal if isSamePlane returns true for the given tolerance listPlanes(vertices,triangles,normals=None,tolerance=1e-9,verbose=False) -> list of planes listPlanes(polyhedron,normals=None,tolerance=1e-9,verbose=False) -> list of planes

mpacts.geometrygenerators.polyhedron.lookupVertex(vertex, vertices, tolerance=2.220446049250313e-12)

look up of vertex ‘vertex’ in vertex list ‘vertices’. Two vertices are considered equal if its components differ by no more than ‘tolerance’. Returns the index of that vertex, or -1 if it is not found.

mpacts.geometrygenerators.polyhedron.normal(v0, v1, v2, area=[])

compute the normal of a triangle with vertices v0, v1 and v2 as the normalized cross product of v01 and v20. If area is a nonzero, nonempty list, it will be used instead of calculated to normalize, if it is an external variable, it will be appended with the area calculated in this function. Returns a numpy array

mpacts.geometrygenerators.polyhedron.offsetList(lst, offset)

take a list (or list of lists/tuples) of ints and add an offset to each int

mpacts.geometrygenerators.polyhedron.project_to_sphere(x)

Assumes points to be centered around (0,0,0), then projects them to the unit-sphere. (The commented-out version is used by Paul’s matlab script, but according to my tests it produces results of inferior numerical precision at higher computational cost.)

mpacts.geometrygenerators.polyhedron.removeVertexDoubles(vertices, triangles, tolerance=2.220446049250313e-12, verbose=False)

Vertices (a list of 3D coordinates) and triangles (a list of 3-tuples of ints referencing vertices) are assumed to define a triangulated surface. This function removes all copies of vertices and renumbers the vertex numbers in triangles

mpacts.geometrygenerators.polyhedron.rotateList(alist, n=1, smallestFirst=False)

Rotate the items of a list n positions, i.e. the n-th item becomes the first if smallestFirst==True, the smallest item is rotated to the first position.

mpacts.geometrygenerators.polyhedron.shuffle_indices(shuffled_idxs, to_shuffle, inPlace=True)
mpacts.geometrygenerators.polyhedron.test()