1 // Copyright (C) 2007-2014 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
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>
69 #include <math_Gauss.hxx>
77 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
78 #define CONT_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); continue; }
79 #define SHOW_SHAPE(v,msg) \
81 // if ( (v).IsNull() ) cout << msg << " NULL SHAPE" << endl; \
82 // else if ((v).ShapeType() == TopAbs_VERTEX) {\
83 // gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( (v) ));\
84 // cout<<msg<<" "<<shapeIndex((v))<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;} \
86 // cout << msg << " "; TopAbs::Print((v).ShapeType(),cout) <<" "<<shapeIndex((v))<<endl;}\
88 #define SHOW_LIST(msg,l) \
90 // cout << msg << " ";\
91 // list< TopoDS_Edge >::const_iterator e = l.begin();\
92 // for ( int i = 0; e != l.end(); ++e, ++i ) {\
93 // cout << i << "V (" << TopExp::FirstVertex( *e, true ).TShape().operator->() << ") "\
94 // << i << "E (" << e->TShape().operator->() << "); "; }\
98 namespace HERE = StdMeshers_ProjectionUtils;
102 static SMESHDS_Mesh* theMeshDS[2] = { 0, 0 }; // used for debug only
103 long shapeIndex(const TopoDS_Shape& S)
105 if ( theMeshDS[0] && theMeshDS[1] )
106 return max(theMeshDS[0]->ShapeToIndex(S), theMeshDS[1]->ShapeToIndex(S) );
107 return long(S.TShape().operator->());
110 //================================================================================
112 * \brief Write shape for debug purposes
114 //================================================================================
116 bool storeShapeForDebug(const TopoDS_Shape& shape)
119 const char* type[] ={"COMPOUND","COMPSOLID","SOLID","SHELL","FACE","WIRE","EDGE","VERTEX"};
120 BRepTools::Write( shape, SMESH_Comment("/tmp/") << type[shape.ShapeType()] << "_"
121 << shape.TShape().operator->() << ".brep");
126 //================================================================================
128 * \brief Reverse order of edges in a list and their orientation
129 * \param edges - list of edges to reverse
130 * \param nbEdges - number of edges to reverse
132 //================================================================================
134 void reverseEdges( list< TopoDS_Edge > & edges, const int nbEdges, const int firstEdge=0)
136 SHOW_LIST("BEFORE REVERSE", edges);
138 list< TopoDS_Edge >::iterator eIt = edges.begin();
139 std::advance( eIt, firstEdge );
140 list< TopoDS_Edge >::iterator eBackIt = eIt;
141 for ( int i = 0; i < nbEdges; ++i, ++eBackIt )
142 eBackIt->Reverse(); // reverse edge
145 while ( eIt != eBackIt )
147 std::swap( *eIt, *eBackIt );
148 SHOW_LIST("# AFTER SWAP", edges)
149 if ( (++eIt) != eBackIt )
152 SHOW_LIST("ATFER REVERSE", edges)
155 //================================================================================
157 * \brief Check if propagation is possible
158 * \param theMesh1 - source mesh
159 * \param theMesh2 - target mesh
160 * \retval bool - true if possible
162 //================================================================================
164 bool isPropagationPossible( SMESH_Mesh* theMesh1, SMESH_Mesh* theMesh2 )
166 if ( theMesh1 != theMesh2 ) {
167 TopoDS_Shape mainShape1 = theMesh1->GetMeshDS()->ShapeToMesh();
168 TopoDS_Shape mainShape2 = theMesh2->GetMeshDS()->ShapeToMesh();
169 return mainShape1.IsSame( mainShape2 );
174 //================================================================================
176 * \brief Fix up association of edges in faces by possible propagation
177 * \param nbEdges - nb of edges in an outer wire
178 * \param edges1 - edges of one face
179 * \param edges2 - matching edges of another face
180 * \param theMesh1 - mesh 1
181 * \param theMesh2 - mesh 2
182 * \retval bool - true if association was fixed
184 //================================================================================
186 bool fixAssocByPropagation( const int nbEdges,
187 list< TopoDS_Edge > & edges1,
188 list< TopoDS_Edge > & edges2,
189 SMESH_Mesh* theMesh1,
190 SMESH_Mesh* theMesh2)
192 if ( nbEdges == 2 && isPropagationPossible( theMesh1, theMesh2 ) )
194 list< TopoDS_Edge >::iterator eIt2 = ++edges2.begin(); // 2nd edge of the 2nd face
195 TopoDS_Edge edge2 = HERE::GetPropagationEdge( theMesh1, *eIt2, edges1.front() ).second;
196 if ( !edge2.IsNull() ) { // propagation found for the second edge
197 reverseEdges( edges2, nbEdges );
204 //================================================================================
206 * \brief Associate faces having one edge in the outer wire.
207 * No check is done if there is really only one outer edge
209 //================================================================================
211 bool assocFewEdgesFaces( const TopoDS_Face& face1,
213 const TopoDS_Face& face2,
215 HERE::TShapeShapeMap & theMap)
217 TopoDS_Vertex v1 = TopoDS::Vertex( HERE::OuterShape( face1, TopAbs_VERTEX ));
218 TopoDS_Vertex v2 = TopoDS::Vertex( HERE::OuterShape( face2, TopAbs_VERTEX ));
219 TopoDS_Vertex VV1[2] = { v1, v1 };
220 TopoDS_Vertex VV2[2] = { v2, v2 };
221 list< TopoDS_Edge > edges1, edges2;
222 if ( int nbE = HERE::FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 ))
224 HERE::InsertAssociation( face1, face2, theMap );
225 fixAssocByPropagation( nbE, edges1, edges2, mesh1, mesh2 );
226 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
227 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
228 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
230 HERE::InsertAssociation( *eIt1, *eIt2, theMap );
231 v1 = SMESH_MesherHelper::IthVertex( 0, *eIt1 );
232 v2 = SMESH_MesherHelper::IthVertex( 0, *eIt2 );
233 HERE::InsertAssociation( v1, v2, theMap );
240 //================================================================================
242 * \brief Look for a group containing a target shape and similar to a source group
243 * \param tgtShape - target edge or face
244 * \param tgtMesh1 - target mesh
245 * \param srcGroup - source group
246 * \retval TopoDS_Shape - found target group
248 //================================================================================
250 TopoDS_Shape findGroupContaining(const TopoDS_Shape& tgtShape,
251 const SMESH_Mesh* tgtMesh1,
252 const TopoDS_Shape& srcGroup)
254 list<SMESH_subMesh*> subMeshes = tgtMesh1->GetGroupSubMeshesContaining(tgtShape);
255 list<SMESH_subMesh*>::iterator sm = subMeshes.begin();
256 int type, last = TopAbs_SHAPE;
257 for ( ; sm != subMeshes.end(); ++sm ) {
258 const TopoDS_Shape & group = (*sm)->GetSubShape();
259 // check if group is similar to srcGroup
260 for ( type = srcGroup.ShapeType(); type < last; ++type)
261 if ( SMESH_MesherHelper::Count( srcGroup, (TopAbs_ShapeEnum)type, 0) !=
262 SMESH_MesherHelper::Count( group, (TopAbs_ShapeEnum)type, 0))
267 return TopoDS_Shape();
270 //================================================================================
272 * \brief Find association of groups at top and bottom of prism
274 //================================================================================
276 bool assocGroupsByPropagation(const TopoDS_Shape& theGroup1,
277 const TopoDS_Shape& theGroup2,
279 HERE::TShapeShapeMap& theMap)
281 // If groups are on top and bottom of prism then we can associate
282 // them using "vertical" (or "side") edges and faces of prism since
283 // they connect corresponding vertices and edges of groups.
285 TopTools_IndexedMapOfShape subshapes1, subshapes2;
286 TopExp::MapShapes( theGroup1, subshapes1 );
287 TopExp::MapShapes( theGroup2, subshapes2 );
288 TopTools_ListIteratorOfListOfShape ancestIt;
290 // Iterate on vertices of group1 to find corresponding vertices in group2
291 // and associate adjacent edges and faces
293 TopTools_MapOfShape verticShapes;
294 TopExp_Explorer vExp1( theGroup1, TopAbs_VERTEX );
295 for ( ; vExp1.More(); vExp1.Next() )
297 const TopoDS_Vertex& v1 = TopoDS::Vertex( vExp1.Current() );
298 if ( theMap.IsBound( v1 )) continue; // already processed
300 // Find "vertical" edge ending in v1 and whose other vertex belongs to group2
301 TopoDS_Shape verticEdge, v2;
302 ancestIt.Initialize( theMesh.GetAncestors( v1 ));
303 for ( ; verticEdge.IsNull() && ancestIt.More(); ancestIt.Next() )
305 if ( ancestIt.Value().ShapeType() != TopAbs_EDGE ) continue;
306 v2 = HERE::GetNextVertex( TopoDS::Edge( ancestIt.Value() ), v1 );
307 if ( subshapes2.Contains( v2 ))
308 verticEdge = ancestIt.Value();
310 if ( verticEdge.IsNull() )
313 HERE::InsertAssociation( v1, v2, theMap);
315 // Associate edges by vertical faces sharing the found vertical edge
316 ancestIt.Initialize( theMesh.GetAncestors( verticEdge ) );
317 for ( ; ancestIt.More(); ancestIt.Next() )
319 if ( ancestIt.Value().ShapeType() != TopAbs_FACE ) continue;
320 if ( !verticShapes.Add( ancestIt.Value() )) continue;
321 const TopoDS_Face& face = TopoDS::Face( ancestIt.Value() );
323 // get edges of the face
324 TopoDS_Edge edgeGr1, edgeGr2, verticEdge2;
325 list< TopoDS_Edge > edges; list< int > nbEdgesInWire;
326 SMESH_Block::GetOrderedEdges( face, edges, nbEdgesInWire, v1);
327 if ( nbEdgesInWire.front() != 4 )
328 return storeShapeForDebug( face );
329 list< TopoDS_Edge >::iterator edge = edges.begin();
330 if ( verticEdge.IsSame( *edge )) {
332 verticEdge2 = *(++edge);
336 verticEdge2 = *(edge++);
340 HERE::InsertAssociation( edgeGr1, edgeGr2.Reversed(), theMap);
345 TopoDS_Iterator gr1It( theGroup1 );
346 if ( gr1It.Value().ShapeType() == TopAbs_FACE )
348 // find a boundary edge of group1 to start from
349 TopoDS_Shape bndEdge = HERE::GetBoundaryEdge( theGroup1, theMesh );
350 if ( bndEdge.IsNull() )
353 list< TopoDS_Shape > edges(1, bndEdge);
354 list< TopoDS_Shape >::iterator edge1 = edges.begin();
355 for ( ; edge1 != edges.end(); ++edge1 )
357 // there must be one or zero not associated faces between ancestors of edge
358 // belonging to theGroup1
360 ancestIt.Initialize( theMesh.GetAncestors( *edge1 ) );
361 for ( ; ancestIt.More() && face1.IsNull(); ancestIt.Next() ) {
362 if ( ancestIt.Value().ShapeType() == TopAbs_FACE &&
363 !theMap.IsBound( ancestIt.Value() ) &&
364 subshapes1.Contains( ancestIt.Value() ))
365 face1 = ancestIt.Value();
367 // add edges of face1 to start searching for adjacent faces from
368 for ( TopExp_Explorer e(face1, TopAbs_EDGE); e.More(); e.Next())
369 if ( !edge1->IsSame( e.Current() ))
370 edges.push_back( e.Current() );
372 if ( !face1.IsNull() ) {
373 // find the corresponding face of theGroup2
374 TopoDS_Shape edge2 = theMap( *edge1 );
376 ancestIt.Initialize( theMesh.GetAncestors( edge2 ) );
377 for ( ; ancestIt.More() && face2.IsNull(); ancestIt.Next() ) {
378 if ( ancestIt.Value().ShapeType() == TopAbs_FACE &&
379 !theMap.IsBound( ancestIt.Value(), /*is2nd=*/true ) &&
380 subshapes2.Contains( ancestIt.Value() ))
381 face2 = ancestIt.Value();
383 if ( face2.IsNull() )
386 HERE::InsertAssociation( face1, face2, theMap);
393 //================================================================================
395 * \brief Return true if uv position of the vIndex-th vertex of edge on face is close
398 //================================================================================
400 bool sameVertexUV( const TopoDS_Edge& edge,
401 const TopoDS_Face& face,
404 const double& tol2d )
407 TopExp::Vertices( edge, VV[0], VV[1], true);
408 gp_Pnt2d v1UV = BRep_Tool::Parameters( VV[vIndex], face);
409 double dist2d = v1UV.Distance( uv );
410 return dist2d < tol2d;
413 //================================================================================
415 * \brief Returns an EDGE suitable for search of initial vertex association
417 //================================================================================
419 bool getOuterEdges( const TopoDS_Shape shape,
421 std::list< TopoDS_Edge >& allBndEdges )
423 if ( shape.ShapeType() == TopAbs_COMPOUND )
425 TopoDS_Iterator it( shape );
426 if ( it.More() && it.Value().ShapeType() == TopAbs_FACE ) // group of FACEs
428 // look for a boundary EDGE of a group
429 StdMeshers_ProjectionUtils::GetBoundaryEdge( shape, mesh, &allBndEdges );
430 if ( !allBndEdges.empty() )
434 TopExp_Explorer expF( shape, TopAbs_FACE ), expE;
436 for ( ; expF.More(); expF.Next() ) {
438 StdMeshers_ProjectionUtils::OuterShape( TopoDS::Face( expF.Current() ), TopAbs_WIRE );
439 for ( expE.Init( wire, TopAbs_EDGE ); expE.More(); expE.Next() )
440 if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( expE.Current() )))
441 allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
444 else if ( shape.ShapeType() != TopAbs_EDGE) { // no faces
445 for ( expE.Init( shape, TopAbs_EDGE ); expE.More(); expE.Next() )
446 if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( expE.Current() )))
447 allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
449 else if ( shape.ShapeType() == TopAbs_EDGE ) {
450 if ( !SMESH_MesherHelper::IsClosedEdge( TopoDS::Edge( shape )))
451 allBndEdges.push_back( TopoDS::Edge( shape ));
453 return !allBndEdges.empty();
458 //=======================================================================
460 * Looks for association of all sub-shapes of two shapes
461 * \param theShape1 - target shape
462 * \param theMesh1 - mesh built on shape 1
463 * \param theShape2 - source shape
464 * \param theMesh2 - mesh built on shape 2
465 * \param theAssociation - association map to be filled that may
466 * contain association of one or two pairs of vertices
467 * \retval bool - true if association found
469 //=======================================================================
471 bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& theShape1,
472 SMESH_Mesh* theMesh1,
473 const TopoDS_Shape& theShape2,
474 SMESH_Mesh* theMesh2,
475 TShapeShapeMap & theMap)
477 // Structure of this long function is following
478 // 1) Group -> Group projection: theShape1 is a group member,
479 // theShape2 is another group. We find a group theShape1 is in and recall self.
480 // 2) Accosiate same shapes with different location (partners).
481 // 3) If vertex association is given, perform accosiation according to shape type:
482 // switch ( ShapeType ) {
486 // 4) else try to accosiate in different ways:
487 // a) accosiate shapes by propagation and other simple cases
488 // switch ( ShapeType ) {
492 // b) find association of a couple of vertices and recall self.
495 theMeshDS[0] = theMesh1->GetMeshDS(); // debug
496 theMeshDS[1] = theMesh2->GetMeshDS();
498 // =================================================================================
499 // 1) Is it the case of associating a group member -> another group? (PAL16202, 16203)
500 // =================================================================================
501 if ( theShape1.ShapeType() != theShape2.ShapeType() ) {
502 TopoDS_Shape group1, group2;
503 if ( theShape1.ShapeType() == TopAbs_COMPOUND ) {
505 group2 = findGroupContaining( theShape2, theMesh2, group1 );
507 else if ( theShape2.ShapeType() == TopAbs_COMPOUND ) {
509 group1 = findGroupContaining( theShape1, theMesh1, group2 );
511 if ( group1.IsNull() || group2.IsNull() )
512 RETURN_BAD_RESULT("Different shape types");
513 // Associate compounds
514 return FindSubShapeAssociation(group1, theMesh1, group2, theMesh2, theMap );
520 bool partner = theShape1.IsPartner( theShape2 );
521 TopTools_DataMapIteratorOfDataMapOfShapeShape vvIt( theMap._map1to2 );
522 for ( ; partner && vvIt.More(); vvIt.Next() )
523 partner = vvIt.Key().IsPartner( vvIt.Value() );
525 if ( partner ) // Same shape with different location
527 // recursively associate all sub-shapes of theShape1 and theShape2
528 typedef list< pair< TopoDS_Shape, TopoDS_Shape > > TShapePairsList;
529 TShapePairsList shapesQueue( 1, make_pair( theShape1, theShape2 ));
530 TShapePairsList::iterator s1_s2 = shapesQueue.begin();
531 for ( ; s1_s2 != shapesQueue.end(); ++s1_s2 )
533 if ( theMap.IsBound( s1_s2->first )) // avoid re-binding for a seam edge
534 continue; // to avoid this: Forward seam -> Reversed seam
535 InsertAssociation( s1_s2->first, s1_s2->second, theMap );
536 TopoDS_Iterator s1It( s1_s2->first), s2It( s1_s2->second );
537 for ( ; s1It.More(); s1It.Next(), s2It.Next() )
538 shapesQueue.push_back( make_pair( s1It.Value(), s2It.Value() ));
543 if ( !theMap.IsEmpty() )
545 //======================================================================
546 // 3) HAS initial vertex association
547 //======================================================================
548 switch ( theShape1.ShapeType() ) {
549 // ----------------------------------------------------------------------
550 case TopAbs_EDGE: { // TopAbs_EDGE
551 // ----------------------------------------------------------------------
552 if ( theMap.Extent() != 2 )
553 RETURN_BAD_RESULT("Wrong map extent " << theMap.Extent() );
554 TopoDS_Edge edge1 = TopoDS::Edge( theShape1 );
555 TopoDS_Edge edge2 = TopoDS::Edge( theShape2 );
556 if ( edge1.Orientation() >= TopAbs_INTERNAL ) edge1.Orientation( TopAbs_FORWARD );
557 if ( edge2.Orientation() >= TopAbs_INTERNAL ) edge2.Orientation( TopAbs_FORWARD );
558 TopoDS_Vertex VV1[2], VV2[2];
559 TopExp::Vertices( edge1, VV1[0], VV1[1] );
560 TopExp::Vertices( edge2, VV2[0], VV2[1] );
562 if ( theMap.IsBound( VV1[ i1 ] )) i1 = 1;
563 if ( theMap.IsBound( VV2[ i2 ] )) i2 = 1;
564 InsertAssociation( VV1[ i1 ], VV2[ i2 ], theMap );
565 InsertAssociation( theShape1, theShape2, theMap );
568 // ----------------------------------------------------------------------
569 case TopAbs_FACE: { // TopAbs_FACE
570 // ----------------------------------------------------------------------
571 TopoDS_Face face1 = TopoDS::Face( theShape1 );
572 TopoDS_Face face2 = TopoDS::Face( theShape2 );
573 if ( face1.Orientation() >= TopAbs_INTERNAL ) face1.Orientation( TopAbs_FORWARD );
574 if ( face2.Orientation() >= TopAbs_INTERNAL ) face2.Orientation( TopAbs_FORWARD );
576 TopoDS_Vertex VV1[2], VV2[2];
577 // find a not closed edge of face1 both vertices of which are associated
579 TopExp_Explorer exp ( face1, TopAbs_EDGE );
580 for ( ; VV2[ 1 ].IsNull() && exp.More(); exp.Next(), ++nbEdges ) {
581 TopExp::Vertices( TopoDS::Edge( exp.Current() ), VV1[0], VV1[1] );
582 if ( theMap.IsBound( VV1[0] ) ) {
583 VV2[ 0 ] = TopoDS::Vertex( theMap( VV1[0] ));
584 if ( theMap.IsBound( VV1[1] ) && !VV1[0].IsSame( VV1[1] ))
585 VV2[ 1 ] = TopoDS::Vertex( theMap( VV1[1] ));
588 if ( VV2[ 1 ].IsNull() ) { // 2 bound vertices not found
590 RETURN_BAD_RESULT("2 bound vertices not found" );
595 list< TopoDS_Edge > edges1, edges2;
596 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
597 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
598 fixAssocByPropagation( nbE, edges1, edges2, theMesh1, theMesh2 );
600 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
601 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
602 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
604 InsertAssociation( *eIt1, *eIt2, theMap );
605 VV1[0] = TopExp::FirstVertex( *eIt1, true );
606 VV2[0] = TopExp::FirstVertex( *eIt2, true );
607 InsertAssociation( VV1[0], VV2[0], theMap );
609 InsertAssociation( theShape1, theShape2, theMap );
612 // ----------------------------------------------------------------------
613 case TopAbs_SHELL: // TopAbs_SHELL, TopAbs_SOLID
615 // ----------------------------------------------------------------------
616 TopoDS_Vertex VV1[2], VV2[2];
617 // try to find a not closed edge of shape1 both vertices of which are associated
619 TopExp_Explorer exp ( theShape1, TopAbs_EDGE );
620 for ( ; VV2[ 1 ].IsNull() && exp.More(); exp.Next() ) {
621 edge1 = TopoDS::Edge( exp.Current() );
622 if ( edge1.Orientation() >= TopAbs_INTERNAL ) edge1.Orientation( TopAbs_FORWARD );
623 TopExp::Vertices( edge1 , VV1[0], VV1[1] );
624 if ( theMap.IsBound( VV1[0] )) {
625 VV2[ 0 ] = TopoDS::Vertex( theMap( VV1[0] ));
626 if ( theMap.IsBound( VV1[1] ) && !VV1[0].IsSame( VV1[1] ))
627 VV2[ 1 ] = TopoDS::Vertex( theMap( VV1[1] ));
630 if ( VV2[ 1 ].IsNull() ) // 2 bound vertices not found
631 RETURN_BAD_RESULT("2 bound vertices not found" );
632 // get an edge2 of theShape2 corresponding to edge1
633 TopoDS_Edge edge2 = GetEdgeByVertices( theMesh2, VV2[ 0 ], VV2[ 1 ]);
634 if ( edge2.IsNull() )
635 RETURN_BAD_RESULT("GetEdgeByVertices() failed");
637 // build map of edge to faces if shapes are not sub-shapes of main ones
638 bool isSubOfMain = false;
639 if ( SMESHDS_SubMesh * sm = theMesh1->GetMeshDS()->MeshElements( theShape1 ))
640 isSubOfMain = !sm->IsComplexSubmesh();
642 isSubOfMain = theMesh1->GetMeshDS()->ShapeToIndex( theShape1 );
643 TAncestorMap e2f1, e2f2;
644 const TAncestorMap& edgeToFace1 = isSubOfMain ? theMesh1->GetAncestorMap() : e2f1;
645 const TAncestorMap& edgeToFace2 = isSubOfMain ? theMesh2->GetAncestorMap() : e2f2;
647 TopExp::MapShapesAndAncestors( theShape1, TopAbs_EDGE, TopAbs_FACE, e2f1 );
648 TopExp::MapShapesAndAncestors( theShape2, TopAbs_EDGE, TopAbs_FACE, e2f2 );
649 if ( !edgeToFace1.Contains( edge1 ))
650 RETURN_BAD_RESULT("edge1 does not belong to theShape1");
651 if ( !edgeToFace2.Contains( edge2 ))
652 RETURN_BAD_RESULT("edge2 does not belong to theShape2");
655 // Look for 2 corresponing faces:
659 // get a face sharing edge1 (F1)
661 TopTools_ListIteratorOfListOfShape ancestIt1( edgeToFace1.FindFromKey( edge1 ));
662 for ( ; F1.IsNull() && ancestIt1.More(); ancestIt1.Next() )
663 if ( ancestIt1.Value().ShapeType() == TopAbs_FACE )
664 F1 = ancestIt1.Value().Oriented //( TopAbs_FORWARD );
665 ( SMESH_MesherHelper::GetSubShapeOri( theShape1, ancestIt1.Value() ));
667 RETURN_BAD_RESULT(" Face1 not found");
669 // get 2 faces sharing edge2 (one of them is F2)
670 TopTools_ListIteratorOfListOfShape ancestIt2( edgeToFace2.FindFromKey( edge2 ));
671 for ( int i = 0; FF2[1].IsNull() && ancestIt2.More(); ancestIt2.Next() )
672 if ( ancestIt2.Value().ShapeType() == TopAbs_FACE )
673 FF2[ i++ ] = ancestIt2.Value().Oriented // ( TopAbs_FORWARD );
674 ( SMESH_MesherHelper::GetSubShapeOri( theShape2, ancestIt2.Value() ));
676 // get oriented edge1 and edge2 from F1 and FF2[0]
677 for ( exp.Init( F1, TopAbs_EDGE ); exp.More(); exp.Next() )
678 if ( edge1.IsSame( exp.Current() )) {
679 edge1 = TopoDS::Edge( exp.Current() );
682 for ( exp.Init( FF2[ 0 ], TopAbs_EDGE ); exp.More(); exp.Next() )
683 if ( edge2.IsSame( exp.Current() )) {
684 edge2 = TopoDS::Edge( exp.Current() );
688 // compare first vertices of edge1 and edge2
689 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
690 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
691 F2 = FF2[ 0 ]; // (F2 !)
692 if ( !VV1[ 0 ].IsSame( theMap( VV2[ 0 ], /*is2=*/true))) {
694 if ( FF2[ 1 ].IsNull() )
700 TopTools_MapOfShape boundEdges;
702 // association of face sub-shapes and neighbour faces
703 list< pair < TopoDS_Face, TopoDS_Edge > > FE1, FE2;
704 list< pair < TopoDS_Face, TopoDS_Edge > >::iterator fe1, fe2;
705 FE1.push_back( make_pair( TopoDS::Face( F1 ), edge1 ));
706 FE2.push_back( make_pair( TopoDS::Face( F2 ), edge2 ));
707 for ( fe1 = FE1.begin(), fe2 = FE2.begin(); fe1 != FE1.end(); ++fe1, ++fe2 )
709 const TopoDS_Face& face1 = fe1->first;
710 if ( theMap.IsBound( face1 ) ) continue;
711 const TopoDS_Face& face2 = fe2->first;
714 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
715 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
716 list< TopoDS_Edge > edges1, edges2;
717 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
718 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
719 InsertAssociation( face1, face2, theMap ); // assoc faces
720 MESSAGE("Assoc FACE " << theMesh1->GetMeshDS()->ShapeToIndex( face1 )<<
721 " to " << theMesh2->GetMeshDS()->ShapeToIndex( face2 ));
722 if ( nbE == 2 && (edge1.IsSame( edges1.front())) != (edge2.IsSame( edges2.front())))
724 reverseEdges( edges2, nbE );
726 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
727 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
728 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
730 if ( !boundEdges.Add( *eIt1 )) continue; // already associated
731 InsertAssociation( *eIt1, *eIt2, theMap ); // assoc edges
732 VV1[0] = TopExp::FirstVertex( *eIt1, true );
733 VV2[0] = TopExp::FirstVertex( *eIt2, true );
734 InsertAssociation( VV1[0], VV2[0], theMap ); // assoc vertices
736 // add adjacent faces to process
737 TopoDS_Face nextFace1 = GetNextFace( edgeToFace1, *eIt1, face1 );
738 TopoDS_Face nextFace2 = GetNextFace( edgeToFace2, *eIt2, face2 );
739 if ( !nextFace1.IsNull() && !nextFace2.IsNull() ) {
740 FE1.push_back( make_pair( nextFace1, *eIt1 ));
741 FE2.push_back( make_pair( nextFace2, *eIt2 ));
745 InsertAssociation( theShape1, theShape2, theMap );
748 // ----------------------------------------------------------------------
749 case TopAbs_COMPOUND: { // GROUP
750 // ----------------------------------------------------------------------
751 // Maybe groups contain only one member
752 TopoDS_Iterator it1( theShape1 ), it2( theShape2 );
753 TopAbs_ShapeEnum memberType = it1.Value().ShapeType();
754 int nbMembers = SMESH_MesherHelper::Count( theShape1, memberType, true );
755 if ( nbMembers == 0 ) return true;
756 if ( nbMembers == 1 ) {
757 return FindSubShapeAssociation( it1.Value(), theMesh1, it2.Value(), theMesh2, theMap );
759 // Try to make shells of faces
761 BRep_Builder builder;
762 TopoDS_Shell shell1, shell2;
763 builder.MakeShell(shell1); builder.MakeShell(shell2);
764 if ( memberType == TopAbs_FACE ) {
765 // just add faces of groups to shells
766 for (; it1.More(); it1.Next(), it2.Next() )
767 builder.Add( shell1, it1.Value() ), builder.Add( shell2, it2.Value() );
769 else if ( memberType == TopAbs_EDGE ) {
770 // Try to add faces sharing more than one edge of a group or
771 // sharing all its vertices with the group
772 TopTools_IndexedMapOfShape groupVertices[2];
773 TopExp::MapShapes( theShape1, TopAbs_VERTEX, groupVertices[0]);
774 TopExp::MapShapes( theShape2, TopAbs_VERTEX, groupVertices[1]);
776 TopTools_MapOfShape groupEdges[2], addedFaces[2];
777 bool hasInitAssoc = (!theMap.IsEmpty()), initAssocOK = !hasInitAssoc;
778 for (; it1.More(); it1.Next(), it2.Next() ) {
779 groupEdges[0].Add( it1.Value() );
780 groupEdges[1].Add( it2.Value() );
781 if ( !initAssocOK ) {
782 // for shell association there must be an edge with both vertices bound
783 TopoDS_Vertex v1, v2;
784 TopExp::Vertices( TopoDS::Edge( it1.Value().Oriented(TopAbs_FORWARD)), v1, v2 );
785 initAssocOK = ( theMap.IsBound( v1 ) && theMap.IsBound( v2 ));
788 for (int is2ndGroup = 0; initAssocOK && is2ndGroup < 2; ++is2ndGroup) {
789 const TopoDS_Shape& group = is2ndGroup ? theShape2: theShape1;
790 SMESH_Mesh* mesh = is2ndGroup ? theMesh2 : theMesh1;
791 TopoDS_Shell& shell = is2ndGroup ? shell2 : shell1;
792 for ( TopoDS_Iterator it( group ); it.More(); it.Next() ) {
793 const TopoDS_Edge& edge = TopoDS::Edge( it.Value() );
795 for ( int iF = 0; iF < 2; ++iF ) { // loop on 2 faces sharing edge
796 face = GetNextFace(mesh->GetAncestorMap(), edge, face);
797 if ( !face.IsNull() ) {
798 int nbGroupEdges = 0;
799 for ( TopExp_Explorer f( face, TopAbs_EDGE ); f.More(); f.Next())
800 if ( groupEdges[ is2ndGroup ].Contains( f.Current() ))
801 if ( ++nbGroupEdges > 1 )
803 bool add = (nbGroupEdges > 1 ||
804 SMESH_MesherHelper::Count( face, TopAbs_EDGE, true ) == 1 );
807 for ( TopExp_Explorer v( face, TopAbs_VERTEX ); add && v.More(); v.Next())
808 add = groupVertices[ is2ndGroup ].Contains( v.Current() );
810 if ( add && addedFaces[ is2ndGroup ].Add( face ))
811 builder.Add( shell, face );
817 RETURN_BAD_RESULT("Unexpected group type");
821 int nbFaces1 = SMESH_MesherHelper:: Count( shell1, TopAbs_FACE, 0 );
822 int nbFaces2 = SMESH_MesherHelper:: Count( shell2, TopAbs_FACE, 0 );
823 if ( nbFaces1 != nbFaces2 )
824 RETURN_BAD_RESULT("Different nb of faces found for shells");
825 if ( nbFaces1 > 0 ) {
827 if ( nbFaces1 == 1 ) {
828 TopoDS_Shape F1 = TopoDS_Iterator( shell1 ).Value();
829 TopoDS_Shape F2 = TopoDS_Iterator( shell2 ).Value();
830 ok = FindSubShapeAssociation( F1, theMesh1, F2, theMesh2, theMap );
833 ok = FindSubShapeAssociation(shell1, theMesh1, shell2, theMesh2, theMap );
835 // Check if all members are mapped
837 TopTools_MapOfShape boundMembers[2];
839 for ( mIt.Initialize( theShape1 ); mIt.More(); mIt.Next())
840 if ( theMap.IsBound( mIt.Value() )) {
841 boundMembers[0].Add( mIt.Value() );
842 boundMembers[1].Add( theMap( mIt.Value() ));
844 if ( boundMembers[0].Extent() != nbMembers ) {
845 // make compounds of not bound members
846 TopoDS_Compound comp[2];
847 for ( int is2ndGroup = 0; is2ndGroup < 2; ++is2ndGroup ) {
848 builder.MakeCompound( comp[is2ndGroup] );
849 for ( mIt.Initialize( is2ndGroup ? theShape2:theShape1 ); mIt.More(); mIt.Next())
850 if ( ! boundMembers[ is2ndGroup ].Contains( mIt.Value() ))
851 builder.Add( comp[ is2ndGroup ], mIt.Value() );
853 // check if theMap contains initial association for the comp's
854 bool hasInitialAssoc = false;
855 if ( memberType == TopAbs_EDGE ) {
856 for ( TopExp_Explorer v( comp[0], TopAbs_VERTEX ); v.More(); v.Next())
857 if ( theMap.IsBound( v.Current() )) {
858 hasInitialAssoc = true;
862 if ( hasInitialAssoc == bool( !theMap.IsEmpty() ))
863 ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, theMap );
865 TShapeShapeMap tmpMap;
866 ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, tmpMap );
868 TopTools_DataMapIteratorOfDataMapOfShapeShape mapIt( tmpMap._map1to2 );
869 for ( ; mapIt.More(); mapIt.Next() )
870 theMap.Bind( mapIt.Key(), mapIt.Value());
877 // Each edge of an edge group is shared by own faces
878 // ------------------------------------------------------------------
880 // map vertices to edges sharing them, avoid doubling edges in lists
881 TopTools_DataMapOfShapeListOfShape v2e[2];
882 for (int isFirst = 0; isFirst < 2; ++isFirst ) {
883 const TopoDS_Shape& group = isFirst ? theShape1 : theShape2;
884 TopTools_DataMapOfShapeListOfShape& veMap = v2e[ isFirst ? 0 : 1 ];
885 TopTools_MapOfShape addedEdges;
886 for ( TopExp_Explorer e( group, TopAbs_EDGE ); e.More(); e.Next() ) {
887 const TopoDS_Shape& edge = e.Current();
888 if ( addedEdges.Add( edge )) {
889 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next()) {
890 const TopoDS_Shape& vertex = v.Current();
891 if ( !veMap.IsBound( vertex )) {
892 TopTools_ListOfShape l;
893 veMap.Bind( vertex, l );
895 veMap( vertex ).Append( edge );
900 while ( !v2e[0].IsEmpty() )
902 // find a bound vertex
904 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape v2eIt( v2e[0] );
905 for ( ; v2eIt.More(); v2eIt.Next())
906 if ( theMap.IsBound( v2eIt.Key() )) {
907 V[0] = TopoDS::Vertex( v2eIt.Key() );
908 V[1] = TopoDS::Vertex( theMap( V[0] ));
912 RETURN_BAD_RESULT("No more bound vertices");
914 while ( !V[0].IsNull() && v2e[0].IsBound( V[0] )) {
915 TopTools_ListOfShape& edges0 = v2e[0]( V[0] );
916 TopTools_ListOfShape& edges1 = v2e[1]( V[1] );
917 int nbE0 = edges0.Extent(), nbE1 = edges1.Extent();
919 RETURN_BAD_RESULT("Different nb of edges: "<< nbE0 << " != " << nbE1);
923 TopoDS_Edge e0 = TopoDS::Edge( edges0.First() );
924 TopoDS_Edge e1 = TopoDS::Edge( edges1.First() );
925 v2e[0].UnBind( V[0] );
926 v2e[1].UnBind( V[1] );
927 InsertAssociation( e0, e1, theMap );
928 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0 )<<
929 " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1 ));
930 V[0] = GetNextVertex( e0, V[0] );
931 V[1] = GetNextVertex( e1, V[1] );
932 if ( !V[0].IsNull() ) {
933 InsertAssociation( V[0], V[1], theMap );
934 MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( V[0] )<<
935 " to " << theMesh2->GetMeshDS()->ShapeToIndex( V[1] ));
938 else if ( nbE0 == 2 )
940 // one of edges must have both ends bound
941 TopoDS_Vertex v0e0 = GetNextVertex( TopoDS::Edge( edges0.First() ), V[0] );
942 TopoDS_Vertex v1e0 = GetNextVertex( TopoDS::Edge( edges0.Last() ), V[0] );
943 TopoDS_Vertex v0e1 = GetNextVertex( TopoDS::Edge( edges1.First() ), V[1] );
944 TopoDS_Vertex v1e1 = GetNextVertex( TopoDS::Edge( edges1.Last() ), V[1] );
945 TopoDS_Shape e0b, e1b, e0n, e1n, v1b; // bound and not-bound
946 TopoDS_Vertex v0n, v1n;
947 if ( theMap.IsBound( v0e0 )) {
948 v0n = v1e0; e0b = edges0.First(); e0n = edges0.Last(); v1b = theMap( v0e0 );
949 } else if ( theMap.IsBound( v1e0 )) {
950 v0n = v0e0; e0n = edges0.First(); e0b = edges0.Last(); v1b = theMap( v1e0 );
952 RETURN_BAD_RESULT("None of vertices bound");
954 if ( v1b.IsSame( v1e1 )) {
955 v1n = v0e1; e1n = edges1.First(); e1b = edges1.Last();
957 v1n = v1e1; e1b = edges1.First(); e1n = edges1.Last();
959 InsertAssociation( e0b, e1b, theMap );
960 InsertAssociation( e0n, e1n, theMap );
961 InsertAssociation( v0n, v1n, theMap );
962 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0b )<<
963 " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1b ));
964 MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0n )<<
965 " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1n ));
966 MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( v0n )<<
967 " to " << theMesh2->GetMeshDS()->ShapeToIndex( v1n ));
968 v2e[0].UnBind( V[0] );
969 v2e[1].UnBind( V[1] );
974 RETURN_BAD_RESULT("Not implemented");
977 } //while ( !v2e[0].IsEmpty() )
982 RETURN_BAD_RESULT("Unexpected shape type");
984 } // end switch by shape type
985 } // end case of available initial vertex association
987 //======================================================================
988 // 4) NO INITIAL VERTEX ASSOCIATION
989 //======================================================================
991 switch ( theShape1.ShapeType() ) {
994 // ----------------------------------------------------------------------
995 TopoDS_Edge edge1 = TopoDS::Edge( theShape1 );
996 TopoDS_Edge edge2 = TopoDS::Edge( theShape2 );
997 if ( isPropagationPossible( theMesh1, theMesh2 ))
999 TopoDS_Edge prpEdge = GetPropagationEdge( theMesh1, edge2, edge1 ).second;
1000 if ( !prpEdge.IsNull() )
1002 TopoDS_Vertex VV1[2], VV2[2];
1003 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
1004 TopExp::Vertices( prpEdge, VV2[0], VV2[1], true );
1005 InsertAssociation( VV1[ 0 ], VV2[ 0 ], theMap );
1006 InsertAssociation( VV1[ 1 ], VV2[ 1 ], theMap );
1007 if ( VV1[0].IsSame( VV1[1] ) || // one of edges is closed
1008 VV2[0].IsSame( VV2[1] ) )
1010 InsertAssociation( edge1, prpEdge, theMap ); // insert with a proper orientation
1012 InsertAssociation( theShape1, theShape2, theMap );
1013 return true; // done
1016 if ( SMESH_MesherHelper::IsClosedEdge( edge1 ) &&
1017 SMESH_MesherHelper::IsClosedEdge( edge2 ))
1019 // TODO: find out a proper orientation (is it possible?)
1020 InsertAssociation( edge1, edge2, theMap ); // insert with a proper orientation
1021 InsertAssociation( TopExp::FirstVertex(edge1), TopExp::FirstVertex(edge2),
1023 InsertAssociation( theShape1, theShape2, theMap );
1024 return true; // done
1026 break; // try by vertex closeness
1030 // ----------------------------------------------------------------------
1031 if ( isPropagationPossible( theMesh1, theMesh2 )) // try by propagation in one mesh
1033 TopoDS_Face face1 = TopoDS::Face(theShape1);
1034 TopoDS_Face face2 = TopoDS::Face(theShape2);
1035 if ( face1.Orientation() >= TopAbs_INTERNAL ) face1.Orientation( TopAbs_FORWARD );
1036 if ( face2.Orientation() >= TopAbs_INTERNAL ) face2.Orientation( TopAbs_FORWARD );
1037 TopoDS_Edge edge1, edge2;
1038 // get outer edge of theShape1
1039 TopoDS_Shape wire = OuterShape( face1, TopAbs_WIRE );
1040 //edge1 = TopoDS::Edge( OuterShape( face1, TopAbs_EDGE ));
1041 // use map to find the closest propagation edge
1042 map<int, pair< TopoDS_Edge, TopoDS_Edge > > propag_edges;
1043 for ( TopoDS_Iterator edgeIt( wire ); edgeIt.More(); edgeIt.Next() )
1045 edge1 = TopoDS::Edge( edgeIt.Value() );
1046 // find out if any edge of face2 is a propagation edge of outer edge1
1047 for ( TopExp_Explorer exp( face2, TopAbs_EDGE ); exp.More(); exp.Next() ) {
1048 edge2 = TopoDS::Edge( exp.Current() );
1049 pair<int,TopoDS_Edge> step_edge = GetPropagationEdge( theMesh1, edge2, edge1 );
1050 if ( !step_edge.second.IsNull() ) { // propagation found
1051 propag_edges.insert( make_pair( step_edge.first,
1052 ( make_pair( edge1, step_edge.second ))));
1053 if ( step_edge.first == 1 ) break; // most close found
1056 if ( !propag_edges.empty() && propag_edges.begin()->first == 1 ) break;
1058 if ( !propag_edges.empty() ) // propagation found
1060 edge1 = propag_edges.begin()->second.first;
1061 edge2 = propag_edges.begin()->second.second;
1062 TopoDS_Vertex VV1[2], VV2[2];
1063 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
1064 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
1065 list< TopoDS_Edge > edges1, edges2;
1066 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
1067 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
1068 // take care of proper association of propagated edges
1069 bool same1 = edge1.IsSame( edges1.front() );
1070 bool same2 = edge2.IsSame( edges2.front() );
1071 if ( same1 != same2 )
1073 reverseEdges(edges2, nbE);
1074 if ( nbE != 2 ) // 2 degen edges of 4 (issue 0021144)
1075 edges2.splice( edges2.end(), edges2, edges2.begin());
1077 // store association
1078 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
1079 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
1080 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
1082 InsertAssociation( *eIt1, *eIt2, theMap );
1083 VV1[0] = TopExp::FirstVertex( *eIt1, true );
1084 VV2[0] = TopExp::FirstVertex( *eIt2, true );
1085 InsertAssociation( VV1[0], VV2[0], theMap );
1087 InsertAssociation( theShape1, theShape2, theMap );
1091 break; // try by vertex closeness
1093 case TopAbs_COMPOUND: {
1094 // ----------------------------------------------------------------------
1095 if ( isPropagationPossible( theMesh1, theMesh2 )) {
1097 // try to accosiate all using propagation
1098 if ( assocGroupsByPropagation( theShape1, theShape2, *theMesh1, theMap ))
1101 // find a boundary edge of theShape1
1102 TopoDS_Edge E = GetBoundaryEdge( theShape1, *theMesh1 );
1104 break; // try by vertex closeness
1106 // find association for vertices of edge E
1107 TopoDS_Vertex VV1[2], VV2[2];
1108 for(TopExp_Explorer eexp(E, TopAbs_VERTEX); eexp.More(); eexp.Next()) {
1109 TopoDS_Vertex V1 = TopoDS::Vertex( eexp.Current() );
1110 // look for an edge ending in E whose one vertex is in theShape1
1111 // and the other, in theShape2
1112 const TopTools_ListOfShape& Ancestors = theMesh1->GetAncestors(V1);
1113 TopTools_ListIteratorOfListOfShape ita(Ancestors);
1114 for(; ita.More(); ita.Next()) {
1115 if( ita.Value().ShapeType() != TopAbs_EDGE ) continue;
1116 TopoDS_Edge edge = TopoDS::Edge(ita.Value());
1117 bool FromShape1 = false;
1118 for(TopExp_Explorer expe(theShape1, TopAbs_EDGE); expe.More(); expe.Next() ) {
1119 if(edge.IsSame(expe.Current())) {
1125 // is it an edge between theShape1 and theShape2?
1126 TopExp_Explorer expv(edge, TopAbs_VERTEX);
1127 TopoDS_Vertex V2 = TopoDS::Vertex( expv.Current() );
1130 V2 = TopoDS::Vertex( expv.Current() );
1132 bool FromShape2 = false;
1133 for ( expv.Init( theShape2, TopAbs_VERTEX ); expv.More(); expv.Next()) {
1134 if ( V2.IsSame( expv.Current() )) {
1140 if ( VV1[0].IsNull() )
1141 VV1[0] = V1, VV2[0] = V2;
1143 VV1[1] = V1, VV2[1] = V2;
1144 break; // from loop on ancestors of V1
1149 if ( !VV1[1].IsNull() ) {
1150 InsertAssociation( VV1[0], VV2[0], theMap );
1151 InsertAssociation( VV1[1], VV2[1], theMap );
1152 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap);
1155 break; // try by vertex closeness
1160 // 4.b) Find association by closeness of vertices
1161 // ----------------------------------------------
1163 TopTools_IndexedMapOfShape vMap1, vMap2;
1164 TopExp::MapShapes( theShape1, TopAbs_VERTEX, vMap1 );
1165 TopExp::MapShapes( theShape2, TopAbs_VERTEX, vMap2 );
1166 TopoDS_Vertex VV1[2], VV2[2];
1168 if ( vMap1.Extent() != vMap2.Extent() )
1170 if ( SMESH_MesherHelper:: Count( theShape1, TopAbs_EDGE, /*ignoreSame=*/false ) !=
1171 SMESH_MesherHelper:: Count( theShape2, TopAbs_EDGE, /*ignoreSame=*/false ))
1172 RETURN_BAD_RESULT("Different nb of vertices");
1175 if ( vMap1.Extent() == 1 || vMap2.Extent() == 1 ) {
1176 InsertAssociation( vMap1(1), vMap2(1), theMap );
1177 if ( theShape1.ShapeType() == TopAbs_EDGE ) {
1178 if ( vMap1.Extent() == 2 )
1179 InsertAssociation( vMap1(2), vMap2(1), theMap );
1180 else if ( vMap2.Extent() == 2 )
1181 InsertAssociation( vMap2(2), vMap1(1), theMap );
1182 InsertAssociation( theShape1, theShape2, theMap );
1185 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap);
1188 // Try to associate by common vertices of an edge
1189 for ( int i = 1; i <= vMap1.Extent(); ++i )
1191 const TopoDS_Shape& v1 = vMap1(i);
1192 if ( vMap2.Contains( v1 ))
1194 // find an egde sharing v1 and sharing at the same time another common vertex
1195 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( v1, *theMesh1, TopAbs_EDGE);
1196 bool edgeFound = false;
1197 while ( edgeIt->more() && !edgeFound )
1199 TopoDS_Edge edge = TopoDS::Edge( edgeIt->next()->Oriented(TopAbs_FORWARD));
1200 TopExp::Vertices(edge, VV1[0], VV1[1]);
1201 if ( !VV1[0].IsSame( VV1[1] ))
1202 edgeFound = ( vMap2.Contains( VV1[ v1.IsSame(VV1[0]) ? 1:0]));
1206 InsertAssociation( VV1[0], VV1[0], theMap );
1207 InsertAssociation( VV1[1], VV1[1], theMap );
1208 if (FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap ))
1214 // Find transformation to make the shapes be of similar size at same location
1217 for ( int i = 1; i <= vMap1.Extent(); ++i )
1218 box[ 0 ].Add( BRep_Tool::Pnt ( TopoDS::Vertex( vMap1( i ))));
1219 for ( int i = 1; i <= vMap2.Extent(); ++i )
1220 box[ 1 ].Add( BRep_Tool::Pnt ( TopoDS::Vertex( vMap2( i ))));
1222 gp_Pnt gc[2]; // box center
1223 double x0,y0,z0, x1,y1,z1;
1224 box[0].Get( x0,y0,z0, x1,y1,z1 );
1225 gc[0] = 0.5 * ( gp_XYZ( x0,y0,z0 ) + gp_XYZ( x1,y1,z1 ));
1226 box[1].Get( x0,y0,z0, x1,y1,z1 );
1227 gc[1] = 0.5 * ( gp_XYZ( x0,y0,z0 ) + gp_XYZ( x1,y1,z1 ));
1230 gp_Vec vec01( gc[0], gc[1] );
1231 double scale = sqrt( box[1].SquareExtent() / box[0].SquareExtent() );
1233 // Find 2 closest vertices
1235 // get 2 linked vertices of shape 1 not belonging to an inner wire of a face
1236 std::list< TopoDS_Edge > allBndEdges1;
1237 if ( !getOuterEdges( theShape1, *theMesh1, allBndEdges1 ))
1239 if ( theShape1.ShapeType() != TopAbs_FACE )
1240 RETURN_BAD_RESULT("Edge not found");
1241 return assocFewEdgesFaces( TopoDS::Face( theShape1 ), theMesh1,
1242 TopoDS::Face( theShape2 ), theMesh2, theMap );
1244 std::list< TopoDS_Edge >::iterator edge1 = allBndEdges1.begin();
1245 double minDist = std::numeric_limits<double>::max();
1246 for ( int nbChecked=0; edge1 != allBndEdges1.end() && nbChecked++ < 10; ++edge1 )
1248 TopoDS_Vertex edge1VV[2];
1249 TopExp::Vertices( TopoDS::Edge( edge1->Oriented(TopAbs_FORWARD)), edge1VV[0], edge1VV[1]);
1250 if ( edge1VV[0].IsSame( edge1VV[1] ))
1251 continue;//RETURN_BAD_RESULT("Only closed edges");
1253 // find vertices closest to 2 linked vertices of shape 1
1254 double dist2[2] = { 1e+100, 1e+100 };
1255 TopoDS_Vertex edge2VV[2];
1256 for ( int i1 = 0; i1 < 2; ++i1 )
1258 gp_Pnt p1 = BRep_Tool::Pnt( edge1VV[ i1 ]);
1259 p1.Scale( gc[0], scale );
1260 p1.Translate( vec01 );
1262 // select a closest vertex among all ones in vMap2
1263 for ( int i2 = 1; i2 <= vMap2.Extent(); ++i2 )
1265 TopoDS_Vertex V2 = TopoDS::Vertex( vMap2( i2 ));
1266 gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
1267 double d2 = p1.SquareDistance( p2 );
1268 if ( d2 < dist2[ 0 ] && d2 < minDist ) {
1274 else if ( !edge2VV[0].IsNull() ) {
1275 // select a closest vertex among ends of edges meeting at edge2VV[0]
1276 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( edge2VV[0],
1277 *theMesh2, TopAbs_EDGE);
1278 while ( const TopoDS_Shape* edge2 = edgeIt->next() )
1279 for ( TopoDS_Iterator itV2( *edge2 ); itV2.More(); itV2.Next() )
1281 if ( itV2.Value().IsSame( edge2VV[ 0 ])) continue;
1282 if ( !vMap2.Contains( itV2.Value() )) continue;
1283 TopoDS_Vertex V2 = TopoDS::Vertex( itV2.Value() );
1284 gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
1285 double d2 = p1.SquareDistance( p2 );
1286 if ( d2 < dist2[1] && d2 < minDist ) {
1293 if ( dist2[0] + dist2[1] < minDist ) {
1294 VV1[0] = edge1VV[0];
1295 VV1[1] = edge1VV[1];
1296 VV2[0] = edge2VV[0];
1297 VV2[1] = edge2VV[1];
1298 minDist = dist2[0] + dist2[1];
1299 if ( minDist < 1e-10 )
1304 InsertAssociation( VV1[ 0 ], VV2[ 0 ], theMap );
1305 InsertAssociation( VV1[ 1 ], VV2[ 1 ], theMap );
1306 MESSAGE("Initial assoc VERT " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[ 0 ] )<<
1307 " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[ 0 ] )<<
1308 "\nand VERT " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[ 1 ] )<<
1309 " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[ 1 ] ));
1310 if ( theShape1.ShapeType() == TopAbs_EDGE ) {
1311 InsertAssociation( theShape1, theShape2, theMap );
1315 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap );
1318 //================================================================================
1320 * Find association of edges of faces
1321 * \param face1 - face 1
1322 * \param VV1 - vertices of face 1
1323 * \param face2 - face 2
1324 * \param VV2 - vertices of face 2 associated with ones of face 1
1325 * \param edges1 - out list of edges of face 1
1326 * \param edges2 - out list of edges of face 2
1327 * \retval int - nb of edges in an outer wire in a success case, else zero
1329 //================================================================================
1331 int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face& face1,
1332 TopoDS_Vertex VV1[2],
1333 const TopoDS_Face& face2,
1334 TopoDS_Vertex VV2[2],
1335 list< TopoDS_Edge > & edges1,
1336 list< TopoDS_Edge > & edges2)
1339 list< int > nbEInW1, nbEInW2;
1340 list< TopoDS_Edge >::iterator edgeIt;
1341 int i_ok_wire_algo = -1;
1342 for ( int outer_wire_algo = 0; outer_wire_algo < 2 && !OK; ++outer_wire_algo )
1347 if ( SMESH_Block::GetOrderedEdges( face1, edges1, nbEInW1, VV1[0], outer_wire_algo) !=
1348 SMESH_Block::GetOrderedEdges( face2, edges2, nbEInW2, VV2[0], outer_wire_algo) )
1349 CONT_BAD_RESULT("Different number of wires in faces ");
1351 if ( nbEInW1 != nbEInW2 && outer_wire_algo == 0 &&
1352 ( std::accumulate( nbEInW1.begin(), nbEInW1.end(), 0) !=
1353 std::accumulate( nbEInW2.begin(), nbEInW2.end(), 0)))
1354 RETURN_BAD_RESULT("Different number of edges in faces");
1356 if ( nbEInW1.front() != nbEInW2.front() )
1357 CONT_BAD_RESULT("Different number of edges in the outer wire: " <<
1358 nbEInW1.front() << " != " << nbEInW2.front());
1360 i_ok_wire_algo = outer_wire_algo;
1362 // Define if we need to reverse one of wires to make edges in lists match each other
1364 bool reverse = false;
1366 if ( !VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) {
1368 edgeIt = --edges1.end();
1369 // check if the second vertex belongs to the first or last edge in the wire
1370 if ( !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
1371 bool KO = true; // belongs to none
1372 if ( nbEInW1.size() > 1 ) { // several wires
1373 edgeIt = edges1.begin();
1374 std::advance( edgeIt, nbEInW1.front()-1 );
1375 KO = !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ));
1378 CONT_BAD_RESULT("GetOrderedEdges() failed");
1381 if ( !VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))) {
1383 edgeIt = --edges2.end();
1384 // move a degenerated edge from back to front
1385 // http://www.salome-platform.org/forum/forum_11/173031193
1386 if ( TopExp::FirstVertex( *edgeIt ).IsSame( TopExp::LastVertex( *edgeIt ))) {
1387 edges2.splice( edges2.begin(), edges2, edgeIt );
1388 edgeIt = --edges2.end();
1390 // check if the second vertex belongs to the first or last edge in the wire
1391 if ( !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
1392 bool KO = true; // belongs to none
1393 if ( nbEInW2.size() > 1 ) { // several wires
1394 edgeIt = edges2.begin();
1395 std::advance( edgeIt, nbEInW2.front()-1 );
1396 KO = !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ));
1399 CONT_BAD_RESULT("GetOrderedEdges() failed");
1404 reverseEdges( edges2 , nbEInW2.front());
1405 if (( VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) !=
1406 ( VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))))
1407 CONT_BAD_RESULT("GetOrderedEdges() failed");
1411 } // loop algos getting an outer wire
1413 // Try to orient all (if !OK) or only internal wires (issue 0020996) by UV similarity
1415 if (( !OK || nbEInW1.size() > 1 ) && i_ok_wire_algo > -1 )
1417 // Check that Vec(VV1[0],VV1[1]) in 2D on face1 is the same
1418 // as Vec(VV2[0],VV2[1]) on face2
1419 double vTol = BRep_Tool::Tolerance( VV1[0] );
1420 BRepAdaptor_Surface surface1( face1, false );
1422 surface1.UResolution( vTol ) + surface1.VResolution( vTol ); // let's be tolerant
1423 gp_Pnt2d v0f1UV = BRep_Tool::Parameters( VV1[0], face1 );
1424 gp_Pnt2d v0f2UV = BRep_Tool::Parameters( VV2[0], face2 );
1425 gp_Pnt2d v1f1UV = BRep_Tool::Parameters( VV1[1], face1 );
1426 gp_Pnt2d v1f2UV = BRep_Tool::Parameters( VV2[1], face2 );
1427 gp_Vec2d v01f1Vec( v0f1UV, v1f1UV );
1428 gp_Vec2d v01f2Vec( v0f2UV, v1f2UV );
1429 if ( Abs( v01f1Vec.X()-v01f2Vec.X()) < vTolUV &&
1430 Abs( v01f1Vec.Y()-v01f2Vec.Y()) < vTolUV )
1432 if ( !OK /*i_ok_wire_algo != 1*/ )
1436 SMESH_Block::GetOrderedEdges( face1, edges1, nbEInW1, VV1[0], i_ok_wire_algo);
1437 SMESH_Block::GetOrderedEdges( face2, edges2, nbEInW2, VV2[0], i_ok_wire_algo);
1439 gp_XY dUV = v0f2UV.XY() - v0f1UV.XY(); // UV shift between 2 faces
1441 // skip edges of the outer wire (if the outer wire is OK)
1442 list< int >::iterator nbE2, nbE1 = nbEInW1.begin();
1443 list< TopoDS_Edge >::iterator edge2Beg, edge1Beg = edges1.begin();
1444 if ( OK ) std::advance( edge1Beg, *nbE1++ );
1445 // reach an end of edges of a current wire1
1446 list< TopoDS_Edge >::iterator edge2End, edge1End;
1448 // find corresponding wires of face2
1449 for ( int iW1 = OK; nbE1 != nbEInW1.end(); ++nbE1, ++iW1 ) // loop on wires of face1
1451 // reach an end of edges of a current wire1
1452 edge1End = edge1Beg;
1453 std::advance( edge1End, *nbE1 );
1454 // UV on face1 to find on face2
1455 v0f1UV = BRep_Tool::Parameters( TopExp::FirstVertex(*edge1Beg,true), face1 );
1456 v1f1UV = BRep_Tool::Parameters( TopExp::LastVertex (*edge1Beg,true), face1 );
1457 v0f1UV.ChangeCoord() += dUV;
1458 v1f1UV.ChangeCoord() += dUV;
1460 // look through wires of face2
1461 edge2Beg = edges2.begin();
1462 nbE2 = nbEInW2.begin();
1463 if ( OK ) std::advance( edge2Beg, *nbE2++ );
1464 for ( int iW2 = OK; nbE2 != nbEInW2.end(); ++nbE2, ++iW2 ) // loop on wires of face2
1466 // reach an end of edges of a current wire2
1467 edge2End = edge2Beg;
1468 std::advance( edge2End, *nbE2 );
1469 if ( *nbE1 == *nbE2 && iW2 >= iW1 )
1471 // rotate edge2 untill coincidence with edge1 in 2D
1473 while ( i-- > 0 && !sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
1474 // move edge2Beg to place before edge2End
1475 edges2.splice( edge2End, edges2, edge2Beg++ );
1477 if ( edge2Beg != edges2.end() &&
1478 sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV ))
1480 if ( iW1 == 0 ) OK = true; // OK is for the first wire
1481 // reverse edges2 if needed
1482 if ( !sameVertexUV( *edge2Beg, face2, 1, v1f1UV, vTolUV ))
1483 reverseEdges( edges2 , *nbE2, std::distance( edges2.begin(),edge2Beg ));
1484 // put wire2 at a right place within edges2
1486 list< TopoDS_Edge >::iterator place2 = edges2.begin();
1487 std::advance( place2, std::distance( edges1.begin(), edge1Beg ));
1488 edges2.splice( place2, edges2, edge2Beg, edge2End );
1489 // move nbE2 as well
1490 list< int >::iterator placeNbE2 = nbEInW2.begin();
1491 std::advance( placeNbE2, iW1 );
1492 nbEInW2.splice( placeNbE2, nbEInW2, nbE2 );
1497 // prepare to the next wire loop
1498 edge2Beg = edge2End;
1500 edge1Beg = edge1End;
1505 const int nbEdges = nbEInW1.front();
1506 if ( OK && nbEdges == 2 )
1508 // if wires include 2 edges, it's impossible to associate them using
1509 // topological information only. Try to use length of edges for association.
1510 double l1[2], l2[2];
1511 edgeIt = edges1.begin();
1512 l1[0] = SMESH_Algo::EdgeLength( *edgeIt++ );
1513 l1[1] = SMESH_Algo::EdgeLength( *edgeIt++ );
1514 if ( Abs( l1[0] - l1[1] ) > 0.1 * Max( l1[0], l1[1] ) )
1516 edgeIt = edges2.begin();
1517 l2[0] = SMESH_Algo::EdgeLength( *edgeIt++ );
1518 l2[1] = SMESH_Algo::EdgeLength( *edgeIt++ );
1519 if (( l1[0] < l1[1] ) != ( l2[0] < l2[1] ))
1521 reverseEdges( edges2, nbEdges );
1526 return OK ? nbEInW1.front() : 0;
1529 //=======================================================================
1530 //function : InitVertexAssociation
1532 //=======================================================================
1534 void StdMeshers_ProjectionUtils::InitVertexAssociation( const SMESH_Hypothesis* theHyp,
1535 TShapeShapeMap & theAssociationMap)
1537 string hypName = theHyp->GetName();
1538 if ( hypName == "ProjectionSource1D" ) {
1539 const StdMeshers_ProjectionSource1D * hyp =
1540 static_cast<const StdMeshers_ProjectionSource1D*>( theHyp );
1541 if ( hyp->HasVertexAssociation() )
1542 InsertAssociation( hyp->GetTargetVertex(),hyp->GetSourceVertex(),theAssociationMap );
1544 else if ( hypName == "ProjectionSource2D" ) {
1545 const StdMeshers_ProjectionSource2D * hyp =
1546 static_cast<const StdMeshers_ProjectionSource2D*>( theHyp );
1547 if ( hyp->HasVertexAssociation() ) {
1548 InsertAssociation( hyp->GetTargetVertex(1),hyp->GetSourceVertex(1),theAssociationMap);
1549 InsertAssociation( hyp->GetTargetVertex(2),hyp->GetSourceVertex(2),theAssociationMap);
1552 else if ( hypName == "ProjectionSource3D" ) {
1553 const StdMeshers_ProjectionSource3D * hyp =
1554 static_cast<const StdMeshers_ProjectionSource3D*>( theHyp );
1555 if ( hyp->HasVertexAssociation() ) {
1556 InsertAssociation( hyp->GetTargetVertex(1),hyp->GetSourceVertex(1),theAssociationMap);
1557 InsertAssociation( hyp->GetTargetVertex(2),hyp->GetSourceVertex(2),theAssociationMap);
1562 //=======================================================================
1564 * Inserts association theShape1 <-> theShape2 to TShapeShapeMap
1565 * \param theShape1 - target shape
1566 * \param theShape2 - source shape
1567 * \param theAssociationMap - association map
1568 * \retval bool - true if there was no association for these shapes before
1570 //=======================================================================
1572 bool StdMeshers_ProjectionUtils::InsertAssociation( const TopoDS_Shape& theShape1, // tgt
1573 const TopoDS_Shape& theShape2, // src
1574 TShapeShapeMap & theAssociationMap)
1576 if ( !theShape1.IsNull() && !theShape2.IsNull() ) {
1577 SHOW_SHAPE(theShape1,"Assoc ");
1578 SHOW_SHAPE(theShape2," to ");
1579 bool isNew = ( theAssociationMap.Bind( theShape1, theShape2 ));
1583 throw SALOME_Exception("StdMeshers_ProjectionUtils: attempt to associate NULL shape");
1588 //=======================================================================
1590 * Finds an edge by its vertices in a main shape of the mesh
1591 * \param aMesh - the mesh
1592 * \param V1 - vertex 1
1593 * \param V2 - vertex 2
1594 * \retval TopoDS_Edge - found edge
1596 //=======================================================================
1598 TopoDS_Edge StdMeshers_ProjectionUtils::GetEdgeByVertices( SMESH_Mesh* theMesh,
1599 const TopoDS_Vertex& theV1,
1600 const TopoDS_Vertex& theV2)
1602 if ( theMesh && !theV1.IsNull() && !theV2.IsNull() )
1604 TopTools_ListIteratorOfListOfShape ancestorIt( theMesh->GetAncestors( theV1 ));
1605 for ( ; ancestorIt.More(); ancestorIt.Next() )
1606 if ( ancestorIt.Value().ShapeType() == TopAbs_EDGE )
1607 for ( TopExp_Explorer expV ( ancestorIt.Value(), TopAbs_VERTEX );
1610 if ( theV2.IsSame( expV.Current() ))
1611 return TopoDS::Edge( ancestorIt.Value() );
1613 return TopoDS_Edge();
1616 //================================================================================
1618 * Return another face sharing an edge
1619 * \param edgeToFaces - data map of descendants to ancestors
1620 * \param edge - edge
1621 * \param face - face
1622 * \retval TopoDS_Face - found face
1624 //================================================================================
1626 TopoDS_Face StdMeshers_ProjectionUtils::GetNextFace( const TAncestorMap& edgeToFaces,
1627 const TopoDS_Edge& edge,
1628 const TopoDS_Face& face)
1630 // if ( !edge.IsNull() && !face.IsNull() && edgeToFaces.Contains( edge ))
1631 if ( !edge.IsNull() && edgeToFaces.Contains( edge )) // PAL16202
1633 TopTools_ListIteratorOfListOfShape ancestorIt( edgeToFaces.FindFromKey( edge ));
1634 for ( ; ancestorIt.More(); ancestorIt.Next() )
1635 if ( ancestorIt.Value().ShapeType() == TopAbs_FACE &&
1636 !face.IsSame( ancestorIt.Value() ))
1637 return TopoDS::Face( ancestorIt.Value() );
1639 return TopoDS_Face();
1642 //================================================================================
1644 * Return other vertex of an edge
1646 //================================================================================
1648 TopoDS_Vertex StdMeshers_ProjectionUtils::GetNextVertex(const TopoDS_Edge& edge,
1649 const TopoDS_Vertex& vertex)
1651 TopoDS_Vertex vF,vL;
1652 TopExp::Vertices(edge,vF,vL);
1653 if ( vF.IsSame( vL ))
1654 return TopoDS_Vertex();
1655 return vertex.IsSame( vF ) ? vL : vF;
1658 //================================================================================
1660 * Return a propagation edge
1661 * \param aMesh - mesh
1662 * \param anEdge - edge to find by propagation
1663 * \param fromEdge - start edge for propagation
1664 * \param chain - return, if !NULL, a propagation chain passed till
1665 * anEdge; if anEdge.IsNull() then a full propagation chain is returned;
1666 * fromEdge is the 1st in the chain
1667 * \retval pair<int,TopoDS_Edge> - propagation step and found edge
1669 //================================================================================
1671 pair<int,TopoDS_Edge>
1672 StdMeshers_ProjectionUtils::GetPropagationEdge( SMESH_Mesh* aMesh,
1673 const TopoDS_Edge& anEdge,
1674 const TopoDS_Edge& fromEdge,
1675 TopTools_IndexedMapOfShape* chain)
1677 TopTools_IndexedMapOfShape locChain;
1678 TopTools_IndexedMapOfShape& aChain = chain ? *chain : locChain;
1681 //TopTools_IndexedMapOfShape checkedWires;
1682 BRepTools_WireExplorer aWE;
1683 TopoDS_Shape fourEdges[4];
1685 // List of edges, added to chain on the previous cycle pass
1686 TopTools_ListOfShape listPrevEdges;
1687 listPrevEdges.Append( fromEdge );
1688 aChain.Add( fromEdge );
1690 // Collect all edges pass by pass
1691 while (listPrevEdges.Extent() > 0)
1694 // List of edges, added to chain on this cycle pass
1695 TopTools_ListOfShape listCurEdges;
1697 // Find the next portion of edges
1698 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1699 for (; itE.More(); itE.Next())
1701 const TopoDS_Shape& anE = itE.Value();
1703 // Iterate on faces, having edge <anE>
1704 TopTools_ListIteratorOfListOfShape itA (aMesh->GetAncestors(anE));
1705 for (; itA.More(); itA.Next())
1707 const TopoDS_Shape& aW = itA.Value();
1709 // There are objects of different type among the ancestors of edge
1710 if ( aW.ShapeType() == TopAbs_WIRE /*&& checkedWires.Add( aW )*/)
1712 Standard_Integer nb = 0, found = -1;
1713 for ( aWE.Init( TopoDS::Wire( aW )); aWE.More(); aWE.Next() ) {
1718 fourEdges[ nb ] = aWE.Current();
1719 if ( aWE.Current().IsSame( anE )) found = nb;
1722 if (nb == 4 && found >= 0) {
1723 // Quadrangle face found, get an opposite edge
1724 TopoDS_Shape& anOppE = fourEdges[( found + 2 ) % 4 ];
1726 // add anOppE to aChain if ...
1727 int prevChainSize = aChain.Extent();
1728 if ( aChain.Add(anOppE) > prevChainSize ) { // ... anOppE is not in aChain
1729 // Add found edge to the chain oriented so that to
1730 // have it co-directed with a forward MainEdge
1731 TopAbs_Orientation ori = anE.Orientation();
1732 if ( anOppE.Orientation() == fourEdges[found].Orientation() )
1733 ori = TopAbs::Reverse( ori );
1734 anOppE.Orientation( ori );
1735 if ( anOppE.IsSame( anEdge ))
1736 return make_pair( step, TopoDS::Edge( anOppE ));
1737 listCurEdges.Append(anOppE);
1739 } // if (nb == 4 && found >= 0)
1740 } // if (aF.ShapeType() == TopAbs_WIRE)
1741 } // loop on ancestors of anE
1742 } // loop on listPrevEdges
1744 listPrevEdges = listCurEdges;
1745 } // while (listPrevEdges.Extent() > 0)
1747 return make_pair( INT_MAX, TopoDS_Edge());
1750 //================================================================================
1752 * Find corresponding nodes on two faces
1753 * \param face1 - the first face
1754 * \param mesh1 - mesh containing elements on the first face
1755 * \param face2 - the second face
1756 * \param mesh2 - mesh containing elements on the second face
1757 * \param assocMap - map associating sub-shapes of the faces
1758 * \param node1To2Map - map containing found matching nodes
1759 * \retval bool - is a success
1761 //================================================================================
1763 bool StdMeshers_ProjectionUtils::
1764 FindMatchingNodesOnFaces( const TopoDS_Face& face1,
1766 const TopoDS_Face& face2,
1768 const TShapeShapeMap & assocMap,
1769 TNodeNodeMap & node1To2Map)
1771 SMESHDS_Mesh* meshDS1 = mesh1->GetMeshDS();
1772 SMESHDS_Mesh* meshDS2 = mesh2->GetMeshDS();
1774 SMESH_MesherHelper helper1( *mesh1 );
1775 SMESH_MesherHelper helper2( *mesh2 );
1777 // Get corresponding submeshes and roughly check match of meshes
1779 SMESHDS_SubMesh * SM2 = meshDS2->MeshElements( face2 );
1780 SMESHDS_SubMesh * SM1 = meshDS1->MeshElements( face1 );
1782 RETURN_BAD_RESULT("Empty submeshes");
1783 if ( SM2->NbNodes() != SM1->NbNodes() ||
1784 SM2->NbElements() != SM1->NbElements() )
1785 RETURN_BAD_RESULT("Different meshes on corresponding faces "
1786 << meshDS1->ShapeToIndex( face1 ) << " and "
1787 << meshDS2->ShapeToIndex( face2 ));
1788 if ( SM2->NbElements() == 0 )
1789 RETURN_BAD_RESULT("Empty submeshes");
1791 helper1.SetSubShape( face1 );
1792 helper2.SetSubShape( face2 );
1793 if ( helper1.HasSeam() != helper2.HasSeam() )
1794 RETURN_BAD_RESULT("Different faces' geometry");
1796 // Data to call SMESH_MeshEditor::FindMatchingNodes():
1798 // 1. Nodes of corresponding links:
1800 // get 2 matching edges, try to find not seam ones
1801 TopoDS_Edge edge1, edge2, seam1, seam2, anyEdge1, anyEdge2;
1802 TopExp_Explorer eE( OuterShape( face2, TopAbs_WIRE ), TopAbs_EDGE );
1805 TopoDS_Edge e2 = TopoDS::Edge( eE.Current() );
1808 if ( !assocMap.IsBound( e2, /*is2nd=*/true ))
1810 //RETURN_BAD_RESULT("Association not found for edge " << meshDS2->ShapeToIndex( e2 ));
1811 TopoDS_Edge e1 = TopoDS::Edge( assocMap( e2, /*is2nd=*/true ));
1812 if ( !helper1.IsSubShape( e1, face1 ))
1813 RETURN_BAD_RESULT("Wrong association, edge " << meshDS1->ShapeToIndex( e1 ) <<
1814 " isn't a sub-shape of face " << meshDS1->ShapeToIndex( face1 ));
1815 // check that there are nodes on edges
1816 SMESHDS_SubMesh * eSM1 = meshDS1->MeshElements( e1 );
1817 SMESHDS_SubMesh * eSM2 = meshDS2->MeshElements( e2 );
1818 bool nodesOnEdges = ( eSM1 && eSM2 && eSM1->NbNodes() && eSM2->NbNodes() );
1819 // check that the nodes on edges belong to faces
1820 // (as NETGEN ignores nodes on the degenerated geom edge)
1821 bool nodesOfFaces = false;
1822 if ( nodesOnEdges ) {
1823 const SMDS_MeshNode* n1 = eSM1->GetNodes()->next();
1824 const SMDS_MeshNode* n2 = eSM2->GetNodes()->next();
1825 nodesOfFaces = ( n1->GetInverseElementIterator(SMDSAbs_Face)->more() &&
1826 n2->GetInverseElementIterator(SMDSAbs_Face)->more() );
1830 if ( helper2.IsRealSeam( e2 )) {
1831 seam1 = e1; seam2 = e2;
1834 edge1 = e1; edge2 = e2;
1838 anyEdge1 = e1; anyEdge2 = e2;
1840 } while ( edge2.IsNull() && eE.More() );
1842 if ( edge2.IsNull() ) {
1843 edge1 = seam1; edge2 = seam2;
1845 bool hasNodesOnEdge = (! edge2.IsNull() );
1846 if ( !hasNodesOnEdge ) {
1847 // 0020338 - nb segments == 1
1848 edge1 = anyEdge1; edge2 = anyEdge2;
1851 // get 2 matching vertices
1852 TopoDS_Vertex V2 = TopExp::FirstVertex( TopoDS::Edge( edge2 ));
1853 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
1855 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
1856 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
1857 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
1859 TopoDS_Vertex V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
1861 // nodes on vertices
1862 const SMDS_MeshNode* vNode1 = SMESH_Algo::VertexNode( V1, meshDS1 );
1863 const SMDS_MeshNode* vNode2 = SMESH_Algo::VertexNode( V2, meshDS2 );
1864 if ( !vNode1 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
1865 if ( !vNode2 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
1867 // nodes on edges linked with nodes on vertices
1868 const SMDS_MeshNode* nullNode = 0;
1869 vector< const SMDS_MeshNode*> eNode1( 2, nullNode );
1870 vector< const SMDS_MeshNode*> eNode2( 2, nullNode );
1871 if ( hasNodesOnEdge )
1873 int nbNodeToGet = 1;
1874 if ( helper1.IsClosedEdge( edge1 ) || helper2.IsClosedEdge( edge2 ) )
1876 for ( int is2 = 0; is2 < 2; ++is2 )
1878 TopoDS_Edge & edge = is2 ? edge2 : edge1;
1879 SMESHDS_Mesh * smDS = is2 ? meshDS2 : meshDS1;
1880 SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
1881 // nodes linked with ones on vertices
1882 const SMDS_MeshNode* vNode = is2 ? vNode2 : vNode1;
1883 vector< const SMDS_MeshNode*>& eNode = is2 ? eNode2 : eNode1;
1885 SMDS_ElemIteratorPtr vElem = vNode->GetInverseElementIterator(SMDSAbs_Edge);
1886 while ( vElem->more() && nbGotNode != nbNodeToGet ) {
1887 const SMDS_MeshElement* elem = vElem->next();
1888 if ( edgeSM->Contains( elem ))
1889 eNode[ nbGotNode++ ] =
1890 ( elem->GetNode(0) == vNode ) ? elem->GetNode(1) : elem->GetNode(0);
1892 if ( nbGotNode > 1 ) // sort found nodes by param on edge
1894 SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
1895 double u0 = helper->GetNodeU( edge, eNode[ 0 ]);
1896 double u1 = helper->GetNodeU( edge, eNode[ 1 ]);
1897 if ( u0 > u1 ) std::swap( eNode[ 0 ], eNode[ 1 ]);
1899 if ( nbGotNode == 0 )
1900 RETURN_BAD_RESULT("Found no nodes on edge " << smDS->ShapeToIndex( edge ) <<
1901 " linked to " << vNode );
1904 else // 0020338 - nb segments == 1
1906 // get 2 other matching vertices
1907 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
1908 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
1909 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
1910 V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
1912 // nodes on vertices
1913 eNode1[0] = SMESH_Algo::VertexNode( V1, meshDS1 );
1914 eNode2[0] = SMESH_Algo::VertexNode( V2, meshDS2 );
1915 if ( !eNode1[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
1916 if ( !eNode2[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
1921 set<const SMDS_MeshElement*> Elems1, Elems2;
1922 for ( int is2 = 0; is2 < 2; ++is2 )
1924 set<const SMDS_MeshElement*> & elems = is2 ? Elems2 : Elems1;
1925 SMESHDS_SubMesh* sm = is2 ? SM2 : SM1;
1926 SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
1927 const TopoDS_Face & face = is2 ? face2 : face1;
1928 SMDS_ElemIteratorPtr eIt = sm->GetElements();
1930 if ( !helper->IsRealSeam( is2 ? edge2 : edge1 ))
1932 while ( eIt->more() ) elems.insert( eIt->next() );
1936 // the only suitable edge is seam, i.e. it is a sphere.
1937 // FindMatchingNodes() will not know which way to go from any edge.
1938 // So we ignore all faces having nodes on edges or vertices except
1939 // one of faces sharing current start nodes
1941 // find a face to keep
1942 const SMDS_MeshElement* faceToKeep = 0;
1943 const SMDS_MeshNode* vNode = is2 ? vNode2 : vNode1;
1944 const SMDS_MeshNode* eNode = is2 ? eNode2[0] : eNode1[0];
1945 TIDSortedElemSet inSet, notInSet;
1947 const SMDS_MeshElement* f1 =
1948 SMESH_MeshAlgos::FindFaceInSet( vNode, eNode, inSet, notInSet );
1949 if ( !f1 ) RETURN_BAD_RESULT("The first face on seam not found");
1950 notInSet.insert( f1 );
1952 const SMDS_MeshElement* f2 =
1953 SMESH_MeshAlgos::FindFaceInSet( vNode, eNode, inSet, notInSet );
1954 if ( !f2 ) RETURN_BAD_RESULT("The second face on seam not found");
1956 // select a face with less UV of vNode
1957 const SMDS_MeshNode* notSeamNode[2] = {0, 0};
1958 for ( int iF = 0; iF < 2; ++iF ) {
1959 const SMDS_MeshElement* f = ( iF ? f2 : f1 );
1960 for ( int i = 0; !notSeamNode[ iF ] && i < f->NbNodes(); ++i ) {
1961 const SMDS_MeshNode* node = f->GetNode( i );
1962 if ( !helper->IsSeamShape( node->getshapeId() ))
1963 notSeamNode[ iF ] = node;
1966 gp_Pnt2d uv1 = helper->GetNodeUV( face, vNode, notSeamNode[0] );
1967 gp_Pnt2d uv2 = helper->GetNodeUV( face, vNode, notSeamNode[1] );
1968 if ( uv1.X() + uv1.Y() > uv2.X() + uv2.Y() )
1974 elems.insert( faceToKeep );
1975 while ( eIt->more() ) {
1976 const SMDS_MeshElement* f = eIt->next();
1977 int nbNodes = f->NbNodes();
1978 if ( f->IsQuadratic() )
1981 for ( int i = 0; !onBnd && i < nbNodes; ++i ) {
1982 const SMDS_MeshNode* node = f->GetNode( i );
1983 onBnd = ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE);
1988 // add also faces adjacent to faceToKeep
1989 int nbNodes = faceToKeep->NbNodes();
1990 if ( faceToKeep->IsQuadratic() ) nbNodes /= 2;
1991 notInSet.insert( f1 );
1992 notInSet.insert( f2 );
1993 for ( int i = 0; i < nbNodes; ++i ) {
1994 const SMDS_MeshNode* n1 = faceToKeep->GetNode( i );
1995 const SMDS_MeshNode* n2 = faceToKeep->GetNode(( i+1 ) % nbNodes );
1996 f1 = SMESH_MeshAlgos::FindFaceInSet( n1, n2, inSet, notInSet );
2000 } // case on a sphere
2001 } // loop on 2 faces
2003 // int quadFactor = (*Elems1.begin())->IsQuadratic() ? 2 : 1;
2005 node1To2Map.clear();
2006 int res = SMESH_MeshEditor::FindMatchingNodes( Elems1, Elems2,
2008 eNode1[0], eNode2[0],
2010 if ( res != SMESH_MeshEditor::SEW_OK )
2011 RETURN_BAD_RESULT("FindMatchingNodes() result " << res );
2013 // On a sphere, add matching nodes on the edge
2015 if ( helper1.IsRealSeam( edge1 ))
2017 // sort nodes on edges by param on edge
2018 map< double, const SMDS_MeshNode* > u2nodesMaps[2];
2019 for ( int is2 = 0; is2 < 2; ++is2 )
2021 TopoDS_Edge & edge = is2 ? edge2 : edge1;
2022 SMESHDS_Mesh * smDS = is2 ? meshDS2 : meshDS1;
2023 SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
2024 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[ is2 ];
2026 SMDS_NodeIteratorPtr nIt = edgeSM->GetNodes();
2027 while ( nIt->more() ) {
2028 const SMDS_MeshNode* node = nIt->next();
2029 const SMDS_EdgePosition* pos =
2030 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
2031 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
2033 if ( pos2nodes.size() != edgeSM->NbNodes() )
2034 RETURN_BAD_RESULT("Equal params of nodes on edge "
2035 << smDS->ShapeToIndex( edge ) << " of face " << is2 );
2037 if ( u2nodesMaps[0].size() != u2nodesMaps[1].size() )
2038 RETURN_BAD_RESULT("Different nb of new nodes on edges or wrong params");
2040 // compare edge orientation
2041 double u1 = helper1.GetNodeU( edge1, vNode1 );
2042 double u2 = helper2.GetNodeU( edge2, vNode2 );
2043 bool isFirst1 = ( u1 < u2nodesMaps[0].begin()->first );
2044 bool isFirst2 = ( u2 < u2nodesMaps[1].begin()->first );
2045 bool reverse ( isFirst1 != isFirst2 );
2047 // associate matching nodes
2048 map< double, const SMDS_MeshNode* >::iterator u_Node1, u_Node2, end1;
2049 map< double, const SMDS_MeshNode* >::reverse_iterator uR_Node2;
2050 u_Node1 = u2nodesMaps[0].begin();
2051 u_Node2 = u2nodesMaps[1].begin();
2052 uR_Node2 = u2nodesMaps[1].rbegin();
2053 end1 = u2nodesMaps[0].end();
2054 for ( ; u_Node1 != end1; ++u_Node1 ) {
2055 const SMDS_MeshNode* n1 = u_Node1->second;
2056 const SMDS_MeshNode* n2 = ( reverse ? (uR_Node2++)->second : (u_Node2++)->second );
2057 node1To2Map.insert( make_pair( n1, n2 ));
2060 // associate matching nodes on the last vertices
2061 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
2062 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
2063 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
2064 V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
2065 vNode1 = SMESH_Algo::VertexNode( V1, meshDS1 );
2066 vNode2 = SMESH_Algo::VertexNode( V2, meshDS2 );
2067 if ( !vNode1 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
2068 if ( !vNode2 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
2069 node1To2Map.insert( make_pair( vNode1, vNode2 ));
2072 // don't know why this condition is usually true :(
2073 // if ( node1To2Map.size() * quadFactor < SM1->NbNodes() )
2074 // MESSAGE("FindMatchingNodes() found too few node pairs starting from nodes ("
2075 // << vNode1->GetID() << " - " << eNode1[0]->GetID() << ") ("
2076 // << vNode2->GetID() << " - " << eNode2[0]->GetID() << "):"
2077 // << node1To2Map.size() * quadFactor << " < " << SM1->NbNodes());
2082 //================================================================================
2084 * Return any sub-shape of a face belonging to the outer wire
2085 * \param face - the face
2086 * \param type - type of sub-shape to return
2087 * \retval TopoDS_Shape - the found sub-shape
2089 //================================================================================
2091 TopoDS_Shape StdMeshers_ProjectionUtils::OuterShape( const TopoDS_Face& face,
2092 TopAbs_ShapeEnum type)
2094 TopExp_Explorer exp( BRepTools::OuterWire( face ), type );
2096 return exp.Current();
2097 return TopoDS_Shape();
2100 //================================================================================
2102 * Check that sub-mesh is computed and try to compute it if is not
2103 * \param sm - sub-mesh to compute
2104 * \param iterationNb - int used to stop infinite recursive call
2105 * \retval bool - true if computed
2107 //================================================================================
2109 bool StdMeshers_ProjectionUtils::MakeComputed(SMESH_subMesh * sm, const int iterationNb)
2111 if ( iterationNb > 10 )
2112 RETURN_BAD_RESULT("Infinite recursive projection");
2114 RETURN_BAD_RESULT("NULL submesh");
2115 if ( sm->IsMeshComputed() )
2118 SMESH_Mesh* mesh = sm->GetFather();
2119 SMESH_Gen* gen = mesh->GetGen();
2120 SMESH_Algo* algo = sm->GetAlgo();
2121 TopoDS_Shape shape = sm->GetSubShape();
2124 if ( shape.ShapeType() != TopAbs_COMPOUND )
2126 // No algo assigned to a non-compound sub-mesh.
2127 // Try to find an all-dimensional algo of an upper dimension
2128 int dim = gen->GetShapeDim( shape );
2129 for ( ++dim; ( dim <= 3 && !algo ); ++dim )
2131 SMESH_HypoFilter hypoFilter( SMESH_HypoFilter::IsAlgo() );
2132 hypoFilter.And( SMESH_HypoFilter::HasDim( dim ));
2133 list <const SMESHDS_Hypothesis * > hyps;
2134 list< TopoDS_Shape > assignedTo;
2136 mesh->GetHypotheses( shape, hypoFilter, hyps, true, &assignedTo );
2137 if ( nbAlgos > 1 ) // concurrent algos
2139 vector<SMESH_subMesh*> smList; // where an algo is assigned
2140 list< TopoDS_Shape >::iterator shapeIt = assignedTo.begin();
2141 for ( ; shapeIt != assignedTo.end(); ++shapeIt )
2142 smList.push_back( mesh->GetSubMesh( *shapeIt ));
2144 mesh->SortByMeshOrder( smList );
2145 algo = smList.front()->GetAlgo();
2146 shape = smList.front()->GetSubShape();
2148 else if ( nbAlgos == 1 )
2150 algo = (SMESH_Algo*) hyps.front();
2151 shape = assignedTo.front();
2160 bool computed = true;
2161 for ( TopoDS_Iterator grMember( shape ); grMember.More(); grMember.Next())
2162 if ( SMESH_subMesh* grSub = mesh->GetSubMesh( grMember.Value() ))
2163 if ( !MakeComputed( grSub, iterationNb + 1 ))
2169 string algoType = algo->GetName();
2170 if ( algoType.substr(0, 11) != "Projection_")
2171 return gen->Compute( *mesh, shape, /*shapeOnly=*/true );
2173 // try to compute source mesh
2175 const list <const SMESHDS_Hypothesis *> & hyps =
2176 algo->GetUsedHypothesis( *mesh, shape );
2178 TopoDS_Shape srcShape;
2179 SMESH_Mesh* srcMesh = 0;
2180 list <const SMESHDS_Hypothesis*>::const_iterator hIt = hyps.begin();
2181 for ( ; srcShape.IsNull() && hIt != hyps.end(); ++hIt ) {
2182 string hypName = (*hIt)->GetName();
2183 if ( hypName == "ProjectionSource1D" ) {
2184 const StdMeshers_ProjectionSource1D * hyp =
2185 static_cast<const StdMeshers_ProjectionSource1D*>( *hIt );
2186 srcShape = hyp->GetSourceEdge();
2187 srcMesh = hyp->GetSourceMesh();
2189 else if ( hypName == "ProjectionSource2D" ) {
2190 const StdMeshers_ProjectionSource2D * hyp =
2191 static_cast<const StdMeshers_ProjectionSource2D*>( *hIt );
2192 srcShape = hyp->GetSourceFace();
2193 srcMesh = hyp->GetSourceMesh();
2195 else if ( hypName == "ProjectionSource3D" ) {
2196 const StdMeshers_ProjectionSource3D * hyp =
2197 static_cast<const StdMeshers_ProjectionSource3D*>( *hIt );
2198 srcShape = hyp->GetSource3DShape();
2199 srcMesh = hyp->GetSourceMesh();
2202 if ( srcShape.IsNull() ) // no projection source defined
2203 return gen->Compute( *mesh, shape, /*shapeOnly=*/true );
2205 if ( srcShape.IsSame( shape ))
2206 RETURN_BAD_RESULT("Projection from self");
2211 if ( MakeComputed( srcMesh->GetSubMesh( srcShape ), iterationNb + 1 ) &&
2212 gen->Compute( *mesh, shape, /*shapeOnly=*/true ))
2213 return sm->IsMeshComputed();
2219 //================================================================================
2221 * Returns an error message to show in case if MakeComputed( sm ) fails.
2223 //================================================================================
2225 std::string StdMeshers_ProjectionUtils::SourceNotComputedError( SMESH_subMesh * sm,
2226 SMESH_Algo* projAlgo )
2228 const char usualMessage [] = "Source mesh not computed";
2230 return usualMessage;
2231 if ( !sm || sm->GetAlgoState() != SMESH_subMesh::NO_ALGO )
2232 return usualMessage; // algo is OK, anything else is KO.
2234 // Try to find a type of all-dimentional algorithm that would compute the
2235 // given sub-mesh if it could be launched before projection
2236 const TopoDS_Shape shape = sm->GetSubShape();
2237 const int shapeDim = SMESH_Gen::GetShapeDim( shape );
2239 for ( int dimIncrement = 1; shapeDim + dimIncrement < 4; ++dimIncrement )
2241 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
2242 filter.And( filter.HasDim( shapeDim + dimIncrement ));
2244 SMESH_Algo* algo = (SMESH_Algo*) sm->GetFather()->GetHypothesis( shape, filter, true );
2245 if ( algo && !algo->NeedDiscreteBoundary() )
2246 return SMESH_Comment("\"")
2247 << algo->GetFeatures()._label << "\""
2248 << " can't be used to compute the source mesh for \""
2249 << projAlgo->GetFeatures()._label << "\" in this case";
2251 return usualMessage;
2254 //================================================================================
2256 * Return a boundary EDGE (or all boundary EDGEs) of edgeContainer
2258 //================================================================================
2261 StdMeshers_ProjectionUtils::GetBoundaryEdge(const TopoDS_Shape& edgeContainer,
2262 const SMESH_Mesh& mesh,
2263 std::list< TopoDS_Edge >* allBndEdges)
2265 TopTools_IndexedMapOfShape facesOfEdgeContainer, facesNearEdge;
2266 TopExp::MapShapes( edgeContainer, TopAbs_FACE, facesOfEdgeContainer );
2268 if ( !facesOfEdgeContainer.IsEmpty() )
2269 for ( TopExp_Explorer exp(edgeContainer, TopAbs_EDGE); exp.More(); exp.Next() )
2271 const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
2272 facesNearEdge.Clear();
2273 PShapeIteratorPtr faceIt = SMESH_MesherHelper::GetAncestors( edge, mesh, TopAbs_FACE );
2274 while ( const TopoDS_Shape* face = faceIt->next() )
2275 if ( facesOfEdgeContainer.Contains( *face ))
2276 if ( facesNearEdge.Add( *face ) && facesNearEdge.Extent() > 1 )
2278 if ( facesNearEdge.Extent() == 1 ) {
2280 allBndEdges->push_back( edge );
2286 return TopoDS_Edge();
2290 namespace { // Definition of event listeners
2292 SMESH_subMeshEventListener* getSrcSubMeshListener();
2294 //================================================================================
2296 * \brief Listener that resets an event listener on source submesh when
2297 * "ProjectionSource*D" hypothesis is modified
2299 //================================================================================
2301 struct HypModifWaiter: SMESH_subMeshEventListener
2303 HypModifWaiter():SMESH_subMeshEventListener(false,// won't be deleted by submesh
2304 "StdMeshers_ProjectionUtils::HypModifWaiter") {}
2305 void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
2306 EventListenerData*, const SMESH_Hypothesis*)
2308 if ( event == SMESH_subMesh::MODIF_HYP &&
2309 eventType == SMESH_subMesh::ALGO_EVENT)
2311 // delete current source listener
2312 subMesh->DeleteEventListener( getSrcSubMeshListener() );
2313 // let algo set a new one
2314 if ( SMESH_Algo* algo = subMesh->GetAlgo() )
2315 algo->SetEventListener( subMesh );
2319 //================================================================================
2321 * \brief return static HypModifWaiter
2323 //================================================================================
2325 SMESH_subMeshEventListener* getHypModifWaiter() {
2326 static HypModifWaiter aHypModifWaiter;
2327 return &aHypModifWaiter;
2329 //================================================================================
2331 * \brief return static listener for source shape submeshes
2333 //================================================================================
2335 SMESH_subMeshEventListener* getSrcSubMeshListener() {
2336 static SMESH_subMeshEventListener srcListener(false, // won't be deleted by submesh
2337 "StdMeshers_ProjectionUtils::SrcSubMeshListener");
2338 return &srcListener;
2342 //================================================================================
2344 * Set event listeners to submesh with projection algo
2345 * \param subMesh - submesh with projection algo
2346 * \param srcShape - source shape
2347 * \param srcMesh - source mesh
2349 //================================================================================
2351 void StdMeshers_ProjectionUtils::SetEventListener(SMESH_subMesh* subMesh,
2352 TopoDS_Shape srcShape,
2353 SMESH_Mesh* srcMesh)
2355 // Set the listener that resets an event listener on source submesh when
2356 // "ProjectionSource*D" hypothesis is modified since source shape can be changed
2357 subMesh->SetEventListener( getHypModifWaiter(),0,subMesh);
2359 // Set an event listener to submesh of the source shape
2360 if ( !srcShape.IsNull() )
2363 srcMesh = subMesh->GetFather();
2365 SMESH_subMesh* srcShapeSM = srcMesh->GetSubMesh( srcShape );
2367 if ( srcShapeSM != subMesh ) {
2368 if ( srcShapeSM->GetSubMeshDS() &&
2369 srcShapeSM->GetSubMeshDS()->IsComplexSubmesh() )
2370 { // source shape is a group
2371 TopExp_Explorer it(srcShapeSM->GetSubShape(), // explore the group into sub-shapes...
2372 subMesh->GetSubShape().ShapeType()); // ...of target shape type
2373 for (; it.More(); it.Next())
2375 SMESH_subMesh* srcSM = srcMesh->GetSubMesh( it.Current() );
2376 if ( srcSM != subMesh )
2378 SMESH_subMeshEventListenerData* data =
2379 srcSM->GetEventListenerData(getSrcSubMeshListener());
2381 data->mySubMeshes.push_back( subMesh );
2383 data = SMESH_subMeshEventListenerData::MakeData( subMesh );
2384 subMesh->SetEventListener ( getSrcSubMeshListener(), data, srcSM );
2390 if ( SMESH_subMeshEventListenerData* data =
2391 srcShapeSM->GetEventListenerData( getSrcSubMeshListener() ))
2394 (std::find( data->mySubMeshes.begin(),
2395 data->mySubMeshes.end(), subMesh ) != data->mySubMeshes.end() );
2397 data->mySubMeshes.push_back( subMesh );
2401 subMesh->SetEventListener( getSrcSubMeshListener(),
2402 SMESH_subMeshEventListenerData::MakeData( subMesh ),
2410 namespace StdMeshers_ProjectionUtils
2413 //================================================================================
2415 * \brief Computes transformation beween two sets of 2D points using
2416 * a least square approximation
2418 * See "Surface Mesh Projection For Hexahedral Mesh Generation By Sweeping"
2419 * by X.Roca, J.Sarrate, A.Huerta. (2.2)
2421 //================================================================================
2423 bool TrsfFinder2D::Solve( const vector< gp_XY >& srcPnts,
2424 const vector< gp_XY >& tgtPnts )
2426 // find gravity centers
2427 gp_XY srcGC( 0,0 ), tgtGC( 0,0 );
2428 for ( size_t i = 0; i < srcPnts.size(); ++i )
2430 srcGC += srcPnts[i];
2431 tgtGC += tgtPnts[i];
2433 srcGC /= srcPnts.size();
2434 tgtGC /= tgtPnts.size();
2438 math_Matrix mat (1,4,1,4, 0.);
2439 math_Vector vec (1,4, 0.);
2441 // cout << "m1 = smesh.Mesh('src')" << endl
2442 // << "m2 = smesh.Mesh('tgt')" << endl;
2443 double xx = 0, xy = 0, yy = 0;
2444 for ( size_t i = 0; i < srcPnts.size(); ++i )
2446 gp_XY srcUV = srcPnts[i] - srcGC;
2447 gp_XY tgtUV = tgtPnts[i] - tgtGC;
2448 xx += srcUV.X() * srcUV.X();
2449 yy += srcUV.Y() * srcUV.Y();
2450 xy += srcUV.X() * srcUV.Y();
2451 vec( 1 ) += srcUV.X() * tgtUV.X();
2452 vec( 2 ) += srcUV.Y() * tgtUV.X();
2453 vec( 3 ) += srcUV.X() * tgtUV.Y();
2454 vec( 4 ) += srcUV.Y() * tgtUV.Y();
2455 // cout << "m1.AddNode( " << srcUV.X() << ", " << srcUV.Y() << ", 0 )" << endl
2456 // << "m2.AddNode( " << tgtUV.X() << ", " << tgtUV.Y() << ", 0 )" << endl;
2458 mat( 1,1 ) = mat( 3,3 ) = xx;
2459 mat( 2,2 ) = mat( 4,4 ) = yy;
2460 mat( 1,2 ) = mat( 2,1 ) = mat( 3,4 ) = mat( 4,3 ) = xy;
2462 math_Gauss solver( mat );
2463 if ( !solver.IsDone() )
2465 solver.Solve( vec );
2466 if ( vec.Norm2() < gp::Resolution() )
2468 // cout << vec( 1 ) << "\t " << vec( 2 ) << endl
2469 // << vec( 3 ) << "\t " << vec( 4 ) << endl;
2471 _trsf.SetTranslation( tgtGC );
2474 gp_Mat2d& M = const_cast< gp_Mat2d& >( _trsf.HVectorialPart());
2475 M( 1,1 ) = vec( 1 );
2476 M( 2,1 ) = vec( 2 );
2477 M( 1,2 ) = vec( 3 );
2478 M( 2,2 ) = vec( 4 );
2483 //================================================================================
2485 * \brief Transforms a 2D points using a found transformation
2487 //================================================================================
2489 gp_XY TrsfFinder2D::Transform( const gp_Pnt2d& srcUV ) const
2491 gp_XY uv = srcUV.XY() - _srcOrig ;
2492 _trsf.Transforms( uv );
2496 //================================================================================
2498 * \brief Computes transformation beween two sets of 3D points using
2499 * a least square approximation
2501 * See "Surface Mesh Projection For Hexahedral Mesh Generation By Sweeping"
2502 * by X.Roca, J.Sarrate, A.Huerta. (2.4)
2504 //================================================================================
2506 bool TrsfFinder3D::Solve( const vector< gp_XYZ > & srcPnts,
2507 const vector< gp_XYZ > & tgtPnts )
2509 // find gravity center
2510 gp_XYZ srcGC( 0,0,0 ), tgtGC( 0,0,0 );
2511 for ( size_t i = 0; i < srcPnts.size(); ++i )
2513 srcGC += srcPnts[i];
2514 tgtGC += tgtPnts[i];
2516 srcGC /= srcPnts.size();
2517 tgtGC /= tgtPnts.size();
2519 gp_XYZ srcOrig = 2 * srcGC - tgtGC;
2520 gp_XYZ tgtOrig = srcGC;
2524 math_Matrix mat (1,9,1,9, 0.);
2525 math_Vector vec (1,9, 0.);
2527 double xx = 0, yy = 0, zz = 0;
2528 double xy = 0, xz = 0, yz = 0;
2529 for ( size_t i = 0; i < srcPnts.size(); ++i )
2531 gp_XYZ src = srcPnts[i] - srcOrig;
2532 gp_XYZ tgt = tgtPnts[i] - tgtOrig;
2533 xx += src.X() * src.X();
2534 yy += src.Y() * src.Y();
2535 zz += src.Z() * src.Z();
2536 xy += src.X() * src.Y();
2537 xz += src.X() * src.Z();
2538 yz += src.Y() * src.Z();
2539 vec( 1 ) += src.X() * tgt.X();
2540 vec( 2 ) += src.Y() * tgt.X();
2541 vec( 3 ) += src.Z() * tgt.X();
2542 vec( 4 ) += src.X() * tgt.Y();
2543 vec( 5 ) += src.Y() * tgt.Y();
2544 vec( 6 ) += src.Z() * tgt.Y();
2545 vec( 7 ) += src.X() * tgt.Z();
2546 vec( 8 ) += src.Y() * tgt.Z();
2547 vec( 9 ) += src.Z() * tgt.Z();
2549 mat( 1,1 ) = mat( 4,4 ) = mat( 7,7 ) = xx;
2550 mat( 2,2 ) = mat( 5,5 ) = mat( 8,8 ) = yy;
2551 mat( 3,3 ) = mat( 6,6 ) = mat( 9,9 ) = zz;
2552 mat( 1,2 ) = mat( 2,1 ) = mat( 4,5 ) = mat( 5,4 ) = mat( 7,8 ) = mat( 8,7 ) = xy;
2553 mat( 1,3 ) = mat( 3,1 ) = mat( 4,6 ) = mat( 6,4 ) = mat( 7,9 ) = mat( 9,7 ) = xz;
2554 mat( 2,3 ) = mat( 3,2 ) = mat( 5,6 ) = mat( 6,5 ) = mat( 8,9 ) = mat( 9,8 ) = yz;
2556 math_Gauss solver( mat );
2557 if ( !solver.IsDone() )
2559 solver.Solve( vec );
2560 if ( vec.Norm2() < gp::Resolution() )
2563 // << vec( 1 ) << "\t " << vec( 2 ) << "\t " << vec( 3 ) << endl
2564 // << vec( 4 ) << "\t " << vec( 5 ) << "\t " << vec( 6 ) << endl
2565 // << vec( 7 ) << "\t " << vec( 8 ) << "\t " << vec( 9 ) << endl;
2568 _trsf.SetTranslation( tgtOrig );
2570 gp_Mat& M = const_cast< gp_Mat& >( _trsf.HVectorialPart() );
2571 M.SetRows( gp_XYZ( vec( 1 ), vec( 2 ), vec( 3 )),
2572 gp_XYZ( vec( 4 ), vec( 5 ), vec( 6 )),
2573 gp_XYZ( vec( 7 ), vec( 8 ), vec( 9 )));
2577 //================================================================================
2579 * \brief Transforms a 3D point using a found transformation
2581 //================================================================================
2583 gp_XYZ TrsfFinder3D::Transform( const gp_Pnt& srcP ) const
2585 gp_XYZ p = srcP.XYZ() - _srcOrig;
2586 _trsf.Transforms( p );
2590 //================================================================================
2592 * \brief Transforms a 3D vector using a found transformation
2594 //================================================================================
2596 gp_XYZ TrsfFinder3D::TransformVec( const gp_Vec& v ) const
2598 return v.XYZ().Multiplied( _trsf.HVectorialPart() );
2600 //================================================================================
2604 //================================================================================
2606 bool TrsfFinder3D::Invert()
2608 if (( _trsf.Form() == gp_Translation ) &&
2609 ( _srcOrig.X() != 0 || _srcOrig.Y() != 0 || _srcOrig.Z() != 0 ))
2611 // seems to be defined via Solve()
2612 gp_XYZ newSrcOrig = _trsf.TranslationPart();
2613 gp_Mat& M = const_cast< gp_Mat& >( _trsf.HVectorialPart() );
2614 const double D = M.Determinant();
2615 if ( D < 1e-3 * ( newSrcOrig - _srcOrig ).Modulus() )
2618 cerr << "TrsfFinder3D::Invert()"
2619 << "D " << M.Determinant() << " IsSingular " << M.IsSingular() << endl;
2623 gp_Mat Minv = M.Inverted();
2624 _trsf.SetTranslation( _srcOrig );
2625 _srcOrig = newSrcOrig;