Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1524 - in trunk/Model: Groups Readers


Chronological Thread 
  • From: thiago@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1524 - in trunk/Model: Groups Readers
  • Date: Fri, 20 Jul 2007 20:48:37 -0600 (MDT)

Author: thiago
Date: Fri Jul 20 20:48:36 2007
New Revision: 1524

Modified:
   trunk/Model/Groups/Mesh.cc
   trunk/Model/Groups/Mesh.h
   trunk/Model/Groups/ObjGroup.cc
   trunk/Model/Readers/PlyReader.cc
Log:
Mesh can now remove degenerate triangles. The ObjGroup and PlyReader
loaders will call this automatically, so the end user need not do
anything.

This commit might fix problems you are having in your code because
degenerate triangles screw things up all over the place.

 --This line, and those below, will be ignored--

M    Groups/Mesh.cc
M    Groups/Mesh.h
M    Groups/ObjGroup.cc
M    Readers/PlyReader.cc


Modified: trunk/Model/Groups/Mesh.cc
==============================================================================
--- trunk/Model/Groups/Mesh.cc  (original)
+++ trunk/Model/Groups/Mesh.cc  Fri Jul 20 20:48:36 2007
@@ -241,3 +241,51 @@
     vertexNormals[i] = vertexNormals[i].normal();
   }    
 }
+
+
+void Mesh::removeDegenerateTriangles()
+{
+  //return;
+  size_t original_size = vertex_indices.size();
+  for (size_t i=0; i < vertex_indices.size(); i+=3) {
+    const int index0 = vertex_indices[i+0];
+    const int index1 = vertex_indices[i+1];
+    const int index2 = vertex_indices[i+2];
+
+    const Vector& a = vertices[index0];
+    const Vector& b = vertices[index1];
+    const Vector& c = vertices[index2];
+
+    if (a==b || a==c || b==c) {
+      //this triangle is degenerate. Let's remove it.
+      vertex_indices[i+0] = vertex_indices[vertex_indices.size()-3];
+      vertex_indices[i+1] = vertex_indices[vertex_indices.size()-2];
+      vertex_indices[i+2] = vertex_indices[vertex_indices.size()-1];
+      vertex_indices.resize(vertex_indices.size()-3);
+
+      if (!normal_indices.empty()) {
+        normal_indices[i+0] = normal_indices[normal_indices.size()-3];
+        normal_indices[i+1] = normal_indices[normal_indices.size()-2];
+        normal_indices[i+2] = normal_indices[normal_indices.size()-1];
+        normal_indices.resize(normal_indices.size()-3);
+      }
+
+      if (!texture_indices.empty()) {
+        texture_indices[i+0] = texture_indices[texture_indices.size()-3];
+        texture_indices[i+1] = texture_indices[texture_indices.size()-2];
+        texture_indices[i+2] = texture_indices[texture_indices.size()-1];
+        texture_indices.resize(texture_indices.size()-3);
+      }
+
+      const size_t tri_index = i/3;
+      face_material[tri_index] = face_material.back();
+      face_material.pop_back();
+
+      WaldTriangle* tri = static_cast<WaldTriangle*>(objs[tri_index]);
+      tri->attachMesh(this, tri_index);
+      shrinkTo(size()-1, false); //note this might cause a memory leak.
+
+      i-=3;
+    }
+  }
+}

Modified: trunk/Model/Groups/Mesh.h
==============================================================================
--- trunk/Model/Groups/Mesh.h   (original)
+++ trunk/Model/Groups/Mesh.h   Fri Jul 20 20:48:36 2007
@@ -68,6 +68,9 @@
     BBox getBBox(unsigned int which);
  
     virtual void preprocess(const PreprocessContext& context);
+
+    //removes degenerate triangles from mesh.
+    void removeDegenerateTriangles();
   };
 }
 

Modified: trunk/Model/Groups/ObjGroup.cc
==============================================================================
--- trunk/Model/Groups/ObjGroup.cc      (original)
+++ trunk/Model/Groups/ObjGroup.cc      Fri Jul 20 20:48:36 2007
@@ -214,7 +214,6 @@
     int total_faces = group->numtriangles;
     for (int i=0;i<total_faces;++i) {
 
-      bool have_normals = true;
       bool have_textures = true;
       
       for (int v=0;v<3;++v) {
@@ -225,10 +224,18 @@
         if (index > 0) {
           normal_indices.push_back(index-1);
         }
-        else {
-          // If one of the vertices doesn't have texture
-          // coordinates, then all don't.
-          have_normals = false;
+        else if (!vertexNormals.empty()) {
+          //Not sure if this can happen in the obj spec, but just in
+          //case...
+          //we've already added in the vertices several lines
+          //above, so we know enough to calculate a face normal.
+          size_t  vIndex = vertex_indices.size()-3;
+          const Vector &n0 = vertices[vertex_indices[vIndex+0]];
+          const Vector &n1 = vertices[vertex_indices[vIndex+1]];
+          const Vector &n2 = vertices[vertex_indices[vIndex+2]];
+          const Vector normal = Cross(n1-n0, n2-n0);
+          normal_indices.push_back(vertexNormals.size());
+          vertexNormals.push_back(normal);
         }
         
         index = model->triangles[ group->triangles[i] ].tindices[v];
@@ -259,7 +266,7 @@
   }
   
   // std::cerr << "Total triangles added: " << tri << std::endl;
-    
+  removeDegenerateTriangles();
 }
 
  ObjGroup::~ObjGroup() {

Modified: trunk/Model/Readers/PlyReader.cc
==============================================================================
--- trunk/Model/Readers/PlyReader.cc    (original)
+++ trunk/Model/Readers/PlyReader.cc    Fri Jul 20 20:48:36 2007
@@ -224,5 +224,7 @@
      
      ply_close(ply);
 
+     mesh->removeDegenerateTriangles();
+
      return true;
 }




  • [MANTA] r1524 - in trunk/Model: Groups Readers, thiago, 07/20/2007

Archive powered by MHonArc 2.6.16.

Top of page