Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/mesh.h
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/mesh.h	(revision 2194)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/include/mesh.h	(revision 2323)
@@ -47,4 +47,8 @@
 extern double computeTriangleVolume(Vertex *vertices, Triangle *t);
 
+extern int addEdge(Mesh *mesh, int u, int v);
+extern int addTriangle(Mesh *mesh, int curGroup, int v1, int v2, int v3);
+extern int addVertex(Mesh *mesh, float x, float y, float z);
+
 extern Vertex *addVertex(Vertex *list, int *n, float x, float y, float z, int *pos);
 extern int findEdge(Edge *e, int num, int _u, int _v);
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/saliency.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/saliency.cpp	(revision 2127)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/saliency.cpp	(revision 2323)
@@ -58,50 +58,27 @@
 
 double VMI::computeTriangleSaliency(Mesh *mesh, int **histogram, int numCameras, int k) {
-    int i, l, v0, v1, v2;
-    int *triangles = NULL, n = 0;
+    int i, j, t, v0;
+    int n = 0;
     double sal = 0.0;
     
-    v0 = mesh->triangles[k].indices[0];
-    v1 = mesh->triangles[k].indices[1];
-    v2 = mesh->triangles[k].indices[2];
-    
-    /* Allocating memory */
-    triangles = (int *)malloc((mesh->vertices[v0].numTriangles +
-                               mesh->vertices[v1].numTriangles +
-                               mesh->vertices[v2].numTriangles) * sizeof(int));
-
-    for(i=0; i<mesh->vertices[v0].numTriangles; i++) {
-        l = mesh->vertices[v0].triangles[i];
+    for (j=0; j<3; j++) {
         
-        if (l != k) 
-            addItem(triangles, &n, l);
-    }
-    
-    for(i=0; i<mesh->vertices[v1].numTriangles; i++) {
-        l = mesh->vertices[v1].triangles[i];
+        v0 = mesh->triangles[k].indices[j];
         
-        if (l != k) 
-            addItem(triangles, &n, l);
-    }
-    
-    for(i=0; i<mesh->vertices[v2].numTriangles; i++) {
-        l = mesh->vertices[v2].triangles[i];
-        
-        if (l != k) 
-            addItem(triangles, &n, l);
-    }
-    
-    //printItemList(triangles, n);
-    
-    for(i=0; i<n; i++) {
-        //printf("\n%d %d", k, triangles[i]);
-        //sal += computeJS(histogram, numCameras, k, triangles[i]);
-		sal = MAX ( sal, computeJS(histogram, numCameras, k, triangles[i]));
+        for(i=0; i<mesh->vertices[v0].numTriangles; i++) {
+            t = mesh->vertices[v0].triangles[i];
+            
+            if (t != k) {
+                //printf("\n%d %d", k, triangles[i]);
+                //sal += computeJS(histogram, numCameras, k, t);
+                sal = MAX(sal, computeJS(histogram, numCameras, k, t));
+                
+                n++;
+            }
+        }
     }
     //printf("\nT%d Sal: %f\n", k, (sal / n));
     //getchar();
     
-    free(triangles);
-
     return (sal /*/ n*/);
 }
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/mesh.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/mesh.cpp	(revision 2194)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/mesh.cpp	(revision 2323)
@@ -212,180 +212,213 @@
     t->normal[2] = cpz / r;
 }
+int VMI::addEdge(Mesh *mesh, int u, int v) {
+	int n = mesh->numEdges;
+	
+	mesh->edges[n].u = u;
+	mesh->edges[n].v = v;
+	mesh->edges[n].enable = TRUE;
+	
+	mesh->vertices[u].edges = 
+		(int *)realloc(mesh->vertices[u].edges, (mesh->vertices[u].numEdges + 1) * sizeof(int));
+	// Adding edge n adjacent to vertex u
+	addItem(mesh->vertices[u].edges, &mesh->vertices[u].numEdges, n);
+	
+	mesh->vertices[v].edges = 
+		(int *)realloc(mesh->vertices[v].edges, (mesh->vertices[v].numEdges + 1) * sizeof(int));
+	// Adding edge n adjacent to vertex v
+	addItem(mesh->vertices[v].edges, &mesh->vertices[v].numEdges, n);
+	
+	mesh->numEdges++;
+	
+	return n;
+}
+
+int VMI::addVertex(Mesh *mesh, float x, float y, float z) {
+	int n = mesh->numVertices;
+
+	mesh->vertices[n].x = x;
+	mesh->vertices[n].y = y;
+	mesh->vertices[n].z = z;
+	mesh->vertices[n].numTriangles = 0;
+	mesh->vertices[n].triangles = NULL;
+	mesh->vertices[n].numEdges = 0;
+	mesh->vertices[n].edges = NULL;
+	mesh->vertices[n].enable = TRUE;
+
+	mesh->numVertices++;
+
+	return n;
+}
+
+int VMI::addTriangle(Mesh *mesh, int curGroup, int v1, int v2, int v3) {
+	int j, n = mesh->numTriangles;
+
+	mesh->triangles[n].id = n;
+	mesh->triangles[n].group = curGroup; // set group
+	mesh->triangles[n].submesh = curGroup; // set group
+	mesh->triangles[n].indices[0] = v1;
+	mesh->triangles[n].indices[1] = v2;
+	mesh->triangles[n].indices[2] = v3;
+	mesh->triangles[n].area = computeTriangleArea(mesh->vertices, &mesh->triangles[n]);
+	//printf("\n%d a: %f",i , mesh->triangles[i].area);
+	computeTriangleNormal(mesh->vertices, &mesh->triangles[n]);
+	mesh->triangles[n].saliency = 0.0;
+	mesh->triangles[n].enable = TRUE;
+
+	for (j=0; j<3; j++) {
+		// Adding triangle n adjacent to 3 vertices
+		v1 = mesh->triangles[n].indices[j];
+
+		// Reallocate memory for the new adjacent triangle
+		mesh->vertices[v1].triangles = 
+			(int *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(int));
+		addItem(mesh->vertices[v1].triangles, &mesh->vertices[v1].numTriangles, n);
+	}
+
+	mesh->numTriangles++;
+
+	return n;
+}
 
 Mesh *VMI::initMesh(GLMmodel* pmodel) {
-
-    int i, j, curGroup, v1, v2, v3, n, m, t;
-    int e;
-    Mesh *mesh;
-    GLMgroup *group;
-
-    mesh = (Mesh *)malloc (sizeof(Mesh));
-    
-    if (mesh == NULL) {
-        fprintf(stderr, "Error allocating memory\n");
-        exit(1);
-    }
-
-    mesh->vertices = (Vertex *)malloc (sizeof(Vertex) * pmodel->numvertices);
-    
-    if (mesh->vertices == NULL) {
-        fprintf(stderr, "Error allocating memory\n");
-        exit(1);
-    }
-
-    mesh->triangles = (Triangle *)malloc (sizeof(Triangle) * pmodel->numtriangles);
-    
-    if (mesh->triangles == NULL) {
-        fprintf(stderr, "Error allocating memory\n");
-        exit(1);
-    }
+	int i, e;
+	int v1, v2, v3;
+	float x, y, z;
+	//int *vlist = NULL, j, num;
+	Mesh *mesh;
+#ifdef OUTPUT_SEVERAL_GROUPS
+		int t;
+		int curGroup;
+		GLMgroup *group;
+#endif
+
+		mesh = (Mesh *)malloc (sizeof(Mesh));
+
+		if (mesh == NULL) {
+			fprintf(stderr, "Error allocating memory\n");
+			exit(1);
+		}
+
+		mesh->vertices = (Vertex *)malloc (sizeof(Vertex) * pmodel->numvertices);
+		mesh->numVertices = 0;
+
+		if (mesh->vertices == NULL) {
+			fprintf(stderr, "Error allocating memory\n");
+			exit(1);
+		}
+
+		mesh->triangles = (Triangle *)malloc (sizeof(Triangle) * pmodel->numtriangles);
+		mesh->numTriangles = 0;
+
+		if (mesh->triangles == NULL) {
+			fprintf(stderr, "Error allocating memory\n");
+			exit(1);
+		}
     
     printf("Adding vertices...");
-    for (i=1; i<=(int)pmodel->numvertices; i++) { // Vertices start at 1
-        mesh->vertices[i - 1].x = pmodel->vertices[3 * i + 0];
-        mesh->vertices[i - 1].y = pmodel->vertices[3 * i + 1];
-        mesh->vertices[i - 1].z = pmodel->vertices[3 * i + 2];
-        mesh->vertices[i - 1].numTriangles = 0;
-        mesh->vertices[i - 1].triangles = NULL;
-				mesh->vertices[i - 1].numEdges = 0;
-        mesh->vertices[i - 1].edges = NULL;
-        mesh->vertices[i - 1].enable = GL_TRUE;
-    }
+		for (i=1; i<=(int)pmodel->numvertices; i++) { // Vertices start at 1
+
+			x = pmodel->vertices[3 * i + 0];
+			y = pmodel->vertices[3 * i + 1];
+			z = pmodel->vertices[3 * i + 2];
+
+			addVertex(mesh, x, y, z);
+		}
     printf("Ok\n");
-    mesh->numVertices = pmodel->numvertices;
-    mesh->currentNumVertices = pmodel->numvertices;
+    mesh->currentNumVertices = mesh->numVertices;
+    printf("Number of vertices: %d\n", mesh->numVertices);
 
     printf("Adding triangles...");
-
 #ifdef OUTPUT_SEVERAL_GROUPS
     group = pmodel->groups;
     curGroup=0; // current group
-    while(group) {
-        
-        for (i=0; i<(int)group->numtriangles; i++) {
-            
-            t = group->triangles[i];
-
-            mesh->triangles[t].id = t;
-            mesh->triangles[t].group = curGroup; // set group
-            mesh->triangles[t].indices[0] = pmodel->triangles[t].vindices[0] - 1;
-            mesh->triangles[t].indices[1] = pmodel->triangles[t].vindices[1] - 1;
-            mesh->triangles[t].indices[2] = pmodel->triangles[t].vindices[2] - 1;
-            mesh->triangles[t].area = computeTriangleArea(mesh->vertices, &mesh->triangles[t]);
-            //printf("\n%d a: %f",i , mesh->triangles[i].area);
-            computeTriangleNormal(mesh->vertices, &mesh->triangles[t]);
-            
-            mesh->triangles[t].saliency = 0.0;
-            
-            mesh->triangles[t].enable = GL_TRUE;
-            
-            for (j=0; j<3; j++) {
-                // Adding triangle i adjacent to 3 vertices
-                v1 = mesh->triangles[t].indices[j];
-                
-                // Reallocate memory for the new adjacent triangle
-                mesh->vertices[v1].triangles = 
-					(int *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(int));
-                addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, t);
-            }
-        }
-        curGroup++;
-        group = group->next;
-    }
+		while(group) {
+
+			for (i=0; i<(int)group->numtriangles; i++) {
+
+				t = group->triangles[i];
+
+				v1 = pmodel->triangles[t].vindices[0] - 1;
+				v2 = pmodel->triangles[t].vindices[1] - 1;
+				v3 = pmodel->triangles[t].vindices[2] - 1;
+
+				// not a degenerated triangle
+				if ((v1 != v2) && (v2 != v3) && (v1 != v3)) {
+
+					addTriangle(mesh, curGroup, v1, v2, v3);
+				}
+			}
+			curGroup++;
+			group = group->next;
+		}
 #else // One single group
-    for (i=0; i<pmodel->numtriangles; i++) {
-        mesh->triangles[i].id = i;
-        mesh->triangles[i].indices[0] = (int)pmodel->triangles[i].vindices[0] - 1;
-        mesh->triangles[i].indices[1] = (int)pmodel->triangles[i].vindices[1] - 1;
-        mesh->triangles[i].indices[2] = (int)pmodel->triangles[i].vindices[2] - 1;
-        mesh->triangles[i].area = computeTriangleArea(mesh->vertices, &mesh->triangles[i]);
-        //printf("\n%d a: %f",i , mesh->triangles[i].area);
-        computeTriangleNormal(mesh->vertices, &mesh->triangles[i]);
-
-        mesh->triangles[i].saliency = 0.0;
-
-        mesh->triangles[i].enable = GL_TRUE;
-        
-        for (j=0; j<3; j++) {
-            // Adding triangle i adjacent to 3 vertices
-            v1 = mesh->triangles[i].indices[j];
-   
-            // Reallocate memory for the new adjacent triangle
-            mesh->vertices[v1].triangles = 
-			    (int *)realloc(mesh->vertices[v1].triangles, (mesh->vertices[v1].numTriangles + 1) * sizeof(int));
-            addItem((int *)mesh->vertices[v1].triangles, (int *)&mesh->vertices[v1].numTriangles, i);
-        }
-    }
+		for (i=0; i<(int)pmodel->numtriangles; i++) {
+
+			v1 = pmodel->triangles[i].vindices[0] - 1;
+			v2 = pmodel->triangles[i].vindices[1] - 1;
+			v3 = pmodel->triangles[i].vindices[2] - 1;
+
+			// not a degenerated triangle
+			if ((v1 != v2) && (v2 != v3) && (v1 != v3)) {
+
+				addTriangle(mesh, 1, v1, v2, v3);
+			}
+		}
     printf("\n");
 #endif
+		printf("Ok\n");
+		mesh->currentNumTriangles = mesh->numTriangles;
+		printf("Number of triangles: %d\n", mesh->numTriangles);
+		printf("Number of degenerated triangles: %d\n", pmodel->numtriangles - mesh->numTriangles);
+
+		mesh->edges = (Edge *)malloc (sizeof(Edge) * mesh->numTriangles * 3);  // E = 3 T / 2
+		mesh->numEdges = 0;
+
+		if (mesh->edges == NULL) {
+			fprintf(stderr, "Error allocating memory\n");
+			exit(1);
+		}
+
+    printf("Adding edges...");
+
+    /*for (i=0; i<mesh->numVertices; i++) {
+        vlist = verticesAdjToVertex(mesh, i, &num);
+        
+        for (j=0; j<num; j++) {
+            
+            if  (i < vlist[j]) {
+				
+				addEdge(mesh, i, vlist[j]);
+            }
+        }
+        free(vlist);
+    }*/
+
+		for (i=0; i<mesh->numTriangles; i++) {
+
+			v1 = mesh->triangles[i].indices[0];
+			v2 = mesh->triangles[i].indices[1];
+			v3 = mesh->triangles[i].indices[2];
+
+			/////////////////////////////////////////////////////////////////////////////////
+			// edge (v1,v2)
+			if ((e = findEdge(mesh->edges, mesh->numEdges, v1, v2)) == -1) {
+
+				addEdge(mesh, v1, v2);
+			}
+			/////////////////////////////////////////////////////////////////////////////////
+			// edge (v2,v3)
+			if ((e = findEdge(mesh->edges, mesh->numEdges, v2, v3)) == -1) {
+
+				addEdge(mesh, v2, v3);
+			}
+			/////////////////////////////////////////////////////////////////////////////////
+			// edge (v3,v1)
+			if ((e = findEdge(mesh->edges, mesh->numEdges, v3, v1)) == -1) {
+
+				addEdge(mesh, v3, v1);
+			}
+		}
     printf("Ok\n");
-
-    mesh->numTriangles = pmodel->numtriangles;
-    mesh->currentNumTriangles = pmodel->numtriangles;
-    printf("Number of triangles: %d\n", mesh->numTriangles);
-
-    mesh->edges = (Edge *)malloc (sizeof(Edge) * mesh->numTriangles * 3);  // E = 3 T / 2
-    
-    if (mesh->edges == NULL) {
-        fprintf(stderr, "Error allocating memory\n");
-        exit(1);
-    }
-    
-    printf("Adding edges...");
-    n = 0;
-
-    for (i=0; i<mesh->numTriangles; i++) {
-        t = 0;
-        v1 = mesh->triangles[i].indices[0];
-        v2 = mesh->triangles[i].indices[1];
-        v3 = mesh->triangles[i].indices[2];
-        
-        /////////////////////////////////////////////////////////////////////////////////
-		// edge (v1,v2)
-        if ((e = findEdge(mesh->edges, n, v1, v2)) == -1) {
-            mesh->edges[n].u = v1;
-            mesh->edges[n].v = v2;
-            mesh->edges[n].enable = GL_TRUE;
-            m = n;
-            n++;
-        } else m = e;
-
-        /////////////////////////////////////////////////////////////////////////////////
-        // edge (v2,v3)
-        if (( e = findEdge(mesh->edges, n, v2, v3)) == -1) {
-            mesh->edges[n].u = v2;
-            mesh->edges[n].v = v3;
-            mesh->edges[n].enable = GL_TRUE;
-            m = n;
-            n++;
-        } else m = e;
-
-        /////////////////////////////////////////////////////////////////////////////////
-        // edge (v3,v1)
-        if ((e = findEdge(mesh->edges, n, v3, v1)) == -1) {
-            mesh->edges[n].u = v3;
-            mesh->edges[n].v = v1;
-            mesh->edges[n].enable = GL_TRUE;
-            m = n;
-            n++;
-        } else m = e;
-    }
-    printf("Ok\n");
-    mesh->numEdges = n;
-
-		for (i=0; i<mesh->numEdges; i++) {
-			v1 = mesh->edges[i].u;
-			v2 = mesh->edges[i].v;
-
-			mesh->vertices[v1].edges = 
-				(int *)realloc(mesh->vertices[v1].edges, (mesh->vertices[v1].numEdges + 1) * sizeof(int));
-			// Adding edge i adjacent to vertex v1
-			addItem(mesh->vertices[v1].edges, &mesh->vertices[v1].numEdges, i);
-
-			mesh->vertices[v2].edges = 
-				(int *)realloc(mesh->vertices[v2].edges, (mesh->vertices[v2].numEdges + 1) * sizeof(int));
-			// Adding edge i adjacent to vertex v2
-			addItem(mesh->vertices[v2].edges, &mesh->vertices[v2].numEdges, i);
-		}
 
     return mesh;
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp	(revision 2300)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoMeshSimplifier.cpp	(revision 2323)
@@ -55,11 +55,4 @@
 	for	(size_t	i	=	0; i < geoMesh->mSubMeshCount;	i++)
 	{
-		//	Debug.
-		cout	<<	"Indices of submesh "
-					<<	i
-					<<	": "
-					<<	geoMesh->mSubMesh[i].mIndexCount
-					<<	endl;
-
 		if ((geoMesh->mSubMesh[i].mIndexCount > 0)
 				||
@@ -69,7 +62,4 @@
 		}
 	}
-
-	//	Reassign submesh count.
-	geoMesh->mSubMeshCount	=	valid_submesh_count;
 
 	//	Reserve memory for valid submeshes.
@@ -171,5 +161,6 @@
 	delete	[]geoMesh->mSubMesh;
 
-	geoMesh->mSubMesh	=	geosubmesh;
+	geoMesh->mSubMeshCount	=	valid_submesh_count;
+	geoMesh->mSubMesh				=	geosubmesh;
 }
 
@@ -377,5 +368,7 @@
 }
 
+//------------------------------------------------------------------------
 //	Init VMI options.
+//------------------------------------------------------------------------
 int	ViewPointDrivenSimplifier::init(void)
 {
@@ -394,5 +387,5 @@
 
 	if (VMI::bLoadCamerasFromFile == GL_FALSE)
-		VMI::cameras = VMI::setCameras(	VMI::radius, 
+		VMI::cameras = VMI::setCameras(	VMI::radius,
 																		VMI::cameraType,
 																		&VMI::numCameras);
@@ -535,10 +528,10 @@
 VMI::Mesh *	ViewPointDrivenSimplifier::initMeshStructure(Mesh	*geoMesh)
 {
-	GLuint				i, j, v1, v2, v3, n, e, t;
+	GLuint				i, j, v1, v2, v3, e;
+	float x, y, z;
 	VMI::Mesh 		*vmi_mesh;
 	SubMesh				*geosubmesh;
 	VertexBuffer	*vertex_buffer;
 	int						mesh_index_count;
-	int						index;
 
 	//	Reallocate memory for vmi mesh object.
@@ -558,4 +551,5 @@
 																				*
 																				vertex_buffer->mVertexCount);
+	vmi_mesh->numVertices = 0;
 
 	if (vmi_mesh->vertices == NULL)
@@ -579,4 +573,5 @@
 																						*
 																						(mesh_index_count / 3));
+	vmi_mesh->numTriangles = 0;
 
 	if (vmi_mesh->triangles == NULL)
@@ -592,18 +587,13 @@
 	{
 		// Vertices start at 0.
-		vmi_mesh->vertices[i].x = vertex_buffer->mPosition[i].x;
-		vmi_mesh->vertices[i].y = vertex_buffer->mPosition[i].y;
-		vmi_mesh->vertices[i].z = vertex_buffer->mPosition[i].z;
-
-		vmi_mesh->vertices[i].numTriangles	= 0;
-		vmi_mesh->vertices[i].triangles			= NULL;
-		vmi_mesh->vertices[i].numEdges			= 0;
-		vmi_mesh->vertices[i].edges					= NULL;
-		vmi_mesh->vertices[i].enable				= GL_TRUE;
+		x = vertex_buffer->mPosition[i].x;
+		y = vertex_buffer->mPosition[i].y;
+		z = vertex_buffer->mPosition[i].z;
+
+		addVertex(vmi_mesh, x, y, z);
 	}
 	
 	printf("Ok\n");
-	vmi_mesh->numVertices = int(vertex_buffer->mVertexCount);
-	vmi_mesh->currentNumVertices = int(vertex_buffer->mVertexCount);
+	vmi_mesh->currentNumVertices = vmi_mesh->numVertices;
 
 	printf("Vertices: %d\n",vmi_mesh->numVertices);
@@ -611,7 +601,4 @@
 	//	Fill up triangles.
 	printf("Adding triangles...");
-	
-	//	Initialize index of triangle.
-	index	=	0;
 	
 	//	For each submesh.
@@ -626,40 +613,13 @@
 		for (i = 0; i < (geosubmesh->mIndexCount / 3); i++)
 		{
-			vmi_mesh->triangles[index].id 				= index;
-			vmi_mesh->triangles[index].submesh		= submesh;
-			vmi_mesh->triangles[index].indices[0] = geosubmesh->mIndex[(3 * i)];
-			vmi_mesh->triangles[index].indices[1] = geosubmesh->mIndex[(3 * i) + 1];
-			vmi_mesh->triangles[index].indices[2] = geosubmesh->mIndex[(3 * i) + 2];
-
-			vmi_mesh->triangles[index].area = computeTriangleArea(
-																						vmi_mesh->vertices, 
-																						&vmi_mesh->triangles[index]);
-
-			computeTriangleNormal(vmi_mesh->vertices,
-														&vmi_mesh->triangles[index]);
-
-			vmi_mesh->triangles[index].saliency = 0.0;
-
-			vmi_mesh->triangles[index].enable = GL_TRUE;
-
-			for (j = 0; j < 3; j++)
-			{
-				// Adding triangle index adjacent to 3 vertices.
-				v1 = vmi_mesh->triangles[index].indices[j];
-
-				// Reallocate memory for the new adjacent triangle.
-				vmi_mesh->vertices[v1].triangles = 
-											(int *)realloc(vmi_mesh->vertices[v1].triangles,
-											(vmi_mesh->vertices[v1].numTriangles + 1)
-											*
-											sizeof(int));
-
-				VMI::addItem(	(int *)vmi_mesh->vertices[v1].triangles,
-											(int *)&vmi_mesh->vertices[v1].numTriangles,
-											index);
-			}
-
-			//	Increments triangle count.
-			index++;
+			v1 = geosubmesh->mIndex[(3 * i)];
+			v2 = geosubmesh->mIndex[(3 * i) + 1];
+			v3 = geosubmesh->mIndex[(3 * i) + 2];
+
+			// not a degenerated triangle
+			//if ((v1 != v2) && (v2 != v3) && (v1 != v3)) {
+
+				addTriangle(vmi_mesh, submesh, v1, v2, v3);
+			//}
 		}
 	}
@@ -667,8 +627,7 @@
 	printf("Ok\n");
 
-	vmi_mesh->numTriangles 				= mesh_index_count / 3;
-	vmi_mesh->currentNumTriangles = mesh_index_count / 3;
-
-	printf("Num Triangles: %d\n",vmi_mesh->numTriangles);
+	vmi_mesh->currentNumTriangles = vmi_mesh->numTriangles;
+	printf("Number of triangles: %d\n", vmi_mesh->numTriangles);
+	//printf("Number of degenerated triangles: %d\n", pmodel->numtriangles - vmi_mesh->numTriangles);
 	
 	// E = 3 T / 2
@@ -676,4 +635,5 @@
 										*
 										vmi_mesh->numTriangles * 3);
+	vmi_mesh->numEdges = 0;
 
 	if (vmi_mesh->edges == NULL)
@@ -684,68 +644,29 @@
 
 	printf("Adding edges...");
-	n = 0;
 
 	//	For each triangle adds three edges.
 	for (i	=	0; i < vmi_mesh->numTriangles; i++)
 	{
-		t		=	0;
 		v1	=	vmi_mesh->triangles[i].indices[0];
 		v2	=	vmi_mesh->triangles[i].indices[1];
 		v3	=	vmi_mesh->triangles[i].indices[2];
 
-		if ((e = findEdge(vmi_mesh->edges, n, v1, v2)) == -1)
-		{
-			vmi_mesh->edges[n].u						=	v1;
-			vmi_mesh->edges[n].v						=	v2;
-			vmi_mesh->edges[n].enable 			=	GL_TRUE;
-
-			n++;
-		}
-
-		if ((e = findEdge(vmi_mesh->edges, n, v2, v3)) == -1)
-		{
-			vmi_mesh->edges[n].u 						= v2;
-			vmi_mesh->edges[n].v 						= v3;
-			vmi_mesh->edges[n].enable 			= GL_TRUE;
-
-			n++;
+		if ((e = findEdge(vmi_mesh->edges, vmi_mesh->numEdges, v1, v2)) == -1)
+		{
+			addEdge(vmi_mesh, v1, v2);
+		}
+
+		if ((e = findEdge(vmi_mesh->edges, vmi_mesh->numEdges, v2, v3)) == -1)
+		{
+			addEdge(vmi_mesh, v2, v3);
 		}
 		
-		if ((e = findEdge(vmi_mesh->edges, n, v3, v1)) == -1)
-		{
-			vmi_mesh->edges[n].u 						= v3;
-			vmi_mesh->edges[n].v 						= v1;
-			vmi_mesh->edges[n].enable 			= GL_TRUE;
-
-			n++;
+		if ((e = findEdge(vmi_mesh->edges, vmi_mesh->numEdges, v3, v1)) == -1)
+		{
+			addEdge(vmi_mesh, v3, v1);
 		}
 	}
 	
 	printf("Ok\n");
-	vmi_mesh->numEdges = n;
-
-	for (i	=	0; i < vmi_mesh->numEdges; i++)
-	{
-		v1 = vmi_mesh->edges[i].u;
-		v2 = vmi_mesh->edges[i].v;
-
-		vmi_mesh->vertices[v1].edges	=
-							(int *)realloc(vmi_mesh->vertices[v1].edges,
-							(vmi_mesh->vertices[v1].numEdges + 1) * sizeof(int));
-
-		// Adding edge i adjacent to vertex v1
-		VMI::addItem(	vmi_mesh->vertices[v1].edges,
-									&vmi_mesh->vertices[v1].numEdges,
-									i);
-
-		vmi_mesh->vertices[v2].edges =
-								(int *)realloc(vmi_mesh->vertices[v2].edges,
-								(vmi_mesh->vertices[v2].numEdges + 1) * sizeof(int));
-
-		// Adding edge i adjacent to vertex v2
-		VMI::addItem(	vmi_mesh->vertices[v2].edges,
-									&vmi_mesh->vertices[v2].numEdges,
-									i);
-	}
 
 	//	Creates vertex multimap.
@@ -942,4 +863,5 @@
 
 			mGeoMesh->mBones.push_back(bone);
+
 			vertex_found	=	true;
 		}
Index: GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshViewUI.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshViewUI.cpp	(revision 2300)
+++ GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshViewUI.cpp	(revision 2323)
@@ -413,5 +413,5 @@
 //-------------------------------------------------------------------------
 inline void GeoMeshViewUI::cb_menuEditPan_i(fltk::Item	*item, void*)
-{	
+{
 	//	If the item is activated.
 	if (item->value())
@@ -496,5 +496,5 @@
 		geoMeshView->deactiveSolid();
 	}
-									
+
 	//	Repaint the canvas.
 	//geoMeshView->redraw();
@@ -790,5 +790,6 @@
 //	Leaves collapse Callback
 //-------------------------------------------------------------------------
-inline void GeoMeshViewUI::cb_menuSimplifyLeavesCollapse_i(fltk::Item*, void*)
+inline void GeoMeshViewUI::cb_menuSimplifyLeavesCollapse_i(	fltk::Item	*,
+																														void				*)
 {
 	//	Show title.
@@ -896,5 +897,4 @@
 										cb_menuLodTreesGenerate_i(o,v);
 }
-
 
 //-------------------------------------------------------------------------
@@ -1038,6 +1038,4 @@
 }
 
-
-
 //-------------------------------------------------------------------------
 //	About Callback
@@ -1103,5 +1101,5 @@
 				}
 				
-				//	if the mesh is stripified.
+				//	If the mesh is stripified.
 				if (error)
 				{
@@ -1119,5 +1117,5 @@
 					mGeoMesh	=	mesh_aux;
 
-					// Visualize mesh.
+					//	Visualize mesh.
 					geoMeshView->setMesh(mGeoMesh);
 
@@ -1128,5 +1126,5 @@
 					}
 
-					// Visualize mesh.
+					//	Visualize mesh.
 					geoMeshView->setMesh(mGeoMesh);
 
@@ -1134,5 +1132,5 @@
 				break;
 
-				// Simplify leaves
+				//	Simplify leaves
 			case	LEAVES_COLLAPSE:
 
@@ -1347,5 +1345,4 @@
 					delete mUndoMesh;
 
-
 					mUndoMesh	=	new Mesh();
 
@@ -1617,7 +1614,7 @@
 }
 
-//---------------------------------------------------------------------------
+//-------------------------------------------------------------------------
 //	Show the Simlify Leaves Collapse.
-//---------------------------------------------------------------------------
+//-------------------------------------------------------------------------
 void	GeoMeshViewUI::showLeavesCollapse()
 {
@@ -2021,5 +2018,5 @@
 		//	If the mesh does not have shared vertex.
 		if (!geoSubMesh->mSharedVertexBuffer)
-		{			
+		{
 			//	Adds the vertex of the submesh to the total count.
 			vertex_count	+=	geoSubMesh->mVertexBuffer->mVertexCount;
@@ -2089,5 +2086,5 @@
 		{
 			strip_count	+=	geoSubMesh->mStripCount;
-		}	
+		}
 	}
 
@@ -2351,5 +2348,5 @@
 	tree_sequencer	=	mTreeSimplifier->GetSimplificationSequence();
 	
-	tree_sequencer->putMeshName(nombremesh); 	
+	tree_sequencer->putMeshName(nombremesh);
 	tree_sequencer->Save(Serializer(filename,Serializer::APPEND));
 
@@ -2483,5 +2480,7 @@
 }
 
+//-------------------------------------------------------------------------
 //	Method that updates the porgress bar.
+//-------------------------------------------------------------------------
 float GeoMeshViewUI::updateProgressBar(float v)
 {
@@ -2538,5 +2537,8 @@
 //	Initialize the lodTreelibrary for visualization.
 //-------------------------------------------------------------------------
-void	GeoMeshViewUI::setLodTreesLibrary(const LodStripsLibraryData *lodstripsdata, const TreeSimplificationSequence *treesimpseq, Mesh *geomesh/*, uint32 ileafSubMesh*/)
+void	GeoMeshViewUI::setLodTreesLibrary(
+													const LodStripsLibraryData 				*lodstripsdata,
+													const TreeSimplificationSequence	*treesimpseq,
+													Mesh 															*geomesh)
 {
 	//	If there is no lod strips object.
@@ -2548,7 +2550,10 @@
 
 	//	New lod strips object.
-
 	lod_index_data	=	new GeoToolIndexData(geomesh->mSubMeshCount);
-	lodTreeLib = new Geometry::LodTreeLibrary(lodstripsdata,treesimpseq,geomesh,lod_index_data);
+
+	lodTreeLib	=	new Geometry::LodTreeLibrary(	lodstripsdata,
+																							treesimpseq,
+																							geomesh,
+																							lod_index_data);
 
 	//	Sets the slider range.
@@ -2616,6 +2621,6 @@
 			//	Translate the MB count to a char value.
 			sprintf(char_value,
-					"MB %.3f",
-					(float)geoMeshLoader->getFileSize()/1024/1024);
+							"MB %.3f",
+							(float)geoMeshLoader->getFileSize()/1024/1024);
 
 			mMB->label(char_value);
@@ -2747,7 +2752,7 @@
 	//	Initialize the lod strip object.
 	lodStripsLib	=	NULL;
-	lodTreeLib	=	NULL;
-
-	//	Identify the mesh that stores the leaves
+	lodTreeLib		=	NULL;
+
+	//	Identify the mesh that stores the leaves.
 	idMeshLeaves	=	-1;
 
