I did in here (Your first link is its duplicate)
You will find there more than just cross-section and also C++ 4D tesseract example with cross-section rendering (without too).
Now what you are asking is how to compute intersection between edge of geometry and axis alligned 4D hyperplane w = constant
? That is easy as edge is line in really defined by two points p0,p1
so you can use linear interpolation for this:
p(t) = p0 + (p1-p0)*t
this will give you any point in the line while t = <0,1>
is scalar linear parameter defining where on the line p(t)
is.
p(0) = p0
p(1) = p1
p(0.5) = mid point between p0,p1
Now you just want to solve t
so the w
is equal to your constant let call it w_cut
as cutting plane.
p(t).w = w_cut
p0.w + (p1.w-p0.w)*t = w_cut
t = (w_cut-p0.w) / (p1.w-p0.w)
if t
is inside <0,1>
interval the edge intersect your plane. if (p1.w-p0.w)=0
entire edge is in the plane.
Now as mentioned in the linked answer this does not deal with topology so you would obtain points and edges but not inter-connection info on how to construct 3D geometry from them and thorough analysis is needed to do so. So much better is organize your mesh in a form of tetrahedrons and inspecting intersection of its triangles instead of edges alone.
So you inspect 3 edges of each triangle of tetrahedron. Each triangle will convert after intersection to:
nothing - ignore
single point - remember it
single edge - remember its 2 points
whole triangle - remember its 3 points
Remove duplicate points and after that you should have a list of points (0,3
or 4
points) of the tetrahedron after intersection so:
0
points - ignore
3
points - render triangle
4
points - render tetrahedron
there is also a possibility for 1
and 2
points but you can ignore them unless you want to render also infinitely thin lines and points in which case render them. For more info inspect this function in the link above:
void mesh4D::draw_cut(double w_cut)
it does exactly as I described here. The only problem with this is that we lose the polygon winding. But that can be repaired by doing dot between the normal_of_triangle
and vector center_of_triangle - center_of_tetrahedron
if the sign is negative the normal is pointing inward. So if yo know which way you want to point reverse order of triangle points if wrong direction is present.