1 // Copyright (C) 2007-2016 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 "SMDS_EdgePosition.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMESHDS_Mesh.hxx"
33 #include "SMESH_Algo.hxx"
34 #include "SMESH_Block.hxx"
35 #include "SMESH_Gen.hxx"
36 #include "SMESH_HypoFilter.hxx"
37 #include "SMESH_Hypothesis.hxx"
38 #include "SMESH_Mesh.hxx"
39 #include "SMESH_MeshAlgos.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_subMesh.hxx"
42 #include "SMESH_subMeshEventListener.hxx"
43 #include "StdMeshers_ProjectionSource1D.hxx"
44 #include "StdMeshers_ProjectionSource2D.hxx"
45 #include "StdMeshers_ProjectionSource3D.hxx"
47 #include "utilities.h"
49 #include <BRepAdaptor_Surface.hxx>
50 #include <BRepMesh_Delaun.hxx>
51 #include <BRepTools.hxx>
52 #include <BRepTools_WireExplorer.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRep_Tool.hxx>
55 #include <Bnd_Box.hxx>
56 #include <Geom2d_Curve.hxx>
57 #include <Geom_Curve.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <TopTools_Array1OfShape.hxx>
62 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
63 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
64 #include <TopTools_IndexedMapOfShape.hxx>
65 #include <TopTools_ListIteratorOfListOfShape.hxx>
66 #include <TopTools_ListOfShape.hxx>
67 #include <TopTools_MapOfShape.hxx>
69 #include <TopoDS_Compound.hxx>
70 #include <TopoDS_Shape.hxx>
73 #include <math_Gauss.hxx>
81 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
82 #define CONT_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); continue; }
83 #define SHOW_SHAPE(v,msg) \
84 // { show_shape((v),(msg)); }
85 #define SHOW_LIST(msg,l) \
86 // { show_list((msg),(l)); }
88 namespace HERE = StdMeshers_ProjectionUtils;
92 static SMESHDS_Mesh* theMeshDS[2] = { 0, 0 }; // used for debug only
93 long shapeIndex(const TopoDS_Shape& S)
95 if ( theMeshDS[0] && theMeshDS[1] )
96 return max(theMeshDS[0]->ShapeToIndex(S), theMeshDS[1]->ShapeToIndex(S) );
97 return long(S.TShape().operator->());
99 void show_shape( TopoDS_Shape v, const char* msg ) // debug
101 if ( v.IsNull() ) cout << msg << " NULL SHAPE" << endl;
102 else if (v.ShapeType() == TopAbs_VERTEX) {
103 gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( v ));
104 cout<<msg<<" "<<shapeIndex((v))<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;}
106 cout << msg << " "; TopAbs::Print((v).ShapeType(),cout) <<" "<<shapeIndex((v))<<endl;}
108 void show_list( const char* msg, const list< TopoDS_Edge >& l ) // debug
111 list< TopoDS_Edge >::const_iterator e = l.begin();
112 for ( int i = 0; e != l.end(); ++e, ++i ) {
113 cout << i << "V (" << TopExp::FirstVertex( *e, true ).TShape().operator->() << ") "
114 << i << "E (" << e->TShape().operator->() << "); "; }
117 //================================================================================
119 * \brief Write shape for debug purposes
121 //================================================================================
123 bool storeShapeForDebug(const TopoDS_Shape& shape)
126 const char* type[] ={"COMPOUND","COMPSOLID","SOLID","SHELL","FACE","WIRE","EDGE","VERTEX"};
127 BRepTools::Write( shape, SMESH_Comment("/tmp/") << type[shape.ShapeType()] << "_"
128 << shape.TShape().operator->() << ".brep");
129 if ( !theMeshDS[0] ) {
130 show_shape( TopoDS_Shape(), "avoid warning: show_shape() defined but not used");
131 show_list( "avoid warning: show_list() defined but not used", list< TopoDS_Edge >() );
137 //================================================================================
139 * \brief Reverse order of edges in a list and their orientation
140 * \param edges - list of edges to reverse
141 * \param nbEdges - number of edges to reverse
143 //================================================================================
145 void reverseEdges( list< TopoDS_Edge > & edges, const int nbEdges, const int firstEdge=0)
147 SHOW_LIST("BEFORE REVERSE", edges);
149 list< TopoDS_Edge >::iterator eIt = edges.begin();
150 std::advance( eIt, firstEdge );
151 list< TopoDS_Edge >::iterator eBackIt = eIt;
152 for ( int i = 0; i < nbEdges; ++i, ++eBackIt )
153 eBackIt->Reverse(); // reverse edge
156 while ( eIt != eBackIt )
158 std::swap( *eIt, *eBackIt );
159 SHOW_LIST("# AFTER SWAP", edges)
160 if ( (++eIt) != eBackIt )
163 SHOW_LIST("ATFER REVERSE", edges)
166 //================================================================================
168 * \brief Check if propagation is possible
169 * \param theMesh1 - source mesh
170 * \param theMesh2 - target mesh
171 * \retval bool - true if possible
173 //================================================================================
175 bool isPropagationPossible( SMESH_Mesh* theMesh1, SMESH_Mesh* theMesh2 )
177 if ( theMesh1 != theMesh2 ) {
178 TopoDS_Shape mainShape1 = theMesh1->GetMeshDS()->ShapeToMesh();
179 TopoDS_Shape mainShape2 = theMesh2->GetMeshDS()->ShapeToMesh();
180 return mainShape1.IsSame( mainShape2 );
185 //================================================================================
187 * \brief Fix up association of edges in faces by possible propagation
188 * \param nbEdges - nb of edges in an outer wire
189 * \param edges1 - edges of one face
190 * \param edges2 - matching edges of another face
191 * \param theMesh1 - mesh 1
192 * \param theMesh2 - mesh 2
193 * \retval bool - true if association was fixed
195 //================================================================================
197 bool fixAssocByPropagation( const int nbEdges,
198 list< TopoDS_Edge > & edges1,
199 list< TopoDS_Edge > & edges2,
200 SMESH_Mesh* theMesh1,
201 SMESH_Mesh* theMesh2)
203 if ( nbEdges == 2 && isPropagationPossible( theMesh1, theMesh2 ) )
205 list< TopoDS_Edge >::iterator eIt2 = ++edges2.begin(); // 2nd edge of the 2nd face
206 TopoDS_Edge edge2 = HERE::GetPropagationEdge( theMesh1, *eIt2, edges1.front() ).second;
207 if ( !edge2.IsNull() ) { // propagation found for the second edge
208 reverseEdges( edges2, nbEdges );
215 //================================================================================
217 * \brief Associate faces having one edge in the outer wire.
218 * No check is done if there is really only one outer edge
220 //================================================================================
222 bool assocFewEdgesFaces( const TopoDS_Face& face1,
224 const TopoDS_Face& face2,
226 HERE::TShapeShapeMap & theMap)
228 TopoDS_Vertex v1 = TopoDS::Vertex( HERE::OuterShape( face1, TopAbs_VERTEX ));
229 TopoDS_Vertex v2 = TopoDS::Vertex( HERE::OuterShape( face2, TopAbs_VERTEX ));
230 TopoDS_Vertex VV1[2] = { v1, v1 };
231 TopoDS_Vertex VV2[2] = { v2, v2 };
232 list< TopoDS_Edge > edges1, edges2;
233 if ( int nbE = HERE::FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 ))
235 HERE::InsertAssociation( face1, face2, theMap );
236 fixAssocByPropagation( nbE, edges1, edges2, mesh1, mesh2 );
237 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
238 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
239 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
241 HERE::InsertAssociation( *eIt1, *eIt2, theMap );
242 v1 = SMESH_MesherHelper::IthVertex( 0, *eIt1 );
243 v2 = SMESH_MesherHelper::IthVertex( 0, *eIt2 );
244 HERE::InsertAssociation( v1, v2, theMap );
246 theMap.SetAssocType( HERE::TShapeShapeMap::FEW_EF );
252 //================================================================================
254 * \brief Look for a group containing a target shape and similar to a source group
255 * \param tgtShape - target edge or face
256 * \param tgtMesh1 - target mesh
257 * \param srcGroup - source group
258 * \retval TopoDS_Shape - found target group
260 //================================================================================
262 TopoDS_Shape findGroupContaining(const TopoDS_Shape& tgtShape,
263 const SMESH_Mesh* tgtMesh1,
264 const TopoDS_Shape& srcGroup)
266 list<SMESH_subMesh*> subMeshes = tgtMesh1->GetGroupSubMeshesContaining(tgtShape);
267 list<SMESH_subMesh*>::iterator sm = subMeshes.begin();
268 int type, last = TopAbs_SHAPE;
269 for ( ; sm != subMeshes.end(); ++sm ) {
270 const TopoDS_Shape & group = (*sm)->GetSubShape();
271 // check if group is similar to srcGroup
272 for ( type = srcGroup.ShapeType(); type < last; ++type)
273 if ( SMESH_MesherHelper::Count( srcGroup, (TopAbs_ShapeEnum)type, 0) !=
274 SMESH_MesherHelper::Count( group, (TopAbs_ShapeEnum)type, 0))
279 return TopoDS_Shape();
282 //================================================================================
284 * \brief Find association of groups at top and bottom of prism
286 //================================================================================
288 bool assocGroupsByPropagation(const TopoDS_Shape& theGroup1,
289 const TopoDS_Shape& theGroup2,
291 HERE::TShapeShapeMap& theMap)
293 // If groups are on top and bottom of prism then we can associate
294 // them using "vertical" (or "side") edges and faces of prism since
295 // they connect corresponding vertices and edges of groups.
297 TopTools_IndexedMapOfShape subshapes1, subshapes2;
298 TopExp::MapShapes( theGroup1, subshapes1 );
299 TopExp::MapShapes( theGroup2, subshapes2 );
300 TopTools_ListIteratorOfListOfShape ancestIt;
302 // Iterate on vertices of group1 to find corresponding vertices in group2
303 // and associate adjacent edges and faces
305 TopTools_MapOfShape verticShapes;
306 TopExp_Explorer vExp1( theGroup1, TopAbs_VERTEX );
307 for ( ; vExp1.More(); vExp1.Next() )
309 const TopoDS_Vertex& v1 = TopoDS::Vertex( vExp1.Current() );
310 if ( theMap.IsBound( v1 )) continue; // already processed
312 // Find "vertical" edge ending in v1 and whose other vertex belongs to group2
313 TopoDS_Shape verticEdge, v2;
314 ancestIt.Initialize( theMesh.GetAncestors( v1 ));
315 for ( ; verticEdge.IsNull() && ancestIt.More(); ancestIt.Next() )
317 if ( ancestIt.Value().ShapeType() != TopAbs_EDGE ) continue;
318 v2 = HERE::GetNextVertex( TopoDS::Edge( ancestIt.Value() ), v1 );
319 if ( subshapes2.Contains( v2 ))
320 verticEdge = ancestIt.Value();
322 if ( verticEdge.IsNull() )
325 HERE::InsertAssociation( v1, v2, theMap);
327 // Associate edges by vertical faces sharing the found vertical edge
328 ancestIt.Initialize( theMesh.GetAncestors( verticEdge ) );
329 for ( ; ancestIt.More(); ancestIt.Next() )
331 if ( ancestIt.Value().ShapeType() != TopAbs_FACE ) continue;
332 if ( !verticShapes.Add( ancestIt.Value() )) continue;
333 const TopoDS_Face& face = TopoDS::Face( ancestIt.Value() );
335 // get edges of the face
336 TopoDS_Edge edgeGr1, edgeGr2, verticEdge2;
337 list< TopoDS_Edge > edges; list< int > nbEdgesInWire;
338 SMESH_Block::GetOrderedEdges( face, edges, nbEdgesInWire, v1);
339 if ( nbEdgesInWire.front() != 4 )
340 return storeShapeForDebug( face );
341 list< TopoDS_Edge >::iterator edge = edges.begin();
342 if ( verticEdge.IsSame( *edge )) {
344 verticEdge2 = *(++edge);
348 verticEdge2 = *(edge++);
352 HERE::InsertAssociation( edgeGr1, edgeGr2.Reversed(), theMap);
357 TopoDS_Iterator gr1It( theGroup1 );
358 if ( gr1It.Value().ShapeType() == TopAbs_FACE )
360 // find a boundary edge of group1 to start from
361 TopoDS_Shape bndEdge = HERE::GetBoundaryEdge( theGroup1, theMesh );
362 if ( bndEdge.IsNull() )
365 list< TopoDS_Shape > edges(1, bndEdge);
366 list< TopoDS_Shape >::iterator edge1 = edges.begin();
367 for ( ; edge1 != edges.end(); ++edge1 )
369 // there must be one or zero not associated faces between ancestors of edge
370 // belonging to theGroup1
372 ancestIt.Initialize( theMesh.GetAncestors( *edge1 ) );
373 for ( ; ancestIt.More() && face1.IsNull(); ancestIt.Next() ) {
374 if ( ancestIt.Value().ShapeType() == TopAbs_FACE &&
375 !theMap.IsBound( ancestIt.Value() ) &&
376 subshapes1.Contains( ancestIt.Value() ))
377 face1 = ancestIt.Value();
379 // add edges of face1 to start searching for adjacent faces from
380 for ( TopExp_Explorer e(face1, TopAbs_EDGE); e.More(); e.Next())
381 if ( !edge1->IsSame( e.Current() ))
382 edges.push_back( e.Current() );
384 if ( !face1.IsNull() ) {
385 // find the corresponding face of theGroup2
386 TopoDS_Shape edge2 = theMap( *edge1 );
388 ancestIt.Initialize( theMesh.GetAncestors( edge2 ) );
389 for ( ; ancestIt.More() && face2.IsNull(); ancestIt.Next() ) {
390 if ( ancestIt.Value().ShapeType() == TopAbs_FACE &&
391 !theMap.IsBound( ancestIt.Value(), /*is2nd=*/true ) &&
392 subshapes2.Contains( ancestIt.Value() ))
393 face2 = ancestIt.Value();
395 if ( face2.IsNull() )
398 HERE::InsertAssociation( face1, face2, theMap);
402 theMap.SetAssocType( HERE::TShapeShapeMap::PROPAGATION );
406 //================================================================================
408 * \brief Return true if uv position of the vIndex-th vertex of edge on face is close
411 //================================================================================
413 bool sameVertexUV( const TopoDS_Edge& edge,
414 const TopoDS_Face& face,
417 const double& tol2d )
419 TopoDS_Vertex V = SMESH_MesherHelper::IthVertex( vIndex, edge, /*CumOri=*/true );
420 gp_Pnt2d v1UV = BRep_Tool::Parameters( V, face);
421 double dist2d = v1UV.Distance( uv );
422 return dist2d < tol2d;
425 //================================================================================
427 * \brief Returns an EDGE suitable for search of initial vertex association
429 //================================================================================
431 bool getOuterEdges( const TopoDS_Shape shape,
433 std::list< TopoDS_Edge >& allBndEdges )
435 if ( shape.ShapeType() == TopAbs_COMPOUND )
437 TopoDS_Iterator it( shape );
438 if ( it.More() && it.Value().ShapeType() == TopAbs_FACE ) // group of FACEs
440 // look for a boundary EDGE of a group
441 StdMeshers_ProjectionUtils::GetBoundaryEdge( shape, mesh, &allBndEdges );
442 if ( !allBndEdges.empty() )
446 SMESH_MesherHelper helper( mesh );
447 helper.SetSubShape( shape );
449 TopExp_Explorer expF( shape, TopAbs_FACE ), expE;
451 for ( ; expF.More(); expF.Next() ) {
453 StdMeshers_ProjectionUtils::OuterShape( TopoDS::Face( expF.Current() ), TopAbs_WIRE );
454 for ( expE.Init( wire, TopAbs_EDGE ); expE.More(); expE.Next() )
455 if ( ! helper.IsClosedEdge( TopoDS::Edge( expE.Current() )))
457 if ( helper.IsSeamShape( expE.Current() ))
458 allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
460 allBndEdges.push_front( TopoDS::Edge( expE.Current() ));
464 else if ( shape.ShapeType() != TopAbs_EDGE) { // no faces
465 for ( expE.Init( shape, TopAbs_EDGE ); expE.More(); expE.Next() )
466 if ( ! helper.IsClosedEdge( TopoDS::Edge( expE.Current() )))
468 if ( helper.IsSeamShape( expE.Current() ))
469 allBndEdges.push_back( TopoDS::Edge( expE.Current() ));
471 allBndEdges.push_front( TopoDS::Edge( expE.Current() ));
474 else if ( shape.ShapeType() == TopAbs_EDGE ) {
475 if ( ! helper.IsClosedEdge( TopoDS::Edge( shape )))
476 allBndEdges.push_back( TopoDS::Edge( shape ));
478 return !allBndEdges.empty();
483 //=======================================================================
485 * Looks for association of all sub-shapes of two shapes
486 * \param theShape1 - target shape
487 * \param theMesh1 - mesh built on shape 1
488 * \param theShape2 - source shape
489 * \param theMesh2 - mesh built on shape 2
490 * \param theAssociation - association map to be filled that may
491 * contain association of one or two pairs of vertices
492 * \retval bool - true if association found
494 //=======================================================================
496 bool StdMeshers_ProjectionUtils::FindSubShapeAssociation(const TopoDS_Shape& theShape1,
497 SMESH_Mesh* theMesh1,
498 const TopoDS_Shape& theShape2,
499 SMESH_Mesh* theMesh2,
500 TShapeShapeMap & theMap)
502 // Structure of this long function is following
503 // 1) Group -> Group projection: theShape1 is a group member,
504 // theShape2 is another group. We find the group theShape1 is in and recall self.
505 // 2) Accosiate same shapes with different location (partners).
506 // 3) If vertex association is given, perform association according to shape type:
507 // switch ( ShapeType ) {
511 // 4) else try to accosiate in different ways:
512 // a) accosiate shapes by propagation and other simple cases
513 // switch ( ShapeType ) {
517 // b) find association of a couple of vertices and recall self.
520 theMeshDS[0] = theMesh1->GetMeshDS(); // debug
521 theMeshDS[1] = theMesh2->GetMeshDS();
523 // =================================================================================
524 // 1) Is it the case of associating a group member -> another group? (PAL16202, 16203)
525 // =================================================================================
526 if ( theShape1.ShapeType() != theShape2.ShapeType() )
528 TopoDS_Shape group1, group2;
529 if ( theShape1.ShapeType() == TopAbs_COMPOUND ) {
531 group2 = findGroupContaining( theShape2, theMesh2, group1 );
533 else if ( theShape2.ShapeType() == TopAbs_COMPOUND ) {
535 group1 = findGroupContaining( theShape1, theMesh1, group2 );
537 if ( group1.IsNull() || group2.IsNull() )
538 RETURN_BAD_RESULT("Different shape types");
539 // Associate compounds
540 return FindSubShapeAssociation(group1, theMesh1, group2, theMesh2, theMap );
546 bool partner = theShape1.IsPartner( theShape2 );
547 TopTools_DataMapIteratorOfDataMapOfShapeShape vvIt( theMap._map1to2 );
548 for ( ; partner && vvIt.More(); vvIt.Next() )
549 partner = vvIt.Key().IsPartner( vvIt.Value() );
551 if ( partner ) // Same shape with different location
553 // recursively associate all sub-shapes of theShape1 and theShape2
554 typedef list< pair< TopoDS_Shape, TopoDS_Shape > > TShapePairsList;
555 TShapePairsList shapesQueue( 1, make_pair( theShape1, theShape2 ));
556 TShapePairsList::iterator s1_s2 = shapesQueue.begin();
557 for ( ; s1_s2 != shapesQueue.end(); ++s1_s2 )
559 if ( theMap.IsBound( s1_s2->first )) // avoid re-binding for a seam edge
560 continue; // to avoid this: Forward seam -> Reversed seam
561 InsertAssociation( s1_s2->first, s1_s2->second, theMap );
562 TopoDS_Iterator s1It( s1_s2->first), s2It( s1_s2->second );
563 for ( ; s1It.More(); s1It.Next(), s2It.Next() )
564 shapesQueue.push_back( make_pair( s1It.Value(), s2It.Value() ));
566 theMap.SetAssocType( TShapeShapeMap::PARTNER );
570 if ( !theMap.IsEmpty() )
572 //======================================================================
573 // 3) HAS initial vertex association
574 //======================================================================
575 bool isVCloseness = ( theMap._assocType == TShapeShapeMap::CLOSE_VERTEX );
576 theMap.SetAssocType( TShapeShapeMap::INIT_VERTEX );
577 switch ( theShape1.ShapeType() ) {
578 // ----------------------------------------------------------------------
579 case TopAbs_EDGE: { // TopAbs_EDGE
580 // ----------------------------------------------------------------------
581 if ( theMap.Extent() != 1 )
582 RETURN_BAD_RESULT("Wrong map extent " << theMap.Extent() );
583 TopoDS_Edge edge1 = TopoDS::Edge( theShape1 );
584 TopoDS_Edge edge2 = TopoDS::Edge( theShape2 );
585 if ( edge1.Orientation() >= TopAbs_INTERNAL ) edge1.Orientation( TopAbs_FORWARD );
586 if ( edge2.Orientation() >= TopAbs_INTERNAL ) edge2.Orientation( TopAbs_FORWARD );
587 TopoDS_Vertex VV1[2], VV2[2];
588 TopExp::Vertices( edge1, VV1[0], VV1[1] );
589 TopExp::Vertices( edge2, VV2[0], VV2[1] );
591 if ( theMap.IsBound( VV1[ i1 ] )) i1 = 1;
592 if ( theMap.IsBound( VV2[ i2 ] )) i2 = 1;
593 InsertAssociation( VV1[ i1 ], VV2[ i2 ], theMap );
594 InsertAssociation( theShape1, theShape2, theMap );
597 // ----------------------------------------------------------------------
598 case TopAbs_FACE: { // TopAbs_FACE
599 // ----------------------------------------------------------------------
600 TopoDS_Face face1 = TopoDS::Face( theShape1 );
601 TopoDS_Face face2 = TopoDS::Face( theShape2 );
602 if ( face1.Orientation() >= TopAbs_INTERNAL ) face1.Orientation( TopAbs_FORWARD );
603 if ( face2.Orientation() >= TopAbs_INTERNAL ) face2.Orientation( TopAbs_FORWARD );
605 TopoDS_Vertex VV1[2], VV2[2];
606 // find a not closed edge of face1 both vertices of which are associated
608 TopExp_Explorer exp ( face1, TopAbs_EDGE );
609 for ( ; VV2[ 1 ].IsNull() && exp.More(); exp.Next(), ++nbEdges ) {
610 TopExp::Vertices( TopoDS::Edge( exp.Current() ), VV1[0], VV1[1] );
611 if ( theMap.IsBound( VV1[0] ) ) {
612 VV2[ 0 ] = TopoDS::Vertex( theMap( VV1[0] ));
613 if ( theMap.IsBound( VV1[1] ) && !VV1[0].IsSame( VV1[1] ))
614 VV2[ 1 ] = TopoDS::Vertex( theMap( VV1[1] ));
617 if ( VV2[ 1 ].IsNull() ) { // 2 bound vertices not found
619 RETURN_BAD_RESULT("2 bound vertices not found" );
624 list< TopoDS_Edge > edges1, edges2;
625 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2, isVCloseness );
626 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
627 fixAssocByPropagation( nbE, edges1, edges2, theMesh1, theMesh2 );
629 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
630 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
631 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
633 InsertAssociation( *eIt1, *eIt2, theMap );
634 VV1[0] = TopExp::FirstVertex( *eIt1, true );
635 VV2[0] = TopExp::FirstVertex( *eIt2, true );
636 InsertAssociation( VV1[0], VV2[0], theMap );
638 InsertAssociation( theShape1, theShape2, theMap );
641 // ----------------------------------------------------------------------
642 case TopAbs_SHELL: // TopAbs_SHELL, TopAbs_SOLID
644 // ----------------------------------------------------------------------
645 TopoDS_Vertex VV1[2], VV2[2];
646 // try to find a not closed edge of shape1 both vertices of which are associated
648 TopExp_Explorer exp ( theShape1, TopAbs_EDGE );
649 for ( ; VV2[ 1 ].IsNull() && exp.More(); exp.Next() ) {
650 edge1 = TopoDS::Edge( exp.Current() );
651 if ( edge1.Orientation() >= TopAbs_INTERNAL ) edge1.Orientation( TopAbs_FORWARD );
652 TopExp::Vertices( edge1 , VV1[0], VV1[1] );
653 if ( theMap.IsBound( VV1[0] )) {
654 VV2[ 0 ] = TopoDS::Vertex( theMap( VV1[0] ));
655 if ( theMap.IsBound( VV1[1] ) && !VV1[0].IsSame( VV1[1] ))
656 VV2[ 1 ] = TopoDS::Vertex( theMap( VV1[1] ));
659 if ( VV2[ 1 ].IsNull() ) // 2 bound vertices not found
660 RETURN_BAD_RESULT("2 bound vertices not found" );
661 // get an edge2 of theShape2 corresponding to edge1
662 TopoDS_Edge edge2 = GetEdgeByVertices( theMesh2, VV2[ 0 ], VV2[ 1 ]);
663 if ( edge2.IsNull() )
664 RETURN_BAD_RESULT("GetEdgeByVertices() failed");
666 // build map of edge to faces if shapes are not sub-shapes of main ones
667 bool isSubOfMain = false;
668 if ( SMESHDS_SubMesh * sm = theMesh1->GetMeshDS()->MeshElements( theShape1 ))
669 isSubOfMain = !sm->IsComplexSubmesh();
671 isSubOfMain = theMesh1->GetMeshDS()->ShapeToIndex( theShape1 );
672 TAncestorMap e2f1, e2f2;
673 const TAncestorMap& edgeToFace1 = isSubOfMain ? theMesh1->GetAncestorMap() : e2f1;
674 const TAncestorMap& edgeToFace2 = isSubOfMain ? theMesh2->GetAncestorMap() : e2f2;
676 TopExp::MapShapesAndAncestors( theShape1, TopAbs_EDGE, TopAbs_FACE, e2f1 );
677 TopExp::MapShapesAndAncestors( theShape2, TopAbs_EDGE, TopAbs_FACE, e2f2 );
678 if ( !edgeToFace1.Contains( edge1 ))
679 RETURN_BAD_RESULT("edge1 does not belong to theShape1");
680 if ( !edgeToFace2.Contains( edge2 ))
681 RETURN_BAD_RESULT("edge2 does not belong to theShape2");
684 // Look for 2 corresponing faces:
688 // get a face sharing edge1 (F1)
689 TopTools_ListIteratorOfListOfShape ancestIt1( edgeToFace1.FindFromKey( edge1 ));
690 for ( ; F1.IsNull() && ancestIt1.More(); ancestIt1.Next() )
691 if ( ancestIt1.Value().ShapeType() == TopAbs_FACE )
692 F1 = ancestIt1.Value().Oriented //( TopAbs_FORWARD );
693 ( SMESH_MesherHelper::GetSubShapeOri( theShape1, ancestIt1.Value() ));
695 RETURN_BAD_RESULT(" Face1 not found");
697 // get 2 faces sharing edge2 (one of them is F2)
699 TopTools_ListIteratorOfListOfShape ancestIt2( edgeToFace2.FindFromKey( edge2 ));
700 for ( int i = 0; FF2[1].IsNull() && ancestIt2.More(); ancestIt2.Next() )
701 if ( ancestIt2.Value().ShapeType() == TopAbs_FACE )
702 FF2[ i++ ] = ancestIt2.Value().Oriented // ( TopAbs_FORWARD );
703 ( SMESH_MesherHelper::GetSubShapeOri( theShape2, ancestIt2.Value() ));
705 // get oriented edge1 and edge2 from F1 and FF2[0]
706 for ( exp.Init( F1, TopAbs_EDGE ); exp.More(); exp.Next() )
707 if ( edge1.IsSame( exp.Current() )) {
708 edge1 = TopoDS::Edge( exp.Current() );
711 for ( exp.Init( FF2[ 0 ], TopAbs_EDGE ); exp.More(); exp.Next() )
712 if ( edge2.IsSame( exp.Current() )) {
713 edge2 = TopoDS::Edge( exp.Current() );
717 // compare first vertices of edge1 and edge2
718 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
719 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
720 F2 = FF2[ 0 ]; // (F2 !)
721 if ( !VV1[ 0 ].IsSame( theMap( VV2[ 0 ], /*is2=*/true))) {
723 if ( FF2[ 1 ].IsNull() )
729 // association of face sub-shapes and neighbour faces
730 list< pair < TopoDS_Face, TopoDS_Edge > > FE1, FE2;
731 list< pair < TopoDS_Face, TopoDS_Edge > >::iterator fe1, fe2;
732 FE1.push_back( make_pair( TopoDS::Face( F1 ), edge1 ));
733 FE2.push_back( make_pair( TopoDS::Face( F2 ), edge2 ));
734 for ( fe1 = FE1.begin(), fe2 = FE2.begin(); fe1 != FE1.end(); ++fe1, ++fe2 )
736 const TopoDS_Face& face1 = fe1->first;
737 if ( theMap.IsBound( face1 ) ) continue;
738 const TopoDS_Face& face2 = fe2->first;
741 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
742 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
743 list< TopoDS_Edge > edges1, edges2;
744 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2, isVCloseness );
745 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
746 InsertAssociation( face1, face2, theMap ); // assoc faces
747 // MESSAGE("Assoc FACE " << theMesh1->GetMeshDS()->ShapeToIndex( face1 )<<
748 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( face2 ));
749 if ( nbE == 2 && (edge1.IsSame( edges1.front())) != (edge2.IsSame( edges2.front())))
751 reverseEdges( edges2, nbE );
753 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
754 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
755 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
757 if ( !InsertAssociation( *eIt1, *eIt2, theMap )) // assoc edges
758 continue; // already associated
759 VV1[0] = TopExp::FirstVertex( *eIt1, true );
760 VV2[0] = TopExp::FirstVertex( *eIt2, true );
761 InsertAssociation( VV1[0], VV2[0], theMap ); // assoc vertices
763 // add adjacent faces to process
764 TopoDS_Face nextFace1 = GetNextFace( edgeToFace1, *eIt1, face1 );
765 TopoDS_Face nextFace2 = GetNextFace( edgeToFace2, *eIt2, face2 );
766 if ( !nextFace1.IsNull() && !nextFace2.IsNull() ) {
767 if ( SMESH_MesherHelper::GetSubShapeOri( nextFace1, *eIt1 ) == eIt1->Orientation() )
769 if ( SMESH_MesherHelper::GetSubShapeOri( nextFace2, *eIt2 ) == eIt2->Orientation() )
771 FE1.push_back( make_pair( nextFace1, *eIt1 ));
772 FE2.push_back( make_pair( nextFace2, *eIt2 ));
776 InsertAssociation( theShape1, theShape2, theMap );
779 // ----------------------------------------------------------------------
780 case TopAbs_COMPOUND: { // GROUP
781 // ----------------------------------------------------------------------
782 // Maybe groups contain only one member
783 TopoDS_Iterator it1( theShape1 ), it2( theShape2 );
784 TopAbs_ShapeEnum memberType = it1.Value().ShapeType();
785 int nbMembers = SMESH_MesherHelper::Count( theShape1, memberType, true );
786 if ( nbMembers == 0 ) return true;
787 if ( nbMembers == 1 ) {
788 return FindSubShapeAssociation( it1.Value(), theMesh1, it2.Value(), theMesh2, theMap );
790 // Try to make shells of faces
792 BRep_Builder builder;
793 TopoDS_Shell shell1, shell2;
794 builder.MakeShell(shell1); builder.MakeShell(shell2);
795 if ( memberType == TopAbs_FACE ) {
796 // just add faces of groups to shells
797 for (; it1.More(); it1.Next(), it2.Next() )
798 builder.Add( shell1, it1.Value() ), builder.Add( shell2, it2.Value() );
800 else if ( memberType == TopAbs_EDGE ) {
801 // Try to add faces sharing more than one edge of a group or
802 // sharing all its vertices with the group
803 TopTools_IndexedMapOfShape groupVertices[2];
804 TopExp::MapShapes( theShape1, TopAbs_VERTEX, groupVertices[0]);
805 TopExp::MapShapes( theShape2, TopAbs_VERTEX, groupVertices[1]);
807 TopTools_MapOfShape groupEdges[2], addedFaces[2];
808 bool hasInitAssoc = (!theMap.IsEmpty()), initAssocOK = !hasInitAssoc;
809 for (; it1.More(); it1.Next(), it2.Next() ) {
810 groupEdges[0].Add( it1.Value() );
811 groupEdges[1].Add( it2.Value() );
812 if ( !initAssocOK ) {
813 // for shell association there must be an edge with both vertices bound
814 TopoDS_Vertex v1, v2;
815 TopExp::Vertices( TopoDS::Edge( it1.Value().Oriented(TopAbs_FORWARD)), v1, v2 );
816 initAssocOK = ( theMap.IsBound( v1 ) && theMap.IsBound( v2 ));
819 for (int is2ndGroup = 0; initAssocOK && is2ndGroup < 2; ++is2ndGroup) {
820 const TopoDS_Shape& group = is2ndGroup ? theShape2: theShape1;
821 SMESH_Mesh* mesh = is2ndGroup ? theMesh2 : theMesh1;
822 TopoDS_Shell& shell = is2ndGroup ? shell2 : shell1;
823 for ( TopoDS_Iterator it( group ); it.More(); it.Next() ) {
824 const TopoDS_Edge& edge = TopoDS::Edge( it.Value() );
826 for ( int iF = 0; iF < 2; ++iF ) { // loop on 2 faces sharing edge
827 face = GetNextFace(mesh->GetAncestorMap(), edge, face);
828 if ( !face.IsNull() ) {
829 int nbGroupEdges = 0;
830 for ( TopExp_Explorer f( face, TopAbs_EDGE ); f.More(); f.Next())
831 if ( groupEdges[ is2ndGroup ].Contains( f.Current() ))
832 if ( ++nbGroupEdges > 1 )
834 bool add = (nbGroupEdges > 1 ||
835 SMESH_MesherHelper::Count( face, TopAbs_EDGE, true ) == 1 );
838 for ( TopExp_Explorer v( face, TopAbs_VERTEX ); add && v.More(); v.Next())
839 add = groupVertices[ is2ndGroup ].Contains( v.Current() );
841 if ( add && addedFaces[ is2ndGroup ].Add( face ))
842 builder.Add( shell, face );
848 RETURN_BAD_RESULT("Unexpected group type");
852 int nbFaces1 = SMESH_MesherHelper::Count( shell1, TopAbs_FACE, 0 );
853 int nbFaces2 = SMESH_MesherHelper::Count( shell2, TopAbs_FACE, 0 );
854 if ( nbFaces1 != nbFaces2 )
855 RETURN_BAD_RESULT("Different nb of faces found for shells");
856 if ( nbFaces1 > 0 ) {
858 if ( nbFaces1 == 1 ) {
859 TopoDS_Shape F1 = TopoDS_Iterator( shell1 ).Value();
860 TopoDS_Shape F2 = TopoDS_Iterator( shell2 ).Value();
861 ok = FindSubShapeAssociation( F1, theMesh1, F2, theMesh2, theMap );
864 ok = FindSubShapeAssociation(shell1, theMesh1, shell2, theMesh2, theMap );
866 // Check if all members are mapped
868 TopTools_MapOfShape boundMembers[2];
870 for ( mIt.Initialize( theShape1 ); mIt.More(); mIt.Next())
871 if ( theMap.IsBound( mIt.Value() )) {
872 boundMembers[0].Add( mIt.Value() );
873 boundMembers[1].Add( theMap( mIt.Value() ));
875 if ( boundMembers[0].Extent() != nbMembers ) {
876 // make compounds of not bound members
877 TopoDS_Compound comp[2];
878 for ( int is2ndGroup = 0; is2ndGroup < 2; ++is2ndGroup ) {
879 builder.MakeCompound( comp[is2ndGroup] );
880 for ( mIt.Initialize( is2ndGroup ? theShape2:theShape1 ); mIt.More(); mIt.Next())
881 if ( ! boundMembers[ is2ndGroup ].Contains( mIt.Value() ))
882 builder.Add( comp[ is2ndGroup ], mIt.Value() );
884 // check if theMap contains initial association for the comp's
885 bool hasInitialAssoc = false;
886 if ( memberType == TopAbs_EDGE ) {
887 for ( TopExp_Explorer v( comp[0], TopAbs_VERTEX ); v.More(); v.Next())
888 if ( theMap.IsBound( v.Current() )) {
889 hasInitialAssoc = true;
893 if ( hasInitialAssoc == bool( !theMap.IsEmpty() ))
894 ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, theMap );
896 TShapeShapeMap tmpMap;
897 ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, tmpMap );
899 TopTools_DataMapIteratorOfDataMapOfShapeShape mapIt( tmpMap._map1to2 );
900 for ( ; mapIt.More(); mapIt.Next() )
901 theMap.Bind( mapIt.Key(), mapIt.Value());
908 // Each edge of an edge group is shared by own faces
909 // ------------------------------------------------------------------
911 // map vertices to edges sharing them, avoid doubling edges in lists
912 TopTools_DataMapOfShapeListOfShape v2e[2];
913 for (int isFirst = 0; isFirst < 2; ++isFirst ) {
914 const TopoDS_Shape& group = isFirst ? theShape1 : theShape2;
915 TopTools_DataMapOfShapeListOfShape& veMap = v2e[ isFirst ? 0 : 1 ];
916 TopTools_MapOfShape addedEdges;
917 for ( TopExp_Explorer e( group, TopAbs_EDGE ); e.More(); e.Next() ) {
918 const TopoDS_Shape& edge = e.Current();
919 if ( addedEdges.Add( edge )) {
920 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next()) {
921 const TopoDS_Shape& vertex = v.Current();
922 if ( !veMap.IsBound( vertex )) {
923 TopTools_ListOfShape l;
924 veMap.Bind( vertex, l );
926 veMap( vertex ).Append( edge );
931 while ( !v2e[0].IsEmpty() )
933 // find a bound vertex
935 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape v2eIt( v2e[0] );
936 for ( ; v2eIt.More(); v2eIt.Next())
937 if ( theMap.IsBound( v2eIt.Key() )) {
938 V[0] = TopoDS::Vertex( v2eIt.Key() );
939 V[1] = TopoDS::Vertex( theMap( V[0] ));
943 RETURN_BAD_RESULT("No more bound vertices");
945 while ( !V[0].IsNull() && v2e[0].IsBound( V[0] )) {
946 TopTools_ListOfShape& edges0 = v2e[0]( V[0] );
947 TopTools_ListOfShape& edges1 = v2e[1]( V[1] );
948 int nbE0 = edges0.Extent(), nbE1 = edges1.Extent();
950 RETURN_BAD_RESULT("Different nb of edges: "<< nbE0 << " != " << nbE1);
954 TopoDS_Edge e0 = TopoDS::Edge( edges0.First() );
955 TopoDS_Edge e1 = TopoDS::Edge( edges1.First() );
956 v2e[0].UnBind( V[0] );
957 v2e[1].UnBind( V[1] );
958 InsertAssociation( e0, e1, theMap );
959 // MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0 )<<
960 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1 ));
961 V[0] = GetNextVertex( e0, V[0] );
962 V[1] = GetNextVertex( e1, V[1] );
963 if ( !V[0].IsNull() ) {
964 InsertAssociation( V[0], V[1], theMap );
965 // MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( V[0] )<<
966 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( V[1] ));
969 else if ( nbE0 == 2 )
971 // one of edges must have both ends bound
972 TopoDS_Vertex v0e0 = GetNextVertex( TopoDS::Edge( edges0.First() ), V[0] );
973 TopoDS_Vertex v1e0 = GetNextVertex( TopoDS::Edge( edges0.Last() ), V[0] );
974 TopoDS_Vertex v0e1 = GetNextVertex( TopoDS::Edge( edges1.First() ), V[1] );
975 TopoDS_Vertex v1e1 = GetNextVertex( TopoDS::Edge( edges1.Last() ), V[1] );
976 TopoDS_Shape e0b, e1b, e0n, e1n, v1b; // bound and not-bound
977 TopoDS_Vertex v0n, v1n;
978 if ( theMap.IsBound( v0e0 )) {
979 v0n = v1e0; e0b = edges0.First(); e0n = edges0.Last(); v1b = theMap( v0e0 );
980 } else if ( theMap.IsBound( v1e0 )) {
981 v0n = v0e0; e0n = edges0.First(); e0b = edges0.Last(); v1b = theMap( v1e0 );
983 RETURN_BAD_RESULT("None of vertices bound");
985 if ( v1b.IsSame( v1e1 )) {
986 v1n = v0e1; e1n = edges1.First(); e1b = edges1.Last();
988 v1n = v1e1; e1b = edges1.First(); e1n = edges1.Last();
990 InsertAssociation( e0b, e1b, theMap );
991 InsertAssociation( e0n, e1n, theMap );
992 InsertAssociation( v0n, v1n, theMap );
993 // MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0b )<<
994 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1b ));
995 // MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0n )<<
996 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1n ));
997 // MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( v0n )<<
998 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( v1n ));
999 v2e[0].UnBind( V[0] );
1000 v2e[1].UnBind( V[1] );
1005 RETURN_BAD_RESULT("Not implemented");
1008 } //while ( !v2e[0].IsEmpty() )
1013 RETURN_BAD_RESULT("Unexpected shape type");
1015 } // end switch by shape type
1016 } // end case of available initial vertex association
1018 //======================================================================
1019 // 4) NO INITIAL VERTEX ASSOCIATION
1020 //======================================================================
1022 switch ( theShape1.ShapeType() ) {
1025 // ----------------------------------------------------------------------
1026 TopoDS_Edge edge1 = TopoDS::Edge( theShape1 );
1027 TopoDS_Edge edge2 = TopoDS::Edge( theShape2 );
1028 if ( isPropagationPossible( theMesh1, theMesh2 ))
1030 TopoDS_Edge prpEdge = GetPropagationEdge( theMesh1, edge2, edge1 ).second;
1031 if ( !prpEdge.IsNull() )
1033 TopoDS_Vertex VV1[2], VV2[2];
1034 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
1035 TopExp::Vertices( prpEdge, VV2[0], VV2[1], true );
1036 InsertAssociation( VV1[ 0 ], VV2[ 0 ], theMap );
1037 InsertAssociation( VV1[ 1 ], VV2[ 1 ], theMap );
1038 if ( VV1[0].IsSame( VV1[1] ) || // one of edges is closed
1039 VV2[0].IsSame( VV2[1] ) )
1041 InsertAssociation( edge1, prpEdge, theMap ); // insert with a proper orientation
1043 InsertAssociation( theShape1, theShape2, theMap );
1044 theMap.SetAssocType( TShapeShapeMap::PROPAGATION );
1045 return true; // done
1048 if ( SMESH_MesherHelper::IsClosedEdge( edge1 ) &&
1049 SMESH_MesherHelper::IsClosedEdge( edge2 ))
1051 // TODO: find out a proper orientation (is it possible?)
1052 InsertAssociation( edge1, edge2, theMap ); // insert with a proper orientation
1053 InsertAssociation( TopExp::FirstVertex(edge1), TopExp::FirstVertex(edge2),
1055 InsertAssociation( theShape1, theShape2, theMap );
1056 return true; // done
1058 break; // try by vertex closeness
1062 // ----------------------------------------------------------------------
1063 if ( isPropagationPossible( theMesh1, theMesh2 )) // try by propagation in one mesh
1065 TopoDS_Face face1 = TopoDS::Face(theShape1);
1066 TopoDS_Face face2 = TopoDS::Face(theShape2);
1067 if ( face1.Orientation() >= TopAbs_INTERNAL ) face1.Orientation( TopAbs_FORWARD );
1068 if ( face2.Orientation() >= TopAbs_INTERNAL ) face2.Orientation( TopAbs_FORWARD );
1069 TopoDS_Edge edge1, edge2;
1070 // get outer edge of theShape1
1071 TopoDS_Shape wire = OuterShape( face1, TopAbs_WIRE );
1072 //edge1 = TopoDS::Edge( OuterShape( face1, TopAbs_EDGE ));
1073 // use map to find the closest propagation edge
1074 map<int, pair< TopoDS_Edge, TopoDS_Edge > > propag_edges;
1075 for ( TopoDS_Iterator edgeIt( wire ); edgeIt.More(); edgeIt.Next() )
1077 edge1 = TopoDS::Edge( edgeIt.Value() );
1078 // find out if any edge of face2 is a propagation edge of outer edge1
1079 for ( TopExp_Explorer exp( face2, TopAbs_EDGE ); exp.More(); exp.Next() ) {
1080 edge2 = TopoDS::Edge( exp.Current() );
1081 pair<int,TopoDS_Edge> step_edge = GetPropagationEdge( theMesh1, edge2, edge1 );
1082 if ( !step_edge.second.IsNull() ) { // propagation found
1083 propag_edges.insert( make_pair( step_edge.first,
1084 ( make_pair( edge1, step_edge.second ))));
1085 if ( step_edge.first == 1 ) break; // most close found
1088 if ( !propag_edges.empty() && propag_edges.begin()->first == 1 ) break;
1090 if ( !propag_edges.empty() ) // propagation found
1092 edge1 = propag_edges.begin()->second.first;
1093 edge2 = propag_edges.begin()->second.second;
1094 TopoDS_Vertex VV1[2], VV2[2];
1095 TopExp::Vertices( edge1, VV1[0], VV1[1], true );
1096 TopExp::Vertices( edge2, VV2[0], VV2[1], true );
1097 list< TopoDS_Edge > edges1, edges2;
1098 int nbE = FindFaceAssociation( face1, VV1, face2, VV2, edges1, edges2 );
1099 if ( !nbE ) RETURN_BAD_RESULT("FindFaceAssociation() failed");
1100 // take care of proper association of propagated edges
1101 bool same1 = edge1.IsSame( edges1.front() );
1102 bool same2 = edge2.IsSame( edges2.front() );
1103 if ( same1 != same2 )
1105 reverseEdges(edges2, nbE);
1106 if ( nbE != 2 ) // 2 degen edges of 4 (issue 0021144)
1107 edges2.splice( edges2.end(), edges2, edges2.begin());
1109 // store association
1110 list< TopoDS_Edge >::iterator eIt1 = edges1.begin();
1111 list< TopoDS_Edge >::iterator eIt2 = edges2.begin();
1112 for ( ; eIt1 != edges1.end(); ++eIt1, ++eIt2 )
1114 InsertAssociation( *eIt1, *eIt2, theMap );
1115 VV1[0] = SMESH_MesherHelper::IthVertex( 0, *eIt1, true );
1116 VV2[0] = SMESH_MesherHelper::IthVertex( 0, *eIt2, true );
1117 InsertAssociation( VV1[0], VV2[0], theMap );
1119 InsertAssociation( theShape1, theShape2, theMap );
1120 theMap.SetAssocType( TShapeShapeMap::PROPAGATION );
1124 break; // try by vertex closeness
1126 case TopAbs_COMPOUND: {
1127 // ----------------------------------------------------------------------
1128 if ( isPropagationPossible( theMesh1, theMesh2 )) {
1130 // try to accosiate all using propagation
1131 if ( assocGroupsByPropagation( theShape1, theShape2, *theMesh1, theMap ))
1134 // find a boundary edge of theShape1
1135 TopoDS_Edge E = GetBoundaryEdge( theShape1, *theMesh1 );
1137 break; // try by vertex closeness
1139 // find association for vertices of edge E
1140 TopoDS_Vertex VV1[2], VV2[2];
1141 for(TopExp_Explorer eexp(E, TopAbs_VERTEX); eexp.More(); eexp.Next()) {
1142 TopoDS_Vertex V1 = TopoDS::Vertex( eexp.Current() );
1143 // look for an edge ending in E whose one vertex is in theShape1
1144 // and the other, in theShape2
1145 const TopTools_ListOfShape& Ancestors = theMesh1->GetAncestors(V1);
1146 TopTools_ListIteratorOfListOfShape ita(Ancestors);
1147 for(; ita.More(); ita.Next()) {
1148 if( ita.Value().ShapeType() != TopAbs_EDGE ) continue;
1149 TopoDS_Edge edge = TopoDS::Edge(ita.Value());
1150 bool FromShape1 = false;
1151 for(TopExp_Explorer expe(theShape1, TopAbs_EDGE); expe.More(); expe.Next() ) {
1152 if(edge.IsSame(expe.Current())) {
1158 // is it an edge between theShape1 and theShape2?
1159 TopExp_Explorer expv(edge, TopAbs_VERTEX);
1160 TopoDS_Vertex V2 = TopoDS::Vertex( expv.Current() );
1163 V2 = TopoDS::Vertex( expv.Current() );
1165 bool FromShape2 = false;
1166 for ( expv.Init( theShape2, TopAbs_VERTEX ); expv.More(); expv.Next()) {
1167 if ( V2.IsSame( expv.Current() )) {
1173 if ( VV1[0].IsNull() )
1174 VV1[0] = V1, VV2[0] = V2;
1176 VV1[1] = V1, VV2[1] = V2;
1177 break; // from loop on ancestors of V1
1182 if ( !VV1[1].IsNull() ) {
1183 InsertAssociation( VV1[0], VV2[0], theMap );
1184 InsertAssociation( VV1[1], VV2[1], theMap );
1185 TShapeShapeMap::EAssocType asType = theMap._assocType;
1186 theMap.SetAssocType( TShapeShapeMap::PROPAGATION );
1187 if ( FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap ))
1189 theMap._assocType = asType;
1192 break; // try by vertex closeness
1197 // 4.b) Find association by closeness of vertices
1198 // ----------------------------------------------
1200 TopTools_IndexedMapOfShape vMap1, vMap2;
1201 TopExp::MapShapes( theShape1, TopAbs_VERTEX, vMap1 );
1202 TopExp::MapShapes( theShape2, TopAbs_VERTEX, vMap2 );
1203 TopoDS_Vertex VV1[2], VV2[2];
1205 if ( vMap1.Extent() != vMap2.Extent() )
1207 if ( SMESH_MesherHelper:: Count( theShape1, TopAbs_EDGE, /*ignoreSame=*/false ) !=
1208 SMESH_MesherHelper:: Count( theShape2, TopAbs_EDGE, /*ignoreSame=*/false ))
1209 RETURN_BAD_RESULT("Different nb of vertices");
1212 if ( vMap1.Extent() == 1 || vMap2.Extent() == 1 ) {
1213 InsertAssociation( vMap1(1), vMap2(1), theMap );
1214 if ( theShape1.ShapeType() == TopAbs_EDGE ) {
1215 if ( vMap1.Extent() == 2 )
1216 InsertAssociation( vMap1(2), vMap2(1), theMap );
1217 else if ( vMap2.Extent() == 2 )
1218 InsertAssociation( vMap2(2), vMap1(1), theMap );
1219 InsertAssociation( theShape1, theShape2, theMap );
1222 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap);
1225 // Try to associate by common vertices of an edge
1226 for ( int i = 1; i <= vMap1.Extent(); ++i )
1228 const TopoDS_Shape& v1 = vMap1(i);
1229 if ( vMap2.Contains( v1 ))
1231 // find an egde sharing v1 and sharing at the same time another common vertex
1232 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( v1, *theMesh1, TopAbs_EDGE);
1233 bool edgeFound = false;
1234 while ( edgeIt->more() && !edgeFound )
1236 TopoDS_Edge edge = TopoDS::Edge( edgeIt->next()->Oriented(TopAbs_FORWARD));
1237 TopExp::Vertices(edge, VV1[0], VV1[1]);
1238 if ( !VV1[0].IsSame( VV1[1] ))
1239 edgeFound = ( vMap2.Contains( VV1[ v1.IsSame(VV1[0]) ? 1:0]));
1243 InsertAssociation( VV1[0], VV1[0], theMap );
1244 InsertAssociation( VV1[1], VV1[1], theMap );
1245 TShapeShapeMap::EAssocType asType = theMap._assocType;
1246 theMap.SetAssocType( TShapeShapeMap::COMMON_VERTEX );
1247 if ( FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap ))
1249 theMap._assocType = asType;
1254 // Find transformation to make the shapes be of similar size at same location
1257 for ( int i = 1; i <= vMap1.Extent(); ++i )
1258 box[ 0 ].Add( BRep_Tool::Pnt ( TopoDS::Vertex( vMap1( i ))));
1259 for ( int i = 1; i <= vMap2.Extent(); ++i )
1260 box[ 1 ].Add( BRep_Tool::Pnt ( TopoDS::Vertex( vMap2( i ))));
1262 gp_Pnt gc[2]; // box center
1263 double x0,y0,z0, x1,y1,z1;
1264 box[0].Get( x0,y0,z0, x1,y1,z1 );
1265 gc[0] = 0.5 * ( gp_XYZ( x0,y0,z0 ) + gp_XYZ( x1,y1,z1 ));
1266 box[1].Get( x0,y0,z0, x1,y1,z1 );
1267 gc[1] = 0.5 * ( gp_XYZ( x0,y0,z0 ) + gp_XYZ( x1,y1,z1 ));
1270 gp_Vec vec01( gc[0], gc[1] );
1271 double scale = sqrt( box[1].SquareExtent() / box[0].SquareExtent() );
1273 // Find 2 closest vertices
1275 // get 2 linked vertices of shape 1 not belonging to an inner wire of a face
1276 std::list< TopoDS_Edge > allBndEdges1;
1277 if ( !getOuterEdges( theShape1, *theMesh1, allBndEdges1 ))
1279 if ( theShape1.ShapeType() != TopAbs_FACE )
1280 RETURN_BAD_RESULT("Edge not found");
1281 return assocFewEdgesFaces( TopoDS::Face( theShape1 ), theMesh1,
1282 TopoDS::Face( theShape2 ), theMesh2, theMap );
1284 std::list< TopoDS_Edge >::iterator edge1 = allBndEdges1.begin();
1285 double minDist = std::numeric_limits<double>::max();
1286 for ( int nbChecked=0; edge1 != allBndEdges1.end() && nbChecked++ < 10; ++edge1 )
1288 TopoDS_Vertex edge1VV[2];
1289 TopExp::Vertices( TopoDS::Edge( edge1->Oriented(TopAbs_FORWARD)), edge1VV[0], edge1VV[1]);
1290 if ( edge1VV[0].IsSame( edge1VV[1] ))
1291 continue;//RETURN_BAD_RESULT("Only closed edges");
1293 // find vertices closest to 2 linked vertices of shape 1
1294 double dist2[2] = { 1e+100, 1e+100 };
1295 TopoDS_Vertex edge2VV[2];
1296 for ( int i1 = 0; i1 < 2; ++i1 )
1298 gp_Pnt p1 = BRep_Tool::Pnt( edge1VV[ i1 ]);
1299 p1.Scale( gc[0], scale );
1300 p1.Translate( vec01 );
1302 // select a closest vertex among all ones in vMap2
1303 for ( int i2 = 1; i2 <= vMap2.Extent(); ++i2 )
1305 TopoDS_Vertex V2 = TopoDS::Vertex( vMap2( i2 ));
1306 gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
1307 double d2 = p1.SquareDistance( p2 );
1308 if ( d2 < dist2[ 0 ] && d2 < minDist ) {
1314 else if ( !edge2VV[0].IsNull() ) {
1315 // select a closest vertex among ends of edges meeting at edge2VV[0]
1316 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( edge2VV[0],
1317 *theMesh2, TopAbs_EDGE);
1318 while ( const TopoDS_Shape* edge2 = edgeIt->next() )
1319 for ( TopoDS_Iterator itV2( *edge2 ); itV2.More(); itV2.Next() )
1321 if ( itV2.Value().IsSame( edge2VV[ 0 ])) continue;
1322 if ( !vMap2.Contains( itV2.Value() )) continue;
1323 TopoDS_Vertex V2 = TopoDS::Vertex( itV2.Value() );
1324 gp_Pnt p2 = BRep_Tool::Pnt ( V2 );
1325 double d2 = p1.SquareDistance( p2 );
1326 if ( d2 < dist2[1] && d2 < minDist ) {
1333 if ( dist2[0] + dist2[1] < minDist ) {
1334 VV1[0] = edge1VV[0];
1335 VV1[1] = edge1VV[1];
1336 VV2[0] = edge2VV[0];
1337 VV2[1] = edge2VV[1];
1338 minDist = dist2[0] + dist2[1];
1339 if ( minDist < 1e-10 )
1343 theMap.SetAssocType( TShapeShapeMap::CLOSE_VERTEX );
1345 InsertAssociation( VV1[ 0 ], VV2[ 0 ], theMap );
1346 InsertAssociation( VV1[ 1 ], VV2[ 1 ], theMap );
1347 // MESSAGE("Initial assoc VERT " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[ 0 ] )<<
1348 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[ 0 ] )<<
1349 // "\nand VERT " << theMesh1->GetMeshDS()->ShapeToIndex( VV1[ 1 ] )<<
1350 // " to " << theMesh2->GetMeshDS()->ShapeToIndex( VV2[ 1 ] ));
1351 if ( theShape1.ShapeType() == TopAbs_EDGE ) {
1352 InsertAssociation( theShape1, theShape2, theMap );
1356 return FindSubShapeAssociation( theShape1, theMesh1, theShape2, theMesh2, theMap );
1359 //================================================================================
1361 * Find association of edges of faces
1362 * \param face1 - face 1
1363 * \param VV1 - vertices of face 1
1364 * \param face2 - face 2
1365 * \param VV2 - vertices of face 2 associated with ones of face 1
1366 * \param edges1 - out list of edges of face 1
1367 * \param edges2 - out list of edges of face 2
1368 * \param isClosenessAssoc - is association starting by VERTEX closeness
1369 * \retval int - nb of edges in an outer wire in a success case, else zero
1371 //================================================================================
1373 int StdMeshers_ProjectionUtils::FindFaceAssociation(const TopoDS_Face& face1,
1374 TopoDS_Vertex VV1[2],
1375 const TopoDS_Face& face2,
1376 TopoDS_Vertex VV2[2],
1377 list< TopoDS_Edge > & edges1,
1378 list< TopoDS_Edge > & edges2,
1379 const bool isClosenessAssoc)
1382 list< int > nbEInW1, nbEInW2;
1383 list< TopoDS_Edge >::iterator edgeIt;
1384 int i_ok_wire_algo = -1;
1385 for ( int outer_wire_algo = 0; outer_wire_algo < 2 && !OK; ++outer_wire_algo )
1390 if ( SMESH_Block::GetOrderedEdges( face1, edges1, nbEInW1, VV1[0], outer_wire_algo) !=
1391 SMESH_Block::GetOrderedEdges( face2, edges2, nbEInW2, VV2[0], outer_wire_algo) )
1392 CONT_BAD_RESULT("Different number of wires in faces ");
1394 if ( nbEInW1 != nbEInW2 && outer_wire_algo == 0 &&
1395 ( std::accumulate( nbEInW1.begin(), nbEInW1.end(), 0) !=
1396 std::accumulate( nbEInW2.begin(), nbEInW2.end(), 0)))
1397 RETURN_BAD_RESULT("Different number of edges in faces");
1399 if ( nbEInW1.front() != nbEInW2.front() )
1400 CONT_BAD_RESULT("Different number of edges in the outer wire: " <<
1401 nbEInW1.front() << " != " << nbEInW2.front());
1403 i_ok_wire_algo = outer_wire_algo;
1405 // Define if we need to reverse one of wires to make edges in lists match each other
1407 bool reverse = false;
1408 const bool severalWires = ( nbEInW1.size() > 1 );
1410 if ( !VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true )))
1413 // check if the second vertex belongs to the first or last edge in the wire
1414 edgeIt = --edges1.end(); // pointer to the last edge in the outer wire
1415 if ( severalWires ) {
1416 edgeIt = edges1.begin();
1417 std::advance( edgeIt, nbEInW1.front()-1 );
1419 if ( TopExp::FirstVertex( *edgeIt ).IsSame( TopExp::LastVertex( *edgeIt )) &&
1420 SMESH_Algo::isDegenerated( *edgeIt )) {
1421 --edgeIt; // skip a degenerated edge (test 3D_mesh_Projection_00/A3)
1423 if ( !VV1[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
1424 CONT_BAD_RESULT("GetOrderedEdges() failed");
1427 if ( !VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true )))
1430 // check if the second vertex belongs to the first or last edge in the wire
1431 edgeIt = --edges2.end(); // pointer to the last edge in the outer wire
1432 if ( severalWires ) {
1433 edgeIt = edges2.begin();
1434 std::advance( edgeIt, nbEInW2.front()-1 );
1436 if ( TopExp::FirstVertex( *edgeIt ).IsSame( TopExp::LastVertex( *edgeIt )) &&
1437 SMESH_Algo::isDegenerated( *edgeIt )) {
1438 --edgeIt; // skip a degenerated edge
1440 if ( !VV2[1].IsSame( TopExp::FirstVertex( *edgeIt, true ))) {
1441 CONT_BAD_RESULT("GetOrderedEdges() failed");
1446 reverseEdges( edges2 , nbEInW2.front());
1448 if ( SMESH_Algo::isDegenerated( edges2.front() ))
1450 // move a degenerated edge to the back of the outer wire
1451 edgeIt = edges2.end();
1452 if ( severalWires ) {
1453 edgeIt = edges2.begin();
1454 std::advance( edgeIt, nbEInW2.front() );
1456 edges2.splice( edgeIt, edges2, edges2.begin() );
1458 if (( VV1[1].IsSame( TopExp::LastVertex( edges1.front(), true ))) !=
1459 ( VV2[1].IsSame( TopExp::LastVertex( edges2.front(), true ))))
1460 CONT_BAD_RESULT("GetOrderedEdges() failed");
1464 } // loop algos getting an outer wire
1466 if ( OK && nbEInW1.front() > 4 ) // care of a case where faces are closed (23032)
1468 // check if the first edges are seam ones
1469 list< TopoDS_Edge >::iterator revSeam1, revSeam2;
1470 revSeam1 = std::find( ++edges1.begin(), edges1.end(), edges1.front().Reversed());
1471 revSeam2 = edges2.end();
1472 if ( revSeam1 != edges1.end() )
1473 revSeam2 = std::find( ++edges2.begin(), edges2.end(), edges2.front().Reversed());
1474 if ( revSeam2 != edges2.end() ) // two seams detected
1477 std::distance( edges1.begin(), revSeam1 ) != std::distance( edges2.begin(), revSeam2 );
1478 if ( !reverse && isClosenessAssoc )
1480 // compare orientations of a non-seam edges using 3D closeness;
1481 // look for a non-seam edges
1482 list< TopoDS_Edge >::iterator edge1 = ++edges1.begin();
1483 list< TopoDS_Edge >::iterator edge2 = ++edges2.begin();
1484 for ( ; edge1 != edges1.end(); ++edge1, ++edge2 )
1486 if (( edge1 == revSeam1 ) ||
1487 ( SMESH_Algo::isDegenerated( *edge1 )) ||
1488 ( std::find( ++edges1.begin(), edges1.end(), edge1->Reversed()) != edges1.end() ))
1490 gp_Pnt p1 = BRep_Tool::Pnt( VV1[0] );
1491 gp_Pnt p2 = BRep_Tool::Pnt( VV2[0] );
1492 gp_Vec vec2to1( p2, p1 );
1494 gp_Pnt pp1[2], pp2[2];
1495 const double r = 0.2345;
1497 Handle(Geom_Curve) C = BRep_Tool::Curve( *edge1, f,l );
1498 pp1[0] = C->Value( f * r + l * ( 1. - r ));
1499 pp1[1] = C->Value( l * r + f * ( 1. - r ));
1500 if ( edge1->Orientation() == TopAbs_REVERSED )
1501 std::swap( pp1[0], pp1[1] );
1502 C = BRep_Tool::Curve( *edge2, f,l );
1503 if ( C.IsNull() ) return 0;
1504 pp2[0] = C->Value( f * r + l * ( 1. - r )).Translated( vec2to1 );
1505 pp2[1] = C->Value( l * r + f * ( 1. - r )).Translated( vec2to1 );
1506 if ( edge2->Orientation() == TopAbs_REVERSED )
1507 std::swap( pp2[0], pp2[1] );
1509 double dist00 = pp1[0].SquareDistance( pp2[0] );
1510 double dist01 = pp1[0].SquareDistance( pp2[1] );
1511 reverse = ( dist00 > dist01 );
1515 if ( reverse ) // make a seam counterpart be the first
1517 list< TopoDS_Edge >::iterator outWireEnd = edges2.begin();
1518 std::advance( outWireEnd, nbEInW2.front() );
1519 edges2.splice( outWireEnd, edges2, edges2.begin(), ++revSeam2 );
1520 reverseEdges( edges2 , nbEInW2.front());
1525 // Try to orient all (if !OK) or only internal wires (issue 0020996) by UV similarity
1527 if (( !OK || nbEInW1.size() > 1 ) && i_ok_wire_algo > -1 )
1529 // Check that Vec(VV1[0],VV1[1]) in 2D on face1 is the same
1530 // as Vec(VV2[0],VV2[1]) on face2
1531 double vTol = BRep_Tool::Tolerance( VV1[0] );
1532 BRepAdaptor_Surface surface1( face1, true );
1533 BRepAdaptor_Surface surface2( face2, true );
1534 // TODO: use TrsfFinder2D to superpose the faces
1535 gp_Pnt2d v0f1UV( surface1.FirstUParameter(), surface1.FirstVParameter() );
1536 gp_Pnt2d v0f2UV( surface2.FirstUParameter(), surface2.FirstVParameter() );
1537 gp_Pnt2d v1f1UV( surface1.LastUParameter(), surface1.LastVParameter() );
1538 gp_Pnt2d v1f2UV( surface2.LastUParameter(), surface2.LastVParameter() );
1540 surface1.UResolution( vTol ) + surface1.VResolution( vTol ); // let's be tolerant
1541 // VV1[0] = TopExp::FirstVertex( edges1.front(), true ); // ori is important if face is closed
1542 // VV1[1] = TopExp::LastVertex ( edges1.front(), true );
1543 // VV2[0] = TopExp::FirstVertex( edges2.front(), true );
1544 // VV2[1] = TopExp::LastVertex ( edges2.front(), true );
1545 // gp_Pnt2d v0f1UV = BRep_Tool::Parameters( VV1[0], face1 );
1546 // gp_Pnt2d v0f2UV = BRep_Tool::Parameters( VV2[0], face2 );
1547 // gp_Pnt2d v1f1UV = BRep_Tool::Parameters( VV1[1], face1 );
1548 // gp_Pnt2d v1f2UV = BRep_Tool::Parameters( VV2[1], face2 );
1549 gp_Vec2d v01f1Vec( v0f1UV, v1f1UV );
1550 gp_Vec2d v01f2Vec( v0f2UV, v1f2UV );
1551 if ( Abs( v01f1Vec.X()-v01f2Vec.X()) < vTolUV &&
1552 Abs( v01f1Vec.Y()-v01f2Vec.Y()) < vTolUV )
1554 if ( !OK /*i_ok_wire_algo != 1*/ )
1558 SMESH_Block::GetOrderedEdges( face1, edges1, nbEInW1, VV1[0], i_ok_wire_algo);
1559 SMESH_Block::GetOrderedEdges( face2, edges2, nbEInW2, VV2[0], i_ok_wire_algo);
1561 gp_XY dUV = v0f2UV.XY() - v0f1UV.XY(); // UV shift between 2 faces
1563 // skip edges of the outer wire (if the outer wire is OK)
1564 list< int >::iterator nbE2, nbE1 = nbEInW1.begin();
1565 list< TopoDS_Edge >::iterator edge2Beg, edge1Beg = edges1.begin();
1566 if ( OK ) std::advance( edge1Beg, *nbE1++ );
1567 list< TopoDS_Edge >::iterator edge2End, edge1End;
1569 // find corresponding wires of face2
1570 for ( int iW1 = OK; nbE1 != nbEInW1.end(); ++nbE1, ++iW1 ) // loop on wires of face1
1572 // reach an end of edges of a current wire1
1573 edge1End = edge1Beg;
1574 std::advance( edge1End, *nbE1 );
1575 // UV on face1 to find on face2
1576 TopoDS_Vertex v01 = SMESH_MesherHelper::IthVertex(0,*edge1Beg);
1577 TopoDS_Vertex v11 = SMESH_MesherHelper::IthVertex(1,*edge1Beg);
1578 v0f1UV = BRep_Tool::Parameters( v01, face1 );
1579 v1f1UV = BRep_Tool::Parameters( v11, face1 );
1580 v0f1UV.ChangeCoord() += dUV;
1581 v1f1UV.ChangeCoord() += dUV;
1583 // look through wires of face2
1584 edge2Beg = edges2.begin();
1585 nbE2 = nbEInW2.begin();
1586 if ( OK ) std::advance( edge2Beg, *nbE2++ );
1587 for ( int iW2 = OK; nbE2 != nbEInW2.end(); ++nbE2, ++iW2 ) // loop on wires of face2
1589 // reach an end of edges of a current wire2
1590 edge2End = edge2Beg;
1591 std::advance( edge2End, *nbE2 );
1592 if ( *nbE1 == *nbE2 && iW2 >= iW1 )
1594 // rotate edge2 until coincides with edge1 in 2D
1596 bool sameUV = false;
1597 while ( !( sameUV = sameVertexUV( *edge2Beg, face2, 0, v0f1UV, vTolUV )) && --i > 0 )
1598 // move edge2Beg to place before edge2End
1599 edges2.splice( edge2End, edges2, edge2Beg++ );
1603 if ( iW1 == 0 ) OK = true; // OK is for the first wire
1605 // reverse edges2 if needed
1606 if ( SMESH_MesherHelper::IsClosedEdge( *edge1Beg ))
1608 // Commented (so far?) as it's not checked if orientation must be same or reversed
1610 // Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( *edge1Beg, face1,f,l );
1611 // if ( edge1Beg->Orientation() == TopAbs_REVERSED )
1612 // std::swap( f,l );
1613 // gp_Pnt2d uv1 = dUV + c1->Value( f * 0.8 + l * 0.2 ).XY();
1615 // Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( *edge2Beg, face2,f,l );
1616 // if ( edge2Beg->Orientation() == TopAbs_REVERSED )
1617 // std::swap( f,l );
1618 // gp_Pnt2d uv2 = c2->Value( f * 0.8 + l * 0.2 );
1619 // gp_Pnt2d uv3 = c2->Value( l * 0.8 + f * 0.2 );
1621 // if ( uv1.SquareDistance( uv2 ) > uv1.SquareDistance( uv3 ))
1622 // edge2Beg->Reverse();
1626 if ( !sameVertexUV( *edge2Beg, face2, 1, v1f1UV, vTolUV ))
1627 reverseEdges( edges2 , *nbE2, std::distance( edges2.begin(),edge2Beg ));
1630 // put wire2 at a right place within edges2
1632 list< TopoDS_Edge >::iterator place2 = edges2.begin();
1633 std::advance( place2, std::distance( edges1.begin(), edge1Beg ));
1634 edges2.splice( place2, edges2, edge2Beg, edge2End );
1635 // move nbE2 as well
1636 list< int >::iterator placeNbE2 = nbEInW2.begin();
1637 std::advance( placeNbE2, iW1 );
1638 nbEInW2.splice( placeNbE2, nbEInW2, nbE2 );
1643 // prepare for the next wire loop
1644 edge2Beg = edge2End;
1646 edge1Beg = edge1End;
1651 const int nbEdges = nbEInW1.front();
1652 if ( OK && nbEdges == 2 )
1654 // if wires include 2 edges, it's impossible to associate them using
1655 // topological information only. Try to use length of edges for association.
1656 double l1[2], l2[2];
1657 edgeIt = edges1.begin();
1658 l1[0] = SMESH_Algo::EdgeLength( *edgeIt++ );
1659 l1[1] = SMESH_Algo::EdgeLength( *edgeIt++ );
1660 if ( Abs( l1[0] - l1[1] ) > 0.1 * Max( l1[0], l1[1] ) )
1662 edgeIt = edges2.begin();
1663 l2[0] = SMESH_Algo::EdgeLength( *edgeIt++ );
1664 l2[1] = SMESH_Algo::EdgeLength( *edgeIt++ );
1665 if (( l1[0] < l1[1] ) != ( l2[0] < l2[1] ))
1667 reverseEdges( edges2, nbEdges );
1672 return OK ? nbEInW1.front() : 0;
1675 //=======================================================================
1676 //function : InitVertexAssociation
1678 //=======================================================================
1680 void StdMeshers_ProjectionUtils::InitVertexAssociation( const SMESH_Hypothesis* theHyp,
1681 TShapeShapeMap & theAssociationMap)
1683 string hypName = theHyp->GetName();
1684 if ( hypName == "ProjectionSource1D" ) {
1685 const StdMeshers_ProjectionSource1D * hyp =
1686 static_cast<const StdMeshers_ProjectionSource1D*>( theHyp );
1687 if ( hyp->HasVertexAssociation() )
1688 InsertAssociation( hyp->GetTargetVertex(),hyp->GetSourceVertex(),theAssociationMap );
1690 else if ( hypName == "ProjectionSource2D" ) {
1691 const StdMeshers_ProjectionSource2D * hyp =
1692 static_cast<const StdMeshers_ProjectionSource2D*>( theHyp );
1693 if ( hyp->HasVertexAssociation() ) {
1694 InsertAssociation( hyp->GetTargetVertex(1),hyp->GetSourceVertex(1),theAssociationMap);
1695 InsertAssociation( hyp->GetTargetVertex(2),hyp->GetSourceVertex(2),theAssociationMap);
1698 else if ( hypName == "ProjectionSource3D" ) {
1699 const StdMeshers_ProjectionSource3D * hyp =
1700 static_cast<const StdMeshers_ProjectionSource3D*>( theHyp );
1701 if ( hyp->HasVertexAssociation() ) {
1702 InsertAssociation( hyp->GetTargetVertex(1),hyp->GetSourceVertex(1),theAssociationMap);
1703 InsertAssociation( hyp->GetTargetVertex(2),hyp->GetSourceVertex(2),theAssociationMap);
1708 //=======================================================================
1710 * Inserts association theShape1 <-> theShape2 to TShapeShapeMap
1711 * \param theShape1 - target shape
1712 * \param theShape2 - source shape
1713 * \param theAssociationMap - association map
1714 * \retval bool - true if there was no association for these shapes before
1716 //=======================================================================
1718 bool StdMeshers_ProjectionUtils::InsertAssociation( const TopoDS_Shape& theShape1, // tgt
1719 const TopoDS_Shape& theShape2, // src
1720 TShapeShapeMap & theAssociationMap)
1722 if ( !theShape1.IsNull() && !theShape2.IsNull() ) {
1723 SHOW_SHAPE(theShape1,"Assoc ");
1724 SHOW_SHAPE(theShape2," to ");
1725 bool isNew = ( theAssociationMap.Bind( theShape1, theShape2 ));
1729 throw SALOME_Exception("StdMeshers_ProjectionUtils: attempt to associate NULL shape");
1734 //=======================================================================
1736 * Finds an edge by its vertices in a main shape of the mesh
1737 * \param aMesh - the mesh
1738 * \param V1 - vertex 1
1739 * \param V2 - vertex 2
1740 * \retval TopoDS_Edge - found edge
1742 //=======================================================================
1744 TopoDS_Edge StdMeshers_ProjectionUtils::GetEdgeByVertices( SMESH_Mesh* theMesh,
1745 const TopoDS_Vertex& theV1,
1746 const TopoDS_Vertex& theV2)
1748 if ( theMesh && !theV1.IsNull() && !theV2.IsNull() )
1750 TopTools_ListIteratorOfListOfShape ancestorIt( theMesh->GetAncestors( theV1 ));
1751 for ( ; ancestorIt.More(); ancestorIt.Next() )
1752 if ( ancestorIt.Value().ShapeType() == TopAbs_EDGE )
1753 for ( TopExp_Explorer expV ( ancestorIt.Value(), TopAbs_VERTEX );
1756 if ( theV2.IsSame( expV.Current() ))
1757 return TopoDS::Edge( ancestorIt.Value() );
1759 return TopoDS_Edge();
1762 //================================================================================
1764 * Return another face sharing an edge
1765 * \param edgeToFaces - data map of descendants to ancestors
1766 * \param edge - edge
1767 * \param face - face
1768 * \retval TopoDS_Face - found face
1770 //================================================================================
1772 TopoDS_Face StdMeshers_ProjectionUtils::GetNextFace( const TAncestorMap& edgeToFaces,
1773 const TopoDS_Edge& edge,
1774 const TopoDS_Face& face)
1776 // if ( !edge.IsNull() && !face.IsNull() && edgeToFaces.Contains( edge ))
1777 if ( !edge.IsNull() && edgeToFaces.Contains( edge )) // PAL16202
1779 TopTools_ListIteratorOfListOfShape ancestorIt( edgeToFaces.FindFromKey( edge ));
1780 for ( ; ancestorIt.More(); ancestorIt.Next() )
1781 if ( ancestorIt.Value().ShapeType() == TopAbs_FACE &&
1782 !face.IsSame( ancestorIt.Value() ))
1783 return TopoDS::Face( ancestorIt.Value() );
1785 return TopoDS_Face();
1788 //================================================================================
1790 * Return other vertex of an edge
1792 //================================================================================
1794 TopoDS_Vertex StdMeshers_ProjectionUtils::GetNextVertex(const TopoDS_Edge& edge,
1795 const TopoDS_Vertex& vertex)
1797 TopoDS_Vertex vF,vL;
1798 TopExp::Vertices(edge,vF,vL);
1799 if ( vF.IsSame( vL ))
1800 return TopoDS_Vertex();
1801 return vertex.IsSame( vF ) ? vL : vF;
1804 //================================================================================
1806 * Return a propagation edge
1807 * \param aMesh - mesh
1808 * \param anEdge - edge to find by propagation
1809 * \param fromEdge - start edge for propagation
1810 * \param chain - return, if !NULL, a propagation chain passed till
1811 * anEdge; if anEdge.IsNull() then a full propagation chain is returned;
1812 * fromEdge is the 1st in the chain
1813 * \retval pair<int,TopoDS_Edge> - propagation step and found edge
1815 //================================================================================
1817 pair<int,TopoDS_Edge>
1818 StdMeshers_ProjectionUtils::GetPropagationEdge( SMESH_Mesh* aMesh,
1819 const TopoDS_Edge& anEdge,
1820 const TopoDS_Edge& fromEdge,
1821 TopTools_IndexedMapOfShape* chain)
1823 TopTools_IndexedMapOfShape locChain;
1824 TopTools_IndexedMapOfShape& aChain = chain ? *chain : locChain;
1827 //TopTools_IndexedMapOfShape checkedWires;
1828 BRepTools_WireExplorer aWE;
1829 TopoDS_Shape fourEdges[4];
1831 // List of edges, added to chain on the previous cycle pass
1832 TopTools_ListOfShape listPrevEdges;
1833 listPrevEdges.Append( fromEdge );
1834 aChain.Add( fromEdge );
1836 // Collect all edges pass by pass
1837 while (listPrevEdges.Extent() > 0)
1840 // List of edges, added to chain on this cycle pass
1841 TopTools_ListOfShape listCurEdges;
1843 // Find the next portion of edges
1844 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1845 for (; itE.More(); itE.Next())
1847 const TopoDS_Shape& anE = itE.Value();
1849 // Iterate on faces, having edge <anE>
1850 TopTools_ListIteratorOfListOfShape itA (aMesh->GetAncestors(anE));
1851 for (; itA.More(); itA.Next())
1853 const TopoDS_Shape& aW = itA.Value();
1855 // There are objects of different type among the ancestors of edge
1856 if ( aW.ShapeType() == TopAbs_WIRE /*&& checkedWires.Add( aW )*/)
1858 Standard_Integer nb = 0, found = -1;
1859 for ( aWE.Init( TopoDS::Wire( aW )); aWE.More(); aWE.Next() ) {
1864 fourEdges[ nb ] = aWE.Current();
1865 if ( aWE.Current().IsSame( anE )) found = nb;
1868 if (nb == 4 && found >= 0) {
1869 // Quadrangle face found, get an opposite edge
1870 TopoDS_Shape& anOppE = fourEdges[( found + 2 ) % 4 ];
1872 // add anOppE to aChain if ...
1873 int prevChainSize = aChain.Extent();
1874 if ( aChain.Add(anOppE) > prevChainSize ) { // ... anOppE is not in aChain
1875 // Add found edge to the chain oriented so that to
1876 // have it co-directed with a fromEdge
1877 TopAbs_Orientation ori = anE.Orientation();
1878 if ( anOppE.Orientation() == fourEdges[found].Orientation() )
1879 ori = TopAbs::Reverse( ori );
1880 anOppE.Orientation( ori );
1881 if ( anOppE.IsSame( anEdge ))
1882 return make_pair( step, TopoDS::Edge( anOppE ));
1883 listCurEdges.Append(anOppE);
1885 } // if (nb == 4 && found >= 0)
1886 } // if (aF.ShapeType() == TopAbs_WIRE)
1887 } // loop on ancestors of anE
1888 } // loop on listPrevEdges
1890 listPrevEdges = listCurEdges;
1891 } // while (listPrevEdges.Extent() > 0)
1893 return make_pair( INT_MAX, TopoDS_Edge());
1896 //================================================================================
1898 * Find corresponding nodes on two faces
1899 * \param face1 - the first face
1900 * \param mesh1 - mesh containing elements on the first face
1901 * \param face2 - the second face
1902 * \param mesh2 - mesh containing elements on the second face
1903 * \param assocMap - map associating sub-shapes of the faces
1904 * \param node1To2Map - map containing found matching nodes
1905 * \retval bool - is a success
1907 //================================================================================
1909 bool StdMeshers_ProjectionUtils::
1910 FindMatchingNodesOnFaces( const TopoDS_Face& face1,
1912 const TopoDS_Face& face2,
1914 const TShapeShapeMap & assocMap,
1915 TNodeNodeMap & node1To2Map)
1917 SMESHDS_Mesh* meshDS1 = mesh1->GetMeshDS();
1918 SMESHDS_Mesh* meshDS2 = mesh2->GetMeshDS();
1920 SMESH_MesherHelper helper1( *mesh1 );
1921 SMESH_MesherHelper helper2( *mesh2 );
1923 // Get corresponding submeshes and roughly check match of meshes
1925 SMESHDS_SubMesh * SM2 = meshDS2->MeshElements( face2 );
1926 SMESHDS_SubMesh * SM1 = meshDS1->MeshElements( face1 );
1928 RETURN_BAD_RESULT("Empty submeshes");
1929 if ( SM2->NbNodes() != SM1->NbNodes() ||
1930 SM2->NbElements() != SM1->NbElements() )
1931 RETURN_BAD_RESULT("Different meshes on corresponding faces "
1932 << meshDS1->ShapeToIndex( face1 ) << " and "
1933 << meshDS2->ShapeToIndex( face2 ));
1934 if ( SM2->NbElements() == 0 )
1935 RETURN_BAD_RESULT("Empty submeshes");
1937 helper1.SetSubShape( face1 );
1938 helper2.SetSubShape( face2 );
1939 if ( helper1.HasRealSeam() != helper2.HasRealSeam() )
1940 RETURN_BAD_RESULT("Different faces' geometry");
1942 // Data to call SMESH_MeshEditor::FindMatchingNodes():
1944 // 1. Nodes of corresponding links:
1946 // get 2 matching edges, try to find not seam ones
1947 TopoDS_Edge edge1, edge2, seam1, seam2, anyEdge1, anyEdge2;
1948 TopExp_Explorer eE( OuterShape( face2, TopAbs_WIRE ), TopAbs_EDGE );
1951 TopoDS_Edge e2 = TopoDS::Edge( eE.Current() );
1954 if ( !assocMap.IsBound( e2, /*is2nd=*/true ))
1956 //RETURN_BAD_RESULT("Association not found for edge " << meshDS2->ShapeToIndex( e2 ));
1957 TopoDS_Edge e1 = TopoDS::Edge( assocMap( e2, /*is2nd=*/true ));
1958 if ( !helper1.IsSubShape( e1, face1 ))
1959 RETURN_BAD_RESULT("Wrong association, edge " << meshDS1->ShapeToIndex( e1 ) <<
1960 " isn't a sub-shape of face " << meshDS1->ShapeToIndex( face1 ));
1961 // check that there are nodes on edges
1962 SMESHDS_SubMesh * eSM1 = meshDS1->MeshElements( e1 );
1963 SMESHDS_SubMesh * eSM2 = meshDS2->MeshElements( e2 );
1964 bool nodesOnEdges = ( eSM1 && eSM2 && eSM1->NbNodes() && eSM2->NbNodes() );
1965 // check that the nodes on edges belong to faces
1966 // (as NETGEN ignores nodes on the degenerated geom edge)
1967 bool nodesOfFaces = false;
1968 if ( nodesOnEdges ) {
1969 const SMDS_MeshNode* n1 = eSM1->GetNodes()->next();
1970 const SMDS_MeshNode* n2 = eSM2->GetNodes()->next();
1971 nodesOfFaces = ( n1->GetInverseElementIterator(SMDSAbs_Face)->more() &&
1972 n2->GetInverseElementIterator(SMDSAbs_Face)->more() );
1976 if ( helper2.IsRealSeam( e2 )) {
1977 seam1 = e1; seam2 = e2;
1980 edge1 = e1; edge2 = e2;
1984 anyEdge1 = e1; anyEdge2 = e2;
1986 } while ( edge2.IsNull() && eE.More() );
1988 if ( edge2.IsNull() ) {
1989 edge1 = seam1; edge2 = seam2;
1991 bool hasNodesOnEdge = (! edge2.IsNull() );
1992 if ( !hasNodesOnEdge ) {
1993 // 0020338 - nb segments == 1
1994 edge1 = anyEdge1; edge2 = anyEdge2;
1997 // get 2 matching vertices
1998 TopoDS_Vertex V2 = TopExp::FirstVertex( TopoDS::Edge( edge2 ));
1999 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
2001 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
2002 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
2003 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
2005 TopoDS_Vertex V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
2007 // nodes on vertices
2008 const SMDS_MeshNode* vNode1 = SMESH_Algo::VertexNode( V1, meshDS1 );
2009 const SMDS_MeshNode* vNode2 = SMESH_Algo::VertexNode( V2, meshDS2 );
2010 if ( !vNode1 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
2011 if ( !vNode2 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
2013 // nodes on edges linked with nodes on vertices
2014 const SMDS_MeshNode* nullNode = 0;
2015 vector< const SMDS_MeshNode*> eNode1( 2, nullNode );
2016 vector< const SMDS_MeshNode*> eNode2( 2, nullNode );
2017 if ( hasNodesOnEdge )
2019 int nbNodeToGet = 1;
2020 if ( helper1.IsClosedEdge( edge1 ) || helper2.IsClosedEdge( edge2 ) )
2022 for ( int is2 = 0; is2 < 2; ++is2 )
2024 TopoDS_Edge & edge = is2 ? edge2 : edge1;
2025 SMESHDS_Mesh * smDS = is2 ? meshDS2 : meshDS1;
2026 SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
2027 // nodes linked with ones on vertices
2028 const SMDS_MeshNode* vNode = is2 ? vNode2 : vNode1;
2029 vector< const SMDS_MeshNode*>& eNode = is2 ? eNode2 : eNode1;
2031 SMDS_ElemIteratorPtr vElem = vNode->GetInverseElementIterator(SMDSAbs_Edge);
2032 while ( vElem->more() && nbGotNode != nbNodeToGet ) {
2033 const SMDS_MeshElement* elem = vElem->next();
2034 if ( edgeSM->Contains( elem ))
2035 eNode[ nbGotNode++ ] =
2036 ( elem->GetNode(0) == vNode ) ? elem->GetNode(1) : elem->GetNode(0);
2038 if ( nbGotNode > 1 ) // sort found nodes by param on edge
2040 SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
2041 double u0 = helper->GetNodeU( edge, eNode[ 0 ]);
2042 double u1 = helper->GetNodeU( edge, eNode[ 1 ]);
2043 if ( u0 > u1 ) std::swap( eNode[ 0 ], eNode[ 1 ]);
2045 if ( nbGotNode == 0 )
2046 RETURN_BAD_RESULT("Found no nodes on edge " << smDS->ShapeToIndex( edge ) <<
2047 " linked to " << vNode );
2050 else // 0020338 - nb segments == 1
2052 // get 2 other matching vertices
2053 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
2054 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
2055 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
2056 V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
2058 // nodes on vertices
2059 eNode1[0] = SMESH_Algo::VertexNode( V1, meshDS1 );
2060 eNode2[0] = SMESH_Algo::VertexNode( V2, meshDS2 );
2061 if ( !eNode1[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
2062 if ( !eNode2[0] ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
2068 for ( int iAttempt = 0; iAttempt < 2; ++iAttempt )
2070 set<const SMDS_MeshElement*> Elems1, Elems2;
2071 for ( int is2 = 0; is2 < 2; ++is2 )
2073 set<const SMDS_MeshElement*> & elems = is2 ? Elems2 : Elems1;
2074 SMESHDS_SubMesh* sm = is2 ? SM2 : SM1;
2075 SMESH_MesherHelper* helper = is2 ? &helper2 : &helper1;
2076 const TopoDS_Face & face = is2 ? face2 : face1;
2077 SMDS_ElemIteratorPtr eIt = sm->GetElements();
2079 if ( !helper->IsRealSeam( is2 ? edge2 : edge1 ))
2081 while ( eIt->more() ) elems.insert( elems.end(), eIt->next() );
2085 // the only suitable edge is seam, i.e. it is a sphere.
2086 // FindMatchingNodes() will not know which way to go from any edge.
2087 // So we ignore all faces having nodes on edges or vertices except
2088 // one of faces sharing current start nodes
2090 // find a face to keep
2091 const SMDS_MeshElement* faceToKeep = 0;
2092 const SMDS_MeshNode* vNode = is2 ? vNode2 : vNode1;
2093 const SMDS_MeshNode* eNode = is2 ? eNode2[0] : eNode1[0];
2094 TIDSortedElemSet inSet, notInSet;
2096 const SMDS_MeshElement* f1 =
2097 SMESH_MeshAlgos::FindFaceInSet( vNode, eNode, inSet, notInSet );
2098 if ( !f1 ) RETURN_BAD_RESULT("The first face on seam not found");
2099 notInSet.insert( f1 );
2101 const SMDS_MeshElement* f2 =
2102 SMESH_MeshAlgos::FindFaceInSet( vNode, eNode, inSet, notInSet );
2103 if ( !f2 ) RETURN_BAD_RESULT("The second face on seam not found");
2105 // select a face with less UV of vNode
2106 const SMDS_MeshNode* notSeamNode[2] = {0, 0};
2107 for ( int iF = 0; iF < 2; ++iF ) {
2108 const SMDS_MeshElement* f = ( iF ? f2 : f1 );
2109 for ( int i = 0; !notSeamNode[ iF ] && i < f->NbNodes(); ++i ) {
2110 const SMDS_MeshNode* node = f->GetNode( i );
2111 if ( !helper->IsSeamShape( node->getshapeId() ))
2112 notSeamNode[ iF ] = node;
2115 gp_Pnt2d uv1 = helper->GetNodeUV( face, vNode, notSeamNode[0] );
2116 gp_Pnt2d uv2 = helper->GetNodeUV( face, vNode, notSeamNode[1] );
2117 if ( uv1.X() + uv1.Y() > uv2.X() + uv2.Y() )
2123 elems.insert( faceToKeep );
2124 while ( eIt->more() ) {
2125 const SMDS_MeshElement* f = eIt->next();
2126 int nbNodes = f->NbNodes();
2127 if ( f->IsQuadratic() )
2130 for ( int i = 0; !onBnd && i < nbNodes; ++i ) {
2131 const SMDS_MeshNode* node = f->GetNode( i );
2132 onBnd = ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE);
2137 // add also faces adjacent to faceToKeep
2138 int nbNodes = faceToKeep->NbNodes();
2139 if ( faceToKeep->IsQuadratic() ) nbNodes /= 2;
2140 notInSet.insert( f1 );
2141 notInSet.insert( f2 );
2142 for ( int i = 0; i < nbNodes; ++i ) {
2143 const SMDS_MeshNode* n1 = faceToKeep->GetNode( i );
2144 const SMDS_MeshNode* n2 = faceToKeep->GetNode(( i+1 ) % nbNodes );
2145 f1 = SMESH_MeshAlgos::FindFaceInSet( n1, n2, inSet, notInSet );
2149 } // case on a sphere
2150 } // loop on 2 faces
2152 node1To2Map.clear();
2153 assocRes = SMESH_MeshEditor::FindMatchingNodes( Elems1, Elems2,
2155 eNode1[0], eNode2[0],
2157 if (( assocRes != SMESH_MeshEditor::SEW_OK ) &&
2158 ( eNode1[1] || eNode2[1] )) // there is another node to try (on a closed EDGE)
2160 node1To2Map.clear();
2161 if ( eNode1[1] ) std::swap( eNode1[0], eNode1[1] );
2162 else std::swap( eNode2[0], eNode2[1] );
2163 continue; // one more attempt
2168 if ( assocRes != SMESH_MeshEditor::SEW_OK )
2169 RETURN_BAD_RESULT("FindMatchingNodes() result " << assocRes );
2171 // On a sphere, add matching nodes on the edge
2173 if ( helper1.IsRealSeam( edge1 ))
2175 // sort nodes on edges by param on edge
2176 map< double, const SMDS_MeshNode* > u2nodesMaps[2];
2177 for ( int is2 = 0; is2 < 2; ++is2 )
2179 TopoDS_Edge & edge = is2 ? edge2 : edge1;
2180 SMESHDS_Mesh * smDS = is2 ? meshDS2 : meshDS1;
2181 SMESHDS_SubMesh* edgeSM = smDS->MeshElements( edge );
2182 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[ is2 ];
2184 SMDS_NodeIteratorPtr nIt = edgeSM->GetNodes();
2185 while ( nIt->more() ) {
2186 const SMDS_MeshNode* node = nIt->next();
2187 const SMDS_EdgePosition* pos =
2188 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
2189 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
2191 if ((int) pos2nodes.size() != edgeSM->NbNodes() )
2192 RETURN_BAD_RESULT("Equal params of nodes on edge "
2193 << smDS->ShapeToIndex( edge ) << " of face " << is2 );
2195 if ( u2nodesMaps[0].size() != u2nodesMaps[1].size() )
2196 RETURN_BAD_RESULT("Different nb of new nodes on edges or wrong params");
2198 // compare edge orientation
2199 double u1 = helper1.GetNodeU( edge1, vNode1 );
2200 double u2 = helper2.GetNodeU( edge2, vNode2 );
2201 bool isFirst1 = ( u1 < u2nodesMaps[0].begin()->first );
2202 bool isFirst2 = ( u2 < u2nodesMaps[1].begin()->first );
2203 bool reverse ( isFirst1 != isFirst2 );
2205 // associate matching nodes
2206 map< double, const SMDS_MeshNode* >::iterator u_Node1, u_Node2, end1;
2207 map< double, const SMDS_MeshNode* >::reverse_iterator uR_Node2;
2208 u_Node1 = u2nodesMaps[0].begin();
2209 u_Node2 = u2nodesMaps[1].begin();
2210 uR_Node2 = u2nodesMaps[1].rbegin();
2211 end1 = u2nodesMaps[0].end();
2212 for ( ; u_Node1 != end1; ++u_Node1 ) {
2213 const SMDS_MeshNode* n1 = u_Node1->second;
2214 const SMDS_MeshNode* n2 = ( reverse ? (uR_Node2++)->second : (u_Node2++)->second );
2215 node1To2Map.insert( make_pair( n1, n2 ));
2218 // associate matching nodes on the last vertices
2219 V2 = TopExp::LastVertex( TopoDS::Edge( edge2 ));
2220 if ( !assocMap.IsBound( V2, /*is2nd=*/true ))
2221 RETURN_BAD_RESULT("Association not found for vertex " << meshDS2->ShapeToIndex( V2 ));
2222 V1 = TopoDS::Vertex( assocMap( V2, /*is2nd=*/true ));
2223 vNode1 = SMESH_Algo::VertexNode( V1, meshDS1 );
2224 vNode2 = SMESH_Algo::VertexNode( V2, meshDS2 );
2225 if ( !vNode1 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS1->ShapeToIndex( V1 ));
2226 if ( !vNode2 ) RETURN_BAD_RESULT("No node on vertex #" << meshDS2->ShapeToIndex( V2 ));
2227 node1To2Map.insert( make_pair( vNode1, vNode2 ));
2230 // don't know why this condition is usually true :(
2231 // if ( node1To2Map.size() * quadFactor < SM1->NbNodes() )
2232 // MESSAGE("FindMatchingNodes() found too few node pairs starting from nodes ("
2233 // << vNode1->GetID() << " - " << eNode1[0]->GetID() << ") ("
2234 // << vNode2->GetID() << " - " << eNode2[0]->GetID() << "):"
2235 // << node1To2Map.size() * quadFactor << " < " << SM1->NbNodes());
2240 //================================================================================
2242 * Return any sub-shape of a face belonging to the outer wire
2243 * \param face - the face
2244 * \param type - type of sub-shape to return
2245 * \retval TopoDS_Shape - the found sub-shape
2247 //================================================================================
2249 TopoDS_Shape StdMeshers_ProjectionUtils::OuterShape( const TopoDS_Face& face,
2250 TopAbs_ShapeEnum type)
2252 TopExp_Explorer exp( BRepTools::OuterWire( face ), type );
2254 return exp.Current();
2255 return TopoDS_Shape();
2258 //================================================================================
2260 * Check that sub-mesh is computed and try to compute it if is not
2261 * \param sm - sub-mesh to compute
2262 * \param iterationNb - int used to stop infinite recursive call
2263 * \retval bool - true if computed
2265 //================================================================================
2267 bool StdMeshers_ProjectionUtils::MakeComputed(SMESH_subMesh * sm, const int iterationNb)
2269 if ( iterationNb > 10 )
2270 RETURN_BAD_RESULT("Infinite recursive projection");
2272 RETURN_BAD_RESULT("NULL submesh");
2273 if ( sm->IsMeshComputed() )
2276 SMESH_Mesh* mesh = sm->GetFather();
2277 SMESH_Gen* gen = mesh->GetGen();
2278 SMESH_Algo* algo = sm->GetAlgo();
2279 TopoDS_Shape shape = sm->GetSubShape();
2282 if ( shape.ShapeType() != TopAbs_COMPOUND )
2284 // No algo assigned to a non-compound sub-mesh.
2285 // Try to find an all-dimensional algo of an upper dimension
2286 int dim = gen->GetShapeDim( shape );
2287 for ( ++dim; ( dim <= 3 && !algo ); ++dim )
2289 SMESH_HypoFilter hypoFilter( SMESH_HypoFilter::IsAlgo() );
2290 hypoFilter.And( SMESH_HypoFilter::HasDim( dim ));
2291 list <const SMESHDS_Hypothesis * > hyps;
2292 list< TopoDS_Shape > assignedTo;
2294 mesh->GetHypotheses( shape, hypoFilter, hyps, true, &assignedTo );
2295 if ( nbAlgos > 1 ) // concurrent algos
2297 vector<SMESH_subMesh*> smList; // where an algo is assigned
2298 list< TopoDS_Shape >::iterator shapeIt = assignedTo.begin();
2299 for ( ; shapeIt != assignedTo.end(); ++shapeIt )
2300 smList.push_back( mesh->GetSubMesh( *shapeIt ));
2302 mesh->SortByMeshOrder( smList );
2303 algo = smList.front()->GetAlgo();
2304 shape = smList.front()->GetSubShape();
2306 else if ( nbAlgos == 1 )
2308 algo = (SMESH_Algo*) hyps.front();
2309 shape = assignedTo.front();
2318 bool computed = true;
2319 for ( TopoDS_Iterator grMember( shape ); grMember.More(); grMember.Next())
2320 if ( SMESH_subMesh* grSub = mesh->GetSubMesh( grMember.Value() ))
2321 if ( !MakeComputed( grSub, iterationNb + 1 ))
2327 string algoType = algo->GetName();
2328 if ( algoType.substr(0, 11) != "Projection_")
2329 return gen->Compute( *mesh, shape, SMESH_Gen::SHAPE_ONLY );
2331 // try to compute source mesh
2333 const list <const SMESHDS_Hypothesis *> & hyps =
2334 algo->GetUsedHypothesis( *mesh, shape );
2336 TopoDS_Shape srcShape;
2337 SMESH_Mesh* srcMesh = 0;
2338 list <const SMESHDS_Hypothesis*>::const_iterator hIt = hyps.begin();
2339 for ( ; srcShape.IsNull() && hIt != hyps.end(); ++hIt ) {
2340 string hypName = (*hIt)->GetName();
2341 if ( hypName == "ProjectionSource1D" ) {
2342 const StdMeshers_ProjectionSource1D * hyp =
2343 static_cast<const StdMeshers_ProjectionSource1D*>( *hIt );
2344 srcShape = hyp->GetSourceEdge();
2345 srcMesh = hyp->GetSourceMesh();
2347 else if ( hypName == "ProjectionSource2D" ) {
2348 const StdMeshers_ProjectionSource2D * hyp =
2349 static_cast<const StdMeshers_ProjectionSource2D*>( *hIt );
2350 srcShape = hyp->GetSourceFace();
2351 srcMesh = hyp->GetSourceMesh();
2353 else if ( hypName == "ProjectionSource3D" ) {
2354 const StdMeshers_ProjectionSource3D * hyp =
2355 static_cast<const StdMeshers_ProjectionSource3D*>( *hIt );
2356 srcShape = hyp->GetSource3DShape();
2357 srcMesh = hyp->GetSourceMesh();
2360 if ( srcShape.IsNull() ) // no projection source defined
2361 return gen->Compute( *mesh, shape, /*shapeOnly=*/true );
2363 if ( srcShape.IsSame( shape ))
2364 RETURN_BAD_RESULT("Projection from self");
2369 if ( MakeComputed( srcMesh->GetSubMesh( srcShape ), iterationNb + 1 ) &&
2370 gen->Compute( *mesh, shape, SMESH_Gen::SHAPE_ONLY ))
2371 return sm->IsMeshComputed();
2377 //================================================================================
2379 * Returns an error message to show in case if MakeComputed( sm ) fails.
2381 //================================================================================
2383 std::string StdMeshers_ProjectionUtils::SourceNotComputedError( SMESH_subMesh * sm,
2384 SMESH_Algo* projAlgo )
2386 const char usualMessage [] = "Source mesh not computed";
2388 return usualMessage;
2389 if ( !sm || sm->GetAlgoState() != SMESH_subMesh::NO_ALGO )
2390 return usualMessage; // algo is OK, anything else is KO.
2392 // Try to find a type of all-dimensional algorithm that would compute the
2393 // given sub-mesh if it could be launched before projection
2394 const TopoDS_Shape shape = sm->GetSubShape();
2395 const int shapeDim = SMESH_Gen::GetShapeDim( shape );
2397 for ( int dimIncrement = 1; shapeDim + dimIncrement < 4; ++dimIncrement )
2399 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
2400 filter.And( filter.HasDim( shapeDim + dimIncrement ));
2402 SMESH_Algo* algo = (SMESH_Algo*) sm->GetFather()->GetHypothesis( shape, filter, true );
2403 if ( algo && !algo->NeedDiscreteBoundary() )
2404 return SMESH_Comment("\"")
2405 << algo->GetFeatures()._label << "\""
2406 << " can't be used to compute the source mesh for \""
2407 << projAlgo->GetFeatures()._label << "\" in this case";
2409 return usualMessage;
2412 //================================================================================
2414 * Return a boundary EDGE (or all boundary EDGEs) of edgeContainer
2416 //================================================================================
2419 StdMeshers_ProjectionUtils::GetBoundaryEdge(const TopoDS_Shape& edgeContainer,
2420 const SMESH_Mesh& mesh,
2421 std::list< TopoDS_Edge >* allBndEdges)
2423 TopTools_IndexedMapOfShape facesOfEdgeContainer, facesNearEdge;
2424 TopExp::MapShapes( edgeContainer, TopAbs_FACE, facesOfEdgeContainer );
2426 if ( !facesOfEdgeContainer.IsEmpty() )
2427 for ( TopExp_Explorer exp(edgeContainer, TopAbs_EDGE); exp.More(); exp.Next() )
2429 const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
2430 facesNearEdge.Clear();
2431 PShapeIteratorPtr faceIt = SMESH_MesherHelper::GetAncestors( edge, mesh, TopAbs_FACE );
2432 while ( const TopoDS_Shape* face = faceIt->next() )
2433 if ( facesOfEdgeContainer.Contains( *face ))
2434 if ( facesNearEdge.Add( *face ) && facesNearEdge.Extent() > 1 )
2436 if ( facesNearEdge.Extent() == 1 ) {
2438 allBndEdges->push_back( edge );
2444 return TopoDS_Edge();
2448 namespace { // Definition of event listeners
2450 SMESH_subMeshEventListener* getSrcSubMeshListener();
2452 //================================================================================
2454 * \brief Listener that resets an event listener on source submesh when
2455 * "ProjectionSource*D" hypothesis is modified
2457 //================================================================================
2459 struct HypModifWaiter: SMESH_subMeshEventListener
2461 HypModifWaiter():SMESH_subMeshEventListener(false,// won't be deleted by submesh
2462 "StdMeshers_ProjectionUtils::HypModifWaiter") {}
2463 void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
2464 EventListenerData*, const SMESH_Hypothesis*)
2466 if ( event == SMESH_subMesh::MODIF_HYP &&
2467 eventType == SMESH_subMesh::ALGO_EVENT)
2469 // delete current source listener
2470 subMesh->DeleteEventListener( getSrcSubMeshListener() );
2471 // let algo set a new one
2472 if ( SMESH_Algo* algo = subMesh->GetAlgo() )
2473 algo->SetEventListener( subMesh );
2477 //================================================================================
2479 * \brief return static HypModifWaiter
2481 //================================================================================
2483 SMESH_subMeshEventListener* getHypModifWaiter() {
2484 static HypModifWaiter aHypModifWaiter;
2485 return &aHypModifWaiter;
2487 //================================================================================
2489 * \brief return static listener for source shape submeshes
2491 //================================================================================
2493 SMESH_subMeshEventListener* getSrcSubMeshListener() {
2494 static SMESH_subMeshEventListener srcListener(false, // won't be deleted by submesh
2495 "StdMeshers_ProjectionUtils::SrcSubMeshListener");
2496 return &srcListener;
2500 //================================================================================
2502 * Set event listeners to submesh with projection algo
2503 * \param subMesh - submesh with projection algo
2504 * \param srcShape - source shape
2505 * \param srcMesh - source mesh
2507 //================================================================================
2509 void StdMeshers_ProjectionUtils::SetEventListener(SMESH_subMesh* subMesh,
2510 TopoDS_Shape srcShape,
2511 SMESH_Mesh* srcMesh)
2513 // Set the listener that resets an event listener on source submesh when
2514 // "ProjectionSource*D" hypothesis is modified since source shape can be changed
2515 subMesh->SetEventListener( getHypModifWaiter(),0,subMesh);
2517 // Set an event listener to submesh of the source shape
2518 if ( !srcShape.IsNull() )
2521 srcMesh = subMesh->GetFather();
2523 SMESH_subMesh* srcShapeSM = srcMesh->GetSubMesh( srcShape );
2525 if ( srcShapeSM != subMesh ) {
2526 if ( srcShapeSM->GetSubMeshDS() &&
2527 srcShapeSM->GetSubMeshDS()->IsComplexSubmesh() )
2528 { // source shape is a group
2529 TopExp_Explorer it(srcShapeSM->GetSubShape(), // explore the group into sub-shapes...
2530 subMesh->GetSubShape().ShapeType()); // ...of target shape type
2531 for (; it.More(); it.Next())
2533 SMESH_subMesh* srcSM = srcMesh->GetSubMesh( it.Current() );
2534 if ( srcSM != subMesh )
2536 SMESH_subMeshEventListenerData* data =
2537 srcSM->GetEventListenerData(getSrcSubMeshListener());
2539 data->mySubMeshes.push_back( subMesh );
2541 data = SMESH_subMeshEventListenerData::MakeData( subMesh );
2542 subMesh->SetEventListener ( getSrcSubMeshListener(), data, srcSM );
2548 if ( SMESH_subMeshEventListenerData* data =
2549 srcShapeSM->GetEventListenerData( getSrcSubMeshListener() ))
2552 (std::find( data->mySubMeshes.begin(),
2553 data->mySubMeshes.end(), subMesh ) != data->mySubMeshes.end() );
2555 data->mySubMeshes.push_back( subMesh );
2559 subMesh->SetEventListener( getSrcSubMeshListener(),
2560 SMESH_subMeshEventListenerData::MakeData( subMesh ),
2568 namespace StdMeshers_ProjectionUtils
2571 //================================================================================
2573 * \brief Computes transformation between two sets of 2D points using
2574 * a least square approximation
2576 * See "Surface Mesh Projection For Hexahedral Mesh Generation By Sweeping"
2577 * by X.Roca, J.Sarrate, A.Huerta. (2.2)
2579 //================================================================================
2581 bool TrsfFinder2D::Solve( const vector< gp_XY >& srcPnts,
2582 const vector< gp_XY >& tgtPnts )
2584 // find gravity centers
2585 gp_XY srcGC( 0,0 ), tgtGC( 0,0 );
2586 for ( size_t i = 0; i < srcPnts.size(); ++i )
2588 srcGC += srcPnts[i];
2589 tgtGC += tgtPnts[i];
2591 srcGC /= srcPnts.size();
2592 tgtGC /= tgtPnts.size();
2596 math_Matrix mat (1,4,1,4, 0.);
2597 math_Vector vec (1,4, 0.);
2599 // cout << "m1 = smesh.Mesh('src')" << endl
2600 // << "m2 = smesh.Mesh('tgt')" << endl;
2601 double xx = 0, xy = 0, yy = 0;
2602 for ( size_t i = 0; i < srcPnts.size(); ++i )
2604 gp_XY srcUV = srcPnts[i] - srcGC;
2605 gp_XY tgtUV = tgtPnts[i] - tgtGC;
2606 xx += srcUV.X() * srcUV.X();
2607 yy += srcUV.Y() * srcUV.Y();
2608 xy += srcUV.X() * srcUV.Y();
2609 vec( 1 ) += srcUV.X() * tgtUV.X();
2610 vec( 2 ) += srcUV.Y() * tgtUV.X();
2611 vec( 3 ) += srcUV.X() * tgtUV.Y();
2612 vec( 4 ) += srcUV.Y() * tgtUV.Y();
2613 // cout << "m1.AddNode( " << srcUV.X() << ", " << srcUV.Y() << ", 0 )" << endl
2614 // << "m2.AddNode( " << tgtUV.X() << ", " << tgtUV.Y() << ", 0 )" << endl;
2616 mat( 1,1 ) = mat( 3,3 ) = xx;
2617 mat( 2,2 ) = mat( 4,4 ) = yy;
2618 mat( 1,2 ) = mat( 2,1 ) = mat( 3,4 ) = mat( 4,3 ) = xy;
2620 math_Gauss solver( mat );
2621 if ( !solver.IsDone() )
2623 solver.Solve( vec );
2624 if ( vec.Norm2() < gp::Resolution() )
2626 // cout << vec( 1 ) << "\t " << vec( 2 ) << endl
2627 // << vec( 3 ) << "\t " << vec( 4 ) << endl;
2629 _trsf.SetTranslationPart( tgtGC );
2632 gp_Mat2d& M = const_cast< gp_Mat2d& >( _trsf.VectorialPart());
2633 M( 1,1 ) = vec( 1 );
2634 M( 2,1 ) = vec( 2 ); // | 1 3 | -- is it correct ????????
2635 M( 1,2 ) = vec( 3 ); // | 2 4 |
2636 M( 2,2 ) = vec( 4 );
2641 //================================================================================
2643 * \brief Transforms a 2D points using a found transformation
2645 //================================================================================
2647 gp_XY TrsfFinder2D::Transform( const gp_Pnt2d& srcUV ) const
2649 gp_XY uv = srcUV.XY() - _srcOrig ;
2650 _trsf.Transforms( uv );
2654 //================================================================================
2656 * \brief Computes transformation between two sets of 3D points using
2657 * a least square approximation
2659 * See "Surface Mesh Projection For Hexahedral Mesh Generation By Sweeping"
2660 * by X.Roca, J.Sarrate, A.Huerta. (2.4)
2662 //================================================================================
2664 bool TrsfFinder3D::Solve( const vector< gp_XYZ > & srcPnts,
2665 const vector< gp_XYZ > & tgtPnts )
2667 // find gravity center
2668 gp_XYZ srcGC( 0,0,0 ), tgtGC( 0,0,0 );
2669 for ( size_t i = 0; i < srcPnts.size(); ++i )
2671 srcGC += srcPnts[i];
2672 tgtGC += tgtPnts[i];
2674 srcGC /= srcPnts.size();
2675 tgtGC /= tgtPnts.size();
2677 gp_XYZ srcOrig = 2 * srcGC - tgtGC;
2678 gp_XYZ tgtOrig = srcGC;
2682 math_Matrix mat (1,9,1,9, 0.);
2683 math_Vector vec (1,9, 0.);
2685 double xx = 0, yy = 0, zz = 0;
2686 double xy = 0, xz = 0, yz = 0;
2687 for ( size_t i = 0; i < srcPnts.size(); ++i )
2689 gp_XYZ src = srcPnts[i] - srcOrig;
2690 gp_XYZ tgt = tgtPnts[i] - tgtOrig;
2691 xx += src.X() * src.X();
2692 yy += src.Y() * src.Y();
2693 zz += src.Z() * src.Z();
2694 xy += src.X() * src.Y();
2695 xz += src.X() * src.Z();
2696 yz += src.Y() * src.Z();
2697 vec( 1 ) += src.X() * tgt.X();
2698 vec( 2 ) += src.Y() * tgt.X();
2699 vec( 3 ) += src.Z() * tgt.X();
2700 vec( 4 ) += src.X() * tgt.Y();
2701 vec( 5 ) += src.Y() * tgt.Y();
2702 vec( 6 ) += src.Z() * tgt.Y();
2703 vec( 7 ) += src.X() * tgt.Z();
2704 vec( 8 ) += src.Y() * tgt.Z();
2705 vec( 9 ) += src.Z() * tgt.Z();
2707 mat( 1,1 ) = mat( 4,4 ) = mat( 7,7 ) = xx;
2708 mat( 2,2 ) = mat( 5,5 ) = mat( 8,8 ) = yy;
2709 mat( 3,3 ) = mat( 6,6 ) = mat( 9,9 ) = zz;
2710 mat( 1,2 ) = mat( 2,1 ) = mat( 4,5 ) = mat( 5,4 ) = mat( 7,8 ) = mat( 8,7 ) = xy;
2711 mat( 1,3 ) = mat( 3,1 ) = mat( 4,6 ) = mat( 6,4 ) = mat( 7,9 ) = mat( 9,7 ) = xz;
2712 mat( 2,3 ) = mat( 3,2 ) = mat( 5,6 ) = mat( 6,5 ) = mat( 8,9 ) = mat( 9,8 ) = yz;
2714 math_Gauss solver( mat );
2715 if ( !solver.IsDone() )
2717 solver.Solve( vec );
2718 if ( vec.Norm2() < gp::Resolution() )
2721 // << vec( 1 ) << "\t " << vec( 2 ) << "\t " << vec( 3 ) << endl
2722 // << vec( 4 ) << "\t " << vec( 5 ) << "\t " << vec( 6 ) << endl
2723 // << vec( 7 ) << "\t " << vec( 8 ) << "\t " << vec( 9 ) << endl;
2726 _trsf.SetTranslationPart( tgtOrig );
2728 gp_Mat& M = const_cast< gp_Mat& >( _trsf.VectorialPart() );
2729 M.SetRows( gp_XYZ( vec( 1 ), vec( 2 ), vec( 3 )),
2730 gp_XYZ( vec( 4 ), vec( 5 ), vec( 6 )),
2731 gp_XYZ( vec( 7 ), vec( 8 ), vec( 9 )));
2735 //================================================================================
2737 * \brief Transforms a 3D point using a found transformation
2739 //================================================================================
2741 gp_XYZ TrsfFinder3D::Transform( const gp_Pnt& srcP ) const
2743 gp_XYZ p = srcP.XYZ() - _srcOrig;
2744 _trsf.Transforms( p );
2748 //================================================================================
2750 * \brief Transforms a 3D vector using a found transformation
2752 //================================================================================
2754 gp_XYZ TrsfFinder3D::TransformVec( const gp_Vec& v ) const
2756 return v.XYZ().Multiplied( _trsf.VectorialPart() );
2758 //================================================================================
2762 //================================================================================
2764 bool TrsfFinder3D::Invert()
2766 if (( _trsf.Form() == gp_Translation ) &&
2767 ( _srcOrig.X() != 0 || _srcOrig.Y() != 0 || _srcOrig.Z() != 0 ))
2769 // seems to be defined via Solve()
2770 gp_XYZ newSrcOrig = _trsf.TranslationPart();
2771 gp_Mat& M = const_cast< gp_Mat& >( _trsf.VectorialPart() );
2772 const double D = M.Determinant();
2773 if ( D < 1e-3 * ( newSrcOrig - _srcOrig ).Modulus() )
2776 cerr << "TrsfFinder3D::Invert()"
2777 << "D " << M.Determinant() << " IsSingular " << M.IsSingular() << endl;
2781 gp_Mat Minv = M.Inverted();
2782 _trsf.SetTranslationPart( _srcOrig );
2783 _srcOrig = newSrcOrig;
2793 //================================================================================
2795 * \brief Add in-FACE nodes surrounding a given node to a queue
2797 //================================================================================
2799 void Morph::AddCloseNodes( const SMDS_MeshNode* srcNode,
2800 const BRepMesh_Triangle* bmTria,
2801 const int srcFaceID,
2802 TNodeTriaList & noTriQueue )
2804 // find in-FACE nodes
2805 SMDS_ElemIteratorPtr elems = srcNode->GetInverseElementIterator(SMDSAbs_Face);
2806 while ( elems->more() )
2808 const SMDS_MeshElement* elem = elems->next();
2809 if ( elem->getshapeId() == srcFaceID )
2811 for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
2813 const SMDS_MeshNode* n = elem->GetNode( i );
2814 if ( !n->isMarked() /*&& n->getshapeId() == srcFaceID*/ )
2815 noTriQueue.push_back( make_pair( n, bmTria ));
2821 //================================================================================
2823 * \brief Find a delauney triangle containing a given 2D point and return
2824 * barycentric coordinates within the found triangle
2826 //================================================================================
2828 const BRepMesh_Triangle* Morph::FindTriangle( const gp_XY& uv,
2829 const BRepMesh_Triangle* bmTria,
2836 Standard_Boolean ori[3];
2842 _triaDS->ElementNodes( *bmTria, nodeIDs );
2843 nodeUVs[0] = _triaDS->GetNode( nodeIDs[0] ).Coord();
2844 nodeUVs[1] = _triaDS->GetNode( nodeIDs[1] ).Coord();
2845 nodeUVs[2] = _triaDS->GetNode( nodeIDs[2] ).Coord();
2847 SMESH_MeshAlgos::GetBarycentricCoords( uv,
2848 nodeUVs[0], nodeUVs[1], nodeUVs[2],
2850 if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
2852 bc[2] = 1 - bc[0] - bc[1];
2853 triaNodes[0] = nodeIDs[0];
2854 triaNodes[1] = nodeIDs[1];
2855 triaNodes[2] = nodeIDs[2];
2859 // look for a neighbor triangle, which is adjacent to a link intersected
2860 // by a segment( triangle center -> uv )
2862 gp_XY gc = ( nodeUVs[0] + nodeUVs[1] + nodeUVs[2] ) / 3.;
2863 gp_XY seg = uv - gc;
2865 bmTria->Edges( linkIDs, ori );
2866 int triaID = _triaDS->IndexOf( *bmTria );
2869 for ( int i = 0; i < 3; ++i )
2871 const BRepMesh_PairOfIndex & triIDs = _triaDS->ElementsConnectedTo( linkIDs[i] );
2872 if ( triIDs.Extent() < 2 )
2873 continue; // no neighbor triangle
2875 // check if a link intersects gc2uv
2876 const BRepMesh_Edge & link = _triaDS->GetLink( linkIDs[i] );
2877 const BRepMesh_Vertex & n1 = _triaDS->GetNode( link.FirstNode() );
2878 const BRepMesh_Vertex & n2 = _triaDS->GetNode( link.LastNode() );
2879 gp_XY uv1 = n1.Coord();
2880 gp_XY lin = n2.Coord() - uv1; // link direction
2882 double crossSegLin = seg ^ lin;
2883 if ( Abs( crossSegLin ) < std::numeric_limits<double>::min() )
2884 continue; // parallel
2886 double uSeg = ( uv1 - gc ) ^ lin / crossSegLin;
2887 if ( 0. <= uSeg && uSeg <= 1. )
2889 bmTria = & _triaDS->GetElement( triIDs.Index( 1 + ( triIDs.Index(1) == triaID )));
2897 //================================================================================
2899 * \brief Return a triangle sharing a given boundary node
2900 * \param [in] iBndNode - index of the boundary node
2901 * \return const BRepMesh_Triangle* - a found triangle
2903 //================================================================================
2905 const BRepMesh_Triangle* Morph::GetTriangleNear( int iBndNode )
2907 const BRepMesh::ListOfInteger & linkIds = _triaDS->LinksConnectedTo( iBndNode );
2908 const BRepMesh_PairOfIndex & triaIds = _triaDS->ElementsConnectedTo( linkIds.First() );
2909 const BRepMesh_Triangle& tria = _triaDS->GetElement( triaIds.Index(1) );
2913 //================================================================================
2915 * \brief triangulate the srcFace in 2D
2916 * \param [in] srcWires - boundary of the src FACE
2918 //================================================================================
2920 Morph::Morph(const TSideVector& srcWires)
2922 _srcSubMesh = srcWires[0]->GetMesh()->GetSubMesh( srcWires[0]->Face() );
2926 BRepAdaptor_Surface surf( srcWires[0]->Face() );
2927 const int nbDiv = 100;
2928 const double uRange = surf.LastUParameter() - surf.FirstUParameter();
2929 const double vRange = surf.LastVParameter() - surf.FirstVParameter();
2930 const double dU = uRange / nbDiv;
2931 const double dV = vRange / nbDiv;
2932 double u = surf.FirstUParameter(), v = surf.FirstVParameter();
2933 gp_Pnt p0U = surf.Value( u, v ), p0V = p0U;
2934 double lenU = 0, lenV = 0;
2935 for ( ; u < surf.LastUParameter(); u += dU, v += dV )
2937 gp_Pnt p1U = surf.Value( u, surf.FirstVParameter() );
2938 lenU += p1U.Distance( p0U );
2940 gp_Pnt p1V = surf.Value( surf.FirstUParameter(), v );
2941 lenV += p1V.Distance( p0V );
2944 _scale.SetCoord( lenU / uRange, lenV / vRange );
2947 // count boundary points
2948 int iP = 1, nbP = 0;
2949 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
2950 nbP += srcWires[iW]->NbPoints() - 1; // 1st and last points coincide
2952 _bndSrcNodes.resize( nbP + 1 ); _bndSrcNodes[0] = 0;
2954 // fill boundary points
2955 BRepMesh::Array1OfVertexOfDelaun srcVert( 1, 1 + nbP );
2956 BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
2957 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
2959 const UVPtStructVec& srcPnt = srcWires[iW]->GetUVPtStruct();
2960 for ( int i = 0, nb = srcPnt.size() - 1; i < nb; ++i, ++iP )
2962 _bndSrcNodes[ iP ] = srcPnt[i].node;
2963 srcPnt[i].node->setIsMarked( true );
2965 v.ChangeCoord() = srcPnt[i].UV().Multiplied( _scale );
2969 // triangulate the srcFace in 2D
2970 BRepMesh_Delaun delauney( srcVert );
2971 _triaDS = delauney.Result();
2974 //================================================================================
2976 * \brief Move non-marked target nodes
2977 * \param [in,out] tgtHelper - helper
2978 * \param [in] tgtWires - boundary nodes of the target FACE; must be in the
2979 * same order as the nodes in srcWires given in the constructor
2980 * \param [in] src2tgtNodes - map of src -> tgt nodes
2981 * \param [in] moveAll - to move all nodes; if \c false, move only non-marked nodes
2982 * \return bool - Ok or not
2984 //================================================================================
2986 bool Morph::Perform(SMESH_MesherHelper& tgtHelper,
2987 const TSideVector& tgtWires,
2988 Handle(ShapeAnalysis_Surface) tgtSurface,
2989 const TNodeNodeMap& src2tgtNodes,
2992 // get tgt boundary points corresponding to _bndSrcNodes
2994 for ( size_t iW = 0; iW < tgtWires.size(); ++iW )
2995 nbP += tgtWires[iW]->NbPoints() - 1; // 1st and last points coincide
2996 if ( nbP != _bndSrcNodes.size() - 1 )
2999 BRepMesh::Array1OfVertexOfDelaun tgtVert( 1, 1 + nbP );
3000 BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
3001 for ( size_t iW = 0, iP = 1; iW < tgtWires.size(); ++iW )
3003 const UVPtStructVec& tgtPnt = tgtWires[iW]->GetUVPtStruct();
3004 for ( int i = 0, nb = tgtPnt.size() - 1; i < nb; ++i, ++iP )
3006 v.ChangeCoord() = tgtPnt[i].UV().Multiplied( _scale );
3011 const TopoDS_Face& srcFace = TopoDS::Face( _srcSubMesh->GetSubShape() );
3012 const int srcFaceID = _srcSubMesh->GetId();
3013 SMESHDS_Mesh* tgtMesh = tgtHelper.GetMeshDS();
3014 const SMDS_MeshNode *srcNode, *tgtNode;
3015 const BRepMesh_Triangle *bmTria;
3017 // un-mark internal src nodes; later we will mark moved nodes
3019 SMDS_NodeIteratorPtr nIt = _srcSubMesh->GetSubMeshDS()->GetNodes();
3020 if ( !nIt || !nIt->more() ) return true;
3023 nbSrcNodes = _srcSubMesh->GetSubMeshDS()->NbNodes();
3024 while ( nIt->more() )
3025 nIt->next()->setIsMarked( false );
3029 while ( nIt->more() )
3030 nbSrcNodes += int( !nIt->next()->isMarked() );
3035 double bc[3]; // barycentric coordinates
3037 bool checkUV = true;
3038 const SMDS_FacePosition* pos;
3040 // a queue of nodes with starting triangles
3041 TNodeTriaList noTriQueue;
3042 size_t iBndSrcN = 1;
3044 while ( nbSrcNodes > 0 )
3046 while ( !noTriQueue.empty() )
3048 srcNode = noTriQueue.front().first;
3049 bmTria = noTriQueue.front().second;
3050 noTriQueue.pop_front();
3051 if ( srcNode->isMarked() )
3054 srcNode->setIsMarked( true );
3056 // find a delauney triangle containing the src node
3057 gp_XY uv = tgtHelper.GetNodeUV( srcFace, srcNode, NULL, &checkUV );
3059 bmTria = FindTriangle( uv, bmTria, bc, nodeIDs );
3063 // compute new coordinates for a corresponding tgt node
3064 gp_XY uvNew( 0., 0. ), nodeUV;
3065 for ( int i = 0; i < 3; ++i )
3066 uvNew += bc[i] * tgtVert( nodeIDs[i]).Coord();
3067 uvNew.SetCoord( uvNew.X() / _scale.X(),
3068 uvNew.Y() / _scale.Y() );
3069 gp_Pnt xyz = tgtSurface->Value( uvNew );
3071 // find and move tgt node
3072 TNodeNodeMap::const_iterator n2n = src2tgtNodes.find( srcNode );
3073 if ( n2n == src2tgtNodes.end() ) continue;
3074 tgtNode = n2n->second;
3075 tgtMesh->MoveNode( tgtNode, xyz.X(), xyz.Y(), xyz.Z() );
3077 if (( pos = dynamic_cast< const SMDS_FacePosition* >( tgtNode->GetPosition() )))
3078 const_cast<SMDS_FacePosition*>( pos )->SetParameters( uvNew.X(), uvNew.Y() );
3080 AddCloseNodes( srcNode, bmTria, srcFaceID, noTriQueue );
3083 if ( nbSrcNodes > 0 )
3085 // assure that all src nodes are visited
3086 for ( ; iBndSrcN < _bndSrcNodes.size() && noTriQueue.empty(); ++iBndSrcN )
3088 const BRepMesh_Triangle* tria = GetTriangleNear( iBndSrcN );
3089 AddCloseNodes( _bndSrcNodes[ iBndSrcN ], tria, srcFaceID, noTriQueue );
3091 if ( noTriQueue.empty() )
3093 SMDS_NodeIteratorPtr nIt = _srcSubMesh->GetSubMeshDS()->GetNodes();
3094 while ( nIt->more() )
3096 srcNode = nIt->next();
3097 if ( !srcNode->isMarked() )
3098 noTriQueue.push_back( make_pair( srcNode, bmTria ));
3108 gp_XY Morph::GetBndUV(const int iNode) const
3110 return _triaDS->GetNode( iNode ).Coord();
3114 } // namespace StdMeshers_ProjectionUtils