I am trying to approximate 3d triangles with rectangular prisms but I cant figure out the math
Im using lua
Ive tried flattening the triangle to a 2d plane and approximating the triangles with normal rectangles and then applying rotation again but couldnt figure it out
Ive also tried voxelization but I am trying to use as little rectangular prisms as I can

I have been trying to find a big rectangle inside the triangle by drawing a line from the center of the two smallest lines as seen in the picture ^ but I am struggling how to figure out how to find the two points on the bigger line (keep in mind this is 3d)
you got 2 options either use aligned quads (no holes) or arbitrary quads (holes but better coverage)...
The idea is simply take biggest or alignment side of triangle and fit biggest area quad inscribed in it. here simple C++ example for both:
preview for
triangle2quads_aligned(tri[0],tri[1],tri[2]);:and preview for
triangle2quads(tri[0],tri[1],tri[2]);:The number in Caption of window is number of quads...
The
quad[]is output list of points 4 per quad. The code is not heavily optimized yet and the recursion can be improved a lot...I used mine libs so few explanations...
I used mine dynamic list template
list.hso:List<double> xxx;is the same asdouble xxx[];xxx.add(5);adds5to end of the listxxx[7]access array element (safe)xxx.dat[7]access array element (unsafe but fast direct access)xxx.numis the actual used size of the arrayxxx.reset()clears the array and setxxx.num=0xxx.allocate(100)preallocate space for100itemsYou can use anything you have at disposal instead like
vector<>or whatever (even simple array)for vector math I used GLSL like math
GLSL_math.hwhich can be found in here but you can use any other like GLM instead or encode it yourself you need just basic operations like+,*,dot.for the search I used mine approximation search
approx.hwhich can be found (and its described) in here:You could use linear search or bisection instead (binary search is not usable).
Now back to the problem if you look into both implementations you see they almost identical first find
u,vdirections and then searchv1and computeu0,u1parameters from it and remember biggest area quad.After that just add the quad to output list and recursively repeat for 3 remainder/leftover triengles...
The only difference is that aligned version search
uonly for first triangle and from then always align to the first quad (last two points passed to_triangle2quads_aligned).I avoided goniometrics and use vector math instead I used identities:
where
ais angle betweenu,vandu,vare unit vectors.I used coordinates in pixels so in case you have different scale you have to change the ending thresholds to value usable to your case. In code lines:
means triangle area must be bigger or equal to 400/2 pixels^2 so just change the 400 to whatever suits you.
Now the last thing I used QUADs as output if you want boxes (prisms) instead just shift the 4 points perpendicularly by box thickness to get the 8 points of your box ... the shift direction is
w=cross(u,v)If you want the triangle in the middle of thickness then for each guad
p0,p1,p2,p3output box: