From b1e0f7cc785cf34d01e304d52613cbf389d9cedd Mon Sep 17 00:00:00 2001 From: Pierre Kraemer Date: Mon, 20 Feb 2012 16:55:58 +0100 Subject: [PATCH] import MR DAT file --- include/Algo/Import/import2tablesSurface.hpp | 8 +- include/Algo/Import/importMRDAT.h | 95 ++++++++++++- include/Algo/Import/importMRDAT.hpp | 131 +++++++++++++++--- .../map/map2MR/map2MR_PrimalRegular.h | 2 +- 4 files changed, 209 insertions(+), 27 deletions(-) diff --git a/include/Algo/Import/import2tablesSurface.hpp b/include/Algo/Import/import2tablesSurface.hpp index d80b3fa9..0c11525f 100644 --- a/include/Algo/Import/import2tablesSurface.hpp +++ b/include/Algo/Import/import2tablesSurface.hpp @@ -298,7 +298,7 @@ bool MeshTablesSurface::importOff(const std::string& filename, std::vector< do { std::getline (fp, ligne); - } while (ligne.size()==0); + } while (ligne.size() == 0); std::stringstream oss(ligne); oss >> m_nbVertices; @@ -344,10 +344,10 @@ bool MeshTablesSurface::importOff(const std::string& filename, std::vector< } while (ligne.size() == 0); std::stringstream oss(ligne); - int n; + unsigned int n; oss >> n; m_nbEdges.push_back(n); - for (int j=0;j> index; @@ -455,7 +455,7 @@ bool MeshTablesSurface::importObj(const std::string& filename, std::vector< unsigned int ind = 0; - while ( (ind 0) diff --git a/include/Algo/Import/importMRDAT.h b/include/Algo/Import/importMRDAT.h index 55d924a5..5d20722d 100644 --- a/include/Algo/Import/importMRDAT.h +++ b/include/Algo/Import/importMRDAT.h @@ -35,8 +35,9 @@ namespace Import { template -bool importMRDAT(typename PFP::MAP& map, const std::string& filename, typename PFP::TVEC3& position) ; +bool importMRDAT(typename PFP::MAP& map, const std::string& filename, std::vector& attrNames) ; +template class QuadTreeNode { public: @@ -46,6 +47,7 @@ public: indices[i] = -1 ; for(unsigned int i = 0; i < 4; ++i) children[i] = NULL ; + parent = NULL ; } ~QuadTreeNode() @@ -59,23 +61,112 @@ public: { assert(children[0] == NULL) ; for(unsigned int i = 0; i < 4; ++i) + { children[i] = new QuadTreeNode() ; + children[i]->parent = this ; + } + } + + bool isSubdivided() + { + return children[0] != NULL ; + } + + void embed(typename PFP::MAP& map, Dart d, std::vector& vID, CellMarker& cm, bool CCW) + { + if(isSubdivided()) + { + unsigned int emb0 = vID[children[0]->indices[0]] ; + unsigned int emb1 = vID[children[0]->indices[1]] ; + unsigned int emb2 = vID[children[0]->indices[2]] ; + + Dart d0 = map.phi1(d) ; + Dart d1, d2 ; + if(CCW) + { + d1 = map.phi_1(d) ; + d2 = d ; + } + else + { + d1 = d ; + d2 = map.phi_1(d) ; + } + map.incCurrentLevel() ; + map.embedOrbit(VERTEX, map.phi1(d0), emb0) ; + map.embedOrbit(VERTEX, map.phi1(d1), emb1) ; + map.embedOrbit(VERTEX, map.phi1(d2), emb2) ; + map.decCurrentLevel() ; + + Dart t0 = map.phi_1(d) ; + map.incCurrentLevel() ; + t0 = map.phi2(map.phi1(t0)) ; + children[0]->embed(map, t0, vID, cm, CCW) ; + map.decCurrentLevel() ; + + Dart t1 = d ; + map.incCurrentLevel() ; + children[1]->embed(map, t1, vID, cm, !CCW) ; + map.decCurrentLevel() ; + + Dart t2 = map.phi1(d) ; + map.incCurrentLevel() ; + t2 = map.phi1(t2) ; + children[2]->embed(map, t2, vID, cm, !CCW) ; + map.decCurrentLevel() ; + + Dart t3 = map.phi_1(d) ; + map.incCurrentLevel() ; + t3 = map.phi_1(t3) ; + children[3]->embed(map, t3, vID, cm, !CCW) ; + map.decCurrentLevel() ; + } + } + + void print() + { + std::cout << indices[0] << " " << indices[1] << " " << indices[2] << std::endl ; + if(isSubdivided()) + { + for(unsigned int i = 0; i < 4; ++i) + children[i]->print() ; + } } unsigned int indices[3] ; QuadTreeNode* children[4] ; + QuadTreeNode* parent ; } ; +template class QuadTree { public: - std::vector roots ; + std::vector*> roots ; + std::vector darts ; ~QuadTree() { for(unsigned int i = 0; i < roots.size(); ++i) delete roots[i] ; } + + void embed(typename PFP::MAP& map, std::vector& vID) + { + CellMarker cm(map, VERTEX) ; + for(unsigned int i = 0; i < roots.size(); ++i) + roots[i]->embed(map, darts[i], vID, cm, true) ; + } + + void print() + { + std::cout << "printing quadtree (" << roots.size() << " roots)" << std::endl ; + for(unsigned int i = 0; i < roots.size(); ++i) + { + std::cout << "root " << i << std::endl ; + roots[i]->print() ; + } + } } ; } // namespace Import diff --git a/include/Algo/Import/importMRDAT.hpp b/include/Algo/Import/importMRDAT.hpp index 5aaf5a4f..f2a28693 100644 --- a/include/Algo/Import/importMRDAT.hpp +++ b/include/Algo/Import/importMRDAT.hpp @@ -39,8 +39,15 @@ inline void nextNonEmptyLine(std::ifstream& fp, std::string& line) } template -bool importMRDAT(typename PFP::MAP& map, const std::string& filename, typename PFP::TVEC3& position) +bool importMRDAT(typename PFP::MAP& map, const std::string& filename, std::vector& attrNames) { + AttributeHandler position = map.template getAttribute(VERTEX, "position") ; + + if (!position.isValid()) + position = map.template addAttribute(VERTEX, "position") ; + + attrNames.push_back(position.name()) ; + AttributeContainer& container = map.getAttributeContainer(VERTEX) ; // open file @@ -66,8 +73,11 @@ bool importMRDAT(typename PFP::MAP& map, const std::string& filename, typename P { nextNonEmptyLine(fp, line) ; std::stringstream oss(line) ; + std::string s ; + oss >> s ; oss >> depth ; } + std::cout << "MR depth -> " << depth << std::endl ; // read vertices nextNonEmptyLine(fp, line) ; @@ -78,35 +88,35 @@ bool importMRDAT(typename PFP::MAP& map, const std::string& filename, typename P return false ; } - std::vector positions ; - std::vector nbVerticesPerLevel ; - nbVerticesPerLevel.resize(depth + 1) ; + std::vector verticesID ; nextNonEmptyLine(fp, line) ; - while(line.rfind("Triangles") != std::string::npos) + while(line.rfind("Triangles") == std::string::npos) { std::stringstream oss(line) ; unsigned int level ; oss >> level ; - ++nbVerticesPerLevel[level - (depth + 1)] ; float x, y, z ; oss >> x ; oss >> y ; oss >> z ; - positions.push_back(typename PFP::VEC3(x, y, z)) ; + typename PFP::VEC3 pos(x, y, z) ; + + unsigned int id = container.insertLine() ; + position[id] = pos ; + verticesID.push_back(id) ; nextNonEmptyLine(fp, line) ; } - QuadTree qt ; - QuadTreeNode* current = NULL ; - + QuadTree qt ; + QuadTreeNode* current = NULL ; unsigned int prevNum = -1 ; nextNonEmptyLine(fp, line) ; - while(line.rfind("end") != std::string::npos) + while(line.rfind("end") == std::string::npos) { std::stringstream oss(line) ; @@ -123,7 +133,7 @@ bool importMRDAT(typename PFP::MAP& map, const std::string& filename, typename P if(root == 1) { assert(num == 0) ; - QuadTreeNode* n = new QuadTreeNode() ; + QuadTreeNode* n = new QuadTreeNode() ; n->indices[0] = idx0 ; n->indices[1] = idx1 ; n->indices[2] = idx2 ; @@ -133,28 +143,109 @@ bool importMRDAT(typename PFP::MAP& map, const std::string& filename, typename P } else { - if(num > prevNum) // on lit un autre triangle du même niveau + if(num == prevNum + 1) // on lit un autre triangle du même niveau { - + current = current->parent->children[num] ; } - else // on subdivise le triangle courant + else // on monte ou on descend d'un niveau { - if(num == 0) + if(num == 0) // on subdivise le triangle courant { current->subdivide() ; + current = current->children[0] ; } - else + else // on remonte d'un niveau { - + assert(prevNum == 3) ; + assert(current->parent->parent != NULL) ; + current = current->parent->parent->children[num] ; } } + current->indices[0] = idx0 ; + current->indices[1] = idx1 ; + current->indices[2] = idx2 ; + prevNum = num ; } nextNonEmptyLine(fp, line) ; } - fp.close(); - return true; + fp.close() ; + + AutoAttributeHandler< NoMathIONameAttribute< std::vector > > vecDartsPerVertex(map, VERTEX, "incidents") ; + DartMarkerNoUnmark m(map) ; + + unsigned nbf = qt.roots.size() ; + + // for each root face + for(unsigned int i = 0; i < nbf; ++i) + { + Dart d = map.newFace(3, false) ; + qt.darts.push_back(d) ; + for (unsigned int j = 0; j < 3; ++j) + { + unsigned int idx = qt.roots[i]->indices[j] ; + unsigned int emb = verticesID[idx] ; + + FunctorSetEmb fsetemb(map, VERTEX, emb) ; + map.foreach_dart_of_orbit(PFP::MAP::ORBIT_IN_PARENT(VERTEX), d, fsetemb) ; + + m.mark(d) ; // mark on the fly to unmark on second loop + vecDartsPerVertex[emb].push_back(d) ; // store incident darts for fast adjacency reconstruction + d = map.phi1(d) ; + } + } + + // reconstruct neighbourhood between root faces + unsigned int nbBoundaryEdges = 0 ; + for (Dart d = map.begin(); d != map.end(); map.next(d)) + { + if (m.isMarked(d)) + { + // darts incident to end vertex of edge + std::vector& vec = vecDartsPerVertex[map.phi1(d)] ; + + unsigned int embd = map.getEmbedding(VERTEX, d) ; + Dart good_dart = NIL ; + for (typename std::vector::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it) + { + if (map.getEmbedding(VERTEX, map.phi1(*it)) == embd) + good_dart = *it ; + } + + if (good_dart != NIL) + { + map.sewFaces(d, good_dart, false) ; + m.unmarkOrbit(EDGE, d) ; + } + else + { + m.unmark(d) ; + ++nbBoundaryEdges ; + } + } + } + + if (nbBoundaryEdges > 0) + { +// map.closeMap() ; + CGoGNout << "Open mesh.. not managed for now.." << CGoGNendl ; + return false ; + } + + for(unsigned int i = 0; i < depth; ++i) + map.addNewLevel(false) ; + map.setCurrentLevel(0) ; + + qt.embed(map, verticesID) ; + + for(unsigned int l = 0; l <= map.getMaxLevel(); ++l) + { + map.setCurrentLevel(l) ; + map.check() ; + } + + return true ; } } // namespace Import diff --git a/include/Topology/map/map2MR/map2MR_PrimalRegular.h b/include/Topology/map/map2MR/map2MR_PrimalRegular.h index 301161fd..3a551480 100644 --- a/include/Topology/map/map2MR/map2MR_PrimalRegular.h +++ b/include/Topology/map/map2MR/map2MR_PrimalRegular.h @@ -49,7 +49,7 @@ public: bool isOddVertex(Dart d) ; - void addNewLevel() ; + void addNewLevel(bool embedNewVertices) ; void addSynthesisFilter(Multiresolution::MRFilter* f) { synthesisFilters.push_back(f) ; } void addAnalysisFilter(Multiresolution::MRFilter* f) { analysisFilters.push_back(f) ; } -- GitLab