# Compute bounding sphere

**URL:** <https://discourse.slicer.org/t/compute-bounding-sphere/4361>\
**Category:** Support\
**Created:** [October 11, 2018, 2:03pm UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361 "2018-10-11T14:03:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThomPote](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/thompote/32/2256_2.png) [@ThomPote](https://discourse.slicer.org/u/ThomPote)\
**Post date:** [October 11, 2018, 2:03pm UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/1 "2018-10-11T14:03:33Z")

</div>

Hi everybody,

Can you help me to extract bounding sphere of my segment ?  
I did it with bounding box but not with bounding sphere.  
I use CT scan and segmentation

_I’m new on Slicer it’s still difficult for me to understand all “3D-knowledges”_

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:** [October 11, 2018, 6:15pm UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/2 "2018-10-11T18:15:28Z")

</div>

I don’t think bounding spheres is implemented in any of the libraries that are bundled with Slicer. We use directed bounding box instead, because it can be computed very quickly.

Why do you need a bounding sphere?

---

<div class="post-metadata">

**Author:** ![ThomPote](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/thompote/32/2256_2.png) [@ThomPote](https://discourse.slicer.org/u/ThomPote)\
**Post date:** [October 12, 2018, 7:02am UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/3 "2018-10-12T07:02:45Z")

</div>

I just found this in vtkSphere Class Reference

function static void [ComputeBoundingSphere](https://www.vtk.org/doc/nightly/html/classvtkSphere.html#ad86cfd7f3ebd6f1d62ff06ad83aacdf5) ([float](https://www.vtk.org/doc/nightly/html/vtkVectorOperators_8h.html#a6a1bb6ed41f44b60e7bd83b0e9945aa7) \*pts, [vtkIdType](https://www.vtk.org/doc/nightly/html/vtkType_8h.html#a20bd6b6dedfe1bbb096c50354d52cc7e) numPts, [float](https://www.vtk.org/doc/nightly/html/vtkVectorOperators_8h.html#a6a1bb6ed41f44b60e7bd83b0e9945aa7) sphere[4], [vtkIdType](https://www.vtk.org/doc/nightly/html/vtkType_8h.html#a20bd6b6dedfe1bbb096c50354d52cc7e) hints[2])

But I was in troubles with arguments to use it, and slicer-app crashes …

**Do you know what is exactly the type of data required with argument 1 “Points” ?**

I still have my segment :

n = getNode(‘Segmentation’)  
s = n.GetSegmentation()  
ssid = s.GetSegmentIdBySegmentName(‘mysegment’)  
ss = s.GetSegment(ssid)  
pd = ss.GetRepresentation(‘Closed surface’)

In fact I have a volume which looks like a half-sphere and I want to have the sphere center.  
I can’t use the center of gravity here because the center of sphere and half-sphere is not same.

Thanks @lassoan

---

<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 13, 2018, 5:57pm UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/4 "2018-10-13T17:57:41Z")

</div>

Thanks for the additional information. It is now clear that you need high-accuracy sphere fitting - this rules out most of the fast implementations (that only provide approximate results).

There are still many options how to approach this. Optimal method depends on what are the next steps and how much and what kind of interactions and manual processing do you plan to do.

Would you like to implement automatic sizing and positioning of hip implants? Do you plan to segment the femoral head or you would place points on the bone surface on a number of slices?

---

<div class="post-metadata">

**Author:** ![ThomPote](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/thompote/32/2256_2.png) [@ThomPote](https://discourse.slicer.org/u/ThomPote)\
**Post date:** [October 15, 2018, 1:11pm UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/6 "2018-10-15T13:11:54Z")

</div>

Hi,

Thanks it’s ok for me for the function now.

I just try to analyze segments by myself for the moment.

---

<div class="post-metadata">

**Author:** ![ThomPote](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/thompote/32/2256_2.png) [@ThomPote](https://discourse.slicer.org/u/ThomPote)\
**Post date:** [October 17, 2018, 8:25am UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/7 "2018-10-17T08:25:39Z")

</div>

I’m still in troubles with argument 4 “hints” now

I can’t use None for arg4 when I call the function, it’s said hints are required in error message

In code source I can see that an else condition can execute script without “hints”

```
void vtkSphereComputeBoundingSphere(T *pts, vtkIdType numPts, T sphere[4],
                                    vtkIdType hints[2])
{
  [...]
  }

  vtkIdType i;
  T *p, d1[3], d2[3];
  if ( hints )
  {
   [...]
  }
  else //no hints provided, compute an initial guess
{ [...]

```

Normally without hints the function has to find the points that span the greatest distance on the x-y-z axes and define a sphere centered between the two points.

Can you help me ?

---

<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 17, 2018, 12:48pm UTC](https://discourse.slicer.org/t/compute-bounding-sphere/4361/8 "2018-10-17T12:48:01Z")

</div>

You can fit spheres using various calibration and registration methods that are already available in Slicer but it is not possible to recommend a good choice without knowing what specific input and constraints you have. So, it would be useful if you could tell us the big picture - what you would like to achieve - and we can help you to get you there. Also, most likely several people have done very similar things that you would like to do, so they can share their experience - what worked for them and what not.

For example, you can fit a generic sphere model using SlicerIGT extension. Create a sphere model using CreeateModel module then register it to another model’s points using SlicerIGT extension’s Model registration module, using Similarity method. The resulting transform gives you the sphere’s origin and relative scale to the input sphere model. This may or may not be the ideal method, depending on your end goal.
