# vtkSphere : passing a null parameter in ComputeBoundingSphere from Python

**URL:** https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473
**Category:** Support
**Created:** [August 25, 2024, 10:37am UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473 "2024-08-25T10:37:00Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![chir-set](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chir-set/32/9213_2.png) [@chir-set](https://discourse.vtk.org/u/chir-set)
#### Post date: [August 25, 2024, 10:37am UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/1 "2024-08-25T10:37:00Z")

</div>

Hello,

I’m calling vtk.vtkSphere.ComputeBoundingSphere() from Python and stumbling on passing the hints[2] parameter. I don’t want to determine the 2 values of this parameter since vtkSphere does it on its own if a nullptr is passed. However, passing None from python fails:

`TypeError: ComputeBoundingSphere argument 4: expected a sequence of 2 values, got NoneType`

In this particular case, passing an arbitrary valid range will be OK since all points will be enclosed in the resulting sphere, and the result is tagged as approximate (comments in the cxx file).

I wish to know if there’s a way to pass a real nullptr from Python in general. Searching the web did not help.

Thank you.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [August 26, 2024, 12:37pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/2 "2024-08-26T12:37:08Z")

</div>

Hi and welcome to the group.  
I wish there were more documentation in the documentation for that method. I got this to _run_ … but I’m not entirely sure I know what I asked VTK to do.

```python
from vtkmodules.vtkCommonDataModel import vtkSphere
import numpy as np

s = vtkSphere()
points = np.random.rand(5, 3).ravel() # 5 3D points, selected at random
result = [0., 0., 0., 0.]
s.ComputeBoundingSphere(points, 5, result, [0, 1])
print(result)

```

I’m guessing that the result is (center\_x, center\_y, center\_z, radius).

---

<div class="post-metadata">

### Author: ![chir-set](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chir-set/32/9213_2.png) [@chir-set](https://discourse.vtk.org/u/chir-set)
#### Post date: [August 26, 2024, 2:23pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/3 "2024-08-26T14:23:46Z")

</div>

> [@rexthor](#):
>
> `[0, 1]`

You mean pass anything when we don’t know because we cannot say we don’t know (passing None) in Python?

Indeed, a little more precision in the documentation would have saved much time. I was about to open a merge request to change ‘[hints[2]](https://vtk.org/doc/nightly/html/classvtkSphere.html#a9c7415830155c80a3d060a743b73f8b0)’ to ‘hints[2] = nullptr’, I think now it’s superfluous.

Thank you.

> [@rexthor](#):
>
> I’m guessing that the result is (center\_x, center\_y, center\_z, radius).

Yes, that’s documented.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [August 26, 2024, 2:31pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/4 "2024-08-26T14:31:01Z")

</div>

Oh - I think in this case, I passed in `[0, 1]` because I didn’t want to bother figuring out where the farthest points might have been. In someone else’s case, where there might have been thousands or millions of points - that might have made the difference between seconds and days.

Passing references from Python to C++ is … well … not fun. I’ve tried it before in other toolkits, but VTK just makes it so easy. I think that whoever wrote this interface wanted a Python object that looks like a list … if you pass it a None, it isn’t list-like. You might try passing `[None, None]` and it might work as well.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 26, 2024, 10:39pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/5 "2024-08-26T22:39:49Z")

</div>

If you pass “nullptr” then the function will compute the point index hints in a smart way that reduces the computation time and overestimation of the bounding sphere. If you just use the first two point indices as hints (passing `[0, 1]` vector as the last parameter) as it was suggested above then the method may run much slower and the radius may be way off.

@dgobbi Is there way to pass a “nullptr” from Python as `hints` parameter to [vtkSphere::vtkSphereComputeBoundingSphere(T\* pts, vtkIdType numPts, T sphere[4], vtkIdType hints[2])](https://github.com/Kitware/VTK/blob/0e50bc44a491f9a5afb86a77a2970b2b2aa26cac/Common/DataModel/vtkSphere.cxx#L59)?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 26, 2024, 11:07pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/6 "2024-08-26T23:07:56Z")

</div>

> [@lassoan](#):
>
> Is there way to pass a “nullptr” from Python as `hints` parameter

No. Since it’s declared as “`vtkIdType hints[2]`”, the Python wrappers assume that the function does not expect it to be null. It would be unsafe to assume otherwise, unless the wrappers received some hint that the function checked for null.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 26, 2024, 11:20pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/7 "2024-08-26T23:20:23Z")

</div>

Though this won’t help people using the current version of VTK, vtkSphere should add an overload that defaults `hints` to `nullptr`:

```auto
template <class T>
void ComputeBoundingSphere(T* pts, vtkIdType numPts, double sphere[4])
{
  return ComputeBoundingSphere(pts, numPts, sphere, nullptr);
}

```

Edit: I see this has already been suggested above.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 27, 2024, 10:30pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/8 "2024-08-27T22:30:21Z")

</div>

> [@dgobbi](#):
>
> Since it’s declared as “`vtkIdType hints[2]`”, the Python wrappers assume that the function does not expect it to be null. It would be unsafe to assume otherwise,

Thank you, this makes sense.

> [@dgobbi](#):
>
> vtkSphere should add an overload that defaults `hints` to `nullptr`

@chir-set could you submit a merge request with this change?

---

<div class="post-metadata">

### Author: ![chir-set](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chir-set/32/9213_2.png) [@chir-set](https://discourse.vtk.org/u/chir-set)
#### Post date: [August 28, 2024, 9:39am UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/9 "2024-08-28T09:39:26Z")

</div>

> [@lassoan](#):
>
> could you submit a merge request with this change?

Ok, I can do that.

I request one precision.

a. Should it be an additional function to the vtkSphere class as [written](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/7) by @dgobbi ?

b. Or should it a modification of existing function signatures, replacing ‘hints[2]’ by ‘hints[2] = nullptr’?

I understand it should be #a .

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 28, 2024, 11:50am UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/10 "2024-08-28T11:50:21Z")

</div>

Yes, overloads are preferred over default arguments.

[https://docs.vtk.org/en/latest/developers\_guide/coding\_conventions.html](https://docs.vtk.org/en/latest/developers_guide/coding_conventions.html)

> [@](#):
>
> 1. Prefer overloading functions to default arguments.  
> Rationale: Default function arguments in C++ are a tempting way to add an argument to a function while maintaining easy backwards compatibility. However, if you later want to add another argument to the list in a way that preserves backwards compatibility, it, too, must be a default argument. To supply the second of these arguments in a call forces you to also supply the first argument, even if it is the default value. As a result, this is not a clean way to add a argument to a function. Insetad, function overloading should be preferred.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 28, 2024, 11:09pm UTC](https://discourse.vtk.org/t/vtksphere-passing-a-null-parameter-in-computeboundingsphere-from-python/14473/11 "2024-08-28T23:09:32Z")

</div>

I’m not sure if there is a clean way to set default for a `double[2]` argument, so I fully agree that overload is a better approach in this case. In general, I’m not sure that overloads is always preferable (and if they are preferable then not because they are better for managing backward compatibility), but that’s a separate discussion topic in itself - so I’ve added a new topic for this:

> [@Default argument value vs. overload](https://discourse.vtk.org/t/default-argument-value-vs-overload/14498):
>
> Recently the following was added to the VTK coding conventions document: Prefer overloading functions to default arguments. Rationale: Default function arguments in C++ are a tempting way to add an argument to a function while maintaining easy backwards compatibility. However, if you later want to add another argument to the list in a way that preserves backwards compatibility, it, too, must be a default argument. To supply the second of these arguments in a call forces you to also supply th…
