length of Bezier curve

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
matt
Posts: 2
Joined: Mon Jul 17, 2017 2:26 pm

length of Bezier curve

Post by matt »

Hi,

I want to calculate the length of an Bezier curve

Code: Select all

import FreeCAD
import Part

P1=FreeCAD.Vector(0,1)
P2=FreeCAD.Vector(0,0)
P3=FreeCAD.Vector(1,0)

b = Part.BezierCurve()
b.setPoles([P1,P2,P3])
#b.setWeight(1,5) # --> difference in length increases = 0.0026

# length from Part.BezierCurve().length()
print(b.length())  # = 1.623225240139941

# length from .toShape().Length
s = b.toShape()
print(s.Length)  # = 1.6233215898436886

# difference
print(b.length() - s.Length)  # = -9.634970374761131e-05
why is there a difference between Part.BezierCurve().length() and Part.BezierCurve().toShape().Length? What is the "true" length?

Matthias
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: length of Bezier curve

Post by wmayer »

When looking at the source code then in both cases the same algorithm is used to determine the length. However, the method GCPnts_AbscissaPoint::Length has several overloaded variants where in one version you can only pass the curve and in another version you can pass the curve and a certain tolerance (be default 1e-7). It seems that for the former a tolerance is auto-computed that is a bit higher than the 1e-7 and thus a coarser approximation is created.

So, the true length should be: b.length()

I will change the code of the edge class to return the same result as of the curve class.
Post Reply