Hi all
I have tried to test the two algorithms explained here, plus a third algorithm that, after dividing the curve, discards even points and only uses odd points …
It’s only a quick test, but I get pretty different iteration count ( and the points seem a little apart from each other )
( I tried it onto a half circle )
… But I suspect that the script be somewhat buggy … particularly on the second algorithm, maybe it’s not written correctly …
First algorithm is the one from Mitch’s script
Second is the one explained by David
Third is the one that uses odd points only
Should someone happen to see some bug here, please tell me about it, thanks …
BTW Thanks Mitch for teaching me about reduce() !
import Rhino
import scriptcontext
import System
def cent1( cur, cnt ):
'''
algorithm 1
'''
pars = cur.DivideByCount( cnt, True )
pts = [ cur.PointAt( par ) for par in pars ]
cen = reduce( lambda a, b : a + b, pts ) / len( pts )
return cen
def cent2( cur, cnt ):
'''
algorithm 2
'''
pars = cur.DivideByCount( cnt, True )
pts = [ cur.PointAt( par ) for par in pars ]
pts[ 0 ] /= 2
pts[ -1 ] /= 2
cen = reduce( lambda a, b : a + b, pts ) / len( pts )
return cen
def cent3( cur, cnt ):
'''
algorithm 3
'''
pars = cur.DivideByCount( cnt, True )
pars = pars[ 1 : : 2 ]
pts = [ cur.PointAt( par ) for par in pars ]
cen = reduce( lambda a, b : a + b, pts ) / len( pts )
return cen
def main():
gob = Rhino.Input.Custom.GetObject()
gob.AcceptNothing( True )
gob.SetCommandPrompt( 'Curve for centroid ?' )
gob.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
gob.Get()
res = gob.Result()
if res == Rhino.Input.GetResult.Cancel:
return
elif res == Rhino.Input.GetResult.Object:
obref = gob.Object( 0 )
cur = obref.Curve()
else:
return
tolr = 0.001
cnt = 4
prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
itr = 0
att = Rhino.DocObjects.ObjectAttributes()
att.ObjectColor = System.Drawing.Color.Red
att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
while True:
scriptcontext.escape_test()
itr += 1
cen = cent1( cur, cnt )
if cen.DistanceTo( prev ) < tolr:
print 'Algorithm 1: %d iterations' % itr
Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
break
prev = cen
cnt += 2
cnt = 4
prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
itr = 0
att = Rhino.DocObjects.ObjectAttributes()
att.ObjectColor = System.Drawing.Color.Green
att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
while True:
scriptcontext.escape_test()
itr += 1
cen = cent2( cur, cnt )
if cen.DistanceTo( prev ) < tolr:
print 'Algorithm 2: %d iterations' % itr
Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
break
prev = cen
cnt += 2
cnt = 4
prev = Rhino.Geometry.Point3d( 1.0e99, 0, 0 )
itr = 0
att = Rhino.DocObjects.ObjectAttributes()
att.ObjectColor = System.Drawing.Color.Blue
att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
while True:
scriptcontext.escape_test()
itr += 1
cen = cent3( cur, cnt )
if cen.DistanceTo( prev ) < tolr:
print 'Algorithm 3: %d iterations' % itr
Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint( cen, att )
break
prev = cen
cnt += 2
# Rhino.RhinoDoc.ActiveDoc.Views.Redraw()
main()