//////////////////////////////////////////////////// // // off2plc.cpp // // Alex Rand // 12-5-2008 // // off2plc.cpp converts _simple_ .off files to the // .plc file format. This program only succeeds if // the off files contain only convex faces. // //////////////////////////////////////////////////// # include # include # include # include # include # include # include # include # include # include "foreach.hpp" # define foreach BOOST_FOREACH using namespace std; typedef pair P; typedef pair,double> T; # define EPSILON 0.000001 double dist ( double x, double y, double z, T p ); double perturb ( double d ) ; double round ( double d ); //****************************************************************************80 int main ( int argc, char* argv[] ) //****************************************************************************80 { string fn; if ( argc == 1 ) { cout << "Usage: dir3 filename" << endl; return 0; } else { string tmp(argv[1]); fn = tmp; } // // strip .off from the file name // if (fn.size() > 4 && fn.compare(fn.size()-4,fn.size()-1,".off")==0) { fn = fn.substr(0,fn.size()-4); } else { cout << "Argument " << fn << " is not a file of type .off." << endl; throw("Incorrect filetype."); } cout << "Converting file " << fn << ".off to .plc format" << endl; ifstream in( (fn+".off").c_str() ); if ( !in ) { cerr << "can't open input file:" << fn << ".off" << endl; throw("Can't open input file"); } ofstream out( (fn+".plc").c_str() ); if ( !in ) { cerr << "can't open output file:" << fn << ".plc" << endl; throw("Can't open output file"); } string junk; in >> junk; out << "PLC" << endl; out << "DIMENSION 3" << endl; int nP,nF,nE; in >> nP >> nF >> nE; double x,y,z; map p2id; map id2p; map old2new; int mP = 0; for (int iP=0; iP> x >> y >> z; T tmp; (tmp.first).first = x; (tmp.first).second = y; tmp.second = z; int found =-1; if (found == -1) { p2id[tmp] = mP; id2p[mP] = tmp; mP++; old2new[iP] = p2id[tmp]; } else { old2new[iP] = found; } } out.precision(15); out << "POINTS " << mP << endl; for(int iP=0; iP seg2id; map id2seg; vector< set > faces; int nS=0; int size,first,cur,last; for (int iF=0; iF myF; in >> size; for (int iV=0; iV> first; first = old2new[first]; last = first; } else { in >> cur; cur = old2new[cur]; if (cur != last) { P seg; if (cur < last) { seg.first = cur; seg.second = last; } else { seg.first = last; seg.second = cur; } if (seg2id.find(seg) == seg2id.end()) { seg2id[seg] = nS; id2seg[nS] = seg; nS++; } myF.insert(seg2id[seg]); } last = cur; } } if (cur != first) { P seg; if (cur < first) { seg.first = cur; seg.second = first; } else { seg.first = first; seg.second = cur; } if (seg2id.find(seg) == seg2id.end()) { seg2id[seg] = nS; id2seg[nS] = seg; nS++; } myF.insert(seg2id[seg]); } if (myF.size() > 2) { faces.push_back(myF); } } out << "SEGMENTS " << nS << endl; for (int i=0; i= .5 || ( fractpart < 0 && fractpart > - 0.5 ) ) { tmp = ceil ( tmp ); } else { tmp = floor ( tmp ); } value = tmp * EPSILON; return value; }