TriangleLoader.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include <sofa/helper/io/TriangleLoader.h>
00028 #include <sofa/helper/system/FileRepository.h>
00029 
00030 #include <assert.h>
00031 #include <stdlib.h>
00032 #include <iostream>
00033 #include <vector>
00034 #include <sofa/defaulttype/Vec.h>
00035 #include <string.h>
00036 
00037 namespace sofa
00038 {
00039 
00040 namespace helper
00041 {
00042 
00043 namespace io
00044 {
00045 
00046 using namespace sofa::defaulttype;
00047 
00048 // static void skipToEOL(FILE* f)
00049 // {
00050 //  int ch;
00051 //  while ((ch = fgetc(f)) != EOF && ch != '\n');
00052 // }
00053 
00054 bool TriangleLoader::load(const char *filename)
00055 {
00056     std::string fname = filename;
00057     if (!sofa::helper::system::DataRepository.findFile(fname)) return false;
00058     FILE*   file;
00059 
00060     /* open the file */
00061     file = fopen(fname.c_str(), "r");
00062     if (!file)
00063     {
00064         fprintf(stderr, "readOBJ() failed: can't open data file \"%s\".\n",
00065                 filename);
00066         return false;
00067     }
00068 
00069     /* announce the model name */
00070     printf("Loading Triangle model: %s\n", filename);
00071 
00072     loadTriangles (file);
00073     fclose(file);
00074 
00075     return true;
00076 }
00077 
00078 void TriangleLoader::loadTriangles(FILE *file)
00079 {
00080     assert (file != NULL);
00081 
00082     char buf[128];
00083     std::vector<Vector3> normals;
00084     Vector3 n;
00085     float x, y, z;
00086 //  /* make a default group */
00087 //  
00088     while (fscanf(file, "%s", buf) != EOF)
00089     {
00090         switch (buf[0])
00091         {
00092             case '#':
00093                 /* comment */
00094                 /* eat up rest of line */
00095                 fgets(buf, sizeof(buf), file);
00096                 break;
00097             case 'v':
00098                 /* v, vn, vt */
00099                 switch (buf[1])
00100                 {
00101                     case '\0':
00102                         /* vertex */
00103                         //p = new Vector3();    
00104                         fscanf(file, "%f %f %f", &x, &y, &z);
00105                         addVertices(x, y, z);
00106                         
00107                         // fgets(buf, sizeof(buf), file);
00108                         // numvertices++;
00109                         break;
00110                     case 'n':
00111                         /* normal */
00112                         /* eat up rest of line */
00113                         fgets(buf, sizeof(buf), file);
00114                         //fscanf(file, "%lf %lf %lf", &(n.x), &(n.y), &(n.z));
00115                         //normals.push_back(n);
00116                         break;
00117                     case 't':
00118                         /* texcoord */
00119                         /* eat up rest of line */
00120                         fgets(buf, sizeof(buf), file);
00121                         break;
00122                     default:
00123                         printf("loadTriangles(): Unknown token \"%s\".\n", buf);
00124                         exit(1);
00125                         break;
00126                 }
00127                 break;
00128             case 'm':
00129                 /* eat up rest of line */
00130                 fgets(buf, sizeof(buf), file);
00131                 break;
00132             case 'u':
00133                 /* eat up rest of line */
00134                 fgets(buf, sizeof(buf), file);
00135                 break;
00136             case 'g':
00137                 /* eat up rest of line */
00138                 fgets(buf, sizeof(buf), file);
00139                 break;
00140             case 'f':
00141                 /* face */
00142                 fscanf(file, "%s", buf);
00143                 int n1, n2, n3, v1, v2, v3, t1, t2, t3;
00144                 /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
00145                 if (strstr(buf, "//"))
00146                 {
00147                     /* v//n */
00148                     sscanf(buf, "%d//%d", &v1, &n1);
00149                     fscanf(file, "%d//%d", &v2, &n2);
00150                     fscanf(file, "%d//%d", &v3, &n3);
00151                     //Triangle *t = new Triangle(vertices[v1 - 1], 
00152                     //                         vertices[v2 - 1], 
00153                     //                         vertices[v3 - 1], 
00154                     //                         velocityVertices[v1 - 1],
00155                     //                         velocityVertices[v2 - 1],
00156                     //                         velocityVertices[v3 - 1],
00157                     //                         (normals[n1] + normals[n2] + normals[n3]) / 3, 
00158                     //                         this);
00159                     //elems.push_back(t);
00160                     addTriangle(v1 - 1, v2 - 1, v3 -1);
00161                 }
00162                 else if (sscanf(buf, "%d/%d/%d", &v1, &t1, &n1) == 3)
00163                 {
00164                     /* v/t/n */
00165                     
00166                     fscanf(file, "%d/%d/%d", &v2, &t2, &n2);
00167                     fscanf(file, "%d/%d/%d", &v3, &t3, &n3);
00168                     /* Triangle *t = new Triangle(vertices[v1 - 1],
00169                                                vertices[v2 - 1], 
00170                                                vertices[v3 - 1], 
00171                                                velocityVertices[v1 - 1],
00172                                                velocityVertices[v2 - 1],
00173                                                velocityVertices[v3 - 1],
00174                                                (normals[n1] + normals[n2] + normals[n3]) / 3, 
00175                                                this);
00176                     elems.push_back(t); */
00177                     addTriangle(v1 - 1, v2 - 1, v3 -1);
00178                 }
00179                 else if (sscanf(buf, "%d/%d", &v1, &t1) == 2)
00180                 {
00181                     /* v/t */
00182                     fscanf(file, "%d/%d", &v2, &t2);
00183                     fscanf(file, "%d/%d", &v3, &t3);
00184                     /* Triangle *t = new Triangle(vertices[v1 - 1], 
00185                                                vertices[v2 - 1], 
00186                                                vertices[v3 - 1], 
00187                                                velocityVertices[v1 - 1],
00188                                                velocityVertices[v2 - 1],
00189                                                velocityVertices[v3 - 1],
00190                                                (normals[n1] + normals[n2] + normals[n3]) / 3, 
00191                                                this);
00192                     elems.push_back(t); */
00193                     addTriangle(v1 - 1, v2 - 1, v3 -1);
00194                 }
00195                 else
00196                 {
00197                     /* v */
00198                     sscanf(buf, "%d", &v1);
00199                     fscanf(file, "%d", &v2);
00200                     fscanf(file, "%d", &v3);
00201                     
00202                     // compute the normal
00203                     /* Triangle *t = new Triangle(vertices[v1 - 1], 
00204                                                vertices[v2 - 1], 
00205                                                vertices[v3 - 1], 
00206                                                velocityVertices[v1 - 1],
00207                                                velocityVertices[v2 - 1],
00208                                                velocityVertices[v3 - 1],
00209                                                this);
00210                     elems.push_back(t); */
00211                     addTriangle(v1 - 1, v2 - 1, v3 -1);
00212                 }
00213                 break;
00214 
00215             default:
00216                 /* eat up rest of line */
00217                 fgets(buf, sizeof(buf), file);
00218                 break;
00219         }
00220     }
00221 
00222     if (normals.empty())
00223     {
00224         // compute the normal for the triangles
00225 /*      std::vector<CollisionElement*>::iterator it = elems.begin()
00226         std::vector<CollisionElement*>::iterator itEnd = elems.end();
00227         
00228         for (; it != itEnd; it++)
00229         {
00230             Triangle *t = static_cast<Triangle*> (*it);
00231             Vector3 u,v;
00232             u = *(t->p2) - *(t->p1);
00233             v = *(t->p3) - *(t->p1);
00234 
00235             Vector3 uCrossV = u.Cross(v);
00236 
00237             t->normal = uCrossV;
00238         }*/
00239     }
00240 }
00241 
00242 } // namespace io
00243 
00244 } // namespace helper
00245 
00246 } // namespace sofa
00247 

Generated on Sun Nov 23 02:29:30 2008 for Sofa by  doxygen 1.5.7.1