padne.mesh

Attributes

Exceptions

MeshingException

Exception raised when CGAL mesh generation fails due to invalid geometry.

Classes

HasIndex

Base class for protocol classes.

Vector

Point

Vertex

HalfEdge

Face

IndexStore

A simple class that stores objects with indices.

Mesh

ZeroForm

OneForm

A discrete 1-form defined on the (h)edges of a mesh.

TwoForm

A discrete 2-form defined on the faces of a mesh.

Mesher

This class is responsible for generating a mesh from a Shapely polygon.

Module Contents

padne.mesh.index_type[source]
class padne.mesh.HasIndex[source]

Bases: Protocol

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).

For example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto[T](Protocol):
    def meth(self) -> T:
        ...
i: index_type[source]
class padne.mesh.Vector[source]
dx: float[source]
dy: float[source]
dot(other: Vector) float[source]
class padne.mesh.Point[source]
x: float[source]
y: float[source]
distance(other: Point) float[source]
to_shapely() shapely.geometry.Point[source]

Convert this Point to a shapely.geometry.Point.

Returns:

A shapely Point with the same coordinates

class padne.mesh.Vertex[source]
p: Point[source]
out: HalfEdge | None = None[source]
i: index_type[source]
orbit() Iterator[HalfEdge][source]
class padne.mesh.HalfEdge[source]
origin: Vertex[source]
twin: HalfEdge | None = None[source]
next: HalfEdge | None = None[source]
prev: HalfEdge | None = None[source]
face: Face | None = None[source]
i: index_type[source]
property is_boundary: bool[source]
static connect(e1: HalfEdge, e2: HalfEdge) None[source]
walk() Iterator[HalfEdge][source]
cotan() float[source]

Compute the cotangent weight for this half-edge.

class padne.mesh.Face[source]
edge: HalfEdge = None[source]
is_boundary: bool = False[source]
i: index_type[source]
property edges[source]
property vertices[source]
property centroid: Point[source]

Compute the centroid of the face using the average of vertex coordinates.

property area: float[source]

Compute the area using the shoelace formula. Returns the absolute value to ensure positive area regardless of vertex order.

class padne.mesh.IndexStore[T: HasIndex][source]

A simple class that stores objects with indices.

property next_index: index_type[source]

Get the next available index without adding an object.

add(obj: T) None[source]
to_index(obj: T) index_type[source]

Get the index of an object. This is for backwards compatibility, otherwise use obj.i directly.

to_object(idx: int | index_type) T[source]

Get the object at a given index.

items() Iterator[tuple[index_type, T]][source]
class padne.mesh.Mesh[source]
vertices[source]
halfedges[source]
faces[source]
boundaries[source]
make_vertex(p: Point) Vertex[source]
connect_vertices(v1: Vertex, v2: Vertex) HalfEdge[source]

Return a half-edge between the two vertices v1 and v2. If the half edge does not exist, create it. The twin half-edge is also created.

euler_characteristic() int[source]
classmethod from_triangle_soup(points: list[Point], triangles: list[tuple[int, int, int]]) Mesh[source]
class padne.mesh.ZeroForm[source]
mesh: Mesh[source]
values: numpy.ndarray[source]
d() OneForm[source]

Compute the exterior derivative (gradient) of this 0-form.

For a function f on vertices, the exterior derivative df is a 1-form where: (df)[edge] = f(target_vertex) - f(source_vertex)

Returns:

A OneForm representing the gradient of this function

class padne.mesh.OneForm[source]

A discrete 1-form defined on the (h)edges of a mesh.

mesh: Mesh[source]
values: numpy.ndarray[source]
class padne.mesh.TwoForm[source]

A discrete 2-form defined on the faces of a mesh.

mesh: Mesh[source]
values: numpy.ndarray[source]
padne.mesh.PolyBoundaryDistanceMap[source]
padne.mesh.CGALPolygon[source]
exception padne.mesh.MeshingException[source]

Bases: RuntimeError

Exception raised when CGAL mesh generation fails due to invalid geometry.

This includes cases such as: - Self-intersecting polygons with unauthorized constraint intersections - Degenerate edges that are too short (near-duplicate vertices) - Other geometric degeneracies that prevent mesh generation

With CGAL_DEBUG enabled, these issues are detected early through CGAL’s internal precondition checking, preventing crashes and providing clear error messages.

class padne.mesh.Mesher(config: Mesher | None = None)[source]

This class is responsible for generating a mesh from a Shapely polygon. Works through the triangle library.

class Config[source]

Configuration parameters for mesh generation.

minimum_angle: float = 20.0[source]
maximum_size: float = 0.6[source]
variable_density_min_distance: float = 0.5[source]
variable_density_max_distance: float = 3.0[source]
variable_size_maximum_factor: float = 3.0[source]
distance_map_quantization: float = 1.0[source]
RELAXED = None[source]
property is_variable_density: bool[source]

Return True if variable density meshing is enabled.

config[source]
poly_to_mesh(poly: shapely.geometry.Polygon, seed_points: list[Point] = []) Mesh[source]

Convert a Shapely polygon to a triangular mesh.

Args:

poly: A Shapely polygon, potentially with holes

Returns:

A Mesh object representing the triangulated polygon