1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "InterpKernelGeo2DComposedEdge.hxx"
22 #include "InterpKernelGeo2DElementaryEdge.hxx"
23 #include "InterpKernelGeo2DEdgeArcCircle.hxx"
24 #include "InterpKernelGeo2DEdgeInfLin.hxx"
25 #include "InterpKernelException.hxx"
32 using namespace INTERP_KERNEL;
34 ComposedEdge::ComposedEdge(const ComposedEdge& other)
36 for(std::list<ElementaryEdge *>::const_iterator iter=other._sub_edges.begin();iter!=other._sub_edges.end();iter++)
37 _sub_edges.push_back((*iter)->clone());
40 ComposedEdge::~ComposedEdge()
42 clearAll(_sub_edges.begin());
45 void ComposedEdge::setValueAt(int i, Edge *e, bool direction)
47 std::list<ElementaryEdge*>::iterator it=_sub_edges.begin();
51 *it=new ElementaryEdge(e,direction);
54 /*! \cond HIDDEN_ITEMS */
57 AbsEdgeCmp(ElementaryEdge *b):_b1(b) { }
58 bool operator()(ElementaryEdge *a) { return a->getPtr()==_b1->getPtr();}
64 double ComposedEdge::getCommonLengthWith(const ComposedEdge& other) const
67 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
69 if(find_if(other._sub_edges.begin(),other._sub_edges.end(),AbsEdgeCmp(*iter))!=other._sub_edges.end())
71 const ElementaryEdge *tmp=static_cast<const ElementaryEdge *>(*iter);
72 ret+=tmp->getCurveLength();
78 void ComposedEdge::clear()
80 clearAll(_sub_edges.begin());
84 void ComposedEdge::pushBack(Edge *edge, bool direction)
86 _sub_edges.push_back(new ElementaryEdge(edge,direction));
89 void ComposedEdge::pushBack(ElementaryEdge *elem)
91 _sub_edges.push_back(elem);
94 void ComposedEdge::pushBack(ComposedEdge *elem)
96 std::list<ElementaryEdge *> *elemsOfElem=elem->getListBehind();
97 _sub_edges.insert(_sub_edges.end(),elemsOfElem->begin(),elemsOfElem->end());
100 ElementaryEdge *ComposedEdge::operator[](int i) const
102 std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();
103 for(int ii=0;ii<i;ii++)
108 void ComposedEdge::reverse()
110 _sub_edges.reverse();
111 for(std::list<ElementaryEdge *>::iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
115 bool ComposedEdge::presenceOfOn() const
118 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end() && !ret;iter++)
119 ret=((*iter)->getLoc()==FULL_ON_1);
123 bool ComposedEdge::presenceOfQuadraticEdge() const
126 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end() && !ret;iter++)
128 Edge *e=(*iter)->getPtr();
130 ret=dynamic_cast<EdgeArcCircle*>(e)!=0;
135 void ComposedEdge::initLocations() const
137 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
138 (*iter)->initLocations();
142 * Reset the status of all edges (OUT, IN, ON) because they were potentially assigned
143 * by the previous candidate processing.
145 void ComposedEdge::InitLocationsWithOther(const ComposedEdge& first, const ComposedEdge& other)
147 std::set<Edge *> s1,s2;
148 for(std::list<ElementaryEdge *>::const_iterator it1=first._sub_edges.begin();it1!=first._sub_edges.end();it1++)
149 s1.insert((*it1)->getPtr());
150 for(std::list<ElementaryEdge *>::const_iterator it2=other._sub_edges.begin();it2!=other._sub_edges.end();it2++)
151 s2.insert((*it2)->getPtr());
152 first.initLocations();
153 other.initLocations();
154 std::vector<Edge *> s3;
155 std::set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),std::back_insert_iterator< std::vector<Edge *> >(s3));
156 for(std::vector<Edge *>::const_iterator it3=s3.begin();it3!=s3.end();it3++)
160 ComposedEdge *ComposedEdge::clone() const
162 return new ComposedEdge(*this);
165 bool ComposedEdge::isNodeIn(Node *n) const
168 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end() && !ret;iter++)
169 ret=(*iter)->isNodeIn(n);
174 * This method computes the area of 'this'.
177 * Area=\int_{Polygon} dS
179 * Thanks to Green's theorem we have.
181 * \int_{Polygon} x \cdot dS=\sum_{0 \leq i < nb of edges} -\int_{Edge_{i}}ydx=\sum_{0 \leq i < nb of edges} AreaOfZone_{Edge_{i}}
183 * Where \f$ AreaOfZone_{i} \f$ is computed virtually by INTERP_KERNEL::Edge::getAreaOfZone with following formula :
185 * AreaOfZone_{i}=\int_{Edge_{i}} -ydx
188 double ComposedEdge::getArea() const
191 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
192 ret+=(*iter)->getAreaOfZone();
196 double ComposedEdge::getPerimeter() const
199 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
200 ret+=(*iter)->getCurveLength();
204 double ComposedEdge::getHydraulicDiameter() const
206 return 4*fabs(getArea())/getPerimeter();
210 * This method computes barycenter of 'this' by returning xG in bary[0] and yG in bary[1].
213 * Area \cdot x_{G}=\int_{Polygon} x \cdot dS
216 * Area \cdot y_{G}=\int_{Polygon} y \cdot dS
218 * Thanks to Green's theorem we have.
220 * \int_{Polygon} x \cdot dS=\sum_{0 \leq i < nb of edges} -\int_{Edge_{i}}yxdx
223 * \int_{Polygon} y \cdot dS=\sum_{0 \leq i < nb of edges} -\int_{Edge_{i}}\frac{y^{2}}{2}dx
225 * Area is computed using the same principle than described in INTERP_KERNEL::ComposedEdge::getArea method.
226 * \f$ -\int_{Edge_{i}}yxdx \f$ and \f$ -\int_{Edge_{i}}\frac{y^{2}}{2}dx \f$ are computed virtually with INTERP_KERNEL::Edge::getBarycenterOfZone.
228 void ComposedEdge::getBarycenter(double *bary) const
233 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
235 (*iter)->getBarycenterOfZone(bary);
236 area+=(*iter)->getAreaOfZone();
243 * Idem ComposedEdge::getBarycenter except that the special case where _sub_edges==1 is dealt here.
245 void ComposedEdge::getBarycenterGeneral(double *bary) const
247 if(_sub_edges.empty())
248 throw INTERP_KERNEL::Exception("ComposedEdge::getBarycenterGeneral called on an empty polygon !");
249 if(_sub_edges.size()>2)
250 return getBarycenter(bary);
252 _sub_edges.back()->getBarycenter(bary,w);
255 double ComposedEdge::normalizeMe(double& xBary, double& yBary)
258 b.prepareForAggregation();
260 double dimChar=b.getCaracteristicDim();
261 b.getBarycenter(xBary,yBary);
262 applyGlobalSimilarity(xBary,yBary,dimChar);
266 double ComposedEdge::normalize(ComposedEdge *other, double& xBary, double& yBary)
269 b.prepareForAggregation();
271 other->fillBounds(b);
272 double dimChar=b.getCaracteristicDim();
273 b.getBarycenter(xBary,yBary);
274 applyGlobalSimilarity(xBary,yBary,dimChar);
275 other->applyGlobalSimilarity(xBary,yBary,dimChar);
280 * This method operates the opposite operation than ComposedEdge::applyGlobalSimilarity.
282 void ComposedEdge::unApplyGlobalSimilarityExt(ComposedEdge& other, double xBary, double yBary, double fact)
285 other.initNodeHitStatus();
286 unApplySimilarityOnMyNodes(xBary,yBary,fact);
287 other.unApplySimilarityOnMyNodesIfNotAlreadyHit(xBary,yBary,fact);
289 other.initEdgeHitStatus();
290 unApplySimilarityOnMyEdges(xBary,yBary,fact);
291 other.unApplySimilarityOnMyEdgesIfNotAlreadyHit(xBary,yBary,fact);
294 double ComposedEdge::normalizeExt(ComposedEdge *other, double& xBary, double& yBary)
297 b.prepareForAggregation();
299 other->fillBounds(b);
300 double dimChar=b.getCaracteristicDim();
301 b.getBarycenter(xBary,yBary);
302 applyGlobalSimilarity2(other,xBary,yBary,dimChar);
306 void ComposedEdge::dumpInXfigFile(std::ostream& stream, int resolution, const Bounds& box) const
308 stream.precision(10);
309 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
310 (*iter)->dumpInXfigFile(stream,resolution,box);
313 Node *ComposedEdge::getEndNode() const
315 return _sub_edges.back()->getEndNode();
318 Node *ComposedEdge::getStartNode() const
320 return _sub_edges.front()->getStartNode();
323 bool ComposedEdge::changeEndNodeWith(Node *node) const
325 return _sub_edges.back()->changeEndNodeWith(node);
328 bool ComposedEdge::changeStartNodeWith(Node *node) const
330 return _sub_edges.front()->changeStartNodeWith(node);
333 void ComposedEdge::fillBounds(Bounds& output) const
335 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
336 (*iter)->fillBounds(output);
340 * \b WARNING : applies similarity \b ONLY on edges without any change on Nodes. To perform a global similarity call applyGlobalSimilarity.
342 void ComposedEdge::applySimilarity(double xBary, double yBary, double dimChar)
344 for(std::list<ElementaryEdge *>::iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
345 (*iter)->applySimilarity(xBary,yBary,dimChar);
349 * Perform Similarity transformation on all elements of this Nodes and Edges.
351 void ComposedEdge::applyGlobalSimilarity(double xBary, double yBary, double dimChar)
353 std::set<Node *> allNodes;
354 getAllNodes(allNodes);
355 for(std::set<Node *>::iterator iter=allNodes.begin();iter!=allNodes.end();iter++)
356 (*iter)->applySimilarity(xBary,yBary,dimChar);
357 for(std::list<ElementaryEdge *>::iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
358 (*iter)->applySimilarity(xBary,yBary,dimChar);
362 * Perform Similarity transformation on all elements of this Nodes and Edges on 'this' and 'other'.
363 * Nodes can be shared between 'this' and 'other'.
365 void ComposedEdge::applyGlobalSimilarity2(ComposedEdge *other, double xBary, double yBary, double dimChar)
368 other->initNodeHitStatus();
369 applySimilarityOnMyNodes(xBary,yBary,dimChar);
370 other->applySimilarityOnMyNodesIfNotAlreadyHit(xBary,yBary,dimChar);
372 other->initEdgeHitStatus();
373 applySimilarityOnMyEdges(xBary,yBary,dimChar);
374 other->applySimilarityOnMyEdgesIfNotAlreadyHit(xBary,yBary,dimChar);
378 * This method append to param 'partConsidered' the part of length of subedges IN or ON.
379 * @param partConsidered INOUT param.
381 void ComposedEdge::dispatchPerimeter(double& partConsidered) const
383 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
385 TypeOfEdgeLocInPolygon loc=(*iter)->getLoc();
386 if(loc==FULL_IN_1 || loc==FULL_ON_1)
387 partConsidered+=(*iter)->getCurveLength();
392 * Idem dispatchPerimeterExcl except that when a subedge is declared as ON this subedge is counted in commonPart.
394 void ComposedEdge::dispatchPerimeterExcl(double& partConsidered, double& commonPart) const
396 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
398 TypeOfEdgeLocInPolygon loc=(*iter)->getLoc();
400 partConsidered+=(*iter)->getCurveLength();
402 commonPart+=(*iter)->getCurveLength();
406 void ComposedEdge::getAllNodes(std::set<Node *>& output) const
408 std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();
409 for(;iter!=_sub_edges.end();iter++)
410 (*iter)->getAllNodes(output);
413 void ComposedEdge::initNodeHitStatus() const
415 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
417 (*iter)->getStartNode()->initHitStatus();
418 (*iter)->getEndNode()->initHitStatus();
422 void ComposedEdge::applySimilarityOnMyNodes(double xBary, double yBary, double dimChar) const
424 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
426 (*iter)->getStartNode()->hitMeAlone(xBary,yBary,dimChar);
427 (*iter)->getEndNode()->hitMeAlone(xBary,yBary,dimChar);
431 void ComposedEdge::unApplySimilarityOnMyNodes(double xBary, double yBary, double dimChar) const
433 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
435 (*iter)->getStartNode()->unHitMeAlone(xBary,yBary,dimChar);
436 (*iter)->getEndNode()->unHitMeAlone(xBary,yBary,dimChar);
440 void ComposedEdge::applySimilarityOnMyNodesIfNotAlreadyHit(double xBary, double yBary, double dimChar) const
442 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
444 (*iter)->getStartNode()->hitMeAfter(xBary,yBary,dimChar);
445 (*iter)->getEndNode()->hitMeAfter(xBary,yBary,dimChar);
449 void ComposedEdge::unApplySimilarityOnMyNodesIfNotAlreadyHit(double xBary, double yBary, double dimChar) const
451 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
453 (*iter)->getStartNode()->unHitMeAfter(xBary,yBary,dimChar);
454 (*iter)->getEndNode()->unHitMeAfter(xBary,yBary,dimChar);
458 void ComposedEdge::initEdgeHitStatus() const
460 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
461 (*iter)->getPtr()->initHitStatus();
464 void ComposedEdge::applySimilarityOnMyEdges(double xBary, double yBary, double dimChar) const
466 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
467 (*iter)->getPtr()->hitMeAlone(xBary,yBary,dimChar);
470 void ComposedEdge::unApplySimilarityOnMyEdges(double xBary, double yBary, double dimChar) const
472 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
473 (*iter)->getPtr()->unHitMeAlone(xBary,yBary,dimChar);
476 void ComposedEdge::applySimilarityOnMyEdgesIfNotAlreadyHit(double xBary, double yBary, double dimChar) const
478 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
479 (*iter)->getPtr()->hitMeAfter(xBary,yBary,dimChar);
482 void ComposedEdge::unApplySimilarityOnMyEdgesIfNotAlreadyHit(double xBary, double yBary, double dimChar) const
484 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
485 (*iter)->getPtr()->unHitMeAfter(xBary,yBary,dimChar);
488 void ComposedEdge::getBarycenter(double *bary, double& weigh) const
490 weigh=0.; bary[0]=0.; bary[1]=0.;
492 for(std::list<ElementaryEdge *>::const_iterator iter=_sub_edges.begin();iter!=_sub_edges.end();iter++)
494 (*iter)->getBarycenter(tmp2,tmp1);
496 bary[0]+=tmp1*tmp2[0];
497 bary[1]+=tmp1*tmp2[1];
504 * This method makes the hypothesis that \a nodeToTest can be either IN or OUT.
506 * \sa ComposedEdge::isInOrOut2
508 bool ComposedEdge::isInOrOut(Node *nodeToTest) const
510 Bounds b; b.prepareForAggregation();
512 if(b.nearlyWhere((*nodeToTest)[0],(*nodeToTest)[1])==OUT)
514 std::set< IntersectElement > inOutSwitch;
515 std::set<Node *> nodes;
517 double ref(isInOrOutAlg(nodeToTest,nodes,inOutSwitch));
519 for(std::set< IntersectElement >::iterator iter4=inOutSwitch.begin();iter4!=inOutSwitch.end();iter4++)
521 if((*iter4).getVal1()<ref)
523 if((*iter4).getNodeOnly()->getLoc()==ON_1)
533 * This method is close to ComposedEdge::isInOrOut behaviour except that here EPSILON is taken into account to detect if it is IN or OUT.
534 * If \a nodeToTest is close to an edge in \a this, true will be returned even if it is outside informatically from \a this.
536 * \sa ComposedEdge::isInOrOut
538 bool ComposedEdge::isInOrOut2(Node *nodeToTest) const
540 std::set< IntersectElement > inOutSwitch;
541 std::set<Node *> nodes;
543 for(std::set<Node *>::const_iterator iter=nodes.begin();iter!=nodes.end();iter++)
544 if(sqrt((*iter)->distanceWithSq(*nodeToTest))<QuadraticPlanarPrecision::getPrecision())
546 double ref(isInOrOutAlg(nodeToTest,nodes,inOutSwitch));
548 for(std::set< IntersectElement >::iterator iter4=inOutSwitch.begin();iter4!=inOutSwitch.end();iter4++)
550 double val((*iter4).getVal1());
551 if(fabs(val-ref)>=QuadraticPlanarPrecision::getPrecision())
555 if((*iter4).getNodeOnly()->getLoc()==ON_1)
567 double ComposedEdge::isInOrOutAlg(Node *nodeToTest, const std::set<Node*>& nodes, std::set< IntersectElement >& inOutSwitch) const
570 std::set<double> radialDistributionOfNodes;
571 std::set<Node *>::const_iterator iter;
572 for(iter=nodes.begin();iter!=nodes.end();iter++)
573 radialDistributionOfNodes.insert(nodeToTest->getSlope(*(*iter)));
574 std::vector<double> radialDistrib(radialDistributionOfNodes.begin(),radialDistributionOfNodes.end());
575 radialDistributionOfNodes.clear();
576 std::vector<double> radialDistrib2(radialDistrib.size());
577 copy(radialDistrib.begin()+1,radialDistrib.end(),radialDistrib2.begin());
578 radialDistrib2.back()=M_PI+radialDistrib.front();
579 std::vector<double> radialDistrib3(radialDistrib.size());
580 std::transform(radialDistrib2.begin(),radialDistrib2.end(),radialDistrib.begin(),radialDistrib3.begin(),std::minus<double>());
581 std::vector<double>::iterator iter3=max_element(radialDistrib3.begin(),radialDistrib3.end());
582 int i=iter3-radialDistrib3.begin();
583 // ok for e1 - Let's go.
584 EdgeInfLin *e1=new EdgeInfLin(nodeToTest,radialDistrib[i]+radialDistrib3[i]/2.);
585 double ref=e1->getCharactValue(*nodeToTest);
586 for(std::list<ElementaryEdge *>::const_iterator iter4=_sub_edges.begin();iter4!=_sub_edges.end();iter4++)
588 ElementaryEdge *val=(*iter4);
591 Edge *e=val->getPtr();
592 std::auto_ptr<EdgeIntersector> intersc(Edge::BuildIntersectorWith(e1,e));
593 bool obviousNoIntersection,areOverlapped;
594 intersc->areOverlappedOrOnlyColinears(0,obviousNoIntersection,areOverlapped); // first parameter never used
595 if(obviousNoIntersection)
601 std::list< IntersectElement > listOfIntesc=intersc->getIntersectionsCharacteristicVal();
602 for(std::list< IntersectElement >::iterator iter2=listOfIntesc.begin();iter2!=listOfIntesc.end();iter2++)
603 if((*iter2).isIncludedByBoth())
604 inOutSwitch.insert(*iter2);
606 //if overlapped we can forget
609 throw Exception("Invalid use of ComposedEdge::isInOrOutAlg : only one level supported !");
615 bool ComposedEdge::getDirection() const
617 throw Exception("ComposedEdge::getDirection : no sense");
620 bool ComposedEdge::intresincEqCoarse(const Edge *other) const
622 if(_sub_edges.size()!=1)
624 return _sub_edges.front()->intresincEqCoarse(other);
627 void ComposedEdge::clearAll(std::list<ElementaryEdge *>::iterator startToDel)
629 for(std::list<ElementaryEdge *>::iterator iter=startToDel;iter!=_sub_edges.end();iter++)