Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r308 - in trunk: Model/Groups Model/Readers SCIRun/Core scenes


Chronological Thread 
  • From: thiago@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r308 - in trunk: Model/Groups Model/Readers SCIRun/Core scenes
  • Date: Fri, 13 May 2005 00:56:11 -0600 (MDT)

Author: thiago
Date: Fri May 13 00:56:11 2005
New Revision: 308

Modified:
   trunk/Model/Groups/Group.cc
   trunk/Model/Groups/Group.h
   trunk/Model/Readers/PlyReader.cc
   trunk/SCIRun/Core/CMakeLists.txt
   trunk/scenes/primtest.cc
Log:
fixed a bug in PlyReader that made it crash in certain environments, cleanly 
handles errors, and cleaned up (somewhat) code.

Modified: trunk/Model/Groups/Group.cc
==============================================================================
--- trunk/Model/Groups/Group.cc (original)
+++ trunk/Model/Groups/Group.cc Fri May 13 00:56:11 2005
@@ -25,6 +25,19 @@
   objs.push_back(obj);
 }
 
+void Group::shrinkTo(int firstNumObjs, bool deleteRemainder) 
+{
+    if (deleteRemainder)
+        for(int i=firstNumObjs;i<static_cast<int>(objs.size());i++)
+            delete objs[i];
+    objs.resize(firstNumObjs);
+}
+
+int Group::getNumObjs() 
+{
+    return static_cast<int>(objs.size());
+}
+
 void Group::preprocess(const PreprocessContext& context)
 {
   for(vector<Object*>::iterator iter = objs.begin();

Modified: trunk/Model/Groups/Group.h
==============================================================================
--- trunk/Model/Groups/Group.h  (original)
+++ trunk/Model/Groups/Group.h  Fri May 13 00:56:11 2005
@@ -17,6 +17,8 @@
     virtual ~Group();
 
     void add(Object* obj);
+    void shrinkTo(int firstNumObjs, bool deleteRemainder);
+    int getNumObjs();
 
     virtual void preprocess(const PreprocessContext&);
     virtual void intersect(const RenderContext& context, RayPacket& rays) 
const;

Modified: trunk/Model/Readers/PlyReader.cc
==============================================================================
--- trunk/Model/Readers/PlyReader.cc    (original)
+++ trunk/Model/Readers/PlyReader.cc    Fri May 13 00:56:11 2005
@@ -2,6 +2,8 @@
 #include <Model/Primitives/Triangle.h>
 #include <Model/Readers/PlyReader.h>
 #include <Model/Materials/Lambertian.h>
+#include <SCIRun/Core/Exceptions/FileNotFound.h>
+#include <sstream>
 #include <stdlib.h>
 
 #include "rply/rply.h"
@@ -19,17 +21,15 @@
      double r, g, b;
 };
 
-Point **vertices;
-
-int vertexCount = 0;
+std::vector<Point> vertices;
 VertexData vertexData;
 AffineTransform *t;
 
 static int addPoint() {
      //TODO: check if we used color and if so, add it to color array
-     Point *p = new Point(vertexData.x, vertexData.y, vertexData.z);
-     *p = *t * *p;
-     vertices[vertexCount++] = p;
+
+     Point p(vertexData.x, vertexData.y, vertexData.z);
+     vertices.push_back(*t*p);
 }
 
 static int vertex_cb(p_ply_argument argument) {
@@ -46,7 +46,7 @@
      long vertexIndex;
      ply_get_argument_element(argument, NULL, &vertexIndex);
      
-     if (vertexIndex == vertexCount+1) {
+     if (vertexIndex == vertices.size()+1) {
          addPoint();
      }
      
@@ -95,10 +95,13 @@
      case 1: prevVertexIndex = currVertexIndex;
          break;
      default: 
-         group->add(new Triangle(mat, *vertices[firstVertexIndex], 
*vertices[prevVertexIndex], *vertices[currVertexIndex]));
+         group->add(new Triangle(mat, vertices[firstVertexIndex], 
vertices[prevVertexIndex], vertices[currVertexIndex]));
          prevVertexIndex = currVertexIndex;
          break;
      }
+
+     return 1;
+     
 }
 
 bool
@@ -106,11 +109,19 @@
                   Group *g, int gridsize, Material *m) {
      long nFaces;
      long nVertices;
-     
+     int originalSize = g->getNumObjs();
+
      t = &_t;
      
      p_ply ply = ply_open(fileName.c_str(), NULL);
-     if (!ply) return false;
+     if (!ply)
+     {
+         ostringstream msg;
+         msg << "An error occured while trying to load the following ply 
file: " << fileName;
+         throw SCIRun::FileNotFound(msg.str());
+     }
+
+     
      if (!ply_read_header(ply)) return false;
 
      if (m)
@@ -121,7 +132,7 @@
      nVertices = ply_set_read_cb(ply, "vertex", "x", vertex_cb, 
                                 NULL, 0);
 
-     vertices = new Point* [nVertices];
+     vertices.reserve(nVertices);
 
      ply_set_read_cb(ply, "vertex", "y", vertex_cb, 
                     NULL, 0);
@@ -137,12 +148,16 @@
     
      nFaces = ply_set_read_cb(ply, "face", "vertex_indices", face_cb, g, 0);
 
-     if (!ply_read(ply)) return false;
-
+     if (!ply_read(ply)) {
+         //need to clean up group.
+         g->shrinkTo(originalSize, true);
+         if (!m)
+             delete defaultMaterial;
+         
+         return false;
+     }
+     
      ply_close(ply);
-
-     //TODO: am I deleting the right thing here!?!
-     //delete vertices;
 
      return true;
 }

Modified: trunk/SCIRun/Core/CMakeLists.txt
==============================================================================
--- trunk/SCIRun/Core/CMakeLists.txt    (original)
+++ trunk/SCIRun/Core/CMakeLists.txt    Fri May 13 00:56:11 2005
@@ -8,6 +8,7 @@
      Exceptions/AssertionFailed.cc
      Exceptions/ErrnoException.cc
      Exceptions/Exception.cc
+     Exceptions/FileNotFound.cc
      Exceptions/InternalError.cc
      Exceptions/InvalidState.cc
      )

Modified: trunk/scenes/primtest.cc
==============================================================================
--- trunk/scenes/primtest.cc    (original)
+++ trunk/scenes/primtest.cc    Fri May 13 00:56:11 2005
@@ -244,10 +244,10 @@
     AffineTransform t;
     t.initWithScale(Vector(scale/max*30, scale/max*30, scale/max*30));
     Group *model = new Group();
-    readPlyFile(modelName, t, model, 0, matl);
-    //spinprim = model;
+    if (!readPlyFile(modelName, t, model, 0, matl))
+        printf("error loading or reading ply file: %s\n", modelName); 
//would be better to throw an error.
     group->add(model);
-  } else {
+  } else { 
     throw IllegalArgument("Unknown primitive type for primtest: "+primtype, 
0, args);
   }
 




  • [MANTA] r308 - in trunk: Model/Groups Model/Readers SCIRun/Core scenes, thiago, 05/13/2005

Archive powered by MHonArc 2.6.16.

Top of page