STL_Import: import function for STL files

August 8, 2009 by Luigi Giaccari 
Filed under: CAD, Geometry 
22 Comments
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 9.0/10 (1 vote cast)

NormalsComputation1

STL_Import is a tool designed to import into MATLAB both binary and ASCII STL files.

This script is mainly a collage between file exchange file id 22409 and 3642, plus some other features that can be considered new.

SYNOPSIS:

  • mode 1 (default)
    [p,t,tnorm]=STL_Import(filename,mode)
  • mode 2
    [v,tnorm])=STL_Import(filename,mode)

input:

  • filename: string representing the name of the file

mode:

  • mode=1 (if omitted is automatically set to one)

set the the output to:

output=[p,t,tnorm]

where:

  • p=points (unique) of the model nx3 array
  • t=triangles indexes of the model
  • tnorm= normals of triangles
  • mode=2

set the the output to:

output=[v,tnorm]

where:

  • v= vertex (not unique points) of the model nx3 array. Each trhee points we have a triangle in consecutive order.
  • tnorm= normals of triangles

EXAMPLES:

[p,t,tnorm]=STL_Import('link1.stl',1);%MODE1
[pv,tnorm]=STL_Import('link1.stl',2);%MODE2

Author: Giaccari Luigi (giaccariluigi@msn.com)

Download Now

VN:F [1.8.8_1072]
Rating: 9.0/10 (1 vote cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
STL_Import: import function for STL files9.0101

Popularity: 5% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

Related Posts

Comments

22 Comments on STL_Import: import function for STL files

  1. Shehzad on Thu, 24th Sep 2009 11:19
  2. Dear Sir Giaccari Luigi
    Hope you are fine.

    I am trying to process different information from an STL file. your STL_Import code has been of great help.
    in mode ‘1′ we can extract

    p
    t
    tnorm

    what i want to do is to know that which tnorm vector belongs to which triangular facet from the STL file.
    if i can get that information i will to process the idea that which triangular facet belongs to which surface based on the direction
    of the normal and its comparison with the normals of the neighbor facets.

    please reply me.
    i will be greatly thankful to you sir.
    with regards
    Shehzad

    [Reply]


    Luigi Giaccari Reply:

    Dear Shehzad,

    “what i want to do is to know that which tnorm vector belongs to which triangular facet from the STL file.
    if
    i can get that information i will to process the idea that which
    triangular facet belongs to which surface based on the direction
    of the normal and its comparison with the normals of the neighbor facets.”

    Consider this:

    • v
      is not unique lists of points taken from the STL files. The first trhee
      points are the first STL facet, point 4 5 6 forms the second facet and
      so on…
    • p is a unique list of points. To map triangles we need the t array in order to “point” the points list in p. 
      For example:
      t=[ 1 10 15; 3 6 7]
      means that the first facet is formed by points 1 10 15 and the second by points 3 6 7 (points are now stored in the p array).

    This
    means that the tnorm array stays the same in both cases. Mode 1 and
    mode 2 are just 2 different way to describe a triangulation. Generally
    mode one is better for mesh processing mode 2 for graphics.

    If
    you want the neigbourood triangles you need to use mode 1 and then get
    edge to trianges connectivity. The new computational geometry toolbox
    in R2009a may help you, if you don’t have it contact me and I will give
    you something.

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  3. Andrei on Sat, 10th Oct 2009 12:04
  4. Hi Luigi Giaccari,

    I have a question.
    which is the easier way to plot (to have a graphical representation) of the object that was loaded using this function.

    Thnaks,
    Andrei

    [Reply]


    Luigi Giaccari Reply:

    Very easy,

    type in the command window:

    help trisurf

    or better:

    doc trisurf

    Bye

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  5. Edward on Fri, 6th Nov 2009 02:14
  6. Hi Luigi Giaccari,

    Thanks for sharing this m file. Right now I still couldn’t run this file, even though I have set “filename=’link1.stl’ ” after the first line. Matlab still gave me ‘invalid input number /mode setting’. Please kindly tell me the exact procedure how to run this program in MATLAB.

    PS. I have link1.stl file and set the path in the MATLAB already.

    Thanks a lot,

    Edward

    [Reply]


    Paul Reply:

    Hi,

    Maybe I can help you:

    You don’t have to change anything in the file or to add some extra lines – you don’t even have to open it. You simply have to write in the Matlab – Window Command one of the following lines:

    [p,t,tnorm]=STL_Import(‘link1.stl’,1);
    [pv,tnorm]=STL_Import(‘link1.stl’,2);

    and press enter.

    For example if you have the file ‘c:\work\object1.stl’ and you want to have the unique points, triangle index and tnorm (you have to use mode 1) then write the following command in Window Command:

    [p,t,tnorm]=STL_Import(‘c:\work\object1.stl’,1);

    Regards,
    Paul

    PS: I hope that Luigi G won’t mind that I answer to this question. By the way: nice function. Thanks you Luigi.

    [Reply]

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)

    Luigi Giaccari Reply:

    Paul’s answer is absolutely correct.

    Thanks Paul ;-)

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  7. Gav on Thu, 12th Nov 2009 19:32
  8. I was wondering, using your code, can i use it to view the stl code?…. and perhaps save it as a txt file?….

    If not do you know where I can find it?….

    [Reply]


    Luigi Giaccari Reply:

    What do you mean “see the STL code”?

    maybe you mean reading from binary and saving into ascii…
    is that what you mean?

    [Reply]


    Gav Reply:

    yes, thats what i mean, read from binary to ascii!!! thanks

    [Reply]


    Luigi Giaccari Reply:

    You can use my function to read:

    [p,t,tnorm]=STL_Import(’link1.stl’,1);

    and this function to write:
    http://www.mathworks.com/matlabcentral/fileexchange/20922-patch2stl

    but I seuggest you to code your own function, using the “fprintf” command is pretty easy.

    Luigi

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  9. fran on Sat, 5th Dec 2009 03:54
  10. i already developed the delaunay 3D triangulation and tri surface from the point cloud. but my problem is how to convert it into the STL file so that it can be read by the RP machine..? can u help me…?

    [Reply]


    Luigi Giaccari Reply:

    http://www.mathworks.com/matlabcentral/fileexchange/24400-stlexport

    Luigi

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  11. fran on Tue, 8th Dec 2009 02:13
  12. sir..
    i already download the file from the website that you give to me..

    http://www.mathworks.com/matlabcentral/fileexchange/24400-stlexport

    but i don’t understand what it mean by the “triangles_file”…
    can u explain it …..

    [Reply]


    Luigi Giaccari Reply:

    triangles_file==t

    nodes_files==p

    I guess…

    Luigi

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  13. fran on Tue, 8th Dec 2009 11:01
  14. mmm…
    how about this example of surface reconstruction that i get from the matlab file exchange..

    http://www.mathworks.com/matlabcentral/fileexchange/22185-surface-reconstruction-from-scattered-points-cloud-part1

    what is their triangles_file..?
    for node_file..it is the file .MAT?
    sorry sir.. i still new in matlab..
    i want to try convert it to stl file so that i can fabricate it in RP machine..

    [Reply]


    Luigi Giaccari Reply:

    Dear fran

    I told you all you need to do the job.

    I can give you hints but I can not make your work.

    If you want we can agree a reasonable payment and I will get your surface reconstruction work. I can get binary or ascii STL file, and you can get your rapid prototyping.

    If you are interested contact me on my e-mail

    Luigi

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  15. fran on Wed, 23rd Dec 2009 09:55
  16. sir…
    why we need the normal of the triangulation before convert it to stl file?

    [Reply]


    Luigi Giaccari Reply:

    The STL format is defined as:
    normal
    p1(x,y,z)
    p2(x,y,z)
    p3(x,y,z)

    that’s why we need the normal

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  17. adam on Wed, 17th Mar 2010 23:42
  18. Great .m file, really appreciate it.

    My questions concerns plotting the .STL file.

    I’ve used the following:

    tri=delaunay(t,tnorm)
    trisurf(tri,t,tnorm,p)

    I receive the following error:

    ??? Error using ==> horzcat
    CAT arguments dimensions are not consistent.

    Error in ==> trisurf at 40
    h = patch(‘faces’,tri,’vertices’,[x(:) y(:)
    z(:)],’facevertexcdata’,c(:),…

    I’m simply trying to the plot the output after running the program.

    THANKS!

    [Reply]


    Luigi Giaccari Reply:

     
    trisurf(t,p(:,1),p(:,2),p(:,3));

    [Reply]

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)

    adam Reply:

    Nevermind, I figured it out.

    [Reply]

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!


Include MATLAB code in your comment by doing the following:

<pre lang="MATLAB">

%insert code here

</pre>