Biomechanical Joint Model
 Author: Anderson Maciel

testMetaball_inventor.cpp

Go to the documentation of this file.
00001 #include <util.h>
00002 #undef vector
00003 #undef min
00004 #undef max
00005 
00006 
00007 #include <Inventor/nodes/SoMaterial.h>
00008 #include <Inventor/nodes/SoSeparator.h>
00009 #include <Inventor/Xt/SoXt.h>
00010 #include <Inventor/nodes/SoLineSet.h>
00011 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
00012 #include <Inventor/nodes/SoPointSet.h>
00013 #include <Inventor/nodes/SoVertexProperty.h>
00014 #include <Inventor/nodes/SoDrawStyle.h>
00015 #include <Inventor/nodes/SoSphere.h>
00016 #include <Inventor/actions/SoWriteAction.h>
00017 #include <Inventor/nodes/SoIndexedFaceSet.h>
00018 #include <Inventor/nodes/SoCoordinate3.h>
00019 #include <Inventor/actions/SoSearchAction.h>
00020 #include <Inventor/nodes/SoNormal.h>
00021 #include <Inventor/nodes/SoIndexedLineSet.h>
00022 #include <Inventor/nodes/SoEventCallback.h>
00023 #include <Inventor/events/SoEvents.h>
00024 #include <Inventor/events/SoMouseButtonEvent.h>
00025 
00026 #include <LinAlg/linalg.h>
00027 #include <MarchingCubes/marching_cubes.h>
00028 #include <MarchingCubes/blob.h>
00029 #include <MarchingCubes/mesh.h>
00030 
00031 #include <vector>
00032 #include <list>
00033 
00034 using namespace LinAlg;
00035 using namespace MarchingCubes;
00036 using namespace std;
00037 using namespace VL;
00038 
00039 
00040 // global vars.
00041 float skin_magic_sphere = 1.0;  // sphere threshold redius
00042 float skin_magic_blob = 0.5;
00043 
00044 const int MAX_STRING_SIZE = 256; // maximal lenght of the strings
00045 
00046 void PrintHelp(char *Command){
00047   printf("Usage: %s -r res -m spheres -k pot. field param -out mesh \n",Command);
00048   printf("-r   : Mesh resouluton[0.1 high resolution mesh generated, 2.0 very corse mesh generated]\n");
00049   printf("-m   : File with sphere parameters[cx cy cz r] -> center and radious of the sphere.\n"\
00050          "     : At the begining of the file give the number of the spheres\n");
00051   printf("-k   : Parameter of the potential filed[small values(<1.0) -> implicit surface is big and mesh\n"\
00052          "       doesn't follow the form of the spheres, big values(>1.0) -> surface fallows the form of the spheres\n");
00053   printf("-a   : Parameter of the exponential potential filed[small values(<1.0) -> implicit surface is big and mesh\n"\
00054          "       doesn't follow the form of the spheres, big values(>1.0) -> surface fallows the form of the spheres\n");
00055   printf("out  : Output triangulation base name[generates name.pts(vertices), name.pts.tri(triangles), name.nor(normals)\n"); 
00056 
00057 }
00058 
00059 /**************************************************************************************************
00060   Mouse click callback function  
00061 
00062 ***************************************************************************************************/
00063 
00064 void myMouseEvent(void *userData, SoEventCallback *event){
00065 
00066   
00067   const SoEvent *myEvent = event->getEvent();
00068 
00069   if(SO_MOUSE_PRESS_EVENT(myEvent,BUTTON1))
00070     cout<<"Left mouse button pressed"<<endl;
00071   if(SO_MOUSE_PRESS_EVENT(myEvent,BUTTON2))
00072     cout<<"Middle mouse button pressed"<<endl; 
00073   if(SO_MOUSE_PRESS_EVENT(myEvent,BUTTON3))
00074     cout<<"Right mouse button pressed"<<endl; 
00075 }
00076 
00077 
00078 /***************************************************************************************************
00079   Read ellipsoids from file which format is:
00080  
00081   Number of ellipsoids
00082   N
00083   Center  Radii     Principal axes(they with the center define transf. matrix from g.c.s -> l.c.s
00084   x y z   lx ly lz  (i1.x,i1.y,i1.z) (j1.x,j1.y,j1.z) (k1.x, k1.y, k1.z)
00085 
00086 ****************************************************************************************************/
00087 void readPrimitives(char* fileInp, Metaballs &mb){
00088   FILE *pFile;
00089   VEC C(3), i1(3), j1(3), k1(3);
00090   double lx, ly, lz;
00091   HMAT M;
00092   int number;
00093   
00094   printf("Read metaballs\n");
00095   pFile = fopen ( fileInp , "r" );
00096   if(pFile==NULL){
00097     fprintf(stderr,"Error in reading the file %s\n",fileInp);
00098     exit (1);
00099   }
00100   
00101   // obtain the number of implicit surfaces
00102   fscanf(pFile,"%d",&number);
00103   printf("Numb. of blobs: %d\n",number);
00104    
00105   for (int i=0;i<number;i++){
00106     fscanf(pFile,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",\
00107             &C[0],&C[1],&C[2],&lx,&ly,&lz,&i1[0],&i1[1],&i1[2],&j1[0],&j1[1],&j1[2],&k1[0],&k1[1],&k1[2]);
00108 
00109 
00110     M(0,0)=i1[0]; M(0,1)=j1[0]; M(0,2)=k1[0]; M(0,3)= C[0];
00111     M(1,0)=i1[1]; M(1,1)=j1[1]; M(1,2)=k1[1]; M(1,3)= C[1]; 
00112     M(2,0)=i1[2]; M(2,1)=j1[2]; M(2,2)=k1[2]; M(2,3)= C[2];
00113  
00114     cout<<"M\n"<<M<<endl;
00115     mb.addBlob(Blob(M, lx, ly, lz));
00116   }
00117   getchar();
00118   fclose(pFile);
00119 }
00120 
00121 // Read the metaballs(Raquel's predoc project) from file
00122 void readSpheres(char* fileInp,  MetaballsSphere &mb, double k)
00123 {
00124   // file with the number of implicit surfaces
00125   // for each implicit surface (x y z e k)-> params of the field function for the spherical metaballs
00126 
00127   FILE *pFile;
00128   //TsiSphere *vector;
00129   VEC C(3);
00130   double ro;
00131   int number;
00132 
00133   printf("Read spheres\n");
00134   printf("Reading the file %s\n",fileInp);
00135   pFile = fopen ( fileInp , "r" );
00136   if (pFile==NULL){
00137       printf("Error in reading the file\n");
00138       exit (1);
00139   }
00140 
00141   // obtain the number of implicit surfaces
00142   fscanf(pFile,"%d",&number);
00143   //printf("NUMBER %d\n",*number);
00144 
00145   //printf("number %d\n",*number);
00146   //vector=(TsiSphere *) malloc((*number)*sizeof(TsiSphere));
00147   for (int i=0;i<number;i++){
00148     fscanf(pFile,"%lf %lf %lf %lf\n",&C[0],&C[1],&C[2],&ro);
00149     printf("%lf %lf %lf %lf %lf\n",C[0],C[1],C[2],ro,k);
00150     mb.addSphere(Sphere(C, ro, k));
00151   }
00152   fclose(pFile);
00153   printf("Read spheres finished\n");
00154 }
00155 
00156 /**************************************************************************************************
00157   Save triangulated isosurface of the metaball field function int the Trig like format.
00158   This means make three files: *.pts with vertex coordinates
00159                                *.tri with facets
00160                                *.nx, *.ny, *.nz - binary files with vertex normals
00161 **************************************************************************************************/
00162 
00163 void SaveIsoSurf2Trig(const marchingCubes &surf, char *BaseName){
00164   vector<VEC> vertlist = surf.getVertices();
00165   vector<VEC> normlist = surf.getNormals();
00166   vector<Triangle> trilist = surf.getTriangles();
00167   char pts_fname[MAX_STRING_SIZE], tri_fname[MAX_STRING_SIZE], nor_fname[MAX_STRING_SIZE];
00168   FILE *fpts, *ftri, *fnor;
00169 
00170 
00171   // specific output file name creation
00172 
00173   strcpy(pts_fname,BaseName); strcat(pts_fname,".pts");
00174   strcpy(tri_fname,BaseName); strcat(tri_fname,".pts.tri");
00175   strcpy(nor_fname,BaseName); strcat(nor_fname,".nor");
00176 
00177   // oppening the output files for writting
00178   fpts = fopen (pts_fname,"wt");
00179   if(fpts==NULL){
00180     fprintf(stderr,"Error in reading the file %s\n",pts_fname);
00181     exit (1);
00182   }
00183 
00184   ftri = fopen (tri_fname,"wt");
00185   if(ftri==NULL){
00186     fprintf(stderr,"Error in reading the file %s\n",tri_fname);
00187     exit (1);
00188   }
00189 
00190   fnor = fopen (nor_fname,"wb");
00191   if(fnor==NULL){
00192     fprintf(stderr,"Error in reading the file %s\n",nor_fname);
00193     exit (1);
00194   }
00195 
00196   // copy vertices and normals ito the *.pts and *.nx, *.ny and *.nz files
00197   for (int i=0; i<vertlist.size(); i++){
00198     fprintf(fnor,"%lf %lf %lf\n",normlist[i][0],normlist[i][1],normlist[i][2]);
00199     fprintf(fpts,"%lf %lf %lf\n",vertlist[i][0],vertlist[i][1],vertlist[i][2]);
00200   }
00201   for (vector<Triangle>::const_iterator tri=trilist.begin(); tri!=trilist.end(); tri++){     
00202     fprintf(ftri,"%d %d %d\n",tri->indices[0], tri->indices[1], tri->indices[2]); 
00203   }
00204   fclose(fpts); fprintf(stdout,"%s saved ...\n",pts_fname);
00205   fclose(ftri); fprintf(stdout,"%s saved ...\n",tri_fname);
00206   fclose(fnor); fprintf(stdout,"%s saved ...\n",nor_fname);
00207 }                                          
00208                                                                                                    
00209 /*************************************************************************************
00210   Save caracteristic points defining metaballs into the file in world coordinates.
00211   These points are on the ends of the half radius of the metaballs(in our case
00212   exponential metaballs-elipsoids with exponential field function)
00213 **************************************************************************************/
00214 void SaveMb2Pts(char *in_fname, char *out_fname){
00215   VEC X(3), Lxp(3), Lxn(3), Lyp(3), Lyn(3), Lzp(3), Lzn(3), C(3);
00216   FILE *fin, *fout;
00217   int number;
00218   HMAT M;
00219   VEC  i1(3), j1(3), k1(3);
00220   double lx, ly, lz;
00221   
00222   // oppening the output files for writting
00223   fin = fopen(in_fname,"rt");
00224   if(fin==NULL){
00225     fprintf(stderr,"Error in reading the file %s\n",in_fname);
00226     exit (1);
00227   }
00228   
00229   // oppening the output files for writting
00230   fout = fopen(out_fname,"wt");
00231   if(fout==NULL){
00232     fprintf(stderr,"Error in reading the file %s\n",out_fname);
00233     exit (1);
00234   }
00235 
00236   // obtain the number of implicit surfaces
00237   fscanf(fin,"%d",&number);
00238   printf("Numb. of blobs: %d\n",number);
00239    
00240   for (int i=0;i<number;i++){
00241     // read metaball params from file
00242     fscanf(fin,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",\
00243             &C[0],&C[1],&C[2],&lx,&ly,&lz,&i1[0],&i1[1],&i1[2],&j1[0],&j1[1],&j1[2],&k1[0],&k1[1],&k1[2]);
00244 
00245     M(0,0)=i1[0]; M(0,1)=j1[0]; M(0,2)=k1[0]; M(0,3)= C[0];
00246     M(1,0)=i1[1]; M(1,1)=j1[1]; M(1,2)=k1[1]; M(1,3)= C[1]; 
00247     M(2,0)=i1[2]; M(2,1)=j1[2]; M(2,2)=k1[2]; M(2,3)= C[2];
00248 
00249     // set 8 points in the l.c.s defining ellipsoid and its center
00250     Lxp[0]=lx/2.0;  Lxp[1]=0.0;    Lxp[2]=0.0;
00251     Lyp[0]=0.0;     Lyp[1]=ly/2.0; Lyp[2]=0.0;
00252     Lzp[0]=0.0;     Lzp[1]=0.0;    Lzp[2]=lz/2.0;
00253     Lxn = -Lxp;
00254     Lyn = -Lyp;
00255     Lzn = -Lzp;
00256   
00257     // find their coordinates in g.c.s and write them to file
00258     fprintf(fout,"%lf %lf %lf\n",C[0],C[1],C[2]);
00259     X = M.fullMult(Lxp);
00260     fprintf(fout,"%lf %lf %lf\n",X[0],X[1],X[2]);
00261     X = M.fullMult(Lxn);
00262     fprintf(fout,"%lf %lf %lf\n",X[0],X[1],X[2]);
00263     X = M.fullMult(Lyp);
00264     fprintf(fout,"%lf %lf %lf\n",X[0],X[1],X[2]);
00265     X = M.fullMult(Lyn);
00266     fprintf(fout,"%lf %lf %lf\n",X[0],X[1],X[2]);
00267     X = M.fullMult(Lzp);
00268     fprintf(fout,"%lf %lf %lf\n",X[0],X[1],X[2]);
00269     X = M.fullMult(Lzn);
00270     fprintf(fout,"%lf %lf %lf\n",X[0],X[1],X[2]);
00271  
00272   }
00273 }
00274                                    
00275                                                                                                    
00276 
00277 void displayBBox(SoSeparator *group, const BBox &bb, float lineWidth, uint32_t color){
00278    // Line Set
00279    SoDrawStyle *drawStyle= new SoDrawStyle;
00280    drawStyle->style.setValue(SoDrawStyle::LINES);
00281    drawStyle->lineWidth.setValue(lineWidth);
00282    group->addChild(drawStyle);
00283    
00284    SoIndexedLineSet *ls = new SoIndexedLineSet;
00285    SoVertexProperty *vp = new SoVertexProperty;
00286    vp->materialBinding = SoVertexProperty::OVERALL;  
00287    ls->vertexProperty.setValue(vp);
00288    group->addChild(ls);
00289    vp->orderedRGBA.set1Value(0, color);
00290 
00291    int idx_vtx=0, idx=0;
00292 
00293    VEC IX(bb.getDim()), IY(bb.getDim()), IZ(bb.getDim());
00294    IX[1]=IX[2]=IY[0]=IY[2]=IZ[0]=IZ[1]=0;
00295    VEC O(bb.getMin());
00296              
00297    // construct points
00298    for (int p=0; p<8; p++)
00299    {
00300       int x= ((p&4)>>2);
00301       int y= ((p&2)>>1);
00302       int z= (p&1);
00303                 
00304       VEC P = O + x*IX +  y*IY + z*IZ;
00305       vp->vertex.set1Value(idx_vtx+p, P);
00306    }
00307              
00308    // construct lines
00309    ls->coordIndex.set1Value(idx++, idx_vtx+0);
00310    ls->coordIndex.set1Value(idx++, idx_vtx+4);
00311    ls->coordIndex.set1Value(idx++, idx_vtx+6);
00312    ls->coordIndex.set1Value(idx++, idx_vtx+2);
00313    ls->coordIndex.set1Value(idx++, idx_vtx+0);
00314    ls->coordIndex.set1Value(idx++, SO_END_LINE_INDEX);
00315 
00316    ls->coordIndex.set1Value(idx++, idx_vtx+0);
00317    ls->coordIndex.set1Value(idx++, idx_vtx+1);
00318    ls->coordIndex.set1Value(idx++, idx_vtx+3);
00319    ls->coordIndex.set1Value(idx++, idx_vtx+2);
00320    ls->coordIndex.set1Value(idx++, idx_vtx+0);
00321    ls->coordIndex.set1Value(idx++, SO_END_LINE_INDEX);
00322 
00323    ls->coordIndex.set1Value(idx++, idx_vtx+1);
00324    ls->coordIndex.set1Value(idx++, idx_vtx+5);
00325    ls->coordIndex.set1Value(idx++, idx_vtx+7);
00326    ls->coordIndex.set1Value(idx++, idx_vtx+3);
00327    ls->coordIndex.set1Value(idx++, idx_vtx+1);
00328    ls->coordIndex.set1Value(idx++, SO_END_LINE_INDEX);
00329 
00330    ls->coordIndex.set1Value(idx++, idx_vtx+3);
00331    ls->coordIndex.set1Value(idx++, idx_vtx+2);
00332    ls->coordIndex.set1Value(idx++, idx_vtx+6);
00333    ls->coordIndex.set1Value(idx++, idx_vtx+7);
00334    ls->coordIndex.set1Value(idx++, idx_vtx+3);
00335    ls->coordIndex.set1Value(idx++, SO_END_LINE_INDEX);
00336 
00337    ls->coordIndex.set1Value(idx++, idx_vtx+0);
00338    ls->coordIndex.set1Value(idx++, idx_vtx+1);
00339    ls->coordIndex.set1Value(idx++, idx_vtx+5);
00340    ls->coordIndex.set1Value(idx++, idx_vtx+4);
00341    ls->coordIndex.set1Value(idx++, idx_vtx+0);
00342    ls->coordIndex.set1Value(idx++, SO_END_LINE_INDEX);
00343 
00344    ls->coordIndex.set1Value(idx++, idx_vtx+5);
00345    ls->coordIndex.set1Value(idx++, idx_vtx+4);
00346    ls->coordIndex.set1Value(idx++, idx_vtx+6);
00347    ls->coordIndex.set1Value(idx++, idx_vtx+7);
00348    ls->coordIndex.set1Value(idx++, idx_vtx+5);
00349    ls->coordIndex.set1Value(idx++, SO_END_LINE_INDEX);
00350 
00351    idx_vtx += 8;
00352           
00353    vp->vertex.setNum(idx_vtx);
00354    ls->coordIndex.setNum(idx);
00355 }
00356 
00357 /*
00358 void
00359 displayBBoxHierarchy(SoSeparator *group, const BBoxHierarchy *bbh)
00360 {   
00361    int level = bbh->level;
00362    float scale = 1 - (0.02*level);
00363    BBox bb(bbh->bb);
00364    bb.scale(scale);
00365         
00366    displayBBox(group, bb, 3./(float)(1.+level), (uint32_t)((0xFF00 << 4*level)+ 0xFF));
00367 
00368    for (int i=0; i< bbh->bboxlist.size(); i++)
00369       displayBBoxHierarchy(group, bbh->bboxlist[i] );
00370 }
00371 */
00372 
00373 
00374 SoSeparator *drawVoxelisation(const marchingCubes &surf)
00375 {
00376  
00377   SoSeparator *group = new SoSeparator;
00378   SoMaterial *material = new SoMaterial; 
00379   material->diffuseColor.setValue(0.8, 0.8, 0.8);
00380   group->addChild(material);
00381   SoDrawStyle *style = new SoDrawStyle;
00382   style->style = SoDrawStyle::FILLED;
00383   group->addChild(style);
00384 
00385   SoIndexedFaceSet *set = new SoIndexedFaceSet;
00386   group->addChild(set);
00387   SoVertexProperty *vp = new SoVertexProperty;
00388   vp->normalBinding = SoVertexProperty::PER_VERTEX_INDEXED;
00389   set->vertexProperty = vp;
00390 
00391   vector<VEC> vertlist = surf.getVertices();
00392   vector<VEC> normlist = surf.getNormals();
00393   vector<Triangle> trilist = surf.getTriangles();
00394 
00395   // copy vertices and normals into Vertex Property node
00396   for (int i=0; i<vertlist.size(); i++)
00397   {
00398      vp->normal.set1Value(i, normlist[i]);
00399      vp->vertex.set1Value(i, vertlist[i]);
00400   }
00401   vp->vertex.setNum(vertlist.size());
00402   vp->normal.setNum(normlist.size());
00403    
00404   int num=0;
00405   for (vector<Triangle>::const_iterator tri=trilist.begin(); tri!=trilist.end(); tri++)
00406   {     
00407      for (int k=0; k<3; k++)   
00408      {
00409         set->coordIndex.set1Value(num++, tri->indices[k]); 
00410      }
00411      set->coordIndex.set1Value(num++, SO_END_FACE_INDEX);
00412   }
00413   set->coordIndex.setNum(num);
00414 
00415   cout << "# tris = " << trilist.size() << " # points = " << vertlist.size() << endl;
00416   return group;
00417 }
00418 
00419 SoSeparator * readFile(const char *filename) 
00420 {
00421    // Open the input file       
00422    SoInput mySceneInput;
00423    if (!mySceneInput.openFile(filename)) {
00424       fprintf(stderr, "Cannot open file %s\n", filename);
00425       return NULL;
00426    } 
00427    // Read the whole file into the database
00428    SoSeparator *myGraph = SoDB::readAll(&mySceneInput);
00429    if (myGraph == NULL) {
00430       fprintf(stderr, "Problem reading file\n");
00431       return NULL;
00432    } 
00433    mySceneInput.closeFile();
00434    return myGraph;
00435 }
00436 
00437 void writeFile(SoNode *node, const char *filename)
00438 {
00439   SoWriteAction wa;
00440   wa.getOutput()->openFile(filename);
00441   wa.getOutput()->setBinary(0);
00442   wa.apply(node);
00443 }
00444 
00445 void analyseScene (SoSeparator *root, SoCoordinate3 **coords, SoNormal **norms, SoIndexedFaceSet **ifs)
00446 {
00447    SoSearchAction mySearchAction;
00448    mySearchAction.setType(SoCoordinate3::getClassTypeId());
00449    mySearchAction.setInterest(SoSearchAction::FIRST);
00450    mySearchAction.apply(root);
00451    assert( mySearchAction.getPath()!=NULL );
00452    *coords = (SoCoordinate3*) mySearchAction.getPath()->getTail();
00453    (*coords)->ref();
00454 
00455    mySearchAction.setType(SoNormal::getClassTypeId());
00456    mySearchAction.setInterest(SoSearchAction::FIRST);
00457    mySearchAction.apply(root);
00458    assert( mySearchAction.getPath()!=NULL );
00459    *norms = (SoNormal*) mySearchAction.getPath()->getTail();
00460    (*norms)->ref();
00461 
00462    mySearchAction.setType(SoIndexedFaceSet::getClassTypeId());
00463    mySearchAction.setInterest(SoSearchAction::FIRST);
00464    mySearchAction.apply(root);
00465    assert( mySearchAction.getPath()!=NULL );
00466    *ifs = (SoIndexedFaceSet*) mySearchAction.getPath()->getTail();
00467    (*ifs)->ref();
00468 }
00469 
00470 #include <stdio.h>
00471 void saveToPLY(const marchingCubes &surf, const char *filename)
00472 {
00473    FILE *fp = fopen(filename, "w");
00474    vector<VEC> vertlist = surf.getVertices();
00475    vector<VEC> normlist = surf.getNormals();
00476    vector<Triangle> trilist = surf.getTriangles();
00477 
00478    fprintf(fp, "ply\nformat ascii 1.0\n");
00479    fprintf(fp, "element vertex %d\n", vertlist.size());
00480    fprintf(fp, "property float x\n");
00481    fprintf(fp, "property float y\n");
00482    fprintf(fp, "property float z\n");
00483    fprintf(fp, "element face %d\n", trilist.size());
00484    fprintf(fp, "property list uchar int vertex_indices\n");
00485    fprintf(fp, "end_header\n");
00486 
00487    for (int i=0; i<vertlist.size(); i++)
00488       fprintf(fp,"%.5f %.5f %.5f \n", vertlist[i][0], vertlist[i][1], vertlist[i][2]);
00489 
00490    for (int i=0; i<trilist.size(); i++)
00491       fprintf(fp,"3 %d %d %d \n", 
00492               trilist[i].indices[0], 
00493               trilist[i].indices[1], 
00494               trilist[i].indices[2]); 
00495 
00496    fclose(fp);
00497 }
00498 
00499 Mesh *buildMeshFromIv(const SoCoordinate3 *coords, const SoNormal *norms, const SoIndexedFaceSet *ifs)
00500 {
00501   int i;
00502   // construct coords
00503   VECArray vertlist(coords->point.getNum());
00504   for (i=0; i<vertlist.size(); i++)
00505      vertlist[i] = VEC(coords->point[i].getValue(), 3);
00506 
00507   // construct normals
00508   VECArray normlist(norms->vector.getNum());
00509   for (i=0; i<normlist.size(); i++)
00510      normlist[i] = VEC(norms->vector[i].getValue(), 3);
00511   
00512   vector<int> triangles;
00513   for (int k, i=0; i<ifs->coordIndex.getNum();i=k)
00514   {
00515      k = i+1;
00516      while (ifs->coordIndex[k] != SO_END_FACE_INDEX) k++;
00517 
00518      if (k-i != 3) 
00519         cout << "found face other than triangle\n";
00520      else
00521      {          
00522         triangles.push_back(ifs->coordIndex[i]);
00523         triangles.push_back(ifs->coordIndex[i+1]);
00524         triangles.push_back(ifs->coordIndex[i+2]);
00525         while (ifs->coordIndex[k] == SO_END_FACE_INDEX) k++;
00526      }
00527   }
00528 
00529   vector<int> normIndex;
00530   for (int k, i=0; i<ifs->normalIndex.getNum();i=k)
00531   {
00532      k = i+1;
00533      while (ifs->normalIndex[k] != SO_END_FACE_INDEX) k++;
00534 
00535      if (k-i != 3) 
00536         cout << "found face other than triangle\n";
00537      else
00538      {          
00539         normIndex.push_back(ifs->normalIndex[i]);
00540         normIndex.push_back(ifs->normalIndex[i+1]);
00541         normIndex.push_back(ifs->normalIndex[i+2]);
00542         while (ifs->normalIndex[k] == SO_END_FACE_INDEX) k++;
00543      }
00544   }
00545   
00546   return new Mesh(vertlist, normlist, triangles, normIndex);
00547 }
00548 
00549 
00550 void main(int argc, char **argv)
00551 {
00552   double res, coeff, stiff;
00553   char MbFile[256], OutFile[256];
00554   Widget myWindow = SoXt::init(argv[0]);
00555   if (argc<3) { PrintHelp(argv[0]); exit(-1); }
00556   ReadOptionDouble("-r",&res);
00557   ReadOptionDouble("-k",&stiff);
00558   ReadOptionDouble("-a",&coeff);
00559   ReadOptionString("-m",MbFile);
00560   ReadOptionString("-out",OutFile);
00561   int sP = IsOption("-e");
00562   MetaballsSphere_exp mb_e(skin_magic_blob,coeff);
00563   MetaballsSphere mb_s(skin_magic_sphere);
00564 
00565   if (myWindow == NULL) exit(1); 
00566 
00567   // construct a simple scene graph
00568   SoSeparator *root = new SoSeparator;
00569   root->ref();
00570   vector<Primitive*> primitives;
00571 
00572   if(sP){
00573     readSpheres(MbFile,mb_e,stiff);
00574     primitives.push_back(&mb_e);
00575     fprintf(stderr,"Added to primitives[-e]\n"); 
00576   }
00577   else{
00578     readSpheres(MbFile,mb_s,stiff);
00579     primitives.push_back(&mb_s);
00580     fprintf(stderr,"Added to primitives[-s]\n");
00581   }
00582 
00583      /*
00584   // }else if(IsOption("-e")){
00585     Metaballs_exp mb_e(skin_magic_blob); 
00586     readPrimitives(MbFile,mb_e);
00587     primitives.push_back(&mb_e);
00588     
00589   }else{
00590     printf("Error: Input file with metaball primitives is missing\n");
00591     exit(-1);
00592   }
00593     */
00594   marchingCubes surface(primitives, res);
00595   fprintf(stderr,"Marching cubes performed\n");
00596   /*
00597   SoSeparator *voxelisation = drawVoxelisation(surface);
00598   root->addChild(voxelisation);
00599 
00600   // Adding event handler for mouse clicked
00601   SoEventCallback *mouseEvent = new  SoEventCallback;
00602   mouseEvent->addEventCallback(SoMouseButtonEvent::getClassTypeId(),myMouseEvent,voxelisation);
00603   voxelisation->addChild(mouseEvent);
00604 
00605 
00606 //   root->addChild(bone); 
00607 //   displayBBoxHierarchy(root, &(mesh->bbox));
00608 
00609   writeFile(root, "./metaballs.iv");
00610   */
00611   if(strcmp(OutFile,""))
00612     SaveIsoSurf2Trig(surface, OutFile);
00613   //SaveMb2Pts(argv[2],argv[3]); 
00614 
00615   // Set up viewer:
00616   /*
00617   SoXtExaminerViewer *myViewer;
00618   myViewer = new SoXtExaminerViewer(myWindow);
00619   myViewer->setSceneGraph(root);
00620   myViewer->setTitle("Examiner Viewer");
00621   myViewer->show();
00622 
00623   SoXt::show(myWindow);
00624   SoXt::mainLoop();
00625   */
00626 }

Generated on Thu Dec 1 10:13:40 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5