STL文件及其读取

1引言

STL(Stereo lithographic)文件格式是美国3D SYSTEMS公司提出的三维实体造型系统的一个接口标准,其接口格式规范。采用三角形面片离散地近似表示三维模型,目前已被工业界认为是快速成形(rapid prototypi ng)领域的标准描述文件格式。在逆向工程、有限元分析、医学成像系统、文物保护等方面有广泛的应用。

STL文件的最大特点也是其主要问题是,它是由一系列的三角形面片无序排列组合在一起的,没有反映三角形面片之间的拓扑关系。

2 STL文件格式的结构

STL文件是一种用许多空间小三角形面片逼近三维实体表面的数据模型,STL模型的数据通过给出组成三角形法向量的3个分量(用于确定三角面片的正反方向)及三角形的3个顶点坐标来实现,一个完整的STL文件记载了组成实体模型的所有三角形面片的法向量数据和顶点坐标数据信息。目前的STL文件格式包括二进制文件(BINARY)和文本文件(ASCII)两种。

2.1 STL的二进制格式

二进制STL文件用固定的字节数来给出三角面片的几何信息。文件起始的80个字节是文件头,用于存贮零件名;紧接着用4个字节的整数来描述模型的三角面片个数,后面逐个给出每个三角面片的几何信息。每个三角面片占用固定的50个字节,依次是3个4字节浮点数(角面片的法矢量),3个4字节浮点数(1个顶点的坐标),3个4字节浮点数(2个顶点的坐标),3个4字节浮点数(3个顶点的坐标),最后2个字节用来描述三角面片的属性信息。一个完整二进制STL文件的大小为三角形面片数乘以50再加上84个字节,总共1 34个字节。

UINT8[80]         -   Header
UINT32            -   Number of triangles
foreach triangle
  REAL32[3]       -    Normal vector
  REAL32[3]       -    Vertex 1
  REAL32[3]       -    Vertex 2
  REAL32[3]       -    Vertex 3
  UINT16          -    Attribute byte count
end

 

2.2 STL的ASCII文件格式

ASCII码格式的STL文件逐行给出三角面片的几何信息,每一行以1个或2个关键字开头。在STL文件中的三角面片的信息单元facet是一个带矢量方向的三角面片,STL三维模型就是由一系列这样的三角面片构成。整个STL文件的首行给出了文件路径及文件名。在一个STL文件中,每一个facet由7行数据组成,facetnormal是三角面片指向实体外部的法矢量坐标,outer loop说明随后的3行数据分别是三角面片的3个顶点坐标,3顶点沿指向实体外部的法矢量方向逆时针排列

ASCII格式的STL文件结构如下:

 

通过对STL两种文件格式的分析可知,二进制格式文件较小(通常是ASC¨码格式的1/5),节省文件存储空间,而ASCII码格式的文件可读性更强,更容易进行进一步的数据处理。

3三角片法矢量的计算,注意,点为v1 v2 v3逆时针排列

读取STL文件时,只需要读取STL文件中表示向量和三角形顶点的相应数据,不需要读文件中的其它信息。依次按逆时针方向读入各个三角形面片的3顶点坐标值。由于三角面片外法矢量可以通过右手螺旋法则由3顶点坐标值计算出来,因此可不对其进行存储,以节省存储空间。如果后续处理需用到法矢量,

可利用以下的外法矢量计算公式:

 

下面分别是 维基百科 对stl文件的论述,和我写的stl文件读取vc程序

 

STL (file format)        http://en.wikipedia.org/wiki/STL_file

From Wikipedia, the free encyclopedia
(Redirected from  STL file)
Jump to:  navigation,  search
This article is about the stereolithography format.  For the C++ programming language template library, see Standard Template Library.
STL
Filename extension.stl
Developed by3D Systems
Type of formatStereolithography

STL (STereoLithography) is a file format native to the stereolithography CAD software created by 3D Systems. STL is also known as Standard Tessellation Language[1] This file format is supported by many other software packages; it is widely used for rapid prototyping and computer-aided manufacturing. STL files describe only the surface geometry of a three dimensional object without any representation of color, texture or other common CAD model attributes. The STL format specifies bothASCII and binary representations. Binary files are more common, since they are more compact.[2]

An STL file describes a raw unstructured triangulated surface by the unit normal and vertices (ordered by theright-hand rule) of the triangles using a three-dimensional Cartesian coordinate system.

Contents

[ hide]
  • 1ASCII STL
  • 2Binary STL
  • 3Color in binary STL
  • 4The facet normal
  • 5History of use
  • 6Use in other fields
  • 7See also
  • 8References
  • 9External links

[edit]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 continues with any number of triangles, each represented as follows:

facet normal ni nj nkouter loopvertex v1x v1y v1zvertex v2x v2y v2zvertex v3x v3y v3zendloopendfacet

where each n or v is a floating point number in sign-mantissa 'e'-sign-exponent format, e.g., "-2.648000e-002". The file 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.[2]

[edit]Binary STL

Because ASCII STL files can become very large, a binary version of STL exists. A binary STL file has an 80 character 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). 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 ends after 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 byte unsigned 'short' integer that is the 'attribute byte count' – in the standard format, this should be zero because most software does not understand anything else.[2]

Floating point numbers are represented as IEEE floating point numbers and the endianness is assumed to belittle endian although this is not stated in documentation.

UINT8[80] – HeaderUINT32 – Number of trianglesforeach triangleREAL32[3] – Normal vectorREAL32[3] – Vertex 1REAL32[3] – Vertex 2REAL32[3] – Vertex 3UINT16 – Attribute byte countend

[edit]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 bitRGB 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 theASCII string "COLOR=" followed by four bytes representing red, green, blue andalpha 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 ofdiffuse reflection, second is a color ofspecular 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 formats could easily have been compatible the reversal of the order of the colors means that they are not – and worse still, a generic STL file reader cannot automatically distinguish between them. There is also no way to have facets be selectively transparent because there is no per-facet alpha value – although in the context of current rapid prototyping machinery, this is not important.

 

 

下面vc程序

class CSTL
{
private:
 float *vx;
 float *vy;
 float *vz;

 int *triaV1;
 int *triaV2;
 int *triaV3;

 int m_TriaNum;
public:

 CSTL();
 ~CSTL();
public:
 bool SetStlInf(float *px,float *py,float *pz,
       int *TriaNum1,int *TriaNum2,int *TriaNum3,int TriaTotalNum);
 bool SaveStlBinary(char *pathname,char * filename);
 bool SaveStlASCII(char *pathname,char * filename);


};

 

 

 


CSTL::CSTL()
{
}

CSTL::~CSTL()
{
}

bool CSTL::SetStlInf(float *px, float *py, float *pz,
      int *TriaNum1, int *TriaNum2, int *TriaNum3, int TriaTotalNum)
{
 bool success = false;

 vx = px;
 vy = py;
 vz = pz;

 triaV1 = TriaNum1;
 triaV2 = TriaNum2;
 triaV3 = TriaNum3;

 m_TriaNum = TriaTotalNum;
 
 success = true;
 return success;
}

bool CSTL::SaveStlASCII(char *pathname,char * filename)
{
 bool suc = true;
 char *savename = new char[100];
 sprintf(savename,"%s%s.ast",pathname,filename);

 char *fileInf = new char[200];
 sprintf(fileInf,"solid %s.ast  %s",filename,"by master");

 FILE *fp = fopen(savename,"w");
 fprintf(fp,"%s\n",fileInf);
 delete []savename;

 for(int i=0;i<m_TriaNum;i++)
 {
  int id= triaV1[i];
  float v1x = vx[id];
  float v1y = vy[id];
  float v1z = vz[id];
  
  id= triaV2[i];
  float v2x = vx[id];
  float v2y = vy[id];
  float v2z = vz[id];

  id= triaV3[i];
  float v3x = vx[id];
  float v3y = vy[id];
  float v3z = vz[id];

  float nx = (v1y-v3y)*(v2z-v3z)-(v1z-v3z)*(v2y-v3y);
  float ny = (v1z-v3z)*(v2x-v3x)-(v2z-v3z)*(v1x-v3x);
  float nz = (v1x-v3x)*(v2y-v3y)-(v2x-v3x)*(v1y-v3y);

  float nxyz = sqrt(nx*nx+ny*ny+nz*nz);

  fprintf(fp,"facet normal %f %f %f\n",nx/nxyz,ny/nxyz,nz/nxyz);
  fprintf(fp,"outer loop\n");
  fprintf(fp,"vertex %f %f %f\n",v1x,v1y,v1z);
  fprintf(fp,"vertex %f %f %f\n",v2x,v2y,v2z);
  fprintf(fp,"vertex %f %f %f\n",v3x,v3y,v3z);
  fprintf(fp,"endloop\n");
  fprintf(fp,"endfacet\n");

 }
 sprintf(fileInf,"endsolid %s.ast  %s",filename,"by master");
 fprintf(fp,"%s\n",fileInf);
 fclose(fp);

 delete []fileInf;
 


 return suc;
}

bool CSTL::SaveStlBinary(char *pathname,char * filename)
{

 bool suc = true;
 char *savename = new char[100];
 sprintf(savename,"%s%s.stl",pathname,filename);

 char *fileInf = new char[200];
 sprintf(fileInf,"solid %s.stl  %s",filename,"by master");

 FILE *fp = fopen(savename,"wb");
 delete []savename;

 
 float* dat = new float[12];

 fwrite(fileInf,sizeof(char),80,fp);
 fwrite(&m_TriaNum,sizeof(int),1,fp);

 for(int i=0;i<m_TriaNum;i++)
 {
  int id= triaV1[i];
  float v1x = vx[id];
  float v1y = vy[id];
  float v1z = vz[id];
  
  id= triaV2[i];
  float v2x = vx[id];
  float v2y = vy[id];
  float v2z = vz[id];

  id= triaV3[i];
  float v3x = vx[id];
  float v3y = vy[id];
  float v3z = vz[id];

  float nx = (v1y-v3y)*(v2z-v3z)-(v1z-v3z)*(v2y-v3y);
  float ny = (v1z-v3z)*(v2x-v3x)-(v2z-v3z)*(v1x-v3x);
  float nz = (v1x-v3x)*(v2y-v3y)-(v2x-v3x)*(v1y-v3y);

  float nxyz = sqrt(nx*nx+ny*ny+nz*nz);
  
  dat[0] = nx/nxyz;
  dat[1] = ny/nxyz;
  dat[2] = nz/nxyz;

  dat[3] = v1x;
  dat[4] = v1y;
  dat[5] = v1z;

  dat[6] = v2x;
  dat[7] = v2y;
  dat[8] = v2z;

  dat[9] = v3x;
  dat[10] = v3y;
  dat[11] = v3z;

  fwrite(dat,sizeof(float),12,fp);
  fwrite("wl",sizeof(char),2,fp);

 }

 fclose(fp);

 delete []dat;
 delete []fileInf;
 


 return suc;

}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1379709.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

Python-OpenCV中的图像处理-直方图

Python-OpenCV中的图像处理-直方图 直方图统计直方图绘制直方图Matplotlib绘制灰度直方图Matplotlib绘制RGB直方图 使用掩膜统计直方图直方图均衡化Numpy图像直方图均衡化OpenCV中的直方图均衡化CLAHE 有限对比适应性直方图均衡化 2D直方图OpenCV中的2D直方图Numpy中2D直方图 直…

基于frida检测demo来学习frida检测及anti

原文地址:https://www.zhuoyue360.com/crack/108.html 前言 随着逆向的攻防强度不断的提升,目前主流的移动安全厂商的加固服务基本上都已包含了常见Hook框架的反调试,我们最常见的hook工具如下: fridaxposed 为了更好的提升自己相关的经验,我们可以拿这类demo来进行原理的学…

腾讯云轻量应用服务器镜像应用模板清单大全

腾讯云轻量应用服务器支持多种应用模板镜像&#xff0c;Windows和Linux镜像模板都有&#xff0c;如&#xff1a;宝塔Linux面板腾讯云专享版、WordPress、WooCommerce、LAMP、Node.js、Docker CE、K3s、宝塔Windows面板和ASP.NET等应用模板镜像&#xff0c;腾讯云服务器网分享腾…

聊一下互联网开源变现

(点击即可收听) 互联网开源变现其实是指通过开源软件或者开放源代码的方式&#xff0c;实现收益或盈利。这种方式越来越被广泛应用于互联网行业 在互联网开源变现的模式中&#xff0c;最常见的方式是通过捐款、广告、付费支持或者授权等方式获利。 例如&#xff0c;有些开源软件…

Linux 基础(五)常用命令-文件属性

文件属性 文件权限文件属性修改文件权限属性 文件所有者 文件权限 文件属性 Linux中文件权限 可以通过文件属性体现&#xff1b; 使用 ll 查看文件列表 最前面的 l d 表示文件类型 1 5 表示硬链接数 或者 子文件夹个数 所属用户 所属用户组 文件大小 创建/更新时间 文件&…

首个女性向3A手游要来了?获IGN认可,《以闪亮之名》能否突出重围

最近的手游市场可以说是热度十足&#xff0c;各大厂商都发布了旗下新作的消息&#xff0c;3A高自由度似乎成了所有新游的主基调&#xff0c;但说起与众不同&#xff0c;那便不得不说这款《以闪亮之名》&#xff0c;这是本季度新游中唯一一个女性向3A作品。 这款手游主打超自由时…

2009年“五一”假期市民旅游指南

信息来源于&#xff1a;上海旅游官网 为使广大市民更好地领略上海的都市风情&#xff0c;满足 市民的旅游消费需求&#xff0c;丰富节日生活 &#xff0c;本市部分景点 、旅游企业 精心策划&#xff0c;积极准备&#xff0c;推出一系列适合市民市内旅游的节目&#xff0c;在…

常州嬉戏谷游玩全攻略

攻略导读&#xff1a;常州环球动漫嬉戏谷&#xff0c;一座国际动漫游戏体验博览园&#xff0c;颠覆传统&#xff0c;突破创新&#xff0c;定位鲜明&#xff0c;以满足逾4亿中国互联网用户的庞大娱乐需求为目标&#xff0c;以更适合未来前往的体验型公园为前瞻。假面文化的“梦幻…

第四十八周周报

学习目标&#xff1a; 修改ViTGAN 学习内容&#xff1a; 位置编码和多尺度 学习时间&#xff1a; 8.5-8。12 学习产出&#xff1a; 这两周主要工作在修改ViTGAN的结构和代码&#xff0c;将相对位置编码加入ViTGAN并将生成器变为多尺度&#xff0c;由于匹配维度很困难&am…

Jpa与Druid线程池及Spring Boot整合(二): spring-boot-starter-data-jpa 踏坑异常处理方案

Jpa与Druid线程池及Spring Boot整合(一) Jpa与Druid线程池及Spring Boot整合(二)&#xff1a;几个坑 附录官网文档&#xff1a;core.domain-events域事件 从聚合根发布事件 存储库管理的实体是聚合根。在领域驱动设计应用程序中&#xff0c;这些聚合根通常会发布领域事件。Sp…

pass 软件_PASS软件非劣效Logrank检验的h1参数如何设置?

前言 近日&#xff0c;有朋友在《统计咨询》公众号咨询&#xff1a;在使用PASS中的Non-Inferiority Logrank Tests程序计算样本量时&#xff0c;h1(Hazard Rate of Reference Group) 这个参数不懂得如何设置&#xff1f;见下图红色矩形标注的参数。相信这个也是其他很多朋友碰…

简单聊聊什么是Sass、Pass和Iass?

Iass&#xff0c;Pass和Saas都是什么意思&#xff1f;想必大家都听过也查阅过资料。但现在网上很多文章都会把一些比较简单的概念包装得非常牛气&#xff0c;逼格很高&#xff0c;各种高大上就是不说大白话&#xff0c;本文正好通过搭建网校平台为例和小伙伴简单分享一下它们之…

项目如何简单的使用pass平台部署服务

目录 前言&#xff1a; 一&#xff1a;Pass平台的优势 二&#xff1a;Pass平台的相关要素 三&#xff1a;docker|jenkins\k8s\pass\git之间关系 四&#xff1a;项目如何使用pass 五&#xff1a;pass平台常规操作 5.1应用重启 5.1.1定位到命名空间下的容器项目 5.1.2服务…

云计算之IasS、PasS、SaaS

越来越多的软件&#xff0c;开始采用云服务。 云服务只是一个统称&#xff0c;可以分成三大类。 IaaS&#xff1a;基础设施服务&#xff0c;Infrastructure-as-a-servicePaaS&#xff1a;平台服务&#xff0c;Platform-as-a-serviceSaaS&#xff1a;软件服务&#xff0c;Softw…

pass平台的搭建

Docker容器化部署Rancher CentOS 7.0默认使用的是firewall作为防火墙 查看防火墙状态 firewall-cmd --state ​ 停止firewall systemctl stop firewalld.service ​ 禁止firewall开机启动 systemctl disable firewalld.service ​ 安装rancher docker run -d --restart=unless-…

pass,saas区别

毫无疑问&#xff0c;PaaS与企业在服务与开发过程中的需求密切相关&#xff0c;特别是随着云计算的发展和企业平台化战略的驱动&#xff0c;企业对于云原生应用和全新的应用开发都提出了更高要求&#xff0c;而PaaS作为“承上启下”的中间层也变得越来越重要&#xff0c;更成为…

saas,pass介绍

毫无疑问&#xff0c;PaaS与企业在服务与开发过程中的需求密切相关&#xff0c;特别是随着云计算的发展和企业平台化战略的驱动&#xff0c;企业对于云原生应用和全新的应用开发都提出了更高要求&#xff0c;而PaaS作为“承上启下”的中间层也变得越来越重要&#xff0c;更成为…

Saas、Pass和lass

lass&#xff1a;需要将源码系统署到服务器上才能让大家访问&#xff0c;那服务器从哪来&#xff0c;咱们可以单独买一台实体服务器放家里放公司里&#xff0c;但是这样成本会比较高&#xff0c;而且维护会比较麻烦&#xff0c;所以更方便的方式就是去云服务平台&#xff0c;租…

SaaS,iass 和pass,你知道吗?

一、IaaS IaaS是Infrastructure as a server的缩写&#xff0c;意思是基础设施即服务。又云端公司把IT环境的基础设施建设好&#xff0c;然后直接对外出租硬件服务器或者虚拟机。消费者可以利用所有计算基础设施&#xff0c;包括处理CPU、内存、存储、网络和其它基本的计算资源…

saas和pass的区别

毫无疑问&#xff0c;PaaS与企业在服务与开发过程中的需求密切相关&#xff0c;特别是随着云计算的发展和企业平台化战略的驱动&#xff0c;企业对于云原生应用和全新的应用开发都提出了更高要求&#xff0c;而PaaS作为“承上启下”的中间层也变得越来越重要&#xff0c;更成为…