1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_MAT2d.cxx
23 // Created : Thu May 28 17:49:53 2015
24 // Author : Edward AGAPOV (eap)
26 #include "SMESH_MAT2d.hxx"
30 #include <BRepAdaptor_CompCurve.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Bnd_B2d.hxx>
36 //#include <GCPnts_AbscissaPoint.hxx>
37 #include <GCPnts_TangentialDeflection.hxx>
38 // #include <GCPnts_UniformAbscissa.hxx>
39 // #include <GCPnts_UniformDeflection.hxx>
40 #include <Geom2d_Curve.hxx>
41 //#include <GeomAdaptor_Curve.hxx>
42 #include <Geom2dAdaptor_Curve.hxx>
43 #include <Geom_Curve.hxx>
44 #include <Geom_Surface.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Wire.hxx>
51 #include "SMESH_File.hxx"
52 #include "SMESH_Comment.hxx"
56 using boost::polygon::x;
57 using boost::polygon::y;
58 using SMESH_MAT2d::TVD;
59 using SMESH_MAT2d::TVDEdge;
60 using SMESH_MAT2d::TVDCell;
61 using SMESH_MAT2d::TVDVertex;
65 // Input data for construct_voronoi()
66 // -------------------------------------------------------------------------------------
70 int _a, _b; // coordinates
71 double _param; // param on EDGE
72 InPoint(int x, int y, double param) : _a(x), _b(y), _param(param) {}
73 InPoint() : _a(0), _b(0), _param(0) {}
76 list< const TVDEdge* > _edges; // MA edges of a concave InPoint in CCW order
78 size_t index( const vector< InPoint >& inPoints ) const { return this - &inPoints[0]; }
79 bool operator==( const InPoint& other ) const { return _a == other._a && _b == other._b; }
80 bool operator==( const TVDVertex* v ) const { return ( Abs( _a - v->x() ) < 1. &&
81 Abs( _b - v->y() ) < 1. ); }
83 // -------------------------------------------------------------------------------------
91 size_t _geomEdgeInd; // EDGE index within the FACE
93 list< const TVDEdge* > _edges; // MA edges in CCW order within _cell
95 InSegment( InPoint * p0, InPoint * p1, size_t iE)
96 : _p0(p0), _p1(p1), _geomEdgeInd(iE) {}
97 InSegment() : _p0(0), _p1(0), _geomEdgeInd(0) {}
99 const InPoint& point0() const { return *_p0; }
100 const InPoint& point1() const { return *_p1; }
102 inline bool isConnected( const TVDEdge* edge );
104 inline bool isExternal( const TVDEdge* edge );
106 static void setGeomEdgeToCell( const TVDCell* cell, size_t eID ) { cell->color( eID ); }
108 static size_t getGeomEdge( const TVDCell* cell ) { return cell->color(); }
111 // check if a TVDEdge begins at my end or ends at my start
112 inline bool InSegment::isConnected( const TVDEdge* edge )
114 return (( edge->vertex0() && edge->vertex1() )
116 ((Abs( edge->vertex0()->x() - _p1->_a ) < 1.&&
117 Abs( edge->vertex0()->y() - _p1->_b ) < 1. ) ||
118 (Abs( edge->vertex1()->x() - _p0->_a ) < 1.&&
119 Abs( edge->vertex1()->y() - _p0->_b ) < 1. )));
122 // check if a MA TVDEdge is outside of a domain
123 inline bool InSegment::isExternal( const TVDEdge* edge )
125 double dot = // x1*x2 + y1*y2; (x1,y1) - internal normal of InSegment
126 ( _p0->_b - _p1->_b ) * ( 0.5 * ( edge->vertex0()->x() + edge->vertex1()->x() ) - _p0->_a ) +
127 ( _p1->_a - _p0->_a ) * ( 0.5 * ( edge->vertex0()->y() + edge->vertex1()->y() ) - _p0->_b );
131 // // -------------------------------------------------------------------------------------
132 // const size_t theExternMA = 111; // to mark external MA edges
134 // bool isExternal( const TVDEdge* edge )
136 // return ( SMESH_MAT2d::Branch::getBndSegment( edge ) == theExternMA );
139 // // mark external MA edges
140 // void markExternalEdges( const TVDEdge* edge )
142 // if ( isExternal( edge ))
144 // SMESH_MAT2d::Branch::setBndSegment( theExternMA, edge );
145 // SMESH_MAT2d::Branch::setBndSegment( theExternMA, edge->twin() );
146 // if ( edge->is_primary() && edge->vertex1() )
148 // const TVDVertex * v = edge->vertex1();
149 // edge = v->incident_edge();
151 // markExternalEdges( edge );
152 // edge = edge->rot_next();
153 // } while ( edge != v->incident_edge() );
157 // -------------------------------------------------------------------------------------
159 // writes segments into a txt file readable by voronoi_visualizer
160 void inSegmentsToFile( vector< InSegment>& inSegments)
162 if ( inSegments.size() > 1000 )
164 const char* fileName = "/misc/dn25/salome/eap/salome/misc/Code/C++/MAdebug.txt";
165 SMESH_File file(fileName, false );
167 file.openForWriting();
169 text << "0\n"; // nb points
170 text << inSegments.size() << "\n"; // nb segments
171 for ( size_t i = 0; i < inSegments.size(); ++i )
173 text << inSegments[i]._p0->_a << " "
174 << inSegments[i]._p0->_b << " "
175 << inSegments[i]._p1->_a << " "
176 << inSegments[i]._p1->_b << "\n";
179 file.write( text.c_str(), text.size() );
180 cout << "Write " << fileName << endl;
182 void dumpEdge( const TVDEdge* edge )
184 cout << "*Edge_" << edge;
185 if ( !edge->vertex0() )
186 cout << " ( INF, INF";
188 cout << " ( " << edge->vertex0()->x() << ", " << edge->vertex0()->y();
189 if ( !edge->vertex1() )
190 cout << ") -> ( INF, INF";
192 cout << ") -> ( " << edge->vertex1()->x() << ", " << edge->vertex1()->y();
193 cout << ")\t cell=" << edge->cell()
194 << " iBnd=" << edge->color()
195 << " twin=" << edge->twin()
196 << " twin_cell=" << edge->twin()->cell()
197 << " prev=" << edge->prev() << " next=" << edge->next()
198 << ( edge->is_primary() ? " MA " : " SCND" )
199 << ( edge->is_linear() ? " LIN " : " CURV" )
202 void dumpCell( const TVDCell* cell )
204 cout << "**Cell_" << cell << " GEOM=" << cell->color() << " ";
205 cout << ( cell->contains_segment() ? " SEG " : " PNT " );
206 if ( cell-> is_degenerate() )
211 const TVDEdge* edge = cell->incident_edge();
215 cout << " - " << ++i << " ";
217 } while (edge != cell->incident_edge());
221 void inSegmentsToFile( vector< InSegment>& inSegments) {}
222 void dumpEdge( const TVDEdge* edge ) {}
223 void dumpCell( const TVDCell* cell ) {}
226 // -------------------------------------------------------------------------------------
232 struct geometry_concept<InPoint> {
233 typedef point_concept type;
236 struct point_traits<InPoint> {
237 typedef int coordinate_type;
239 static inline coordinate_type get(const InPoint& point, orientation_2d orient) {
240 return (orient == HORIZONTAL) ? point._a : point._b;
245 struct geometry_concept<InSegment> {
246 typedef segment_concept type;
250 struct segment_traits<InSegment> {
251 typedef int coordinate_type;
252 typedef InPoint point_type;
254 static inline point_type get(const InSegment& segment, direction_1d dir) {
255 return *(dir.to_int() ? segment._p1 : segment._p0);
258 } // namespace polygon
260 // -------------------------------------------------------------------------------------
264 const int theNoBrachID = 0;
265 double theScale[2]; // scale used in bndSegsToMesh()
266 const size_t theNoEdgeID = std::numeric_limits<size_t>::max() / 1000;
268 // -------------------------------------------------------------------------------------
270 * \brief Intermediate DS to create InPoint's
276 UVU( gp_Pnt2d uv, double u ): _uv(uv), _u(u) {}
277 InPoint getInPoint( double scale[2] )
279 return InPoint( int( _uv.X() * scale[0]), int( _uv.Y() * scale[1]), _u );
282 // -------------------------------------------------------------------------------------
284 * \brief Segment of EDGE, used to create BndPoints
289 const TVDEdge* _edge;
291 BndSeg* _prev; // previous BndSeg in FACE boundary
292 int _branchID; // negative ID means reverse direction
294 BndSeg( InSegment* seg, const TVDEdge* edge, double u ):
295 _inSeg(seg), _edge(edge), _uLast(u), _prev(0), _branchID( theNoBrachID ) {}
297 void setIndexToEdge( size_t id )
299 SMESH_MAT2d::Branch::setBndSegment( id, _edge );
302 int branchID() const { return Abs( _branchID ); }
304 size_t geomEdge() const { return _inSeg->_geomEdgeInd; }
306 static BndSeg* getBndSegOfEdge( const TVDEdge* edge,
307 vector< vector< BndSeg > >& bndSegsPerEdge )
312 size_t oppSegIndex = SMESH_MAT2d::Branch::getBndSegment( edge );
313 size_t oppEdgeIndex = SMESH_MAT2d::Branch::getGeomEdge ( edge );
314 if ( oppEdgeIndex < bndSegsPerEdge.size() &&
315 oppSegIndex < bndSegsPerEdge[ oppEdgeIndex ].size() )
317 seg = & bndSegsPerEdge[ oppEdgeIndex ][ oppSegIndex ];
323 void setBranch( int branchID, vector< vector< BndSeg > >& bndSegsPerEdge )
325 _branchID = branchID;
327 // pass branch to an opposite BndSeg
329 if ( BndSeg* oppSeg = getBndSegOfEdge( _edge->twin(), bndSegsPerEdge ))
331 if ( oppSeg->_branchID == theNoBrachID )
332 oppSeg->_branchID = -branchID;
335 bool hasOppositeEdge()
337 if ( !_edge ) return false;
338 return ( _inSeg->getGeomEdge( _edge->twin()->cell() ) != theNoEdgeID );
341 // check a next segment in CW order
342 bool isSameBranch( const BndSeg& seg2 )
344 if ( !_edge || !seg2._edge )
347 const TVDCell* cell1 = this->_edge->twin()->cell();
348 const TVDCell* cell2 = seg2. _edge->twin()->cell();
349 if ( cell1 == cell2 )
352 const TVDEdge* edgeMedium1 = this->_edge->twin()->next();
353 const TVDEdge* edgeMedium2 = seg2. _edge->twin()->prev();
355 if ( edgeMedium1->is_secondary() && edgeMedium2->is_secondary() )
357 if ( edgeMedium1->twin() == edgeMedium2 )
359 // edgeMedium's are edges whose twin()->cell is built on an end point of inSegment
360 // and is located between cell1 and cell2
361 if ( edgeMedium1->twin() == edgeMedium2->twin() ) // is this possible???
363 if ( edgeMedium1->twin() == edgeMedium2->twin()->next() &&
364 edgeMedium1->twin()->cell()->contains_point() )
367 else if ( edgeMedium1->is_primary() && edgeMedium2->is_primary() )
369 if ( edgeMedium1->twin() == edgeMedium2 &&
370 SMESH_MAT2d::Branch::getBndSegment( edgeMedium1 ) ==
371 SMESH_MAT2d::Branch::getBndSegment( edgeMedium2 ))
372 // this is an ignored MA edge between inSegment's on one EDGE forming a convex corner
380 // -------------------------------------------------------------------------------------
384 struct BranchIterator
387 const std::vector<const TVDEdge*> & _edges;
390 BranchIterator(const std::vector<const TVDEdge*> & edges, int i )
391 :_i( i ), _size( edges.size() ), _edges( edges )
393 _closed = ( edges[0]->vertex1() == edges.back()->vertex0() ); // closed branch
395 const TVDEdge* operator++() { ++_i; return edge(); }
396 const TVDEdge* operator--() { --_i; return edge(); }
397 bool operator<( const BranchIterator& other ) { return _i < other._i; }
398 BranchIterator& operator=( const BranchIterator& other ) { _i = other._i; return *this; }
399 void set(int i) { _i = i; }
400 int index() const { return _i; }
401 int indexMod() const { return ( _i + _size ) % _size; }
402 const TVDEdge* edge() const {
403 return _closed ? _edges[ indexMod() ] : ( _i < 0 || _i >= _size ) ? 0 : _edges[ _i ];
405 const TVDEdge* edgePrev() { --_i; const TVDEdge* e = edge(); ++_i; return e; }
406 const TVDEdge* edgeNext() { ++_i; const TVDEdge* e = edge(); --_i; return e; }
409 //================================================================================
411 * \brief debug: to visually check found MA edges
413 //================================================================================
415 void bndSegsToMesh( const vector< BndSeg >& bndSegs )
418 if ( !getenv("bndSegsToMesh")) return;
419 map< const TVDVertex *, int > v2Node;
420 map< const TVDVertex *, int >::iterator v2n;
421 set< const TVDEdge* > addedEdges;
423 const char* fileName = "/misc/dn25/salome/eap/salome/misc/Code/C++/MAedges.py";
424 SMESH_File file(fileName, false );
426 file.openForWriting();
428 text << "import salome, SMESH\n";
429 text << "salome.salome_init()\n";
430 text << "from salome.smesh import smeshBuilder\n";
431 text << "smesh = smeshBuilder.New(salome.myStudy)\n";
432 text << "m=smesh.Mesh()\n";
433 for ( size_t i = 0; i < bndSegs.size(); ++i )
435 if ( !bndSegs[i]._edge )
436 text << "# " << i << " NULL edge\n";
437 else if ( !bndSegs[i]._edge->vertex0() ||
438 !bndSegs[i]._edge->vertex1() )
439 text << "# " << i << " INFINITE edge\n";
440 else if ( addedEdges.insert( bndSegs[i]._edge ).second &&
441 addedEdges.insert( bndSegs[i]._edge->twin() ).second )
443 v2n = v2Node.insert( make_pair( bndSegs[i]._edge->vertex0(), v2Node.size() + 1 )).first;
444 int n0 = v2n->second;
445 if ( n0 == v2Node.size() )
446 text << "n" << n0 << " = m.AddNode( "
447 << bndSegs[i]._edge->vertex0()->x() / theScale[0] << ", "
448 << bndSegs[i]._edge->vertex0()->y() / theScale[1] << ", 0 )\n";
450 v2n = v2Node.insert( make_pair( bndSegs[i]._edge->vertex1(), v2Node.size() + 1 )).first;
451 int n1 = v2n->second;
452 if ( n1 == v2Node.size() )
453 text << "n" << n1 << " = m.AddNode( "
454 << bndSegs[i]._edge->vertex1()->x() / theScale[0] << ", "
455 << bndSegs[i]._edge->vertex1()->y() / theScale[1] << ", 0 )\n";
457 text << "e" << i << " = m.AddEdge([ n" << n0 << ", n" << n1 << " ])\n";
461 file.write( text.c_str(), text.size() );
462 cout << "execfile( '" << fileName << "')" << endl;
466 //================================================================================
468 * \brief Computes length of a TVDEdge
470 //================================================================================
472 double length( const TVDEdge* edge )
474 gp_XY d( edge->vertex0()->x() - edge->vertex1()->x(),
475 edge->vertex0()->y() - edge->vertex1()->y() );
479 //================================================================================
481 * \brief Compute scale to have the same 2d proportions as in 3d
483 //================================================================================
485 void computeProportionScale( const TopoDS_Face& face,
486 const Bnd_B2d& uvBox,
489 scale[0] = scale[1] = 1.;
490 if ( uvBox.IsVoid() ) return;
493 Handle(Geom_Surface) surface = BRep_Tool::Surface( face, loc );
495 const int nbDiv = 30;
496 gp_XY uvMin = uvBox.CornerMin(), uvMax = uvBox.CornerMax();
497 gp_XY uvMid = 0.5 * ( uvMin + uvMax );
498 double du = ( uvMax.X() - uvMin.X() ) / nbDiv;
499 double dv = ( uvMax.Y() - uvMin.Y() ) / nbDiv;
501 double uLen3d = 0, vLen3d = 0;
502 gp_Pnt uPrevP = surface->Value( uvMin.X(), uvMid.Y() );
503 gp_Pnt vPrevP = surface->Value( uvMid.X(), uvMin.Y() );
504 for (int i = 1; i <= nbDiv; i++)
506 double u = uvMin.X() + du * i;
507 double v = uvMin.Y() + dv * i;
508 gp_Pnt uP = surface->Value( u, uvMid.Y() );
509 gp_Pnt vP = surface->Value( uvMid.X(), v );
510 uLen3d += uP.Distance( uPrevP );
511 vLen3d += vP.Distance( vPrevP );
515 scale[0] = uLen3d / ( uvMax.X() - uvMin.X() );
516 scale[1] = vLen3d / ( uvMax.Y() - uvMin.Y() );
519 //================================================================================
521 * \brief Fill input data for construct_voronoi()
523 //================================================================================
525 bool makeInputData(const TopoDS_Face& face,
526 const std::vector< TopoDS_Edge >& edges,
527 const double minSegLen,
528 vector< InPoint >& inPoints,
529 vector< InSegment>& inSegments,
532 const double theDiscrCoef = 0.5; // to decrease minSegLen for discretization
535 // discretize the EDGEs to get 2d points and segments
537 vector< vector< UVU > > uvuVec( edges.size() );
539 for ( size_t iE = 0; iE < edges.size(); ++iE )
541 vector< UVU > & points = uvuVec[ iE ];
544 Handle(Geom_Curve) c3d = BRep_Tool::Curve ( edges[ iE ], loc, f, l );
545 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface( edges[ iE ], face, f, l );
546 if ( c2d.IsNull() ) return false;
548 points.push_back( UVU( c2d->Value( f ), f ));
549 uvBox.Add( points.back()._uv );
551 Geom2dAdaptor_Curve c2dAdaptor (c2d, f,l );
552 double curDeflect = 0.3; //0.01; //Curvature deflection
553 double angDeflect = 0.2; // 0.09; //Angular deflection
555 GCPnts_TangentialDeflection discret(c2dAdaptor, angDeflect, curDeflect);
556 // if ( discret.NbPoints() > 2 )
561 // discret.Initialize( c2dAdaptor, 100, curDeflect );
562 // cout << "C " << curDeflect << " " << discret.NbPoints() << endl;
563 // curDeflect *= 1.5;
565 // while ( discret.NbPoints() > 5 );
569 // discret.Initialize( c2dAdaptor, angDeflect, 100 );
570 // cout << "A " << angDeflect << " " << discret.NbPoints() << endl;
571 // angDeflect *= 1.5;
573 // while ( discret.NbPoints() > 5 );
577 pPrev = c3d->Value( f );
578 if ( discret.NbPoints() > 2 )
579 for ( int i = 2; i <= discret.NbPoints(); i++ ) // skip the 1st point
581 double u = discret.Parameter(i);
585 int nbDiv = int( p.Distance( pPrev ) / minSegLen / theDiscrCoef );
586 double dU = ( u - points.back()._u ) / nbDiv;
587 for ( int iD = 1; iD < nbDiv; ++iD )
589 double uD = points.back()._u + dU;
590 points.push_back( UVU( c2d->Value( uD ), uD ));
594 points.push_back( UVU( c2d->Value( u ), u ));
595 uvBox.Add( points.back()._uv );
597 // if ( !c3d.IsNull() )
599 // vector<double> params;
600 // GeomAdaptor_Curve c3dAdaptor( c3d,f,l );
603 // const double deflection = minSegLen * 0.1;
604 // GCPnts_UniformDeflection discret( c3dAdaptor, deflection, f, l, true );
605 // if ( !discret.IsDone() )
607 // int nbP = discret.NbPoints();
608 // for ( int i = 2; i < nbP; i++ ) // skip 1st and last points
609 // params.push_back( discret.Parameter(i) );
613 // double eLen = GCPnts_AbscissaPoint::Length( c3dAdaptor );
614 // int nbSeg = Max( 1, int( eLen / minSegLen / theDiscrCoef ));
615 // double segLen = eLen / nbSeg;
616 // GCPnts_UniformAbscissa discret( c3dAdaptor, segLen, f, l );
617 // int nbP = Min( discret.NbPoints(), nbSeg + 1 );
618 // for ( int i = 2; i < nbP; i++ ) // skip 1st and last points
619 // params.push_back( discret.Parameter(i) );
621 // for ( size_t i = 0; i < params.size(); ++i )
623 // points.push_back( UVU( c2d->Value( params[i] ), params[i] ));
624 // uvBox.Add( points.back()._uv );
627 if ( points.size() < 2 )
629 points.push_back( UVU( c2d->Value( l ), l ));
630 uvBox.Add( points.back()._uv );
632 if ( edges[ iE ].Orientation() == TopAbs_REVERSED )
633 std::reverse( points.begin(), points.end() );
636 // make connected EDGEs have same UV at shared VERTEX
637 TopoDS_Vertex vShared;
638 for ( size_t iE = 0; iE < edges.size(); ++iE )
640 size_t iE2 = (iE+1) % edges.size();
641 if ( !TopExp::CommonVertex( edges[iE], edges[iE2], vShared ))
643 if ( !vShared.IsSame( TopExp::LastVertex( edges[iE], true )))
645 vector< UVU > & points1 = uvuVec[ iE ];
646 vector< UVU > & points2 = uvuVec[ iE2 ];
647 gp_Pnt2d & uv1 = points1.back() ._uv;
648 gp_Pnt2d & uv2 = points2.front()._uv;
649 uv1 = uv2 = 0.5 * ( uv1.XY() + uv2.XY() );
652 // get scale to have the same 2d proportions as in 3d
653 computeProportionScale( face, uvBox, scale );
655 // make scale to have coordinates precise enough when converted to int
657 gp_XY uvMin = uvBox.CornerMin(), uvMax = uvBox.CornerMax();
658 uvMin.ChangeCoord(1) = uvMin.X() * scale[0];
659 uvMin.ChangeCoord(2) = uvMin.Y() * scale[1];
660 uvMax.ChangeCoord(1) = uvMax.X() * scale[0];
661 uvMax.ChangeCoord(2) = uvMax.Y() * scale[1];
662 double vMax[2] = { Max( Abs( uvMin.X() ), Abs( uvMax.X() )),
663 Max( Abs( uvMin.Y() ), Abs( uvMax.Y() )) };
664 int iMax = ( vMax[0] > vMax[1] ) ? 0 : 1;
665 const double precision = 1e-5;
666 double preciScale = Min( vMax[iMax] / precision,
667 std::numeric_limits<int>::max() / vMax[iMax] );
668 preciScale /= scale[iMax];
669 double roundedScale = 10; // to ease debug
670 while ( roundedScale * 10 < preciScale )
672 scale[0] *= roundedScale;
673 scale[1] *= roundedScale;
675 // create input points and segments
680 for ( size_t iE = 0; iE < uvuVec.size(); ++iE )
681 nbPnt += uvuVec[ iE ].size();
682 inPoints.resize( nbPnt );
683 inSegments.reserve( nbPnt );
686 if ( face.Orientation() == TopAbs_REVERSED )
688 for ( int iE = uvuVec.size()-1; iE >= 0; --iE )
690 vector< UVU > & points = uvuVec[ iE ];
691 inPoints[ iP++ ] = points.back().getInPoint( scale );
692 for ( size_t i = points.size()-1; i >= 1; --i )
694 inPoints[ iP++ ] = points[i-1].getInPoint( scale );
695 inSegments.push_back( InSegment( & inPoints[ iP-2 ], & inPoints[ iP-1 ], iE ));
701 for ( size_t iE = 0; iE < uvuVec.size(); ++iE )
703 vector< UVU > & points = uvuVec[ iE ];
704 inPoints[ iP++ ] = points[0].getInPoint( scale );
705 for ( size_t i = 1; i < points.size(); ++i )
707 inPoints[ iP++ ] = points[i].getInPoint( scale );
708 inSegments.push_back( InSegment( & inPoints[ iP-2 ], & inPoints[ iP-1 ], iE ));
713 theScale[0] = scale[0];
714 theScale[1] = scale[1];
719 //================================================================================
721 * \brief Update a branch joined to another one
723 //================================================================================
725 void updateJoinedBranch( vector< const TVDEdge* > & branchEdges,
727 vector< vector< BndSeg > > & bndSegs,
733 for ( size_t i = 0; i < branchEdges.size(); ++i )
735 if (( seg1 = BndSeg::getBndSegOfEdge( branchEdges[i], bndSegs )) &&
736 ( seg2 = BndSeg::getBndSegOfEdge( branchEdges[i]->twin(), bndSegs )))
738 seg1->_branchID /= seg1->branchID();
739 seg2->_branchID /= seg2->branchID();
740 seg1->_branchID *= -newID;
741 seg2->_branchID *= -newID;
742 branchEdges[i] = branchEdges[i]->twin();
745 std::reverse( branchEdges.begin(), branchEdges.end() );
749 for ( size_t i = 0; i < branchEdges.size(); ++i )
751 if (( seg1 = BndSeg::getBndSegOfEdge( branchEdges[i], bndSegs )) &&
752 ( seg2 = BndSeg::getBndSegOfEdge( branchEdges[i]->twin(), bndSegs )))
754 seg1->_branchID /= seg1->branchID();
755 seg2->_branchID /= seg2->branchID();
756 seg1->_branchID *= newID;
757 seg2->_branchID *= newID;
763 //================================================================================
765 * \brief Create MA branches and FACE boundary data
766 * \param [in] vd - voronoi diagram of \a inSegments
767 * \param [in] inPoints - FACE boundary points
768 * \param [in,out] inSegments - FACE boundary segments
769 * \param [out] branch - MA branches to fill
770 * \param [out] branchEnd - ends of MA branches to fill
771 * \param [out] boundary - FACE boundary to fill
773 //================================================================================
775 void makeMA( const TVD& vd,
776 const bool ignoreCorners,
777 vector< InPoint >& inPoints,
778 vector< InSegment > & inSegments,
779 vector< SMESH_MAT2d::Branch >& branch,
780 vector< const SMESH_MAT2d::BranchEnd* >& branchPnt,
781 SMESH_MAT2d::Boundary& boundary )
783 // Associate MA cells with geom EDGEs
784 for (TVD::const_cell_iterator it = vd.cells().begin(); it != vd.cells().end(); ++it)
786 const TVDCell* cell = &(*it);
787 if ( cell->contains_segment() )
789 InSegment& seg = inSegments[ cell->source_index() ];
791 seg.setGeomEdgeToCell( cell, seg._geomEdgeInd );
795 InSegment::setGeomEdgeToCell( cell, theNoEdgeID );
799 vector< bool > inPntChecked( inPoints.size(), false );
801 // Find MA edges of each inSegment
803 for ( size_t i = 0; i < inSegments.size(); ++i )
805 InSegment& inSeg = inSegments[i];
807 // get edges around the cell lying on MA
808 bool hasSecondary = false;
809 const TVDEdge* edge = inSeg._cell->incident_edge();
811 edge = edge->next(); // Returns the CCW next edge within the cell.
812 if ( edge->is_primary() && !inSeg.isExternal( edge ) )
813 inSeg._edges.push_back( edge ); // edge equidistant from two InSegments
816 } while (edge != inSeg._cell->incident_edge());
818 // there can be several continuous MA edges but maEdges can begin in the middle of
819 // a chain of continuous MA edges. Make the chain continuous.
820 list< const TVDEdge* >& maEdges = inSeg._edges;
821 if ( maEdges.empty() )
824 while ( maEdges.back()->next() == maEdges.front() )
825 maEdges.splice( maEdges.end(), maEdges, maEdges.begin() );
827 // remove maEdges equidistant from two neighbor InSegments of the same geom EDGE
828 list< const TVDEdge* >::iterator e = maEdges.begin();
829 while ( e != maEdges.end() )
831 const TVDCell* cell2 = (*e)->twin()->cell(); // cell on the other side of a MA edge
832 size_t geoE2 = InSegment::getGeomEdge( cell2 );
833 bool toRemove = ( inSeg._geomEdgeInd == geoE2 && inSeg.isConnected( *e ));
835 e = maEdges.erase( e );
839 if ( maEdges.empty() )
842 // add MA edges corresponding to concave InPoints
843 for ( int is2nd = 0; is2nd < 2; ++is2nd ) // loop on two ends of inSeg
845 InPoint& inPnt = *( is2nd ? inSeg._p1 : inSeg._p0 );
846 size_t pInd = inPnt.index( inPoints );
847 if ( inPntChecked[ pInd ] )
850 inPntChecked[ pInd-1 ] &&
851 inPoints[ pInd-1 ] == inPnt )
853 inPntChecked[ pInd ] = true;
855 const TVDEdge* maE = is2nd ? maEdges.front() : maEdges.back();
856 if ( inPnt == ( is2nd ? maE->vertex0() : maE->vertex1() ))
858 const TVDEdge* edge = // a secondary TVDEdge connecting inPnt and maE
859 is2nd ? maE->prev() : maE->next();
860 while ( inSeg.isConnected( edge ))
862 if ( edge->is_primary() ) break; // this should not happen
863 const TVDEdge* edge2 = edge->twin(); // we are in a neighbor cell, add MA edges to inPnt
864 if ( inSeg.getGeomEdge( edge2->cell() ) != theNoEdgeID )
865 break; // cell of an InSegment
866 bool hasInfinite = false;
867 list< const TVDEdge* > pointEdges;
871 edge = edge->next(); // Returns the CCW next edge within the cell.
872 if ( edge->is_infinite() )
874 else if ( edge->is_primary() && !inSeg.isExternal( edge ))
875 pointEdges.push_back( edge );
877 while ( edge != edge2 && !hasInfinite );
879 if ( hasInfinite || pointEdges.empty() )
881 inPnt._edges.splice( inPnt._edges.end(), pointEdges );
882 inSeg.setGeomEdgeToCell( edge->cell(), inSeg._geomEdgeInd );
884 edge = is2nd ? inPnt._edges.front()->prev() : inPnt._edges.back()->next();
886 } // add MA edges corresponding to concave InPoints
888 } // loop on inSegments to find corresponding MA edges
891 // -------------------------------------------
892 // Create Branches and BndPoints for each EDGE
893 // -------------------------------------------
895 if ( inPoints.front() == inPoints.back() /*&& !inPoints[0]._edges.empty()*/ )
897 inPntChecked[0] = false; // do not use the 1st point twice
898 //InSegment::setGeomEdgeToCell( inPoints[0]._edges.back()->cell(), theNoEdgeID );
899 inPoints[0]._edges.clear();
902 // Divide InSegment's into BndSeg's (so that each BndSeg corresponds to one MA edge)
904 vector< vector< BndSeg > > bndSegsPerEdge( boundary.nbEdges() ); // all BndSeg's
906 vector< BndSeg > bndSegs; // bndSeg's of a current EDGE
907 size_t prevGeomEdge = theNoEdgeID;
909 list< const TVDEdge* >::reverse_iterator e;
910 for ( size_t i = 0; i < inSegments.size(); ++i )
912 InSegment& inSeg = inSegments[i];
914 if ( inSeg._geomEdgeInd != prevGeomEdge )
916 if ( !bndSegs.empty() )
917 bndSegsPerEdge[ prevGeomEdge ].swap( bndSegs );
918 prevGeomEdge = inSeg._geomEdgeInd;
921 // segments around 1st concave point
922 size_t ip0 = inSeg._p0->index( inPoints );
923 if ( inPntChecked[ ip0 ] )
924 for ( e = inSeg._p0->_edges.rbegin(); e != inSeg._p0->_edges.rend(); ++e )
925 bndSegs.push_back( BndSeg( &inSeg, *e, inSeg._p0->_param ));
926 inPntChecked[ ip0 ] = false;
928 // segments of InSegment's
929 const size_t nbMaEdges = inSeg._edges.size();
930 switch ( nbMaEdges ) {
931 case 0: // "around" circle center
932 bndSegs.push_back( BndSeg( &inSeg, 0, inSeg._p1->_param )); break;
934 bndSegs.push_back( BndSeg( &inSeg, inSeg._edges.back(), inSeg._p1->_param )); break;
936 gp_XY inSegDir( inSeg._p1->_a - inSeg._p0->_a,
937 inSeg._p1->_b - inSeg._p0->_b );
938 const double inSegLen2 = inSegDir.SquareModulus();
939 e = inSeg._edges.rbegin();
940 for ( size_t iE = 1; iE < nbMaEdges; ++e, ++iE )
942 gp_XY toMA( (*e)->vertex0()->x() - inSeg._p0->_a,
943 (*e)->vertex0()->y() - inSeg._p0->_b );
944 double r = toMA * inSegDir / inSegLen2;
945 double u = r * inSeg._p1->_param + ( 1. - r ) * inSeg._p0->_param;
946 bndSegs.push_back( BndSeg( &inSeg, *e, u ));
948 bndSegs.push_back( BndSeg( &inSeg, *e, inSeg._p1->_param ));
950 // segments around 2nd concave point
951 size_t ip1 = inSeg._p1->index( inPoints );
952 if ( inPntChecked[ ip1 ] )
953 for ( e = inSeg._p1->_edges.rbegin(); e != inSeg._p1->_edges.rend(); ++e )
954 bndSegs.push_back( BndSeg( &inSeg, *e, inSeg._p1->_param ));
955 inPntChecked[ ip1 ] = false;
957 if ( !bndSegs.empty() )
958 bndSegsPerEdge[ prevGeomEdge ].swap( bndSegs );
961 // prepare to MA branch search
962 for ( size_t iE = 0; iE < bndSegsPerEdge.size(); ++iE )
964 // 1) make TVDEdge's know it's BndSeg to enable passing branchID to
965 // an opposite BndSeg in BndSeg::setBranch(); geom EDGE ID is known from TVDCell
966 // 2) connect bndSegs via BndSeg::_prev
968 vector< BndSeg >& bndSegs = bndSegsPerEdge[ iE ];
969 if ( bndSegs.empty() ) continue;
971 for ( size_t i = 1; i < bndSegs.size(); ++i )
973 bndSegs[i]._prev = & bndSegs[i-1];
974 bndSegs[i].setIndexToEdge( i );
976 // look for the last bndSeg of previous EDGE to set bndSegs[0]._prev
977 const InPoint& p0 = bndSegs[0]._inSeg->point0();
978 for ( size_t iE2 = 0; iE2 < bndSegsPerEdge.size(); ++iE2 )
979 if ( p0 == bndSegsPerEdge[ iE2 ].back()._inSeg->point1() )
981 bndSegs[0]._prev = & bndSegsPerEdge[ iE2 ].back();
984 bndSegs[0].setIndexToEdge( 0 );
987 //bndSegsToMesh( bndSegsPerEdge ); // debug: visually check found MA edges
990 // Find TVDEdge's of Branches and associate them with bndSegs
992 vector< vector<const TVDEdge*> > branchEdges;
993 branchEdges.reserve( boundary.nbEdges() * 4 );
995 map< const TVDVertex*, SMESH_MAT2d::BranchEndType > endType;
997 int branchID = 1; // we code orientation as branchID sign
998 branchEdges.resize( branchID );
1000 for ( size_t iE = 0; iE < bndSegsPerEdge.size(); ++iE )
1002 vector< BndSeg >& bndSegs = bndSegsPerEdge[ iE ];
1003 for ( size_t i = 0; i < bndSegs.size(); ++i )
1005 if ( bndSegs[i].branchID() )
1007 if ( bndSegs[i]._prev &&
1008 bndSegs[i]._branchID == -bndSegs[i]._prev->_branchID &&
1011 SMESH_MAT2d::BranchEndType type =
1012 ( bndSegs[i]._inSeg->isConnected( bndSegs[i]._edge ) ?
1013 SMESH_MAT2d::BE_ON_VERTEX :
1014 SMESH_MAT2d::BE_END );
1015 endType.insert( make_pair( bndSegs[i]._edge->vertex1(), type ));
1019 if ( !bndSegs[i]._prev &&
1020 !bndSegs[i].hasOppositeEdge() )
1023 if ( !bndSegs[i]._prev ||
1024 !bndSegs[i]._prev->isSameBranch( bndSegs[i] ))
1026 branchEdges.resize(( branchID = branchEdges.size()) + 1 );
1027 if ( bndSegs[i]._edge && bndSegs[i]._prev )
1028 endType.insert( make_pair( bndSegs[i]._edge->vertex1(), SMESH_MAT2d::BE_BRANCH_POINT ));
1030 else if ( bndSegs[i]._prev->_branchID )
1032 branchID = bndSegs[i]._prev->_branchID; // with sign
1034 else if ( bndSegs[i]._edge && // 1st bndSeg of a WIRE
1035 bndSegs[i]._inSeg->isConnected( bndSegs[i]._edge ))
1037 branchEdges.resize(( branchID = branchEdges.size()) + 1 );
1038 if ( bndSegs[i]._inSeg->point0() == bndSegs[i]._edge->vertex1() )
1039 endType.insert( make_pair( bndSegs[i]._edge->vertex1(), SMESH_MAT2d::BE_ON_VERTEX ));
1041 endType.insert( make_pair( bndSegs[i]._edge->vertex0(), SMESH_MAT2d::BE_ON_VERTEX ));
1044 bndSegs[i].setBranch( branchID, bndSegsPerEdge ); // set to i-th and to the opposite bndSeg
1045 if ( bndSegs[i].hasOppositeEdge() )
1046 branchEdges[ bndSegs[i].branchID() ].push_back( bndSegs[i]._edge );
1050 // join the 1st and the last branch edges if it is the same branch
1051 // if ( bndSegs.back().branchID() != bndSegs.front().branchID() &&
1052 // bndSegs.back().isSameBranch( bndSegs.front() ))
1054 // vector<const TVDEdge*> & br1 = branchEdges[ bndSegs.front().branchID() ];
1055 // vector<const TVDEdge*> & br2 = branchEdges[ bndSegs.back().branchID() ];
1056 // br1.insert( br1.begin(), br2.begin(), br2.end() );
1060 // remove branches ending at BE_ON_VERTEX
1062 vector<bool> isBranchRemoved( branchEdges.size(), false );
1064 if ( ignoreCorners && branchEdges.size() > 2 && !branchEdges[2].empty() )
1066 // find branches to remove
1067 map< const TVDVertex*, SMESH_MAT2d::BranchEndType >::iterator v2et;
1068 for ( size_t iB = 1; iB < branchEdges.size(); ++iB )
1070 if ( branchEdges[iB].empty() )
1072 const TVDVertex* v0 = branchEdges[iB][0]->vertex1();
1073 const TVDVertex* v1 = branchEdges[iB].back()->vertex0();
1074 v2et = endType.find( v0 );
1075 if ( v2et != endType.end() && v2et->second == SMESH_MAT2d::BE_ON_VERTEX )
1076 isBranchRemoved[ iB ] = true;
1077 v2et = endType.find( v1 );
1078 if ( v2et != endType.end() && v2et->second == SMESH_MAT2d::BE_ON_VERTEX )
1079 isBranchRemoved[ iB ] = true;
1081 // try to join not removed branches into one
1082 for ( size_t iB = 1; iB < branchEdges.size(); ++iB )
1084 if ( branchEdges[iB].empty() || isBranchRemoved[iB] )
1086 const TVDVertex* v0 = branchEdges[iB][0]->vertex1();
1087 const TVDVertex* v1 = branchEdges[iB].back()->vertex0();
1088 v2et = endType.find( v0 );
1089 if ( v2et == endType.end() || v2et->second != SMESH_MAT2d::BE_BRANCH_POINT )
1091 v2et = endType.find( v1 );
1092 if ( v2et == endType.end() || v2et->second != SMESH_MAT2d::BE_BRANCH_POINT )
1097 for ( int isV0 = 0; isV0 < 2; ++isV0 )
1099 const TVDVertex* v = isV0 ? v0 : v1;
1100 size_t iBrToJoin = 0;
1101 for ( size_t iB2 = 1; iB2 < branchEdges.size(); ++iB2 )
1103 if ( branchEdges[iB2].empty() || isBranchRemoved[iB2] || iB == iB2 )
1105 const TVDVertex* v02 = branchEdges[iB2][0]->vertex1();
1106 const TVDVertex* v12 = branchEdges[iB2].back()->vertex0();
1107 if ( v == v02 || v == v12 )
1109 if ( iBrToJoin > 0 )
1112 break; // more than 2 not removed branches meat at a TVDVertex
1117 if ( iBrToJoin > 0 )
1119 vector<const TVDEdge*>& branch = branchEdges[ iBrToJoin ];
1120 const TVDVertex* v02 = branch[0]->vertex1();
1121 const TVDVertex* v12 = branch.back()->vertex0();
1122 updateJoinedBranch( branch, iB, bndSegsPerEdge, /*reverse=*/(v0 == v02 || v1 == v12 ));
1123 if ( v0 == v02 || v0 == v12 )
1124 branchEdges[iB].insert( branchEdges[iB].begin(), branch.begin(), branch.end() );
1126 branchEdges[iB].insert( branchEdges[iB].end(), branch.begin(), branch.end() );
1130 } // loop on branchEdges
1131 } // if ( ignoreCorners )
1133 // associate branchIDs and the input branch vector (arg)
1134 vector< SMESH_MAT2d::Branch* > branchByID( branchEdges.size(), 0 );
1136 for ( size_t i = 0; i < branchEdges.size(); ++i )
1138 nbBranches += ( !branchEdges[i].empty() );
1140 branch.resize( nbBranches );
1142 for ( size_t brID = 1; brID < branchEdges.size(); ++brID ) // 1st - not removed
1144 if ( !branchEdges[ brID ].empty() && !isBranchRemoved[ brID ])
1145 branchByID[ brID ] = & branch[ iBr++ ];
1147 for ( size_t brID = 1; brID < branchEdges.size(); ++brID ) // then - removed
1149 if ( !branchEdges[ brID ].empty() && isBranchRemoved[ brID ])
1150 branchByID[ brID ] = & branch[ iBr++ ];
1153 // Fill in BndPoints of each EDGE of the boundary
1156 int edgeInd = -1, dInd = 0;
1157 for ( size_t iE = 0; iE < bndSegsPerEdge.size(); ++iE )
1159 vector< BndSeg >& bndSegs = bndSegsPerEdge[ iE ];
1160 SMESH_MAT2d::BndPoints & bndPoints = boundary.getPoints( iE );
1162 // make TVDEdge know an index of bndSegs within BndPoints
1163 for ( size_t i = 0; i < bndSegs.size(); ++i )
1164 if ( bndSegs[i]._edge )
1165 SMESH_MAT2d::Branch::setBndSegment( i, bndSegs[i]._edge );
1167 // parameters on EDGE
1169 bndPoints._params.reserve( bndSegs.size() + 1 );
1170 bndPoints._params.push_back( bndSegs[ 0 ]._inSeg->_p0->_param );
1172 for ( size_t i = 0; i < bndSegs.size(); ++i )
1173 bndPoints._params.push_back( bndSegs[ i ]._uLast );
1177 bndPoints._maEdges.reserve( bndSegs.size() );
1179 for ( size_t i = 0; i < bndSegs.size(); ++i )
1181 const size_t brID = bndSegs[ i ].branchID();
1182 const SMESH_MAT2d::Branch* br = branchByID[ brID ];
1184 if ( bndSegs[ i ]._edge && !branchEdges[ brID ].empty() )
1188 if (( edgeInd < 0 ||
1189 edgeInd >= (int) branchEdges[ brID ].size() ) ||
1190 ( branchEdges[ brID ][ edgeInd ] != bndSegs[ i ]._edge &&
1191 branchEdges[ brID ][ edgeInd ]->twin() != bndSegs[ i ]._edge ))
1193 if ( bndSegs[ i ]._branchID < 0 )
1196 for ( edgeInd = branchEdges[ brID ].size() - 1; edgeInd > 0; --edgeInd )
1197 if ( branchEdges[ brID ][ edgeInd ]->twin() == bndSegs[ i ]._edge )
1200 else // bndSegs[ i ]._branchID > 0
1203 for ( edgeInd = 0; edgeInd < branchEdges[ brID ].size(); ++edgeInd )
1204 if ( branchEdges[ brID ][ edgeInd ] == bndSegs[ i ]._edge )
1211 // no MA edge, bndSeg corresponds to an end point of a branch
1212 if ( bndPoints._maEdges.empty() )
1215 edgeInd = branchEdges[ brID ].size();
1216 dInd = bndSegs[ i ]._branchID > 0 ? +1 : -1;
1219 bndPoints._maEdges.push_back( make_pair( br, ( 1 + edgeInd ) * dInd ));
1221 } // loop on bndSegs of an EDGE
1222 } // loop on all bndSegs to construct Boundary
1224 // Initialize branches
1226 // find a not removed branch
1227 size_t iBrNorRemoved = 0;
1228 for ( size_t brID = 1; brID < branchEdges.size(); ++brID )
1229 if ( !branchEdges[brID].empty() && !isBranchRemoved[brID] )
1231 iBrNorRemoved = brID;
1234 // fill the branches with MA edges
1235 for ( size_t brID = 1; brID < branchEdges.size(); ++brID )
1236 if ( !branchEdges[brID].empty() )
1238 branchByID[ brID ]->init( branchEdges[brID], & boundary, endType );
1240 // mark removed branches
1241 for ( size_t brID = 1; brID < branchEdges.size(); ++brID )
1242 if ( isBranchRemoved[brID] && iBrNorRemoved > 0 )
1244 SMESH_MAT2d::Branch* branch = branchByID[ brID ];
1245 SMESH_MAT2d::Branch* mainBranch = branchByID[ iBrNorRemoved ];
1246 bool is1stBrPnt = ( branch->getEnd(0)->_type == SMESH_MAT2d::BE_BRANCH_POINT );
1247 const TVDVertex* branchVextex =
1248 is1stBrPnt ? branch->getEnd(0)->_vertex : branch->getEnd(1)->_vertex;
1249 SMESH_MAT2d::BranchPoint bp = mainBranch->getPoint( branchVextex );
1250 branch->setRemoved( bp );
1252 // set branches to branch ends
1253 for ( size_t i = 0; i < branch.size(); ++i )
1254 if ( !branch[i].isRemoved() )
1255 branch[i].setBranchesToEnds( branch );
1257 // fill branchPnt arg
1258 map< const TVDVertex*, const SMESH_MAT2d::BranchEnd* > v2end;
1259 for ( size_t i = 0; i < branch.size(); ++i )
1261 if ( branch[i].getEnd(0)->_branches.size() > 2 )
1262 v2end.insert( make_pair( branch[i].getEnd(0)->_vertex, branch[i].getEnd(0) ));
1263 if ( branch[i].getEnd(1)->_branches.size() > 2 )
1264 v2end.insert( make_pair( branch[i].getEnd(1)->_vertex, branch[i].getEnd(1) ));
1266 branchPnt.resize( v2end.size() );
1267 map< const TVDVertex*, const SMESH_MAT2d::BranchEnd* >::iterator v2e = v2end.begin();
1268 for ( size_t i = 0; v2e != v2end.end(); ++v2e, ++i )
1269 branchPnt[ i ] = v2e->second;
1275 //================================================================================
1277 * \brief MedialAxis constructor
1278 * \param [in] face - a face to create MA for
1279 * \param [in] edges - edges of the face (possibly not all) on the order they
1280 * encounter in the face boundary.
1281 * \param [in] minSegLen - minimal length of a mesh segment used to discretize
1282 * the edges. It is used to define precision of MA approximation
1284 //================================================================================
1286 SMESH_MAT2d::MedialAxis::MedialAxis(const TopoDS_Face& face,
1287 const std::vector< TopoDS_Edge >& edges,
1288 const double minSegLen,
1289 const bool ignoreCorners):
1290 _face( face ), _boundary( edges.size() )
1292 // input to construct_voronoi()
1293 vector< InPoint > inPoints;
1294 vector< InSegment> inSegments;
1295 if ( !makeInputData( face, edges, minSegLen, inPoints, inSegments, _scale ))
1298 inSegmentsToFile( inSegments );
1300 // build voronoi diagram
1301 construct_voronoi( inSegments.begin(), inSegments.end(), &_vd );
1304 makeMA( _vd, ignoreCorners, inPoints, inSegments, _branch, _branchPnt, _boundary );
1306 // count valid branches
1307 _nbBranches = _branch.size();
1308 for ( size_t i = 0; i < _branch.size(); ++i )
1309 if ( _branch[i].isRemoved() )
1313 //================================================================================
1315 * \brief Returns the i-th branch
1317 //================================================================================
1319 const SMESH_MAT2d::Branch* SMESH_MAT2d::MedialAxis::getBranch(size_t i) const
1321 return i < _nbBranches ? &_branch[i] : 0;
1324 //================================================================================
1326 * \brief Return UVs of ends of MA edges of a branch
1328 //================================================================================
1330 void SMESH_MAT2d::MedialAxis::getPoints( const Branch* branch,
1331 std::vector< gp_XY >& points) const
1333 branch->getPoints( points, _scale );
1336 //================================================================================
1338 * \brief Returns a BranchPoint corresponding to a given point on a geom EDGE
1339 * \param [in] iEdge - index of geom EDGE within a vector passed at MA construction
1340 * \param [in] u - parameter of the point on EDGE curve
1341 * \param [out] p - the found BranchPoint
1342 * \return bool - is OK
1344 //================================================================================
1346 bool SMESH_MAT2d::Boundary::getBranchPoint( const std::size_t iEdge,
1348 BranchPoint& p ) const
1350 if ( iEdge >= _pointsPerEdge.size() || _pointsPerEdge[iEdge]._params.empty() )
1353 const BndPoints& points = _pointsPerEdge[ iEdge ];
1354 const bool edgeReverse = ( points._params[0] > points._params.back() );
1356 if ( u < ( edgeReverse ? points._params.back() : points._params[0] ))
1357 u = edgeReverse ? points._params.back() : points._params[0];
1358 else if ( u > ( edgeReverse ? points._params[0] : points._params.back()) )
1359 u = edgeReverse ? points._params[0] : points._params.back();
1361 double r = ( u - points._params[0] ) / ( points._params.back() - points._params[0] );
1362 int i = int( r * double( points._maEdges.size()-1 ));
1365 while ( points._params[i ] < u ) --i;
1366 while ( points._params[i+1] > u ) ++i;
1370 while ( points._params[i ] > u ) --i;
1371 while ( points._params[i+1] < u ) ++i;
1374 double edgeParam = ( u - points._params[i] ) / ( points._params[i+1] - points._params[i] );
1376 if ( !points._maEdges[ i ].second ) // no branch at the EDGE end, look for a closest branch
1378 if ( i < points._maEdges.size() / 2 ) // near 1st point
1380 while ( i < points._maEdges.size()-1 && !points._maEdges[ i ].second )
1382 edgeParam = edgeReverse;
1384 else // near last point
1386 while ( i > 0 && !points._maEdges[ i ].second )
1388 edgeParam = !edgeReverse;
1391 const std::pair< const Branch*, int >& maE = points._maEdges[ i ];
1392 bool maReverse = ( maE.second < 0 );
1394 p._branch = maE.first;
1395 p._iEdge = ( maReverse ? -maE.second : maE.second ) - 1; // countered from 1 to store sign
1396 p._edgeParam = ( maE.first && maReverse ) ? ( 1. - edgeParam ) : edgeParam;
1401 //================================================================================
1403 * \brief Check if a given boundary segment is a null-length segment on a concave
1405 * \param [in] iEdge - index of a geom EDGE
1406 * \param [in] iSeg - index of a boundary segment
1407 * \return bool - true if the segment is on concave corner
1409 //================================================================================
1411 bool SMESH_MAT2d::Boundary::isConcaveSegment( std::size_t iEdge, std::size_t iSeg ) const
1413 if ( iEdge >= _pointsPerEdge.size() || _pointsPerEdge[iEdge]._params.empty() )
1416 const BndPoints& points = _pointsPerEdge[ iEdge ];
1417 if ( points._params.size() <= iSeg+1 )
1420 return Abs( points._params[ iSeg ] - points._params[ iSeg+1 ]) < 1e-20;
1423 //================================================================================
1425 * \brief Moves (changes _param) a given BoundaryPoint to a closest EDGE end
1427 //================================================================================
1429 bool SMESH_MAT2d::Boundary::moveToClosestEdgeEnd( BoundaryPoint& bp ) const
1431 if ( bp._edgeIndex >= _pointsPerEdge.size() )
1434 const BndPoints& points = _pointsPerEdge[ bp._edgeIndex ];
1435 if ( Abs( bp._param - points._params[0]) < Abs( points._params.back() - bp._param ))
1436 bp._param = points._params[0];
1438 bp._param = points._params.back();
1443 //================================================================================
1445 * \brief Creates a 3d curve corresponding to a Branch
1446 * \param [in] branch - the Branch
1447 * \return Adaptor3d_Curve* - the new curve the caller is to delete
1449 //================================================================================
1451 Adaptor3d_Curve* SMESH_MAT2d::MedialAxis::make3DCurve(const Branch& branch) const
1453 Handle(Geom_Surface) surface = BRep_Tool::Surface( _face );
1454 if ( surface.IsNull() )
1458 branch.getPoints( uv, _scale );
1459 if ( uv.size() < 2 )
1462 vector< TopoDS_Vertex > vertex( uv.size() );
1463 for ( size_t i = 0; i < uv.size(); ++i )
1464 vertex[i] = BRepBuilderAPI_MakeVertex( surface->Value( uv[i].X(), uv[i].Y() ));
1467 BRep_Builder aBuilder;
1468 aBuilder.MakeWire(aWire);
1469 for ( size_t i = 1; i < vertex.size(); ++i )
1471 TopoDS_Edge edge = BRepBuilderAPI_MakeEdge( vertex[i-1], vertex[i] );
1472 aBuilder.Add( aWire, edge );
1475 // if ( myEdge.size() == 2 && FirstVertex().IsSame( LastVertex() ))
1476 // aWire.Closed(true); // issue 0021141
1478 return new BRepAdaptor_CompCurve( aWire );
1481 //================================================================================
1483 * \brief Copy points of an EDGE
1485 //================================================================================
1487 void SMESH_MAT2d::Branch::init( vector<const TVDEdge*>& maEdges,
1488 const Boundary* boundary,
1489 map< const TVDVertex*, BranchEndType > endType )
1491 if ( maEdges.empty() ) return;
1493 _boundary = boundary;
1494 _maEdges.swap( maEdges );
1497 _params.reserve( _maEdges.size() + 1 );
1498 _params.push_back( 0. );
1499 for ( size_t i = 0; i < _maEdges.size(); ++i )
1500 _params.push_back( _params.back() + length( _maEdges[i] ));
1502 for ( size_t i = 1; i < _params.size(); ++i )
1503 _params[i] /= _params.back();
1506 _endPoint1._vertex = _maEdges.front()->vertex1();
1507 _endPoint2._vertex = _maEdges.back ()->vertex0();
1509 if ( endType.count( _endPoint1._vertex ))
1510 _endPoint1._type = endType[ _endPoint1._vertex ];
1511 if ( endType.count( _endPoint2._vertex ))
1512 _endPoint2._type = endType[ _endPoint2._vertex ];
1515 //================================================================================
1517 * \brief fills BranchEnd::_branches of its ends
1519 //================================================================================
1521 void SMESH_MAT2d::Branch::setBranchesToEnds( const vector< Branch >& branches )
1523 for ( size_t i = 0; i < branches.size(); ++i )
1525 if ( this->_endPoint1._vertex == branches[i]._endPoint1._vertex ||
1526 this->_endPoint1._vertex == branches[i]._endPoint2._vertex )
1527 this->_endPoint1._branches.push_back( &branches[i] );
1529 if ( this->_endPoint2._vertex == branches[i]._endPoint1._vertex ||
1530 this->_endPoint2._vertex == branches[i]._endPoint2._vertex )
1531 this->_endPoint2._branches.push_back( &branches[i] );
1535 //================================================================================
1537 * \brief returns a BranchPoint corresponding to a TVDVertex
1539 //================================================================================
1541 SMESH_MAT2d::BranchPoint SMESH_MAT2d::Branch::getPoint( const TVDVertex* vertex ) const
1547 if ( vertex == _maEdges[0]->vertex1() )
1553 for ( ; p._iEdge < _maEdges.size(); ++p._iEdge )
1554 if ( vertex == _maEdges[ p._iEdge ]->vertex0() )
1556 p._edgeParam = _params[ p._iEdge ];
1563 //================================================================================
1565 * \brief Sets a proxy point for a removed branch
1566 * \param [in] proxyPoint - a point of another branch to which all points of this
1569 //================================================================================
1571 void SMESH_MAT2d::Branch::setRemoved( const BranchPoint& proxyPoint )
1573 _proxyPoint = proxyPoint;
1576 //================================================================================
1578 * \brief Returns points on two EDGEs, equidistant from a given point of this Branch
1579 * \param [in] param - [0;1] normalized param on the Branch
1580 * \param [out] bp1 - BoundaryPoint on EDGE with a lower index
1581 * \param [out] bp2 - BoundaryPoint on EDGE with a higher index
1582 * \return bool - true if the BoundaryPoint's found
1584 //================================================================================
1586 bool SMESH_MAT2d::Branch::getBoundaryPoints(double param,
1588 BoundaryPoint& bp2 ) const
1590 if ( param < _params[0] || param > _params.back() )
1593 // look for an index of a MA edge by param
1594 double ip = param * _params.size();
1595 size_t i = size_t( Min( int( _maEdges.size()-1), int( ip )));
1597 while ( param < _params[i ] ) --i;
1598 while ( param > _params[i+1] ) ++i;
1600 double r = ( param - _params[i] ) / ( _params[i+1] - _params[i] );
1602 return getBoundaryPoints( i, r, bp1, bp2 );
1605 //================================================================================
1607 * \brief Returns points on two EDGEs, equidistant from a given point of this Branch
1608 * \param [in] iMAEdge - index of a MA edge within this Branch
1609 * \param [in] maEdgeParam - [0;1] normalized param on the \a iMAEdge
1610 * \param [out] bp1 - BoundaryPoint on EDGE with a lower index
1611 * \param [out] bp2 - BoundaryPoint on EDGE with a higher index
1612 * \return bool - true if the BoundaryPoint's found
1614 //================================================================================
1616 bool SMESH_MAT2d::Branch::getBoundaryPoints(std::size_t iMAEdge,
1619 BoundaryPoint& bp2 ) const
1622 return _proxyPoint._branch->getBoundaryPoints( _proxyPoint, bp1, bp2 );
1624 if ( iMAEdge > _maEdges.size() )
1626 if ( iMAEdge == _maEdges.size() )
1627 iMAEdge = _maEdges.size() - 1;
1629 size_t iGeom1 = getGeomEdge( _maEdges[ iMAEdge ] );
1630 size_t iGeom2 = getGeomEdge( _maEdges[ iMAEdge ]->twin() );
1631 size_t iSeg1 = getBndSegment( _maEdges[ iMAEdge ] );
1632 size_t iSeg2 = getBndSegment( _maEdges[ iMAEdge ]->twin() );
1634 return ( _boundary->getPoint( iGeom1, iSeg1, maEdgeParam, bp1 ) &&
1635 _boundary->getPoint( iGeom2, iSeg2, maEdgeParam, bp2 ));
1638 //================================================================================
1640 * \brief Returns points on two EDGEs, equidistant from a given point of this Branch
1642 //================================================================================
1644 bool SMESH_MAT2d::Branch::getBoundaryPoints(const BranchPoint& p,
1646 BoundaryPoint& bp2 ) const
1648 return ( p._branch ? p._branch : this )->getBoundaryPoints( p._iEdge, p._edgeParam, bp1, bp2 );
1651 //================================================================================
1653 * \brief Return a parameter of a BranchPoint normalized within this Branch
1655 //================================================================================
1657 bool SMESH_MAT2d::Branch::getParameter(const BranchPoint & p, double & u ) const
1659 if ( this != p._branch && p._branch )
1660 return p._branch->getParameter( p, u );
1663 return _proxyPoint._branch->getParameter( _proxyPoint, u );
1665 if ( p._iEdge > _params.size()-1 )
1667 if ( p._iEdge == _params.size()-1 )
1670 u = ( _params[ p._iEdge ] * ( 1 - p._edgeParam ) +
1671 _params[ p._iEdge+1 ] * p._edgeParam );
1676 //================================================================================
1678 * \brief Check type of both ends
1680 //================================================================================
1682 bool SMESH_MAT2d::Branch::hasEndOfType(BranchEndType type) const
1684 return ( _endPoint1._type == type || _endPoint2._type == type );
1687 //================================================================================
1689 * \brief Returns MA points
1690 * \param [out] points - the 2d points
1691 * \param [in] scale - the scale that was used to scale the 2d space of MA
1693 //================================================================================
1695 void SMESH_MAT2d::Branch::getPoints( std::vector< gp_XY >& points,
1696 const double scale[2]) const
1698 points.resize( _maEdges.size() + 1 );
1700 points[0].SetCoord( _maEdges[0]->vertex1()->x() / scale[0], // CCW order! -> vertex1 not vertex0
1701 _maEdges[0]->vertex1()->y() / scale[1] );
1703 for ( size_t i = 0; i < _maEdges.size(); ++i )
1704 points[i+1].SetCoord( _maEdges[i]->vertex0()->x() / scale[0],
1705 _maEdges[i]->vertex0()->y() / scale[1] );
1708 //================================================================================
1710 * \brief Return indices of EDGEs equidistant from this branch
1712 //================================================================================
1714 void SMESH_MAT2d::Branch::getGeomEdges( std::vector< std::size_t >& edgeIDs1,
1715 std::vector< std::size_t >& edgeIDs2 ) const
1717 edgeIDs1.push_back( getGeomEdge( _maEdges[0] ));
1718 edgeIDs2.push_back( getGeomEdge( _maEdges[0]->twin() ));
1720 for ( size_t i = 1; i < _maEdges.size(); ++i )
1722 size_t ie1 = getGeomEdge( _maEdges[i] );
1723 size_t ie2 = getGeomEdge( _maEdges[i]->twin() );
1725 if ( edgeIDs1.back() != ie1 ) edgeIDs1.push_back( ie1 );
1726 if ( edgeIDs2.back() != ie2 ) edgeIDs2.push_back( ie2 );
1730 //================================================================================
1732 * \brief Looks for a BranchPoint position around a concave VERTEX
1734 //================================================================================
1736 bool SMESH_MAT2d::Branch::addDivPntForConcaVertex( std::vector< std::size_t >& edgeIDs1,
1737 std::vector< std::size_t >& edgeIDs2,
1738 std::vector< BranchPoint >& divPoints,
1739 const vector<const TVDEdge*>& maEdges,
1740 const vector<const TVDEdge*>& maEdgesTwin,
1743 // if there is a concave vertex between EDGEs
1744 // then position of a dividing BranchPoint is undefined, it is somewhere
1745 // on an arc-shaped part of the Branch around the concave vertex.
1746 // Chose this position by a VERTEX of the opposite EDGE, or put it in the middle
1747 // of the arc if there is no opposite VERTEX.
1748 // All null-length segments around a VERTEX belong to one of EDGEs.
1750 BranchPoint divisionPnt;
1751 divisionPnt._branch = this;
1753 BranchIterator iCur( maEdges, i );
1755 size_t ie1 = getGeomEdge( maEdges [i] );
1756 size_t ie2 = getGeomEdge( maEdgesTwin[i] );
1758 size_t iSeg1 = getBndSegment( iCur.edgePrev() );
1759 size_t iSeg2 = getBndSegment( iCur.edge() );
1760 bool isConcaPrev = _boundary->isConcaveSegment( edgeIDs1.back(), iSeg1 );
1761 bool isConcaNext = _boundary->isConcaveSegment( ie1, iSeg2 );
1762 if ( !isConcaNext && !isConcaPrev )
1765 bool isConcaveV = false;
1768 BranchIterator iPrev( maEdges, i ), iNext( maEdges, i );
1770 if ( isConcaNext ) // all null-length segments follow
1772 // look for a VERTEX of the opposite EDGE
1773 // iNext - next after all null-length segments
1774 while ( maE = ++iNext )
1776 iSeg2 = getBndSegment( maE );
1777 if ( !_boundary->isConcaveSegment( ie1, iSeg2 ))
1780 bool vertexFound = false;
1781 for ( ++iCur; iCur < iNext; ++iCur )
1783 ie2 = getGeomEdge( maEdgesTwin[ iCur.indexMod() ] );
1784 if ( ie2 != edgeIDs2.back() )
1786 // opposite VERTEX found
1787 divisionPnt._iEdge = iCur.indexMod();
1788 divisionPnt._edgeParam = 0;
1789 divPoints.push_back( divisionPnt );
1790 edgeIDs1.push_back( ie1 );
1791 edgeIDs2.push_back( ie2 );
1798 iPrev = iNext; // not to add a BP in the moddle
1799 i = iNext.indexMod();
1803 else if ( isConcaPrev )
1805 // all null-length segments passed, find their beginning
1806 while ( maE = iPrev.edgePrev() )
1808 iSeg1 = getBndSegment( maE );
1809 if ( _boundary->isConcaveSegment( edgeIDs1.back(), iSeg1 ))
1816 if ( iPrev.index() < i-1 || iNext.index() > i )
1818 // no VERTEX on the opposite EDGE, put the Branch Point in the middle
1819 divisionPnt._iEdge = iPrev.indexMod();
1821 double par1 = _params[ iPrev.indexMod() ], par2 = _params[ iNext.indexMod() ];
1822 double midPar = 0.5 * ( par1 + par2 );
1823 for ( ; _params[ iPrev.indexMod() ] < midPar; ++iPrev )
1824 divisionPnt._iEdge = iPrev.indexMod();
1825 divisionPnt._edgeParam =
1826 ( _params[ iPrev.indexMod() ] - midPar ) /
1827 ( _params[ iPrev.indexMod() ] - _params[ divisionPnt._iEdge ] );
1828 divPoints.push_back( divisionPnt );
1835 //================================================================================
1837 * \brief Return indices of opposite parts of EDGEs equidistant from this branch
1838 * \param [out] edgeIDs1 - EDGE index opposite to the edgeIDs2[i]-th EDGE
1839 * \param [out] edgeIDs2 - EDGE index opposite to the edgeIDs1[i]-th EDGE
1840 * \param [out] divPoints - BranchPoint's located between two successive unique
1841 * pairs of EDGE indices. A \a divPoints[i] can separate e.g. two following pairs
1842 * of EDGE indices < 0, 2 > and < 0, 1 >. Number of \a divPoints is one less
1843 * than number of \a edgeIDs
1845 //================================================================================
1847 void SMESH_MAT2d::Branch::getOppositeGeomEdges( std::vector< std::size_t >& edgeIDs1,
1848 std::vector< std::size_t >& edgeIDs2,
1849 std::vector< BranchPoint >& divPoints) const
1855 std::vector<const TVDEdge*> twins( _maEdges.size() );
1856 for ( size_t i = 0; i < _maEdges.size(); ++i )
1857 twins[i] = _maEdges[i]->twin();
1859 BranchIterator maIter ( _maEdges, 0 );
1860 BranchIterator twIter ( twins, 0 );
1861 // size_t lastConcaE1 = _boundary.nbEdges();
1862 // size_t lastConcaE2 = _boundary.nbEdges();
1864 // if ( maIter._closed ) // closed branch
1866 // edgeIDs1.push_back( getGeomEdge( _maEdges.back() ));
1867 // edgeIDs2.push_back( getGeomEdge( _maEdges.back()->twin() ));
1871 edgeIDs1.push_back( getGeomEdge( maIter.edge() ));
1872 edgeIDs2.push_back( getGeomEdge( twIter.edge() ));
1875 BranchPoint divisionPnt;
1876 divisionPnt._branch = this;
1878 for ( ++maIter, ++twIter; maIter.index() < _maEdges.size(); ++maIter, ++twIter )
1880 size_t ie1 = getGeomEdge( maIter.edge() );
1881 size_t ie2 = getGeomEdge( twIter.edge() );
1883 bool otherE1 = ( edgeIDs1.back() != ie1 );
1884 bool otherE2 = ( edgeIDs2.back() != ie2 );
1886 if ( !otherE1 && !otherE2 && maIter._closed )
1888 int iSegPrev1 = getBndSegment( maIter.edgePrev() );
1889 int iSegCur1 = getBndSegment( maIter.edge() );
1890 otherE1 = Abs( iSegPrev1 - iSegCur1 ) != 1;
1891 int iSegPrev2 = getBndSegment( twIter.edgePrev() );
1892 int iSegCur2 = getBndSegment( twIter.edge() );
1893 otherE2 = Abs( iSegPrev2 - iSegCur2 ) != 1;
1896 if ( otherE1 || otherE2 )
1898 bool isConcaveV = false;
1899 if ( otherE1 && !otherE2 )
1901 isConcaveV = addDivPntForConcaVertex( edgeIDs1, edgeIDs2, divPoints,
1902 _maEdges, twins, maIter._i );
1904 if ( !otherE1 && otherE2 )
1906 isConcaveV = addDivPntForConcaVertex( edgeIDs2, edgeIDs1, divPoints,
1907 twins, _maEdges, maIter._i );
1912 ie1 = getGeomEdge( maIter.edge() );
1913 ie2 = getGeomEdge( twIter.edge() );
1915 if ( !isConcaveV || otherE1 || otherE2 )
1917 edgeIDs1.push_back( ie1 );
1918 edgeIDs2.push_back( ie2 );
1920 if ( divPoints.size() < edgeIDs1.size() - 1 )
1922 divisionPnt._iEdge = maIter.index();
1923 divisionPnt._edgeParam = 0;
1924 divPoints.push_back( divisionPnt );
1927 } // if ( edgeIDs1.back() != ie1 || edgeIDs2.back() != ie2 )
1928 } // loop on _maEdges
1931 //================================================================================
1933 * \brief Store data of boundary segments in TVDEdge
1935 //================================================================================
1937 void SMESH_MAT2d::Branch::setGeomEdge( std::size_t geomIndex, const TVDEdge* maEdge )
1939 if ( maEdge ) maEdge->cell()->color( geomIndex );
1941 std::size_t SMESH_MAT2d::Branch::getGeomEdge( const TVDEdge* maEdge )
1943 return maEdge ? maEdge->cell()->color() : std::string::npos;
1945 void SMESH_MAT2d::Branch::setBndSegment( std::size_t segIndex, const TVDEdge* maEdge )
1947 if ( maEdge ) maEdge->color( segIndex );
1949 std::size_t SMESH_MAT2d::Branch::getBndSegment( const TVDEdge* maEdge )
1951 return maEdge ? maEdge->color() : std::string::npos;
1954 //================================================================================
1956 * \brief Returns a boundary point on a given EDGE
1957 * \param [in] iEdge - index of the EDGE within MedialAxis
1958 * \param [in] iSeg - index of a boundary segment within this Branch
1959 * \param [in] u - [0;1] normalized param within \a iSeg-th segment
1960 * \param [out] bp - the found BoundaryPoint
1961 * \return bool - true if the BoundaryPoint is found
1963 //================================================================================
1965 bool SMESH_MAT2d::Boundary::getPoint( std::size_t iEdge,
1968 BoundaryPoint& bp ) const
1970 if ( iEdge >= _pointsPerEdge.size() )
1972 if ( iSeg+1 >= _pointsPerEdge[ iEdge ]._params.size() )
1975 // This method is called by Branch that can have an opposite orientation,
1976 // hence u is inverted depending on orientation coded as a sign of _maEdge index
1977 bool isReverse = ( _pointsPerEdge[ iEdge ]._maEdges[ iSeg ].second < 0 );
1981 double p0 = _pointsPerEdge[ iEdge ]._params[ iSeg ];
1982 double p1 = _pointsPerEdge[ iEdge ]._params[ iSeg+1 ];
1984 bp._param = p0 * ( 1. - u ) + p1 * u;
1985 bp._edgeIndex = iEdge;