读取STL模型

读取二进制格式的STL模型文件

std::ifstream fin;
fin.open(stlFilePath, std::ios::in | std::ios::binary);
bool isBinary=true;//判断stl是否是二进制流文件
fin.seekg(0, std::ios::end);
std::streamoff stlFileSize=fin.tellg();//文件大小
char buf[4];
fin.seekg(80, std::ios::beg);
fin.read(buf, 4);
int triNum=*(int*)(buf);//三角面个数
if ((80+4+triNum*50)!=stlFileSize)
{
 isBinary=false;
}
//一个STL里面的facet占50个字节
float normal[3];//三角面法向量3*sizeof(float)=12字节
float v1[3]; //第一个顶点3*sizeof(float)=12字节
float v2[3]; //第二个顶点3*sizeof(float)=12字节
float v3[3]; //第三个顶点3*sizeof(float)=12字节
short attribute;//2字节,属性信息(暂未使用)
for (int meshId=0; meshId<triNum; meshId++)
{
fin.read((char*)normal, 12);
fin.read((char*)v1, 12);
fin.read((char*)v2, 12);
fin.read((char*)v3, 12);
fin.read((char*)&attribute, sizeof(short));
}

原文地址:https://www.cnblogs.com/coolbear/p/3458865.html