The Parametric Pseudo-Manifold (PPS) Library 1.0
sampler.cpp
Go to the documentation of this file.
00001 
00024 #include <iostream>              // std::cout, std::endl
00025 #include <string>                // std::string
00026 #include <cmath>                 // sqrt
00027 #include <cstdlib>               // atoi
00028 
00029 #include "surface.h"             // dcel::Surface
00030 
00031 #include "pps.h"                 // pps::PPS
00032 #include "ppssampler.h"          // pps::PPSsampler
00033 #include "ppsfrompnt.h"          // ppsfrompnt::PPSfromPNT
00034 
00035 #include "reader.h"              // offlib::Reader
00036 #include "writer.h"              // offlib::Writer
00037 
00038 #include "vertex_attribute.h"    // ppsfrompnt::VertexAttribute
00039 #include "halfedge_attribute.h"  // ppsfrompnt::HalfedgeAttribute
00040 #include "face_attribute.h"      // ppsfrompnt::FaceAttribute
00041 
00042 
00053 int main( int argc , char *argv[ ] )
00054 {
00055   using std::cout ;
00056   using std::endl ;
00057 
00058   using ppsfrompnt::VertexAttribute ;
00059   using ppsfrompnt::HalfedgeAttribute ;
00060   using ppsfrompnt::FaceAttribute ;
00061 
00062   //
00063   // typedef Surface
00064   //
00065   typedef dcel::Surface< VertexAttribute , FaceAttribute , int ,
00066       HalfedgeAttribute > Surface ;
00067 
00068   //
00069   // This program expects two command-line arguments.
00070   //
00071   if ( argc != 3 ) {
00072     cout << endl ;
00073     cout << "Usage: sampler-pnt <arg 1> <arg 2>" << endl ;
00074     cout << endl ;
00075     cout << "arg1: the filename of an OFF file without extension." << endl ;
00076     cout << "arg2: the level-of-detail of the output triangular mesh." << endl ;
00077     cout << endl ;
00078     cout << "For instance:" << endl ;
00079     cout << "sampler-pnt star 3" << endl ;
00080     cout << endl ;
00081     return 0 ;
00082   }
00083 
00090   std::string fn1( argv[ 1 ] ) ;
00091   unsigned lod = unsigned( atoi( argv[ 2 ] ) ) ;
00092 
00093   //
00094   // Read in the underlying mesh information.
00095   //
00096 
00097   cout << "Reading input file..." << endl ;
00098 
00099   unsigned nv, nf ;
00100   double* vset ;
00101   unsigned* fset ;
00102 
00103   offlib::Reader reader( fn1 ) ;
00104 
00105   reader.read( nv , vset , nf , fset ) ;
00106 
00107   //
00108   // Creates the underlyng mesh and stores it in a DCEL.
00109   //
00110 
00111   cout << "Creating surface..." << endl ;
00112 
00113   Surface* mesh = new Surface( nv , vset , nf , fset ) ;
00114 
00115   //
00116   // Release memory.
00117   //
00118 
00119   cout << "Releasing memory..." << endl ;
00120 
00121   if ( vset != 0 ) {
00122     delete[] vset ;
00123   }
00124 
00125   if ( fset != 0 ) {
00126     delete[] fset ;
00127   }
00128 
00129   //
00130   // Creates a PPS that approximates the PN triangle surface.
00131   //
00132 
00133   cout << "Creating the PPS..." << endl ;
00134 
00135   ppsfrompnt::PPSfromPNT* pps = new ppsfrompnt::PPSfromPNT( mesh ) ;
00136 
00137   pps->build() ;
00138 
00139   //
00140   // Sample the PN triangle surface and the PPS.
00141   //
00142 
00143   cout << "Sampling the PN triangle surface and the PPS ..." << endl ;
00144 
00145   pps::PPSsampler< Surface >* sampler = 
00146     new pps::PPSsampler< Surface >( pps ) ;
00147 
00148   double* lv1 ;
00149   double* lv2 ;
00150 
00151   unsigned* lf ;
00152 
00153   sampler->sample( lod , nv , lv1 , lv2 , nf , lf ) ;
00154 
00155   //
00156   // Writing out the output files.
00157   //
00158 
00159   cout << "Generating the output files..." << endl ;
00160 
00161   std::string fn2( fn1 + std::string( "-pnt-pps-" ) 
00162                    + std::string( argv[ 2 ] ) ) ;
00163 
00164   std::string fn3( fn1 + std::string( "-pnt-" ) 
00165                    + std::string( argv[ 2 ] ) ) ;
00166 
00167   nv /= 3 ;
00168   nf /= 3 ;
00169 
00170   offlib::Writer writer1( fn2 ) ;
00171   writer1.write( nv , &lv1[ 0 ] , nf , &lf[ 0 ] ) ;
00172 
00173   if ( lv1 != 0 ) delete lv1 ;
00174 
00175   offlib::Writer writer2( fn3 ) ;
00176   writer2.write( nv , &lv2[ 0 ] , nf , &lf[ 0 ] ) ;
00177 
00178   if ( lv2 != 0 ) delete lv2 ;
00179 
00180   if ( lf != 0 ) delete lf ;
00181 
00182   //
00183   // Release memory.
00184   //
00185 
00186   cout << "Releasing memory..." << endl ;
00187 
00188   if ( sampler != 0 ) {
00189     delete sampler ;
00190   }
00191 
00192   if ( pps != 0 ) {
00193     delete pps ;
00194   }
00195 
00196   if ( mesh != 0 ) {
00197     delete mesh ;
00198   }
00199 
00200   return 0 ;
00201 }