# Finding the normals of a tileable 2D surface extracted from 4D space

If I use a 4D noise function which returns a noise value and a four-part analytically derived gradient vector, I can create a 2D tile which is seamless (i.e. the opposite edges match) through remapping x,y and z,w coordinates to independent circles, i.e.

$$\ \theta = a*2*\pi$$
$$\ \phi = b*2*\pi$$

where $$a$$ and $$b$$ are a point on the surface. They should be within the range $$[0, size)$$ where size is some arbitrary shared dimension for the surface.

We can then plug $$\theta$$ and $$\phi$$ into the below functions to get the equivalent point in 4D space.

$$\ x = r_0*\cos(\theta)$$
$$\ y = r_0*\sin(\theta)$$
$$\ z = r_1*\cos(\phi)$$
$$\ w = r_1*\sin(\phi)$$

Effectively the surface created through this coordinate mapping is a flat torus.

However, since the noise function operates in 4D space I'm left with a 4D gradient vector. Is it possible to transform this 4D vector into a 2D vector which describes the gradient of the surface?

• What surface are you talking about? It's not obvious what you want from your question. Jan 30, 2019 at 22:10
• @lightxbulb Added some extra information, and tweaked the title. I'm looking to "unwrap" a torus in four dimensions to create a two-dimensional tile, this tile (or surface) will contain noise values which could be used for any form of procedural texture (such as a height map). It is possible to also retrieve the gradient at each point on this surface, however since they're 4D I don't know how to translate that to 2D (or even if it is possible). Jan 30, 2019 at 22:58
• Can you define the map that you use to transform it into 2 dimensions? Because currently you have a surface in 4 dimensions, which you can't even embed in 3d. It's also not obvious what you want to do with the gradient vector. It simply gives you the direction in which your function increases the fastest, and there are infinitely many mappings to 2D. Jan 30, 2019 at 23:16
• Ok, I believe that article clarified what you're doing: you're taking a 2-manifold slice out of some 4-dimensional scalar field. What is still unclear is what you want to do with that gradient, because it is indeed 4D. Assuming that you want the gradient of the image, then there are a few possibilities. The obvious is to take finite differences from your image, then you get the 2D gradient. Another possibility is to try and taking the derivative analytically. Let $f(x,y,z,w)$ be your scalar field (the noise function), the I take it that image space for you is really $\theta, \phi$, that is... Jan 31, 2019 at 13:36
• You can take the partial derivatives with respect to $\theta, \phi$: $\frac{\partial f}{\partial \theta} = -\frac{\partial f}{\partial x}\sin\theta + \frac{\partial f}{\partial y}\cos\theta$ and similarly: $\frac{\partial f}{\partial \phi} = -\frac{\partial f}{\partial z}\sin\phi + \frac{\partial f}{\partial w}\cos\phi$ where I have used the chain rule for taking a partial derivative of a multivariable composite function (the last two terms for $\theta$ are $0$, same for the first two of $\phi$). If this answers your question I can formulate it as an answer. Jan 31, 2019 at 13:41

Since the question was somewhat clarified I will formalize both the question and the answer for future readers. Having a differentiable scalar field $$f : \mathbb{R}^4 \rightarrow \mathbb{R}$$ we want to find the gradient of the field with respect to $$\theta, \phi$$ on the 2-manifold defined parametrically by: $$(x(\theta,\phi), y(\theta,\phi) z(\theta,\phi), w(\theta,\phi)) = (r_0\cos\theta, r_0\sin\theta, r_1\cos\phi,r_1\sin\phi)$$ Where $$r_0,r_1$$ are constants. We can compute the partial derivatives as follows: $$\frac{\partial f}{\partial \theta} = \frac{\partial f}{\partial x}\frac{\partial x}{\partial \theta} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial \theta} + \frac{\partial f}{\partial z}\frac{\partial z}{\partial \theta} + \frac{\partial f}{\partial w}\frac{\partial w}{\partial \theta} \\ = -r_0\frac{\partial f}{\partial x}\sin\theta + r_0\frac{\partial f}{\partial y}\cos\theta$$ $$\frac{\partial f}{\partial \phi} = \frac{\partial f}{\partial x}\frac{\partial x}{\partial \phi} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial \phi} + \frac{\partial f}{\partial z}\frac{\partial z}{\partial \phi} + \frac{\partial f}{\partial w}\frac{\partial w}{\partial \phi} \\ = -r_1\frac{\partial f}{\partial x}\sin\phi + r_1\frac{\partial f}{\partial y}\cos\phi$$
Assuming that the gradient $$\nabla f = (\frac{\partial f}{\partial x},\frac{\partial f}{\partial y},\frac{\partial f}{\partial z},\frac{\partial f}{\partial w})$$ is known, it is straightforward to compute the 2d gradient.
Note that another possibility is to compute the gradient numerically (in case you don't have the partial derivatives of $$f$$): $$\frac{\partial f}{\partial \theta} \approx \frac{f(r_0\cos(\theta+h_{\theta}), r_0\sin(\theta+h_{\theta}), r_1\cos\phi,r_1\sin\phi) - f(r_0\cos\theta, r_0\sin\theta, r_1\cos\phi,r_1\sin\phi)}{h_{\theta}}$$ $$\frac{\partial f}{\partial \phi} \approx \frac{f(r_0\cos\theta, r_0\sin\theta, r_1\cos(\phi+h_{\phi}),r_1\sin(\phi+h_{\phi})) - f(r_0\cos\theta, r_0\sin\theta, r_1\cos\phi,r_1\sin\phi)}{h_{\phi}}$$ For $$h_{\theta}, h_{\phi}$$ chosen adequately (note that the choice of these parameters is a nontrivial problem).