Note: this document is only useful if one wants to use FTK's C++ API and hack FTK impelemntations.
We use ftk::ndarray<T>
as the n-dimensional template container in the implementation of FTK, T
being a trivially copyable type (e.g. float
, double
, and int
).
We use the row-major order (a.k.a the "C" order) in ftk::ndarray
. For example, in order to represent a WxH image, the array stores the every scanline of the image by the width, followed by the next scanline.
If an array is representing a vector field or tensor field, it is a convention to use the first couple dimensions as the "component" dimension. For example, if the shape of a 3D array scalar
is WxHxD,
- The gradient field array
grad
has the shape of 3xWxHxD - The Hessian field array
hess
has the shape of 3x3xWxHxD
If you would like to convert this volume to VTK image data or other compatible formats, it is important to let ftk::ndarray
know the how many dimensions are used to represent the components. In the case of grad
, the gradient components are one-dimensional; useset_multicomponents(1)
. In the case of hess
, the Hessian components (3x3) are two-dimensional; useset_multicomponents(2)
.
We provide two ways to access individual elements. Assuming the dimensionality of array
is WxHxD, the following methods lead to the same element:
array.at(i, j, k)
array(i, j, k)
array[i + W*(j + D*k)]
To access the raw pointer of the array, use array.data()
.
Functions are provided to get the size of all dimensions:
array.nd()
returns the dimensionality of the arrayarray.shape()
returns a vector of sizes of each dimensionarray.shape(i)
returns the size of the i-th dimensionarray.reshape(n0, n1, ..., n_d)
reshapes the array into the designated shape and reallocate the memory if necessaryarray.nelem()
andarray.size()
return the total number of elementsarray.empty()
is true if the array is empty
We ease the I/O with inline functions. The use of VTK, NetCDF, HDF5, and ADIOS2 are required to compile and link FTK with corresponding external libraries
from_file(filename, [varname, communicator])
read data from file based on the filename extension. Supported filename extensions include.bp
,.h5
,.vti
,.nc
. The communicator isMPI_COMM_WORLD
by default.
Refer to VTK documentation for more details in VTK data structures.
from_vtk_image_data_file(filename, varname)
uses thevtkXMLImageDataReader
to read a.vti
fileto_vtk_image_data(varname)
convertsndarray
tovtkImageData
. Thevarname
and data components will be handled in the same manner ofto_vtk_data_array
from_vtk_image_data(image_data, varname)
convertsvtkImageData
tondarray
. Ifvarname
is specified, the designated array in the image data will be used; otherwise will useGetArray(0)
to determine which array to read
to_vtk_data_array(varname)
convertsndarray
tovtkDataArray
Ifvarname
is specified, the name ofvtkDataArray
will be set tovarname
. If there are multipe components, thevtkDataArray
will have multiple components as well. The data type of thevtkDataArray
will be automatically determined byT
. For example,ndarray<double>
will usevtkDataArray::CreateDataArray(VTK_DOUBLE)
to create the VTK data array.
Refer to NetCDF documentation for more details on NetCDF I/O.
read_netcdf(filename/ncid, varname/varid, [starts, sizes])
read NetCDF variable with the designated file name / file identifier, variable name / variable identifier, and optionally starts and sizes of the readto_netcdf(ncid, varid, [starts, sizes])
writes the array to an NetCDF variable
Refer to HDF5 documentation for more details on HDF5 I/O.
read_h5(filename/fid, dataname/did)
read HDF5 data with the designated filename / file identifer and data name / data identifer.
Refer to ADIOS2 documentation for more details on ADIOS2.
read_bp(filename, varname, [communicator])
read.bp
file with the designated file name and variable nameread_bp(adios2::IO, adios2::Engine, varname, [timestep])
read ADIOS2 data with designated variable name and timestep
read_binary_file(filename/FILE, [offset])
read data from a raw binary file with POSIX I/Oto_binary_file(filename/FILE)
write data to a raw binary file with POSIX I/O
Conversion from the following data structures are supported:
- Raw data pointer. The shape of the array needs to be specified
std::vector
andstd::array
. The output will be a 1D array; reshape as neededpybind11::array_t<T, pybind11::array::c_style | pybind11::array::forcecast>
. Conversion betweennumpy
arrays withpybind11
. The numpy array must be in C style.vtkDataArray
andvtkImageData
. See previous I/O section for more details.