判断点是否在三角形内的算法精度问题

栏目: 编程工具 · 发布时间: 5年前

内容简介:今天一个同事反应,在使用具体是他给出了一组测试参数,abc 三点为 {261.137939, 8.13000488} , {73.6379318, 8.13000488}, {76.9379349, 10.2300053} ,测试 p 为 {74.4069519 , 8.6193819 } 应该在这个三角形内,但是这个函数计算出来并不是。

今天一个同事反应,在使用 recastnavigation 库时,判断一个点是否在一个三角形内,遇到了精度问题,而且精度误差很大。

具体是 dtClosestHeightPointTriangle 这个函数。

他给出了一组测试参数,abc 三点为 {261.137939, 8.13000488} , {73.6379318, 8.13000488}, {76.9379349, 10.2300053} ,测试 p 为 {74.4069519 , 8.6193819 } 应该在这个三角形内,但是这个函数计算出来并不是。

我看了一下源代码,把这个函数提出来,改写了一点点,方便独立测试。

#include <stdio.h>

// #define float double

static float
dtVdot2D(float v0[2], float v1[2]) {
    return v0[0] * v1[0] + v0[1] * v1[1];
}

static float *
dtVsub(float p[2], float v0[2], float v1[2]) {
    p[0] = v0[0] - v1[0];
    p[1] = v0[1] - v1[1];
    return p;
}

static int
dtClosestHeightPointTriangle(float p[2], float a[2], float b[2],float c[2], float *h) {
    float v0[2], v1[2], v2[2];

    dtVsub(v0, c,a);
    dtVsub(v1, b,a);
    dtVsub(v2, p,a);

    float dot00 = dtVdot2D(v0, v0);
    float dot01 = dtVdot2D(v0, v1);
    float dot02 = dtVdot2D(v0, v2);
    float dot11 = dtVdot2D(v1, v1);
    float dot12 = dtVdot2D(v1, v2);

    // Compute barycentric coordinates
    float InvDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
    float u = (dot11 * dot02 - dot01 * dot12) * InvDenom;
    float v = (dot00 * dot12 - dot01 * dot02) * InvDenom;

    // The (sloppy) epsilon is needed to allow to get height of points which
    // are interpolated along the edges of the triangles.
    float EPS = 1e-4f;

    // If point lies inside the triangle, return interpolated ycoord.
    if (u >= -EPS && v >= -EPS && (u+v) <= 1+EPS) {
        *h = a[1] + (v0[1]*u + v1[1]*v);
        return 1;
    }
    return 0;
}

int
main() {
    float a[2] = {261.137939, 8.13000488};
    float b[2] = {73.6379318, 8.13000488};
    float c[2] = {76.9379349, 10.2300053};
    float p[2] = {74.4069519 , 8.6193819 };
    float h;

    int r = dtClosestHeightPointTriangle(p, a, b, c, &h);

    printf("%d %f\n", r, h);

    return 0;
}

如果你在前面加上 #define float double ,把所有 float 换成双精度,那么测试是可以通过的。

我认为问题出在 dot00 * dot11 - dot01 * dot01 这样的运算上。dot00 点乘已经是单个量的平方,在测试数据中,大约这个量会是 261 - 73 = 188 ,小数点前大约是 8bit 的信息含量,如果我们计算 dot00 * dot11 ,差不多会得到一个这个量的 4 次方的结果,也就是 28bit ~ 32bit 之间。

但是 float 本身的有效精度才 23bit ,对一个 2^32 的数字做加减法,本身的误差就可能在 2 ~ 2^9 左右,这个误差是相当巨大的。

这段程序一个明显可以改进的地方是把乘 InvDenom 从 u v 中去掉。那么代码应该写成:

float Denom = (dot00 * dot11 - dot01 * dot01);
    float u = (dot11 * dot02 - dot01 * dot12);
    float v = (dot00 * dot12 - dot01 * dot02);

    float EPS = 1e-4f * Denom ;

    // If point lies inside the triangle, return interpolated ycoord.
    if (u >= -EPS && v >= -EPS && (u+v) <= Denom+EPS) {
        *h = a[1] + (v0[1]*u + v1[1]*v) / Denom;
        return 1;
    }

光这样写还是不够,其实我们应该进一步把 dot00 * dot11 - dot01 * dot01 展开为 (v0[0] * v1[1] - v0[1] * v1[0]) * (v0[0] * v1[1] - v1[0] * v0[1]) 。这样,就不会在四次方的基础上再做加减法,而是在二次方的基础上先做加减,再做乘法。这样就最大化的保持了精度。

我优化过的函数是这样的:

static int
dtClosestHeightPointTriangle(float p[2], float a[2], float b[2],float c[2], float *h) {
    float v0[2], v1[2], v2[2];

    dtVsub(v0, c,a);
    dtVsub(v1, b,a);
    dtVsub(v2, p,a);

    float Denom = (v0[0] * v1[1] - v0[1] * v1[0]) * (v0[0] * v1[1] - v1[0] * v0[1]);
    float EPS = - 1e-4f * Denom;

    float u = (v1[0] * v2[1] - v1[1] * v2[0]) * (v1[0] * v0[1] - v0[0] * v1[1]);
    if (u < EPS)
        return 0;

    float v = (v0[0] * v2[1] - v0[1] * v2[0]) * (v0[0] * v1[1] - v1[0] * v0[1]);

    if (v >= EPS && (u+v) <= Denom - EPS) {
        *h = a[1] + (v0[1]*u + v1[1]*v) / Denom;
        return 1;
    }
    return 0;
}

最后这个优化版本可以通过测试。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

最优状态估计

最优状态估计

[美] D. 西蒙 / 张勇刚、李宁、奔粤阳 / 国防工业出版社 / 2013-5-1 / 68.00元

《最优状态估计——卡尔曼H∞及非线性滤波》共分为四个部分,全面介绍了最优状态估计的理论和方法。第1部分为基础知识,回顾了线性系统、概率论和随机过程相关知识,介绍了最小二乘法、维纳滤波、状态的统计特性随时间的传播过程。第2部分详细介绍了卡尔曼滤波及其等价形式,介绍了卡尔曼滤 波的扩展形式,包括相关噪声和有色噪声条件下的卡尔曼滤波、稳态滤波、衰减记忆滤波和带约束的卡尔 曼滤波等。第3部分详细介绍了H∞......一起来看看 《最优状态估计》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具