ug4
stl_reader.h File Reference

Provides functions to read stl files into user provided arrays. More...

#include <algorithm>
#include <exception>
#include <fstream>
#include <sstream>
#include <vector>

Go to the source code of this file.

Classes

struct  stl_reader::stl_reader_impl::CoordWithIndex< number_t, index_t >
 
class  stl_reader::StlMesh< TNumber, TIndex >
 convenience mesh class which makes accessing the stl data more easy More...
 

Namespaces

 stl_reader
 
 stl_reader::stl_reader_impl
 

Macros

#define STL_READER_COND_THROW(cond, msg)   if(cond){std::stringstream ss; ss << msg; throw(std::runtime_error(ss.str()));}
 Throws an std::runtime_error with the given message, if the given condition evaluates to true. More...
 
#define STL_READER_THROW(msg)   {std::stringstream ss; ss << msg; throw(std::runtime_error(ss.str()));}
 Throws an std::runtime_error with the given message. More...
 

Functions

template<class TNumberContainer , class TIndexContainer >
bool stl_reader::ReadStlFile (const char *filename, TNumberContainer &coordsOut, TNumberContainer &normalsOut, TIndexContainer &trisOut, TIndexContainer &solidRangesOut)
 Reads an ASCII or binary stl file into several arrays. More...
 
template<class TNumberContainer , class TIndexContainer >
bool stl_reader::ReadStlFile_ASCII (const char *filename, TNumberContainer &coordsOut, TNumberContainer &normalsOut, TIndexContainer &trisOut, TIndexContainer &solidRangesOut)
 Reads an ASCII stl file into several arrays. More...
 
template<class TNumberContainer , class TIndexContainer >
bool stl_reader::ReadStlFile_BINARY (const char *filename, TNumberContainer &coordsOut, TNumberContainer &normalsOut, TIndexContainer &trisOut, TIndexContainer &solidRangesOut)
 Reads a binary stl file into several arrays. More...
 
template<class TNumberContainer , class TIndexContainer >
void stl_reader::stl_reader_impl::RemoveDoubles (TNumberContainer &uniqueCoordsOut, TIndexContainer &trisInOut, std::vector< CoordWithIndex< typename TNumberContainer::value_type, typename TIndexContainer::value_type > > &coordsWithIndexInOut)
 
bool stl_reader::StlFileHasASCIIFormat (const char *filename)
 Determines whether a stl file has ASCII format. More...
 

Detailed Description

Provides functions to read stl files into user provided arrays.

The central function of this file is ReadStlFile(...). It automatically recognizes whether an ASCII or a Binary file is to be read. It identifies matching corner coordinates of triangles with each other, so that the resulting coordinate array does not contain the same coordinate-triple multiple times.

The function operates on template container types. Those containers should have similar interfaces as std::vector and operate on float or double types (TNumberContainer) or on int or size_t types (TIndexContainer).

A conveniance class StlMesh is also provided, which makes accessing triangle corners and corresponding corner coordinates much more easy. It still provides raw access to the underlying data arrays.

Usage example 1 (using <tt>StlMesh</tt>):

try {
for(size_t itri = 0; itri < mesh.num_tris(); ++itri) {
std::cout << "coordinates of triangle " << itri << ": ";
for(size_t icorner = 0; icorner < 3; ++icorner) {
float* c = mesh.vrt_coords (mesh.tri_corner_ind (itri, icorner));
std::cout << "(" << c[0] << ", " << c[1] << ", " << c[2] << ") ";
}
std::cout << std::endl;
float* n = mesh.tri_normal (itri);
std::cout << "normal of triangle " << itri << ": "
<< "(" << n[0] << ", " << n[1] << ", " << n[2] << ")\n";
}
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
convenience mesh class which makes accessing the stl data more easy
Definition: stl_reader.h:230

Usage example 2 (using <tt>StlMesh</tt> and <em>solids</em>)

try {
for(size_t isolid = 0; isolid < mesh.num_solids(); ++isolid) {
std::cout << "solid " << isolid << std::endl;
for(size_t itri = mesh.solid_tris_begin(isolid);
itri < mesh.solid_tris_end(isolid); ++itri)
{
const float* n = mesh.tri_normal (itri);
std::cout << "normal of triangle " << itri << ": "
<< "(" << n[0] << ", " << n[1] << ", " << n[2] << ")\n";
}
}
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}

Usage example 3 (using raw data arrays)

std::vector<float> coords, normals;
std::vector<unsigned int> tris, solids;
try {
stl_reader::ReadStlFile ("geometry.stl", coords, normals, tris, solids);
const size_t numTris = tris.size() / 3;
for(size_t itri = 0; itri < numTris; ++itri) {
std::cout << "coordinates of triangle " << itri << ": ";
for(size_t icorner = 0; icorner < 3; ++icorner) {
float* c = &coords[3 * tris [3 * itri + icorner]];
std::cout << "(" << c[0] << ", " << c[1] << ", " << c[2] << ") ";
}
std::cout << std::endl;
float* n = &normals [3 * itri];
std::cout << "normal of triangle " << itri << ": "
<< "(" << n[0] << ", " << n[1] << ", " << n[2] << ")\n";
}
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
bool ReadStlFile(const char *filename, TNumberContainer &coordsOut, TNumberContainer &normalsOut, TIndexContainer &trisOut, TIndexContainer &solidRangesOut)
Reads an ASCII or binary stl file into several arrays.
Definition: stl_reader.h:494

If you do not want to use exceptions, you may define the macro STL_READER_NO_EXCEPTIONS before including 'stl_reader.h'. In that case, functions will return false if an error occurred.

Macro Definition Documentation

◆ STL_READER_COND_THROW

#define STL_READER_COND_THROW (   cond,
  msg 
)    if(cond){std::stringstream ss; ss << msg; throw(std::runtime_error(ss.str()));}

Throws an std::runtime_error with the given message, if the given condition evaluates to true.

◆ STL_READER_THROW

#define STL_READER_THROW (   msg)    {std::stringstream ss; ss << msg; throw(std::runtime_error(ss.str()));}

Throws an std::runtime_error with the given message.