Text archives Help
- From: bigler@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r997 - in trunk/Model: Groups Intersections Readers
- Date: Mon, 3 Apr 2006 13:19:40 -0600 (MDT)
Author: bigler
Date: Mon Apr 3 13:19:40 2006
New Revision: 997
Added:
trunk/Model/Readers/IW.cc
trunk/Model/Readers/IW.h
Modified:
trunk/Model/Groups/KDTreeLoaderIW.cc
trunk/Model/Intersections/TriangleEdge.h
trunk/Model/Readers/CMakeLists.txt
Log:
Model/Groups/KDTreeLoaderIW.cc
Use the new IWObjectReader call. It now parses out the colors
properly.
Model/Intersections/TriangleEdge.h
Change EPSILON to use scientific notation, so it is easier to tell
the difference between 0.000001 and 0.00001 (1e-6,1e-5).
Model/Readers/CMakeLists.txt
Added IW.cc
Model/Readers/IW.cc
Model/Readers/IW.h
Loader for Ingo's IW geometry format.
Modified: trunk/Model/Groups/KDTreeLoaderIW.cc
==============================================================================
--- trunk/Model/Groups/KDTreeLoaderIW.cc (original)
+++ trunk/Model/Groups/KDTreeLoaderIW.cc Mon Apr 3 13:19:40 2006
@@ -28,6 +28,7 @@
#include <Model/Groups/KDTree.h>
#include <Model/Groups/KDTreeLoaderIW.h>
+#include <Model/Readers/IW.h>
#include <stack>
#include <iostream>
@@ -148,83 +149,19 @@
int Manta::Kdtree::loadIW( KDTreeData* kdtree, const char* geom_filename,
const char* bsp_filename, int np )
{
- // Attempt to open the two files
- FILE* geomf = fopen(geom_filename, "r");
- if (!geomf) {
- fprintf(stderr, "Error: cannot open %s for loading\n",
geom_filename);
- return LoadingProblem;
- }
FILE* bspf = fopen(bsp_filename, "r");
if (!bspf) {
fprintf(stderr, "Error: cannot open %s for loading\n",
bsp_filename);
- fclose(bspf);
- return LoadingProblem;
- }
-
- // Start loading in the geometry
-#define LINE_WIDTH 100
-
- char line[LINE_WIDTH];
- fgets(line, LINE_WIDTH, geomf);
- char token[LINE_WIDTH];
- sscanf(line, "%s %%*\n", token);
- // Check to see if the token matches newobject
- if (strcmp(token, "newobject")) {
- fprintf(stderr, "First string (%s) is not newobject\n", token);
- fclose(geomf); fclose(bspf);
- return LoadingProblem;
- }
-
- /////////////////////////////////////////////////////////////////
- // Load the vertices
- fgets(line, LINE_WIDTH, geomf);
- unsigned int num_vertices;
- sscanf(line, "%s %u", token, &num_vertices);
- // Check to see if the token matches vertices
- if (strcmp(token, "vertices:")) {
- fprintf(stderr, "Token (%s) not equal to vertices:\n", token);
- fclose(geomf); fclose(bspf);
- return LoadingProblem;
- }
- float* vertices = (float*)(malloc(num_vertices*sizeof(float)*3));
- float* vp = vertices;
- for(unsigned int vi = 0; vi < num_vertices; ++vi) {
- fscanf(geomf, "\t%f %f %f\n", vp, vp+1, vp+2);
- vp+=3;
- }
-
- /////////////////////////////////////////////////////////////////
- // Load the vertex normals
- fgets(line, LINE_WIDTH, geomf);
- unsigned int num_vtxnormals;
- sscanf(line, "%s %u", token, &num_vtxnormals);
- // Check to see if the token matches vtxnormals
- if (strcmp(token, "vtxnormals:")) {
- fprintf(stderr, "Token (%s) not equal to vtxnormals:\n", token);
- fclose(geomf); fclose(geomf); free(vertices);
return LoadingProblem;
}
-
- float* vtxnormals = (float*)(malloc(num_vtxnormals*sizeof(float)*3));
- vp = vtxnormals;
- for(unsigned int vi = 0; vi < num_vtxnormals; ++vi) {
- fscanf(geomf, "\t%f %f %f\n", vp, vp+1, vp+2);
- vp+=3;
- }
-
- /////////////////////////////////////////////////////////////////
- // Load the triangles
- fgets(line, LINE_WIDTH, geomf);
- unsigned int num_tris;
- sscanf(line, "%s %u", token, &num_tris);
- // Check to see if the token matches triangles
- if (strcmp(token, "triangles:")) {
- fprintf(stderr, "Token (%s) not equal to triangles:\n", token);
- fclose(geomf); fclose(geomf); free(vertices); free(vtxnormals);
+ IWObject* iw = IWObjectRead(geom_filename);
+ if (!iw) {
+ fclose(bspf);
return LoadingProblem;
}
// Allocate space for the triangles and read them in:
+ unsigned int num_tris = static_cast<unsigned int>(iw->triangles.size());
kdtree->tris = new VArray<Triangle>(num_tris);
kdtree->tris->setLen(num_tris);
kdtree->normals = reinterpret_cast<TriangleNormal*>(new
char[sizeof(TriangleNormal)*num_tris]);
@@ -242,30 +179,31 @@
BBox bbox;
for(unsigned int ti = 0; ti < num_tris; ++ti) {
- unsigned int a,b,c,group;
- fscanf(geomf, "\t%u %u %u %u\n", &a, &b, &c, &group);
-
- Vectorf va(vertices+(a*3));
- Vectorf vb(vertices+(b*3));
- Vectorf vc(vertices+(c*3));
+ unsigned int a, b, c, group;
+ a = iw->triangles[ti][0];
+ b = iw->triangles[ti][1];
+ c = iw->triangles[ti][2];
+ group = iw->triangles[ti][3];
+
// Do bounding box extension
- bbox.extendByPoint(va);
- bbox.extendByPoint(vb);
- bbox.extendByPoint(vc);
+ bbox.extendByPoint(iw->vertices[a]);
+ bbox.extendByPoint(iw->vertices[b]);
+ bbox.extendByPoint(iw->vertices[c]);
// Add the triangle geometry
Triangle& t = kdtree->tris->get(ti);
- t.v = va;
- t.edge1 = vb - t.v;
- t.edge2 = vc - t.v;
+ t.v = iw->vertices[a];
+ t.edge1 = Vectorf(iw->vertices[b]) - t.v;
+ t.edge2 = Vectorf(iw->vertices[c]) - t.v;
// kdtree->triToGroupMap->get(ti) = group;
// Set up the vertex normal
TriangleNormal& tn= kdtree->normals[ti];
+ size_t num_vtxnormals = iw->vtxnormals.size();
if (a < num_vtxnormals && b < num_vtxnormals && c < num_vtxnormals) {
- tn[0] = Vectorf(vtxnormals+(a*3));
- tn[1] = Vectorf(vtxnormals+(b*3));
- tn[2] = Vectorf(vtxnormals+(c*3));
+ tn[0] = Vectorf(iw->vtxnormals[a]);
+ tn[1] = Vectorf(iw->vtxnormals[b]);
+ tn[2] = Vectorf(iw->vtxnormals[c]);
} else {
// Compute the normal
Vectorf normal(Cross(t.edge1, t.edge2).normal());
@@ -273,25 +211,14 @@
tn[1] = normal;
tn[2] = normal;
}
- }
- // Here you should parse out the shaders, but we skip this for now
-
- Color all_color(RGBColor(0.5,0.5,0.5));
- for(unsigned int ti = 0; ti < num_tris; ++ti) {
- Triangle& t = kdtree->tris->get(ti);
- // int group = kdtree->triToGroupMap.get(ti);
- t.payload = all_color;
+ // Set the color
+ if (group < iw->shaders.size()) {
+ t.payload = iw->shaders[group];
+ } else {
+ t.payload = Color(RGBColor(0.5, 0.5, 0.5));
+ }
}
-
- fprintf(stderr, "vertices :%10u\n", num_vertices);
- fprintf(stderr, "vertex normals :%10u\n", num_vtxnormals);
- fprintf(stderr, "triangles :%10u\n", num_tris);
-
- // Cleanup memory
- free(vertices);
- free(vtxnormals);
- fclose(geomf);
// Copy the bounding box over.
kdtree->bbox = bbox;
Modified: trunk/Model/Intersections/TriangleEdge.h
==============================================================================
--- trunk/Model/Intersections/TriangleEdge.h (original)
+++ trunk/Model/Intersections/TriangleEdge.h Mon Apr 3 13:19:40 2006
@@ -50,7 +50,7 @@
// C-style ray triangle edge intersection test. Slightly faster than C++
// version included below.
-#define EPSILON 0.000001
+#define EPSILON 1e-6f
#define CROSS(dest,v1,v2) \
(dest)[0]=(v1)[1]*(v2)[2]-(v1)[2]*(v2)[1]; \
@@ -62,10 +62,11 @@
(dest)[1]=(v1)[1]-(v2)[1]; \
(dest)[2]=(v1)[2]-(v2)[2];
- inline int intersect_triangle3_edge(const float orig[3], const float
dir[3],
- const float vert0[3],
- float *t, float *u, float *v, const
float *edge1, const float *edge2 )
- {
+ inline int intersect_triangle3_edge(const float orig[3], const float
dir[3],
+ const float vert0[3],
+ float *t, float *u, float *v,
+ const float *edge1, const float *edge2
)
+ {
// float edge1[3], edge2[3];
float tvec[3], pvec[3], qvec[3];
float det,inv_det;
@@ -82,7 +83,6 @@
/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);
- inv_det = 1.0f/ det;
CROSS(qvec, tvec, edge1);
@@ -113,6 +113,8 @@
}
else return 0; /* ray is parallell to the plane of the triangle */
+ inv_det = 1.0f/ det;
+
*t = DOT(edge2, qvec) * inv_det;
(*u) = uu*inv_det;
(*v) = vv*inv_det;
Modified: trunk/Model/Readers/CMakeLists.txt
==============================================================================
--- trunk/Model/Readers/CMakeLists.txt (original)
+++ trunk/Model/Readers/CMakeLists.txt Mon Apr 3 13:19:40 2006
@@ -7,6 +7,7 @@
Readers/BART/kbsplrot.c
Readers/BART/parse.cc
Readers/BART/quat.c
+ Readers/IW.cc
Readers/PlyReader.cc
Readers/V3C1.h
Readers/V3C1.cc
Added: trunk/Model/Readers/IW.cc
==============================================================================
--- (empty file)
+++ trunk/Model/Readers/IW.cc Mon Apr 3 13:19:40 2006
@@ -0,0 +1,168 @@
+/*
+ For more information, please see:
http://software.sci.utah.edu
+
+ The MIT License
+
+ Copyright (c) 2005-2006
+ Scientific Computing and Imaging Institute, University of Utah
+
+ License for the specific language governing rights and limitations under
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+#include <Model/Readers/IW.h>
+#include <MantaTypes.h>
+#include <Core/Color/ColorSpace.h>
+
+using namespace Manta;
+
+// Return null if there was a problem
+IWObject* Manta::IWObjectRead(const char* filename) {
+
+ // Attempt to open the file
+ FILE* geomf = fopen(filename, "r");
+ if (!geomf) {
+ fprintf(stderr, "Error: cannot open %s for loading\n",
filename);
+ return 0;
+ }
+
+ // Start loading in the geometry
+ // Buffer for line. This will be allocated by getline and
+ // reallocated as needed. We will need to free it later, though.
+ char* line = NULL;
+ size_t line_len = 0;
+ IWObject* iw = new IWObject();
+
+ while((getline(&line, &line_len, geomf)) != -1) {
+ char token[100];
+ sscanf(line, "%100s %%*\n", token);
+ // Check to see if the token matches newobject
+ if (strcmp(token, "newobject") == 0) {
+ // Don't do anything yet. We don't know how to handle multiple objects
+ } else if (strcmp(token, "vertices:") == 0) {
+ /////////////////////////////////////////////////////////////////
+ // Load the vertices
+ unsigned int num_vertices;
+ sscanf(line, "%*s %u", &num_vertices);
+
+ // Allocate space
+ iw->vertices.reserve(num_vertices);
+
+ // Read them in
+ for(unsigned int vi = 0; vi < num_vertices; ++vi) {
+ float x,y,z;
+ fscanf(geomf, "\t%f %f %f\n", &x, &y, &z);
+ iw->vertices.push_back(Vector(x,y,z));
+ }
+
+ // Check to make sure we read the right number
+ if (iw->vertices.size() != num_vertices) {
+ fprintf(stderr, "Read %llu vertices and expected %u vertices\n",
+ (unsigned long long)iw->vertices.size(), num_vertices);
+ fclose(geomf);
+ delete iw;
+ return 0;
+ }
+ } else if (strcmp(token, "vtxnormals:") == 0) {
+ /////////////////////////////////////////////////////////////////
+ // Load the vertex normals
+ unsigned int num_vtxnormals;
+ sscanf(line, "%*s %u", &num_vtxnormals);
+
+ // Allocate space
+ iw->vtxnormals.reserve(num_vtxnormals);
+
+ // Read them in
+ for(unsigned int vi = 0; vi < num_vtxnormals; ++vi) {
+ float x,y,z;
+ fscanf(geomf, "\t%f %f %f\n", &x, &y, &z);
+ iw->vtxnormals.push_back(Vector(x,y,z));
+ }
+
+ // Check to make sure we read the right number
+ if (iw->vtxnormals.size() != num_vtxnormals) {
+ fprintf(stderr, "Read %llu normals and expected %u normals\n",
+ (unsigned long long)iw->vtxnormals.size(), num_vtxnormals);
+ fclose(geomf);
+ delete iw;
+ return 0;
+ }
+ } else if (strcmp(token, "triangles:") == 0) {
+ /////////////////////////////////////////////////////////////////
+ // Load the triangles
+ unsigned int num_tris;
+ sscanf(line, "%*s %u", &num_tris);
+
+ // Allocate space
+ iw->triangles.reserve(num_tris);
+
+ // Read them in
+ for(unsigned int ti = 0; ti < num_tris; ++ti) {
+ unsigned int data[4];
+ fscanf(geomf, "\t%u %u %u %u\n", data, data+1, data+2, data+3);
+ iw->triangles.push_back(VectorT<unsigned int, 4>(data));
+ }
+
+ // Check to make sure we read the right number
+ if (iw->triangles.size() != num_tris) {
+ fprintf(stderr, "Read %llu triangles and expected %u triangles\n",
+ (unsigned long long)iw->triangles.size(), num_tris);
+ fclose(geomf);
+ delete iw;
+ return 0;
+ }
+ } else if (strcmp(token, "endobject") == 0) {
+ // Do nothing right now
+ } else if (strcmp(token, "camera") == 0) {
+ // Do nothing right now
+ } else if (strcmp(token, "shaders") == 0) {
+ /////////////////////////////////////////////////////////////////
+ // Load the triangles
+ unsigned int num_shaders;
+ sscanf(line, "%*s %u", &num_shaders);
+
+ // Allocate space, need to resize, becase we can't simply just
+ // push_back as the shader has the index as part of the file spec.
+ iw->shaders.resize(num_shaders, Color(RGBColor(1,0,1)));
+
+ // Read them in
+ for(unsigned int i = 0; i < num_shaders; ++i) {
+ float r, g, b;
+ unsigned int index;
+ fscanf(geomf, "shader %u diffuse (%f,%f,%f)", &index, &r, &g, &b);
+ iw->shaders[index] = Color(RGBColor(r,g,b));
+ }
+ } else {
+ if (strlen(token))
+ fprintf(stderr, "Unknown token: (%s)\n", token);
+ }
+ } // end while loop
+
+ fprintf(stderr, "vertices :%10llu\n",
+ (unsigned long long)iw->vertices.size());
+ fprintf(stderr, "vertex normals :%10llu\n",
+ (unsigned long long)iw->vtxnormals.size());
+ fprintf(stderr, "triangles :%10llu\n",
+ (unsigned long long)iw->triangles.size());
+ fprintf(stderr, "shaders :%10llu\n",
+ (unsigned long long)iw->shaders.size());
+
+ fclose(geomf);
+ return iw;
+}
Added: trunk/Model/Readers/IW.h
==============================================================================
--- (empty file)
+++ trunk/Model/Readers/IW.h Mon Apr 3 13:19:40 2006
@@ -0,0 +1,91 @@
+/*
+ For more information, please see:
http://software.sci.utah.edu
+
+ The MIT License
+
+ Copyright (c) 2005-2006
+ Scientific Computing and Imaging Institute, University of Utah
+
+ License for the specific language governing rights and limitations under
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef Manta_Model_IW_h
+#define Manta_Model_IW_h
+
+/* This reads in geometry from an .iw format that is used locally by a
+ few of us. Not really useful if you don't have some lying
+ around. */
+
+/*
+ The iw-file looks like:
+
+newobject 0
+vertices: <numvertices>
+\tx y z
+\tx y z
+... ("numvertices" times, that's the vertex positions)
+vtxnormals: <numvertices> # This can be zero
+\tnx ny nz
+\tnx ny nz
+...
+triangles: <numtriangles>
+\ta b c s
+\ta b c s
+... ## each triangle consists of vertex ID's a, b, and c ('0' is
+ ## the first vertex from 'vertices' above);
+ ## 's' is the shader ID (ignore for this test)
+endobject
+camera pos (%f,%f,%f) to (%f,%f,%f) up (%f,%f,%f)
+shaders <num shaders>
+shader <index> <type> <parameters>
+...
+ ## type can be:
+ ## diffuse (%f,%f,%f) # where %f is [0-1].
+
+*/
+
+#include <Core/Geometry/Vector.h>
+#include <Core/Geometry/VectorT.h>
+#include <MantaTypes.h>
+#include <vector>
+
+namespace Manta {
+
+ class IWObject {
+ public:
+ IWObject() {}
+
+ std::vector<Vector> vertices;
+ std::vector<Vector> vtxnormals;
+ std::vector<Color> shaders;
+ // First three indicies are the indicies of verticies/vtxnormals
+ // the last index is the shaders index.
+ std::vector<VectorT<unsigned int, 4> > triangles;
+ private:
+ // Copying could be nasty, so don't allow it.
+ IWObject( const Vector& copy ) {}
+ };
+
+ // Return null if there was a problem
+ IWObject* IWObjectRead(const char* filename);
+
+} // end namespace Manta
+
+#endif // Manta_Model_IW_h
- [MANTA] r997 - in trunk/Model: Groups Intersections Readers, bigler, 04/03/2006
Archive powered by MHonArc 2.6.16.