Hello!
I have the following code:
using WriteVTK
# Cell dimensions
dx = 1.0
dy = 1.0
# Initialize lists for storing points and connectivity
points = Vector{SVector{3, Float64}}() # List to store unique SVector points
cells = Int32[] # List for connectivity of each cell
offsets = Int32[] # List for offset positions
cell_types = Int8[] # VTK cell type array
# Example set of CartesianIndex cells (5x5 grid)
UniqueCells = [CartesianIndex(i, j) for i in 1:5, j in 1:2]
# Loop through each CartesianIndex cell
iter =
for cell in UniqueCells
iter += 1
# Get x and y from the CartesianIndex and calculate cell center
xi, yi = cell.I
x_center = (xi - 0.5) * dx
y_center = (yi - 0.5) * dy
# Define corners in counterclockwise order
x0, y0 = x_center - dx / 2, y_center - dy / 2
x1, y1 = x_center + dx / 2, y_center - dy / 2
x2, y2 = x_center + dx / 2, y_center + dy / 2
x3, y3 = x_center - dx / 2, y_center + dy / 2
# Add corners as individual points
push!(points, SVector(x0, y0, 0.0)); push!(cells, length(points))
push!(points, SVector(x1, y1, 0.0)); push!(cells, length(points))
push!(points, SVector(x2, y2, 0.0)); push!(cells, length(points))
push!(points, SVector(x3, y3, 0.0)); push!(cells, length(points))
# Ensure correct connectivity for each cell
push!(offsets, length(cells)) # Track the end of the cell in the cells array
push!(cell_types, 9) # VTK_QUAD type for square cells
end
# Write to VTK as unstructured grid
vtk_grid("ExtractedCells", points) do vtk
vtk["cells"] = cells
vtk["offsets"] = offsets
vtk["celltypes"] = cell_types
end
Which generates:
This is of course wrong, since I want a grid of squares.
I want to use this and no other way, since my real use case is where UniqueCells are a lot of disjointed indices and this seems to work well, I am just struggling with the connectivity… maybe someone else has solved this before?
Kind regards