How can I use Plane.FitPlaneToPoints from Python correctly?

Hi all,

the doc to this method shows, that it is possible to get the maximum deviation as an additional result.
http://4.rhino3d.com/5/rhinocommon/html/M_Rhino_Geometry_Plane_FitPlaneToPoints_1.htm
How can I get this number in Python? I’ve seen an example in VB.net by David Rutten here:
http://www.grasshopper3d.com/forum/topics/points3darepointscoplanar-1?xg
Basically he calls it like this:

Dim rc As PlaneFitResult = Plane.FitPlaneToPoints(New Point3d() {p, q, r, s}, pf, dev)

So he passes two additional “empty” parameters into the method, which are then filled by the method.
Is this not possible in Python? I get the method to work when I only pass the pointlist, but then I’m missing the maximum deviation as a result.

If someone knows how to make this work in Python I’d be glad to see how the method has to be called correctly.

Thanks in advance,
Andi

Hi Ane,

You can use the clr.StrongBox to replicate the VB.NET parameters:

import rhinoscriptsyntax as rs
import System
import Rhino
import clr

ptsIds = rs.GetObjects("pick up your points", 1)
ptsObjs = [rs.coerce3dpoint(pt) for pt in ptsIds]

plane = clr.StrongBox[Rhino.Geometry.Plane]()
deviation = clr.StrongBox[System.Double]()

planeFitResult = Rhino.Geometry.Plane.FitPlaneToPoints(ptsObjs, plane, deviation)
fitPlane = plane
maxDeviation = deviation
print maxDeviation

More simpler, maximum deviation would be the point with the maximal distance from that plane (below or above it):

import rhinoscriptsyntax as rs
import Rhino

ptsIds = rs.GetObjects("pick up your points", 1)
ptsObjs = [rs.coerce3dpoint(pt) for pt in ptsIds]

planeFitResultPlane = Rhino.Geometry.Plane.FitPlaneToPoints(ptsObjs)
fitPlane = planeFitResultPlane[1]

deviations = []
for pt in ptsObjs:
    d = fitPlane.DistanceTo(pt)
    deviations.append(abs(d))

maxDeviation = max(deviations)
print maxDeviation
1 Like

Hey djordje!

Thank you so much for that trick with the StrongBox, I’m pretty sure I never would have found that out.
Writing some extra lines of code to get maxdist would have been my next approach, but then I was kind of interested in how to make this work.
Still it is kind of obscure to me, why the out values “plane” and “deviation” need to have their type already predefined, while the return value “planeFitResult” is dynamically set to the Rhino type PlaneFitResult.
Or how I could deduct from the documentation, that it returns a tuple, when I only pass the point list. I’m glad I don’t have to make this stuff up :wink:
Thanks again for the example, this will also help in the future, when I run into a similar method.