本文介绍了计算 3D 网格的表面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由顶点和三角形定义的 3D 网格.我也有网格的法线.我想计算网格的面积,假设它总是关闭的.我在 这个问题,我在 C 代码中应用它来构建一个由 R 调用的函数.这是代码:

I have a 3D mesh defined by verteces and triangles. I have also normals of the mesh. I'd like to calculate the area of the mesh, assuming it's always closed. I found an interesting implementation of calculation of the 3D volume in this question, and I applied it in a C code to build a function called by R. This is the code:

double SignedVolumeOfTriangle(double p1X, double p1Y, double p1Z,
        double p2X, double p2Y, double p2Z, double p3X, double p3Y, double p3Z) {
    double v321 = p3X*p2Y*p1Z;
    double v231 = p2X*p3Y*p1Z;
    double v312 = p3X*p1Y*p2Z;
    double v132 = p1X*p3Y*p2Z;
    double v213 = p2X*p1Y*p3Z;
    double v123 = p1X*p2Y*p3Z;
    return (double)(1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123);
}
void MeshVolume(double *X, double *Y, double *Z, int *numT, int *V1, int *V2, int *V3, double *Volume) {
    int n;
    *Volume=0;
    for (n=0; n<*numT; n++) {
        *Volume = *Volume + SignedVolumeOfTriangle(X[V1[n]], Y[V1[n]], Z[V1[n]], X[V2[n]], Y[V2[n]], Z[V2[n]], X[V3[n]], Y[V3[n]], Z[V3[n]]);
    }
    *Volume = fabs(*Volume);
}

无论是在问题中还是在链接的文章中,我都找到了计算网格面积的算法.有人可以帮我吗?

Neither in the question nor in the article linked I found the algorithm for calculating the Area of the mesh. Is there anybody can help me please?

推荐答案

您有一个封闭的体积,其表面由三角形组成.所有三角形都对外表面有贡献.对吗?

You have a closed volume whose surface is made up by triangles. And all triangles contribute to the outer surface. right?

PQR之间的三角形的表面可以通过以下方式获得:

The surface of a triangle between points P, Q and R can be obtained by:

A = 0.5 * |PQ × PR|
  = 0.5 * |PQ| * |PR| * sin(Ɵ)

哪里

PQ = Q - P
PR = R - P

×表示交叉产品Ɵ 是向量之间的角度.(叉积的结果向量的大小是两个原始向量之间的平行四边形的面积.其中一半是三角形的面积.)

and × denotes the cross product and Ɵ is the angle between the vectors. (The magnitude of the resulting vector of a cross product is the area of a parallelogramme between the two original vectors. Half of that is the area of a triangle.)

对所有三角形的面积求和.不需要取绝对值,因为面积只能为零或正.所以:

Sum the aeras of all triangles. There's no need to take the absolute value, because the area can only be zero or positive. So:

double AreaOfTriangle(double p1X, double p1Y, double p1Z,
        double p2X, double p2Y, double p2Z,
        double p3X, double p3Y, double p3Z)
{
    double ax = p2x - p1x;
    double ay = p2y - p1y;
    double az = p2z - p1z;
    double bx = p3x - p1x;
    double by = p3y - p1y;
    double bz = p3z - p1z;
    double cx = ay*bz - az*by;
    double cy = az*bx - ax*bz;
    double cz = ax*by - ay*bx;

    return 0.5 * sqrt(cx*cx + cy*cy + cz*cz);
}

void MeshSurface(double *X, double *Y, double *Z,
    int *numT, int *V1, int *V2, int *V3, double *Area)
{
    int n;

    *Area = 0.0;

    for (n=0; n<*numT; n++) {
        *Area += AreaOfTriangle(X[V1[n]], Y[V1[n]], Z[V1[n]],
            X[V2[n]], Y[V2[n]], Z[V2[n]],
            X[V3[n]], Y[V3[n]], Z[V3[n]]);
    }
}

这篇关于计算 3D 网格的表面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:30