# Automatic DBS lead topology correction

### Citation

### What this code does

### Download files

The code, examples and additional scripts for creation of DBS leads in HFSS can be downloaded here.

### Instructions

Download the zip file using the link above and unzip it.

#### Compitlation

If you have a GPU running on a Linux computer, go to the CODE/ folder and compile the gamma distance GPU kernel as follows: nvcc -ptx GLOBAL_CONST_CUDAKernel.cu This will create the GLOBAL_CONST_CUDAKernel.ptx file required to launch the kernel from Matlab. The GLOBAL_CONST_CUDAKernel.ptx file present in the CODE/ subfolder was created on a CentOS Linux computer (version 7.2.1511) using CUDA 6.5. You may try to use it, but there is no guarantee that it will work on your computer.

#### Running an example

The subfolder EXAMPLE/ contains an example of how to run the code. The master script is RUN_OPT_bilateral.m. All the options used in this script are commented and should be easy to understand. There are several things to note in this script:

- Both the left and right DBS leads are loaded in Matlab and are connected in a single wireframe dataset. The topology correction process is run on this "super-lead" in order to remove gamma distance violations not only between the segments of a given lead, but also between segments belonging to different leads. Once the topology correction has been performed, the leaft and right leads can be separated again.
- The topology correction process is run at several multi-resolution levels. In the RUN_OPT_bilateral.m, the multi-resolution process has only two iterations: In the first iteration, the input lead are resampled with a 5 mm linear resolution and the topology correction process is run on this resampled lead model. In the second iterations, the corrected lead resulting from the first iteration is resampled at 3 mm resolution and the topogy correction process is run again on this refined wireframe model. This multi-resolution strategy avoids the correction process to get stuck on a local minimum that does not satisfy the constraints. More multi-resolution loops and resolutions can be added by simply lines 21 and 22 of the script.
- The topology correction process itself is performed by the Matlab function deform_path_LINEAR.m. The input to this function are, in this order:

- path: The lead model to be corrected for;
- n_resample: If resample>0, before the correction is run the input path is resampled and the number of points in the resampled path is n_resample. If n_resample<0, the input path is not resampled prior to the topology correction.

- Dmin: The minimum gamma distance between any two segments. This should be set to the lead diameter, plus a little extra more for margin. - Kmax: The maximum curvature of the cable model. This should be set to 2/D, where D is the diameter of the lead, minus a little extra for margin. - display: 1 for display ON, 0 for display OFF. The display ON options plots some interesting statistics about the correction process so I recommend using it. - nCPUs: This the computation method flag. If nCPUs<0, the GPU is used for the optimization process. If nCPUs>0, this number indicates the number of CPUs used for the solve process (nCPUs=1 is for using a single CPU).