# Regular grid

**URL:** <https://discourse.julialang.org/t/regular-grid/82371>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 7, 2022, 1:01pm UTC](https://discourse.julialang.org/t/regular-grid/82371 "2022-06-07T13:01:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbrisard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbrisard/32/24712_2.png) [@sbrisard](https://discourse.julialang.org/u/sbrisard)\
**Post date:** [June 7, 2022, 1:01pm UTC](https://discourse.julialang.org/t/regular-grid/82371/1 "2022-06-07T13:01:33Z")

</div>

Hello there,  
I’m implementing Siddon’s algorithm for the calculation of the radiological path through an array of voxels. I was wondering what would be the most Juliesque way to define a regular grid? As an array of StepRange, for example? There is probably a package around there that already defines such a structure.  
Thanks for your advice!  
Sébastien

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [June 7, 2022, 2:14pm UTC](https://discourse.julialang.org/t/regular-grid/82371/2 "2022-06-07T14:14:25Z")

</div>

Depends on what functionality you need from the grid.  
The most bare-bones rectangular grid is `Iterators.product(1:10, 1:100)`: it can be iterated or `map`ped over, but isn’t even an array.  
More extensive grids can be created with the [RectiGrids.jl](https://gitlab.com/aplavin/RectiGrids.jl) package. They are arrays, and even more, KeyedArrays, providing convenient lookup methods. _(disclaimer: I’m the package author)_

---

<div class="post-metadata">

**Author:** ![Andrea\_Vigliotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_vigliotti/32/7635_2.png) [@Andrea\_Vigliotti](https://discourse.julialang.org/u/Andrea_Vigliotti)\
**Post date:** [June 7, 2022, 2:51pm UTC](https://discourse.julialang.org/t/regular-grid/82371/3 "2022-06-07T14:51:22Z")

</div>

one quick and dirty way is to do

```julia
ones(5)*transpose(range(-1, 5, length=4))

5×4 Matrix{Float64}:
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0

```

```julia
range(-1, 5, length=4)*transpose(ones(5))

4×5 Matrix{Float64}:
 -1.0 -1.0 -1.0 -1.0 -1.0
  1.0 1.0 1.0 1.0 1.0
  3.0 3.0 3.0 3.0 3.0
  5.0 5.0 5.0 5.0 5.0

```

(@aplavin reply is definitely more Juliasque, as it does not allocate memory in advance and it can map over multiple dimensions and different type of collections, the above would be analogous to Matlab’s `meshgrid`)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 7, 2022, 2:58pm UTC](https://discourse.julialang.org/t/regular-grid/82371/4 "2022-06-07T14:58:48Z")

</div>

Shorter:

```julia
julia> ones(5) * (-1:2:5)'
5×4 Matrix{Float64}:
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0
 -1.0 1.0 3.0 5.0

```

---

<div class="post-metadata">

**Author:** ![sbrisard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbrisard/32/24712_2.png) [@sbrisard](https://discourse.julialang.org/u/sbrisard)\
**Post date:** [June 7, 2022, 3:02pm UTC](https://discourse.julialang.org/t/regular-grid/82371/5 "2022-06-07T15:02:45Z")

</div>

To be more precise, I need a `RegularGrid` type, that knows about the number of cells in each direction, the step in each direction, etc. A kind of multidimensional `range`.

This is what I’ve come up with for the time being

```julia
struct RegularGrid{NDIMS,T}
    size::SVector{NDIMS,Int}
    step::SVector{NDIMS,T}
    center::SVector{NDIMS,T}
    bb_min::SVector{NDIMS, T}
    bb_max::SVector{NDIMS, T}
    function RegularGrid{NDIMS, T}(size, step, center) where {NDIMS, T}
        bb_min = center .- 0.5 * size .* step
        bb_max = center .+ 0.5 * size .* step
        new(size, step, center, bb_min, bb_max)
    end
end

Base.ndims(::RegularGrid{NDIMS,T}) where {NDIMS,T} = NDIMS
Base.eltype(::RegularGrid{NDIMS,T}) where {NDIMS,T} = T
Base.ndims(::Type{RegularGrid{NDIMS,T}}) where {NDIMS,T} = NDIMS
Base.eltype(::Type{RegularGrid{NDIMS,T}}) where {NDIMS,T} = T

Base.size(grid::RegularGrid{NDIMS,T}, dim) where {NDIMS,T} = grid.size[dim]
Base.step(grid::RegularGrid{NDIMS,T}, dim) where {NDIMS,T} = grid.step[dim]

```

This does the job, but I’d rather use a struct that has already been defined in another package.

---

<div class="post-metadata">

**Author:** ![sbrisard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbrisard/32/24712_2.png) [@sbrisard](https://discourse.julialang.org/u/sbrisard)\
**Post date:** [June 7, 2022, 3:04pm UTC](https://discourse.julialang.org/t/regular-grid/82371/6 "2022-06-07T15:04:41Z")

</div>

Thanks, `RectiGrids.jl` looks promising. Does the grids thus defined know about the spacing of the cells?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [June 7, 2022, 3:18pm UTC](https://discourse.julialang.org/t/regular-grid/82371/7 "2022-06-07T15:18:51Z")

</div>

[https://juliageometry.github.io/Meshes.jl/dev/meshes.html#Overview](https://juliageometry.github.io/Meshes.jl/dev/meshes.html#Overview)

---

<div class="post-metadata">

**Author:** ![sbrisard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbrisard/32/24712_2.png) [@sbrisard](https://discourse.julialang.org/u/sbrisard)\
**Post date:** [June 7, 2022, 4:48pm UTC](https://discourse.julialang.org/t/regular-grid/82371/8 "2022-06-07T16:48:42Z")

</div>

Thank you! This is definitely what I’m looking for.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [June 7, 2022, 9:59pm UTC](https://discourse.julialang.org/t/regular-grid/82371/9 "2022-06-07T21:59:40Z")

</div>

> [@sbrisard](#):
>
> Thanks, `RectiGrids.jl` looks promising. Does the grids thus defined know about the spacing of the cells?

Of course!

```julia
julia> using RectiGrids

julia> G = grid(x=1:10, y=20:10:100);

julia> axiskeys(G, :x)
1:10

julia> step(axiskeys(G, :y))
10

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [June 8, 2022, 1:34am UTC](https://discourse.julialang.org/t/regular-grid/82371/10 "2022-06-08T01:34:25Z")

</div>

> [@sbrisard](#):
>
> To be more precise, I need a `RegularGrid` type, that knows about the number of cells in each direction, the step in each direction, etc. A kind of multidimensional `range`.

[https://www.generic-mapping-tools.org/GMT.jl/dev/types/#Grid-type](https://www.generic-mapping-tools.org/GMT.jl/dev/types/#Grid-type)
