Skip to content

Commit f452375

Browse files
authored
Add unit tests for mesh combining and extraction methods (#5)
- Implemented unit tests for the Mesh.combine() method, covering scenarios such as combining simple meshes, preserving existing markers, and handling marker name conflicts. - Added tests for the Mesh.extract_by_marker() method to ensure correct extraction of submeshes by marker name, including error handling for nonexistent markers. - Updated TypeScript documentation to include examples for extracting meshes by marker and converting them to THREE.js geometries. - Enhanced MeshUtils with methods for extracting submeshes by marker and converting them to BufferGeometry, ensuring compatibility with THREE.js.
1 parent 02850ac commit f452375

8 files changed

Lines changed: 1196 additions & 50 deletions

File tree

python/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,93 @@ Common use cases:
501501
- **Mesh processing**: Feature identification and region marking
502502
- **Visualization**: Highlighting specific mesh regions or boundaries
503503

504+
### Combining and Extracting Meshes
505+
506+
Meshly provides powerful functionality for combining multiple meshes and extracting submeshes by marker:
507+
508+
#### Combining Meshes
509+
510+
```python
511+
# Create multiple meshes to combine
512+
mesh1 = Mesh(
513+
vertices=np.array([[0, 0, 0], [1, 0, 0], [0.5, 1, 0]], dtype=np.float32),
514+
indices=np.array([0, 1, 2], dtype=np.uint32)
515+
)
516+
517+
mesh2 = Mesh(
518+
vertices=np.array([[2, 0, 0], [3, 0, 0], [2.5, 1, 0]], dtype=np.float32),
519+
indices=np.array([0, 1, 2], dtype=np.uint32)
520+
)
521+
522+
# Combine meshes without markers
523+
combined = Mesh.combine([mesh1, mesh2])
524+
print(f"Combined mesh has {combined.vertex_count} vertices")
525+
526+
# Combine meshes and assign marker names to each
527+
combined_with_markers = Mesh.combine(
528+
[mesh1, mesh2],
529+
marker_names=["part1", "part2"]
530+
)
531+
print(f"Markers: {list(combined_with_markers.markers.keys())}")
532+
533+
# Preserve existing markers when combining
534+
mesh1.markers = {"boundary": np.array([0, 1], dtype=np.uint32)}
535+
mesh2.markers = {"boundary": np.array([1, 2], dtype=np.uint32)}
536+
537+
combined_preserve = Mesh.combine([mesh1, mesh2], preserve_markers=True)
538+
print(f"Combined boundary marker has {len(combined_preserve.markers['boundary'])} elements")
539+
540+
# If meshes have the same marker name, they are merged
541+
# If marker_names is provided, it takes precedence over existing markers
542+
```
543+
544+
#### Extracting Submeshes by Marker
545+
546+
```python
547+
# Create a mesh with multiple marked regions
548+
vertices = np.array([
549+
[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0],
550+
[0.5, 0.5, 1]
551+
], dtype=np.float32)
552+
553+
indices = np.array([
554+
0, 1, 4, # bottom triangle
555+
1, 2, 4, # right triangle
556+
2, 3, 4, # top triangle
557+
3, 0, 4 # left triangle
558+
], dtype=np.uint32)
559+
560+
mesh = Mesh(
561+
vertices=vertices,
562+
indices=indices,
563+
markers={
564+
"bottom_faces": [[0, 1, 4]],
565+
"side_faces": [[1, 2, 4], [2, 3, 4], [3, 0, 4]]
566+
}
567+
)
568+
569+
# Extract a submesh containing only the bottom face
570+
bottom_mesh = mesh.extract_by_marker("bottom_faces")
571+
print(f"Bottom mesh has {bottom_mesh.vertex_count} vertices")
572+
print(f"Bottom mesh has {bottom_mesh.polygon_count} polygons")
573+
574+
# Extract side faces
575+
side_mesh = mesh.extract_by_marker("side_faces")
576+
print(f"Side mesh has {side_mesh.vertex_count} vertices")
577+
print(f"Side mesh has {side_mesh.polygon_count} polygons")
578+
579+
# The extracted mesh contains only the referenced vertices and elements
580+
# Vertex indices are automatically remapped to the new mesh
581+
```
582+
583+
Features of mesh combining and extraction:
584+
- **Automatic vertex offset computation** for efficient merging
585+
- **Marker preservation** with optional marker name assignment
586+
- **Cell-based markers** that reference mesh elements, not just vertices
587+
- **Vertex remapping** using efficient numpy operations (O(n log n))
588+
- **Element structure preservation** maintains polygon sizes and cell types
589+
- **Error handling** for invalid markers or missing data
590+
504591
## Mesh Copying
505592

506593
Create independent copies of meshes with the [`copy()`](python/meshly/mesh.py:129) method:

0 commit comments

Comments
 (0)