vtk文件编写

本文介绍了一种使用C++编程语言生成VTK格式文件的方法,并详细展示了如何在Paraview软件中加载并展示这些文件,以便进行三维空间图像的可视化。

   在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中打开即可。如下:

 

转载于:https://www.cnblogs.com/zhuzhenwei918/p/8809641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值