vtk文件編寫


   在paraview中加載vtk文件,可以很好的顯示三維空間圖像,如下cpp代碼:

#include <iostream>
#include <fstream>
#include <windows.h>
#include <ctime>

using namespace std;
int main() {
    cout << "this is beginning!" << endl;
    int Nx = 50, Ny = 50;
    fstream outfile;
    char filename[20] = "output.vtk";
    outfile.open(filename, ios::out);
    if (!outfile) {
        cout << "open failed" << endl;
    }
    outfile << "# vtk DataFile Version 2.0" << endl;
    outfile << filename << endl;
    outfile << "ASCII " << endl;
    outfile << "DATASET STRUCTURED_GRID" << endl;
    outfile << "DIMENSIONS " << Nx << " " << Ny << " " << 1 << endl;
    outfile << "POINTS " << Nx * Ny * 1 << " float" << endl;
    for (int i = 0; i < 50; i++) {
        for (int j = 0; j < 50; j++) {
            outfile << i << " " << j << " " << 0 << endl; 
        }
    }

    outfile << "POINT_DATA " << Nx * Ny * 1 << endl;
    outfile << "SCALARS CON float 1" << endl;
    outfile << "LOOKUP_TABLE default" << endl;

    // 獲取隨機數
    srand(time(0));
    for (int i = 0; i < 50; i++)
    {
        for (int j = 0; j < 50; j++)
        {
            outfile << rand() % 10 << "\t";
        }
    }
    outfile.close();
    system("pause");
    return 0;
}

格式是固定的,將輸出的文件在paraview中打開即可。如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM