Mesh#
Triangle-mesh container with Laplacian smoothing and aspect-ratio quality metrics. Introduced as
Phase-4 backfill P33 to support mesh-output workflows (TIN exports, gmsh .geo round-trips, FEM
preprocessing).
Top-level surface:
boundary_vertex_mask— bool mask flagging boundary vertices (used as fixed anchors in smoothing).neighbour_lists— per-vertex adjacency from the triangle index list.laplacian_smooth(n_iterations=..., relaxation=..., hold_boundary=True)— Persson & Strang 2004 Laplacian smoothing; boundary vertices are pinned by default.aspect_ratios()— per-triangle aspect-ratio quality metric.
digitalrivers.mesh.Mesh
#
A triangle mesh with vertex and triangle index arrays.
Performance note. boundary_vertex_mask, neighbour_lists and
aspect_ratios iterate triangles in pure Python — fine for
small / medium meshes (<~50k triangles). Above that, prefer a vendor
library (meshio / pymesh) or vectorise the kernels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
|
required |
triangles
|
ndarray
|
|
required |
Attributes:
| Name | Type | Description |
|---|---|---|
vertices |
|
|
triangles |
|
|
n_vertices, |
n_triangles
|
counts. |
Examples:
-
Build a two-triangle quad and inspect its size:
import numpy as np from digitalrivers.mesh import Mesh verts = np.array( ... [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], ... dtype=np.float64, ... ) tris = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.int64) mesh = Mesh(verts, tris) mesh.n_vertices 4 mesh.n_triangles 2
-
3-D input keeps Z untouched:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [[0.0, 0.0, 10.0], [1.0, 0.0, 11.0], [0.0, 1.0, 12.0]], ... dtype=np.float64, ... ) t = np.array([[0, 1, 2]], dtype=np.int64) Mesh(v, t).vertices.shape (3, 3)
See Also
Mesh.laplacian_smooth: iterative quality-improvement smoothing. Mesh.aspect_ratios: per-triangle quality metric.
Source code in src/digitalrivers/mesh.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
boundary_vertex_mask()
#
Boolean (n_vertices,) mask of boundary vertices.
A vertex is on the boundary iff at least one of its incident edges belongs to only one triangle (the canonical mesh-boundary criterion).
Examples:
-
Every vertex of a two-triangle quad sits on the boundary:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], ... dtype=np.float64, ... ) t = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.int64) mask = Mesh(v, t).boundary_vertex_mask() bool(mask.all()) True
-
Adding a centre vertex moves only the four corners to the boundary:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [ ... [0.0, 0.0], [2.0, 0.0], [2.0, 2.0], ... [0.0, 2.0], [1.0, 1.0], ... ], ... dtype=np.float64, ... ) t = np.array( ... [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]], ... dtype=np.int64, ... ) mask = Mesh(v, t).boundary_vertex_mask() mask.tolist() [True, True, True, True, False]
Source code in src/digitalrivers/mesh.py
neighbour_lists()
#
Per-vertex list of neighbour vertex indices (1-ring).
Examples:
-
Inspect the shared diagonal of a two-triangle quad:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], ... dtype=np.float64, ... ) t = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.int64) adj = Mesh(v, t).neighbour_lists() adj[0][1, 2, 3] adj[1][0, 2]
Source code in src/digitalrivers/mesh.py
laplacian_smooth(n_iterations=10, relaxation=0.5, hold_boundary=True)
#
Iterative Laplacian smoothing.
Each iteration moves every non-boundary vertex toward the
centroid of its 1-ring neighbours by relaxation of the
full step:
v_new = v + relaxation * (centroid(neighbours) - v)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_iterations
|
int
|
Number of smoothing passes. |
10
|
relaxation
|
float
|
Step size in |
0.5
|
hold_boundary
|
bool
|
If True (default), boundary vertices are fixed. Set False only when the mesh is closed (no boundary). |
True
|
Returns:
| Type | Description |
|---|---|
'Mesh'
|
A new |
'Mesh'
|
connectivity is unchanged. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
-
Smooth an off-centre interior vertex toward the centroid of its four corner neighbours; the corners stay pinned:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [ ... [0.0, 0.0], [2.0, 0.0], [2.0, 2.0], ... [0.0, 2.0], [1.5, 1.5], ... ], ... dtype=np.float64, ... ) t = np.array( ... [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]], ... dtype=np.int64, ... ) smoothed = Mesh(v, t).laplacian_smooth( ... n_iterations=20, relaxation=1.0, ... ) [round(float(c), 6) for c in smoothed.vertices[4]][1.0, 1.0] smoothed.vertices[0].tolist() [0.0, 0.0]
Source code in src/digitalrivers/mesh.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
aspect_ratios()
#
Per-triangle aspect ratio circumradius / (2 * inradius).
Equilateral triangles score 1.0 (the optimum). Higher values
indicate worse quality. Degenerate triangles (zero area) score
+inf.
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Examples:
-
An equilateral triangle scores exactly 1.0:
import numpy as np from digitalrivers.mesh import Mesh h = np.sqrt(3.0) / 2.0 v = np.array([[0.0, 0.0], [1.0, 0.0], [0.5, h]], dtype=np.float64) t = np.array([[0, 1, 2]], dtype=np.int64) round(float(Mesh(v, t).aspect_ratios()[0]), 6) 1.0
-
A 3-4-5 right triangle has aspect ratio 1.25:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [[0.0, 0.0], [3.0, 0.0], [0.0, 4.0]], dtype=np.float64, ... ) t = np.array([[0, 1, 2]], dtype=np.int64) round(float(Mesh(v, t).aspect_ratios()[0]), 6) 1.25
-
Three collinear points give a degenerate (infinite) ratio:
import numpy as np from digitalrivers.mesh import Mesh v = np.array( ... [[0.0, 0.0], [1.0, 0.0], [2.0, 0.0]], dtype=np.float64, ... ) t = np.array([[0, 1, 2]], dtype=np.int64) float(Mesh(v, t).aspect_ratios()[0]) inf