Fit a sphere to an arbitrary set of points.
(points)
| 1106 | |
| 1107 | |
| 1108 | def _fit_sphere(points): |
| 1109 | """Fit a sphere to an arbitrary set of points.""" |
| 1110 | # linear least-squares sphere fit, see for example |
| 1111 | # https://stackoverflow.com/a/78909044 |
| 1112 | # TODO: At some point we should maybe reject outliers first... |
| 1113 | A = np.c_[2 * points, np.ones((len(points), 1))] |
| 1114 | b = (points**2).sum(axis=1) |
| 1115 | x, _, _, _ = np.linalg.lstsq(A, b, rcond=1e-6) |
| 1116 | origin = x[:3] |
| 1117 | radius = np.sqrt(x[0] ** 2 + x[1] ** 2 + x[2] ** 2 + x[3]) |
| 1118 | return radius, origin |
| 1119 | |
| 1120 | |
| 1121 | def _check_origin(origin, info, coord_frame="head", disp=False): |