簡體   English   中英

由於浮點精度誤差導致的方格問題細分

[英]Subdividing square problems due to floating point precision errors

我有一個問題(在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

在看了你的代碼后,它沒有任何意義,它會失敗,因為據我所知,你沒有在那個失敗的情況下對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

我修好了它! 感謝所有的幫助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());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM