# Automatic DBS lead topology correction

### Citation

Coming up soon.

### What this code does

This codes takes as input a wireframe model of a DBS lead and returns in output the wireframe model that is the closest to the initial path (in the L2 sense) but for which:

- The distance between any two segments of the path is greater than Dmin, where Dmin is a distance defined by the user.
- At every point of the path, the curvature is smaller than Kmax, where Kmax is the maximum curvature defined by the user.

By wireframe path, I mean that a piecewise linear curve in 3D space that represents the centerline path of the DBS lead. Every segment is linear and the wireframe path model is therefore defined by a set of points in 3D space.

I refer to this process of enforcing (i) a minimum distance between any two segments of the path and (ii) a maximum curvature at every point of the path as "topology correction". This is because if the two segments are too close to each other (distance constraint violation) or if the path bends too quickly (curvature constraint violation), once the path is thickened to the actual lead diameter this will result in self-intersection of the cable model. This topology correction process is formulated as a constrained optimization problem solved using an active-set approach. The technique is accelerated using a GPU, but even then computation is relatively slow: Count a few hours for a lead with ~200 points.

### 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).