Skip to content
Snippets Groups Projects
Commit db929950 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

make STL reader accept multiple solids per file

parent a2077d20
No related branches found
No related tags found
No related merge requests found
...@@ -123,6 +123,7 @@ void VertexArray::finalize() ...@@ -123,6 +123,7 @@ void VertexArray::finalize()
_data3.clear(); _data3.clear();
} }
_barycenters.clear(); _barycenters.clear();
//printf("vert array : %d Mb\n", getMemoryUsage());
} }
class AlphaElement { class AlphaElement {
...@@ -208,3 +209,10 @@ void VertexArray::sort(double x, double y, double z) ...@@ -208,3 +209,10 @@ void VertexArray::sort(double x, double y, double z)
_normals = sortedNormals; _normals = sortedNormals;
_colors = sortedColors; _colors = sortedColors;
} }
int VertexArray::getMemoryUsage()
{
int bytes = _vertices.size() * sizeof(float) + _normals.size() * sizeof(char) +
_colors.size() * sizeof(unsigned char);
return bytes / 1024 / 1024;
}
...@@ -158,6 +158,8 @@ class VertexArray{ ...@@ -158,6 +158,8 @@ class VertexArray{
void finalize(); void finalize();
// sort the arrays with elements back to front wrt the eye position // sort the arrays with elements back to front wrt the eye position
void sort(double x, double y, double z); void sort(double x, double y, double z);
// estimate the size of the vertex array in megabytes
int getMemoryUsage();
}; };
#endif #endif
...@@ -602,17 +602,14 @@ int GModel::writePOS(const std::string &name, bool printElementary, ...@@ -602,17 +602,14 @@ int GModel::writePOS(const std::string &name, bool printElementary,
int GModel::readSTL(const std::string &name, double tolerance) int GModel::readSTL(const std::string &name, double tolerance)
{ {
// Note: this routine only reads a single "solid" (not sure if the
// spec allows to read multiple solids in a single file)
FILE *fp = fopen(name.c_str(), "rb"); FILE *fp = fopen(name.c_str(), "rb");
if(!fp){ if(!fp){
Msg::Error("Unable to open file '%s'", name.c_str()); Msg::Error("Unable to open file '%s'", name.c_str());
return 0; return 0;
} }
// read all facets and store triplets of points // store triplets of points for each solid found in the file
std::vector<SPoint3> points; std::vector<std::vector<SPoint3> > points;
SBoundingBox3d bbox; SBoundingBox3d bbox;
// "solid", or binary data header // "solid", or binary data header
...@@ -621,21 +618,29 @@ int GModel::readSTL(const std::string &name, double tolerance) ...@@ -621,21 +618,29 @@ int GModel::readSTL(const std::string &name, double tolerance)
if(!strncmp(buffer, "solid", 5)){ if(!strncmp(buffer, "solid", 5)){
// ASCII STL // ASCII STL
points.resize(1);
while(!feof(fp)) { while(!feof(fp)) {
// "facet normal x y z", or "endsolid" // "facet normal x y z" or "endsolid"
if(!fgets(buffer, sizeof(buffer), fp)) break;
if(!strncmp(buffer, "endsolid", 8)){
// "solid"
if(!fgets(buffer, sizeof(buffer), fp)) break;
if(!strncmp(buffer, "solid", 5)){
points.resize(points.size() + 1);
// "facet normal x y z"
if(!fgets(buffer, sizeof(buffer), fp)) break; if(!fgets(buffer, sizeof(buffer), fp)) break;
if(!strncmp(buffer, "endsolid", 8)) break; }
char s1[256], s2[256]; }
float x, y, z;
sscanf(buffer, "%s %s %f %f %f", s1, s2, &x, &y, &z);
// "outer loop" // "outer loop"
if(!fgets(buffer, sizeof(buffer), fp)) break; if(!fgets(buffer, sizeof(buffer), fp)) break;
// "vertex x y z" // "vertex x y z"
for(int i = 0; i < 3; i++){ for(int i = 0; i < 3; i++){
if(!fgets(buffer, sizeof(buffer), fp)) break; if(!fgets(buffer, sizeof(buffer), fp)) break;
sscanf(buffer, "%s %f %f %f", s1, &x, &y, &z); char s1[256];
double x, y, z;
if(sscanf(buffer, "%s %lf %lf %lf", s1, &x, &y, &z) != 4) break;
SPoint3 p(x, y, z); SPoint3 p(x, y, z);
points.push_back(p); points.back().push_back(p);
bbox += p; bbox += p;
} }
// "endloop" // "endloop"
...@@ -648,8 +653,9 @@ int GModel::readSTL(const std::string &name, double tolerance) ...@@ -648,8 +653,9 @@ int GModel::readSTL(const std::string &name, double tolerance)
// Binary STL // Binary STL
Msg::Info("Mesh is in binary format"); Msg::Info("Mesh is in binary format");
rewind(fp); rewind(fp);
while(!feof(fp)) {
char header[80]; char header[80];
if(fread(header, sizeof(char), 80, fp)){ if(!fread(header, sizeof(char), 80, fp)) break;
unsigned int nfacets = 0; unsigned int nfacets = 0;
size_t ret = fread(&nfacets, sizeof(unsigned int), 1, fp); size_t ret = fread(&nfacets, sizeof(unsigned int), 1, fp);
bool swap = false; bool swap = false;
...@@ -659,6 +665,7 @@ int GModel::readSTL(const std::string &name, double tolerance) ...@@ -659,6 +665,7 @@ int GModel::readSTL(const std::string &name, double tolerance)
SwapBytes((char*)&nfacets, sizeof(unsigned int), 1); SwapBytes((char*)&nfacets, sizeof(unsigned int), 1);
} }
if(ret && nfacets){ if(ret && nfacets){
points.resize(points.size() + 1);
char *data = new char[nfacets * 50 * sizeof(char)]; char *data = new char[nfacets * 50 * sizeof(char)];
ret = fread(data, sizeof(char), nfacets * 50, fp); ret = fread(data, sizeof(char), nfacets * 50, fp);
if(ret == nfacets * 50){ if(ret == nfacets * 50){
...@@ -667,7 +674,7 @@ int GModel::readSTL(const std::string &name, double tolerance) ...@@ -667,7 +674,7 @@ int GModel::readSTL(const std::string &name, double tolerance)
if(swap) SwapBytes((char*)xyz, sizeof(float), 12); if(swap) SwapBytes((char*)xyz, sizeof(float), 12);
for(int j = 0; j < 3; j++){ for(int j = 0; j < 3; j++){
SPoint3 p(xyz[3 + 3 * j], xyz[3 + 3 * j + 1], xyz[3 + 3 * j + 2]); SPoint3 p(xyz[3 + 3 * j], xyz[3 + 3 * j + 1], xyz[3 + 3 * j + 2]);
points.push_back(p); points.back().push_back(p);
bbox += p; bbox += p;
} }
} }
...@@ -677,43 +684,49 @@ int GModel::readSTL(const std::string &name, double tolerance) ...@@ -677,43 +684,49 @@ int GModel::readSTL(const std::string &name, double tolerance)
} }
} }
if(!points.size()){ std::vector<GFace*> faces;
Msg::Error("No facets found in STL file"); for(unsigned int i = 0; i < points.size(); i++){
if(points[i].empty()){
Msg::Error("No facets found in STL file for solid %d", i);
return 0; return 0;
} }
if(points[i].size() % 3){
if(points.size() % 3){ Msg::Error("Wrong number of points (%d) in STL file for solid %d",
Msg::Error("Wrong number of points in STL file"); points[i].size(), i);
return 0; return 0;
} }
Msg::Info("%d facets in solid %d", points[i].size() / 3, i);
Msg::Info("%d facets", points.size() / 3);
// create face // create face
GFace *face = new discreteFace(this, getNumFaces() + 1); GFace *face = new discreteFace(this, getNumFaces() + 1);
faces.push_back(face);
add(face); add(face);
}
// create (unique) vertices and triangles // create (unique) vertices and triangles
double lc = norm(SVector3(bbox.max(), bbox.min())); double lc = norm(SVector3(bbox.max(), bbox.min()));
double old_tol = MVertexLessThanLexicographic::tolerance; double old_tol = MVertexLessThanLexicographic::tolerance;
MVertexLessThanLexicographic::tolerance = lc * tolerance; MVertexLessThanLexicographic::tolerance = lc * tolerance;
std::set<MVertex*, MVertexLessThanLexicographic> vertices; std::set<MVertex*, MVertexLessThanLexicographic> vertices;
for(unsigned int i = 0; i < points.size(); i += 3){ for(unsigned int i = 0; i < points.size(); i ++){
for(unsigned int j = 0; j < points[i].size(); j += 3){
MVertex *v[3]; MVertex *v[3];
for(int j = 0; j < 3; j++){ for(int k = 0; k < 3; k++){
double x = points[i + j].x(), y = points[i + j].y(), z = points[i + j].z(); double x = points[i][j + k].x();
double y = points[i][j + k].y();
double z = points[i][j + k].z();
MVertex w(x, y, z); MVertex w(x, y, z);
std::set<MVertex*, MVertexLessThanLexicographic>::iterator it = vertices.find(&w); std::set<MVertex*, MVertexLessThanLexicographic>::iterator it = vertices.find(&w);
if(it != vertices.end()) { if(it != vertices.end()) {
v[j] = *it; v[k] = *it;
} }
else { else {
v[j] = new MVertex(x, y, z, face); v[k] = new MVertex(x, y, z, faces[i]);
vertices.insert(v[j]); vertices.insert(v[k]);
face->mesh_vertices.push_back(v[j]); faces[i]->mesh_vertices.push_back(v[k]);
}
} }
faces[i]->triangles.push_back(new MTriangle(v[0], v[1], v[2]));
} }
face->triangles.push_back(new MTriangle(v[0], v[1], v[2]));
} }
MVertexLessThanLexicographic::tolerance = old_tol; MVertexLessThanLexicographic::tolerance = old_tol;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment