1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// Copyright (c) 2015 GeometryFactory
//
// This file is part of CGAL (www.cgal.org);
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Stream_support/include/CGAL/IO/STL/STL_reader.h $
// $Id: include/CGAL/IO/STL/STL_reader.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Andreas Fabri,
// Mael Rouxel-Labbé
#ifndef CGAL_IO_STL_STL_READER_H
#define CGAL_IO_STL_STL_READER_H
#include <CGAL/IO/io.h>
#include <CGAL/IO/helpers.h>
#include <boost/cstdint.hpp>
#include <boost/range/value_type.hpp>
#include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>
namespace CGAL {
namespace IO {
namespace internal {
template <class PointRange, class TriangleRange, typename IndexMap>
bool read_ASCII_facet(std::istream& is,
PointRange& points,
TriangleRange& facets,
int& index,
IndexMap& index_map,
const bool verbose = false)
{
typedef typename boost::range_value<PointRange>::type Point;
typedef typename boost::range_value<TriangleRange>::type Triangle;
// 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;
double x,y,z;
Point p;
Triangle ijk;
CGAL::internal::resize(ijk, 3);
while(is >> s)
{
if(s == endfacet)
{
if(count != 3)
{
if(verbose)
std::cerr << "Error: only triangulated surfaces are supported" << std::endl;
return false;
}
facets.push_back(ijk);
return true;
}
else if(s == vertex)
{
if(count >= 3)
{
if(verbose)
std::cerr << "Error: only triangulated surfaces are supported" << std::endl;
return false;
}
if(!(is >> IO::iformat(x) >> IO::iformat(y) >> IO::iformat(z)))
{
if(verbose)
std::cerr << "Error while reading point coordinates (premature end of file)" << std::endl;
return false;
}
else
{
fill_point(x, y, z, 1 /*w*/, p);
typename std::map<Point, 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;
}
template <class PointRange, class TriangleRange>
bool parse_ASCII_STL(std::istream& is,
PointRange& points,
TriangleRange& facets,
const bool verbose = false)
{
typedef typename boost::range_value<PointRange>::type Point;
bool solid_found = false;
if(verbose)
std::cout << "Parsing ASCII file..." << std::endl;
if(!is.good())
return false;
// Here, we have already read the word 'solid'
int index = 0;
std::map<Point, int> index_map;
std::string s, facet("facet"), endsolid("endsolid"), solid("solid");
bool in_solid(false);
while(is >> s)
{
if(s == solid)
{
if(in_solid)
break;
in_solid = true;
}
if(s == facet)
{
if(!read_ASCII_facet(is, points, facets, index, index_map, verbose))
return false;
}
else if(s == endsolid)
{
in_solid = false;
solid_found = true;
}
}
if(in_solid)
{
if(verbose)
std::cerr << "Error while parsing ASCII file" << std::endl;
return false;
}
return solid_found && !in_solid;
}
template <class PointRange, class TriangleRange>
bool parse_binary_STL(std::istream& is,
PointRange& points,
TriangleRange& facets,
const bool verbose = false)
{
typedef typename boost::range_value<PointRange>::type Point;
typedef typename boost::range_value<TriangleRange>::type Triangle;
if(verbose)
std::cout << "Parsing binary file..." << std::endl;
// Start from the beginning again to simplify things
is.clear();
is.seekg(0, std::ios::beg);
if(!is.good())
return false;
// Discard the first 80 chars (unused header)
int pos = 0;
char c;
if(verbose)
std::cout << "header: ";
while(pos < 80)
{
is.read(reinterpret_cast<char*>(&c), sizeof(c));
if(!is.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<Point, int> index_map;
std::uint32_t N32;
if(!(is.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(!(is.read(reinterpret_cast<char*>(&normal[0]), sizeof(normal[0]))) ||
!(is.read(reinterpret_cast<char*>(&normal[1]), sizeof(normal[1]))) ||
!(is.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;
}
Triangle ijk;
CGAL::internal::resize(ijk, 3);
for(int j=0; j<3; ++j)
{
float x,y,z;
if(!(is.read(reinterpret_cast<char*>(&x), sizeof(x))) ||
!(is.read(reinterpret_cast<char*>(&y), sizeof(y))) ||
!(is.read(reinterpret_cast<char*>(&z), sizeof(z))))
{
if(verbose)
std::cerr << "Error while reading vertex coordinates (premature end of file)" << std::endl;
return false;
}
Point p;
fill_point(x, y, z, 1 /*w*/, p);
typename std::map<Point, 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(!(is.read(reinterpret_cast<char*>(&c), sizeof(c))) ||
!(is.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 !is.fail();
}
} // namespace internal
} // namespace IO
} // namespace CGAL
#endif // CGAL_IO_STL_STL_READER_H
|