c#-从曲面点的一段中找到圆心

我需要根据圆柱物体的多个表面测量值找到圆的中心.

我目前使用基于三点的简化算法(用C#编写)来找到中心(取自How to determine the radius and center of a circle when only three noncollinear points are known?):

private Point CircleCenter(List<Point> points, double p1Skip, double p2Skip, double p3Skip)
{
    var p1 = points.Skip((int)(points.Count * p1Skip)).First();
    var p2 = points.Skip((int)(points.Count * p2Skip)).First();
    var p3 = points.Skip((int)(points.Count * p3Skip)).First();

    double mr = (p2.Y - p1.Y) / (p2.X - p1.X);
    double mt = (p3.Y - p2.Y) / (p3.X - p2.X);

    double centerX = (mr * mt * (p3.Y - p1.Y) + mr * (p2.X + p3.X) - mt * (p1.X + p2.X)) / (2 * (mr - mt));

    double centerY = (-1 / mr) * (centerX - ((p1.X + p2.X) / 2)) + ((p1.Y + p2.Y) / 2);

    return new Point(centerX, centerY, p1.Z);
}

问题在于该方法对噪声非常敏感.如果其中一个点关闭,则当然会影响中心点.
对于每个横截面,我有640个表面点可用,并且应该可以使用3个以上的点.

我猜想现有的算法应该可以扩展更多的点,但是我不知道怎么做.

解决方法:

在我的数学大师同事的帮助下,我找到了使用最小二乘矩阵计算的解决方案.

描述的算法为here.

public static void FitCircle(IEnumerable<Point> points, out double x0, out double y0, out double r)
{
    setMklProvider();
    DenseMatrix A = DenseMatrix.Create(3, 3, (i, j) => 0);
    DenseMatrix b = DenseMatrix.Create(3, 1, (i, j) => 0);
    A[0, 0] = points.Sum(point => point.X * point.X);
    A[0, 1] = points.Sum(point => point.X * point.Y);
    A[0, 2] = points.Sum(point => point.X);
    A[1, 0] = A[0, 1];
    A[1, 1] = points.Sum(point => point.Y * point.Y);
    A[1, 2] = points.Sum(point => point.Y);
    A[2, 0] = A[0, 2];
    A[2, 1] = A[1, 2];
    A[2, 2] = points.Count();
    b[0, 0] = points.Sum(point => point.X * (point.X * point.X + point.Y * point.Y));
    b[1, 0] = points.Sum(point => point.Y * (point.X * point.X + point.Y * point.Y));
    b[2, 0] = points.Sum(point => point.X * point.X + point.Y * point.Y);
    var x = A.QR().Solve(b);
    x0 = x[0, 0] / 2;
    y0 = x[1, 0] / 2;
    r = Math.Sqrt(x[2, 0] + x0 * x0 + y0 * y0);
}

private static void setMklProvider()
{
    if (!_mklProviderSet) MathNet.Numerics.Control.LinearAlgebraProvider = new MathNet.Numerics.Algorithms.LinearAlgebra.Mkl.MklLinearAlgebraProvider();
}

该解决方案至少在我的数据上产生了非常好的可重复结果.
DenseMatrix是MathNet库的一部分.

编辑

为了进一步降低噪声,正如samgak用户建议的那样,我添加了一种迭代减少方法,以提高精度:

double x0, y0, r;
FitCircle(surfacePoints, out x0, out y0, out r);
var center = new Point(x0, y0, surfacePoints.First().Z);

int reductionIterations = 10;
var reducedSet = surfacePoints;

for (int i = 1; i < reductionIterations; i++)
{
    var orderedByDistanceToCenter = reducedSet.OrderBy(p => (p-center).GetRho()).ToList();

    reducedSet = orderedByDistanceToCenter
        .Skip((int)(orderedByDistanceToCenter.Count * (i / 10f)))
        .Take((int)(orderedByDistanceToCenter.Count - orderedByDistanceToCenter.Count * (i / 10f)*2))
        .ToList();

    // Reduced to zero, abort
    if (reducedSet.Count < 3)
        break;

    FitCircle(reducedSet, out x0, out y0, out r);
    center = new Point(x0, y0, reducedSet.First().Z);
}

public static double GetRho(this Point p) => Math.Sqrt(p.X * p.X + p.Y * p.Y);
上一篇:HDU 6242 Geometry Problem(计算几何 + 随机化)


下一篇:加快顺序检查点是否在Python中的形状中的速度