# Calculate a scalp to scalp distance

**URL:** https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141
**Category:** Support
**Created:** [September 28, 2017, 2:01pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141 "2017-09-28T14:01:20Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [September 28, 2017, 2:01pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/1 "2017-09-28T14:01:20Z")

</div>

Dear Slicer experts,

Quick mail to know if you know a way/a module to calculate a scalp to scalp distance (i.e. distance along the surface)?

More than “CurveMaker”, I would like an automatic way to realize that (1/mesh the brain,2/ calculate distance between nodes?).

Thanks in advance,  
All the best,

Frederic

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 28, 2017, 11:40pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/2 "2017-09-28T23:40:00Z")

</div>

You can use `Model to model distance` extension to measure distance between surfaces.

What do you mean by “More than CurveMaker…”?

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [September 29, 2017, 9:14am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/3 "2017-09-29T09:14:31Z")

</div>

Hi Andras,  
Thanks for your quick reply.

My question lacked of precision: I have several T1-weighted images, and I would like, always on the same scalp, calculate distance along the surface.

“Model to model distance” seems works for two different models, I would rather work just about one model (T1–\>mesh of the scalp–\>compute the distance between two points on the mesh).  
Is-it possible?

Thanks a lot!  
Frederic

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [September 29, 2017, 12:58pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/4 "2017-09-29T12:58:27Z")

</div>

I don’t know about any module that provides this but it’s very easy to create a new module for this, as this VTK class compute geodesic path on a surface: [https://www.vtk.org/doc/nightly/html/classvtkDijkstraGraphGeodesicPath.html#details](https://www.vtk.org/doc/nightly/html/classvtkDijkstraGraphGeodesicPath.html#details)

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [October 2, 2017, 4:16pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/5 "2017-10-02T16:16:58Z")

</div>

Thanks Andrea,  
Very easy for you, but not for me ;).

Before use the VTK class, how is-it possible to mesh the scalp of T1 image with 3dslicer?

Thanks once again,

Frederic

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [October 2, 2017, 4:28pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/6 "2017-10-02T16:28:30Z")

</div>

Use Segment Editor tool to create surface mesh from an image.

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [October 3, 2017, 3:42pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/7 "2017-10-03T15:42:13Z")

</div>

Great, Thanks a lot Andras.

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [October 6, 2017, 3:30pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/8 "2017-10-06T15:30:02Z")

</div>

Dear Andras and Slicer users,  
I have begun my script to calculate distance along the surface.  
However, I butt into an obstacle. Indeed, How could I make correspond vertex (in bold below) of my vtkPolyDataMapper with coordinate obtain by the fiducal properties (in italic)?  
Perhaps by a KD-Tree (tk.vtkKdTree)?

Thanks in advance.  
Frederic

In python:

```python
##Access to Fiducial Properties
fidList = slicer.util.getNode('F')
numFids = fidList.GetNumberOfFiducials()
for i in range(numFids):
  ras = [0,0,0]
  fidList.GetNthFiducialPosition(i,ras)
  print i,": RAS =",ras_

#vtkDijkstraGraphGeodesicPath
dijkstra = vtk.vtkDijkstraGraphGeodesicPath()
dijkstra.SetInputConnection(reader.GetOutputPort())
dijkstra.SetStartVertex( **20** )
dijkstra.SetEndVertex( **500000** )
dijkstra.Update()
```

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [October 7, 2017, 4:50am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/9 "2017-10-07T04:50:44Z")

</div>

To get index of a point closest to a spatial position, you need use a locator:

```
somePolyData = someModelNode.GetPolyData()
pointLocator = vtk.vtkPointLocator()
pointLocator.SetDataSet(somePolyData)
pointLocator.BuildLocator()
closestPointId = curvePointsLocator.FindClosestPoint(point)

```

Not that these are all standard VTK operations, there is nothing specific to Slicer. So, you may find answers by searching in [VTK source code](https://github.com/Kitware/VTK), reading the [VTK textbook](https://www.vtk.org/vtk-textbook/) (it is worth reading the book cover to cover, a few times), and browsing [VTK examples](https://lorensen.github.io/VTKExamples/site/).

---

<div class="post-metadata">

### Author: ![doc-xie](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doc-xie](https://discourse.slicer.org/u/doc-xie)
#### Post date: [October 9, 2017, 1:21pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/10 "2017-10-09T13:21:34Z")

</div>

Hello，I have the same question with you? On the surface of the scalp，how to measure the distance between two pionts without Curve maker module?Dose the problem has been solved?  
Thanks for your attention!

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [October 9, 2017, 2:42pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/11 "2017-10-09T14:42:38Z")

</div>

@Frederic it would be great if you could upload current version of your module to Github, even if it is not functional yet. It would allow me, @doc-xie, and others to see the progress and help out. The core of this feature can be implemented by adding 30-40 lines of python code to the module template generated by Extension wizard module, so it should be possible to complete it in a couple of days. If you get stuck anywhere then let us know.

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [October 9, 2017, 5:27pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/12 "2017-10-09T17:27:36Z")

</div>

Hi,  
First of all, thanks @lassoan for your locator code.  
Second, my in progress draft for the module was [here](https://github.com/FredericBr/Frederic), with two python scripts: One is a standalone version for python (without 3dslicer) and the second for 3dslicer.  
I am moving slowly because I found this [RvtkStatismo](https://github.com/zarquon42b/RvtkStatismo) that made all that I want (HTH @doc-xie) .

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [October 9, 2017, 6:26pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/13 "2017-10-09T18:26:59Z")

</div>

Thanks for the update. Slicer is a frontend, from where you can use various libraries. So, if you find Statismo useful then you can add wrappers to make it avaiable directly from Slicer.

@doc-xie if you are interested to pick it up from where @Frederic left off then, I would recommend to create a skeleton extension and a Python scripted module inside (as explained in the [Slicer programming tutorial](https://www.slicer.org/wiki/Documentation/Nightly/Training#Slicer4_Programming_Tutorial)) and add Frederic’s processing code to that.

---

<div class="post-metadata">

### Author: ![doc-xie](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doc-xie](https://discourse.slicer.org/u/doc-xie)
#### Post date: [October 17, 2017, 12:53pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/14 "2017-10-17T12:53:07Z")

</div>

Yes, thanks for your believing, I am very intresting about 3D Slicer, but I am a new learner and have not any knowledge about Python. So I decide to study it with your help because the importance of Python. By the way, I am looking forward to the progress of this topic!

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [October 17, 2017, 1:34pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/15 "2017-10-17T13:34:39Z")

</div>

I have made much advance in this script, I will return to you when I will have more time (I defend my PhD research in two weeks).  
Best,  
Frederic

---

<div class="post-metadata">

### Author: ![doc-xie](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doc-xie](https://discourse.slicer.org/u/doc-xie)
#### Post date: [November 14, 2017, 8:33am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/16 "2017-11-14T08:33:26Z")

</div>

OK!  
Waiting for your good news!

---

<div class="post-metadata">

### Author: ![doc-xie](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doc-xie](https://discourse.slicer.org/u/doc-xie)
#### Post date: [December 14, 2017, 8:18am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/17 "2017-12-14T08:18:46Z")

</div>

Hello Frederic!  
Is there any progress in the calculation tools or methods of measurement excepting Curve maker Module in 3D Slicer?  
Best wishes!

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [December 14, 2017, 11:31am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/18 "2017-12-14T11:31:54Z")

</div>

Hi doc-xie.  
Yes, I have update the [Github](https://github.com/FredericBr/GeodesicPathSlicer).  
@lassoan could you help me to create an extension (I have begun on my Github)?  
@doc-xie For the moment, you could use it from your python interactor (for 3 points):

1/ Load your model in 3dslicer  
2/ Draw your 3 points with the “Create-and-Place-Fiducial” button  
3/ Insert this code in your python interactor:

```
import sys, slicer, numpy, os, math, vtk

model = getNode('name_of_your_data') #Go to Data module and see the name of your model, "Name"
#Access to Fiducial Properties (to determinate the start and the end point of the geodesic path)
fidList = slicer.util.getNode('F')
numFids = fidList.GetNumberOfFiducials()
list=[]
for i in range(numFids):
  ras = [0,0,0]
  fidList.GetNthFiducialPosition(i,ras)
  print i,": RAS =",ras
  list.append(ras)
  
#create locator
pd = model.GetModelDisplayNode()
pd1=pd.GetOutputPolyData()
pd1.GetNumberOfPoints()
loc = vtk.vtkPointLocator()
loc.SetDataSet(pd1)
loc.BuildLocator()
closestPointId = loc.FindClosestPoint(list[0]) #point1 
closestPointId1 = loc.FindClosestPoint(list[1]) #point2
closestPointId2 = loc.FindClosestPoint(list[2]) #point3

#get the distance of the geodesic path 
appendFilter = vtk.vtkAppendFilter()
appendFilter.MergePointsOn()
points = vtk.vtkPoints()

vIds = [closestPointId,closestPointId1,closestPointId2]
p0 = [0,0,0]
p1 = [0,0,0]
dist = 0.0
for n in range(len(vIds)-1):
	v0 = vIds[n]
	v1 = vIds[n+1]
	
	#create geodesic path: vtkDijkstraGraphGeodesicPath
	dijkstra = vtk.vtkDijkstraGraphGeodesicPath()
	dijkstra.SetInputConnection(pd.GetOutputPolyDataConnection())
	dijkstra.SetStartVertex(v0)
	dijkstra.SetEndVertex(v1)
	dijkstra.Update()
				
	pts = dijkstra.GetOutput().GetPoints()
	end = n<len(vIds)-2 and 0 or -1
	for ptId in range(pts.GetNumberOfPoints()-1, end, -1):
		pts.GetPoint(ptId, p0)
		points.InsertNextPoint(p0)		
	
	for ptId in range(pts.GetNumberOfPoints()-1):
		pts.GetPoint(ptId, p0)
		pts.GetPoint(ptId+1, p1)
		dist += math.sqrt(vtk.vtkMath.Distance2BetweenPoints(p0, p1))
		
	appendFilter.AddInputConnection(dijkstra.GetOutputPort())

print 'length= ', dist/10 ,'cm'

```

Best

---

<div class="post-metadata">

### Author: ![doc-xie](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@doc-xie](https://discourse.slicer.org/u/doc-xie)
#### Post date: [December 14, 2017, 12:51pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/19 "2017-12-14T12:51:55Z")

</div>

Hi,Frederic!  
After I insert the code in my phthon interactor, what i get is following:

Traceback (most recent call last):  
File “”, line 1, in   
NameError: name ‘vIds’ is not defined

> > > print 'length= ', dist/10 ,‘cm’  
> > > length= 0.0 cm

What is the reason?  
Thanks a lot!  
doc-xie

---

<div class="post-metadata">

### Author: ![Frederic](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/frederic/32/4907_2.png) [@Frederic](https://discourse.slicer.org/u/Frederic)
#### Post date: [December 14, 2017, 1:06pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/20 "2017-12-14T13:06:06Z")

</div>

Don’t forget to change this line:  
`model = getNode('name_of_your_data') #Go to Data module and see the name of your model, "Name"`

[Next page](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141.md?page=2)
