Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

m2sm.C

Go to the documentation of this file.
00001 /**********************************************************************
00002  * m2sm.C:
00003  * 
00004  *   convert from Hugues Hoppe's .m format to jot .sm format.
00005  *
00006  **********************************************************************/
00007 #include "std/config.H"
00008 #include "mesh/mi.H"
00009 
00010 void
00011 read_vert(Wpt_list& pts) 
00012 {
00013   int num; // vert number 
00014   double x = 0, y = 0, z = 0;
00015   cin >> num >> x >> y >> z;
00016 
00017   if (num < 1) {
00018     cerr << "invalid vert num " << num << endl;
00019   } else if (num > 1e6) {
00020     cerr << "too scared to handle large vert num " << num << endl;
00021   } else {
00022     while(pts.num() <= num) {
00023       pts += Wpt();
00024     }
00025     pts[num] = Wpt(x,y,z);
00026   }
00027 
00028   cin.ignore(256, '\n');
00029 }
00030 
00031 void
00032 read_face(ARRAY<Point3i>& faces)
00033 {
00034   int num; // face number (ignored)
00035   int i = -1, j = -1, k = -1; // vert indices
00036   cin >> num >> i >> j >> k;
00037   if ( i>0 && j>0 && k>0 ) {
00038     faces += Point3i(i,j,k);
00039   } else {
00040     cerr << "error reading face " << num 
00041          << ": " << i << ", " << j << ", " << k << endl;
00042   }
00043   cin.ignore(256, '\n');
00044 }
00045 
00046 int 
00047 main(int argc, char *argv[])
00048 {
00049    if (argc != 1) {
00050       err_msg("Usage: %s < input.m", argv[0]);
00051       return 1;
00052    }
00053 
00054    Wpt_list pts;
00055    ARRAY<Point3i> faces;
00056 
00057    char keyword[256];
00058 
00059    do {
00060      cin >> keyword;
00061      if ( !strcmp(keyword, "Vertex")) {
00062        read_vert(pts);
00063      } else if ( !strcmp(keyword, "Face")) {
00064        read_face(faces);
00065      } else {
00066        cin.ignore(256, '\n');
00067      }
00068      keyword[0] = 0; // start fresh
00069    } while (!cin.eof());   
00070    
00071    BMESHptr mesh = new BMESH;
00072    
00073    int i=0;
00074    for (i=1; i<pts.num(); i++) {
00075      mesh->add_vertex(pts[i]);
00076    }
00077 
00078    for (i=0; i<faces.num(); i++) {
00079      mesh->add_face(faces[i][0]-1,
00080                     faces[i][1]-1,
00081                     faces[i][2]-1);
00082    }
00083 
00084    // remove any unused vertices
00085    for (i=mesh->nverts()-1; i>=0; i--) {
00086      if (mesh->bv(i)->degree() == 0) {
00087        mesh->remove_vertex(mesh->bv(i));
00088      }
00089    }
00090 
00091    mesh->changed();
00092 
00093    // Remove duplicate vertices while we're at it
00094    mesh->remove_duplicate_vertices(false); // don't keep the bastards
00095 
00096    bool is_bad = false;
00097    for (i=0; i<mesh->nedges(); i++)
00098       if (!mesh->be(i)->consistent_orientation())
00099          is_bad = true;
00100    if (is_bad)
00101       err_msg("Warning: inconsistently oriented triangles -- can't fix");
00102 
00103    // What the hell, recenter if they like:
00104    if (Config::get_var_bool("JOT_RECENTER"))
00105       mesh->recenter();
00106 
00107    if (Config::get_var_bool("JOT_PRINT_MESH"))
00108       mesh->print();
00109 
00110    mesh->write_stream(cout);
00111 
00112    return 0;
00113 }

Generated on Mon Sep 18 11:39:32 2006 for jot by  doxygen 1.4.4