# 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:** 2

<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, 1:18pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/21 "2017-12-14T13:18:09Z")

</div>

I am so bad about the result.  
When I change the “name of your data”,“Name” or both of them,the result is the same as before.

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

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

Looking forward to your reply!  
Thanks!  
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:22pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/22 "2017-12-14T13:22:53Z")

</div>

What do you obtain after write  
`model`  
in your interactor?

---

<div class="post-metadata">

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

</div>

> > > import sys, slicer, numpy, os, math, vtk
> > > 
> > > model = getNode(‘kk’) #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()  
> > > File “”, line 8  
> > > pd = model.GetModelDisplayNode()  
> > > ^  
> > > SyntaxError: invalid syntax  
> > > pd1=pd.GetOutputPolyData()  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘pd’ is not defined  
> > > pd1.GetNumberOfPoints()  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘pd1’ is not defined  
> > > loc = vtk.vtkPointLocator()  
> > > loc.SetDataSet(pd1)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘pd1’ is not defined  
> > > loc.BuildLocator()  
> > > closestPointId = loc.FindClosestPoint(list[0]) #point1  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > IndexError: list index out of range  
> > > closestPointId1 = loc.FindClosestPoint(list[1]) #point2  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > IndexError: list index out of range  
> > > closestPointId2 = loc.FindClosestPoint(list[2]) #point3  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > IndexError: list index out of range
> > > 
> > > #get the distance of the geodesic path  
> > > appendFilter = vtk.vtkAppendFilter()  
> > > appendFilter.MergePointsOn()  
> > > points = vtk.vtkPoints()
> > > 
> > > vIds = [closestPointId,closestPointId1,closestPointId2]  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘closestPointId’ is not defined  
> > > 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())  
> > > …  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘vIds’ is not defined  
> > > print 'length= ', dist/10 ,‘cm’  
> > > length= 0.0 cm

---

<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, 1:31pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/24 "2017-12-14T13:31:04Z")

</div>

The result just like before:

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

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

Dose the code should be changed include “name of your data” and “Name”?  
Best wishes!  
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:31pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/25 "2017-12-14T13:31:37Z")

</div>

the error is here:  
… #create locator  
… pd = model.GetModelDisplayNode()

The indentation is bad.  
Stop after each loop, press “enter key” and copy/past the rest of the code.

---

<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:45pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/26 "2017-12-14T13:45:01Z")

</div>

Cf. paint

 ![Sans titre](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/6/66381724c774a67f85c2d89ba9dd601be4006e05.png)

---

<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, 1:50pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/27 "2017-12-14T13:50:27Z")

</div>

I cannot understand your meaning precisely.  
How many and which code should be changed in total?  
Would you like show us the step in details?  
How to stop the loop?  
Or can you release the changed code?  
Because the problem confused me for a long tine.  
Thank you very much!  
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:59pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/28 "2017-12-14T13:59:16Z")

</div>

The code is the same.  
Just copy it step by step.

---

<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, 2:13pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/29 "2017-12-14T14:13:37Z")

</div>

But I got the same result,what is the reason?

 ![无标题](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/1/51d92fd93194d9881198a20d5b21351a9276364a.jpeg)

---

<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, 2:34pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/30 "2017-12-14T14:34:46Z")

</div>

Do you have draw 3 fiducials points on your bones?

---

<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, 2:58pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/31 "2017-12-14T14:58:10Z")

</div>

Yes,I set three fiducial points on the surface of the bone model.  
Is there any other code should be changed?  
Thanks for your quick answer!  
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, 3:02pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/32 "2017-12-14T15:02:25Z")

</div>

Please, could you send me, a picture with:  
Your model + your 3 points + the code in the python interactor.  
Thx

---

<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 15, 2017, 2:35am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/33 "2017-12-15T02:35:05Z")

</div>

My model is bone,and the three Fiducial points locate on the surface of the bone.  
The code with the changed name of the bone is in the following picture!  
无标题|690x387

---

<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 15, 2017, 8:12am UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/34 "2017-12-15T08:12:47Z")

</div>

Hi,  
I don’t see your picture, the link is break.

---

<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 15, 2017, 1:26pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/35 "2017-12-15T13:26:38Z")

</div>

Hello,Frederic!  
The picture show the model,Fiducial pionts and the changed code.  
Is there any mistake with it?

 ![0000](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/3/93d0cfc22142a5e78f08e70ea8365691ff0f91e7.jpeg)

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 15, 2017, 1:42pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/36 "2017-12-15T13:42:45Z")

</div>

It seems good.  
Write the code until 'list.append(ras)'  
Then, say me what do you obtain in list.  
(write list in interactor).

---

<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 15, 2017, 1:48pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/37 "2017-12-15T13:48:03Z")

</div>

The result is:  
0 : RAS = [-64.88647044483935, 31.38609213676277, 22.4068165428881]  
1 : RAS = [-67.15588955489847, 15.631492316231501, -7.362209206466714]  
2 : RAS = [-69.599602782564, -17.377627328452363, -16.836592046659394]

---

<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 15, 2017, 2:03pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/38 "2017-12-15T14:03:45Z")

</div>

Perfect. Now write the code until the loop. What do you obtain for the ClosestPointId, ClosestPointId1, ClosestPointId2?

---

<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 15, 2017, 2:06pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/39 "2017-12-15T14:06:08Z")

</div>

There are some mistakes with these code:  
File “”, line 8  
pd = model.GetModelDisplayNode()  
^  
SyntaxError: invalid syntax

> > > pd1=pd.GetOutputPolyData()  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘pd’ is not defined  
> > > pd1.GetNumberOfPoints()  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘pd1’ is not defined  
> > > loc = vtk.vtkPointLocator()  
> > > loc.SetDataSet(pd1)  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > NameError: name ‘pd1’ is not defined  
> > > loc.BuildLocator()  
> > > closestPointId = loc.FindClosestPoint(list[0]) #point1  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > IndexError: list index out of range  
> > > closestPointId1 = loc.FindClosestPoint(list[1]) #point2  
> > > Traceback (most recent call last):  
> > > File “”, line 1, in   
> > > IndexError: list index out of range  
> > > closestPointId2 = loc.FindClosestPoint(list[2]) #point3

---

<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 15, 2017, 2:23pm UTC](https://discourse.slicer.org/t/calculate-a-scalp-to-scalp-distance/1141/40 "2017-12-15T14:23:39Z")

</div>

Please, could you send me your model (bones)?

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

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