fit_circle.py 985 B

12345678910111213141516171819202122232425262728293031323334
  1. """Fit circles analytically to measure
  2. the signed curvature of a line"""
  3. from vedo import *
  4. shape = Spline([
  5. [1.0, 2.0, -1.0],
  6. [1.5, 0.0, 0.4],
  7. [2.0, 4.0, 0.5],
  8. [4.0, 1.5, -0.3]],
  9. res=200,
  10. )
  11. n = 5 # nr. of points to use for the fit
  12. npt = shape.npoints
  13. points = shape.points
  14. fitpts, circles, curvs = [], [], [0]*npt
  15. for i in range(n, npt - n-1):
  16. pts = points[i-n:i+n]
  17. center, R, normal = fit_circle(pts)
  18. z = cross(pts[-1]-pts[0], center-pts[0])[2]
  19. curvs[i] = sqrt(1/R) * z/abs(z)
  20. if R < 0.75:
  21. circle = Circle(center, r=R).wireframe()
  22. circle.reorient([0,0,1], normal)
  23. circles.append(circle)
  24. fitpts.append(center)
  25. shape.lw(8).cmap('coolwarm', curvs).add_scalarbar3d(title=':pm1/:sqrtR', c='w')
  26. # use this trick to make the scalarbar3d become a 2d screen object:
  27. shape.scalarbar = shape.scalarbar.clone2d("bottom-right", 0.2)
  28. show(shape, circles, Points(fitpts, c='white'), __doc__, axes=1, bg='bb').close()