# Converting to USD

The conversion process involves three stages:

**NRRD to NIfTI Conversion**

- Convert segmentation files from NRRD to NIfTI format

**NIfTI to Mesh Processing**

- Groups 140 labels into 17 anatomical categories
- Creates separate OBJ files for each organ
- Applies smoothing and mesh reduction
- Supports detailed anatomical structures including:
  - **Organs:** Liver, Spleen, Pancreas, etc.
  - **Skeletal system:** Spine, Ribs, Shoulders, Hips
  - **Vascular system:** Veins and Arteries
  - **Muscular system:** Back muscles

**Mesh to USD Conversion**

- Converts the processed meshes to USD format
- Final USD files can be imported into Isaac Sim

---

## NRRD to USD Converter Tool

To convert NRRD medical images to USD format, follow these steps:

1. Go to the `i4h-workflows/tutorials/assets/CT_to_USD` folder in the tutorials directory of Isaac for Healthcare.
2. Install the required packages using the provided `requirements.txt`.
3. Make a backup of your CT data, as the conversion will modify files in place.
4. Run `converter.py` on your CT data folder to start the conversion process.

### Usage

**Setup:**
Consider using a seperate virtual environment for this optional part of the course. If you have created a new environment for the MONAI workflow before, you can use this one.

```bash
conda activate monai
cd $I4H_HOME/tutorials/assets/CT_to_USD
pip install -r requirements.txt
```

#### NOTE
If you encounter issues with the additional dependencies, please use a seperate conda environment.

**Usage:**
Copy your data to a sample folder of your choice, then export the folder name.

```bash
export GENERATED_DATA="/media/data/dli/maisi/sample_1/"
```

```bash
# Then run the converter
python utils/converter.py $GENERATED_DATA
```

In this video, we convert the previously generated CT data to a USD file and load it into IsaacS im.

[Video](https://cdnapisec.kaltura.com/p/2935771/sp/293577100/playManifest/entryId/1_5o1xhwob/format/url/protocol/https)

### Output Structure

The converter generates:

```default
output/
├── nii/           # Intermediate NIfTI files
├── obj/           # Intermediate mesh files  
└── all_organs.usd # Final USD file
```

### Supported Anatomical Structures

The tool processes 140 labels grouped into 17 categories including:

- **Organs:** Liver, Spleen, Pancreas, Heart, Gallbladder, Stomach, Kidneys
- **Digestive:** Small bowel, Colon
- **Skeletal:** Spine, Ribs, Shoulders, Hips
- **Vascular:** Veins and Arteries
- **Respiratory:** Lungs
- **Muscular:** Back muscles

#### Conversion Pipeline

The tool implements the complete conversion workflow:

1. **NRRD → NIfTI:** Format conversion for medical data
2. **NIfTI → Mesh:** Surface extraction with smoothing and reduction
3. **Mesh → USD:** Final format suitable for Isaac Sim and 3D visualization

Each organ maintains unique labeling for proper segmentation in the final USD file.

---

### Load CT-derived Data Into Isaac Sim

#### Open assets again

1. Launch **Isaac Sim** again.
2. Find the path of your **I4H-assets**.

```bash
python -c "from i4h_asset_helper import get_i4h_local_asset_path; print(get_i4h_local_asset_path())"
```

1. Open a **CT-derived USD file**, for example from `i4h-workshop/outputs/`.
   We provide some generated CT data, with the converted USD asset. You can downlaod it here.

---

#### Scene With Generated CT

[Video](https://cdnapisec.kaltura.com/p/2935771/sp/293577100/playManifest/entryId/1_ybm38m01/format/url/protocol/https)

In this video, we build a new robotic ultrasound setup, with a patient model derived from the generated CT data.

---
