Drawing Lattice Fields

How the renderer draws lattice fields, why the first differential matters.

This takes a little mental gymnastics due to the way that shaders work backwards (a point asks its colour) to the natural way to think about drawing (positioning colour). It therefore works as the pullback. To draw an image of a lattice therefore we care about the inverse of the lattice basis. I find it easier to think the other and then consider the inverse. So the analysis here is the inverse to the code.

Take an input lattice and distort in by multiplication by a lattice field (defined by a formula) to give a new lattice field F(p)F(p). This gives a lattice at every point, that can’t be visualised directly, but if the lattice is not changing too fast it can be visualised with a dot pattern that locally approximates the lattice around a point.

Using a lattice render method (with the lattice at each point changing) gives distorted (rather than circular) points, as the lattice shifts around. We are interested in points that are lattice points in their own lattice. In other words F(s)−1sF(s)^{-1} s takes integer values. For a given point we want to find the closest point with this property. We approximate this for a point xx using ϕ(x)=F(x)[F(x)−1x]Z2\phi(x) = F(x) [F(x)^{-1} x]_{\Z^2}, where [.]Z2[.]_{\Z^2} rounds to the nearest point in the standard lattice Z2\Z^2 (the lattice basis is reduced to help this work). Iterating ϕ\phi pulls close to a point lying in its own lattice, as desired.

The set of points drawn by this method is therefore approximately f(s)=F(s).sf(s)=F(s).s for s∈Z2s \in \Z^2. However this introduces distortion from the function itself. The lattice we perceive locally is:

df=F+dF.xdf = F + dF.x

not FF as desired. So to visualise FF as a lattice field we need to find a lattice field GG solving the differential equation:

F=G+dG.xF = G + dG.x

The right side is exactly the differential of the map g(x)=G(x).xg(x) = G(x).x. So F=dgF = dg is (perhaps obviously in hindsight) just the differential of a vector field gg. That is equivalent to its rows being curl free or:

∇×F=0\nabla\times F = 0

In this case a candidate GG can be found by integrating along the ray from the origin:

G(x)=∫01F(tx) dtG(x) = \int_0^1 F(tx)\,dt

To check it, integrate the identity

ddt[t F(tx)]=F(tx)+t [(x⋅∇)F]∣tx\frac{d}{dt}\Big[t\,F(tx)\Big] = F(tx) + t\,\big[(x\cdot\nabla)F\big]\Big|_{tx}

from t=0t=0 to 11, where (x⋅∇)F(x\cdot\nabla)F is the derivative of FF along the ray. The left side integrates to F(x)F(x), and the right side to G(x)G(x) plus the ray derivative term. Meanwhile dG.xdG.x is that same integral with the arguments paired the other way, position into the matrix rather than direction into the derivative, giving:

G+dG.x−F=∫01t [dF.x−(x⋅∇)F]tx dt=(∫01t (∇×F)∣tx dt)⊗x⊥G + dG.x - F = \int_0^1 t\,\Big[dF.x - (x\cdot\nabla)F\Big]_{tx}\,dt = \Big(\int_0^1 t\,(\nabla\times F)\Big|_{tx}\,dt\Big)\otimes x^\perp

with x⊥=(−x2,x1)x^\perp = (-x_2, x_1). Which is the integral of the curl along the path. So in the case where F=dgF = dg this is 00 and the field is shown, otherwise there is still distortion from the curl, that can be moved around but not removed by any matrix field with H(x).x=0H(x).x = 0. Note that such fields must be singular.

This is the version of Helmholtz decomposition that F=df1+∇⊥f2F = df_1 + \nabla^{\perp}f_2, just as a vector field have a curl free and divergence free part.