如何读取由C ++和VTK在Fortran中生成的二进制Structural_points数据文件?

时间:2019-02-22 03:46:55

标签: c++ fortran vtk

我是VTK的新手,我试图在vtk中读取二进制的structured_points(图像数据)文件,但出现空白窗口,并显示“读取二进制数据时出错”错误。我正在使用一个简单的fortran程序来创建数据文件(高斯场数据),如下所示,

 program gaussian

            implicit none
            integer i, j, k
            character(1) c

    open(unit=100, file='energyDensity.vtk',
 1        form="unformatted",access="stream")
    write(100) '# vtk DataFile Version 3.0', new_line(c)
    write(100) 'First time trying vtk import \n', new_line(c)
    write(100) 'BINARY', new_line(c)
    write(100) 'DATASET STRUCTURED_POINTS', new_line(c)
    write(100) 'DIMENSIONS 101 101 101', new_line(c)
    write(100) 'ORIGIN 0 0 0', new_line(c)
    write(100) 'SPACING 1 1 1', new_line(c)
    write(100) 'POINT_DATA 1030301', new_line(c)
    write(100) 'SCALARS volume_scalars double 1', new_line(c)
    write(100) 'LOOKUP_TABLE default', new_line(c)


            do k = -50,50
            do j = -50,50
            do i = -50,50

            write(100) 50.*exp(float((-(i*i+j*j+k*k))/25)) 

            enddo
            enddo
            enddo
    close(100)

endprogram

如果数据为ASCII格式,则VTK可以很好地读取和绘制数据(见下图)

enter image description here

我正在VTK中使用以下代码C ++读取数据(不对endian进行任何设置),

vtkNew<vtkStructuredPointsReader> reader;
  reader->SetFileName (argv[1]);
  reader->Update();

我曾尝试在Internet上进行大量搜索,但是找不到正确的读取二进制结构化点数据的方法。似乎也没有办法为结构化点阅读器设置字节序。我不确定在这里做什么。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您的编写过程中有几个问题。

首先,您正在写入单精度数据(通过使用double),但是您声称数据是VTK标头中的(-(i*i+j*j+k*k))/25)

第二,旧版VTK文件中的二进制数据应为big-endian。

您也正在 program gaussian use iso_fortran_env implicit none integer i, j, k character(1) c open(unit=100, file='energyDensity.vtk', & form="unformatted",access="stream") write(100) '# vtk DataFile Version 3.0', new_line(c) write(100) 'First time trying vtk import \n', new_line(c) write(100) 'BINARY', new_line(c) write(100) 'DATASET STRUCTURED_POINTS', new_line(c) write(100) 'DIMENSIONS 101 101 101', new_line(c) write(100) 'ORIGIN 0 0 0', new_line(c) write(100) 'SPACING 1 1 1', new_line(c) write(100) 'POINT_DATA 1030301', new_line(c) write(100) 'SCALARS volume_scalars double 1', new_line(c) write(100) 'LOOKUP_TABLE default', new_line(c) do k = -50,50 do j = -50,50 do i = -50,50 write(100) SwapB64(exp(real((-(i*i+j*j+k*k)), real64) / 25)) enddo enddo enddo close(100) contains elemental function SwapB64(x) result(res) real(real64) :: res real(real64),intent(in) :: x character(8) :: bytes integer(int64) :: t real(real64) :: rbytes, rt equivalence (rbytes, bytes) equivalence (t, rt) rbytes = x t = ichar(bytes(8:8),int64) t = ior( ishftc(ichar(bytes(7:7),int64),8), t ) t = ior( ishftc(ichar(bytes(6:6),int64),16), t ) t = ior( ishftc(ichar(bytes(5:5),int64),24), t ) t = ior( ishftc(ichar(bytes(4:4),int64),32), t ) t = ior( ishftc(ichar(bytes(3:3),int64),40), t ) t = ior( ishftc(ichar(bytes(2:2),int64),48), t ) t = ior( ishftc(ichar(bytes(1:1),int64),56), t ) res = rt end function endprogram 中进行整数除法,但这可能是有意的(或不是故意的)。

这行得通

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:    3.1.5                                                                              │
  │ Browser:    Chrome 72                                                                          │
  │ Specs:      2 found (Login_With_User_Credentials.spec.js, tsconfig.json)                       │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running: Login_With_User_Credentials.spec.js...                                          (1 of 2) 



  1) Correct credentials login

  √ Incorrect credentials login (2082ms)

  1 passing (3m)
  1 failing

  1)  Correct credentials login:
     CypressError: Timed out retrying: Expected to find element: 'button#btnSearch', but never found it.
      at Object.cypressErr (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:65377:11)
      at Object.throwErr (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:65342:18)
      at Object.throwErrByPath (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:65369:17)
      at retry (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:58910:16)
      at http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:51018:18
      at tryCatcher (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:127298:23)
      at Promise._settlePromiseFromHandler (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:125316:31)
      at Promise._settlePromise (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:125373:18)
      at Promise._settlePromise0 (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:125418:10)
      at Promise._settlePromises (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:125493:18)
      at Async._drainQueue (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:122222:16)
      at Async._drainQueues (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:122232:10)
      at Async.drainQueues (http://quicktest.aclessentials.com/__cypress/runner/cypress_runner.js:122106:14)




  (Results)

  ┌───────────────────────────────────────────────────┐
  │ Tests:        2                                   │
  │ Passing:      1                                   │
  │ Failing:      1                                   │
  │ Pending:      0                                   │
  │ Skipped:      0                                   │
  │ Screenshots:  1                                   │
  │ Video:        false                               │
  │ Duration:     2 minutes, 52 seconds               │
  │ Spec Ran:     Login_With_User_Credentials.spec.js │
  └───────────────────────────────────────────────────┘


  (Screenshots)

  - C:\cypress\screenshots\Login_With_User_Credentials.spec.js\Correct credentials login (failed).png (1920x969)


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running: tsconfig.json...                                                                (2 of 2) 


  0 passing (2ms)


  (Results)

  ┌─────────────────────────────┐
  │ Tests:        0             │
  │ Passing:      0             │
  │ Failing:      0             │
  │ Pending:      0             │
  │ Skipped:      0             │
  │ Screenshots:  0             │
  │ Video:        false         │
  │ Duration:     0 seconds     │
  │ Spec Ran:     tsconfig.json │
  └─────────────────────────────┘


====================================================================================================

  (Run Finished)


      Spec                                                Tests  Passing  Failing  Pending  Skipped 
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ × Login_With_User_Credentials.spec.js       02:52        2        1        1        -        - │
  ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  │ √ tsconfig.json                               6ms        -        -        -        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    1 of 2 failed (50%)                         02:52        2        1        1        -        -  

答案 1 :(得分:1)

按照弗拉基米尔(Vladimir)关于仅接受big_endian的旧格式的建议,对我上面的fortran代码进行一些简单的更改就可以解决问题。将“打开”文件语句中的“转换”参数设置为“ big_endian”解决了最大的问题。另外,浮点数应更改为“ real(real64)”,以与vtk数据文件中的double数据类型保持一致。

    program gaussian

    use iso_fortran_env

            implicit none
            integer i, j, k
            character(1) c
            real(real64) x

    open(unit=100, file='energyDensity.vtk',
 1        form="unformatted",access="stream",convert="big_endian")
    write(100) '# vtk DataFile Version 3.0', new_line(c)
    write(100) 'First time trying vtk import \n', new_line(c)
    write(100) 'BINARY', new_line(c)
    write(100) 'DATASET STRUCTURED_POINTS', new_line(c)
    write(100) 'DIMENSIONS 101 101 101', new_line(c)
    write(100) 'ORIGIN 0 0 0', new_line(c)
    write(100) 'SPACING 1 1 1', new_line(c)
    write(100) 'POINT_DATA 1030301', new_line(c)
    write(100) 'SCALARS volume_scalars double 1', new_line(c)
    write(100) 'LOOKUP_TABLE default', new_line(c)


            do k = -50,50
            do j = -50,50
            do i = -50,50

            x=50.*exp(real((-(i*i+j*j+k*k))/25,real64))

            write(100) x

            enddo
            enddo
            enddo
    close(100)

    endprogram