mirror of https://github.com/CGAL/cgal
Improved STL reader
This commit is contained in:
parent
428e1dd00d
commit
abfeaf39cd
|
|
@ -15,221 +15,335 @@
|
|||
// $Id$
|
||||
// SPDX-License-Identifier: LGPL-3.0+
|
||||
//
|
||||
// Author(s) : Andreas Fabri
|
||||
// Author(s) : Andreas Fabri,
|
||||
// Mael Rouxel-Labbé
|
||||
|
||||
#ifndef CGAL_IO_STL_READER_H
|
||||
#define CGAL_IO_STL_READER_H
|
||||
|
||||
#include <CGAL/array.h>
|
||||
#include <CGAL/IO/io.h>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace CGAL{
|
||||
namespace CGAL {
|
||||
|
||||
bool
|
||||
read_STL( std::istream& input,
|
||||
std::vector< cpp11::array<double,3> >& points,
|
||||
std::vector< cpp11::array<int,3> >& facets,
|
||||
bool verbose = false)
|
||||
bool read_ASCII_facet(std::istream& input,
|
||||
std::vector<cpp11::array<double,3> >& points,
|
||||
std::vector<cpp11::array<int,3> >& facets,
|
||||
int& index,
|
||||
std::map<cpp11::array<double, 3>, int >& index_map,
|
||||
bool verbose = false)
|
||||
{
|
||||
// Here, we have already read the word 'facet' and are looking to read till 'endfacet'
|
||||
|
||||
std::string s;
|
||||
std::string vertex("vertex"),
|
||||
endfacet("endfacet");
|
||||
|
||||
int count = 0;
|
||||
cpp11::array<double, 3> p;
|
||||
cpp11::array<int, 3> ijk;
|
||||
|
||||
while(input >> s)
|
||||
{
|
||||
bool is_binary_file = false;
|
||||
|
||||
std::string s, solid("solid"), facet("facet");
|
||||
std::map<cpp11::array<double,3>, int> pmap;
|
||||
int index = 0;
|
||||
cpp11::array<int,3> ijk;
|
||||
cpp11::array<double,3> p;
|
||||
if(s == endfacet)
|
||||
{
|
||||
if(count != 3)
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error: only triangulated surfaces are supported" << std::endl;
|
||||
|
||||
char line[80];
|
||||
int i = 0, ni = 0;
|
||||
|
||||
// read the 5 first characters to check if the first word is "solid"
|
||||
boost::uint8_t c;
|
||||
for(; i < 5; i++){
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
line[i]=c;
|
||||
return false;
|
||||
}
|
||||
|
||||
s = std::string(line,5);
|
||||
if(s == solid){
|
||||
// we found the keyword "solid" which is supposed to indicate the file is Ascii
|
||||
// But it might still be binary, so we have to find out if it is followed
|
||||
// by an (optional) name and then the keyword "facet"
|
||||
// When we find "facet" we conclude that it is really Ascii
|
||||
|
||||
// first skip whitespaces after solid
|
||||
do {
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
line[i++]=c;
|
||||
}while(isspace(c) && ( i < 80));
|
||||
if(i==80){
|
||||
is_binary_file = true;
|
||||
goto done;
|
||||
}
|
||||
// now c is not whitespace
|
||||
ni = i-1; // here starts either the name or the keyword "facet"
|
||||
do {
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
line[i++]=c;
|
||||
}while(! isspace(c) && ( i < 80));
|
||||
s = std::string(line+ni, (i-1) - ni);
|
||||
# ifdef CGAL_DEBUG_BINARY_HEADER
|
||||
std::cout << "|" << s << "|" << std::endl;
|
||||
# endif
|
||||
if(s == facet){
|
||||
goto done;
|
||||
} else if(i == 80){
|
||||
// the entire header is a name
|
||||
is_binary_file = true;
|
||||
goto done;
|
||||
}
|
||||
|
||||
// we continue to read what comes after the name
|
||||
|
||||
// now c is whitespace, skip other whitespaces
|
||||
do {
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
line[i++]=c;
|
||||
}while(isspace(c) && ( i < 80));
|
||||
if(i==80){
|
||||
is_binary_file = true;
|
||||
goto done;
|
||||
}
|
||||
|
||||
// now c is not whitespace
|
||||
ni = i-1; // here starts either "facet", or it is really binary
|
||||
do {
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
line[i++]=c;
|
||||
}while(! isspace(c) && ( i < 80));
|
||||
s = std::string(line+ni, (i-1) - ni);
|
||||
# ifdef CGAL_DEBUG_BINARY_HEADER
|
||||
std::cout << "|" << s << "|" << std::endl;
|
||||
# endif
|
||||
if(s == facet){
|
||||
goto done;
|
||||
} else {
|
||||
for(; i < 80; i++){
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
}
|
||||
is_binary_file = true;
|
||||
goto done;
|
||||
}
|
||||
}else{
|
||||
// we read the other 75 characters of the header
|
||||
for(; i < 80; i++){
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
}
|
||||
is_binary_file = true;
|
||||
}
|
||||
|
||||
done:
|
||||
|
||||
if(is_binary_file){
|
||||
boost::uint32_t N32;
|
||||
input.read(reinterpret_cast<char*>(&N32), sizeof(N32));
|
||||
unsigned int N = N32;
|
||||
|
||||
for(unsigned int i=0; i < N; i++){
|
||||
float normal[3];
|
||||
input.read(reinterpret_cast<char*>(&normal[0]), sizeof(normal[0]));
|
||||
input.read(reinterpret_cast<char*>(&normal[1]), sizeof(normal[1]));
|
||||
input.read(reinterpret_cast<char*>(&normal[2]), sizeof(normal[2]));
|
||||
|
||||
for(int j=0; j < 3; j++){
|
||||
float x,y,z;
|
||||
input.read(reinterpret_cast<char*>(&x), sizeof(x));
|
||||
input.read(reinterpret_cast<char*>(&y), sizeof(y));
|
||||
input.read(reinterpret_cast<char*>(&z), sizeof(z));
|
||||
p[0]=x; p[1]=y; p[2]=z;
|
||||
std::map<cpp11::array<double,3>, int>::iterator iti=
|
||||
pmap.insert(std::make_pair(p,-1)).first;
|
||||
if(iti->second==-1){
|
||||
ijk[j] = index;
|
||||
iti->second = index++;
|
||||
points.push_back(p);
|
||||
} else {
|
||||
ijk[j] = iti->second;
|
||||
}
|
||||
}
|
||||
if((ijk[0] != ijk[1]) &&
|
||||
(ijk[0] != ijk[2]) &&
|
||||
(ijk[1] != ijk[2])){
|
||||
facets.push_back(ijk);
|
||||
}else{
|
||||
if(verbose){
|
||||
std::cerr << "ignore degenerate face" << std::endl;
|
||||
}
|
||||
}
|
||||
char c;
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
}
|
||||
facets.push_back(ijk);
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
else if(s == vertex)
|
||||
{
|
||||
if(count >= 3)
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error: only triangulated surfaces are supported" << std::endl;
|
||||
|
||||
// It is an Ascii file but the first occurence of "facet" has already be parsed
|
||||
bool first_facet = true;
|
||||
std::string outer("outer"),
|
||||
loop("loop"),
|
||||
vertex("vertex"),
|
||||
endloop("endloop"),
|
||||
endsolid("endsolid");
|
||||
s = facet;
|
||||
while(first_facet || (input >> s)){
|
||||
first_facet = false;
|
||||
if(s == endsolid){
|
||||
//std::cerr << "found endsolid" << std::endl;
|
||||
} else if(s == facet){
|
||||
//std::cerr << "found facet" << std::endl;
|
||||
std::getline(input, s); // ignore the normal
|
||||
input >> s;
|
||||
if(s != outer){
|
||||
if (verbose)
|
||||
std::cerr << "Expect 'outer' and got " << s << std::endl;
|
||||
return false;
|
||||
}
|
||||
input >> s;
|
||||
if(s != loop){
|
||||
if (verbose)
|
||||
std::cerr << "Expect 'loop' and got " << s << std::endl;
|
||||
return false;
|
||||
}
|
||||
int count = 0;
|
||||
do {
|
||||
input >> s;
|
||||
if(s == vertex){
|
||||
// std::cerr << "found vertex" << std::endl;
|
||||
if(count < 3){
|
||||
input >> p[0] >> p[1] >> p[2];
|
||||
std::map<cpp11::array<double,3>, int>::iterator iti=
|
||||
pmap.insert(std::make_pair(p,-1)).first;
|
||||
if(iti->second==-1){
|
||||
ijk[count] = index;
|
||||
iti->second = index++;
|
||||
points.push_back(p);
|
||||
} else {
|
||||
ijk[count] = iti->second;
|
||||
}
|
||||
++count;
|
||||
} else {
|
||||
if (verbose)
|
||||
std::cerr << "We can only read triangulated surfaces" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}while(s != endloop);
|
||||
|
||||
facets.push_back(ijk);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(input >> iformat(p[0]) >> iformat(p[1]) >> iformat(p[2])))
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error while reading point coordinates (premature end of file)" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::map<cpp11::array<double, 3>, int>::iterator iti=
|
||||
index_map.insert(std::make_pair(p, -1)).first;
|
||||
|
||||
if(iti->second == -1)
|
||||
{
|
||||
ijk[count] = index;
|
||||
iti->second = index++;
|
||||
points.push_back(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
ijk[count] = iti->second;
|
||||
}
|
||||
}
|
||||
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
if(verbose)
|
||||
std::cerr << "Error while reading facet (premature end of file)" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool parse_ASCII_STL(std::istream& input,
|
||||
std::vector<cpp11::array<double,3> >& points,
|
||||
std::vector<cpp11::array<int,3> >& facets,
|
||||
bool verbose = false)
|
||||
{
|
||||
if(verbose)
|
||||
std::cout << "Parsing ASCII file..." << std::endl;
|
||||
|
||||
if(!input.good())
|
||||
return true;
|
||||
|
||||
// Here, we have already read the word 'solid'
|
||||
|
||||
int index = 0;
|
||||
std::map<cpp11::array<double, 3>, int> index_map;
|
||||
|
||||
std::string s;
|
||||
std::string facet("facet"),
|
||||
endsolid("endsolid");
|
||||
|
||||
while(input >> s)
|
||||
{
|
||||
if(s == facet)
|
||||
{
|
||||
if(!read_ASCII_facet(input, points, facets, index, index_map, verbose))
|
||||
return false;
|
||||
}
|
||||
else if(s == endsolid)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if(verbose)
|
||||
std::cerr << "Error while parsing ASCII file" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool parse_binary_STL(std::istream& input,
|
||||
std::vector<cpp11::array<double,3> >& points,
|
||||
std::vector<cpp11::array<int,3> >& facets,
|
||||
bool verbose = false)
|
||||
{
|
||||
if(verbose)
|
||||
std::cout << "Parsing binary file..." << std::endl;
|
||||
|
||||
// Start from the beginning again to simplify things
|
||||
input.clear();
|
||||
input.seekg(0, std::ios::beg);
|
||||
|
||||
if(!input.good())
|
||||
return true;
|
||||
|
||||
// Discard the first 80 chars (unused header)
|
||||
int pos = 0;
|
||||
char c;
|
||||
|
||||
if(verbose)
|
||||
std::cout << "header: ";
|
||||
|
||||
while(pos < 80)
|
||||
{
|
||||
input.read(reinterpret_cast<char*>(&c), sizeof(c));
|
||||
if(!input.good())
|
||||
break;
|
||||
|
||||
if(verbose)
|
||||
std::cout << c;
|
||||
|
||||
++pos;
|
||||
}
|
||||
|
||||
if(verbose)
|
||||
std::cout << std::endl;
|
||||
|
||||
if(pos != 80)
|
||||
return true; // empty file
|
||||
|
||||
int index = 0;
|
||||
std::map<cpp11::array<double, 3>, int> index_map;
|
||||
|
||||
boost::uint32_t N32;
|
||||
if(!(input.read(reinterpret_cast<char*>(&N32), sizeof(N32))))
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error while reading number of facets" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int N = N32;
|
||||
if(verbose)
|
||||
std::cout << N << " facets to read" << std::endl;
|
||||
|
||||
for(unsigned int i=0; i<N; ++i)
|
||||
{
|
||||
float normal[3];
|
||||
if(!(input.read(reinterpret_cast<char*>(&normal[0]), sizeof(normal[0]))) ||
|
||||
!(input.read(reinterpret_cast<char*>(&normal[1]), sizeof(normal[1]))) ||
|
||||
!(input.read(reinterpret_cast<char*>(&normal[2]), sizeof(normal[2]))))
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error while reading normal coordinates (premature end of file)" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
cpp11::array<int, 3> ijk;
|
||||
|
||||
for(int j=0; j<3; ++j)
|
||||
{
|
||||
float x,y,z;
|
||||
if(!(input.read(reinterpret_cast<char*>(&x), sizeof(x))) ||
|
||||
!(input.read(reinterpret_cast<char*>(&y), sizeof(y))) ||
|
||||
!(input.read(reinterpret_cast<char*>(&z), sizeof(z))))
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error while reading vertex coordinates (premature end of file)" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
cpp11::array<double, 3> p;
|
||||
p[0] = x; p[1] = y; p[2] = z;
|
||||
|
||||
std::map<cpp11::array<double, 3>, int>::iterator iti =
|
||||
index_map.insert(std::make_pair(p, -1)).first;
|
||||
|
||||
if(iti->second == -1)
|
||||
{
|
||||
ijk[j] = index;
|
||||
iti->second = index++;
|
||||
points.push_back(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
ijk[j] = iti->second;
|
||||
}
|
||||
}
|
||||
|
||||
facets.push_back(ijk);
|
||||
|
||||
// Read so-called attribute byte count and ignore it
|
||||
char c;
|
||||
if(!(input.read(reinterpret_cast<char*>(&c), sizeof(c))) ||
|
||||
!(input.read(reinterpret_cast<char*>(&c), sizeof(c))))
|
||||
{
|
||||
if(verbose)
|
||||
std::cerr << "Error while reading attribute byte count (premature end of file)" << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_STL(std::istream& input,
|
||||
std::vector<cpp11::array<double,3> >& points,
|
||||
std::vector<cpp11::array<int,3> >& facets,
|
||||
bool verbose = false)
|
||||
{
|
||||
int pos = 0;
|
||||
|
||||
// Ignore all initial whitespace
|
||||
char c;
|
||||
|
||||
while(input.read(reinterpret_cast<char*>(&c), sizeof(c)))
|
||||
{
|
||||
if(!isspace(c))
|
||||
{
|
||||
input.unget(); // move back to the first interesting char
|
||||
break;
|
||||
}
|
||||
++pos;
|
||||
}
|
||||
|
||||
if(!input.good()) // reached the end
|
||||
return true;
|
||||
|
||||
// If we have gone beyond 80 characters and have not read anything yet,
|
||||
// then this must be an ASCII file.
|
||||
if(pos > 80)
|
||||
return parse_ASCII_STL(input, points, facets, verbose);
|
||||
|
||||
// We are within the first 80 characters, both ASCII and binary are possible
|
||||
|
||||
// Read the 5 first characters to check if the first word is "solid"
|
||||
std::string s, solid("solid");
|
||||
|
||||
char word[5];
|
||||
if(input.read(reinterpret_cast<char*>(&word[0]), sizeof(c)) &&
|
||||
input.read(reinterpret_cast<char*>(&word[1]), sizeof(c)) &&
|
||||
input.read(reinterpret_cast<char*>(&word[2]), sizeof(c)) &&
|
||||
input.read(reinterpret_cast<char*>(&word[3]), sizeof(c)) &&
|
||||
input.read(reinterpret_cast<char*>(&word[4]), sizeof(c)))
|
||||
{
|
||||
s = std::string(word, 5);
|
||||
pos += 5;
|
||||
}
|
||||
else
|
||||
return true; // empty file
|
||||
|
||||
// If the first word is not 'solid', the file must be binary
|
||||
if(s != solid)
|
||||
{
|
||||
if(parse_binary_STL(input, points, facets, verbose))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we failed to read it as a binary, try as ASCII just in case...
|
||||
// The file does not start with 'solid' anyway, so it's fine to reset it.
|
||||
input.clear();
|
||||
input.seekg(0, std::ios::beg);
|
||||
return parse_ASCII_STL(input, points, facets, verbose);
|
||||
}
|
||||
}
|
||||
|
||||
// Now, we have found the keyword "solid" which is supposed to indicate that the file is ASCII
|
||||
if(parse_ASCII_STL(input, points, facets, verbose))
|
||||
{
|
||||
// correctly read the input as an ASCII file
|
||||
return true;
|
||||
}
|
||||
else // Failed to read the ASCII file
|
||||
{
|
||||
// It might have actually have been a binary file... ?
|
||||
return parse_binary_STL(input, points, facets, verbose);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace CGAL
|
||||
|
||||
#endif // CGAL_IO_STL_READER_H
|
||||
|
|
|
|||
Loading…
Reference in New Issue