Tag Archives: matlab

3D printing: your friend the Slicer

Modeling your shape is only the first step of 3D printing. You need to process the resulting mesh with a slicer program that will cut your solid into thin slice and decide how to print them. The output of a slicer program is a G-code file that contain the instructions to move the printer and extrude the filament. They are multiple slicers programs available but I will only mention three that were evaluated by Reprap magazine (in no particular order):

  • Cura and Slic3r are very effective open source packages
  • KISSlicer is a commercial product, but the free version let you print single object with most of the options available.

As you can discover in the Reprap magazine, there is no one solution to the slicing problem. It’s an optimization task and each program is solving the paths differently. Each of these package offers different options to reduce problems like strings / seams on the edges and increase overall printing quality.

slicerJob

In the previous picture you can see how KISSlicer is splitting this “gear”. The path colors on the right represent the type of extrusion the printer will do. Like you want to have a lower speed and more precision for the output shell, but for the inner shells, the printer can go faster.

As I was looking at the subject I found these vase pictures from 3DPrinterGear user on KISSlicer forum where you can see that depending on the printing mode, you can end-up with a very visible seam on one side, or more like a “chicken-pox” effect  when the slicer starts each profile at a different point each time.

seamOnVase

The ‘obvious’ solution was to extrude the plastic in one path, think of it as a spiral and avoid completely the issue. I found later that Cura slicer has an option called “Joris mode” to exactly do that for single wall objects. That seems like an interesting challenge to take and see how hard it would be to build a slicer and generate some G-code to print a continuous extrusion profile. As this is only considering the outer shell profiles it’s a nice subset to experiment and develop algorithm for. So let’s dig in the slicer, algorithm!

Note: all the graph on this post are generated using Matlab. The code is available at the end of the post. The gear shape is coming from this thing on Thingiverse made by Emmett.

Slicer Step 1: Find Intersecting triangles

The first step once the tessellation is read is to find for each Z plan the section of the geometry.

gearSlice

The geometry of a STL file is a tessellation of triangles in space. So the brute force method to do it is to test if each triangle is intersecting the current plan. This is obviously not optimal and we have to weed out most of the candidates otherwise we’ll spend ages processing the file.

Let’s simplify the problem and display it in 2 dimensions (only X and Z), now for each triangle we can find the lower Z vertex and the higher Z vertex. Example in triangle A the lower is at Z=2 and the higher Z=13. Then we have to sort all the minimum and the maximum in 2 lists of increasing Z values.2dSlice

Now we can have a tool to figure out which triangles are in any Z plans in 2 steps by doing:

</code>
for currentZ = 0:N
    1/ Add any triangle from the bottom list
       with bottomZ value smaller or equal than currentZ
    2/ Remove any triangle from the top list
       with topZ value smaller than currentZ
<code> 

So for example if the plan you want to test is Z = 8.5 you will have:

  1. Add in list triangles : [C A D B]
  2. Remove from list triangle C

=> the answer is only triangle [A D B] have to be tested for the section.

For a real implementation you would keep the list from one Z plane to the next and the location in the list were your stopped the search because triangle that are already under will never be used again. Also to keep the R/W low it’s probably more efficient to use a linked list in C.

Slicer Step 2 : projection of Intersecting triangle on current plan

For each of the intersecting triangle we have to check how many vertex are already in the plan as no extra computation are required in this case.

vertexInPlan

For every remaining segment of the triangle with vertex on both side, the “segment to plan” intersection is easy to compute:

segmentProjection

And here are example of the result for the gear section. The left picture is the bottom plan of the object so all the faces are in the plan and displayed as triangles of various colors. The right picture is a plane over were all the intersecting faces are represented as segment.

sliceResult

That’s all for today, the graph are generated using this Matlab package, and all code are copyrighted, only usable for non-commercial purpose and provided as is with no guaranty of any sort! The package includes a Matlab STL reader that can open TXT and binary files.

[<<Prev. – STL Matlab Writer]    [3D Printer articles]   [TBD – Next>>]

Impossible Object 3 – Spheroforms Inventor and Matlab versions

July 2013 edit: I finaly printed the spheres see pictures [here]

Here we are back to our Impossible object series, and I promise this is the last time we are going to cover the Reuleaux polygons! Those constant witdth polygons can be extended to 3 dimensions to build non-regular spheres (I.E. spheroforms).

Here is a video of one ‘sphere’ based on a revolve of the Reuleau triangle:

Here is how to build these Reuleaux spheres:

  1. Build half of the Reuleau polygons (see here how), and define the vertical segment as a center line (optional, the revolve command will let you select it anyway). Before closing the sketch, make sure that all the loops are closed with the sketch doctor tool.
    step1
  2. Use full revolve for all the polygons and the circle (you will need to share the sketch). You can now export your spheroforms in STL and print.
    step2

The Inventor file is here, and the STL is here.

These spheroforms are relatively easy to build using Matlab. Here is a parametric script that:

  1. Build the regular polygon
  2. Build the Reuleaux polygon
  3. Rotate it through one axe of symmetry to get the cloud of points
  4. Tessellate and save the result as a STL file

reuleaux_sphere_matlab

Note: The Matlab  scripts are available HERE, Launch it with the start.m script. all code are copyrighted, only usable for non-commercial purpose and provided as is with no guaranty of any sort!

As a final note there are other spheroform like Meissner’s tetrahedron but I’ve covered enough the constant width solids for the moment. Maybe in the future…

[<<Prev. – Non regular Regular Reuleaux]    [3D Printer articles]   [Penrose Triangle – Next>>]

STL geometry file writer

Most of the 3D printer and CAD software are compatible with the STL file format. One of the reason is probably because it’s the least common denominator to describe a 3D geometry tessellation (assembly of triangles files). It can be in ASCII (like a .txt) or, to save some space and accelerate the loading/writing in binary format.

I will concentrate on the binary format as it’s the easiest to handle. The structure of the file is very simple:

  1. A header of 80 character
  2. A number of face/triangle
  3. The list of triangle with:
    • Normal to the surface (optional can be set to 0)
    • 3x Vertex coordinates in space (float32)
    • 1 uin16 for the status (the usage is not normalized, can be used for color or other things)

From the from this description it’s obvious that it’s not very optimized as the normal can be computed from the 3 vertexes and each vertex is stored multiple time for each face in witch is appears. There is also no dimension units it’s just a geometry and the user will have to select before printing if the coordinates are in mm or inches. The usage of the last status bit is not very clear and quite useless.

Everything considered the good thing is it’s very simple to write a STL file. The only caveat is to write the 3 points in the right order to have the normal of the surface pointing outside of the object (uses the write hand rule).

A lot of data can be represented as a color map (the color represent the value of a particular point in the image). So I wanted to be able to transform a map into an printable STL file. The example I will take is a measurement of my hand on a Projected capacitive touch sensor that I find nice. This is the touch technology used in cellphone where each ‘pixel/electrode’ of the image is a touch intensity reading.

handThe matlab writer (zMapToSTL function) is transforming a matrix of value in a block that can be printed. That means a border and some thickness is added under the top surface. One parameter defines the number of interpolation step that needs to be done on the surface data. It is used to smooth the output but generates a lot of extra points (heavy STL).

hand_surface_noInterpGeometry output without Interpolation

The Interpolation parameter makes the number of face grow exponentially and should be used with caution 🙂

  • No Interpolation (0) gives a surface with 10800 triangles and an output STL size of 500kB
  • Interpolation (1) gives a surface with 42000 triangles and an output STL size of 2MB
  • Interpolation (2) gives a surface with 160000 triangles and an output STL size of 8MB

This is a detail of the (nicely) smooth result with interp = 2:

hand_surface_Interp2

The function prototype is:

function zMapToSTL(Zmap, fileName, interpFactor, minThickness, maxThickness, maxWidth)
% Function to convert a Zmap into a STL file + visualisation of the result
% Parameters:
%   - ZMap (compulsory) : Map to represent
%   - fileName (compulsory) : STL file name
%   - interpFactor (default 0) : Interpolation factor of the map (0 = disable)
%  Scaling parameters:
%   - minThickness (default 2) : Minimum thickness of the output solid
%   - maxThickness (default 10) : Maximum thickness of the output solid
%   - maxWidth (default = max dimension of the image) : surface scaling

Note: The Matlab  function and the image are available HERE, all code are copyrighted, only usable for non-commercial purpose and provided as is with no guaranty of any sort,

The resulting STL files are here.

[<<Prev. – TBD]    [3D Printer articles]   [What is a Slicer – Next>>]

Matlab ILD Files Format Reader (laser show animation)

When I started looking at how to find galvanometers for the scanner I quickly realized that my best chance would be to explore the laser show community to learn more about the subject. One of the topic that was coming back often was that the tuning of the “galvos” is an involved operation and to reach a good rendering one has to spend time learning hoe to do it.

Digging a bit more I discovered that the International Laser Display Association (ILDA) has defined a standard exchange format file (ILD extension) to ease the sharing of laser animations between the laserists.The association is closed to the public and most of the freely info available can be found in communities like the photolexicon site forum. The forum is really handy to grasp most of the aspects of laser shows: sample data files, home build setup, configuration and tuning, advices on the hardware…

theRiddle2
One frame decoded with Matlab of “the Riddle” animation found in the Photolexicon site.

On this post I will concentrate on the ILD file format as it’s the main way the galvo movements data, color and blanking information are stored and transmitted. I found the latest ILD file format description that includes all the types of frames commonly used in the wild. The file format is pretty simple and mostly just a collection of coordinates stored with a header. This web page describes graphically all the frame format and also points at a internal feud in the community going on for years.

All comes from the fact that the color information was poorly integrated in the original format so most of the old files rely on some default palette to choose the color of each point. These default palettes are not completely standard and can change with the software implementation. So to solve the issues the ILDA introduced new format (Frame types 4  & 5) that define 24bits full color for each point.

laserboy trueColor
8bit Palette Frame (left) and true color palette on the right (source)

Long story short, I’ve created a ILD file decoder for Matlab with a few examples data files (see at the bottom of the post). I’ve not tested it with Octave but as I’m not using any specific functions it should be pretty much portable. The reader has been optimized to load all the points/colors of a frame with one “fread” call. This enable a very fast decoding (The Riddle file ~20Mb for 5120 images is decoded on my PC in less than 4 seconds).

In an future article I will explore more the galvo tuning principles using the ILDA test patern (available here). The interesting part is that the blue circle is defined outside the square but with a proper tuning is should appears tangent inside the circle. The ILDA used this trick to standardize the dynamic behavior of the scanners mirror servos.

ILDATest               Ilda_c2_proper
The left picture is the content of the ILDA test frame.
The right picture is the proper result expected from a well tuned scanner (source)

Note: All ILD pictures in this post were generated using the Matlab decoding package available HERE, all code are copyrighted, only usable for non-commercial purpose and provided as is with no guaranty of any sort,

[<<Prev. – Scanner Architecture]    [3D Scanner articles]   [First Steps: the galvo & laser – Next>>]