From a78ac53814ad57cd234e0f2a268d326884fc0ab6 Mon Sep 17 00:00:00 2001 From: malomo Date: Sat, 6 Sep 2014 11:06:23 +0000 Subject: [PATCH] added edge mesh support to OBJ importer corrected a type to avoid clang compiler issues --- vcg/space/triangle3.h | 2 +- wrap/io_trimesh/import_obj.h | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/vcg/space/triangle3.h b/vcg/space/triangle3.h index 317cd3ed..7e5122a2 100644 --- a/vcg/space/triangle3.h +++ b/vcg/space/triangle3.h @@ -90,7 +90,7 @@ Point3Type Normal( Point3Type const &p0, Point3Type const & p1, Point3Type cons /// Like the above, it returns the normal to the plane passing through p0,p1,p2, but normalized. template -Point3 NormalizedNormal(const TriangleType &t) +typename TriangleType::CoordType NormalizedNormal(const TriangleType &t) { return (( t.cP(1) - t.cP(0)) ^ (t.cP(2) - t.cP(0))).Normalize(); } diff --git a/wrap/io_trimesh/import_obj.h b/wrap/io_trimesh/import_obj.h index ea3fed39..928849e5 100644 --- a/wrap/io_trimesh/import_obj.h +++ b/wrap/io_trimesh/import_obj.h @@ -57,6 +57,7 @@ namespace vcg { typedef typename OpenMeshType::VertexPointer VertexPointer; typedef typename OpenMeshType::ScalarType ScalarType; typedef typename OpenMeshType::VertexType VertexType; + typedef typename OpenMeshType::EdgeType EdgeType; typedef typename OpenMeshType::FaceType FaceType; typedef typename OpenMeshType::VertexIterator VertexIterator; typedef typename OpenMeshType::FaceIterator FaceIterator; @@ -82,6 +83,8 @@ namespace vcg { /// number of vertices int numVertices; + /// number of edges + int numEdges; /// number of faces (the number of triangles could be /// larger in presence of polygonal faces int numFaces; @@ -112,6 +115,12 @@ namespace vcg { Color4b c; }; + struct ObjEdge + { + int v0; + int v1; + }; + struct ObjTexCoord { float u; @@ -260,6 +269,7 @@ namespace vcg { materials.push_back(defaultMaterial); int numVertices = 0; // stores the number of vertices been read till now + int numEdges = 0; // stores the number of edges read till now int numTriangles = 0; // stores the number of faces been read till now int numTexCoords = 0; // stores the number of texture coordinates been read till now int numVNormals = 0; // stores the number of vertex normals been read till now @@ -269,6 +279,8 @@ namespace vcg { // vertices and faces allocation VertexIterator vi = vcg::tri::Allocator::AddVertices(m,oi.numVertices); //FaceIterator fi = Allocator::AddFaces(m,oi.numFaces); + // edges found + std::vector ev; std::vector vertexColorVector; ObjIndexedFace ff; const char *loadingStr = "Loading"; @@ -358,6 +370,22 @@ namespace vcg { numVNormals++; } + else if ( header.compare("l")==0 ) + { + loadingStr = "Edge Loading"; + + if (numTokens < 3) + { + result = E_LESS_THAN_3_VERT_IN_FACE; // TODO add proper/handling error code + continue; + } + + ObjEdge e = { (atoi(tokens[1].c_str()) - 1), + (atoi(tokens[2].c_str()) - 1) }; + ev.push_back(e); + + numEdges++; + } else if( (header.compare("f")==0) || (header.compare("q")==0) ) // face { loadingStr="Face Loading"; @@ -605,6 +633,27 @@ namespace vcg { } // end while stream not eof assert((numTriangles +numVertices) == numVerticesPlusFaces+extraTriangles); vcg::tri::Allocator::AddFaces(m,numTriangles); + + // Add found edges + if (numEdges > 0) + { + vcg::tri::Allocator::AddEdges(m,numEdges); + + assert(m.edge.size() == size_t(m.en)); + + for(int i=0; i= 0 && size_t(e.v0) < m.vert.size() && + e.v1 >= 0 && size_t(e.v1) < m.vert.size()); + // TODO add proper handling of bad indices + + edge.V(0) = &(m.vert[e.v0]); + edge.V(1) = &(m.vert[e.v1]); + } + } //------------------------------------------------------------------------------- // Now the final passes: @@ -929,6 +978,7 @@ namespace vcg { bool bHasPerVertexColor = false; oi.numVertices=0; + oi.numEdges=0; oi.numFaces=0; oi.numTexCoords=0; oi.numNormals=0; @@ -961,6 +1011,8 @@ namespace vcg { else { if((line[0]=='f') || (line[0]=='q')) oi.numFaces++; else + if (line[0]=='l') oi.numEdges++; + else if(line[0]=='u' && line[1]=='s') bHasPerFaceColor = true; // there is a usematerial so add per face color } } @@ -983,6 +1035,8 @@ namespace vcg { else oi.mask |= vcg::tri::io::Mask::IOM_WEDGNORMAL; } + if (oi.numEdges) + oi.mask |= vcg::tri::io::Mask::IOM_EDGEINDEX; stream.close();