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

PLYLoader.cpp

00001 /*************************************************************************** 00002 * This file is part of OpenCAL: Open Computer Animation Library * 00003 * I created OpenCAL as my master's thesis Computer Science (multimedia) * 00004 * at the tUL university in Diepenbeek, Belgium * 00005 * * 00006 * Copyright (C) 2003-2004 by Jeroen Dierckx * 00007 * jeroen.dierckx@student.luc.ac.be * 00008 * * 00009 ***************************************************************************/ 00010 00011 // Includes 00012 #include "PLYLoader.h" 00013 #include <OpenCAL/TriangleMesh.h> 00014 #include <OpenCAL/Vertex.h> 00015 #include <OpenCAL/Triangle.h> 00016 using namespace OpenCAL; 00017 00018 #include <OpenCAL/Vector3.h> 00019 using namespace Utils; 00020 00021 #include <fstream> 00022 using namespace std; 00023 00024 00025 /****************************** 00026 * Constructors and destructor * 00027 ******************************/ 00028 00029 PLYLoader::PLYLoader() 00030 { 00031 } 00032 00033 PLYLoader::~PLYLoader() 00034 { 00035 } 00036 00037 00038 /************************ 00039 * Get and set functions * 00040 ************************/ 00041 00042 00043 /****************** 00044 * Other functions * 00045 ******************/ 00046 00047 void PLYLoader::loadMesh(TriangleMesh *mesh, const string &filename) 00048 { 00049 const float scale = 10.0f; 00050 00051 cout << "Let's parse this PLY file (" << filename << ")" << endl; 00052 00053 // Clear the triangle mesh 00054 mesh->clear(); 00055 00056 // Open the file 00057 ifstream file(filename.c_str()); 00058 if(!file) 00059 { 00061 cerr << "The file couldn't be opened" << endl; 00062 return; 00063 } 00064 00065 // Temp variables 00066 char buff[256]; 00067 string line; 00068 unsigned int i; 00069 00070 unsigned int numVertices = 0; 00071 unsigned int numFaces = 0; 00072 00073 00074 /********* 00075 * Header * 00076 *********/ 00077 00078 // The file shouldn't be empty 00079 if(file.eof()) return; 00080 00081 // The first line of the file should be "ply" 00082 file.getline(buff, 256); 00083 line = buff; 00084 if(line != "ply") return; 00085 00086 while(!file.eof() && line != "end_header") 00087 { 00088 file.getline(buff, 256); 00089 line = buff; 00090 00091 if(startsWith(line, "element vertex")) 00092 sscanf(buff, "element vertex %d", &numVertices); 00093 else if(startsWith(line, "element face")) 00094 sscanf(buff, "element face %d", &numFaces); 00095 } 00096 00097 // Prepare the mesh 00098 mesh->reserveVertices(numVertices); 00099 mesh->reserveTriangles(numFaces); 00100 00101 00102 /*********** 00103 * Vertices * 00104 ***********/ 00105 00106 float x, y, z; 00107 00108 for(i = 0; i < numVertices; ++i) 00109 { 00110 file.getline(buff, 256); 00111 00112 sscanf(buff, "%f %f %f", &x, &y, &z); 00113 mesh->addVertex(Vertex(Vector3(x, y, z) * scale)); 00114 } 00115 00116 00117 /************ 00118 * Triangles * 00119 ************/ 00120 00121 Material *green = mesh->addMaterial(); 00122 green->setAmbient(Color(0.1f, 0.6f, 0.08f)); 00123 green->setDiffuse(Color(0.08f, 0.65f, 0.1f)); 00124 green->setSpecular(Color(0.04f, 0.3f, 0.05f)); 00125 green->setShininess(30.0f); 00126 00127 00128 int dummy; 00129 unsigned int a, b, c; 00130 Vertex *v1, *v2, *v3; 00131 00132 for(i = 0; i < numFaces; ++i) 00133 { 00134 file.getline(buff, 256); 00135 00136 sscanf(buff, "%d %d %d %d", &dummy, &a, &c, &b); 00137 00138 v1 = mesh->getVertex(a); 00139 v2 = mesh->getVertex(b); 00140 v3 = mesh->getVertex(c); 00141 Triangle *triangle = mesh->addTriangle(Triangle(v1, v2, v3)); 00142 triangle->setMaterial(green); 00143 triangle->calculateNormal(); 00144 } 00145 00146 00147 // Calculate the vertex normals 00148 mesh->calculateVertexNormals(); 00149 00150 cout << "Done parsing " << mesh->numVertices() << " vertices and " << mesh->numTriangles() << " triangles." << endl; 00151 } 00152 00153 00154 /********************** 00155 * Protected functions * 00156 **********************/

Generated on Sun Aug 15 19:19:23 2004 for OpenCAL: Open Computer Animation Library by doxygen 1.3.8