I'm trying to rasterize and fill a hypersphere. In essence, I have a d-dimensional grid of fixed size and a sphere (center, radius) and want to find out which cells of the grid overlap with the sphere and store their coordinates.
I am aware of the Midpoint circle algorithm which takes advantage of 8-way mirroring and produces the outer cells (border) of a circle. I have also altered the linked wikipedia code so as to fill the circle (that is, to produce the coordinates of all cells inside the border).
However I am unaware of any algorithms for higher dimension. For example in 4d, I've been thinking of implementing by producing all possible circles like in the following pseudocode. The basic idea is that since a 4d sphere is (x-x0)2 + (y-y0)**2 + (z-z0)**2 + (k-k0)**2 = r2, this is equal to (x-x0)2 + (y-y0)**2 = r2 - (z-z0)**2 - (k-k0)**2. Since I know how to draw a circle, i just need to produce all circles for all possible values of z and k.
assume center=(x0,y0,z0,k0) and radius r
for all dimensions equal or higher than 2://this is z and k
//make a list of possible values this dimension can take
//from z0 to z0+radius with a step of 1
all_lists.append([dim0,dim0+1,...,dim0+radius])
produce the product of all the lists in all_lists
//now i have a list [[z0,k0],[z0,k0+1],....,[z0+1,k0],[z0+1,k0+1],....,[z0+radius,k0],...[z0+radius,k0+radius]]
for every element l of the list, compute the radius of the circular "cut"
l.append(r**2 - z**2 - k**2)
Now call the Midpoint Circle Algorithm, but for every (x,y) pair that it produces, we need to export 4 points, namely (x,y,±z,±k)
This question seems relevant, but I don't understand the answer.
well no one answers for some time so here is simple and obvious C++ solution of mine:
This is brute force that can be speeded up using ray castig see:
which can boost speed considerably especially in higher dimensions.
[Edit1] just some testing done
screenshot is taken from test app output for code above. Viewing
XY
plane (z=0
) for 1D,2D,3D and 4D hyperspheres. I am not sure about 1D but the rest is OK (not sure if hyperspace is defined for 1D or should be only a point instead).Even the pixel count ~ Volume looks very similar so the algorithm and code should be OK. Be aware that complexity is
O(N!)
where N is number of dimensions and runtime isc0*(N!)*r
wherec0
is constant time,r
is radius andN
is number of dimensions.[Edit2] Now how to visualize more than 3D? There are 2 common approaches:
projection
either Orthographic (parallel rays as on images above) or Perspective (the moe distant stuff is smaller). The latter reveals the hidden stuff for example 4D axis aligned Tesseract with orthographic projection to 3D is just a cube, but with Perspective its cube inside bigger cube with all 16 corners interconnected connected...
cross section
You just cut N-dimensional space by a N-dimensional hyperplane and any intersection of your object and hyper plane will give you N-1 dimensional object. This can be applied recursively until hit 3D and render with standard methods.
Both approaches can be combined (some dimensions are reduced by cross sections others with projection ...)
Here some more examples of 4D hypersphere (centered
(0,0,0,0)
and low poly count so its not a wireframe mess):Here higher poly count hyper sphere cross-section (W=0.3):
As you can see there are more "grid" like features than the standard parametrical spherical coordinates generated mesh.
However cross section requires that that rendered objects are defined by simplexes covering the objects volume (even surface ones need some kind of volume) otherwise the implementation will get vary nasty filled with edge cases handling.
For more info about 4D rendering see:
To get back to hypersphere:
according to wiki n-sphere we can describe the surface points of n-sphere by parametric equations:
Where all the angles except last are in interval
<0,PI>
and the last one is<0,2*PI>
. This allows to construct n-sphere manifold/mesh directly. In my engine it looks like this:Where
pnt[]
is list of points and_cube
adds a cube constructed from 5 tetrahedrons covering its volume. As you can see this creates 2D disc, 3D ball, and 4D sphere (not full volume just layer between manifold neighboring nodes) "surface".The code just creates n-sphere grid points (with constant angular increase) and then using the 2,4,or 8 neighboring points (and center of sphere for the 2D/3D) to add rendering primitive to object (triangle, tetrahedron, cube).
The renderer then just reduce dimension to 3D and render.
There is also one other approach for this and that is ray tracing. The above stand but with ray tracing we can use algebraic representation of object so we do not need any mesh nor manifolds nor topology. Simply compute closest intersection of a hypersphere and ray (which is just a point) and render accordingly ...