# Point inside a closed surface/centre

**URL:** <https://discourse.slicer.org/t/point-inside-a-closed-surface-centre/9592>\
**Category:** Support\
**Created:** [December 23, 2019, 1:54pm UTC](https://discourse.slicer.org/t/point-inside-a-closed-surface-centre/9592 "2019-12-23T13:54:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jainey](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jainey](https://discourse.slicer.org/u/Jainey)\
**Post date:** [December 23, 2019, 1:54pm UTC](https://discourse.slicer.org/t/point-inside-a-closed-surface-centre/9592/1 "2019-12-23T13:54:42Z")

</div>

I m trying to get the centre inside a closed surface/model.  
Is there a code for this please

Thanks

---

<div class="post-metadata">

**Author:** ![Jainey](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jainey](https://discourse.slicer.org/u/Jainey)\
**Post date:** [December 23, 2019, 2:16pm UTC](https://discourse.slicer.org/t/point-inside-a-closed-surface-centre/9592/2 "2019-12-23T14:16:23Z")

</div>

I tried using this document- did not work

```python
segmentationNode = getNode('Segmentation')
segmentId = 'Segment_1'

# Get array voxel coordinates
import numpy as np
seg=arrayFromSegment(segmentation_node, segmentId)
# numpy array has voxel coordinates in reverse order (KJI instead of IJK)
# and the array is cropped to minimum size in the segmentation
mean_KjiCropped = [coords.mean() for coords in np.nonzero(seg)]

# Get segmentation voxel coordinates
segImage = segmentationNode.GetBinaryLabelmapRepresentation(segmentId)
segImageExtent = segImage.GetExtent()
# origin of the array in voxel coordinates is determined by the start extent
mean_Ijk = [mean_KjiCropped[2], mean_KjiCropped[1], mean_KjiCropped[0]] + np.array([segImageExtent[0], segImageExtent[2], segImageExtent[4]])

# Get segmentation physical coordinates
ijkToWorld = vtk.vtkMatrix4x4()
segImage.GetImageToWorldMatrix(ijkToWorld)
mean_World = [0, 0, 0, 1]
ijkToRas.MultiplyPoint(np.append(mean_Ijk,1.0), mean_World)
mean_World = mean_World[0:3]

# If segmentation node is transformed, apply that transform to get RAS coordinates
transformWorldToRas = vtk.vtkGeneralTransform()
slicer.vtkMRMLTransformNode.GetTransformBetweenNodes(segmentationNode.GetParentTransformNode(), None, transformWorldToRas)
mean_Ras = transformWorldToRas.TransformPoint(mean_World)

# Show mean position value and jump to it in all slice viewers
print(mean_Ras)
slicer.modules.markups.logic().JumpSlicesToLocation(mean_Ras[0], mean_Ras[1], mean_Ras[2], True

```

Thanks

---

<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:** [December 23, 2019, 2:20pm UTC](https://discourse.slicer.org/t/point-inside-a-closed-surface-centre/9592/3 "2019-12-23T14:20:49Z")

</div>

What do you mean by “center”?

Center of gravity, center of coordinate system axis aligned bounding box, center of oriented bounding box, center of largest largest sphere that fits inside the model, etc.?

Most of these center points are not guaranteed to be inside the closed surface.

What would you like to use this for?

The code above is for segmentation nodes. If you have a model node then you need to import to segmentation node first. Note that segmentation nodes already have a “jump to segment” feature (you can try it by right clicking on the segment in Segmentations or Segment Editor module).

---

<div class="post-metadata">

**Author:** ![Jainey](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jainey](https://discourse.slicer.org/u/Jainey)\
**Post date:** [December 23, 2019, 2:44pm UTC](https://discourse.slicer.org/t/point-inside-a-closed-surface-centre/9592/4 "2019-12-23T14:44:04Z")

</div>

I am looking to calculate the centre of mass, Thanks
