1 // Copyright (C) 2007-2013 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.
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
23 // SMESH SMESH : idl implementation based on 'SMESH' unit's calsses
24 // File : StdMeshers_ProjectionUtils.cxx
25 // Created : Fri Oct 27 10:24:28 2006
26 // Author : Edward AGAPOV (eap)
28 #include "StdMeshers_ProjectionUtils.hxx"
30 #include "StdMeshers_ProjectionSource1D.hxx"
31 #include "StdMeshers_ProjectionSource2D.hxx"
32 #include "StdMeshers_ProjectionSource3D.hxx"
34 #include "SMDS_EdgePosition.hxx"
35 #include "SMESH_Algo.hxx"
36 #include "SMESH_Block.hxx"
37 #include "SMESH_Gen.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Hypothesis.hxx"
40 #include "SMESH_Mesh.hxx"
41 #include "SMESH_MesherHelper.hxx"
42 #include "SMESH_subMesh.hxx"
43 #include "SMESH_subMeshEventListener.hxx"
44 #include "SMESH_MeshAlgos.hxx"
46 #include "utilities.h"
48 #include <BRepAdaptor_Surface.hxx>
49 #include <BRepTools.hxx>
50 #include <BRepTools_WireExplorer.hxx>
51 #include <BRep_Builder.hxx>
52 #include <BRep_Tool.hxx>
53 #include <Bnd_Box.hxx>
56 #include <TopExp_Explorer.hxx>
57 #include <TopTools_Array1OfShape.hxx>
58 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
59 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
60 #include <TopTools_IndexedMapOfShape.hxx>
61 #include <TopTools_ListIteratorOfListOfShape.hxx>
62 #include <TopTools_ListOfShape.hxx>
63 #include <TopTools_MapOfShape.hxx>
65 #include <TopoDS_Compound.hxx>
66 #include <TopoDS_Shape.hxx>
76 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
77 #define CONT_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); continue; }
78 #define SHOW_SHAPE(v,msg) \
80 // if ( (v).IsNull() ) cout << msg << " NULL SHAPE" << endl; \
81 // else if ((v).ShapeType() == TopAbs_VERTEX) {\
82 // gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( (v) ));\
83 // cout<<msg<<" "<<shapeIndex((v))<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;} \
85 // cout << msg << " "; TopAbs::Print((v).ShapeType(),cout) <<" "<<shapeIndex((v))<<endl;}\
87 #define SHOW_LIST(msg,l) \
89 // cout << msg << " ";\
90 // list< TopoDS_Edge >::const_iterator e = l.begin();\
91 // for ( int i = 0; e != l.end(); ++e, ++i ) {\
92 // cout << i << "V (" << TopExp::FirstVertex( *e, true ).TShape().operator->() << ") "\
93 // << i << "E (" << e->TShape().operator->() << "); "; }\
97 #define HERE StdMeshers_ProjectionUtils
101 static SMESHDS_Mesh* theMeshDS[2] = { 0, 0 }; // used to debug only
102 long shapeIndex(const TopoDS_Shape& S)
104 if ( theMeshDS[0] && theMeshDS[1] )
105 return max(theMeshDS[0]->ShapeToIndex(S), theMeshDS[1]->ShapeToIndex(S) );
106 return long(S.TShape().operator->());
109 //================================================================================
111 * \brief Write shape for debug purposes
113 //================================================================================
115 bool _StoreBadShape(const TopoDS_Shape& shape)
118 const char* type[] ={"COMPOUND","COMPSOLID","SOLID","SHELL","FACE","WIRE","EDGE","VERTEX"};
119 BRepTools::Write( shape, SMESH_Comment("/tmp/") << type[shape.ShapeType()] << "_"
120 << shape.TShape().operator->() << ".brep");
125 //================================================================================
127 * \brief Reverse order of edges in a list and their orientation
128 * \param edges - list of edges to reverse
129 * \param nbEdges - number of edges to reverse
131 //================================================================================
133 void Reverse( list< TopoDS_Edge > & edges, const int nbEdges, const int firstEdge=0)
135 SHOW_LIST("BEFORE REVERSE", edges);
137 list< TopoDS_Edge >::iterator eIt = edges.begin();
138 std::advance( eIt, firstEdge );
139 list< TopoDS_Edge >::iterator eBackIt = eIt;
140 for ( int i = 0; i < nbEdges; ++i, ++eBackIt )
141 eBackIt->Reverse(); // reverse edge
144 while ( eIt != eBackIt )
146 std::swap( *eIt, *eBackIt );
147 SHOW_LIST("# AFTER SWAP", edges)
148 if ( (++eIt) != eBackIt )
151 SHOW_LIST("ATFER REVERSE", edges)
154 //================================================================================
156 * \brief Check if propagation is possible
157 * \param theMesh1 - source mesh
158 * \param theMesh2 - target mesh
159 * \retval bool - true if possible
161 //================================================================================
163 bool IsPropagationPossible( SMESH_Mesh* theMesh1, SMESH_Mesh* theMesh2 )
165 if ( theMesh1 != theMesh2 ) {
166 TopoDS_Shape mainShape1 = theMesh1->GetMeshDS()->ShapeToMesh();
167 TopoDS_Shape mainShape2 = theMesh2->GetMeshDS()->ShapeToMesh();
168 return mainShape1.IsSame( mainShape2 );
173 //================================================================================
175 * \brief Fix up association of edges in faces by possible propagation
176 * \param nbEdges - nb of edges in an outer wire
177 * \param edges1 - edges of one face
178 * \param edges2 - matching edges of another face
179 * \param theMesh1 - mesh 1
180 * \param theMesh2 - mesh 2
181 * \retval bool - true if association was fixed
183 //================================================================================
185 bool FixAssocByPropagation( const int nbEdges,
186 list< TopoDS_Edge > & edges1,
187 list< TopoDS_Edge > & edges2,
188 SMESH_Mesh* theMesh1,
189 SMESH_Mesh* theMesh2)
191 if ( nbEdges == 2 && IsPropagationPossible( theMesh1, theMesh2 ) )
193 list< TopoDS_Edge >::iterator eIt2 = ++edges2.begin(); // 2nd edge of the 2nd face
194 TopoDS_Edge edge2 = HERE::GetPropagationEdge( theMesh1, *eIt2, edges1.front() ).second;
195 if ( !edge2.IsNull() ) { // propagation found for the second edge
196 Reverse( edges2, nbEdges );
203 //================================================================================
205 * \brief Look for a group containing a target shape and similar to a source group
206 * \param tgtShape - target edge or face
207 * \param tgtMesh1 - target mesh
208 * \param srcGroup - source group
209 * \retval TopoDS_Shape - found target group
211 //================================================================================
213 TopoDS_Shape FindGroupContaining(const TopoDS_Shape& tgtShape,
214 const SMESH_Mesh* tgtMesh1,
215 const TopoDS_Shape& srcGroup)
217 list<SMESH_subMesh*> subMeshes = tgtMesh1->GetGroupSubMeshesContaining(tgtShape);
218 list<SMESH_subMesh*>::iterator sm = subMeshes.begin();
219 int type, last = TopAbs_SHAPE;
220 for ( ; sm != subMeshes.end(); ++sm ) {
221 const TopoDS_Shape & group = (*sm)->GetSubShape();
222 // check if group is similar to srcGroup
223 for ( type = srcGroup.ShapeType(); type < last; ++type)
224 if ( SMESH_MesherHelper::Count( srcGroup, (TopAbs_ShapeEnum)type, 0) !=
225 SMESH_MesherHelper::Count( group, (TopAbs_ShapeEnum)type, 0))
230 return TopoDS_Shape();
233 //================================================================================
235 * \brief Find association of groups at top and bottom of prism
237 //================================================================================
239 bool AssocGroupsByPropagation(const TopoDS_Shape& theGroup1,
240 const TopoDS_Shape& theGroup2,
242 HERE::TShapeShapeMap& theMap)
244 // If groups are on top and bottom of prism then we can associate
245 // them using "vertical" (or "side") edges and faces of prism since
246 // they connect corresponding vertices and edges of groups.
248 TopTools_IndexedMapOfShape subshapes1, subshapes2;
249 TopExp::MapShapes( theGroup1, subshapes1 );
250 TopExp::MapShapes( theGroup2, subshapes2 );
251 TopTools_ListIteratorOfListOfShape ancestIt;
253 // Iterate on vertices of group1 to find corresponding vertices in group2
254 // and associate adjacent edges and faces
256 TopTools_MapOfShape verticShapes;
257 TopExp_Explorer vExp1( theGroup1, TopAbs_VERTEX );
258 for ( ; vExp1.More(); vExp1.Next() )
260 const TopoDS_Vertex& v1 = TopoDS::Vertex( vExp1.Current() );
261 if ( theMap.IsBound( v1 )) continue; // already processed
263 // Find "vertical" edge ending in v1 and whose other vertex belongs to group2
264 TopoDS_Shape verticEdge, v2;
265 ancestIt.Initialize( theMesh.GetAncestors( v1 ));
266 for ( ; verticEdge.IsNull() && ancestIt.More(); ancestIt.Next() )
268 if ( ancestIt.Value().ShapeType() != TopAbs_EDGE ) continue;
269 v2 = HERE::GetNextVertex( TopoDS::Edge( ancestIt.Value() ), v1 );
270 if ( subshapes2.Contains( v2 ))
271 verticEdge = ancestIt.Value();
273 if ( verticEdge.IsNull() )
276 HERE::InsertAssociation( v1, v2, theMap);
278 // Associate edges by vertical faces sharing the found vertical edge
279 ancestIt.Initialize( theMesh.GetAncestors( verticEdge ) );
280 for ( ; ancestIt.More(); ancestIt.Next() )
282 if ( ancestIt.Value().ShapeType() != TopAbs_FACE ) continue;
283 if ( !verticShapes.Add( ancestIt.Value() )) continue;
284 const TopoDS_Face& face = TopoDS::Face( ancestIt.Value() );
286 // get edges of the face
287 TopoDS_Edge edgeGr1, edgeGr2, verticEdge2;
288 list< TopoDS_Edge > edges; list< int > nbEdgesInWire;
289 SMESH_Block::GetOrderedEdges( face, edges, nbEdgesInWire, v1);
290 if ( nbEdgesInWire.front() != 4 )
291 return _StoreBadShape( face );
292 list< TopoDS_Edge >::iterator edge = edges.begin();
293 if ( verticEdge.IsSame( *edge )) {
295 verticEdge2 = *(++edge);
299 verticEdge2 = *(edge++);
303 HERE::InsertAssociation( edgeGr1, edgeGr2.Reversed(), theMap);
308 TopoDS_Iterator gr1It( theGroup1 );
309 if ( gr1It.Value().ShapeType() == TopAbs_FACE )
311 // find a boundary edge of group1 to start from
312 TopoDS_Shape bndEdge = HERE::GetBoundaryEdge( theGroup1, theMesh );
313 if ( bndEdge.IsNull() )
316 list< TopoDS_Shape > edges(1, bndEdge);
317 list< TopoDS_Shape >::iterator edge1 = edges.begin();
318 for ( ; edge1 != edges.end(); ++edge1 )
320 // there must be one or zero not associated faces between ancestors of edge
321 // belonging to theGroup1
323 ancestIt.Initialize( theMesh.GetAncestors( *edge1 ) );
324 for ( ; ancestIt.More() && face1.IsNull(); ancestIt.Next() ) {
325 if ( ancestIt.Value().ShapeType() == TopAbs_FACE &&
326 !theMap.IsBound( ancestIt.Value() ) &&
327 subshapes1.Contains( ancestIt.Value() ))
328 face1 = ancestIt.Value();
330 // add edges of face1 to start searching for adjacent faces from
331 for ( TopExp_Explorer e(face1, TopAbs_EDGE); e.More(); e.Next())
332 if ( !edge1->IsSame( e.Current() ))
333 edges.push_back( e.Current() );
335 if ( !face1.IsNull() ) {
336 // find the corresponding face of theGroup2
337 TopoDS_Shape edge2 = theMap( *edge1 );
339 ancestIt.Initialize( theMesh.GetAncestors( edge2 ) );
340 for ( ; ancestIt.More() && face2.IsNull(); ancestIt.Next() ) {
341 if ( ancestIt.Value().ShapeType() == TopAbs_FACE &&
342 !theMap.IsBound( ancestIt.Value(), /*is2nd=*/true ) &&
343 subshapes2.Contains( ancestIt.Value() ))
344 face2 = ancestIt.Value();
346 if ( face2.IsNull() )
349 HERE::InsertAssociation( face1, face2, theMap);
356 //================================================================================
358 * \brief Return true if uv position of the vIndex-th vertex of edge on face is close
361 //================================================================================
363 bool sameVertexUV( const TopoDS_Edge& edge,
364 const TopoDS_Face& face,
367 const double& tol2d )
370 TopExp::Vertices( edge, VV[0], VV[1], true);
371 gp_Pnt2d v1UV = BRep_Tool::Parameters( VV[vIndex], face);
372 double dist2d = v1UV.Distance( uv );
373 return dist2d < tol2d;
376 //================================================================================
378 * \brief Returns an EDGE suitable for search of initial vertex association
380 //================================================================================
382 bool getOuterEdges( const TopoDS_Shape shape,
384 std::list< TopoDS_Edge >& allBndEdges )
386 if ( shape.ShapeType() == TopAbs_COMPOUND )
388 TopoDS_Iterator it( shape );
389 if ( it.More() && it.Value().ShapeType() == TopAbs_FACE ) // group of FACEs
391 // look for a boundary EDGE of a group
392 StdMeshers_ProjectionUtils::GetBoundaryEdge( shape, mesh, &allBndEdges );
393 if ( !allBndEdges.empty() )
397 TopExp_Explorer expF( shape, TopAbs_FACE ), expE;
399 for ( ; expF.More(); expF.Next() ) {
401 StdMeshers_ProjectionUtils::OuterShape( TopoDS::Face( expF.Current() ), TopAbs_WIRE );
402 for ( expE.Init( wire, TopAbs_EDGE ); expE.More(); expE.Next() )
403 if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( expE.Current() )))
404 allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
407 else if ( shape.ShapeType() != TopAbs_EDGE) { // no faces
408 for ( expE.Init( shape, TopAbs_EDGE ); expE.More(); expE.Next() )
409 if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( expE.Current() )))
410 allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
412 else if ( shape.ShapeType() == TopAbs_EDGE ) {
413 if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( shape )))
414 allBndEdges.push_back( TopoDS::Edge( shape ));
416 return !allBndEdges.empty();
421 //=======================================================================
423 * Looks for association of all sub-shapes of two shapes
424 * \param theShape1 - target shape
425 * \param theMesh1 - mesh built on shape 1
426 * \param theShape2 - source shape
427 * \param theMesh2 - mesh built on shape 2
428 * \param theAssociation - association map to be filled that may
429 * contain association of one or two pairs of vertices
430 * \retval bool - true if association found
432 //=======================================================================
434 bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& theShape1,
435 SMESH_Mesh* theMesh1,
436 const TopoDS_Shape& theShape2,
437 SMESH_Mesh* theMesh2,
438 TShapeShapeMap & theMap)
440 // Structure of this long function is following
441 // 1) Group -> Group projection: theShape1 is a group member,
442 // theShape2 is another group. We find a group theShape1 is in and recall self.
443 // 2) Accosiate same shapes with different location (partners).
444 // 3) If vertex association is given, perform accosiation according to shape type:
445 // switch ( ShapeType ) {
449 // 4) else try to accosiate in different ways:
450 // a) accosiate shapes by propagation and other simple cases
451 // switch ( ShapeType ) {
455 // b) find association of a couple of vertices and recall self.
458 theMeshDS[0] = theMesh1->GetMeshDS(); // debug
459 theMeshDS[1] = theMesh2->GetMeshDS();
461 // =================================================================================
462 // 1) Is it the case of associating a group member -> another group? (PAL16202, 16203)
463 // =================================================================================
464 if ( theShape1.ShapeType() != theShape2.ShapeType() ) {
465 TopoDS_Shape group1, group2;
466 if ( theShape1.ShapeType() == TopAbs_COMPOUND ) {
468 group2 = FindGroupContaining( theShape2, theMesh2, group1 );
470 else if ( theShape2.ShapeType() == TopAbs_COMPOUND ) {
472 group1 = FindGroupContaining( theShape1, theMesh1, group2 );
474 if ( group1.IsNull() || group2.IsNull() )
475 RETURN_BAD_RESULT("Different shape types");
476 // Associate compounds
477 return FindSubShapeAssociation(group1, theMesh1, group2, theMesh2, theMap );
483 bool partner = theShape1.IsPartner( theShape2 );
484 TopTools_DataMapIteratorOfDataMapOfShapeShape vvIt( theMap._map1to2 );
485 for ( ; partner && vvIt.More(); vvIt.Next() )
486 partner = vvIt.Key().IsPartner( vvIt.Value() );
488 if ( partner ) // Same shape with different location
490 // recursively associate all sub-shapes of theShape1 and theShape2
491 typedef list< pair< TopoDS_Shape, TopoDS_Shape > > TShapePairsList;
492 TShapePairsList shapesQueue( 1, make_pair( theShape1, theShape2 ));
493 TShapePairsList::iterator s1_s2 = shapesQueue.begin();
494 for ( ; s1_s2 != shapesQueue.end(); ++s1_s2 )
496 InsertAssociation( s1_s2->first, s1_s2->second, theMap );
497 TopoDS_Iterator s1It( s1_s2->first), s2It( s1_s2->second );
498 for ( ; s1It.More(); s1It.Next(), s2It.Next() )
499 shapesQueue.push_back( make_pair( s1It.Value(), s2It.Value() ));
504 if ( !theMap.IsEmpty() )
506 //======================================================================
507 // 3) HAS initial vertex association
508 //======================================================================
509 switch ( theShape1.ShapeType() ) {
510 // ----------------------------------------------------------------------
511 case TopAbs_EDGE: { // TopAbs_EDGE
512 // ----------------------------------------------------------------------
513 if ( theMap.Extent() != 2 )
514 RETURN_BAD_RESULT("Wrong map extent " << theMap.Extent() );
515 TopoDS_Edge edge1 = TopoDS::Edge( theShape1 );
516 TopoDS_Edge edge2 = TopoDS::Edge( theShape2 );
517 if ( edge1.Orientation() >= TopAbs_INTERNAL ) edge1.Orientation( TopAbs_FORWARD );
518 if ( edge2.Orientation() >= TopAbs_INTERNAL ) edge2.Orientation( TopAbs_FORWARD );
519 TopoDS_Vertex VV1[2], VV2[2];
520 TopExp::Vertices( edge1, VV1[0], VV1[1] );
521 TopExp::Vertices( edge2, VV2[0], VV2[1] );
523 if ( theMap.IsBound( VV1[ i1 ] )) i1 = 1;
524 if ( theMap.IsBound( VV2[ i2 ] )) i2 = 1;
525 InsertAssociation( VV1[ i1 ], VV2[ i2 ], theMap );
526 InsertAssociation( theShape1, theShape2, theMap );
529 // ----------------------------------------------------------------------
530 case TopAbs_FACE: { // TopAbs_FACE
531 // ----------------------------------------------------------------------
532 TopoDS_Face face1 = TopoDS::Face( theShape1 );
533 TopoDS_Face face2 = TopoDS::Face( theShape2 );
534 if ( face1.Orientation() >= TopAbs_INTERNAL ) face1.Orientation( TopAbs_FORWARD );
535 if ( face2.Orientation() >= TopAbs_INTERNAL ) face2.Orientation( TopAbs_FORWARD );
537 TopoDS_Vertex VV1[2], VV2[2];
538 // find a not closed edge of face1 both vertices of which are associated
540 TopExp_Explorer exp ( face1, TopAbs_EDGE );
541 for ( ; VV2[ 1 ].IsNull() && exp.More(); exp.Next(), ++nbEdges ) {
542 TopExp::Vertices( TopoDS::Edge( exp.Current() ), VV1[0], VV1[1] );
543 if ( theMap.IsBound( VV1[0] ) ) {
544 VV2[ 0 ] = TopoDS::Vertex( theMap( VV1[0] ));
545 if ( theMap.IsBound( VV1[1] ) && !VV1[0].IsSame( VV1[1] ))
546 VV2[ 1 ] = TopoDS::Vertex( theMap( VV1[1] ));
549 if ( VV2[ 1 ].IsNull() ) { // 2 bound vertices not found
551 RETURN_BAD_RESULT("2 bound vertices not found" );
556 list< TopoDS_Edge > edges1, edges2;
557 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
558 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
559 FixAssocByPropagation( nbE, edges1, edges2, theMesh1, theMesh2 );
561 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
562 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
563 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
565 InsertAssociation( *eIt1, *eIt2, theMap );
566 VV1[0] = TopExp::FirstVertex( *eIt1, true );
567 VV2[0] = TopExp::FirstVertex( *eIt2, true );
568 InsertAssociation( VV1[0], VV2[0], theMap );
570 InsertAssociation( theShape1, theShape2, theMap );
573 // ----------------------------------------------------------------------
574 case TopAbs_SHELL: // TopAbs_SHELL, TopAbs_SOLID
576 // ----------------------------------------------------------------------
577 TopoDS_Vertex VV1[2], VV2[2];
578 // try to find a not closed edge of shape1 both vertices of which are associated
580 TopExp_Explorer exp ( theShape1, TopAbs_EDGE );
581 for ( ; VV2[ 1 ].IsNull() && exp.More(); exp.Next() ) {
582 edge1 = TopoDS::Edge( exp.Current() );
583 if ( edge1.Orientation() >= TopAbs_INTERNAL ) edge1.Orientation( TopAbs_FORWARD );
584 TopExp::Vertices( edge1 , VV1[0], VV1[1] );
585 if ( theMap.IsBound( VV1[0] )) {
586 VV2[ 0 ] = TopoDS::Vertex( theMap( VV1[0] ));
587 if ( theMap.IsBound( VV1[1] ) && !VV1[0].IsSame( VV1[1] ))
588 VV2[ 1 ] = TopoDS::Vertex( theMap( VV1[1] ));
591 if ( VV2[ 1 ].IsNull() ) // 2 bound vertices not found
592 RETURN_BAD_RESULT("2 bound vertices not found" );
593 // get an edge2 of theShape2 corresponding to edge1
594 TopoDS_Edge edge2 = GetEdgeByVertices( theMesh2, VV2[ 0 ], VV2[ 1 ]);
595 if ( edge2.IsNull() )
596 RETURN_BAD_RESULT("GetEdgeByVertices() failed");
598 // build map of edge to faces if shapes are not sub-shapes of main ones
599 bool isSubOfMain = false;
600 if ( SMESHDS_SubMesh * sm = theMesh1->GetMeshDS()->MeshElements( theShape1 ))
601 isSubOfMain = !sm->IsComplexSubmesh();
603 isSubOfMain = theMesh1->GetMeshDS()->ShapeToIndex( theShape1 );
604 TAncestorMap e2f1, e2f2;
605 const TAncestorMap& edgeToFace1 = isSubOfMain ? theMesh1->GetAncestorMap() : e2f1;
606 const TAncestorMap& edgeToFace2 = isSubOfMain ? theMesh2->GetAncestorMap() : e2f2;
608 TopExp::MapShapesAndAncestors( theShape1, TopAbs_EDGE, TopAbs_FACE, e2f1 );
609 TopExp::MapShapesAndAncestors( theShape2, TopAbs_EDGE, TopAbs_FACE, e2f2 );
610 if ( !edgeToFace1.Contains( edge1 ))
611 RETURN_BAD_RESULT("edge1 does not belong to theShape1");
612 if ( !edgeToFace2.Contains( edge2 ))
613 RETURN_BAD_RESULT("edge2 does not belong to theShape2");
616 // Look for 2 corresponing faces:
620 // get a face sharing edge1 (F1)
622 TopTools_ListIteratorOfListOfShape ancestIt1( edgeToFace1.FindFromKey( edge1 ));
623 for ( ; F1.IsNull() && ancestIt1.More(); ancestIt1.Next() )
624 if ( ancestIt1.Value().ShapeType() == TopAbs_FACE )
625 F1 = ancestIt1.Value().Oriented //( TopAbs_FORWARD );
626 ( SMESH_MesherHelper::GetSubShapeOri( theShape1, ancestIt1.Value() ));
628 RETURN_BAD_RESULT(" Face1 not found");
630 // get 2 faces sharing edge2 (one of them is F2)
631 TopTools_ListIteratorOfListOfShape ancestIt2( edgeToFace2.FindFromKey( edge2 ));
632 for ( int i = 0; FF2[1].IsNull() && ancestIt2.More(); ancestIt2.Next() )
633 if ( ancestIt2.Value().ShapeType() == TopAbs_FACE )
634 FF2[ i++ ] = ancestIt2.Value().Oriented // ( TopAbs_FORWARD );
635 ( SMESH_MesherHelper::GetSubShapeOri( theShape2, ancestIt2.Value() ));
637 // get oriented edge1 and edge2 from F1 and FF2[0]
638 for ( exp.Init( F1, TopAbs_EDGE ); exp.More(); exp.Next() )
639 if ( edge1.IsSame( exp.Current() )) {
640 edge1 = TopoDS::Edge( exp.Current() );
643 for ( exp.Init( FF2[ 0 ], TopAbs_EDGE ); exp.More(); exp.Next() )
644 if ( edge2.IsSame( exp.Current() )) {
645 edge2 = TopoDS::Edge( exp.Current() );
649 // compare first vertices of edge1 and edge2
650 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
651 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
652 F2 = FF2[ 0 ]; // (F2 !)
653 if ( !VV1[ 0 ].IsSame( theMap( VV2[ 0 ], /*is2=*/true))) {
655 if ( FF2[ 1 ].IsNull() )
661 TopTools_MapOfShape boundEdges;
663 // association of face sub-shapes and neighbour faces
664 list< pair < TopoDS_Face, TopoDS_Edge > > FE1, FE2;
665 list< pair < TopoDS_Face, TopoDS_Edge > >::iterator fe1, fe2;
666 FE1.push_back( make_pair( TopoDS::Face( F1 ), edge1 ));
667 FE2.push_back( make_pair( TopoDS::Face( F2 ), edge2 ));
668 for ( fe1 = FE1.begin(), fe2 = FE2.begin(); fe1 != FE1.end(); ++fe1, ++fe2 )
670 const TopoDS_Face& face1 = fe1->first;
671 if ( theMap.IsBound( face1 ) ) continue;
672 const TopoDS_Face& face2 = fe2->first;
675 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
676 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
677 list< TopoDS_Edge > edges1, edges2;
678 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
679 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
680 InsertAssociation( face1, face2, theMap ); // assoc faces
681 MESSAGE("Assoc FACE " << theMesh1->GetMeshDS()->ShapeToIndex( face1 )<<
682 " to " << theMesh2->GetMeshDS()->ShapeToIndex( face2 ));
683 if ( nbE == 2 && (edge1.IsSame( edges1.front())) != (edge2.IsSame( edges2.front())))
685 Reverse( edges2, nbE );
687 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
688 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
689 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
691 if ( !boundEdges.Add( *eIt1 )) continue; // already associated
692 InsertAssociation( *eIt1, *eIt2, theMap ); // assoc edges
693 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( *eIt1 )<<
694 " to " << theMesh2->GetMeshDS()->ShapeToIndex( *eIt2 ));
695 VV1[0] = TopExp::FirstVertex( *eIt1, true );
696 VV2[0] = TopExp::FirstVertex( *eIt2, true );
697 InsertAssociation( VV1[0], VV2[0], theMap ); // assoc vertices
698 MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[0] )<<
699 " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[0] ));
701 // add adjacent faces to process
702 TopoDS_Face nextFace1 = GetNextFace( edgeToFace1, *eIt1, face1 );
703 TopoDS_Face nextFace2 = GetNextFace( edgeToFace2, *eIt2, face2 );
704 if ( !nextFace1.IsNull() && !nextFace2.IsNull() ) {
705 FE1.push_back( make_pair( nextFace1, *eIt1 ));
706 FE2.push_back( make_pair( nextFace2, *eIt2 ));
710 InsertAssociation( theShape1, theShape2, theMap );
713 // ----------------------------------------------------------------------
714 case TopAbs_COMPOUND: { // GROUP
715 // ----------------------------------------------------------------------
716 // Maybe groups contain only one member
717 TopoDS_Iterator it1( theShape1 ), it2( theShape2 );
718 TopAbs_ShapeEnum memberType = it1.Value().ShapeType();
719 int nbMembers = SMESH_MesherHelper::Count( theShape1, memberType, true );
720 if ( nbMembers == 0 ) return true;
721 if ( nbMembers == 1 ) {
722 return FindSubShapeAssociation( it1.Value(), theMesh1, it2.Value(), theMesh2, theMap );
724 // Try to make shells of faces
726 BRep_Builder builder;
727 TopoDS_Shell shell1, shell2;
728 builder.MakeShell(shell1); builder.MakeShell(shell2);
729 if ( memberType == TopAbs_FACE ) {
730 // just add faces of groups to shells
731 for (; it1.More(); it1.Next(), it2.Next() )
732 builder.Add( shell1, it1.Value() ), builder.Add( shell2, it2.Value() );
734 else if ( memberType == TopAbs_EDGE ) {
735 // Try to add faces sharing more than one edge of a group or
736 // sharing all its vertices with the group
737 TopTools_IndexedMapOfShape groupVertices[2];
738 TopExp::MapShapes( theShape1, TopAbs_VERTEX, groupVertices[0]);
739 TopExp::MapShapes( theShape2, TopAbs_VERTEX, groupVertices[1]);
741 TopTools_MapOfShape groupEdges[2], addedFaces[2];
742 bool hasInitAssoc = (!theMap.IsEmpty()), initAssocOK = !hasInitAssoc;
743 for (; it1.More(); it1.Next(), it2.Next() ) {
744 groupEdges[0].Add( it1.Value() );
745 groupEdges[1].Add( it2.Value() );
746 if ( !initAssocOK ) {
747 // for shell association there must be an edge with both vertices bound
748 TopoDS_Vertex v1, v2;
749 TopExp::Vertices( TopoDS::Edge( it1.Value().Oriented(TopAbs_FORWARD)), v1, v2 );
750 initAssocOK = ( theMap.IsBound( v1 ) && theMap.IsBound( v2 ));
753 for (int is2ndGroup = 0; initAssocOK && is2ndGroup < 2; ++is2ndGroup) {
754 const TopoDS_Shape& group = is2ndGroup ? theShape2: theShape1;
755 SMESH_Mesh* mesh = is2ndGroup ? theMesh2 : theMesh1;
756 TopoDS_Shell& shell = is2ndGroup ? shell2 : shell1;
757 for ( TopoDS_Iterator it( group ); it.More(); it.Next() ) {
758 const TopoDS_Edge& edge = TopoDS::Edge( it.Value() );
760 for ( int iF = 0; iF < 2; ++iF ) { // loop on 2 faces sharing edge
761 face = GetNextFace(mesh->GetAncestorMap(), edge, face);
762 if ( !face.IsNull() ) {
763 int nbGroupEdges = 0;
764 for ( TopExp_Explorer f( face, TopAbs_EDGE ); f.More(); f.Next())
765 if ( groupEdges[ is2ndGroup ].Contains( f.Current() ))
766 if ( ++nbGroupEdges > 1 )
768 bool add = (nbGroupEdges > 1 ||
769 SMESH_MesherHelper::Count( face, TopAbs_EDGE, true ) == 1 );
772 for ( TopExp_Explorer v( face, TopAbs_VERTEX ); add && v.More(); v.Next())
773 add = groupVertices[ is2ndGroup ].Contains( v.Current() );
775 if ( add && addedFaces[ is2ndGroup ].Add( face ))
776 builder.Add( shell, face );
782 RETURN_BAD_RESULT("Unexpected group type");
786 int nbFaces1 = SMESH_MesherHelper:: Count( shell1, TopAbs_FACE, 0 );
787 int nbFaces2 = SMESH_MesherHelper:: Count( shell2, TopAbs_FACE, 0 );
788 if ( nbFaces1 != nbFaces2 )
789 RETURN_BAD_RESULT("Different nb of faces found for shells");
790 if ( nbFaces1 > 0 ) {
792 if ( nbFaces1 == 1 ) {
793 TopoDS_Shape F1 = TopoDS_Iterator( shell1 ).Value();
794 TopoDS_Shape F2 = TopoDS_Iterator( shell2 ).Value();
795 ok = FindSubShapeAssociation( F1, theMesh1, F2, theMesh2, theMap );
798 ok = FindSubShapeAssociation(shell1, theMesh1, shell2, theMesh2, theMap );
800 // Check if all members are mapped
802 TopTools_MapOfShape boundMembers[2];
804 for ( mIt.Initialize( theShape1 ); mIt.More(); mIt.Next())
805 if ( theMap.IsBound( mIt.Value() )) {
806 boundMembers[0].Add( mIt.Value() );
807 boundMembers[1].Add( theMap( mIt.Value() ));
809 if ( boundMembers[0].Extent() != nbMembers ) {
810 // make compounds of not bound members
811 TopoDS_Compound comp[2];
812 for ( int is2ndGroup = 0; is2ndGroup < 2; ++is2ndGroup ) {
813 builder.MakeCompound( comp[is2ndGroup] );
814 for ( mIt.Initialize( is2ndGroup ? theShape2:theShape1 ); mIt.More(); mIt.Next())
815 if ( ! boundMembers[ is2ndGroup ].Contains( mIt.Value() ))
816 builder.Add( comp[ is2ndGroup ], mIt.Value() );
818 // check if theMap contains initial association for the comp's
819 bool hasInitialAssoc = false;
820 if ( memberType == TopAbs_EDGE ) {
821 for ( TopExp_Explorer v( comp[0], TopAbs_VERTEX ); v.More(); v.Next())
822 if ( theMap.IsBound( v.Current() )) {
823 hasInitialAssoc = true;
827 if ( hasInitialAssoc == bool( !theMap.IsEmpty() ))
828 ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, theMap );
830 TShapeShapeMap tmpMap;
831 ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, tmpMap );
833 TopTools_DataMapIteratorOfDataMapOfShapeShape mapIt( tmpMap._map1to2 );
834 for ( ; mapIt.More(); mapIt.Next() )
835 theMap.Bind( mapIt.Key(), mapIt.Value());
842 // Each edge of an edge group is shared by own faces
843 // ------------------------------------------------------------------
845 // map vertices to edges sharing them, avoid doubling edges in lists
846 TopTools_DataMapOfShapeListOfShape v2e[2];
847 for (int isFirst = 0; isFirst < 2; ++isFirst ) {
848 const TopoDS_Shape& group = isFirst ? theShape1 : theShape2;
849 TopTools_DataMapOfShapeListOfShape& veMap = v2e[ isFirst ? 0 : 1 ];
850 TopTools_MapOfShape addedEdges;
851 for ( TopExp_Explorer e( group, TopAbs_EDGE ); e.More(); e.Next() ) {
852 const TopoDS_Shape& edge = e.Current();
853 if ( addedEdges.Add( edge )) {
854 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next()) {
855 const TopoDS_Shape& vertex = v.Current();
856 if ( !veMap.IsBound( vertex )) {
857 TopTools_ListOfShape l;
858 veMap.Bind( vertex, l );
860 veMap( vertex ).Append( edge );
865 while ( !v2e[0].IsEmpty() )
867 // find a bound vertex
869 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape v2eIt( v2e[0] );
870 for ( ; v2eIt.More(); v2eIt.Next())
871 if ( theMap.IsBound( v2eIt.Key() )) {
872 V[0] = TopoDS::Vertex( v2eIt.Key() );
873 V[1] = TopoDS::Vertex( theMap( V[0] ));
877 RETURN_BAD_RESULT("No more bound vertices");
879 while ( !V[0].IsNull() && v2e[0].IsBound( V[0] )) {
880 TopTools_ListOfShape& edges0 = v2e[0]( V[0] );
881 TopTools_ListOfShape& edges1 = v2e[1]( V[1] );
882 int nbE0 = edges0.Extent(), nbE1 = edges1.Extent();
884 RETURN_BAD_RESULT("Different nb of edges: "<< nbE0 << " != " << nbE1);
888 TopoDS_Edge e0 = TopoDS::Edge( edges0.First() );
889 TopoDS_Edge e1 = TopoDS::Edge( edges1.First() );
890 v2e[0].UnBind( V[0] );
891 v2e[1].UnBind( V[1] );
892 InsertAssociation( e0, e1, theMap );
893 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0 )<<
894 " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1 ));
895 V[0] = GetNextVertex( e0, V[0] );
896 V[1] = GetNextVertex( e1, V[1] );
897 if ( !V[0].IsNull() ) {
898 InsertAssociation( V[0], V[1], theMap );
899 MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( V[0] )<<
900 " to " << theMesh2->GetMeshDS()->ShapeToIndex( V[1] ));
903 else if ( nbE0 == 2 )
905 // one of edges must have both ends bound
906 TopoDS_Vertex v0e0 = GetNextVertex( TopoDS::Edge( edges0.First() ), V[0] );
907 TopoDS_Vertex v1e0 = GetNextVertex( TopoDS::Edge( edges0.Last() ), V[0] );
908 TopoDS_Vertex v0e1 = GetNextVertex( TopoDS::Edge( edges1.First() ), V[1] );
909 TopoDS_Vertex v1e1 = GetNextVertex( TopoDS::Edge( edges1.Last() ), V[1] );
910 TopoDS_Shape e0b, e1b, e0n, e1n, v1b; // bound and not-bound
911 TopoDS_Vertex v0n, v1n;
912 if ( theMap.IsBound( v0e0 )) {
913 v0n = v1e0; e0b = edges0.First(); e0n = edges0.Last(); v1b = theMap( v0e0 );
914 } else if ( theMap.IsBound( v1e0 )) {
915 v0n = v0e0; e0n = edges0.First(); e0b = edges0.Last(); v1b = theMap( v1e0 );
917 RETURN_BAD_RESULT("None of vertices bound");
919 if ( v1b.IsSame( v1e1 )) {
920 v1n = v0e1; e1n = edges1.First(); e1b = edges1.Last();
922 v1n = v1e1; e1b = edges1.First(); e1n = edges1.Last();
924 InsertAssociation( e0b, e1b, theMap );
925 InsertAssociation( e0n, e1n, theMap );
926 InsertAssociation( v0n, v1n, theMap );
927 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0b )<<
928 " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1b ));
929 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0n )<<
930 " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1n ));
931 MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( v0n )<<
932 " to " << theMesh2->GetMeshDS()->ShapeToIndex( v1n ));
933 v2e[0].UnBind( V[0] );
934 v2e[1].UnBind( V[1] );
939 RETURN_BAD_RESULT("Not implemented");
942 } //while ( !v2e[0].IsEmpty() )
947 RETURN_BAD_RESULT("Unexpected shape type");
949 } // end switch by shape type
950 } // end case of available initial vertex association
952 //======================================================================
953 // 4) NO INITIAL VERTEX ASSOCIATION
954 //======================================================================
956 switch ( theShape1.ShapeType() ) {
959 // ----------------------------------------------------------------------
960 TopoDS_Edge edge1 = TopoDS::Edge( theShape1 );
961 TopoDS_Edge edge2 = TopoDS::Edge( theShape2 );
962 if ( IsPropagationPossible( theMesh1, theMesh2 ))
964 TopoDS_Edge prpEdge = GetPropagationEdge( theMesh1, edge2, edge1 ).second;
965 if ( !prpEdge.IsNull() )
967 TopoDS_Vertex VV1[2], VV2[2];
968 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
969 TopExp::Vertices( prpEdge, VV2[0], VV2[1], true );
970 InsertAssociation( VV1[ 0 ], VV2[ 0 ], theMap );
971 InsertAssociation( VV1[ 1 ], VV2[ 1 ], theMap );
972 if ( VV1[0].IsSame( VV1[1] ) || // one of edges is closed
973 VV2[0].IsSame( VV2[1] ) )
975 InsertAssociation( edge1, prpEdge, theMap ); // insert with a proper orientation
977 InsertAssociation( theShape1, theShape2, theMap );
981 if ( SMESH_MesherHelper::IsClosedEdge( edge1 ) &&
982 SMESH_MesherHelper::IsClosedEdge( edge2 ))
984 // TODO: find out a proper orientation (is it possible?)
985 InsertAssociation( edge1, edge2, theMap ); // insert with a proper orientation
986 InsertAssociation( TopExp::FirstVertex(edge1), TopExp::FirstVertex(edge2),
988 InsertAssociation( theShape1, theShape2, theMap );
991 break; // try by vertex closeness
995 // ----------------------------------------------------------------------
996 if ( IsPropagationPossible( theMesh1, theMesh2 )) // try by propagation in one mesh
998 TopoDS_Face face1 = TopoDS::Face(theShape1);
999 TopoDS_Face face2 = TopoDS::Face(theShape2);
1000 if ( face1.Orientation() >= TopAbs_INTERNAL ) face1.Orientation( TopAbs_FORWARD );
1001 if ( face2.Orientation() >= TopAbs_INTERNAL ) face2.Orientation( TopAbs_FORWARD );
1002 TopoDS_Edge edge1, edge2;
1003 // get outer edge of theShape1
1004 TopoDS_Shape wire = OuterShape( face1, TopAbs_WIRE );
1005 //edge1 = TopoDS::Edge( OuterShape( face1, TopAbs_EDGE ));
1006 // use map to find the closest propagation edge
1007 map<int, pair< TopoDS_Edge, TopoDS_Edge > > propag_edges;
1008 for ( TopoDS_Iterator edgeIt( wire ); edgeIt.More(); edgeIt.Next() )
1010 edge1 = TopoDS::Edge( edgeIt.Value() );
1011 // find out if any edge of face2 is a propagation edge of outer edge1
1012 for ( TopExp_Explorer exp( face2, TopAbs_EDGE ); exp.More(); exp.Next() ) {
1013 edge2 = TopoDS::Edge( exp.Current() );
1014 pair<int,TopoDS_Edge> step_edge = GetPropagationEdge( theMesh1, edge2, edge1 );
1015 if ( !step_edge.second.IsNull() ) { // propagation found
1016 propag_edges.insert( make_pair( step_edge.first,
1017 ( make_pair( edge1, step_edge.second ))));
1018 if ( step_edge.first == 1 ) break; // most close found
1021 if ( !propag_edges.empty() && propag_edges.begin()->first == 1 ) break;
1023 if ( !propag_edges.empty() ) // propagation found
1025 edge1 = propag_edges.begin()->second.first;
1026 edge2 = propag_edges.begin()->second.second;
1027 TopoDS_Vertex VV1[2], VV2[2];
1028 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
1029 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
1030 list< TopoDS_Edge > edges1, edges2;
1031 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
1032 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
1033 // take care of proper association of propagated edges
1034 bool same1 = edge1.IsSame( edges1.front() );
1035 bool same2 = edge2.IsSame( edges2.front() );
1036 if ( same1 != same2 )
1038 Reverse(edges2, nbE);
1039 if ( nbE != 2 ) // 2 degen edges of 4 (issue 0021144)
1040 edges2.splice( edges2.end(), edges2, edges2.begin());
1042 // store association
1043 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
1044 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
1045 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
1047 InsertAssociation( *eIt1, *eIt2, theMap );
1048 VV1[0] = TopExp::FirstVertex( *eIt1, true );
1049 VV2[0] = TopExp::FirstVertex( *eIt2, true );
1050 InsertAssociation( VV1[0], VV2[0], theMap );
1052 InsertAssociation( theShape1, theShape2, theMap );
1056 break; // try by vertex closeness
1058 case TopAbs_COMPOUND: {
1059 // ----------------------------------------------------------------------
1060 if ( IsPropagationPossible( theMesh1, theMesh2 )) {
1062 // try to accosiate all using propagation
1063 if ( AssocGroupsByPropagation( theShape1, theShape2, *theMesh1, theMap ))
1066 // find a boundary edge of theShape1
1067 TopoDS_Edge E = GetBoundaryEdge( theShape1, *theMesh1 );
1069 break; // try by vertex closeness
1071 // find association for vertices of edge E
1072 TopoDS_Vertex VV1[2], VV2[2];
1073 for(TopExp_Explorer eexp(E, TopAbs_VERTEX); eexp.More(); eexp.Next()) {
1074 TopoDS_Vertex V1 = TopoDS::Vertex( eexp.Current() );
1075 // look for an edge ending in E whose one vertex is in theShape1
1076 // and the other, in theShape2
1077 const TopTools_ListOfShape& Ancestors = theMesh1->GetAncestors(V1);
1078 TopTools_ListIteratorOfListOfShape ita(Ancestors);
1079 for(; ita.More(); ita.Next()) {
1080 if( ita.Value().ShapeType() != TopAbs_EDGE ) continue;
1081 TopoDS_Edge edge = TopoDS::Edge(ita.Value());
1082 bool FromShape1 = false;
1083 for(TopExp_Explorer expe(theShape1, TopAbs_EDGE); expe.More(); expe.Next() ) {
1084 if(edge.IsSame(expe.Current())) {
1090 // is it an edge between theShape1 and theShape2?
1091 TopExp_Explorer expv(edge, TopAbs_VERTEX);
1092 TopoDS_Vertex V2 = TopoDS::Vertex( expv.Current() );
1095 V2 = TopoDS::Vertex( expv.Current() );
1097 bool FromShape2 = false;
1098 for ( expv.Init( theShape2, TopAbs_VERTEX ); expv.More(); expv.Next()) {
1099 if ( V2.IsSame( expv.Current() )) {
1105 if ( VV1[0].IsNull() )
1106 VV1[0] = V1, VV2[0] = V2;
1108 VV1[1] = V1, VV2[1] = V2;
1109 break; // from loop on ancestors of V1
1114 if ( !VV1[1].IsNull() ) {
1115 InsertAssociation( VV1[0], VV2[0], theMap );
1116 InsertAssociation( VV1[1], VV2[1], theMap );
1117 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap);
1120 break; // try by vertex closeness
1125 // 4.b) Find association by closeness of vertices
1126 // ----------------------------------------------
1128 TopTools_IndexedMapOfShape vMap1, vMap2;
1129 TopExp::MapShapes( theShape1, TopAbs_VERTEX, vMap1 );
1130 TopExp::MapShapes( theShape2, TopAbs_VERTEX, vMap2 );
1131 TopoDS_Vertex VV1[2], VV2[2];
1133 if ( vMap1.Extent() != vMap2.Extent() )
1135 if ( SMESH_MesherHelper:: Count( theShape1, TopAbs_EDGE, /*ignoreSame=*/false ) !=
1136 SMESH_MesherHelper:: Count( theShape2, TopAbs_EDGE, /*ignoreSame=*/false ))
1137 RETURN_BAD_RESULT("Different nb of vertices");
1140 if ( vMap1.Extent() == 1 ) {
1141 InsertAssociation( vMap1(1), vMap2(1), theMap );
1142 if ( theShape1.ShapeType() == TopAbs_EDGE ) {
1143 InsertAssociation( theShape1, theShape2, theMap );
1146 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap);
1149 // Try to associate by common vertices of an edge
1150 for ( int i = 1; i <= vMap1.Extent(); ++i )
1152 const TopoDS_Shape& v1 = vMap1(i);
1153 if ( vMap2.Contains( v1 ))
1155 // find an egde sharing v1 and sharing at the same time another common vertex
1156 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( v1, *theMesh1, TopAbs_EDGE);
1157 bool edgeFound = false;
1158 while ( edgeIt->more() && !edgeFound )
1160 TopoDS_Edge edge = TopoDS::Edge( edgeIt->next()->Oriented(TopAbs_FORWARD));
1161 TopExp::Vertices(edge, VV1[0], VV1[1]);
1162 if ( !VV1[0].IsSame( VV1[1] ))
1163 edgeFound = ( vMap2.Contains( VV1[ v1.IsSame(VV1[0]) ? 1:0]));
1167 InsertAssociation( VV1[0], VV1[0], theMap );
1168 InsertAssociation( VV1[1], VV1[1], theMap );
1169 if (FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap ))
1175 // Find transformation to make the shapes be of similar size at same location
1178 for ( int i = 1; i <= vMap1.Extent(); ++i )
1179 box[ 0 ].Add( BRep_Tool::Pnt ( TopoDS::Vertex( vMap1( i ))));
1180 for ( int i = 1; i <= vMap2.Extent(); ++i )
1181 box[ 1 ].Add( BRep_Tool::Pnt ( TopoDS::Vertex( vMap2( i ))));
1183 gp_Pnt gc[2]; // box center
1184 double x0,y0,z0, x1,y1,z1;
1185 box[0].Get( x0,y0,z0, x1,y1,z1 );
1186 gc[0] = 0.5 * ( gp_XYZ( x0,y0,z0 ) + gp_XYZ( x1,y1,z1 ));
1187 box[1].Get( x0,y0,z0, x1,y1,z1 );
1188 gc[1] = 0.5 * ( gp_XYZ( x0,y0,z0 ) + gp_XYZ( x1,y1,z1 ));
1191 gp_Vec vec01( gc[0], gc[1] );
1192 double scale = sqrt( box[1].SquareExtent() / box[0].SquareExtent() );
1194 // Find 2 closest vertices
1196 // get 2 linked vertices of shape 1 not belonging to an inner wire of a face
1197 std::list< TopoDS_Edge > allBndEdges1;
1198 if ( !getOuterEdges( theShape1, *theMesh1, allBndEdges1 ))
1199 RETURN_BAD_RESULT("Edge not found");
1201 std::list< TopoDS_Edge >::iterator edge1 = allBndEdges1.begin();
1202 double minDist = std::numeric_limits<double>::max();
1203 for ( int nbChecked=0; edge1 != allBndEdges1.end() && nbChecked++ < 10; ++edge1 )
1205 TopExp::Vertices( TopoDS::Edge( edge1->Oriented(TopAbs_FORWARD)), VV1[0], VV1[1]);
1206 if ( VV1[0].IsSame( VV1[1] ))
1207 continue;//RETURN_BAD_RESULT("Only closed edges");
1209 // find vertices closest to 2 linked vertices of shape 1
1210 double dist2[2] = { 1e+100, 1e+100 };
1211 TopoDS_Vertex edge2VV[2];
1212 for ( int i1 = 0; i1 < 2; ++i1 )
1214 gp_Pnt p1 = BRep_Tool::Pnt( VV1[ i1 ]);
1215 p1.Scale( gc[0], scale );
1216 p1.Translate( vec01 );
1218 // select a closest vertex among all ones in vMap2
1219 for ( int i2 = 1; i2 <= vMap2.Extent(); ++i2 )
1221 TopoDS_Vertex V2 = TopoDS::Vertex( vMap2( i2 ));
1222 gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
1223 double d2 = p1.SquareDistance( p2 );
1224 if ( d2 < dist2[ 0 ] && d2 < minDist ) {
1230 else if ( !edge2VV[0].IsNull() ) {
1231 // select a closest vertex among ends of edges meeting at edge2VV[0]
1232 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( edge2VV[0],
1233 *theMesh2, TopAbs_EDGE);
1234 while ( const TopoDS_Shape* edge2 = edgeIt->next() )
1235 for ( TopoDS_Iterator itV2( *edge2 ); itV2.More(); itV2.Next() )
1237 if ( itV2.Value().IsSame( edge2VV[ 0 ])) continue;
1238 if ( !vMap2.Contains( itV2.Value() )) continue;
1239 TopoDS_Vertex V2 = TopoDS::Vertex( itV2.Value() );
1240 gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
1241 double d2 = p1.SquareDistance( p2 );
1242 if ( d2 < dist2[1] && d2 < minDist ) {
1249 if ( dist2[0] + dist2[1] < minDist ) {
1250 VV2[0] = edge2VV[0];
1251 VV2[1] = edge2VV[1];
1252 minDist = dist2[0] + dist2[1];
1253 if ( minDist < 1e-10 )
1258 InsertAssociation( VV1[ 0 ], VV2[ 0 ], theMap );
1259 InsertAssociation( VV1[ 1 ], VV2[ 1 ], theMap );
1260 MESSAGE("Initial assoc VERT " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[ 0 ] )<<
1261 " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[ 0 ] )<<
1262 "\nand VERT " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[ 1 ] )<<
1263 " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[ 1 ] ));
1264 if ( theShape1.ShapeType() == TopAbs_EDGE ) {
1265 InsertAssociation( theShape1, theShape2, theMap );
1269 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap );
1272 //================================================================================
1274 * Find association of edges of faces
1275 * \param face1 - face 1
1276 * \param VV1 - vertices of face 1
1277 * \param face2 - face 2
1278 * \param VV2 - vertices of face 2 associated with ones of face 1
1279 * \param edges1 - out list of edges of face 1
1280 * \param edges2 - out list of edges of face 2
1281 * \retval int - nb of edges in an outer wire in a success case, else zero
1283 //================================================================================
1285 int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face& face1,
1286 TopoDS_Vertex VV1[2],
1287 const TopoDS_Face& face2,
1288 TopoDS_Vertex VV2[2],
1289 list< TopoDS_Edge > & edges1,
1290 list< TopoDS_Edge > & edges2)
1293 list< int > nbEInW1, nbEInW2;
1294 list< TopoDS_Edge >::iterator edgeIt;
1295 int i_ok_wire_algo = -1;
1296 for ( int outer_wire_algo = 0; outer_wire_algo < 2 && !OK; ++outer_wire_algo )
1301 if ( SMESH_Block::GetOrderedEdges( face1, edges1, nbEInW1, VV1[0], outer_wire_algo) !=
1302 SMESH_Block::GetOrderedEdges( face2, edges2, nbEInW2, VV2[0], outer_wire_algo) )
1303 CONT_BAD_RESULT("Different number of wires in faces ");
1305 if ( nbEInW1 != nbEInW2 && outer_wire_algo == 0 &&
1306 ( std::accumulate( nbEInW1.begin(), nbEInW1.end(), 0) !=
1307 std::accumulate( nbEInW2.begin(), nbEInW2.end(), 0)))
1308 RETURN_BAD_RESULT("Different number of edges in faces");
1310 if ( nbEInW1.front() != nbEInW2.front() )
1311 CONT_BAD_RESULT("Different number of edges in the outer wire: " <<
1312 nbEInW1.front() << " != " << nbEInW2.front());
1314 i_ok_wire_algo = outer_wire_algo;
1316 // Define if we need to reverse one of wires to make edges in lists match each other
1318 bool reverse = false;
1320 if ( !VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) {
1322 edgeIt = --edges1.end();
1323 // check if the second vertex belongs to the first or last edge in the wire
1324 if ( !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
1325 bool KO = true; // belongs to none
1326 if ( nbEInW1.size() > 1 ) { // several wires
1327 edgeIt = edges1.begin();
1328 std::advance( edgeIt, nbEInW1.front()-1 );
1329 KO = !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ));
1332 CONT_BAD_RESULT("GetOrderedEdges() failed");
1335 edgeIt = --edges2.end();
1336 if ( !VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))) {
1338 // check if the second vertex belongs to the first or last edge in the wire
1339 if ( !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
1340 bool KO = true; // belongs to none
1341 if ( nbEInW2.size() > 1 ) { // several wires
1342 edgeIt = edges2.begin();
1343 std::advance( edgeIt, nbEInW2.front()-1 );
1344 KO = !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ));
1347 CONT_BAD_RESULT("GetOrderedEdges() failed");
1352 Reverse( edges2 , nbEInW2.front());
1353 if (( VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) !=
1354 ( VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))))
1355 CONT_BAD_RESULT("GetOrderedEdges() failed");
1359 } // loop algos getting an outer wire
1361 // Try to orient all (if !OK) or only internal wires (issue 0020996) by UV similarity
1363 if (( !OK || nbEInW1.size() > 1 ) && i_ok_wire_algo > -1 )
1365 // Check that Vec(VV1[0],VV1[1]) in 2D on face1 is the same
1366 // as Vec(VV2[0],VV2[1]) on face2
1367 double vTol = BRep_Tool::Tolerance( VV1[0] );
1368 BRepAdaptor_Surface surface1( face1, false );
1370 surface1.UResolution( vTol ) + surface1.VResolution( vTol ); // let's be tolerant
1371 gp_Pnt2d v0f1UV = BRep_Tool::Parameters( VV1[0], face1 );
1372 gp_Pnt2d v0f2UV = BRep_Tool::Parameters( VV2[0], face2 );
1373 gp_Pnt2d v1f1UV = BRep_Tool::Parameters( VV1[1], face1 );
1374 gp_Pnt2d v1f2UV = BRep_Tool::Parameters( VV2[1], face2 );
1375 gp_Vec2d v01f1Vec( v0f1UV, v1f1UV );
1376 gp_Vec2d v01f2Vec( v0f2UV, v1f2UV );
1377 if ( Abs( v01f1Vec.X()-v01f2Vec.X()) < vTolUV &&
1378 Abs( v01f1Vec.Y()-v01f2Vec.Y()) < vTolUV )
1380 if ( !OK /*i_ok_wire_algo != 1*/ )
1384 SMESH_Block::GetOrderedEdges( face1, edges1, nbEInW1, VV1[0], i_ok_wire_algo);
1385 SMESH_Block::GetOrderedEdges( face2, edges2, nbEInW2, VV2[0], i_ok_wire_algo);
1387 gp_XY dUV = v0f2UV.XY() - v0f1UV.XY(); // UV shift between 2 faces
1389 // skip edges of the outer wire (if the outer wire is OK)
1390 list< int >::iterator nbE2, nbE1 = nbEInW1.begin();
1391 list< TopoDS_Edge >::iterator edge2Beg, edge1Beg = edges1.begin();
1392 if ( OK ) std::advance( edge1Beg, *nbE1++ );
1393 // reach an end of edges of a current wire1
1394 list< TopoDS_Edge >::iterator edge2End, edge1End;
1396 // find corresponding wires of face2
1397 for ( int iW1 = OK; nbE1 != nbEInW1.end(); ++nbE1, ++iW1 ) // loop on wires of face1
1399 // reach an end of edges of a current wire1
1400 edge1End = edge1Beg;
1401 std::advance( edge1End, *nbE1 );
1402 // UV on face1 to find on face2
1403 v0f1UV = BRep_Tool::Parameters( TopExp::FirstVertex(*edge1Beg,true), face1 );
1404 v1f1UV = BRep_Tool::Parameters( TopExp::LastVertex (*edge1Beg,true), face1 );
1405 v0f1UV.ChangeCoord() += dUV;
1406 v1f1UV.ChangeCoord() += dUV;
1408 // look through wires of face2
1409 edge2Beg = edges2.begin();
1410 nbE2 = nbEInW2.begin();
1411 if ( OK ) std::advance( edge2Beg, *nbE2++ );
1412 for ( int iW2 = OK; nbE2 != nbEInW2.end(); ++nbE2, ++iW2 ) // loop on wires of face2
1414 // reach an end of edges of a current wire2
1415 edge2End = edge2Beg;
1416 std::advance( edge2End, *nbE2 );
1417 if ( *nbE1 == *nbE2 && iW2 >= iW1 )
1419 // rotate edge2 untill coincidence with edge1 in 2D
1421 while ( i-- > 0 && !sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
1422 // move edge2Beg to place before edge2End
1423 edges2.splice( edge2End, edges2, edge2Beg++ );
1425 if ( sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
1427 if ( iW1 == 0 ) OK = true; // OK is for the first wire
1428 // reverse edges2 if needed
1429 if ( !sameVertexUV( *edge2Beg, face2, 1, v1f1UV, vTolUV ))
1430 Reverse( edges2 , *nbE2, std::distance( edges2.begin(),edge2Beg ));
1431 // put wire2 at a right place within edges2
1433 list< TopoDS_Edge >::iterator place2 = edges2.begin();
1434 std::advance( place2, std::distance( edges1.begin(), edge1Beg ));
1435 edges2.splice( place2, edges2, edge2Beg, edge2End );
1436 // move nbE2 as well
1437 list< int >::iterator placeNbE2 = nbEInW2.begin();
1438 std::advance( placeNbE2, iW1 );
1439 nbEInW2.splice( placeNbE2, nbEInW2, nbE2 );
1444 // prepare to the next wire loop
1445 edge2Beg = edge2End;
1447 edge1Beg = edge1End;
1452 const int nbEdges = nbEInW1.front();
1453 if ( OK && nbEdges == 2 )
1455 // if wires include 2 edges, it's impossible to associate them using
1456 // topological information only. Try to use length of edges for association.
1457 double l1[2], l2[2];
1458 edgeIt = edges1.begin();
1459 l1[0] = SMESH_Algo::EdgeLength( *edgeIt++ );
1460 l1[1] = SMESH_Algo::EdgeLength( *edgeIt++ );
1461 if ( Abs( l1[0] - l1[1] ) > 0.1 * Max( l1[0], l1[1] ) )
1463 edgeIt = edges2.begin();
1464 l2[0] = SMESH_Algo::EdgeLength( *edgeIt++ );
1465 l2[1] = SMESH_Algo::EdgeLength( *edgeIt++ );
1466 if (( l1[0] < l1[1] ) != ( l2[0] < l2[1] ))
1468 Reverse( edges2, nbEdges );
1473 return OK ? nbEInW1.front() : 0;
1476 //=======================================================================
1477 //function : InitVertexAssociation
1479 //=======================================================================
1481 void StdMeshers_ProjectionUtils::InitVertexAssociation( const SMESH_Hypothesis* theHyp,
1482 TShapeShapeMap & theAssociationMap)
1484 string hypName = theHyp->GetName();
1485 if ( hypName == "ProjectionSource1D" ) {
1486 const StdMeshers_ProjectionSource1D * hyp =
1487 static_cast<const StdMeshers_ProjectionSource1D*>( theHyp );
1488 if ( hyp->HasVertexAssociation() )
1489 InsertAssociation( hyp->GetTargetVertex(),hyp->GetSourceVertex(),theAssociationMap );
1491 else if ( hypName == "ProjectionSource2D" ) {
1492 const StdMeshers_ProjectionSource2D * hyp =
1493 static_cast<const StdMeshers_ProjectionSource2D*>( theHyp );
1494 if ( hyp->HasVertexAssociation() ) {
1495 InsertAssociation( hyp->GetTargetVertex(1),hyp->GetSourceVertex(1),theAssociationMap);
1496 InsertAssociation( hyp->GetTargetVertex(2),hyp->GetSourceVertex(2),theAssociationMap);
1499 else if ( hypName == "ProjectionSource3D" ) {
1500 const StdMeshers_ProjectionSource3D * hyp =
1501 static_cast<const StdMeshers_ProjectionSource3D*>( theHyp );
1502 if ( hyp->HasVertexAssociation() ) {
1503 InsertAssociation( hyp->GetTargetVertex(1),hyp->GetSourceVertex(1),theAssociationMap);
1504 InsertAssociation( hyp->GetTargetVertex(2),hyp->GetSourceVertex(2),theAssociationMap);
1509 //=======================================================================
1511 * Inserts association theShape1 <-> theShape2 to TShapeShapeMap
1512 * \param theShape1 - target shape
1513 * \param theShape2 - source shape
1514 * \param theAssociationMap - association map
1515 * \retval bool - true if there was no association for these shapes before
1517 //=======================================================================
1519 bool StdMeshers_ProjectionUtils::InsertAssociation( const TopoDS_Shape& theShape1, // tgt
1520 const TopoDS_Shape& theShape2, // src
1521 TShapeShapeMap & theAssociationMap)
1523 if ( !theShape1.IsNull() && !theShape2.IsNull() ) {
1524 SHOW_SHAPE(theShape1,"Assoc ");
1525 SHOW_SHAPE(theShape2," to ");
1526 bool isNew = ( theAssociationMap.Bind( theShape1, theShape2 ));
1530 throw SALOME_Exception("StdMeshers_ProjectionUtils: attempt to associate NULL shape");
1535 //=======================================================================
1537 * Finds an edge by its vertices in a main shape of the mesh
1538 * \param aMesh - the mesh
1539 * \param V1 - vertex 1
1540 * \param V2 - vertex 2
1541 * \retval TopoDS_Edge - found edge
1543 //=======================================================================
1545 TopoDS_Edge StdMeshers_ProjectionUtils::GetEdgeByVertices( SMESH_Mesh* theMesh,
1546 const TopoDS_Vertex& theV1,
1547 const TopoDS_Vertex& theV2)
1549 if ( theMesh && !theV1.IsNull() && !theV2.IsNull() )
1551 TopTools_ListIteratorOfListOfShape ancestorIt( theMesh->GetAncestors( theV1 ));
1552 for ( ; ancestorIt.More(); ancestorIt.Next() )
1553 if ( ancestorIt.Value().ShapeType() == TopAbs_EDGE )
1554 for ( TopExp_Explorer expV ( ancestorIt.Value(), TopAbs_VERTEX );
1557 if ( theV2.IsSame( expV.Current() ))
1558 return TopoDS::Edge( ancestorIt.Value() );
1560 return TopoDS_Edge();
1563 //================================================================================
1565 * Return another face sharing an edge
1566 * \param edgeToFaces - data map of descendants to ancestors
1567 * \param edge - edge
1568 * \param face - face
1569 * \retval TopoDS_Face - found face
1571 //================================================================================
1573 TopoDS_Face StdMeshers_ProjectionUtils::GetNextFace( const TAncestorMap& edgeToFaces,
1574 const TopoDS_Edge& edge,
1575 const TopoDS_Face& face)
1577 // if ( !edge.IsNull() && !face.IsNull() && edgeToFaces.Contains( edge ))
1578 if ( !edge.IsNull() && edgeToFaces.Contains( edge )) // PAL16202
1580 TopTools_ListIteratorOfListOfShape ancestorIt( edgeToFaces.FindFromKey( edge ));
1581 for ( ; ancestorIt.More(); ancestorIt.Next() )
1582 if ( ancestorIt.Value().ShapeType() == TopAbs_FACE &&
1583 !face.IsSame( ancestorIt.Value() ))
1584 return TopoDS::Face( ancestorIt.Value() );
1586 return TopoDS_Face();
1589 //================================================================================
1591 * Return other vertex of an edge
1593 //================================================================================
1595 TopoDS_Vertex StdMeshers_ProjectionUtils::GetNextVertex(const TopoDS_Edge& edge,
1596 const TopoDS_Vertex& vertex)
1598 TopoDS_Vertex vF,vL;
1599 TopExp::Vertices(edge,vF,vL);
1600 if ( vF.IsSame( vL ))
1601 return TopoDS_Vertex();
1602 return vertex.IsSame( vF ) ? vL : vF;
1605 //================================================================================
1607 * Return a propagation edge
1608 * \param aMesh - mesh
1609 * \param theEdge - edge to find by propagation
1610 * \param fromEdge - start edge for propagation
1611 * \retval pair<int,TopoDS_Edge> - propagation step and found edge
1613 //================================================================================
1615 pair<int,TopoDS_Edge>
1616 StdMeshers_ProjectionUtils::GetPropagationEdge( SMESH_Mesh* aMesh,
1617 const TopoDS_Edge& theEdge,
1618 const TopoDS_Edge& fromEdge)
1620 TopTools_IndexedMapOfShape aChain;
1623 // List of edges, added to chain on the previous cycle pass
1624 TopTools_ListOfShape listPrevEdges;
1625 listPrevEdges.Append(fromEdge);
1627 // Collect all edges pass by pass
1628 while (listPrevEdges.Extent() > 0) {
1630 // List of edges, added to chain on this cycle pass
1631 TopTools_ListOfShape listCurEdges;
1633 // Find the next portion of edges
1634 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1635 for (; itE.More(); itE.Next()) {
1636 TopoDS_Shape anE = itE.Value();
1638 // Iterate on faces, having edge <anE>
1639 TopTools_ListIteratorOfListOfShape itA (aMesh->GetAncestors(anE));
1640 for (; itA.More(); itA.Next()) {
1641 TopoDS_Shape aW = itA.Value();
1643 // There are objects of different type among the ancestors of edge
1644 if (aW.ShapeType() == TopAbs_WIRE) {
1645 TopoDS_Shape anOppE;
1647 BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
1648 Standard_Integer nb = 1, found = 0;
1649 TopTools_Array1OfShape anEdges (1,4);
1650 for (; aWE.More(); aWE.Next(), nb++) {
1655 anEdges(nb) = aWE.Current();
1656 if (anEdges(nb).IsSame(anE)) found = nb;
1659 if (nb == 5 && found > 0) {
1660 // Quadrangle face found, get an opposite edge
1661 Standard_Integer opp = found + 2;
1662 if (opp > 4) opp -= 4;
1663 anOppE = anEdges(opp);
1665 // add anOppE to aChain if ...
1666 if (!aChain.Contains(anOppE)) { // ... anOppE is not in aChain
1667 // Add found edge to the chain oriented so that to
1668 // have it co-directed with a forward MainEdge
1669 TopAbs_Orientation ori = anE.Orientation();
1670 if ( anEdges(opp).Orientation() == anEdges(found).Orientation() )
1671 ori = TopAbs::Reverse( ori );
1672 anOppE.Orientation( ori );
1673 if ( anOppE.IsSame( theEdge ))
1674 return make_pair( step, TopoDS::Edge( anOppE ));
1676 listCurEdges.Append(anOppE);
1678 } // if (nb == 5 && found > 0)
1679 } // if (aF.ShapeType() == TopAbs_WIRE)
1680 } // for (; itF.More(); itF.Next())
1681 } // for (; itE.More(); itE.Next())
1683 listPrevEdges = listCurEdges;
1684 } // while (listPrevEdges.Extent() > 0)
1686 return make_pair( INT_MAX, TopoDS_Edge());
1689 //================================================================================
1691 * Find corresponding nodes on two faces
1692 * \param face1 - the first face
1693 * \param mesh1 - mesh containing elements on the first face
1694 * \param face2 - the second face
1695 * \param mesh2 - mesh containing elements on the second face
1696 * \param assocMap - map associating sub-shapes of the faces
1697 * \param node1To2Map - map containing found matching nodes
1698 * \retval bool - is a success
1700 //================================================================================
1702 bool StdMeshers_ProjectionUtils::
1703 FindMatchingNodesOnFaces( const TopoDS_Face& face1,
1705 const TopoDS_Face& face2,
1707 const TShapeShapeMap & assocMap,
1708 TNodeNodeMap & node1To2Map)
1710 SMESHDS_Mesh* meshDS1 = mesh1->GetMeshDS();
1711 SMESHDS_Mesh* meshDS2 = mesh2->GetMeshDS();
1713 SMESH_MesherHelper helper1( *mesh1 );
1714 SMESH_MesherHelper helper2( *mesh2 );
1716 // Get corresponding submeshes and roughly check match of meshes
1718 SMESHDS_SubMesh * SM2 = meshDS2->MeshElements( face2 );
1719 SMESHDS_SubMesh * SM1 = meshDS1->MeshElements( face1 );
1721 RETURN_BAD_RESULT("Empty submeshes");
1722 if ( SM2->NbNodes() != SM1->NbNodes() ||
1723 SM2->NbElements() != SM1->NbElements() )
1724 RETURN_BAD_RESULT("Different meshes on corresponding faces "
1725 << meshDS1->ShapeToIndex( face1 ) << " and "
1726 << meshDS2->ShapeToIndex( face2 ));
1727 if ( SM2->NbElements() == 0 )
1728 RETURN_BAD_RESULT("Empty submeshes");
1730 helper1.SetSubShape( face1 );
1731 helper2.SetSubShape( face2 );
1732 if ( helper1.HasSeam() != helper2.HasSeam() )
1733 RETURN_BAD_RESULT("Different faces' geometry");
1735 // Data to call SMESH_MeshEditor::FindMatchingNodes():
1737 // 1. Nodes of corresponding links:
1739 // get 2 matching edges, try to find not seam ones
1740 TopoDS_Edge edge1, edge2, seam1, seam2, anyEdge1, anyEdge2;
1741 TopExp_Explorer eE( OuterShape( face2, TopAbs_WIRE ), TopAbs_EDGE );
1744 TopoDS_Edge e2 = TopoDS::Edge( eE.Current() );
1747 if ( !assocMap.IsBound( e2, /*is2nd=*/true ))
1748 RETURN_BAD_RESULT("Association not found for edge " << meshDS2->ShapeToIndex( e2 ));
1749 TopoDS_Edge e1 = TopoDS::Edge( assocMap( e2, /*is2nd=*/true ));
1750 if ( !helper1.IsSubShape( e1, face1 ))
1751 RETURN_BAD_RESULT("Wrong association, edge " << meshDS1->ShapeToIndex( e1 ) <<
1752 " isn't a sub-shape of face " << meshDS1->ShapeToIndex( face1 ));
1753 // check that there are nodes on edges
1754 SMESHDS_SubMesh * eSM1 = meshDS1->MeshElements( e1 );
1755 SMESHDS_SubMesh * eSM2 = meshDS2->MeshElements( e2 );
1756 bool nodesOnEdges = ( eSM1 && eSM2 && eSM1->NbNodes() && eSM2->NbNodes() );
1757 // check that the nodes on edges belong to faces
1758 // (as NETGEN ignores nodes on the degenerated geom edge)
1759 bool nodesOfFaces = false;
1760 if ( nodesOnEdges ) {
1761 const SMDS_MeshNode* n1 = eSM1->GetNodes()->next();
1762 const SMDS_MeshNode* n2 = eSM2->GetNodes()->next();
1763 nodesOfFaces = ( n1->GetInverseElementIterator(SMDSAbs_Face)->more() &&
1764 n2->GetInverseElementIterator(SMDSAbs_Face)->more() );
1768 if ( helper2.IsRealSeam( e2 )) {
1769 seam1 = e1; seam2 = e2;
1772 edge1 = e1; edge2 = e2;
1776 anyEdge1 = e1; anyEdge2 = e2;
1778 } while ( edge2.IsNull() && eE.More() );
1780 if ( edge2.IsNull() ) {
1781 edge1 = seam1; edge2 = seam2;
1783 bool hasNodesOnEdge = (! edge2.IsNull() );
1784 if ( !hasNodesOnEdge ) {
1785 // 0020338 - nb segments == 1
1786 edge1 = anyEdge1; edge2 = anyEdge2;
1789 // get 2 matching vertices
1790 TopoDS_Vertex V2 = TopExp::FirstVertex( TopoDS::Edge( edge2 ));
1791 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
1792 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
1793 TopoDS_Vertex V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
1795 // nodes on vertices
1796 const SMDS_MeshNode* vNode1 = SMESH_Algo::VertexNode( V1, meshDS1 );
1797 const SMDS_MeshNode* vNode2 = SMESH_Algo::VertexNode( V2, meshDS2 );
1798 if ( !vNode1 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
1799 if ( !vNode2 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
1801 // nodes on edges linked with nodes on vertices
1802 const SMDS_MeshNode* nullNode = 0;
1803 vector< const SMDS_MeshNode*> eNode1( 2, nullNode );
1804 vector< const SMDS_MeshNode*> eNode2( 2, nullNode );
1805 if ( hasNodesOnEdge )
1807 int nbNodeToGet = 1;
1808 if ( helper1.IsClosedEdge( edge1 ) || helper2.IsClosedEdge( edge2 ) )
1810 for ( int is2 = 0; is2 < 2; ++is2 )
1812 TopoDS_Edge & edge = is2 ? edge2 : edge1;
1813 SMESHDS_Mesh * smDS = is2 ? meshDS2 : meshDS1;
1814 SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
1815 // nodes linked with ones on vertices
1816 const SMDS_MeshNode* vNode = is2 ? vNode2 : vNode1;
1817 vector< const SMDS_MeshNode*>& eNode = is2 ? eNode2 : eNode1;
1819 SMDS_ElemIteratorPtr vElem = vNode->GetInverseElementIterator(SMDSAbs_Edge);
1820 while ( vElem->more() && nbGotNode != nbNodeToGet ) {
1821 const SMDS_MeshElement* elem = vElem->next();
1822 if ( edgeSM->Contains( elem ))
1823 eNode[ nbGotNode++ ] =
1824 ( elem->GetNode(0) == vNode ) ? elem->GetNode(1) : elem->GetNode(0);
1826 if ( nbGotNode > 1 ) // sort found nodes by param on edge
1828 SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
1829 double u0 = helper->GetNodeU( edge, eNode[ 0 ]);
1830 double u1 = helper->GetNodeU( edge, eNode[ 1 ]);
1831 if ( u0 > u1 ) std::swap( eNode[ 0 ], eNode[ 1 ]);
1833 if ( nbGotNode == 0 )
1834 RETURN_BAD_RESULT("Found no nodes on edge " << smDS->ShapeToIndex( edge ) <<
1835 " linked to " << vNode );
1838 else // 0020338 - nb segments == 1
1840 // get 2 other matching vertices
1841 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
1842 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
1843 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
1844 V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
1846 // nodes on vertices
1847 eNode1[0] = SMESH_Algo::VertexNode( V1, meshDS1 );
1848 eNode2[0] = SMESH_Algo::VertexNode( V2, meshDS2 );
1849 if ( !eNode1[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
1850 if ( !eNode2[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
1855 set<const SMDS_MeshElement*> Elems1, Elems2;
1856 for ( int is2 = 0; is2 < 2; ++is2 )
1858 set<const SMDS_MeshElement*> & elems = is2 ? Elems2 : Elems1;
1859 SMESHDS_SubMesh* sm = is2 ? SM2 : SM1;
1860 SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
1861 const TopoDS_Face & face = is2 ? face2 : face1;
1862 SMDS_ElemIteratorPtr eIt = sm->GetElements();
1864 if ( !helper->IsRealSeam( is2 ? edge2 : edge1 ))
1866 while ( eIt->more() ) elems.insert( eIt->next() );
1870 // the only suitable edge is seam, i.e. it is a sphere.
1871 // FindMatchingNodes() will not know which way to go from any edge.
1872 // So we ignore all faces having nodes on edges or vertices except
1873 // one of faces sharing current start nodes
1875 // find a face to keep
1876 const SMDS_MeshElement* faceToKeep = 0;
1877 const SMDS_MeshNode* vNode = is2 ? vNode2 : vNode1;
1878 const SMDS_MeshNode* eNode = is2 ? eNode2[0] : eNode1[0];
1879 TIDSortedElemSet inSet, notInSet;
1881 const SMDS_MeshElement* f1 =
1882 SMESH_MeshAlgos::FindFaceInSet( vNode, eNode, inSet, notInSet );
1883 if ( !f1 ) RETURN_BAD_RESULT("The first face on seam not found");
1884 notInSet.insert( f1 );
1886 const SMDS_MeshElement* f2 =
1887 SMESH_MeshAlgos::FindFaceInSet( vNode, eNode, inSet, notInSet );
1888 if ( !f2 ) RETURN_BAD_RESULT("The second face on seam not found");
1890 // select a face with less UV of vNode
1891 const SMDS_MeshNode* notSeamNode[2] = {0, 0};
1892 for ( int iF = 0; iF < 2; ++iF ) {
1893 const SMDS_MeshElement* f = ( iF ? f2 : f1 );
1894 for ( int i = 0; !notSeamNode[ iF ] && i < f->NbNodes(); ++i ) {
1895 const SMDS_MeshNode* node = f->GetNode( i );
1896 if ( !helper->IsSeamShape( node->getshapeId() ))
1897 notSeamNode[ iF ] = node;
1900 gp_Pnt2d uv1 = helper->GetNodeUV( face, vNode, notSeamNode[0] );
1901 gp_Pnt2d uv2 = helper->GetNodeUV( face, vNode, notSeamNode[1] );
1902 if ( uv1.X() + uv1.Y() > uv2.X() + uv2.Y() )
1908 elems.insert( faceToKeep );
1909 while ( eIt->more() ) {
1910 const SMDS_MeshElement* f = eIt->next();
1911 int nbNodes = f->NbNodes();
1912 if ( f->IsQuadratic() )
1915 for ( int i = 0; !onBnd && i < nbNodes; ++i ) {
1916 const SMDS_MeshNode* node = f->GetNode( i );
1917 onBnd = ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE);
1922 // add also faces adjacent to faceToKeep
1923 int nbNodes = faceToKeep->NbNodes();
1924 if ( faceToKeep->IsQuadratic() ) nbNodes /= 2;
1925 notInSet.insert( f1 );
1926 notInSet.insert( f2 );
1927 for ( int i = 0; i < nbNodes; ++i ) {
1928 const SMDS_MeshNode* n1 = faceToKeep->GetNode( i );
1929 const SMDS_MeshNode* n2 = faceToKeep->GetNode(( i+1 ) % nbNodes );
1930 f1 = SMESH_MeshAlgos::FindFaceInSet( n1, n2, inSet, notInSet );
1934 } // case on a sphere
1935 } // loop on 2 faces
1937 // int quadFactor = (*Elems1.begin())->IsQuadratic() ? 2 : 1;
1939 node1To2Map.clear();
1940 int res = SMESH_MeshEditor::FindMatchingNodes( Elems1, Elems2,
1942 eNode1[0], eNode2[0],
1944 if ( res != SMESH_MeshEditor::SEW_OK )
1945 RETURN_BAD_RESULT("FindMatchingNodes() result " << res );
1947 // On a sphere, add matching nodes on the edge
1949 if ( helper1.IsRealSeam( edge1 ))
1951 // sort nodes on edges by param on edge
1952 map< double, const SMDS_MeshNode* > u2nodesMaps[2];
1953 for ( int is2 = 0; is2 < 2; ++is2 )
1955 TopoDS_Edge & edge = is2 ? edge2 : edge1;
1956 SMESHDS_Mesh * smDS = is2 ? meshDS2 : meshDS1;
1957 SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
1958 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[ is2 ];
1960 SMDS_NodeIteratorPtr nIt = edgeSM->GetNodes();
1961 while ( nIt->more() ) {
1962 const SMDS_MeshNode* node = nIt->next();
1963 const SMDS_EdgePosition* pos =
1964 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1965 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1967 if ( pos2nodes.size() != edgeSM->NbNodes() )
1968 RETURN_BAD_RESULT("Equal params of nodes on edge "
1969 << smDS->ShapeToIndex( edge ) << " of face " << is2 );
1971 if ( u2nodesMaps[0].size() != u2nodesMaps[1].size() )
1972 RETURN_BAD_RESULT("Different nb of new nodes on edges or wrong params");
1974 // compare edge orientation
1975 double u1 = helper1.GetNodeU( edge1, vNode1 );
1976 double u2 = helper2.GetNodeU( edge2, vNode2 );
1977 bool isFirst1 = ( u1 < u2nodesMaps[0].begin()->first );
1978 bool isFirst2 = ( u2 < u2nodesMaps[1].begin()->first );
1979 bool reverse ( isFirst1 != isFirst2 );
1981 // associate matching nodes
1982 map< double, const SMDS_MeshNode* >::iterator u_Node1, u_Node2, end1;
1983 map< double, const SMDS_MeshNode* >::reverse_iterator uR_Node2;
1984 u_Node1 = u2nodesMaps[0].begin();
1985 u_Node2 = u2nodesMaps[1].begin();
1986 uR_Node2 = u2nodesMaps[1].rbegin();
1987 end1 = u2nodesMaps[0].end();
1988 for ( ; u_Node1 != end1; ++u_Node1 ) {
1989 const SMDS_MeshNode* n1 = u_Node1->second;
1990 const SMDS_MeshNode* n2 = ( reverse ? (uR_Node2++)->second : (u_Node2++)->second );
1991 node1To2Map.insert( make_pair( n1, n2 ));
1994 // associate matching nodes on the last vertices
1995 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
1996 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
1997 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
1998 V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
1999 vNode1 = SMESH_Algo::VertexNode( V1, meshDS1 );
2000 vNode2 = SMESH_Algo::VertexNode( V2, meshDS2 );
2001 if ( !vNode1 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
2002 if ( !vNode2 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
2003 node1To2Map.insert( make_pair( vNode1, vNode2 ));
2006 // don't know why this condition is usually true :(
2007 // if ( node1To2Map.size() * quadFactor < SM1->NbNodes() )
2008 // MESSAGE("FindMatchingNodes() found too few node pairs starting from nodes ("
2009 // << vNode1->GetID() << " - " << eNode1[0]->GetID() << ") ("
2010 // << vNode2->GetID() << " - " << eNode2[0]->GetID() << "):"
2011 // << node1To2Map.size() * quadFactor << " < " << SM1->NbNodes());
2016 //================================================================================
2018 * Return any sub-shape of a face belonging to the outer wire
2019 * \param face - the face
2020 * \param type - type of sub-shape to return
2021 * \retval TopoDS_Shape - the found sub-shape
2023 //================================================================================
2025 TopoDS_Shape StdMeshers_ProjectionUtils::OuterShape( const TopoDS_Face& face,
2026 TopAbs_ShapeEnum type)
2028 TopExp_Explorer exp( BRepTools::OuterWire( face ), type );
2030 return exp.Current();
2031 return TopoDS_Shape();
2034 //================================================================================
2036 * Check that sub-mesh is computed and try to compute it if is not
2037 * \param sm - sub-mesh to compute
2038 * \param iterationNb - int used to stop infinite recursive call
2039 * \retval bool - true if computed
2041 //================================================================================
2043 bool StdMeshers_ProjectionUtils::MakeComputed(SMESH_subMesh * sm, const int iterationNb)
2045 if ( iterationNb > 10 )
2046 RETURN_BAD_RESULT("Infinite recursive projection");
2048 RETURN_BAD_RESULT("NULL submesh");
2049 if ( sm->IsMeshComputed() )
2052 SMESH_Mesh* mesh = sm->GetFather();
2053 SMESH_Gen* gen = mesh->GetGen();
2054 SMESH_Algo* algo = sm->GetAlgo();
2055 TopoDS_Shape shape = sm->GetSubShape();
2058 if ( shape.ShapeType() != TopAbs_COMPOUND )
2060 // No algo assigned to a non-compound sub-mesh.
2061 // Try to find an all-dimensional algo of an upper dimension
2062 int dim = gen->GetShapeDim( shape );
2063 for ( ++dim; ( dim <= 3 && !algo ); ++dim )
2065 SMESH_HypoFilter hypoFilter( SMESH_HypoFilter::IsAlgo() );
2066 hypoFilter.And( SMESH_HypoFilter::HasDim( dim ));
2067 list <const SMESHDS_Hypothesis * > hyps;
2068 list< TopoDS_Shape > assignedTo;
2070 mesh->GetHypotheses( shape, hypoFilter, hyps, true, &assignedTo );
2071 if ( nbAlgos > 1 ) // concurrent algos
2073 list<SMESH_subMesh*> smList; // where an algo is assigned
2074 list< TopoDS_Shape >::iterator shapeIt = assignedTo.begin();
2075 for ( ; shapeIt != assignedTo.end(); ++shapeIt )
2076 smList.push_back( mesh->GetSubMesh( *shapeIt ));
2078 mesh->SortByMeshOrder( smList );
2079 algo = smList.front()->GetAlgo();
2080 shape = smList.front()->GetSubShape();
2082 else if ( nbAlgos == 1 )
2084 algo = (SMESH_Algo*) hyps.front();
2085 shape = assignedTo.front();
2094 bool computed = true;
2095 for ( TopoDS_Iterator grMember( shape ); grMember.More(); grMember.Next())
2096 if ( SMESH_subMesh* grSub = mesh->GetSubMesh( grMember.Value() ))
2097 if ( !MakeComputed( grSub, iterationNb + 1 ))
2103 string algoType = algo->GetName();
2104 if ( algoType.substr(0, 11) != "Projection_")
2105 return gen->Compute( *mesh, shape, /*shapeOnly=*/true );
2107 // try to compute source mesh
2109 const list <const SMESHDS_Hypothesis *> & hyps =
2110 algo->GetUsedHypothesis( *mesh, shape );
2112 TopoDS_Shape srcShape;
2113 SMESH_Mesh* srcMesh = 0;
2114 list <const SMESHDS_Hypothesis*>::const_iterator hIt = hyps.begin();
2115 for ( ; srcShape.IsNull() && hIt != hyps.end(); ++hIt ) {
2116 string hypName = (*hIt)->GetName();
2117 if ( hypName == "ProjectionSource1D" ) {
2118 const StdMeshers_ProjectionSource1D * hyp =
2119 static_cast<const StdMeshers_ProjectionSource1D*>( *hIt );
2120 srcShape = hyp->GetSourceEdge();
2121 srcMesh = hyp->GetSourceMesh();
2123 else if ( hypName == "ProjectionSource2D" ) {
2124 const StdMeshers_ProjectionSource2D * hyp =
2125 static_cast<const StdMeshers_ProjectionSource2D*>( *hIt );
2126 srcShape = hyp->GetSourceFace();
2127 srcMesh = hyp->GetSourceMesh();
2129 else if ( hypName == "ProjectionSource3D" ) {
2130 const StdMeshers_ProjectionSource3D * hyp =
2131 static_cast<const StdMeshers_ProjectionSource3D*>( *hIt );
2132 srcShape = hyp->GetSource3DShape();
2133 srcMesh = hyp->GetSourceMesh();
2136 if ( srcShape.IsNull() ) // no projection source defined
2137 return gen->Compute( *mesh, shape, /*shapeOnly=*/true );
2139 if ( srcShape.IsSame( shape ))
2140 RETURN_BAD_RESULT("Projection from self");
2145 if ( MakeComputed( srcMesh->GetSubMesh( srcShape ), iterationNb + 1 ) &&
2146 gen->Compute( *mesh, shape, /*shapeOnly=*/true ))
2147 return sm->IsMeshComputed();
2153 //================================================================================
2155 * Returns an error message to show in case if MakeComputed( sm ) fails.
2157 //================================================================================
2159 std::string StdMeshers_ProjectionUtils::SourceNotComputedError( SMESH_subMesh * sm,
2160 SMESH_Algo* projAlgo )
2162 const char usualMessage [] = "Source mesh not computed";
2164 return usualMessage;
2165 if ( !sm || sm->GetAlgoState() != SMESH_subMesh::NO_ALGO )
2166 return usualMessage; // algo is OK, anything else is KO.
2168 // Try to find a type of all-dimentional algorithm that would compute the
2169 // given sub-mesh if it could be launched before projection
2170 const TopoDS_Shape shape = sm->GetSubShape();
2171 const int shapeDim = SMESH_Gen::GetShapeDim( shape );
2173 for ( int dimIncrement = 1; shapeDim + dimIncrement < 4; ++dimIncrement )
2175 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
2176 filter.And( filter.HasDim( shapeDim + dimIncrement ));
2178 SMESH_Algo* algo = (SMESH_Algo*) sm->GetFather()->GetHypothesis( shape, filter, true );
2179 if ( algo && !algo->NeedDiscreteBoundary() )
2180 return SMESH_Comment("\"")
2181 << algo->GetFeatures()._label << "\""
2182 << " can't be used to compute the source mesh for \""
2183 << projAlgo->GetFeatures()._label << "\" in this case";
2185 return usualMessage;
2188 //================================================================================
2190 * Return a boundary EDGE (or all boundary EDGEs) of edgeContainer
2192 //================================================================================
2194 TopoDS_Edge StdMeshers_ProjectionUtils::GetBoundaryEdge(const TopoDS_Shape& edgeContainer,
2195 const SMESH_Mesh& mesh,
2196 std::list< TopoDS_Edge >* allBndEdges)
2198 TopTools_IndexedMapOfShape facesOfEdgeContainer, facesNearEdge;
2199 TopExp::MapShapes( edgeContainer, TopAbs_FACE, facesOfEdgeContainer );
2201 if ( !facesOfEdgeContainer.IsEmpty() )
2202 for ( TopExp_Explorer exp(edgeContainer, TopAbs_EDGE); exp.More(); exp.Next() )
2204 const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
2205 facesNearEdge.Clear();
2206 PShapeIteratorPtr faceIt = SMESH_MesherHelper::GetAncestors( edge, mesh, TopAbs_FACE );
2207 while ( const TopoDS_Shape* face = faceIt->next() )
2208 if ( facesOfEdgeContainer.Contains( *face ))
2209 if ( facesNearEdge.Add( *face ) && facesNearEdge.Extent() > 1 )
2211 if ( facesNearEdge.Extent() == 1 ) {
2213 allBndEdges->push_back( edge );
2219 return TopoDS_Edge();
2223 namespace { // Definition of event listeners
2225 SMESH_subMeshEventListener* GetSrcSubMeshListener();
2227 //================================================================================
2229 * \brief Listener that resets an event listener on source submesh when
2230 * "ProjectionSource*D" hypothesis is modified
2232 //================================================================================
2234 struct HypModifWaiter: SMESH_subMeshEventListener
2236 HypModifWaiter():SMESH_subMeshEventListener(false,// won't be deleted by submesh
2237 "StdMeshers_ProjectionUtils::HypModifWaiter") {}
2238 void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
2239 EventListenerData*, const SMESH_Hypothesis*)
2241 if ( event == SMESH_subMesh::MODIF_HYP &&
2242 eventType == SMESH_subMesh::ALGO_EVENT)
2244 // delete current source listener
2245 subMesh->DeleteEventListener( GetSrcSubMeshListener() );
2246 // let algo set a new one
2247 if ( SMESH_Algo* algo = subMesh->GetAlgo() )
2248 algo->SetEventListener( subMesh );
2252 //================================================================================
2254 * \brief return static HypModifWaiter
2256 //================================================================================
2258 SMESH_subMeshEventListener* GetHypModifWaiter() {
2259 static HypModifWaiter aHypModifWaiter;
2260 return &aHypModifWaiter;
2262 //================================================================================
2264 * \brief return static listener for source shape submeshes
2266 //================================================================================
2268 SMESH_subMeshEventListener* GetSrcSubMeshListener() {
2269 static SMESH_subMeshEventListener srcListener(false, // won't be deleted by submesh
2270 "StdMeshers_ProjectionUtils::SrcSubMeshListener");
2271 return &srcListener;
2275 //================================================================================
2277 * Set event listeners to submesh with projection algo
2278 * \param subMesh - submesh with projection algo
2279 * \param srcShape - source shape
2280 * \param srcMesh - source mesh
2282 //================================================================================
2284 void StdMeshers_ProjectionUtils::SetEventListener(SMESH_subMesh* subMesh,
2285 TopoDS_Shape srcShape,
2286 SMESH_Mesh* srcMesh)
2288 // Set the listener that resets an event listener on source submesh when
2289 // "ProjectionSource*D" hypothesis is modified since source shape can be changed
2290 subMesh->SetEventListener( GetHypModifWaiter(),0,subMesh);
2292 // Set an event listener to submesh of the source shape
2293 if ( !srcShape.IsNull() )
2296 srcMesh = subMesh->GetFather();
2298 SMESH_subMesh* srcShapeSM = srcMesh->GetSubMesh( srcShape );
2300 if ( srcShapeSM != subMesh ) {
2301 if ( srcShapeSM->GetSubMeshDS() &&
2302 srcShapeSM->GetSubMeshDS()->IsComplexSubmesh() )
2303 { // source shape is a group
2304 TopExp_Explorer it(srcShapeSM->GetSubShape(), // explore the group into sub-shapes...
2305 subMesh->GetSubShape().ShapeType()); // ...of target shape type
2306 for (; it.More(); it.Next())
2308 SMESH_subMesh* srcSM = srcMesh->GetSubMesh( it.Current() );
2309 if ( srcSM != subMesh )
2311 SMESH_subMeshEventListenerData* data =
2312 srcSM->GetEventListenerData(GetSrcSubMeshListener());
2314 data->mySubMeshes.push_back( subMesh );
2316 data = SMESH_subMeshEventListenerData::MakeData( subMesh );
2317 subMesh->SetEventListener ( GetSrcSubMeshListener(), data, srcSM );
2323 subMesh->SetEventListener( GetSrcSubMeshListener(),
2324 SMESH_subMeshEventListenerData::MakeData( subMesh ),