---
# System prepended metadata

title: Scar projection on 3D Meshes!

---

# Scar projection on 3D Meshes!
>TLDR: We can project the scar in reasonable time! We still need to figure out if the resolution of the scar segmentation needs to be addressed. 

# Workflow overview
+ Convert `msh_cine` to mm using `meshtool convert`
+ Get deformation file (`rigid.dof`) using MIRTK's `register`. In this case, img_lge is moving and img_cine is fixed.
+ Transform the `msh_cine` using deformation file with MIRTK transform-points (`msh_cine_mm_on_LGE`)
+ Get the centre of gravity of `msh_cine_mm_on_LGE` (`msh_cine_mm_on_LGE_cog`)
+ Get scar voxels' real world coordinates and add new scalar field to `msh_cine_mm_on_LGE_cog` based on mesh

> NOTE1: The code has been optimised by vectorising some functions and can run now in much less time. 
> NOTE2: The scar appears blocky, that's because of the resolution of the LGE. The solution for this would be to upscale the LGE segmentation. 

# Usage Instructions
These instructions will show you how to download and install José's library [`imatools`](https://github.com/alonsoJASL/imatools.git), [`meshtool`](https://bitbucket.com/aneic/meshtool), and the MIRTK libraries.
## Installations needed
+ [`meshtool`](https://bitbucket.com/aneic/meshtool)
+ `MIRTK liibraries`
+ [`imatools`](https://github.com/alonsoJASL/imatools)

**Start by creating a folder to put everything**
```shell 
mkdir ~/code 
cd ~/code 
```

### `meshtool` 
Run each line separately: 
```bash=
cd ~/code 
git clone https://bitbucket.org/aneic/meshtool.git
cd ~/code/meshtool 

make 
```
When it finishes, link it to `/usr/bin/local`
```bash

sudo ln -s $HOME/code/meshtool/meshtool /usr/bin/local/meshtool 
```
Test: 
```shell 
meshtool 
```
you should see a help message like below
```
Error: Wrong usage.
Use: meshtool [help|convert|collect|clean|extract|generate|itk|insert|interpolate|split|smooth|transform|map|merge|query|resample|restore|reindex] 
For help use: meshtool help
```

### MIRTK libraries 
Get these from José, once downloaded, put them in the `~/code` folder. They will look as follows
```
code/
├── M3DLib/
│   └── meshtools3d
├── MLib/
│   ├── close-image
│   ├── dilate-image
│   ├── erode-image
│   ├── extract-surface
│   ├── info
│   ├── init-dof
│   ├── open-image
│   ├── register
│   ├── resample-image
│   ├── smooth-surface
│   ├── transform-image
│   └── transform-points
```

### `imatools` on Linux
For full instructions, check the [README page](https://github.com/alonsoJASL/imatools/blob/master/README.md). **Download**:
```
mkdir -p ~/code & git clone https://github.com/alonsoJASL/imatools ~/code/imatools
```
**Setup the environment**: 
> You only need to do this once
```bash=
conda env create -f environment.yaml
conda activate imatools
conda install -c conda-forge vtk=9.2.6 simpleitk numpy scipy=1.9.2 matplotlib pandas seaborn networkx scikit-image nibabel pydicom -n imatools -y

# Create the file IMATOOLS_ENV_SETUP file we'll use later
./setup.sh 
```

# Workflow 
## Data
Make sure you have a folder with the following: 
+ CINE volumetric mesh (`msh_cine`) - main vtk mesh, normally in _um_
+ CINE scan (`img_cine`) - image volume (nii, nrrd, nii.gz)
+ LGE scan (`img_lge`) - image volume (nii, nrrd, nii.gz)
+ LGE segmentation (`seg_lge`) - scar segmentation (nii, nii.gz, nrrd), `scar=3`

## Run the code 
Just need to run a single script in `imatools` for this pipeline. 
**Before running, activate environment:** this must be done each time!!! 
```bash=
cd ~/code/imatools 
source IMATOOLS_ENV_SETUP
```

Call the whole pipeline from a single line.
> Take note where you have your MIRTK libraries. In this example, we assume they're in the `~/code/MLib` folder. 
> This assumes the scar label is `3`, change otherwise.
```bash=
python scripts/vscar_projection.py pipeline -in PATH_TO_DATA/msh_cine -mirtk ~/code/MLib -moving img_CINE -fixed img_LGE -ref seg_lge -label SCAR_LABEL -cwd 
```

For example, 
```bash=
python scripts/vscar_projection.py pipeline -in PATH_TO_DATA/cine_original_mesh.vtk -mirtk ~/code/MLib -moving img_CINE.nii.gz -fixed img_LGE.nii.gz -ref seg_lge.nii.gz -label 3 -cwd 
```
