由于浮点精度误差导致的方格问题细分

时间:2015-11-03 08:19:36

标签: java floating-point geometry

我有一个问题(在JAVA中)由于浮点精度错误而无法解决。

我有一个轴对齐的方形类,它通过指定2个点来定义。在构造函数中,确定最大距离是什么(在x方向或y方向上),并且这用于创建正方形,其中点的距离等于中心的距离。这意味着这些点位于边界上。

例如,如果我定义一个带有点(0,2)和(3,3)的正方形,则最大距离是x距离(3),正方形将定义如下:

  • 左下点=(0,1)
  • 右上角点=(3,4)

可以看出,这些点位于边缘,点的中点正好是方形的中心(1.5,2.5)。

我创建了一个方法来检查方块是否包含某个点,如下所示:

see added code sample below

这意味着如果一个点在边界上,它就会被“包含”。

现在我想将方块细分为4个'同等'大小的部分(NorthEast,NorthWest,SouthWest和SouthEast),逻辑上定义原始正方形的初始点必须至少包含1个部分。但是当单元测试时,随机指向它失败并且它看起来像是因为双精度浮点错误。

我尝试了不同的解决方法,最后一次迭代如下:

  • 使用初始点来定义正方形
  • 的中点(p0)
  • 使用初始点+中点来定义正方形的4个角点(顺时针方向的p1,p2,p3 p4,从左下角开始)

这可以保证包含原始点,并且我可以轻松地创建细分,并且我思想如果我确保没有执行数学运算,它保证原始点至少包含1个部分关于定义正方形的点的特征(因为它们在边界上)。我的细分程序如下:

see added code sample below

但是当使用随机点运行大量迭代的单元测试时,差不多16个仍然失败,我不知道为什么边缘点会发生变化。在所有这些测试中,初始收容检查(无论父方是否包含点,尽管它们位于边缘)通过100%。

修改 一些实际的代码显示了我的实现:

public class Point implements IPoint {
    double x, y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public double x() {
        return x;
    }

    @Override
    public double y() {
        return y;
    }

    @Override
    public IPoint midPoint(IPoint other) {
        return new Point((x() + other.x()) / 2, (y() + other.y()) / 2);
    }
}

public class Rectangle implements IRectangle {
    IPoint p0, p1, p2, p3, p4;

    public Rectangle(IPoint v1, IPoint v2) {
        double dx, dy, dl;
        IPoint v0;

        // calculate dominant length
        dx = Math.abs(v1.x() - v2.x());
        dy = Math.abs(v1.y() - v2.y());
        dl = dx >= dy ? dx : dy;

        if (dx >= dy) {
            // make sure v0 = left-most
            if (v1.x() <= v2.x()) {
                v0 = v1;
                v1 = v2;
            } else {
               v0 = v2;
            }
        } else {
            // make sure v0 = bottom-most
            if (v1.y() <= v2.y()) {
                v0 = v1;
                v1 = v2;
            } else {
                v0 = v2;
            }
        }

        this.p0 = v0.midPoint(v1);
        if (dx >= dy) {
            // this way v0 and v1 are always on the vertical boundaries
            this.p1 = new Point(v0.x(), this.p0.y() - dl / 2);
            this.p2 = new Point(v0.x(), this.p0.y() + dl / 2);
            this.p3 = new Point(v1.x(), this.p0.y() + dl / 2);
            this.p4 = new Point(v1.x(), this.p0.y() - dl / 2);
        } else {
            // this way v0 and v1 are always on the horizontal boundaries
            this.p1 = new Point(this.p0.x() - dl / 2, v0.y());
            this.p2 = new Point(this.p0.x() - dl / 2, v1.y());
            this.p3 = new Point(this.p0.x() + dl / 2, v1.y());
            this.p4 = new Point(this.p0.x() + dl / 2, v0.y());
        }
    }

    @Override
    public boolean contains(IPoint p) {
        if (p.x() < p1.x() || p.x() > p4.x()) return false;
        if (p.y() < p1.y() || p.y() > p2.y()) return false;
        return true;
    }

    @Override
    public IRectangle[] subdivide() {
        return new Rectangle[] {
            new Rectangle(p0, p2),
            new Rectangle(p0, p3),
            new Rectangle(p0, p4),
            new Rectangle(p0, p1)
        };
    }
}

以下是测试用例:

@Test
public void testMassedSubdivide() throws Exception {
    Random r = new Random();
    IPoint p1, p2;
    IRectangle[] rects;
    boolean check1, check2;
    for (int i = 0; i < 100000; i++) {
        p1 = new Point(r.nextDouble(), r.nextDouble());
        p2 = new Point(r.nextDouble(), r.nextDouble());
        q = new Rectangle(p1, p2);
        assertTrue(q.contains(p1));
        assertTrue(q.contains(p2));

        rects = q.subdivide();
        check1 = rects[0].contains(p1) || rects[1].contains(p1) || rects[2].contains(p1) || rects[3].contains(p1);
        check2 = rects[0].contains(p2) || rects[1].contains(p2) || rects[2].contains(p2) || rects[3].contains(p2);
        assertTrue(check1);
        assertTrue(check2);
    }
}

我的随机测试导致的一个失败案例:

p1 = (0.31587198758298796,  0.12796964677511913)
p2 = (0.04837609765424089,  0.6711236142940149)

这个失败了,因为p1应该在SouthEast扇区,但那个被定义为:

p0=(0.31791253449833834,    0.2637581386548431),    
p1=(0.18212404261861442,    0.12796964677511916), <- wrong, last 6 should be 3  
p2=(0.18212404261861442,    0.39954663053456707),   
p3=(0.4537010263780623,     0.39954663053456707),   
p4=(0.4537010263780623,     0.12796964677511916)  <- wrong, last 6 should be 3

2 个答案:

答案 0 :(得分:4)

在查看您的代码后,它没有任何意义,它会失败,因为据我所知,您没有对该失败案例中的Y值应用任何操作 - 你只是在没有任何操作的情况下通过它,所以浮点精度损失是无关紧要的。虽然p1 - p4可以代表任何一个角落,但我有点难以理解,所以我重写了Rectangle类,希望有点清楚:

public class Rectangle implements IRectangle {
    IPoint centroid, bottomLeft, topLeft, bottomRight, topRight;

    public Rectangle(IPoint v0, IPoint v1) {
        IPoint bottomLeft = new Point(Math.min(v0.x, v1.x), Math.min(v0.y, v1.y));
        IPoint topRight   = new Point(Math.max(v0.x, v1.x), Math.max(v0.y, v1.y));

        // calculate dominant length
        double dx = topRight.x - bottomLeft.x;
        double dy = topRight.y - bottomLeft.y;  // Assumes (0, 0) is in the bottom-left.
        double dl = dx >= dy ? dx : dy;

        this.centroid = bottomLeft.midPoint(topRight);
        if (dx >= dy) {
            // this way bottomLeft and topRight are always on the vertical boundaries
            this.bottomLeft  = new Point(bottomLeft.x(), this.centroid.y() - dl / 2);
            this.topLeft     = new Point(bottomLeft.x(), this.centroid.y() + dl / 2);
            this.bottomRight = new Point(topRight.x(), this.centroid.y() - dl / 2);
            this.topRight    = new Point(topRight.x(), this.centroid.y() + dl / 2);
        } else {
            // this way bottomLeft and topRight are always on the horizontal boundaries
            this.bottomLeft  = new Point(this.centroid.x() - dl / 2, bottomLeft.y());
            this.topLeft     = new Point(this.centroid.x() - dl / 2, topLeft.y());
            this.bottomRight = new Point(this.centroid.x() + dl / 2, bottomLeft.y());
            this.topRight    = new Point(this.centroid.x() + dl / 2, topLeft.y());
        }
    }

    @Override
    public boolean contains(IPoint p) {
        if (p.x() < bottomLeft.x() || p.x() > topRight.x()) return false;
        if (p.y() < bottomLeft.y() || p.y() > topRight.y()) return false;
        return true;
    }

    @Override
    public IRectangle[] subdivide() {
        return new Rectangle[] {
            new Rectangle(centroid, bottomLeft),
            new Rectangle(centroid, topLeft),
            new Rectangle(centroid, bottomRight),
            new Rectangle(centroid, topRight)
        };
    }
}

如果这还没有解决问题,可能会有一些日志记录会在0.12796964677511913更改为0.12796964677511916

时触发

答案 1 :(得分:1)

我修好了!感谢所有的帮助0x24a537r9,因为它使它更清晰。我添加了你的代码(并修正了一个拼写错误)但我们仍然错过了一个特殊情况,即它是一个完美的正方形,因此,dx == dy。

如果dx == dy,我们知道正方形的所有点,并且可以在不使用dl的情况下添加它们。在我的失败案例中,它是一个完美的正方形,因此它将在第一个 if 子句中结束,因此使用 dl 来计算2个新的角点(这将导致在浮点错误)。

逻辑上它最终将处于一个正方形状态,因为我强迫它变成方形......

我的最终构造函数代码如下:

IPoint centroid, bottomLeft, topLeft, topRight, bottomRight;

public Rectangle(IPoint v0, IPoint v1) {
    IPoint bottomLeft = new Point(Math.min(v0.x(), v1.x()), Math.min(v0.y(), v1.y()));
    IPoint topRight   = new Point(Math.max(v0.x(), v1.x()), Math.max(v0.y(), v1.y()));

    // calculate dominant length
    double dx = topRight.x() - bottomLeft.x();
    double dy = topRight.y() - bottomLeft.y();  // Assumes (0, 0) is in the bottom-left.
    double dl = dx >= dy ? dx : dy;

    this.centroid = bottomLeft.midPoint(topRight);
    if (dx == dy)  // special case where it is square <- important, because this one fixes the errors
    {
        this.bottomLeft = bottomLeft;
        this.topLeft = new Point(bottomLeft.x(), topRight.y());
        this.topRight = topRight;
        this.bottomRight = new Point(topRight.x(), bottomLeft.y());
    }
    else if (dx >= dy) {
        // this way bottomLeft and topRight are always on the vertical boundaries
        this.bottomLeft  = new Point(bottomLeft.x(), this.centroid.y() - dl / 2);
        this.topLeft     = new Point(bottomLeft.x(), this.centroid.y() + dl / 2);
        this.bottomRight = new Point(topRight.x(), this.centroid.y() - dl / 2);
        this.topRight    = new Point(topRight.x(), this.centroid.y() + dl / 2);
    } else {
        // this way bottomLeft and topRight are always on the horizontal boundaries
        this.bottomLeft  = new Point(this.centroid.x() - dl / 2, bottomLeft.y());
        this.topLeft     = new Point(this.centroid.x() - dl / 2, topRight.y());
        this.bottomRight = new Point(this.centroid.x() + dl / 2, bottomLeft.y());
        this.topRight    = new Point(this.centroid.x() + dl / 2, topRight.y());
    }
}