Search code examples
pythonc++graphics3dimaging

3D *.stl surface model convert to 2D image stack?


OK to start with let me be clear, I am not interested in converting an image stack into a 3D model.

I have an *.stl file (a triangulated surface mesh) & I would like to slice it back into an image stack. I've had a look at Slic3r & Meshmixer but they both only give out Gcode.

So given I have the vertices of all the points on the surface (which is NOT convex incidentally) & their connectivity. What libraries are out there that could help with this?

My feeling is that I would need to interpolate the boundary on slices that did not pass through known vertices.

I'm comfortable with Python & C++ at a push but am willing to broaden my horizons.


Solution

  • For example if you got your mesh to render with OpenGL (by any means inside your app) then to get your slice you would simply:

    1. set your camera so screen projection plane is parallel to the slice...
    2. clear screen buffer as usual with glClearColor set to background color
    3. Clear your depth buffer with glClearDepth set to Z-coordinate of the slice in camera space
    4. set glDepthFunc(GL_EQUAL)
    5. render mesh

    Something like:

    // here set view
    glClearColor( 0.0,0.0,0.0,0.0 ); // <0.0,1.0> r,g,b,a
    glClearDepth( 0.5 );             // <0.0,1.0> ... 0.0 = z_near, 1.0 = z_far 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDepthFunc(GL_EQUAL);
    // here render mesh
    

    This will render only the slice for which fragments have Z==Slice coordinate. This can be also done by GLSL by throwing away all fragments with different Z. The DirectX should have something similar (I do not use it so I do not know for sure).

    As most meshes are BR models (hollow) then you will obtain circumference of your slice so you most likely need to fill it afterwards to suite your needs...

    You can also experiment with rendering a thick slice ... where Z is around a predefined value ...