mirror of https://github.com/vitalif/openscad
Stand-alone program for exporting a Nef polyhedron to STL
parent
b519718b64
commit
6a39812160
|
@ -0,0 +1,179 @@
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
#include <boost/regex.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
|
#include "cgalutils.h"
|
||||||
|
#include "export.h"
|
||||||
|
#include "polyset.h"
|
||||||
|
#include "CGAL_Nef_polyhedron.h"
|
||||||
|
#include "boosty.h"
|
||||||
|
|
||||||
|
#include <CGAL/IO/Nef_polyhedron_iostream_3.h>
|
||||||
|
|
||||||
|
using namespace CGALUtils;
|
||||||
|
|
||||||
|
#define STL_FACET_NUMBYTES 4*3*4+2
|
||||||
|
// as there is no 'float32_t' standard, we assume the systems 'float'
|
||||||
|
// is a 'binary32' aka 'single' standard IEEE 32-bit floating point type
|
||||||
|
union stl_facet {
|
||||||
|
uint8_t data8[ STL_FACET_NUMBYTES ];
|
||||||
|
uint32_t data32[4*3];
|
||||||
|
struct facet_data {
|
||||||
|
float i, j, k;
|
||||||
|
float x1, y1, z1;
|
||||||
|
float x2, y2, z2;
|
||||||
|
float x3, y3, z3;
|
||||||
|
uint16_t attribute_byte_count;
|
||||||
|
} data;
|
||||||
|
};
|
||||||
|
|
||||||
|
void uint32_byte_swap( uint32_t &x )
|
||||||
|
{
|
||||||
|
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 3
|
||||||
|
x = __builtin_bswap32( x );
|
||||||
|
#elif defined(__clang__)
|
||||||
|
x = __builtin_bswap32( x );
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
x = _byteswap_ulong( x );
|
||||||
|
#else
|
||||||
|
uint32_t b1 = ( 0x000000FF & x ) << 24;
|
||||||
|
uint32_t b2 = ( 0x0000FF00 & x ) << 8;
|
||||||
|
uint32_t b3 = ( 0x00FF0000 & x ) >> 8;
|
||||||
|
uint32_t b4 = ( 0xFF000000 & x ) >> 24;
|
||||||
|
x = b1 | b2 | b3 | b4;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_stl_facet( std::ifstream &f, stl_facet &facet )
|
||||||
|
{
|
||||||
|
f.read( (char*)facet.data8, STL_FACET_NUMBYTES );
|
||||||
|
#ifdef BOOST_BIG_ENDIAN
|
||||||
|
for ( int i = 0; i < 12; i++ ) {
|
||||||
|
uint32_byte_swap( facet.data32[ i ] );
|
||||||
|
}
|
||||||
|
// we ignore attribute byte count
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
PolySet *import_stl(const std::string &filename)
|
||||||
|
{
|
||||||
|
PolySet *p = new PolySet(3);
|
||||||
|
|
||||||
|
// Open file and position at the end
|
||||||
|
std::ifstream f(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||||
|
if (!f.good()) {
|
||||||
|
PRINTB("WARNING: Can't open import file '%s'.", filename);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::regex ex_sfe("solid|facet|endloop");
|
||||||
|
boost::regex ex_outer("outer loop");
|
||||||
|
boost::regex ex_vertex("vertex");
|
||||||
|
boost::regex ex_vertices("\\s*vertex\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)");
|
||||||
|
|
||||||
|
bool binary = false;
|
||||||
|
std::streampos file_size = f.tellg();
|
||||||
|
f.seekg(80);
|
||||||
|
if (f.good() && !f.eof()) {
|
||||||
|
uint32_t facenum = 0;
|
||||||
|
f.read((char *)&facenum, sizeof(uint32_t));
|
||||||
|
#ifdef BOOST_BIG_ENDIAN
|
||||||
|
uint32_byte_swap( facenum );
|
||||||
|
#endif
|
||||||
|
if (file_size == static_cast<std::streamoff>(80 + 4 + 50*facenum)) {
|
||||||
|
binary = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.seekg(0);
|
||||||
|
|
||||||
|
char data[5];
|
||||||
|
f.read(data, 5);
|
||||||
|
if (!binary && !f.eof() && f.good() && !memcmp(data, "solid", 5)) {
|
||||||
|
int i = 0;
|
||||||
|
double vdata[3][3];
|
||||||
|
std::string line;
|
||||||
|
std::getline(f, line);
|
||||||
|
while (!f.eof()) {
|
||||||
|
std::getline(f, line);
|
||||||
|
boost::trim(line);
|
||||||
|
if (boost::regex_search(line, ex_sfe)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (boost::regex_search(line, ex_outer)) {
|
||||||
|
i = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boost::smatch results;
|
||||||
|
if (boost::regex_search(line, results, ex_vertices)) {
|
||||||
|
try {
|
||||||
|
for (int v=0;v<3;v++) {
|
||||||
|
vdata[i][v] = boost::lexical_cast<double>(results[v+1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const boost::bad_lexical_cast &blc) {
|
||||||
|
PRINTB("WARNING: Can't parse vertex line '%s'.", line);
|
||||||
|
i = 10;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (++i == 3) {
|
||||||
|
p->append_poly();
|
||||||
|
p->append_vertex(vdata[0][0], vdata[0][1], vdata[0][2]);
|
||||||
|
p->append_vertex(vdata[1][0], vdata[1][1], vdata[1][2]);
|
||||||
|
p->append_vertex(vdata[2][0], vdata[2][1], vdata[2][2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (binary && !f.eof() && f.good())
|
||||||
|
{
|
||||||
|
f.ignore(80-5+4);
|
||||||
|
while (1) {
|
||||||
|
stl_facet facet;
|
||||||
|
read_stl_facet( f, facet );
|
||||||
|
if (f.eof()) break;
|
||||||
|
p->append_poly();
|
||||||
|
p->append_vertex(facet.data.x1, facet.data.y1, facet.data.z1);
|
||||||
|
p->append_vertex(facet.data.x2, facet.data.y2, facet.data.z2);
|
||||||
|
p->append_vertex(facet.data.x3, facet.data.y3, facet.data.z3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
OpenSCAD::debug = "export_nef";
|
||||||
|
CGAL_Nef_polyhedron *N = NULL;
|
||||||
|
|
||||||
|
PolySet *ps = NULL;
|
||||||
|
if (argc == 2) {
|
||||||
|
std::string filename(argv[1]);
|
||||||
|
std::string suffix = boosty::extension_str(filename);
|
||||||
|
boost::algorithm::to_lower(suffix);
|
||||||
|
if (suffix == ".stl") {
|
||||||
|
if (!(ps = import_stl(filename))) {
|
||||||
|
std::cerr << "Error importing STL " << argv[1] << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
std::cerr << "Imported " << ps->numPolygons() << " polygons" << std::endl;
|
||||||
|
}
|
||||||
|
else if (suffix == ".nef3") {
|
||||||
|
N = new CGAL_Nef_polyhedron(new CGAL_Nef_polyhedron3);
|
||||||
|
std::ifstream stream(filename.c_str());
|
||||||
|
stream >> *N->p3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cerr << "Usage: " << argv[0] << " <file.stl>" << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ps && !N) N = createNefPolyhedronFromGeometry(*ps);
|
||||||
|
|
||||||
|
export_stl(N, std::cout);
|
||||||
|
std::cerr << "Done." << std::endl;
|
||||||
|
}
|
|
@ -0,0 +1,109 @@
|
||||||
|
CONFIG += debug
|
||||||
|
CONFIG -= qt
|
||||||
|
debug: DEFINES += DEBUG
|
||||||
|
|
||||||
|
TEMPLATE = app
|
||||||
|
|
||||||
|
INCLUDEPATH += ../src
|
||||||
|
DEPENDPATH += ../src
|
||||||
|
|
||||||
|
# Handle custom library location.
|
||||||
|
# Used when manually installing 3rd party libraries
|
||||||
|
isEmpty(OPENSCAD_LIBDIR) OPENSCAD_LIBDIR = $$(OPENSCAD_LIBRARIES)
|
||||||
|
macx:isEmpty(OPENSCAD_LIBDIR) {
|
||||||
|
exists(/opt/local):exists(/usr/local/Cellar) {
|
||||||
|
error("It seems you might have libraries in both /opt/local and /usr/local. Please specify which one to use with qmake OPENSCAD_LIBDIR=<prefix>")
|
||||||
|
} else {
|
||||||
|
exists(/opt/local) {
|
||||||
|
#Default to MacPorts on Mac OS X
|
||||||
|
message("Automatically searching for libraries in /opt/local. To override, use qmake OPENSCAD_LIBDIR=<prefix>")
|
||||||
|
OPENSCAD_LIBDIR = /opt/local
|
||||||
|
} else:exists(/usr/local/Cellar) {
|
||||||
|
message("Automatically searching for libraries in /usr/local. To override, use qmake OPENSCAD_LIBDIR=<prefix>")
|
||||||
|
OPENSCAD_LIBDIR = /usr/local
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
!isEmpty(OPENSCAD_LIBDIR) {
|
||||||
|
QMAKE_INCDIR = $$OPENSCAD_LIBDIR/include
|
||||||
|
QMAKE_LIBDIR = $$OPENSCAD_LIBDIR/lib
|
||||||
|
}
|
||||||
|
|
||||||
|
TARGET = export_nef
|
||||||
|
mac {
|
||||||
|
CONFIG -= app_bundle
|
||||||
|
}
|
||||||
|
|
||||||
|
macx {
|
||||||
|
# Mac needs special care to link against the correct C++ library
|
||||||
|
# We attempt to auto-detect it by inspecting Boost
|
||||||
|
dirs = $${BOOSTDIR} $${QMAKE_LIBDIR}
|
||||||
|
for(dir, dirs) {
|
||||||
|
system(grep -q __112basic_string $${dir}/libboost_thread* >& /dev/null) {
|
||||||
|
message("Detected libc++-linked boost in $${dir}")
|
||||||
|
CONFIG += libc++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
libc++ {
|
||||||
|
QMAKE_CXXFLAGS += -stdlib=libc++
|
||||||
|
QMAKE_LFLAGS += -stdlib=libc++
|
||||||
|
QMAKE_OBJECTIVE_CFLAGS += -stdlib=libc++
|
||||||
|
# libc++ on requires Mac OS X 10.7+
|
||||||
|
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# See Dec 2011 OpenSCAD mailing list, re: CGAL/GCC bugs.
|
||||||
|
*g++* {
|
||||||
|
QMAKE_CXXFLAGS *= -fno-strict-aliasing
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-unused-local-typedefs # ignored before 4.8
|
||||||
|
}
|
||||||
|
|
||||||
|
*clang* {
|
||||||
|
# http://llvm.org/bugs/show_bug.cgi?id=9182
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-overloaded-virtual
|
||||||
|
# disable enormous amount of warnings about CGAL / boost / etc
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-unused-parameter
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-unused-variable
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-unused-function
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-c++11-extensions
|
||||||
|
# might want to actually turn this on once in a while
|
||||||
|
QMAKE_CXXFLAGS_WARN_ON += -Wno-sign-compare
|
||||||
|
}
|
||||||
|
|
||||||
|
# Application configuration
|
||||||
|
CONFIG += cgal
|
||||||
|
CONFIG += boost
|
||||||
|
CONFIG += eigen
|
||||||
|
CONFIG += gettext
|
||||||
|
|
||||||
|
mac: {
|
||||||
|
LIBS += -framework OpenGL
|
||||||
|
}
|
||||||
|
|
||||||
|
include(../common.pri)
|
||||||
|
|
||||||
|
HEADERS += ../src/cgal.h \
|
||||||
|
../src/cgalutils.h \
|
||||||
|
../src/linalg.h \
|
||||||
|
../src/polyset.h \
|
||||||
|
../src/polyset-utils.h \
|
||||||
|
../src/printutils.h
|
||||||
|
|
||||||
|
SOURCES += export_nef.cpp \
|
||||||
|
../src/polygon2d.cc \
|
||||||
|
../src/polygon2d-CGAL.cc \
|
||||||
|
../src/CGAL_Nef_polyhedron.cc \
|
||||||
|
../src/CGAL_Nef_polyhedron_DxfData.cc \
|
||||||
|
../src/cgalutils.cc \
|
||||||
|
../src/cgalutils-tess.cc \
|
||||||
|
../src/cgalutils-polyhedron.cc \
|
||||||
|
../src/polyset.cc \
|
||||||
|
../src/svg.cc \
|
||||||
|
../src/node.cc \
|
||||||
|
../src/export.cc \
|
||||||
|
../src/polyset-utils.cc \
|
||||||
|
../src/progress.cc \
|
||||||
|
../src/printutils.cc \
|
||||||
|
../src/grid.cc
|
Loading…
Reference in New Issue