Skip to content

Commit 66b8fbe

Browse files
authored
ENH: New submodule for vtk_to_usd with correct colormap handling (#23)
1 parent 6ee68ad commit 66b8fbe

47 files changed

Lines changed: 8457 additions & 2698 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
description: PhysioMotion4D project standards and workflow preferences
3+
alwaysApply: true
4+
---
5+
6+
# PhysioMotion4D Project Standards
7+
8+
## File Operations
9+
10+
**ALWAYS use git commands for file operations in this repository:**
11+
12+
```bash
13+
# Moving files
14+
git mv old_path new_path
15+
16+
# Deleting files
17+
git rm file_path
18+
19+
# Renaming files
20+
git mv old_name.py new_name.py
21+
```
22+
23+
❌ **Don't use**: `mv`, `rm`, `cp` directly
24+
✅ **Do use**: `git mv`, `git rm` to maintain git history
25+
26+
## Documentation
27+
28+
**Do NOT create extra documentation files** describing what was done:
29+
30+
❌ **Don't create**:
31+
- `MIGRATION.md`
32+
- `CHANGES.md`
33+
- `UPDATE_SUMMARY.md`
34+
- `MODERNIZATION_*.md`
35+
- Similar meta-documentation files
36+
37+
✅ **Do document**:
38+
- In-code docstrings
39+
- README files for new modules
40+
- Inline comments for complex logic
41+
- API documentation in existing docs
42+
43+
## Backward Compatibility
44+
45+
**Backward compatibility is NOT a priority** for this project:
46+
47+
- Feel free to make breaking changes to improve code quality
48+
- Remove deprecated code without extensive migration paths
49+
- Update APIs for clarity and consistency
50+
- Prioritize modern, clean design over legacy support
51+
52+
## Code Style
53+
54+
- Use descriptive variable and function names
55+
- Add type hints to Python functions
56+
- Keep functions focused and small
57+
- Use `logging` module instead of `print` statements
58+
- Follow PEP 8 for Python code
59+
60+
## Testing
61+
62+
- Test new functionality with Jupyter notebooks in `experiments/`
63+
- Update existing tests when changing APIs
64+
- Use meaningful test names that describe what is being tested
65+
66+
## Git Workflow
67+
68+
**Do NOT stage files automatically:**
69+
70+
❌ **Don't use**: `git add`, `git stage`
71+
✅ **Do use**: `git status` to show what changed
72+
✅ **User will**: Stage files themselves when ready
73+
74+
The user prefers to review and stage changes manually.
75+
76+
**Other git guidelines:**
77+
- Use `git rm` and `git mv` for file operations
78+
- Make atomic commits with clear messages
79+
- Don't commit large binary files (add to `.gitignore`)
80+
- Use `git status` to verify changes before committing

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ repos:
3737
pass_filenames: false
3838
always_run: false
3939
files: ^(src/physiomotion4d/|tests/)
40-
stages: [pre-push]
40+
stages: [pre-push]

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,16 @@ print(f"PhysioMotion4D version: {physiomotion4d.__version__}")
107107
- **Utility Classes**: Tools for data manipulation and conversion
108108
- `TransformTools`: Comprehensive transform manipulation utilities
109109
- `USDTools`: USD file manipulation for Omniverse integration
110+
- `USDAnatomyTools`: Apply surgical materials to anatomy meshes
110111
- `ImageTools`: Medical image processing utilities
111112
- `ContourTools`: Mesh extraction and contour manipulation
113+
- **USD Conversion**: VTK to USD conversion for Omniverse visualization
114+
- `ConvertVTKToUSD`: High-level converter for PyVista/VTK objects with colormap support
115+
- `vtk_to_usd` module: File-based conversion library
116+
- `VTKToUSDConverter`: Core converter with time-series support
117+
- `read_vtk_file()`: Read VTK/VTP/VTU files into MeshData
118+
- `ConversionSettings`: Configurable conversion parameters
119+
- `MaterialData`: USD material definitions
112120

113121
### Key Dependencies
114122

@@ -261,6 +269,80 @@ inverse_transform = results["inverse_transform"] # Fixed to moving
261269
forward_transform = results["forward_transform"] # Moving to fixed
262270
```
263271

272+
### VTK to USD Conversion
273+
274+
PhysioMotion4D provides two APIs for converting VTK data to USD for NVIDIA Omniverse visualization:
275+
276+
#### Option 1: High-Level ConvertVTKToUSD (for PyVista/VTK objects)
277+
278+
```python
279+
from physiomotion4d import ConvertVTKToUSD
280+
import pyvista as pv
281+
282+
# Load VTK data
283+
meshes = [pv.read(f"cardiac_frame_{i:03d}.vtp") for i in range(20)]
284+
285+
# Convert to animated USD with anatomical labels
286+
converter = ConvertVTKToUSD(
287+
data_basename='CardiacModel',
288+
input_polydata=meshes,
289+
mask_ids={1: 'ventricle', 2: 'atrium', 3: 'vessels'},
290+
compute_normals=True
291+
)
292+
293+
# Optional: Apply colormap visualization
294+
converter.set_colormap(
295+
color_by_array='transmembrane_potential',
296+
colormap='rainbow',
297+
intensity_range=(-80.0, 20.0)
298+
)
299+
300+
stage = converter.convert('cardiac_motion.usd')
301+
```
302+
303+
#### Option 2: File-Based vtk_to_usd Library
304+
305+
```python
306+
from physiomotion4d.vtk_to_usd import (
307+
VTKToUSDConverter,
308+
ConversionSettings,
309+
MaterialData,
310+
convert_vtk_file,
311+
)
312+
313+
# Simple single-file conversion
314+
stage = convert_vtk_file('mesh.vtp', 'output.usd')
315+
316+
# Advanced: Custom settings and materials
317+
settings = ConversionSettings(
318+
triangulate_meshes=True,
319+
compute_normals=True,
320+
meters_per_unit=0.001, # mm to meters
321+
times_per_second=60.0,
322+
)
323+
324+
material = MaterialData(
325+
name="cardiac_tissue",
326+
diffuse_color=(0.9, 0.3, 0.3),
327+
roughness=0.4,
328+
)
329+
330+
converter = VTKToUSDConverter(settings)
331+
stage = converter.convert_file('heart.vtp', 'heart.usd', material=material)
332+
333+
# Time-series conversion
334+
files = ['frame_000.vtp', 'frame_001.vtp', 'frame_002.vtp']
335+
time_codes = [0.0, 0.1, 0.2]
336+
stage = converter.convert_sequence(files, 'animated.usd', time_codes=time_codes)
337+
```
338+
339+
Features:
340+
- Automatic coordinate system conversion (RAS to Y-up)
341+
- Material system with UsdPreviewSurface
342+
- Preserves all VTK data arrays as USD primvars
343+
- Time-series animation support
344+
- Supports VTP, VTK, and VTU file formats
345+
264346
### Logging and Debug Control
265347

266348
PhysioMotion4D provides standardized logging through the `PhysioMotion4DBase` class, which is inherited by workflow and registration classes.

data/CHOP-Valve4D/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Alterra*
2+
TPV*

data/KCL-Heart-Model/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# KCL Heart Model Dataset
22

3+
## ⚠️ Manual Download Required
4+
5+
**This data is NOT automatically downloaded.** Users must manually download and preprocess the required files.
6+
7+
### Required Files for PhysioMotion4D
8+
9+
The following files must be present in this directory for VTK to USD conversion:
10+
- `average_surface.vtp` - Mean heart surface mesh (VTK PolyData format)
11+
- `average_mesh.vtk` - Mean heart volume mesh (VTK UnstructuredGrid format)
12+
13+
These files can be generated from the KCL dataset using conversion tools provided in this directory.
14+
315
## Overview
416

517
This directory contains data from the King's College London (KCL) four-chamber heart model dataset, which provides a virtual cohort of adult healthy heart meshes derived from CT images.
@@ -74,4 +86,3 @@ For questions about the dataset, contact the original authors:
7486
- Pablo Lamata, King's College London
7587

7688
For questions about SlicerSALT, contact: beatriz.paniagua@kitware.com
77-

0 commit comments

Comments
 (0)