Code covered by the BSD License  

Highlights from
Binary STL file reader

4.66667

4.7 | 9 ratings Rate this file 110 Downloads (last 30 days) File Size: 2.44 KB File ID: #29906

Binary STL file reader

by

 

This binary STL file reader is much faster than the existing readers.

| Watch this File

File Information
Description

This function is used for loading binary STL files.

There are already two stlread.m files available on the mathworks file exchange site. Harlev's version reads in the data and returns matrices with X/Y/Z formatted vertex data. Johnson's program reads in the data and returns Face/Vertex/Normal matrices.

This submission also exports Face/Vertex/Normal matrices, but runs about 30 times faster than Johnson's version and 13 times faster than Harlev's version (which doesn't export face normal data).

This submission will also properly read in color values (if present) and the STL file title.

For more info (including timing info) see: http://www.esmonde-white.com/home/diversions/matlab-program-for-loading-stl-files

Acknowledgements

Stlread and Stl File Reader inspired this file.

This file inspired Binary Stl File Reader, Patch Slim (Patchslim.M), Show 3 D Model, Generate U3 D Files From Stl Models For Making Multilayer 3 D Pdf Figures, and Compute Stl Vertex Normals.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (29)
05 May 2014 negar

Hi. thanks for this program. can you send me flowchart?
Best regards.

28 Aug 2013 Emmanuel Farhi

A very good job to read STL binary files. Thanks.

05 Jul 2013 Andreas J.  
03 Jun 2013 Francis Esmonde-White

Dear Rooha,

This stl file reader loads binary STL files. I am unfamiliar with Catia, or the type of STL files exported by Catia. If you do not have a binary STL file, you should try one of the ASCII STL file readers available on the File Exchange.

Have you read the help for this program? When you run this program, it will output the faces, vertices and normals from the STL model.

I also find it odd that you rated the program as three stars, but do not seem to have used it at all.

02 Jun 2013 Rooha Siddiqui

Hello ,
I have a problem . I have saved a catia figure in stl format
now i want to read its vertices and normals in MATlab
can you please help me with the function .. this is related to my final year project.

10 Jan 2012 Bouta

Hello,

I have a problem with importing an STL graphics file to the Simulink diagram. I've tried making the model both in UGS NX and SolidWorks but when I import it, the dimensions are multiplied a 1000 times.

I think it's because Simulink's default lenght unit is 'm' but every CAD software writes it in 'mm'. As I look-up into the binary file, there's no real information about the units in the STL file so my question is: how do I change the default unit in Matlab or how can I convert the STL file itself to match my requirements.

14 Dec 2011 Francis Esmonde-White

Hi Ye,

You can definitely change the patch face (or patch edge) colors, and even specify transparency. For more information, I suggest you read the mathworks/matlab documentation for the patch command. For example, type 'doc patch' at the command line, and look at the section under 'Coloring Patches'.

16 Nov 2011 YE HUICHAO

Hi Francis,

Thank you for your file which helped a lot in the project. I just don't understand that when I run the file to open my stl file ,the plot is right but the color is totally black.Is that a mesh?Can I change the color with patch('Faces',f,'Vertices',v,'r');?

12 Oct 2011 Francis Esmonde-White

The STLVertexNormals submission is now available at http://www.mathworks.com/matlabcentral/fileexchange/33226-compute-stl-vertex-normals

11 Oct 2011 Francis Esmonde-White

Hi David,

I have submitted a new file to the Mathworks file exchange which calculates patch normals using a triangle-area based smoothing method. It will take a while to be approved by the mathworks, so I'll email it to you in the meantime. I thought that this was an interesting exercise, so I created my own function called 'STLVertexNormals'. The compiled 'Patch Normals' submission you linked to is almost certainly faster (I haven't played around with it), but the code I have submitted is (hopefully) quite readable.

Cheers,
Francis

10 Oct 2011 David Robertson

Hi Francis,

First off thank you very much for taking the time to post this function it is very easy to use and does exactly what it says. I just have one question, the function returns a normal matrix and going by the size of it I assume it contains the face normals. But Matlab uses the Vertex normals when doing lighting calculation for a patch. Apparently it can calculate these normals itself for performing these calculations but this does not seem to be working for me. So I was wondering if you may know how to translate these face normals into vertex normals? so I can force Matlab to use them for lighting.

I have looked into adapting this function
http://www.mathworks.com/matlabcentral/fileexchange/24330-patch-normals
because he seems to calculate the face normals first then uses them to calculate the vertex normals like i would like to do but have not had any success with it to date.

If you have any ideas that may help me this this it would be very appreciated.

Either way, thanks again for sharing this function.

Kind regards,
David

09 Oct 2011 David Robertson  
08 Sep 2011 Francis Esmonde-White

Hi Antonio,

The problem is that you have an acsii STL, not a binary STL. The binary file reader assumes that the first few bytes describe the number of faces. In ascii STL files, the first few bytes will be text instead, and define some arbitrarily large numbers. If you can export to binary files instead, they will be *much* smaller and load more quickly.

If you open the file with wordpad, you can see the ascii text. Try this m-file instead: http://people.sc.fsu.edu/~jburkardt/m_src/stla_io/stla_io.html

I have more information about STL files on my personal web site: http://www.esmonde-white.com/home/diversions/matlab-program-for-loading-stl-files

Please let me know if I can do anything else to help!

Cheers,
Francis

08 Sep 2011 Antonio de Nittis

I have 1.7022e+009 faces for 55mb stl file and same problem for the 1mb stl file and the code found 1.9193e+009 faces (var numFaces). is it normal?

07 Sep 2011 Francis Esmonde-White

Dear Antonio,

A 55 MB binary STL file is quite large. Each triangle face of your model requires 50 bytes. Since your larger file is 55 MB I'll estimate the number of faces as 1.15 million triangles based on (55*1024*1024)/50.

Line 72 of the program will require about 3 copies of the 1.15 million triangle matrix (with each byte inefficiently stored as 8 bytes- the matlab double precision format). This will take at least 1.32 GB of memory. In addition, you will need another matrix of the same size for the actual data, requiring a total of 1760 MB (I'm probably underestimating), which must be exceeding your system memory limit (what are the specs of the computer are you trying to run this on?). Could you make a slightly lower resolution model, or break it into pieces?

I can also play some tricks to reduce the memory footprint of the program if necessary, such as reducing the numerical precision of the intermediate matrices and reading the data from the file only after creating the "ind" matrix. Some of these changes will slow the program down, others will pose limitations on the total number of triangle faces that can be handled, so making these changes are not ideal.

Have you tried loading the smaller model? There should be no issues loading a 1 MB stl file, even on an antiquated old PC.

Thanks!

07 Sep 2011 Antonio de Nittis

Dear Francis,
thanks for submitting this m-file. I encountered one problem: i have two stl file (55mb and 1mb) and matlab says
"Maximum variable size allowed by the program is exceeded.

Error in ==> stlread at 72
ind =
reshape(repmat(50*(0:(numFaces-1)),[48,1]),[1,48*numFaces])+repmat(trilist,[1,numFaces]);"
is it possible to solve this problems?

Thanks and your help is appreciated in advance.

22 Jul 2011 Francis Esmonde-White

Hi Qais,

1. Lee White described using patch objects on this page, his comment is immediately before your first comment. For more information, look at:
http://www.mathworks.com/help/techdoc/ref/hgtransform.html

2. If you have an assembly, then you should also have files for each individual part forming the assembly. You can create STL files from each part separately.

3. You can load the STL files for each individual part, then use HGtransform to assemble the parts virtually. This will be more difficult than using the assembly tools directly in solidworks. If you create the STL from the solidworks assembly, there is no straightforward way to re-associate the triangles with the original parts.

19 Jul 2011 Qais AlKhazraji

Dear Francis,
I am very thankful for your replay. I am still confuse about the following points you made above:
1. Where did Lee White describe Parenting "patch objects to hgtransform objects to manipulate patches built from stls" as I do not know what he means by (hgtransform).
2.If I have assembly part contains from several parts , I am not able to create individual stl file for each part since the whole part is one piece as you know.
3.Do you have any thought how I could associate triangles with original parts that come from?
Thanks and your help is appreciated in advance.

18 Jul 2011 Francis Esmonde-White

Hi Qais,

Unfortunately, if the model is a single STL file, it has probably been turned into a single patch object (a single closed surface described by triangles). While you could probably determine a way of associating certain triangles with the original parts that they come from, you won't have a description of the whole part. There is no way that you will be able to recover exact Solidworks models from the STL files. If you want to load the STL models into Matlab from Solidworks, you should export each part as an STL file, then load them all into matlab as different STL models, and transform them as described by Lee White above. Also, the STL models will be imperfect representations of the parts (curved surfaces will be approximated by a tesselated surface).

I hope that is of some help.

18 Jul 2011 Qais AlKhazraji

Hello Francis
I am interesting in subdivide the stl file to patches in which each patch represent subpart that build an object. I am doing assembly technique where assembly part contain several parts and I draw them in SolidWorks. From my knowledge, that STL file is unorder file so that I lost tracking in stl file which data are related to which subpart. When I copy some of the stl file output and redraw them using SolidWork, I will get some parts which are not what my original parts were. I am highly appreciate your help in this issue and how can I get ordering stl file so it will reflect my exact data representation on SolidWork.

15 Jul 2011 Lee White

Works great. Liana, check out parenting the patch objects to hgtransform objects to manipulate patches built from stls. Each hgtranform object has a position and orientation defined by a transformation matrix. You can create serial mechanisms this way like robotics arms. The hgtransform objects are parented to each other in a chain. Moving a parent objects moves all children with it.

30 Jun 2011 iokinberistain Beristain  
26 Apr 2011 Francis Esmonde-White

Hi Liana,

The STLread program doesn't move the model at all, and there is no functionality for rotating the 3D matrix included in this code. A 3D rotation can be performed by multiplying all the vertices by an appropriate rotation matrix. See http://en.wikipedia.org/wiki/Rotation_matrix#Rotations_in_three_dimensions for more information about rotation matrices.

If I have time, I will write and post a program for 3D model rotation.

25 Apr 2011 anup kumar

Hi Francis! I downloaded the file to Matlab directory and renamed my stl file to MyModel.stl.

At the first line of the code, I changed stlread(filename,verbose) to stlread('MyModel.stl'). I've tried all sorts of thing but I keep getting this error,

/////////
??? Error: File: stlread.m Line: 1 Column: 43
Unexpected MATLAB expression.

Thanks!

09 Apr 2011 Lulu

Hi Francis!

Thank you for this function, it's very helpful. Could you please tell me how can I change the orientation of the loaded model according to the XYZ zero point using outputs of your function? Let's say, I want that a polyhedral object (simple house model) loaded from STL file would lay on the XY plane instead of the ZX plane. Thanks!

07 Apr 2011 Francis Esmonde-White

Hi Arielle,

I'm not quite sure why you are interested in breaking the object up into multiple segments. Is it that you want to have a composite object where different parts can move relative to each other? If so, just treat each subsection as an independent object. I would be happy to help, please send me an email with more information about the part you're trying to load (francis*removethis*@esmonde-white.com).

Also, please include more information about exactly how you're using the file loader. From the error message, it's possible that the STL file isn't quite right (perhaps it is an ascii stl file or it doesn't conform to standards) or that you're using the program incorrectly.

Francis

07 Apr 2011 Arielle

Hello. I am interested in dividing a stl.file into different segments (defined by vertices, so that I can load individual parts of an object at a time. I would like to be able to save each part of the object separately and have the ability to load them together as an assembly to form the whole object. Would this function be helpful? I tried it out with a simple part I made with Unigraphics and when I tried to display the model with patch(...), I got an error meesage saying

??? Error using ==> patch
Faces values must be >= 1.0
Vertices array must have at least 2 and at most 3 columns (or be empty)
FaceVertexCData must be an Mx1 vector (indexed colors) or Mx3 matrix (RGB colors)

Do you know what my issue may be? The file was saved as a binary stl.

Thank you.

29 Mar 2011 Francis Esmonde-White

Hi Quadro,

You should not need to change any code in the stlread.m file.

For more information on using 'stlread', download the stlread.m file, and copy it into your matlab working directory (the directory shown in the address bar at the top of your matlab window). Next, type 'help stlread', and it will display the program help information.

To use the program, use the following command to load your model.

[v, f, n, c, stltitle] = stlread('MyModel.stl');

This will read the model into the v and f matrices. To display the model, try starting with:

patch('Faces',f,'Vertices',v,'FaceVertexCData',c);

One important consideration, is whether or not your STL file is a binary or ascii STL file. If you try to open your STL model with a text editor (like wordpad), if you can see a clear list of numbers, then you have an ascii STL file. The above program only works for binary STL files. If you need an ascii STL reader, try looking at http://www.mathworks.com/matlabcentral/fileexchange/24233-stlimport-import-function-for-stl-files

29 Mar 2011 quadro1327

thank you for submitting this m-file. could you guide me on how to read my stl file. i am a beginner in matlab. do i need to change some lines in your program? thank you again.

Contact us