Skip to content

Commit

Permalink
add origin point test
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgepz committed Jun 7, 2022
1 parent c75860d commit 3e7683a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/ReadVTK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ using CodecZlib: ZlibDecompressor
using LightXML: LightXML, XMLDocument, XMLElement, parse_string, attribute, has_attribute,
child_elements, free, content, find_element

export VTKFile, get_points, get_cells, VTKData, get_point_data, get_cell_data, VTKDataArray,
get_data, VTKCells, get_example_file


"""
VTKFile
Expand Down Expand Up @@ -61,6 +57,7 @@ header_type(::VTKFile) = UInt64
# Return true if data is compressed (= XML attribute `compressor` is non-empty in VTK file)
is_compressed(vtk_file::VTKFile) = !isempty(vtk_file.compressor)

include("get_functions.jl")

"""
VTKFile(filename)
Expand Down Expand Up @@ -551,4 +548,6 @@ function get_example_file(filename; head="main", output_directory=".", force=fal
return filepath
end

include("exports.jl")

end # module
4 changes: 4 additions & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export VTKFile, VTKData, VTKDataArray, VTKCells

export get_points, get_cells, get_point_data, get_cell_data, get_data, get_example_file, get_origin
19 changes: 19 additions & 0 deletions src/get_functions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

"""
get_origin(vtk_file)
Retrieve the origin of a VTK regular structured grid file.
"""

function get_origin( vtk_file )

root = LightXML.root( vtk_file.xml_file )

piece = root["ImageData"][1]

originPoint = parse.(Float64, split( attribute(piece, "Origin", required=true) , ' ' ) )

return originPoint
end

24 changes: 11 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,37 +170,35 @@ mkpath(TEST_EXAMPLES_DIR)
@testset "ImageData" begin

## Generate grid file and write vti
x, y, z = 1:3, 1:2, 2:0.1:2.2
inputOrigin = [1.0, 1.0, 2.0]
x, y, z = (inputOrigin[1]):3, (inputOrigin[2]):2, (inputOrigin[3]):0.1:2.2
Nx, Ny, Nz = length(x), length(y), length(z)

pointScalarField = rand(Nx, Ny, Nz)
cellScalarField = rand(Nx-1, Ny-1, Nz-1)

print( "cell scalar field", cellScalarField,"\n")

cellDataName = "Name of Test Cell scalar data"
pointDataName = "Point scalar data"

print(" writing vtk file...")

vtk_grid("grid", x, y, z) do vtk
vtk[ pointDataName, VTKPointData()] = pointScalarField # scalar field attached to points
vtk[ cellDataName, VTKCellData()] = cellScalarField # scalar field attached to cells
end
print("done.\n")


## Read vti file
print(" reading vtk file...")
filepath = "grid.vti"
vtk = VTKFile( filepath )
data = get_data( get_cell_data(vtk)[cellDataName] )
print("done.\n")


origin = get_origin( vtk )
data = get_data( get_cell_data(vtk)[cellDataName] )

reshapedData = reshape( cellScalarField, ( (Nx-1), (Ny-1), (Nz-1) ) )

difference = reshapedData .- cellScalarField
# test if cell data is well read
@test iszero( reshapedData .- cellScalarField )

@test iszero( difference )
# test if the origin is well read
@test iszero( origin .- inputOrigin )

end
end
Expand Down

0 comments on commit 3e7683a

Please sign in to comment.