Seeking a point value Z plane of the surface of the three known spatial composition

Three known space, you can can determine the composition of the three flat space. At this time, according to a point X and Y values ​​to the Z value is obtained at that point in the plane. This process is especially useful for the elevation of a point or a right triangular patches required value, which itself can be seen as a linear interpolation.

The algorithm is particularly simple idea, first calculate the normal vector of the plane of the three-point composition (can be found in "Planar known three-point normal vector" ); and the planar normal vector \ (n = (A, B , C) \) peace surface at a point \ (m = (X0, yO, Z0) \) , with a point in the plane French equation:
\ [a (X--X0) + B (the Y-yO) + C (the Z-Z0) = 0 \]
Finally, according to X, Y values desire point, into equation solver Z value.

Specific codes are as follows:

#include<iostream>

using namespace std;

//三维double矢量
struct Vec3d
{
    double x, y, z;

    Vec3d()
    {
        x = 0.0;
        y = 0.0;
        z = 0.0;
    }
    Vec3d(double dx, double dy, double dz)
    {
        x = dx;
        y = dy;
        z = dz;
    }
    void Set(double dx, double dy, double dz)
    {
        x = dx;
        y = dy;
        z = dz;
    }
};

//计算三点成面的法向量
void Cal_Normal_3D(const Vec3d& v1, const Vec3d& v2, const Vec3d& v3, Vec3d &vn)
{
    //v1(n1,n2,n3);
    //平面方程: na * (x – n1) + nb * (y – n2) + nc * (z – n3) = 0 ;
    double na = (v2.y - v1.y)*(v3.z - v1.z) - (v2.z - v1.z)*(v3.y - v1.y);
    double nb = (v2.z - v1.z)*(v3.x - v1.x) - (v2.x - v1.x)*(v3.z - v1.z);
    double nc = (v2.x - v1.x)*(v3.y - v1.y) - (v2.y - v1.y)*(v3.x - v1.x);

    //平面法向量
    vn.Set(na, nb, nc);
}

void CalPlanePointZ(const Vec3d& v1, const Vec3d& v2, const Vec3d& v3, Vec3d& vp)
{
    Vec3d vn;
    Cal_Normal_3D(v1, v2, v3, vn);  

    if (vn.z != 0)              //如果平面平行Z轴
    {
        vp.z = v1.z - (vn.x * (vp.x - v1.x) + vn.y * (vp.y - v1.y)) / vn.z;         //点法式求解
    }   
}

int main()
{
    Vec3d v1(1.0, 5.2, 3.7);
    Vec3d v2(2.8, 3.9, 4.5);
    Vec3d v3(7.6, 8.4, 6.2);
    Vec3d vp;
    v3.x = 5.6;
    v3.y = 6.4;
    v3.z = 0.0;

    CalPlanePointZ(v1, v2, v3, vp);

    return 0;
}

Guess you like

Origin www.cnblogs.com/charlee44/p/12109904.html