GridCreation

GridCreation includes simple functions for producing 2D grids on which to compute functions. Cartesian grids are only produced as vectors, as broadcasting allows them to be used as a grid with lower memory allocation. Polar grids are generated as dense 2D arrays due to their lack of exploitable symmetry in a cartesian frame of reference.

Installation

GridCreation is a registered Julia package, it may be installed the usual way:

julia> Pkg.add("GridCreation")

Rationale

In Julia, it is viewed as idiomatic to use "meshgrid" for Cartesian grids. This is because dot syntax broadcasts "correctly" between row and column vectors, so the grid need not be stored. For this reason, mkCartVecs produces only vectors. Because these is no "mixing" between X and Y, there is not a more performant approach.

cartVecsToPolarGrid explicitly creates a meshgrid for its output. This is because both the radial and azimuthal variables require some computation. The duplicate computation is more expensive than the allocation when the grid is used several times, as in optical simulation and analysis. For this reason, cartVecsToPolarGrid forms a grid.

Functions

GridCreation.mkCartVecsFunction
mkCartVecs(dx, size[, center])

Create a 2D "grid" by returning a row vector x and column vector y.

The grid will have sample spacing dx, size[0] == size(x), and size[1] == size(y).

If size is an int, it is broadcast to [size,size].

The grid will be centered according to such that the center element contains zero. Special values for center include -1 (default, FFT-like center) and -2, for "interpixel" sampling, where 0 is at size/2. Otherwise, zero is at the centerth element.

The centering rule may not be different between x and y.

Examples

FFT-aligned grid

Note that the ceil rounded center element contains zero.

julia> x,y = mkCartVecs(.1, 4); # implicit center=CFFT
julia> x
1×4 LinearAlgebra.Adjoint{Float64,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}:
 -0.2  -0.1  0.0  0.1
julia> collect(y)
4-element Array{Float64,1}:
 -0.2
 -0.1
  0.0
  0.1

Intersample aligned grid

x,y=mkCartVecs(.1, 4, center=CInterSample);

julia> x
1×4 LinearAlgebra.Adjoint{Float64,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}:
 -0.15  -0.05  0.05  0.15

index-centered grid

julia> x,y=mkCartVecs(.1, 4, center=1);
julia> x
1×4 LinearAlgebra.Adjoint{Float64,StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}:
 1.11022e-17  0.1  0.2  0.3
source
GridCreation.cartVecsToPolarGridFunction
cartVecsToPolarGrid(X, Y)

Construct a polar grid (ρ,θ) from the row vector X and column vector Y. Returns a pair of 2D arrays containing the radial and azimuthal coordinates.

Examples

julia> X=(-2:1)'; Y=-2:1;
julia> ρ,θ=cartVecsToPolarGrid(X,Y);
julia> ρ
4×4 Array{Float64,2}:
 2.82843  2.23607  2.0  2.23607
 2.23607  1.41421  1.0  1.41421
 2.0      1.0      0.0  1.0
 2.23607  1.41421  1.0  1.41421
julia> θ
4×4 Array{Float64,2}:
 -2.35619  -2.03444  -1.5708  -1.10715
 -2.67795  -2.35619  -1.5708  -0.785398
  3.14159   3.14159   0.0      0.0
  2.67795   2.35619   1.5708   0.785398

See also: mkCartVecs

source