Vtk-投影到面

(一) 单个点投影到面

#include <vtkSmartPointer.h>
#include <vtkPlane.h>
int main(int argc, char *argv[]) {
    double projected[3];//这里放投影后的点的坐标
    double x[3] = {26.8539, 24.0056, 9.71558}; //待投影的点
    double ori[3] = {27.4805, 24.1368, 11.1816};//d点
    double nor[3] = {-0.609361, 0.7694, 0.19158};//法向量
    //创建投影平面----------------
    vtkSmartPointer<vtkPlane> plane2 = vtkSmartPointer<vtkPlane>::New();
    plane2->SetOrigin(ori);
    plane2->SetNormal(nor);//点法式构成了一个平面plane2
    plane2->ProjectPoint(x, projected);
    cout << *plane2 << endl;
    std::cout << "Projected: " << projected[0] << " " << projected[1] << " " << projected[2] << std::endl;
    return EXIT_SUCCESS;
}


(二) 点集投影到面

    //-----------------------------------第一个输入target--//第一层投影后的其实是金标准----------------------------------------------------------------
    std::string filename = "../data/test/1";//
    std::ifstream filestream(filename.c_str()); //文件流
    std::string line;

    vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
    while(std::getline(filestream, line)) { //整行读取文件
        double x, y, z;
        int i = 0;
        std::stringstream linestream;
        linestream << line;
        linestream >> x >> y >> z;
        m_Points->InsertNextPoint(x, y, z);  //新读取的数据赋予点的几何结构
    }
    filestream.close();  //关闭文件流操作

    //
    vtkSmartPointer<vtkPolyData> VesselPointsPolyData = vtkSmartPointer<vtkPolyData>::New();
    VesselPointsPolyData->SetPoints(m_Points);
    //这里我想看看有多少个点构成
    vtkIdType PointsNumber = VesselPointsPolyData->GetNumberOfPoints();
    std::cout << "输入第一层TXT构成几何数据点数:" << PointsNumber - 1 << std::endl;
    //下面这几行只是我想看看原来第一层在哪
    vtkSmartPointer<vtkVertexGlyphFilter> vertexGlyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
    vertexGlyphFilter->SetInputData(VesselPointsPolyData);
    vertexGlyphFilter->Update();
    vtkSmartPointer<vtkPolyData>  diyiceng =
        vtkSmartPointer<vtkPolyData>::New();
    diyiceng->ShallowCopy(vertexGlyphFilter->GetOutput());

    //------------------------------------------------------------
    //---------------------------------第2部分新增加的投影功能-----------------------------------------
    double projected[3];//这里放投影后的点的坐标
    ofstream outfile;//创建一个ofstream对象
    outfile.open("../save/touying/touying1");
    //创建投影平面----------------
    vtkSmartPointer<vtkPlane> plane2 = vtkSmartPointer<vtkPlane>::New();
    plane2->SetOrigin(27.4805, 24.1368, 11.1816);
    plane2->SetNormal(-0.609361, 0.7694, 0.19158);
    vtkSmartPointer<vtkPoints> Points = vtkSmartPointer<vtkPoints>::New();
    for(int i = 0; i < VesselPointsPolyData->GetNumberOfPoints() - 1; i++) {
        double x[3] = {0, 0, 0};
        VesselPointsPolyData->GetPoint(i, x);
        Points->InsertPoint(i, x[0], x[1], x[2]);
        /// cout << x[0] << x[1] << x[2] << endl;
        plane2->ProjectPoint(x, projected);
        ///  cout << i << "Projected: " << projected[0] << " " << projected[1] << " " << projected[2] << std::endl;
        outfile << projected[0] << " " << projected[1] << " " << projected[2] <<  endl;
    }
    outfile << endl;
    outfile .close();
发布了37 篇原创文章 · 获赞 5 · 访问量 2163

猜你喜欢

转载自blog.csdn.net/weixin_44723106/article/details/103807428
今日推荐