# vtkPlane.ComputeBestFittingPlane()

**URL:** https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081
**Category:** Support
**Tags:** python
**Created:** [June 19, 2024, 10:21am UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081 "2024-06-19T10:21:01Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![bistek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bistek/32/2344_2.png) [@bistek](https://discourse.vtk.org/u/bistek)
#### Post date: [June 19, 2024, 10:21am UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/1 "2024-06-19T10:21:01Z")

</div>

Hello, I need to fit a vtkPlane to a PolyData, but I do not understand how to use the method ComputeBestFittingPlane().

The [vtkPlane.ComputeBestFittingPlane() documentation](https://vtk.org/doc/nightly/html/classvtkPlane.html#a7b88495d93a5aa5053b1044965df67bd) says “Given a set of points calculate the best-fitting origin and normal for the plane. The origin will be the centroid of the points. The normal is determined by using the covariance matrix of the points relative to the centroid. Returns true if successful. If not successful the origin will still contain the centroid and the normal will point into z-direction.”

I implemented it as:

```auto
my_plane = Plane()
my_plane.ComputeBestFittingPlane(my_polydata.GetOutput().GetPoints())

```

But this rises the error “TypeError: ComputeBestFittingPlane() takes exactly 3 arguments (1 given)”.

Actually the documentation says:

## [◆](https://vtk.org/doc/nightly/html/classvtkPlane.html#a7b88495d93a5aa5053b1044965df67bd)ComputeBestFittingPlane()

| | static bool vtkPlane::ComputeBestFittingPlane | ( | [vtkPoints](https://vtk.org/doc/nightly/html/classvtkPoints.html) \* | _pts_, |
| --- | --- | --- | --- | --- |
| — | — | — | — | |
| | | double \* | _origin_, | |
| | | double \* | _normal_ | |
| | ) | | | |

But according to my understanding origin and normal should be the output of this method, not an input…

Could you help me please? Thanks!

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [June 19, 2024, 12:45pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/2 "2024-06-19T12:45:00Z")

</div>

You’ll need to pass in references for `origin` and `normal`. See this part of the VTK documentation on how to do that: [Python Wrappers - VTK documentation](https://docs.vtk.org/en/latest/advanced/PythonWrappers.html#pass-by-reference)

---

<div class="post-metadata">

### Author: ![bistek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bistek/32/2344_2.png) [@bistek](https://discourse.vtk.org/u/bistek)
#### Post date: [June 19, 2024, 2:17pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/3 "2024-06-19T14:17:12Z")

</div>

Hello and thanks for the really quick answer!

What I understand from your answer is that the origin and normal should be dummy objects that will be filled with actual values after the ComputeBestFittingPlane method runs.

I created the reference objects and now I get an enigmatic crash that does not rise any specific error in the PyCharm debugger.

```auto
from vtk import (...) reference

my_plane = Plane()
my_origin = reference(0.)
my_normal = reference(0.)
my_plane.ComputeBestFittingPlane(my_polydata.GetOutput().GetPoints(), my_plane, my_normal)

>>> Process finished with exit code -1073741819 (0xC0000005)

```

Both the origin and normal should be double, so it seems OK. I also tried with `[0., 0., 0.]`, thinking that they should be vectors actually, but this does not work.

Any ideas? Thanks!

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [June 19, 2024, 2:21pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/4 "2024-06-19T14:21:30Z")

</div>

> [@bistek](#):
>
> I also tried with `[0., 0., 0.]`, thinking that they should be vectors actually, but this does not work.

Try a tuple? `my_normal = reference((0, 0, 0))`

---

<div class="post-metadata">

### Author: ![bistek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bistek/32/2344_2.png) [@bistek](https://discourse.vtk.org/u/bistek)
#### Post date: [June 19, 2024, 2:27pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/5 "2024-06-19T14:27:49Z")

</div>

Both `(0., 0., 0.)` and `(0, 0, 0)` result in:

`TypeError: ComputeBestFittingPlane argument %Id: %V`

---

<div class="post-metadata">

### Author: ![bistek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bistek/32/2344_2.png) [@bistek](https://discourse.vtk.org/u/bistek)
#### Post date: [June 19, 2024, 2:33pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/6 "2024-06-19T14:33:15Z")

</div>

Maybe the problem is with giving the input points as `my_polydata.GetOutput().GetPoints()`?

It should be OK…

---

<div class="post-metadata">

### Author: ![bistek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bistek/32/2344_2.png) [@bistek](https://discourse.vtk.org/u/bistek)
#### Post date: [June 19, 2024, 4:08pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/7 "2024-06-19T16:08:24Z")

</div>

I also tried using `my_plane.GetOrigin()` and `my_plane.GetNormal()`, and `reference(my_plane.GetOrigin())` and `reference(my_plane.GetNormal())`.

`Getorigin()` and `GetNormal()` actually output a tuple, e.g. `(0.0, 0.0, 1.0)`, but in the end ComputeBestFittingPlane always results in the error `TypeError: ComputeBestFittingPlane argument %Id: %V`.

---

<div class="post-metadata">

### Author: ![hollowsunhc](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hollowsunhc/32/8400_2.png) [@hollowsunhc](https://discourse.vtk.org/u/hollowsunhc)
#### Post date: [June 19, 2024, 5:48pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/8 "2024-06-19T17:48:42Z")

</div>

if `my_polydata` is a polydata, there’s no need for `.GetOutput()`

---

<div class="post-metadata">

### Author: ![bistek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bistek/32/2344_2.png) [@bistek](https://discourse.vtk.org/u/bistek)
#### Post date: [June 19, 2024, 9:57pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/9 "2024-06-19T21:57:55Z")

</div>

Hello, Thanks! I tried also this, getting always the same error.

As input I tried:

`my_polydata` - a `vtkAppendPolydata()`

the output from the latter `my_polydata.GetOutput()`

the points `my_polydata.GetOutput().GetPoints()`.

Origin and normal are tuples with reference.

```auto
new_xs_plane_origin = reference((0., 0., 0.))
new_xs_plane_normal = reference((1., 0., 0.))

```

The debugger says they are `vtkmodules.vtkCommonCore.tuple_reference((0.0, 0.0, 1.0))`.

And this always fails:

```auto
outcome = new_xs_plane.ComputeBestFittingPlane(append_points, new_xs_plane_origin, new_xs_plane_normal)

TypeError: ComputeBestFittingPlane argument %Id: %V

```

---

<div class="post-metadata">

### Author: ![hollowsunhc](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hollowsunhc/32/8400_2.png) [@hollowsunhc](https://discourse.vtk.org/u/hollowsunhc)
#### Post date: [June 20, 2024, 5:24pm UTC](https://discourse.vtk.org/t/vtkplane-computebestfittingplane/14081/10 "2024-06-20T17:24:49Z")

</div>

`ComputeBestFittingPlane` is a static method from vtkPlane. I believe it should be called like this:

`outcome = vtkPlane.ComputeBestFittingPlane(append_points, new_xs_plane_origin, new_xs_plane_normal)`
