From 3806dbad7b06c6d5bdb1583c75b4e0a7b4b23c52 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 10 Jun 2011 13:13:06 +0200 Subject: [PATCH] lecture des path simple non ferme dans importSVG --- include/Algo/Import/importSvg.hpp | 356 +++++++++++++++++++++++++----- 1 file changed, 297 insertions(+), 59 deletions(-) diff --git a/include/Algo/Import/importSvg.hpp b/include/Algo/Import/importSvg.hpp index a7b958835..729c2e369 100644 --- a/include/Algo/Import/importSvg.hpp +++ b/include/Algo/Import/importSvg.hpp @@ -1,5 +1,6 @@ #include #include "Geometry/bounding_box.h" +#include namespace CGoGN { @@ -22,6 +23,14 @@ inline bool valueOf(const std::string &s, T &obj) return is >> obj; } +template +bool posSort(const std::pair& a1, const std::pair& a2) +{ + VEC v1 = a1.first; + VEC v2 = a2.first; + return v1[0] < v2[0] || (v1[0] == v2[0] && v1[1] < v2[1]); +} + template bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP::TVEC3& position, CellMarker& polygons, CellMarker& polygonsFaces) { @@ -39,6 +48,8 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP std::vector allPoly; + std::vector allBrokenLines; + std::vector allBrokenLinesWidth; for (xmlNode* cur_node = map_node->children; cur_node; cur_node = cur_node->next) { @@ -51,7 +62,8 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP { if (chechXmlNode(cur_path, "path")) { - allPoly.push_back(POLYGON()); + bool closedPoly=false; + POLYGON curPoly; CGoGNout << "--load a path--"<< CGoGNendl; xmlChar* prop = xmlGetProp(cur_path, BAD_CAST "d"); // CGoGNout << "path "<< prop << CGoGNendl; @@ -111,6 +123,7 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP else if(coord[0]=='z') { // std::cout << "the end" << std::endl; + closedPoly = true; } else //coordinates { @@ -128,7 +141,6 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP valueOf(xS,x); - POLYGON curPoly = allPoly[allPoly.size()-1]; VEC3 previous = (curPoly)[(curPoly).size()-1]; y = previous[1]; @@ -143,7 +155,6 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP valueOf(yS,y); - POLYGON curPoly = allPoly[allPoly.size()-1]; typename PFP::VEC3 previous = (curPoly)[(curPoly).size()-1]; x = previous[0]; @@ -185,7 +196,6 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP //if there is a point to push if(push_point) { - POLYGON& curPoly = allPoly[allPoly.size()-1]; if(relative && curPoly.size()>0) { @@ -200,7 +210,6 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP } //check orientation : set in CCW - POLYGON& curPoly = allPoly[allPoly.size()-1]; if(curPoly.size()>2) { VEC3 v1(curPoly[1]-curPoly[0]); @@ -212,6 +221,29 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP } } + if(closedPoly) + allPoly.push_back(curPoly); + else { + allBrokenLines.push_back(curPoly); + xmlChar* prop = xmlGetProp(cur_path, BAD_CAST "style"); + std::string allstyle((reinterpret_cast(prop))); + std::stringstream is(allstyle); + std::string style; + while ( std::getline( is, style, ';' ) ) + { + if(style.find("stroke-width:")!=std::string::npos) + { + std::stringstream isSize(style); + std::getline( isSize, style, ':' ); + float sizeOfLine; + isSize >> sizeOfLine; + std::cout << "sizeOfLine : " << sizeOfLine << std::endl; + allBrokenLinesWidth.push_back(sizeOfLine); + } + } + + } + } } } @@ -219,8 +251,53 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP xmlFreeDoc(doc); - Geom::BoundingBox bb(*(allPoly.begin()->begin())); + Geom::BoundingBox bb(*(allBrokenLines.begin()->begin())); + + CellMarker brokenMark(map,EDGE); + AttributeHandler edgeWidth = map.template addAttribute(FACE, "width"); + + ///////////////////////////////////////////////////////////////////////////////////////////// + //create broken lines + std::vector::iterator itW = allBrokenLinesWidth.begin(); + for(typename std::vector::iterator it = allBrokenLines.begin() ; it != allBrokenLines.end() ; ++it) + { + if(it->size()<2) + { + it = allBrokenLines.erase(it); + itW = allBrokenLinesWidth.erase(itW); + } + else + { + unsigned int faceDegree = it->size()*2-2; + Dart d = map.newFace(faceDegree); + edgeWidth[d] = *itW; + std::cout << "newFace " << faceDegree << std::endl; + + polygonsFaces.mark(d); + + Dart d1=d; + Dart d_1=map.phi_1(d); + for(unsigned int i = 0; ibegin() ; emb != it->end() ; emb++) + { + position[d] = *emb; + bb.addPoint(*emb); + d = map.phi1(d); + } + } + itW++; + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + //create polygons typename std::vector::iterator it; for(it = allPoly.begin() ; it != allPoly.end() ; ++it) { @@ -243,82 +320,243 @@ bool importSVG(typename PFP::MAP& map, const std::string& filename, typename PFP } } + + ///////////////////////////////////////////////////////////////////////////////////////////// + //add bounding box + + CellMarker boundingBox(map,VERTEX); Dart dBorder = map.newFace(4); - typename PFP::VEC3 bmin = bb.min(); - typename PFP::VEC3 bmax = bb.max(); - position[dBorder] = bmin-0.1f*bmin; - position[map.phi1(dBorder)] = VEC3(bmax[0],bmin[1],0) + 0.1f*VEC3(bmax[0],-bmin[1],0); - position[map.phi1(map.phi1(dBorder))] = VEC3(bmax[0],bmax[1],0) + 0.1f*bmax; - position[map.phi_1(dBorder)] = VEC3(bmin[0],bmax[1],0) + 0.1f*VEC3(-bmin[0],bmax[1],0);; + + VEC3 bbCenter = bb.center(); + VEC3 bmin = bb.min(); + bmin += 0.3f*(bmin-bbCenter); + VEC3 bmax = bb.max(); + bmax -= 0.3f*(bmin-bbCenter); + + position[dBorder] = bmin; + position[map.phi1(dBorder)] = VEC3(bmax[0],bmin[1],0); + position[map.phi1(map.phi1(dBorder))] = VEC3(bmax[0],bmax[1],0); + position[map.phi_1(dBorder)] = VEC3(bmin[0],bmax[1],0); + + Dart d = dBorder; + do + { + boundingBox.mark(d); + d = map.phi1(d); + } while(d!=dBorder); + + ///////////////////////////////////////////////////////////////////////////////////////////// + //cut the edges to have a more regular sampling +// float maxDist=60.0f; +// CellMarker treated(map,EDGE); +// for(Dart d = map.begin(); d != map.end(); map.next(d)) +// { +// if(!treated.isMarked(d)) +// { +// treated.mark(d); +// VEC3 p1 =position[d]; +// VEC3 p2 =position[map.phi1(d)]; +// +// if((p1-p2).norm()>maxDist) +// { +// unsigned int nbSeg = ((p1-p2).norm())/int(maxDist); +// for(unsigned int i=0;imaxDistSq) - { - VEC3 p3 = (p1+p2)/2.0f; - map.cutEdge(d); - position[map.phi1(d)] = p3; - } - } - - CellMarker linked(map,VERTEX); + ///////////////////////////////////////////////////////////////////////////////////////////// + //process broken lines for(Dart d = map.begin();d != map.end(); map.next(d)) { - if(!linked.isMarked(d) && !inside.isMarked(d)) + if(map.phi2(d) != d && map.sameFace(map.phi2(d),d) && !boundingBox.isMarked(d) && inside.isMarked(d)) { - linked.mark(d); - Dart dMin=map.end(); - float distMin = 1.0f/0.0f; - for(Dart dd = map.begin(); dd != map.end(); map.next(dd)) + std::cout << "add face" << std::endl; + + Dart d1 = d; + Dart d2 = map.phi2(d); + VEC3 p1 = position[d1]; + VEC3 p2 = position[d2]; + float width = edgeWidth[d1]; + + VEC3 v = p2-p1; + map.unsewFaces(d); + + Dart dN = map.newFace(4); + + VEC3 ortho = v^VEC3(0,0,1); + ortho.normalize(); + + if(map.alpha1(d1)==d1) { - if(!inside.isMarked(dd) && !map.sameFace(d,dd)) - { - float dist = (position[dd]-position[d]).norm(); - if(dist > toConnect; +// CellMarker connected(map,VERTEX); +// for(Dart d = map.begin(); d != map.end(); map.next(d)) +// { +// if( ((!boundingBox.isMarked(d) && !inside.isMarked(d)) || (boundingBox.isMarked(d) && inside.isMarked(d))) +// && !connected.isMarked(d) +// ) +// { +// connected.mark(d); +// toConnect.push_back(std::make_pair(position[d],d)); +// } +// } +// +// std::sort(toConnect.begin(), toConnect.end(),posSort); +// +// for(typename std::vector >::iterator it = toConnect.begin(); it != toConnect.end() ; ++it) +// { +// Dart d = it->second; +// typename std::vector >::iterator it2 = it+1; +// if(it2!= toConnect.end()) +// { +// Dart dd = it2->second; +// if(!map.sameFace(map.phi2(d),map.phi2(dd))) +// { +// if(!map.sameFace(dd,d)) { +// std::cout << "link" << std::endl; +// map.linkVertices(dd,d); +// } +// else +// { +// map.splitFace(dd,d); +// std::cout << "split" << std::endl; +// } +// } +// } +// +// } + +// CellMarker connected(map,VERTEX); +// unsigned int i=0; +// for(Dart d = map.begin();d != map.end(); map.next(d)) +// { +// if(!connected.isMarked(d) && !boundingBox.isMarked(d) && !inside.isMarked(d)) +// { +// i++; +// Dart dMin=map.end(); +// float distMin = std::numeric_limits::max(); +// for(Dart dd = map.begin(); dd != map.end(); map.next(dd)) +// { +// if( ( +// (boundingBox.isMarked(dd) && inside.isMarked(dd)) +// || (!boundingBox.isMarked(dd) && !inside.isMarked(dd)) +// ) +// && !map.sameFace(map.phi2(d),map.phi2(dd))) +// { +// if(Geom::testOrientation2D(position[dd], position[d], position[map.phi1(d)]) == Geom::LEFT +// && Geom::testOrientation2D(position[dd], position[d], position[map.phi_1(d)]) == Geom::RIGHT) +// { +// float dist = (position[dd]-position[d]).norm(); +// if(dist