I am looking for a solution do "draw" a 3d line (filled cylinder) into a 3d array using python just like the skimage.draw.line function does for 2 arrays.
The line should have a starting point (x1, y1, z1)
, an end point (x2, y2, z2)
, and a radius R
.
I had a look at an example for a 2d line but I was not able to modify it to work in the 3d case.
I was thinking about drawing successive ellipses into the array, but I was not able to figure out how to calculate the two axes and the rotation angle.
Maybe there is a much simpler approach to this problem?
I finally managed to implement this in python using skimage.draw.ellipse:
Let assume this GLSL volumetric back raytracer as a start point. To make a filled 3D line like this:
You need:
endpoints as spheres
see the
add_sphere
in the link above.discs cut at the endpoints
for that we need
U,V
basis vectors (perpendicular vectors to each other and to line itself). With those we can simply use any 2D circle pixels acquisition and convert them to 3D voxel positions with ease. So ifu,v
are coordinates in some 2D circle centered at(0,0)
then:(x,y,z)
are corresponding to 3D circle voxel coordinate with center(x0,y0,z0)
. For more info see my C++ glCircle3D implementation.line body
As wee got all the voxel positions in the disc around
x0,y0,z0
endpoint of the line just cast a line from each of it with the same slope as line(x0,y0,z0),(x1,y1,z1)
which are the endpoints of your line.When put together in C++ (sorry I do not code in python) I got this:
The
vector_xxx
functions are just my 3D vector math and just dot,cross product and normalize to unit size are used which is easy to implement. You can see them here:There are still things that can be improved like the spheres could be just half spheres and their generation can be joined with the disc stuff ... as the dot between normal and not offseted 3D sphere coordinate is either positive/zero/negative which distinct endpoint half-sphere and disc ... that would also fully eliminate the need for
U,V
.Also depending on used HW and circumstances there might be also faster approaches like analytical (filling BBOX based on distance from line) if fast vector math is combined with massive parallelism like on GPU.
After some tweaking in my engine (added zoom and handle some accuracy problem) I got this result:
for
128x128x128
volume inited like this: