http://wenku.baidu.com/view/a3ab7a26ee06eff9aef8077b.html
[每个三角形面片的定义包括三角形各个定点的三维坐标及三角形面片的法矢量[三角形的法线。它是一个向量,是三角形平面上的一条垂线]]
科学技术法[e+n][E(代表指数)表示将前面的数字乘以 10 的 n 次幂。]:10进制123456789 = 1.23e+7
stl格式
STL文件的最大特点也是其主要问题是,它是由一系列的三角形面片无序排列组合在一起的,没有反映三角形面片之间的拓扑关系
STL文件规则
(1)共顶点规则
每一个三角面片必须与其相邻的每一个面片共两个顶点 ,即一个三角面片的顶点不能落在相邻的任何三角面片的边上;
(2)取向规则
单个面片法向量符合右手法则且其法向量必须指向实体外面;
(3)充满规则
小三角面片必须布满三维模型的所有表面,不得有任何遗漏;
(4)取值规则
每个顶点的坐标值必须为非负 ,即 STL 文件的实体应该在坐标系的第一象限.
文件格式的结构
目前的STL文件格式包括二进制文件(BINARY)和文本文件(ASCII)两种
ASCII STL An ASCII STL file begins with the line:solid name where name is an optional string (though if name is omitted there must still be a space after solid). The file continueswith any number of triangles, each represented as follows:
solid filename stl //自定义文件头
facet normal x y z //三角面片法向量的3个坐标
outer loop
vertex v1x v1y v1z //三角面片第一个顶点坐标
vertex v2x v2y v2z //三角面片第二个顶点坐标
vertex v3x v3y v3z //三角面片第三个顶点坐标
endloop
endfacet //完成一个三角面片定义
where each n or v is a floating point number in sign-mantissa ‘e‘-sign-exponent format, e.g., "-2.648000e-002". Thefile concludes with:endsolid name The structure of the format suggests that other possibilities exist (e.g., facets with more than one ‘loop‘, or loops with more than three vertices) but in practice, all facets are simple triangles. White space (spaces, tabs, newlines) may be used anywhere in the file except within numbers or words. The spaces between ‘facet‘ and ‘normal‘ and between ‘outer‘ and ‘loop‘ are required
Binary STL
Because ASCII STL files can become very large, a binary version of STL exists. A binary STL file has an 80character header (which is generally ignored – but which should never begin with ‘solid‘ because that will lead mostsoftware to assume that this is an ASCII STL file). Following the header is a 4 byte unsigned integer indicating the number of triangular facets in the file. Following that is data describing each triangle in turn. The file simply endsafter the last triangle.
Each triangle is described by twelve 32-bit-floating point numbers: three for the normal and then three for the X/Y/Z coordinate of each vertex – just as with the ASCII version of STL. After the twelve floats there is a two byteunsigned ‘short‘ integer that is the ‘attribute byte count‘ – in the standard format, this should be zero because mostsoftware does not understand anything else.
Floating point numbers are represented as IEEE floating point numbers and are assumed to be little endian, althoughthis is not stated in documentation.
UINT8[80] – Header //which is generally ignored – but which should never begin with ‘solid‘ because that will lead most software to assume that this is an ASCII STL file
UINT32 – Number of triangles //indicating thenumber of triangular facets in the file.
foreach triangle
REAL32[3] – Normal vector
REAL32[3] – Vertex 1
REAL32[3] – Vertex 2
REAL32[3] – Vertex 3
UINT16 – Attribute byte count
end
Color in binary STL
There are at least two variations on the binary STL format for adding color information:
•The VisCAM and SolidView software packages use the two ‘attribute byte count‘ bytes at the end of every triangle to store a 15 bit RGB color:
•bit 0 to 4 are the intensity level for blue (0 to 31)
•bits 5 to 9 are the intensity level for green (0 to 31)
•bits 10 to 14 are the intensity level for red (0 to 31)
•bit 15 is 1 if the color is valid
•bit 15 is 0 if the color is not valid (as with normal STL files)
•The Materialise Magics software does things a little differently. It uses the 80 byte header at the top of the file to represent the overall color of the entire part. If color is used, then somewhere in the header should be the ASCII string "COLOR=" followed by four bytes representing red, green, blue and alpha channel (transparency) in the range 0–255.
This is the color of the entire object unless overridden at each facet. Magics also recognizes a material description; a more detailed surface characteristic. Just after "COLOR=RGBA" specification should be another ASCII string ",MATERIAL=" followed by three colors (3 × 4 bytes):
first is a color of diffuse reflection,
second is a color of specular highlight,
and third is an ambient light.
Material settings are preferred over color.The per-facet color is represented in the two ‘attribute byte count‘ bytes as follows:
•bit 0 to 4 are the intensity level for red (0 to 31)
•bits 5 to 9 are the intensity level for green (0 to 31)
•bits 10 to 14 are the intensity level for blue (0 to 31)
•bit 15 is 0 if this facet has its own unique color
•bit 15 is 1 if the per-object color is to be used
The red/green/blue ordering within those two bytes is reversed in these two approaches
– so while these formatscould easily have been compatible the reversal of the order of the colors means that they are not
– and worse still, ageneric STL file reader cannot automatically distinguish between them. There is also no way to have facets beselectively transparent because there is no per-facet alpha value
– although in the context of current rapid prototyping machinery, this is not important.
最后粘贴上,在github上找到的stl文件解析类:
https://github.com/Ultimaker/ruby-admesh/blob/master/ext/admesh/admesh/src/stlinit.c