1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: SMESH_MesherHelper.cxx
24 // Created: 15.02.06 15:22:41
25 // Author: Sergey KUUL
27 #include "SMESH_MesherHelper.hxx"
29 #include "SMDS_EdgePosition.hxx"
30 #include "SMDS_FaceOfNodes.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_VolumeTool.hxx"
34 #include "SMESH_Block.hxx"
35 #include "SMESH_HypoFilter.hxx"
36 #include "SMESH_MeshAlgos.hxx"
37 #include "SMESH_ProxyMesh.hxx"
38 #include "SMESH_subMesh.hxx"
40 #include <BRepAdaptor_Curve.hxx>
41 #include <BRepAdaptor_Surface.hxx>
42 #include <BRepTools.hxx>
43 #include <BRep_Tool.hxx>
44 #include <Geom2d_Curve.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <GeomAPI_ProjectPointOnSurf.hxx>
47 #include <Geom_Curve.hxx>
48 #include <Geom_RectangularTrimmedSurface.hxx>
49 #include <Geom_Surface.hxx>
50 #include <ShapeAnalysis.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TopTools_MapIteratorOfMapOfShape.hxx>
55 #include <TopTools_MapOfShape.hxx>
58 #include <gp_Pnt2d.hxx>
59 #include <gp_Trsf.hxx>
61 #include <Standard_Failure.hxx>
62 #include <Standard_ErrorHandler.hxx>
64 #include <utilities.h>
70 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
74 inline SMESH_TNodeXYZ XYZ(const SMDS_MeshNode* n) { return SMESH_TNodeXYZ(n); }
76 enum { U_periodic = 1, V_periodic = 2 };
79 //================================================================================
83 //================================================================================
85 SMESH_MesherHelper::SMESH_MesherHelper(SMESH_Mesh& theMesh)
89 myCreateQuadratic(false),
90 myCreateBiQuadratic(false),
91 myFixNodeParameters(false)
93 myPar1[0] = myPar2[0] = myPar1[1] = myPar2[1] = 0;
94 mySetElemOnShape = ( ! myMesh->HasShapeToMesh() );
97 //=======================================================================
98 //function : ~SMESH_MesherHelper
100 //=======================================================================
102 SMESH_MesherHelper::~SMESH_MesherHelper()
105 TID2ProjectorOnSurf::iterator i_proj = myFace2Projector.begin();
106 for ( ; i_proj != myFace2Projector.end(); ++i_proj )
107 delete i_proj->second;
110 TID2ProjectorOnCurve::iterator i_proj = myEdge2Projector.begin();
111 for ( ; i_proj != myEdge2Projector.end(); ++i_proj )
112 delete i_proj->second;
116 //=======================================================================
117 //function : IsQuadraticSubMesh
118 //purpose : Check submesh for given shape: if all elements on this shape
119 // are quadratic, quadratic elements will be created.
120 // Also fill myTLinkNodeMap
121 //=======================================================================
123 bool SMESH_MesherHelper::IsQuadraticSubMesh(const TopoDS_Shape& aSh)
125 SMESHDS_Mesh* meshDS = GetMeshDS();
126 // we can create quadratic elements only if all elements
127 // created on sub-shapes of given shape are quadratic
128 // also we have to fill myTLinkNodeMap
129 myCreateQuadratic = true;
130 mySeamShapeIds.clear();
131 myDegenShapeIds.clear();
132 TopAbs_ShapeEnum subType( aSh.ShapeType()==TopAbs_FACE ? TopAbs_EDGE : TopAbs_FACE );
133 if ( aSh.ShapeType()==TopAbs_COMPOUND )
135 TopoDS_Iterator subIt( aSh );
137 subType = ( subIt.Value().ShapeType()==TopAbs_FACE ) ? TopAbs_EDGE : TopAbs_FACE;
139 SMDSAbs_ElementType elemType( subType==TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
142 int nbOldLinks = myTLinkNodeMap.size();
144 if ( !myMesh->HasShapeToMesh() )
146 if (( myCreateQuadratic = myMesh->NbFaces( ORDER_QUADRATIC )))
148 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
149 while ( fIt->more() )
150 AddTLinks( static_cast< const SMDS_MeshFace* >( fIt->next() ));
155 TopExp_Explorer exp( aSh, subType );
156 TopTools_MapOfShape checkedSubShapes;
157 for (; exp.More() && myCreateQuadratic; exp.Next()) {
158 if ( !checkedSubShapes.Add( exp.Current() ))
159 continue; // needed if aSh is compound of solids
160 if ( SMESHDS_SubMesh * subMesh = meshDS->MeshElements( exp.Current() )) {
161 if ( SMDS_ElemIteratorPtr it = subMesh->GetElements() ) {
163 const SMDS_MeshElement* e = it->next();
164 if ( e->GetType() != elemType || !e->IsQuadratic() ) {
165 myCreateQuadratic = false;
170 switch ( e->NbCornerNodes() ) {
172 AddTLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(2)); break;
174 AddTLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(3));
175 AddTLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(4));
176 AddTLinkNode(e->GetNode(2),e->GetNode(0),e->GetNode(5)); break;
178 AddTLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(4));
179 AddTLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(5));
180 AddTLinkNode(e->GetNode(2),e->GetNode(3),e->GetNode(6));
181 AddTLinkNode(e->GetNode(3),e->GetNode(0),e->GetNode(7));
184 myCreateQuadratic = false;
194 // if ( nbOldLinks == myTLinkNodeMap.size() ) -- 0023068
195 if ( myTLinkNodeMap.empty() )
196 myCreateQuadratic = false;
198 if ( !myCreateQuadratic )
199 myTLinkNodeMap.clear();
203 return myCreateQuadratic;
206 //=======================================================================
207 //function : SetSubShape
208 //purpose : Set geometry to make elements on
209 //=======================================================================
211 void SMESH_MesherHelper::SetSubShape(const int aShID)
213 if ( aShID == myShapeID )
216 SetSubShape( GetMeshDS()->IndexToShape( aShID ));
218 SetSubShape( TopoDS_Shape() );
221 //=======================================================================
222 //function : SetSubShape
223 //purpose : Set geometry to create elements on
224 //=======================================================================
226 void SMESH_MesherHelper::SetSubShape(const TopoDS_Shape& aSh)
228 if ( myShape.IsSame( aSh ))
232 mySeamShapeIds.clear();
233 myDegenShapeIds.clear();
235 if ( myShape.IsNull() ) {
239 SMESHDS_Mesh* meshDS = GetMeshDS();
240 myShapeID = meshDS->ShapeToIndex(aSh);
243 // treatment of periodic faces
244 for ( TopExp_Explorer eF( aSh, TopAbs_FACE ); eF.More(); eF.Next() )
246 const TopoDS_Face& face = TopoDS::Face( eF.Current() );
247 BRepAdaptor_Surface surf( face, false );
248 if ( surf.IsUPeriodic() || surf.IsUClosed() ) {
249 myParIndex |= U_periodic;
250 myPar1[0] = surf.FirstUParameter();
251 myPar2[0] = surf.LastUParameter();
253 if ( surf.IsVPeriodic() || surf.IsVClosed() ) {
254 myParIndex |= V_periodic;
255 myPar1[1] = surf.FirstVParameter();
256 myPar2[1] = surf.LastVParameter();
260 for (TopExp_Explorer exp( face, TopAbs_EDGE ); exp.More(); exp.Next())
262 // look for a "seam" edge, a real seam or an edge on period boundary
263 TopoDS_Edge edge = TopoDS::Edge( exp.Current() );
264 const int edgeID = meshDS->ShapeToIndex( edge );
267 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
268 const double du = Abs( uv1.Coord(1) - uv2.Coord(1) );
269 const double dv = Abs( uv1.Coord(2) - uv2.Coord(2) );
271 bool isSeam = BRep_Tool::IsClosed( edge, face );
272 if ( isSeam ) // real seam - having two pcurves on face
274 // pcurve can lie not on pediod boundary (22582, mesh_Quadratic_01/C9)
277 double u1 = uv1.Coord(1);
279 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
280 double u2 = uv1.Coord(1);
281 myPar1[0] = Min( u1, u2 );
282 myPar2[0] = Max( u1, u2 );
286 double v1 = uv1.Coord(2);
288 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
289 double v2 = uv1.Coord(2);
290 myPar1[1] = Min( v1, v2 );
291 myPar2[1] = Max( v1, v2 );
294 else //if ( !isSeam )
296 // one pcurve but on period boundary (22772, mesh_Quadratic_01/D1)
297 if (( myParIndex & U_periodic ) && du < Precision::PConfusion() )
299 isSeam = ( Abs( uv1.Coord(1) - myPar1[0] ) < Precision::PConfusion() ||
300 Abs( uv1.Coord(1) - myPar2[0] ) < Precision::PConfusion() );
302 else if (( myParIndex & V_periodic ) && dv < Precision::PConfusion() )
304 isSeam = ( Abs( uv1.Coord(2) - myPar1[1] ) < Precision::PConfusion() ||
305 Abs( uv1.Coord(2) - myPar2[1] ) < Precision::PConfusion() );
307 if ( isSeam ) // vertices are on period boundary, check a middle point (23032)
309 double f,l, r = 0.2345;
310 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface( edge, face, f, l );
311 uv2 = C2d->Value( f * r + l * ( 1.-r ));
312 if ( du < Precision::PConfusion() )
313 isSeam = ( Abs( uv1.Coord(1) - uv2.Coord(1) ) < Precision::PConfusion() );
315 isSeam = ( Abs( uv1.Coord(2) - uv2.Coord(2) ) < Precision::PConfusion() );
320 // store seam shape indices, negative if shape encounters twice ('real seam')
321 mySeamShapeIds.insert( IsSeamShape( edgeID ) ? -edgeID : edgeID );
322 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() ) {
323 int vertexID = meshDS->ShapeToIndex( v.Current() );
324 mySeamShapeIds.insert( IsSeamShape( vertexID ) ? -vertexID : vertexID );
328 // look for a degenerated edge
329 if ( SMESH_Algo::isDegenerated( edge )) {
330 myDegenShapeIds.insert( edgeID );
331 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() )
332 myDegenShapeIds.insert( meshDS->ShapeToIndex( v.Current() ));
334 if ( !BRep_Tool::SameParameter( edge ) ||
335 !BRep_Tool::SameRange( edge ))
337 setPosOnShapeValidity( edgeID, false );
343 //=======================================================================
344 //function : GetNodeUVneedInFaceNode
345 //purpose : Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
346 // Return true if the face is periodic.
347 // If F is Null, answer about sub-shape set through IsQuadraticSubMesh() or
349 //=======================================================================
351 bool SMESH_MesherHelper::GetNodeUVneedInFaceNode(const TopoDS_Face& F) const
353 if ( F.IsNull() ) return !mySeamShapeIds.empty();
355 if ( !F.IsNull() && !myShape.IsNull() && myShape.IsSame( F ))
356 return !mySeamShapeIds.empty();
359 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( F,loc );
360 if ( !aSurface.IsNull() )
361 return ( aSurface->IsUPeriodic() || aSurface->IsVPeriodic() );
366 //=======================================================================
367 //function : IsMedium
369 //=======================================================================
371 bool SMESH_MesherHelper::IsMedium(const SMDS_MeshNode* node,
372 const SMDSAbs_ElementType typeToCheck)
374 return SMESH_MeshEditor::IsMedium( node, typeToCheck );
377 //=======================================================================
378 //function : GetSubShapeByNode
379 //purpose : Return support shape of a node
380 //=======================================================================
382 TopoDS_Shape SMESH_MesherHelper::GetSubShapeByNode(const SMDS_MeshNode* node,
383 const SMESHDS_Mesh* meshDS)
385 int shapeID = node ? node->getshapeId() : 0;
386 if ( 0 < shapeID && shapeID <= meshDS->MaxShapeIndex() )
387 return meshDS->IndexToShape( shapeID );
389 return TopoDS_Shape();
393 //=======================================================================
394 //function : AddTLinkNode
395 //purpose : add a link in my data structure
396 //=======================================================================
398 void SMESH_MesherHelper::AddTLinkNode(const SMDS_MeshNode* n1,
399 const SMDS_MeshNode* n2,
400 const SMDS_MeshNode* n12)
402 // add new record to map
403 SMESH_TLink link( n1, n2 );
404 myTLinkNodeMap.insert( make_pair(link,n12));
407 //================================================================================
409 * \brief Add quadratic links of edge to own data structure
411 //================================================================================
413 bool SMESH_MesherHelper::AddTLinks(const SMDS_MeshEdge* edge)
415 if ( edge && edge->IsQuadratic() )
416 AddTLinkNode(edge->GetNode(0), edge->GetNode(1), edge->GetNode(2));
422 //================================================================================
424 * \brief Add quadratic links of face to own data structure
426 //================================================================================
428 bool SMESH_MesherHelper::AddTLinks(const SMDS_MeshFace* f)
432 switch ( f->NbNodes() ) {
434 // myMapWithCentralNode.insert
435 // ( make_pair( TBiQuad( f->GetNode(0),f->GetNode(1),f->GetNode(2) ),
437 // break; -- add medium nodes as well
439 AddTLinkNode(f->GetNode(0),f->GetNode(1),f->GetNode(3));
440 AddTLinkNode(f->GetNode(1),f->GetNode(2),f->GetNode(4));
441 AddTLinkNode(f->GetNode(2),f->GetNode(0),f->GetNode(5)); break;
444 // myMapWithCentralNode.insert
445 // ( make_pair( TBiQuad( f->GetNode(0),f->GetNode(1),f->GetNode(2),f->GetNode(3) ),
447 // break; -- add medium nodes as well
449 AddTLinkNode(f->GetNode(0),f->GetNode(1),f->GetNode(4));
450 AddTLinkNode(f->GetNode(1),f->GetNode(2),f->GetNode(5));
451 AddTLinkNode(f->GetNode(2),f->GetNode(3),f->GetNode(6));
452 AddTLinkNode(f->GetNode(3),f->GetNode(0),f->GetNode(7)); break;
459 //================================================================================
461 * \brief Add quadratic links of volume to own data structure
463 //================================================================================
465 bool SMESH_MesherHelper::AddTLinks(const SMDS_MeshVolume* volume)
467 if ( volume->IsQuadratic() )
469 SMDS_VolumeTool vTool( volume );
470 const SMDS_MeshNode** nodes = vTool.GetNodes();
472 for ( int iF = 1; iF < vTool.NbFaces(); ++iF )
474 const int nbN = vTool.NbFaceNodes( iF );
475 const int* iNodes = vTool.GetFaceNodesIndices( iF );
476 for ( int i = 0; i < nbN; )
478 int iN1 = iNodes[i++];
479 int iN12 = iNodes[i++];
481 if ( iN1 > iN2 ) std::swap( iN1, iN2 );
482 int linkID = iN1 * vTool.NbNodes() + iN2;
483 pair< set<int>::iterator, bool > it_isNew = addedLinks.insert( linkID );
484 if ( it_isNew.second )
485 AddTLinkNode( nodes[iN1], nodes[iN2], nodes[iN12] );
487 addedLinks.erase( it_isNew.first ); // each link encounters only twice
489 if ( vTool.NbNodes() == 27 )
491 const SMDS_MeshNode* nFCenter = nodes[ vTool.GetCenterNodeIndex( iF )];
492 if ( nFCenter->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
493 myMapWithCentralNode.insert
494 ( make_pair( TBiQuad( nodes[ iNodes[0]], nodes[ iNodes[1]],
495 nodes[ iNodes[2]], nodes[ iNodes[3]] ),
504 //================================================================================
506 * \brief Return true if position of nodes on the shape hasn't yet been checked or
507 * the positions proved to be invalid
509 //================================================================================
511 bool SMESH_MesherHelper::toCheckPosOnShape(int shapeID ) const
513 map< int,bool >::const_iterator id_ok = myNodePosShapesValidity.find( shapeID );
514 return ( id_ok == myNodePosShapesValidity.end() || !id_ok->second );
517 //================================================================================
519 * \brief Set validity of positions of nodes on the shape.
520 * Once set, validity is not changed
522 //================================================================================
524 void SMESH_MesherHelper::setPosOnShapeValidity(int shapeID, bool ok ) const
526 std::map< int,bool >::iterator sh_ok =
527 ((SMESH_MesherHelper*)this)->myNodePosShapesValidity.insert( make_pair( shapeID, ok)).first;
532 //=======================================================================
533 //function : ToFixNodeParameters
534 //purpose : Enables fixing node parameters on EDGEs and FACEs in
535 // GetNodeU(...,check=true), GetNodeUV(...,check=true), CheckNodeUV() and
536 // CheckNodeU() in case if a node lies on a shape set via SetSubShape().
538 //=======================================================================
540 void SMESH_MesherHelper::ToFixNodeParameters(bool toFix)
542 myFixNodeParameters = toFix;
546 //=======================================================================
547 //function : getUVOnSeam
548 //purpose : Select UV on either of 2 pcurves of a seam edge, closest to the given UV
549 //=======================================================================
551 gp_Pnt2d SMESH_MesherHelper::getUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const
553 gp_Pnt2d result = uv1;
554 for ( int i = U_periodic; i <= V_periodic ; ++i )
556 if ( myParIndex & i )
558 double p1 = uv1.Coord( i );
559 double dp1 = Abs( p1-myPar1[i-1]), dp2 = Abs( p1-myPar2[i-1]);
560 if ( myParIndex == i ||
561 dp1 < ( myPar2[i-1] - myPar1[i-1] ) / 100. ||
562 dp2 < ( myPar2[i-1] - myPar1[i-1] ) / 100. )
564 double p2 = uv2.Coord( i );
565 double p1Alt = ( dp1 < dp2 ) ? myPar2[i-1] : myPar1[i-1];
566 if ( Abs( p2 - p1 ) > Abs( p2 - p1Alt ))
567 result.SetCoord( i, p1Alt );
574 //=======================================================================
575 //function : GetNodeUV
576 //purpose : Return node UV on face
577 //=======================================================================
579 gp_XY SMESH_MesherHelper::GetNodeUV(const TopoDS_Face& F,
580 const SMDS_MeshNode* n,
581 const SMDS_MeshNode* n2,
584 gp_Pnt2d uv( Precision::Infinite(), Precision::Infinite() );
586 const SMDS_PositionPtr Pos = n->GetPosition();
588 if ( Pos->GetTypeOfPosition() == SMDS_TOP_FACE )
590 // node has position on face
591 const SMDS_FacePosition* fpos = static_cast<const SMDS_FacePosition*>( Pos );
592 uv.SetCoord( fpos->GetUParameter(), fpos->GetVParameter() );
594 uvOK = CheckNodeUV( F, n, uv.ChangeCoord(), 2.*getFaceMaxTol( F )); // 2. from 22830
596 else if ( Pos->GetTypeOfPosition() == SMDS_TOP_EDGE )
598 // node has position on EDGE => it is needed to find
599 // corresponding EDGE from FACE, get pcurve for this
600 // EDGE and retrieve value from this pcurve
601 const SMDS_EdgePosition* epos = static_cast<const SMDS_EdgePosition*>( Pos );
602 const int edgeID = n->getshapeId();
603 const TopoDS_Edge& E = TopoDS::Edge( GetMeshDS()->IndexToShape( edgeID ));
604 double f, l, u = epos->GetUParameter();
605 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface( E, F, f, l );
606 bool validU = ( !C2d.IsNull() && ( f < u ) && ( u < l ));
607 if ( validU ) uv = C2d->Value( u );
608 else uv.SetCoord( Precision::Infinite(),0.);
609 if ( check || !validU )
610 uvOK = CheckNodeUV( F, n, uv.ChangeCoord(), 2.*getFaceMaxTol( F ),/*force=*/ !validU );
612 // for a node on a seam EDGE select one of UVs on 2 pcurves
613 if ( n2 && IsSeamShape( edgeID ))
615 uv = getUVOnSeam( uv, GetNodeUV( F, n2, 0, check ));
618 { // adjust uv to period
620 Handle(Geom_Surface) S = BRep_Tool::Surface(F,loc);
621 Standard_Boolean isUPeriodic = S->IsUPeriodic();
622 Standard_Boolean isVPeriodic = S->IsVPeriodic();
624 if ( isUPeriodic || isVPeriodic ) {
625 Standard_Real UF,UL,VF,VL;
626 S->Bounds(UF,UL,VF,VL);
627 if ( isUPeriodic ) newUV.SetX( uv.X() + ShapeAnalysis::AdjustToPeriod(uv.X(),UF,UL));
628 if ( isVPeriodic ) newUV.SetY( uv.Y() + ShapeAnalysis::AdjustToPeriod(uv.Y(),VF,VL));
632 gp_Pnt2d uv2 = GetNodeUV( F, n2, 0, check );
633 if ( isUPeriodic && Abs( uv.X()-uv2.X() ) < Abs( newUV.X()-uv2.X() ))
634 newUV.SetX( uv.X() );
635 if ( isVPeriodic && Abs( uv.Y()-uv2.Y() ) < Abs( newUV.Y()-uv2.Y() ))
636 newUV.SetY( uv.Y() );
642 else if ( Pos->GetTypeOfPosition() == SMDS_TOP_VERTEX )
644 if ( int vertexID = n->getshapeId() ) {
645 const TopoDS_Vertex& V = TopoDS::Vertex(GetMeshDS()->IndexToShape(vertexID));
647 uv = BRep_Tool::Parameters( V, F );
650 catch (Standard_Failure& exc) {
654 if ( !IsSubShape( V, F ))
656 MESSAGE ( "SMESH_MesherHelper::GetNodeUV(); Vertex " << vertexID
657 << " not in face " << GetMeshDS()->ShapeToIndex( F ) );
658 // get UV of a vertex closest to the node
660 gp_Pnt pn = XYZ( n );
661 for ( TopExp_Explorer vert( F,TopAbs_VERTEX ); !uvOK && vert.More(); vert.Next() ) {
662 TopoDS_Vertex curV = TopoDS::Vertex( vert.Current() );
663 gp_Pnt p = BRep_Tool::Pnt( curV );
664 double curDist = p.SquareDistance( pn );
665 if ( curDist < dist ) {
667 uv = BRep_Tool::Parameters( curV, F );
668 uvOK = ( dist < DBL_MIN );
675 TopTools_ListIteratorOfListOfShape it( myMesh->GetAncestors( V ));
676 for ( ; it.More(); it.Next() ) {
677 if ( it.Value().ShapeType() == TopAbs_EDGE ) {
678 const TopoDS_Edge & edge = TopoDS::Edge( it.Value() );
680 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edge, F, f, l);
681 if ( !C2d.IsNull() ) {
682 double u = ( V == IthVertex( 0, edge )) ? f : l;
683 uv = C2d->Value( u );
689 if ( !uvOK && V.Orientation() == TopAbs_INTERNAL )
691 Handle(ShapeAnalysis_Surface) projector = GetSurface( F );
692 if ( n2 ) uv = GetNodeUV( F, n2 );
693 if ( Precision::IsInfinite( uv.X() ))
694 uv = projector->NextValueOfUV( uv, BRep_Tool::Pnt( V ), BRep_Tool::Tolerance( F ));
696 uv = projector->ValueOfUV( BRep_Tool::Pnt( V ), BRep_Tool::Tolerance( F ));
697 uvOK = ( projector->Gap() < getFaceMaxTol( F ));
701 if ( n2 && IsSeamShape( vertexID ))
703 bool isSeam = ( myShape.IsSame( F ));
705 SMESH_MesherHelper h( *myMesh );
707 isSeam = IsSeamShape( vertexID );
711 uv = getUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
717 uvOK = CheckNodeUV( F, n, uv.ChangeCoord(), 2.*getFaceMaxTol( F ));
720 if ( check && !uvOK )
726 //=======================================================================
727 //function : CheckNodeUV
728 //purpose : Check and fix node UV on a face
729 //=======================================================================
731 bool SMESH_MesherHelper::CheckNodeUV(const TopoDS_Face& F,
732 const SMDS_MeshNode* n,
736 double distXYZ[4]) const
738 int shapeID = n->getshapeId();
740 if (( infinit = ( Precision::IsInfinite( uv.X() ) || Precision::IsInfinite( uv.Y() ))) ||
742 ( uv.X() == 0. && uv.Y() == 0. ) ||
743 ( toCheckPosOnShape( shapeID )))
745 // check that uv is correct
747 Handle(Geom_Surface) surface = BRep_Tool::Surface( F,loc );
748 gp_Pnt nodePnt = XYZ( n ), surfPnt(0,0,0);
750 if ( !loc.IsIdentity() ) nodePnt.Transform( loc.Transformation().Inverted() );
752 (dist = nodePnt.Distance( surfPnt = surface->Value( uv.X(), uv.Y() ))) > tol )
754 setPosOnShapeValidity( shapeID, false );
755 if ( !infinit && distXYZ ) {
756 surfPnt.Transform( loc );
758 distXYZ[1] = surfPnt.X(); distXYZ[2] = surfPnt.Y(); distXYZ[3]=surfPnt.Z();
760 // uv incorrect, project the node to surface
761 GeomAPI_ProjectPointOnSurf& projector = GetProjector( F, loc, tol );
762 projector.Perform( nodePnt );
763 if ( !projector.IsDone() || projector.NbPoints() < 1 )
765 MESSAGE( "SMESH_MesherHelper::CheckNodeUV() failed to project" );
768 Quantity_Parameter U,V;
769 projector.LowerDistanceParameters(U,V);
771 surfPnt = surface->Value( U, V );
772 dist = nodePnt.Distance( surfPnt );
774 surfPnt.Transform( loc );
776 distXYZ[1] = surfPnt.X(); distXYZ[2] = surfPnt.Y(); distXYZ[3]=surfPnt.Z();
780 MESSAGE( "SMESH_MesherHelper::CheckNodeUV(), invalid projection" );
783 // store the fixed UV on the face
784 if ( myShape.IsSame(F) && shapeID == myShapeID && myFixNodeParameters )
785 const_cast<SMDS_MeshNode*>(n)->SetPosition
786 ( SMDS_PositionPtr( new SMDS_FacePosition( U, V )));
788 else if ( myShape.IsSame(F) && uv.Modulus() > numeric_limits<double>::min() )
790 setPosOnShapeValidity( shapeID, true );
796 //=======================================================================
797 //function : GetProjector
798 //purpose : Return projector intitialized by given face without location, which is returned
799 //=======================================================================
801 GeomAPI_ProjectPointOnSurf& SMESH_MesherHelper::GetProjector(const TopoDS_Face& F,
802 TopLoc_Location& loc,
805 Handle(Geom_Surface) surface = BRep_Tool::Surface( F,loc );
806 int faceID = GetMeshDS()->ShapeToIndex( F );
807 TID2ProjectorOnSurf& i2proj = const_cast< TID2ProjectorOnSurf&>( myFace2Projector );
808 TID2ProjectorOnSurf::iterator i_proj = i2proj.find( faceID );
809 if ( i_proj == i2proj.end() )
811 if ( tol == 0 ) tol = BRep_Tool::Tolerance( F );
812 double U1, U2, V1, V2;
813 surface->Bounds(U1, U2, V1, V2);
814 GeomAPI_ProjectPointOnSurf* proj = new GeomAPI_ProjectPointOnSurf();
815 proj->Init( surface, U1, U2, V1, V2, tol );
816 i_proj = i2proj.insert( make_pair( faceID, proj )).first;
818 return *( i_proj->second );
821 //=======================================================================
822 //function : GetSurface
823 //purpose : Return a cached ShapeAnalysis_Surface of a FACE
824 //=======================================================================
826 Handle(ShapeAnalysis_Surface) SMESH_MesherHelper::GetSurface(const TopoDS_Face& F ) const
828 Handle(Geom_Surface) surface = BRep_Tool::Surface( F );
829 int faceID = GetMeshDS()->ShapeToIndex( F );
830 TID2Surface::iterator i_surf = myFace2Surface.find( faceID );
831 if ( i_surf == myFace2Surface.end() && faceID )
833 Handle(ShapeAnalysis_Surface) surf( new ShapeAnalysis_Surface( surface ));
834 i_surf = myFace2Surface.insert( make_pair( faceID, surf )).first;
836 return i_surf->second;
841 gp_XY AverageUV(const gp_XY& uv1, const gp_XY& uv2) { return ( uv1 + uv2 ) / 2.; }
842 gp_XY_FunPtr(Added); // define gp_XY_Added pointer to function calling gp_XY::Added(gp_XY)
843 gp_XY_FunPtr(Subtracted);
846 //=======================================================================
847 //function : ApplyIn2D
848 //purpose : Perform given operation on two 2d points in parameric space of given surface.
849 // It takes into account period of the surface. Use gp_XY_FunPtr macro
850 // to easily define pointer to function of gp_XY class.
851 //=======================================================================
853 gp_XY SMESH_MesherHelper::ApplyIn2D(Handle(Geom_Surface) surface,
857 const bool resultInPeriod)
859 if ( surface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface )))
860 surface = Handle(Geom_RectangularTrimmedSurface)::DownCast( surface )->BasisSurface();
861 Standard_Boolean isUPeriodic = surface.IsNull() ? false : surface->IsUPeriodic();
862 Standard_Boolean isVPeriodic = surface.IsNull() ? false : surface->IsVPeriodic();
863 if ( !isUPeriodic && !isVPeriodic )
866 // move uv2 not far than half-period from uv1
868 uv2.X()+(isUPeriodic ? ShapeAnalysis::AdjustByPeriod(uv2.X(),uv1.X(),surface->UPeriod()) :0);
870 uv2.Y()+(isVPeriodic ? ShapeAnalysis::AdjustByPeriod(uv2.Y(),uv1.Y(),surface->VPeriod()) :0);
873 gp_XY res = fun( uv1, gp_XY(u2,v2) );
875 // move result within period
876 if ( resultInPeriod )
878 Standard_Real UF,UL,VF,VL;
879 surface->Bounds(UF,UL,VF,VL);
881 res.SetX( res.X() + ShapeAnalysis::AdjustToPeriod(res.X(),UF,UL));
883 res.SetY( res.Y() + ShapeAnalysis::AdjustToPeriod(res.Y(),VF,VL));
889 //=======================================================================
890 //function : AdjustByPeriod
891 //purpose : Move node positions on a FACE within surface period
892 //=======================================================================
894 void SMESH_MesherHelper::AdjustByPeriod( const TopoDS_Face& face, gp_XY uv[], const int nbUV )
896 SMESH_MesherHelper h( *myMesh ), *ph = face.IsSame( myShape ) ? this : &h;
897 ph->SetSubShape( face );
899 for ( int iCoo = U_periodic; iCoo <= V_periodic; ++iCoo )
900 if ( ph->GetPeriodicIndex() & iCoo )
902 const double period = ( ph->myPar2[iCoo-1] - ph->myPar1[iCoo-1] );
903 const double xRef = uv[0].Coord( iCoo );
904 for ( int i = 1; i < nbUV; ++i )
906 double x = uv[i].Coord( iCoo );
907 double dx = ShapeAnalysis::AdjustByPeriod( x, xRef, period );
908 uv[i].SetCoord( iCoo, x + dx );
913 //=======================================================================
914 //function : GetMiddleUV
915 //purpose : Return middle UV taking in account surface period
916 //=======================================================================
918 gp_XY SMESH_MesherHelper::GetMiddleUV(const Handle(Geom_Surface)& surface,
923 // the proper place of getting basic surface seems to be in ApplyIn2D()
924 // but we put it here to decrease a risk of regressions just before releasing a version
925 // Handle(Geom_Surface) surf = surface;
926 // while ( !surf.IsNull() && surf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface )))
927 // surf = Handle(Geom_RectangularTrimmedSurface)::DownCast( surf )->BasisSurface();
929 return ApplyIn2D( surface, p1, p2, & AverageUV );
932 //=======================================================================
933 //function : GetCenterUV
934 //purpose : Return UV for the central node of a biquadratic triangle
935 //=======================================================================
937 gp_XY SMESH_MesherHelper::GetCenterUV(const gp_XY& uv1,
943 bool * isBadTria/*=0*/)
946 gp_XY uvAvg = ( uv12 + uv23 + uv31 ) / 3.;
948 if (( badTria = (( uvAvg - uv1 ) * ( uvAvg - uv23 ) > 0 )))
949 uvAvg = ( uv1 + uv23 ) / 2.;
950 else if (( badTria = (( uvAvg - uv2 ) * ( uvAvg - uv31 ) > 0 )))
951 uvAvg = ( uv2 + uv31 ) / 2.;
952 else if (( badTria = (( uvAvg - uv3 ) * ( uvAvg - uv12 ) > 0 )))
953 uvAvg = ( uv3 + uv12 ) / 2.;
956 *isBadTria = badTria;
960 //=======================================================================
961 //function : GetNodeU
962 //purpose : Return node U on edge
963 //=======================================================================
965 double SMESH_MesherHelper::GetNodeU(const TopoDS_Edge& E,
966 const SMDS_MeshNode* n,
967 const SMDS_MeshNode* inEdgeNode,
970 double param = Precision::Infinite();
972 const SMDS_PositionPtr pos = n->GetPosition();
973 if ( pos->GetTypeOfPosition()==SMDS_TOP_EDGE )
975 const SMDS_EdgePosition* epos = static_cast<const SMDS_EdgePosition*>( pos );
976 param = epos->GetUParameter();
978 else if( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX )
980 if ( inEdgeNode && TopExp::FirstVertex( E ).IsSame( TopExp::LastVertex( E ))) // issue 0020128
983 BRep_Tool::Range( E, f,l );
984 double uInEdge = GetNodeU( E, inEdgeNode );
985 param = ( fabs( uInEdge - f ) < fabs( l - uInEdge )) ? f : l;
989 SMESHDS_Mesh * meshDS = GetMeshDS();
990 int vertexID = n->getshapeId();
991 const TopoDS_Vertex& V = TopoDS::Vertex(meshDS->IndexToShape(vertexID));
992 param = BRep_Tool::Parameter( V, E );
997 double tol = BRep_Tool::Tolerance( E );
998 double f,l; BRep_Tool::Range( E, f,l );
999 bool force = ( param < f-tol || param > l+tol );
1000 if ( !force && pos->GetTypeOfPosition()==SMDS_TOP_EDGE )
1001 force = ( GetMeshDS()->ShapeToIndex( E ) != n->getshapeId() );
1003 *check = CheckNodeU( E, n, param, 2*tol, force );
1008 //=======================================================================
1009 //function : CheckNodeU
1010 //purpose : Check and fix node U on an edge
1011 // Return false if U is bad and could not be fixed
1012 //=======================================================================
1014 bool SMESH_MesherHelper::CheckNodeU(const TopoDS_Edge& E,
1015 const SMDS_MeshNode* n,
1019 double distXYZ[4]) const
1021 int shapeID = n->getshapeId();
1023 if (( infinit = Precision::IsInfinite( u )) ||
1026 ( toCheckPosOnShape( shapeID )))
1028 TopLoc_Location loc; double f,l;
1029 Handle(Geom_Curve) curve = BRep_Tool::Curve( E,loc,f,l );
1030 if ( curve.IsNull() ) // degenerated edge
1032 if ( u+tol < f || u-tol > l )
1034 double r = Max( 0.5, 1 - tol*n->GetID()); // to get a unique u on edge
1040 gp_Pnt nodePnt = SMESH_TNodeXYZ( n );
1041 if ( !loc.IsIdentity() ) nodePnt.Transform( loc.Transformation().Inverted() );
1043 double dist = 2*tol;
1046 curvPnt = curve->Value( u );
1047 dist = nodePnt.Distance( curvPnt );
1049 curvPnt.Transform( loc );
1051 distXYZ[1] = curvPnt.X(); distXYZ[2] = curvPnt.Y(); distXYZ[3]=curvPnt.Z();
1056 setPosOnShapeValidity( shapeID, false );
1057 // u incorrect, project the node to the curve
1058 int edgeID = GetMeshDS()->ShapeToIndex( E );
1059 TID2ProjectorOnCurve& i2proj = const_cast< TID2ProjectorOnCurve&>( myEdge2Projector );
1060 TID2ProjectorOnCurve::iterator i_proj =
1061 i2proj.insert( make_pair( edgeID, (GeomAPI_ProjectPointOnCurve*) 0 )).first;
1062 if ( !i_proj->second )
1064 i_proj->second = new GeomAPI_ProjectPointOnCurve();
1065 i_proj->second->Init( curve, f, l );
1067 GeomAPI_ProjectPointOnCurve* projector = i_proj->second;
1068 projector->Perform( nodePnt );
1069 if ( projector->NbPoints() < 1 )
1071 MESSAGE( "SMESH_MesherHelper::CheckNodeU() failed to project" );
1074 Quantity_Parameter U = projector->LowerDistanceParameter();
1076 MESSAGE(" f " << f << " l " << l << " u " << u);
1077 curvPnt = curve->Value( u );
1078 dist = nodePnt.Distance( curvPnt );
1080 curvPnt.Transform( loc );
1082 distXYZ[1] = curvPnt.X(); distXYZ[2] = curvPnt.Y(); distXYZ[3]=curvPnt.Z();
1086 MESSAGE( "SMESH_MesherHelper::CheckNodeU(), invalid projection" );
1087 MESSAGE("distance " << dist << " " << tol );
1090 // store the fixed U on the edge
1091 if ( myShape.IsSame(E) && shapeID == myShapeID && myFixNodeParameters )
1092 const_cast<SMDS_MeshNode*>(n)->SetPosition
1093 ( SMDS_PositionPtr( new SMDS_EdgePosition( U )));
1095 else if ( fabs( u ) > numeric_limits<double>::min() )
1097 setPosOnShapeValidity( shapeID, true );
1099 if (( u < f-tol || u > l+tol ) && force )
1101 MESSAGE("u < f-tol || u > l+tol ; u " << u << " f " << f << " l " << l);
1102 // node is on vertex but is set on periodic but trimmed edge (issue 0020890)
1105 // do not use IsPeriodic() as Geom_TrimmedCurve::IsPeriodic () returns false
1106 double period = curve->Period();
1107 u = ( u < f ) ? u + period : u - period;
1109 catch (Standard_Failure& exc)
1119 //=======================================================================
1120 //function : GetMediumPos
1121 //purpose : Return index and type of the shape (EDGE or FACE only) to
1122 // set a medium node on
1123 //param : useCurSubShape - if true, returns the shape set via SetSubShape()
1125 //param : expectedSupport - shape type corresponding to element being created,
1126 // e.g TopAbs_EDGE if SMDSAbs_Edge is created
1127 // basing on \a n1 and \a n2
1128 // Calling GetMediumPos() with useCurSubShape=true is OK only for the
1129 // case where the lower dim mesh is already constructed and converted to quadratic,
1130 // else, nodes on EDGEs are assigned to FACE, for example.
1131 //=======================================================================
1133 std::pair<int, TopAbs_ShapeEnum>
1134 SMESH_MesherHelper::GetMediumPos(const SMDS_MeshNode* n1,
1135 const SMDS_MeshNode* n2,
1136 const bool useCurSubShape,
1137 TopAbs_ShapeEnum expectedSupport)
1139 if ( useCurSubShape && !myShape.IsNull() )
1140 return std::make_pair( myShapeID, myShape.ShapeType() );
1142 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
1146 if (( myShapeID == n1->getshapeId() || myShapeID == n2->getshapeId() ) && myShapeID > 0 )
1148 shapeType = myShape.ShapeType();
1149 shapeID = myShapeID;
1151 else if ( n1->getshapeId() == n2->getshapeId() )
1153 shapeID = n2->getshapeId();
1154 shape = GetSubShapeByNode( n1, GetMeshDS() );
1156 else // 2 different shapes
1158 const SMDS_TypeOfPosition Pos1 = n1->GetPosition()->GetTypeOfPosition();
1159 const SMDS_TypeOfPosition Pos2 = n2->GetPosition()->GetTypeOfPosition();
1161 if ( Pos1 == SMDS_TOP_3DSPACE || Pos2 == SMDS_TOP_3DSPACE )
1165 else if ( Pos1 == SMDS_TOP_FACE || Pos2 == SMDS_TOP_FACE )
1168 if ( Pos1 != SMDS_TOP_FACE || Pos2 != SMDS_TOP_FACE ) // not 2 FACEs
1170 if ( Pos1 != SMDS_TOP_FACE ) std::swap( n1,n2 );
1171 TopoDS_Shape F = GetSubShapeByNode( n1, GetMeshDS() );
1172 TopoDS_Shape S = GetSubShapeByNode( n2, GetMeshDS() );
1173 if ( IsSubShape( S, F ))
1175 shapeType = TopAbs_FACE;
1176 shapeID = n1->getshapeId();
1180 else if ( Pos1 == SMDS_TOP_EDGE && Pos2 == SMDS_TOP_EDGE )
1182 TopoDS_Shape E1 = GetSubShapeByNode( n1, GetMeshDS() );
1183 TopoDS_Shape E2 = GetSubShapeByNode( n2, GetMeshDS() );
1184 shape = GetCommonAncestor( E1, E2, *myMesh, TopAbs_FACE );
1186 else if ( Pos1 == SMDS_TOP_VERTEX && Pos2 == SMDS_TOP_VERTEX )
1188 TopoDS_Shape V1 = GetSubShapeByNode( n1, GetMeshDS() );
1189 TopoDS_Shape V2 = GetSubShapeByNode( n2, GetMeshDS() );
1190 shape = GetCommonAncestor( V1, V2, *myMesh, TopAbs_EDGE );
1191 if ( shape.IsNull() ) shape = GetCommonAncestor( V1, V2, *myMesh, TopAbs_FACE );
1193 else // on VERTEX and EDGE
1195 if ( Pos1 != SMDS_TOP_VERTEX ) std::swap( n1,n2 );
1196 TopoDS_Shape V = GetSubShapeByNode( n1, GetMeshDS() );
1197 TopoDS_Shape E = GetSubShapeByNode( n2, GetMeshDS() );
1198 if ( IsSubShape( V, E ))
1201 shape = GetCommonAncestor( V, E, *myMesh, TopAbs_FACE );
1205 if ( !shape.IsNull() )
1208 shapeID = GetMeshDS()->ShapeToIndex( shape );
1209 shapeType = shape.ShapeType(); // EDGE or FACE
1211 if ( expectedSupport < shapeType &&
1212 expectedSupport != TopAbs_SHAPE &&
1213 !myShape.IsNull() &&
1214 myShape.ShapeType() == expectedSupport )
1216 // e.g. a side of triangle connects nodes on the same EDGE but does not
1217 // lie on this EDGE (an arc with a coarse mesh)
1218 // => shapeType == TopAbs_EDGE, expectedSupport == TopAbs_FACE;
1219 // hope that myShape is a right shape, return it if the found shape
1220 // has converted elements of corresponding dim (segments in our example)
1221 int nbConvertedElems = 0;
1222 SMDSAbs_ElementType type = ( shapeType == TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
1223 for ( int iN = 0; iN < 2; ++iN )
1225 const SMDS_MeshNode* n = iN ? n2 : n1;
1226 SMDS_ElemIteratorPtr it = n->GetInverseElementIterator( type );
1227 while ( it->more() )
1229 const SMDS_MeshElement* elem = it->next();
1230 if ( elem->getshapeId() == shapeID &&
1231 elem->IsQuadratic() )
1238 if ( nbConvertedElems == 2 )
1240 shapeType = myShape.ShapeType();
1241 shapeID = myShapeID;
1245 return make_pair( shapeID, shapeType );
1248 //=======================================================================
1249 //function : GetCentralNode
1250 //purpose : Return existing or create a new central node for a quardilateral
1251 // quadratic face given its 8 nodes.
1252 //@param : force3d - true means node creation in between the given nodes,
1253 // else node position is found on a geometrical face if any.
1254 //=======================================================================
1256 const SMDS_MeshNode* SMESH_MesherHelper::GetCentralNode(const SMDS_MeshNode* n1,
1257 const SMDS_MeshNode* n2,
1258 const SMDS_MeshNode* n3,
1259 const SMDS_MeshNode* n4,
1260 const SMDS_MeshNode* n12,
1261 const SMDS_MeshNode* n23,
1262 const SMDS_MeshNode* n34,
1263 const SMDS_MeshNode* n41,
1266 SMDS_MeshNode *centralNode = 0; // central node to return
1268 // Find an existing central node
1270 TBiQuad keyOfMap(n1,n2,n3,n4);
1271 std::map<TBiQuad, const SMDS_MeshNode* >::iterator itMapCentralNode;
1272 itMapCentralNode = myMapWithCentralNode.find( keyOfMap );
1273 if ( itMapCentralNode != myMapWithCentralNode.end() )
1275 return (*itMapCentralNode).second;
1278 // Get type of shape for the new central node
1280 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
1284 TopTools_ListIteratorOfListOfShape it;
1286 std::map< int, int > faceId2nbNodes;
1287 std::map< int, int > ::iterator itMapWithIdFace;
1289 SMESHDS_Mesh* meshDS = GetMeshDS();
1291 // check if a face lies on a FACE, i.e. its all corner nodes lie either on the FACE or
1292 // on sub-shapes of the FACE
1293 if ( GetMesh()->HasShapeToMesh() )
1295 const SMDS_MeshNode* nodes[] = { n1, n2, n3, n4 };
1296 for(int i = 0; i < 4; i++)
1298 shape = GetSubShapeByNode( nodes[i], meshDS );
1299 if ( shape.IsNull() ) break;
1300 if ( shape.ShapeType() == TopAbs_SOLID )
1302 solidID = nodes[i]->getshapeId();
1303 shapeType = TopAbs_SOLID;
1306 if ( shape.ShapeType() == TopAbs_FACE )
1308 faceID = nodes[i]->getshapeId();
1309 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 ) ).first;
1310 itMapWithIdFace->second++;
1314 PShapeIteratorPtr it = GetAncestors( shape, *GetMesh(), TopAbs_FACE );
1315 while ( const TopoDS_Shape* face = it->next() )
1317 faceID = meshDS->ShapeToIndex( *face );
1318 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 )).first;
1319 itMapWithIdFace->second++;
1324 if ( solidID < 1 && !faceId2nbNodes.empty() ) // SOLID not found
1326 // find ID of the FACE the four corner nodes belong to
1327 itMapWithIdFace = faceId2nbNodes.find( myShapeID ); // IPAL52698
1328 if ( itMapWithIdFace != faceId2nbNodes.end() &&
1329 itMapWithIdFace->second == 4 )
1331 shapeType = TopAbs_FACE;
1336 itMapWithIdFace = faceId2nbNodes.begin();
1337 for ( ; itMapWithIdFace != faceId2nbNodes.end(); ++itMapWithIdFace)
1339 if ( itMapWithIdFace->second == 4 )
1341 shapeType = TopAbs_FACE;
1342 faceID = (*itMapWithIdFace).first;
1350 if ( shapeType == TopAbs_FACE )
1352 F = TopoDS::Face( meshDS->IndexToShape( faceID ));
1359 bool toCheck = true;
1360 if ( !F.IsNull() && !force3d )
1363 GetNodeUV( F,n1, n3, &toCheck ),
1364 GetNodeUV( F,n2, n4, &toCheck ),
1365 GetNodeUV( F,n3, n1, &toCheck ),
1366 GetNodeUV( F,n4, n2, &toCheck ),
1367 GetNodeUV( F,n12, n3 ),
1368 GetNodeUV( F,n23, n4 ),
1369 GetNodeUV( F,n34, n2 ),
1370 GetNodeUV( F,n41, n2 )
1372 AdjustByPeriod( F, uv, 8 ); // put uv[] within a period (IPAL52698)
1374 uvAvg = calcTFI (0.5, 0.5, uv[0],uv[1],uv[2],uv[3], uv[4],uv[5],uv[6],uv[7] );
1376 TopLoc_Location loc;
1377 Handle( Geom_Surface ) S = BRep_Tool::Surface( F, loc );
1378 P = S->Value( uvAvg.X(), uvAvg.Y() ).Transformed( loc );
1379 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1380 // if ( mySetElemOnShape ) node is not elem!
1381 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1383 else // ( force3d || F.IsNull() )
1385 P = calcTFI (0.5, 0.5,
1386 SMESH_TNodeXYZ(n1), SMESH_TNodeXYZ(n2),
1387 SMESH_TNodeXYZ(n3), SMESH_TNodeXYZ(n4),
1388 SMESH_TNodeXYZ(n12), SMESH_TNodeXYZ(n23),
1389 SMESH_TNodeXYZ(n34), SMESH_TNodeXYZ(n41));
1390 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1392 if ( !F.IsNull() ) // force3d
1394 uvAvg = (GetNodeUV(F,n1,n3,&toCheck) +
1395 GetNodeUV(F,n2,n4,&toCheck) +
1396 GetNodeUV(F,n3,n1,&toCheck) +
1397 GetNodeUV(F,n4,n2,&toCheck)) / 4;
1398 //CheckNodeUV( F, centralNode, uvAvg, 2*BRep_Tool::Tolerance( F ), /*force=*/true);
1399 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1401 else if ( solidID > 0 )
1403 meshDS->SetNodeInVolume( centralNode, solidID );
1405 else if ( myShapeID > 0 && mySetElemOnShape )
1407 meshDS->SetMeshElementOnShape( centralNode, myShapeID );
1410 myMapWithCentralNode.insert( std::make_pair( keyOfMap, centralNode ) );
1414 //=======================================================================
1415 //function : GetCentralNode
1416 //purpose : Return existing or create a new central node for a
1417 // quadratic triangle given its 6 nodes.
1418 //@param : force3d - true means node creation in between the given nodes,
1419 // else node position is found on a geometrical face if any.
1420 //=======================================================================
1422 const SMDS_MeshNode* SMESH_MesherHelper::GetCentralNode(const SMDS_MeshNode* n1,
1423 const SMDS_MeshNode* n2,
1424 const SMDS_MeshNode* n3,
1425 const SMDS_MeshNode* n12,
1426 const SMDS_MeshNode* n23,
1427 const SMDS_MeshNode* n31,
1430 SMDS_MeshNode *centralNode = 0; // central node to return
1432 // Find an existing central node
1434 TBiQuad keyOfMap(n1,n2,n3);
1435 std::map<TBiQuad, const SMDS_MeshNode* >::iterator itMapCentralNode;
1436 itMapCentralNode = myMapWithCentralNode.find( keyOfMap );
1437 if ( itMapCentralNode != myMapWithCentralNode.end() )
1439 return (*itMapCentralNode).second;
1442 // Get type of shape for the new central node
1444 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
1448 TopTools_ListIteratorOfListOfShape it;
1450 std::map< int, int > faceId2nbNodes;
1451 std::map< int, int > ::iterator itMapWithIdFace;
1453 SMESHDS_Mesh* meshDS = GetMeshDS();
1455 // check if a face lies on a FACE, i.e. its all corner nodes lie either on the FACE or
1456 // on sub-shapes of the FACE
1457 if ( GetMesh()->HasShapeToMesh() )
1459 const SMDS_MeshNode* nodes[] = { n1, n2, n3 };
1460 for(int i = 0; i < 3; i++)
1462 shape = GetSubShapeByNode( nodes[i], meshDS );
1463 if ( shape.IsNull() ) break;
1464 if ( shape.ShapeType() == TopAbs_SOLID )
1466 solidID = nodes[i]->getshapeId();
1467 shapeType = TopAbs_SOLID;
1470 if ( shape.ShapeType() == TopAbs_FACE )
1472 faceID = nodes[i]->getshapeId();
1473 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 ) ).first;
1474 itMapWithIdFace->second++;
1478 PShapeIteratorPtr it = GetAncestors(shape, *GetMesh(), TopAbs_FACE );
1479 while ( const TopoDS_Shape* face = it->next() )
1481 faceID = meshDS->ShapeToIndex( *face );
1482 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 ) ).first;
1483 itMapWithIdFace->second++;
1488 if ( solidID < 1 && !faceId2nbNodes.empty() ) // SOLID not found
1490 // find ID of the FACE the four corner nodes belong to
1491 itMapWithIdFace = faceId2nbNodes.find( myShapeID ); // IPAL52698
1492 if ( itMapWithIdFace != faceId2nbNodes.end() &&
1493 itMapWithIdFace->second == 4 )
1495 shapeType = TopAbs_FACE;
1500 itMapWithIdFace = faceId2nbNodes.begin();
1501 for ( ; itMapWithIdFace != faceId2nbNodes.end(); ++itMapWithIdFace)
1503 if ( itMapWithIdFace->second == 3 )
1505 shapeType = TopAbs_FACE;
1506 faceID = (*itMapWithIdFace).first;
1516 if ( shapeType == TopAbs_FACE )
1518 F = TopoDS::Face( meshDS->IndexToShape( faceID ));
1519 bool checkOK = true, badTria = false;
1521 GetNodeUV( F, n1, n23, &checkOK ),
1522 GetNodeUV( F, n2, n31, &checkOK ),
1523 GetNodeUV( F, n3, n12, &checkOK ),
1524 GetNodeUV( F, n12, n3, &checkOK ),
1525 GetNodeUV( F, n23, n1, &checkOK ),
1526 GetNodeUV( F, n31, n2, &checkOK )
1528 AdjustByPeriod( F, uv, 6 ); // put uv[] within a period (IPAL52698)
1530 uvAvg = GetCenterUV( uv[0],uv[1],uv[2], uv[3],uv[4],uv[5], &badTria );
1532 if ( badTria || !checkOK )
1536 // Create a central node
1539 if ( !F.IsNull() && !force3d )
1541 TopLoc_Location loc;
1542 Handle( Geom_Surface ) S = BRep_Tool::Surface( F, loc );
1543 P = S->Value( uvAvg.X(), uvAvg.Y() ).Transformed( loc );
1544 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1545 // if ( mySetElemOnShape ) node is not elem!
1546 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1548 else // ( force3d || F.IsNull() )
1550 P = ( SMESH_TNodeXYZ( n12 ) +
1551 SMESH_TNodeXYZ( n23 ) +
1552 SMESH_TNodeXYZ( n31 ) ) / 3;
1553 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1555 if ( !F.IsNull() ) // force3d
1557 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1559 else if ( solidID > 0 )
1561 meshDS->SetNodeInVolume( centralNode, solidID );
1563 else if ( myShapeID > 0 && mySetElemOnShape )
1565 meshDS->SetMeshElementOnShape( centralNode, myShapeID );
1568 myMapWithCentralNode.insert( std::make_pair( keyOfMap, centralNode ) );
1572 //=======================================================================
1573 //function : GetMediumNode
1574 //purpose : Return existing or create a new medium node between given ones
1575 //=======================================================================
1577 const SMDS_MeshNode* SMESH_MesherHelper::GetMediumNode(const SMDS_MeshNode* n1,
1578 const SMDS_MeshNode* n2,
1580 TopAbs_ShapeEnum expectedSupport)
1582 // Find existing node
1584 SMESH_TLink link(n1,n2);
1585 ItTLinkNode itLN = myTLinkNodeMap.find( link );
1586 if ( itLN != myTLinkNodeMap.end() ) {
1587 return (*itLN).second;
1590 // Create medium node
1593 SMESHDS_Mesh* meshDS = GetMeshDS();
1595 if ( IsSeamShape( n1->getshapeId() ))
1596 // to get a correct UV of a node on seam, the second node must have checked UV
1597 std::swap( n1, n2 );
1599 // get type of shape for the new medium node
1600 int faceID = -1, edgeID = -1;
1601 TopoDS_Edge E; double u [2];
1602 TopoDS_Face F; gp_XY uv[2];
1603 bool uvOK[2] = { true, true };
1604 const bool useCurSubShape = ( !myShape.IsNull() && myShape.ShapeType() == TopAbs_EDGE );
1606 pair<int, TopAbs_ShapeEnum> pos = GetMediumPos( n1, n2, useCurSubShape, expectedSupport );
1608 // get positions of the given nodes on shapes
1609 if ( pos.second == TopAbs_FACE )
1611 F = TopoDS::Face(meshDS->IndexToShape( faceID = pos.first ));
1612 uv[0] = GetNodeUV(F,n1,n2, force3d ? 0 : &uvOK[0]);
1614 ( HasDegeneratedEdges() || GetSurface( F )->HasSingularities( 1e-7 )))
1616 // IPAL52850 (degen VERTEX not at singularity)
1617 // project middle point to a surface
1618 SMESH_TNodeXYZ p1( n1 ), p2( n2 );
1619 gp_Pnt pMid = 0.5 * ( p1 + p2 );
1620 Handle(ShapeAnalysis_Surface) projector = GetSurface( F );
1623 uvMid = projector->NextValueOfUV( uv[0], pMid, BRep_Tool::Tolerance( F ));
1625 uvMid = projector->ValueOfUV( pMid, getFaceMaxTol( F ));
1626 if ( projector->Gap() * projector->Gap() < ( p1 - p2 ).SquareModulus() / 4 )
1628 gp_Pnt pProj = projector->Value( uvMid );
1629 n12 = meshDS->AddNode( pProj.X(), pProj.Y(), pProj.Z() );
1630 meshDS->SetNodeOnFace( n12, faceID, uvMid.X(), uvMid.Y() );
1631 myTLinkNodeMap.insert( make_pair ( link, n12 ));
1635 uv[1] = GetNodeUV(F,n2,n1, force3d ? 0 : &uvOK[1]);
1637 else if ( pos.second == TopAbs_EDGE )
1639 const SMDS_PositionPtr Pos1 = n1->GetPosition();
1640 const SMDS_PositionPtr Pos2 = n2->GetPosition();
1641 if ( Pos1->GetTypeOfPosition()==SMDS_TOP_EDGE &&
1642 Pos2->GetTypeOfPosition()==SMDS_TOP_EDGE &&
1643 n1->getshapeId() != n2->getshapeId() )
1646 return getMediumNodeOnComposedWire(n1,n2,force3d);
1648 E = TopoDS::Edge(meshDS->IndexToShape( edgeID = pos.first ));
1650 u[0] = GetNodeU(E,n1,n2, force3d ? 0 : &uvOK[0]);
1651 u[1] = GetNodeU(E,n2,n1, force3d ? 0 : &uvOK[1]);
1653 catch ( Standard_Failure& f )
1655 // issue 22502 / a node is on VERTEX not belonging to E
1656 // issue 22568 / both nodes are on non-connected VERTEXes
1657 return getMediumNodeOnComposedWire(n1,n2,force3d);
1661 if ( !force3d & uvOK[0] && uvOK[1] )
1663 // we try to create medium node using UV parameters of
1664 // nodes, else - medium between corresponding 3d points
1667 //if ( uvOK[0] && uvOK[1] )
1669 if ( IsDegenShape( n1->getshapeId() )) {
1670 if ( myParIndex & U_periodic ) uv[0].SetCoord( 1, uv[1].Coord( 1 ));
1671 else uv[0].SetCoord( 2, uv[1].Coord( 2 ));
1673 else if ( IsDegenShape( n2->getshapeId() )) {
1674 if ( myParIndex & U_periodic ) uv[1].SetCoord( 1, uv[0].Coord( 1 ));
1675 else uv[1].SetCoord( 2, uv[0].Coord( 2 ));
1677 TopLoc_Location loc;
1678 Handle(Geom_Surface) S = BRep_Tool::Surface(F,loc);
1679 gp_XY UV = GetMiddleUV( S, uv[0], uv[1] );
1680 gp_Pnt P = S->Value( UV.X(), UV.Y() ).Transformed(loc);
1681 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
1682 // if ( mySetElemOnShape ) node is not elem!
1683 meshDS->SetNodeOnFace(n12, faceID, UV.X(), UV.Y());
1684 myTLinkNodeMap.insert(make_pair(link,n12));
1688 else if ( !E.IsNull() )
1691 Handle(Geom_Curve) C = BRep_Tool::Curve(E, f, l);
1694 Standard_Boolean isPeriodic = C->IsPeriodic();
1697 Standard_Real Period = C->Period();
1698 Standard_Real p = u[1]+ShapeAnalysis::AdjustByPeriod(u[1],u[0],Period);
1699 Standard_Real pmid = (u[0]+p)/2.;
1700 U = pmid+ShapeAnalysis::AdjustToPeriod(pmid,C->FirstParameter(),C->LastParameter());
1705 gp_Pnt P = C->Value( U );
1706 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
1707 //if ( mySetElemOnShape ) node is not elem!
1708 meshDS->SetNodeOnEdge(n12, edgeID, U);
1709 myTLinkNodeMap.insert(make_pair(link,n12));
1716 double x = ( n1->X() + n2->X() )/2.;
1717 double y = ( n1->Y() + n2->Y() )/2.;
1718 double z = ( n1->Z() + n2->Z() )/2.;
1719 n12 = meshDS->AddNode(x,y,z);
1721 //if ( mySetElemOnShape ) node is not elem!
1725 gp_XY UV = ( uv[0] + uv[1] ) / 2.;
1726 CheckNodeUV( F, n12, UV, 2 * BRep_Tool::Tolerance( F ), /*force=*/true);
1727 meshDS->SetNodeOnFace(n12, faceID, UV.X(), UV.Y() );
1729 else if ( !E.IsNull() )
1731 double U = ( u[0] + u[1] ) / 2.;
1732 CheckNodeU( E, n12, U, 2 * BRep_Tool::Tolerance( E ), /*force=*/true);
1733 meshDS->SetNodeOnEdge(n12, edgeID, U);
1735 else if ( myShapeID > 0 && mySetElemOnShape )
1737 meshDS->SetMeshElementOnShape(n12, myShapeID);
1741 myTLinkNodeMap.insert( make_pair( link, n12 ));
1745 //================================================================================
1747 * \brief Makes a medium node if nodes reside different edges
1749 //================================================================================
1751 const SMDS_MeshNode* SMESH_MesherHelper::getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
1752 const SMDS_MeshNode* n2,
1755 SMESH_TNodeXYZ p1( n1 ), p2( n2 );
1756 gp_Pnt middle = 0.5 * p1 + 0.5 * p2;
1757 SMDS_MeshNode* n12 = AddNode( middle.X(), middle.Y(), middle.Z() );
1759 // To find position on edge and 3D position for n12,
1760 // project <middle> to 2 edges and select projection most close to <middle>
1762 TopoDS_Edge bestEdge;
1763 double u = 0, distMiddleProj = Precision::Infinite(), distXYZ[4], f,l;
1765 // get shapes under the nodes
1766 TopoDS_Shape shape[2];
1768 for ( int is2nd = 0; is2nd < 2; ++is2nd )
1770 const SMDS_MeshNode* n = is2nd ? n2 : n1;
1771 TopoDS_Shape S = GetSubShapeByNode( n, GetMeshDS() );
1773 shape[ nbShapes++ ] = S;
1776 vector< TopoDS_Shape > edges;
1777 for ( int iS = 0; iS < nbShapes; ++iS )
1779 switch ( shape[iS].ShapeType() ) {
1782 edges.push_back( shape[iS] );
1788 if ( nbShapes == 2 && iS==0 && shape[1-iS].ShapeType() == TopAbs_VERTEX )
1789 edge = GetCommonAncestor( shape[iS], shape[1-iS], *myMesh, TopAbs_EDGE );
1791 if ( edge.IsNull() )
1793 PShapeIteratorPtr eIt = GetAncestors( shape[iS], *myMesh, TopAbs_EDGE );
1794 while( const TopoDS_Shape* e = eIt->next() )
1795 edges.push_back( *e );
1801 if ( nbShapes == 1 || shape[1-iS].ShapeType() < TopAbs_EDGE )
1802 for ( TopExp_Explorer e( shape[iS], TopAbs_EDGE ); e.More(); e.Next() )
1803 edges.push_back( e.Current() );
1810 // project to get U of projection and distance from middle to projection
1811 for ( size_t iE = 0; iE < edges.size(); ++iE )
1813 const TopoDS_Edge& edge = TopoDS::Edge( edges[ iE ]);
1814 distXYZ[0] = distMiddleProj;
1816 CheckNodeU( edge, n12, testU, 2 * BRep_Tool::Tolerance(edge), /*force=*/true, distXYZ );
1817 if ( distXYZ[0] < distMiddleProj )
1819 distMiddleProj = distXYZ[0];
1825 // // both projections failed; set n12 on the edge of n1 with U of a common vertex
1826 // TopoDS_Vertex vCommon;
1827 // if ( TopExp::CommonVertex( edges[0], edges[1], vCommon ))
1828 // u = BRep_Tool::Parameter( vCommon, edges[0] );
1831 // double f,l, u0 = GetNodeU( edges[0], n1 );
1832 // BRep_Tool::Range( edges[0],f,l );
1833 // u = ( fabs(u0-f) < fabs(u0-l) ) ? f : l;
1836 // distMiddleProj = 0;
1839 if ( !bestEdge.IsNull() )
1841 // move n12 to position of a successfull projection
1842 //double tol = BRep_Tool::Tolerance(edges[ iOkEdge ]);
1843 if ( !force3d /*&& distMiddleProj > 2*tol*/ )
1845 TopLoc_Location loc;
1846 Handle(Geom_Curve) curve = BRep_Tool::Curve( bestEdge,loc,f,l );
1847 gp_Pnt p = curve->Value( u ).Transformed( loc );
1848 GetMeshDS()->MoveNode( n12, p.X(), p.Y(), p.Z() );
1850 //if ( mySetElemOnShape ) node is not elem!
1852 int edgeID = GetMeshDS()->ShapeToIndex( bestEdge );
1853 if ( edgeID != n12->getshapeId() )
1854 GetMeshDS()->UnSetNodeOnShape( n12 );
1855 GetMeshDS()->SetNodeOnEdge(n12, edgeID, u);
1858 myTLinkNodeMap.insert( make_pair( SMESH_TLink(n1,n2), n12 ));
1863 //=======================================================================
1864 //function : AddNode
1865 //purpose : Creates a node
1866 //=======================================================================
1868 SMDS_MeshNode* SMESH_MesherHelper::AddNode(double x, double y, double z, int ID,
1871 SMESHDS_Mesh * meshDS = GetMeshDS();
1872 SMDS_MeshNode* node = 0;
1874 node = meshDS->AddNodeWithID( x, y, z, ID );
1876 node = meshDS->AddNode( x, y, z );
1877 if ( mySetElemOnShape && myShapeID > 0 ) { // node is not elem ?
1878 switch ( myShape.ShapeType() ) {
1879 case TopAbs_SOLID: meshDS->SetNodeInVolume( node, myShapeID); break;
1880 case TopAbs_SHELL: meshDS->SetNodeInVolume( node, myShapeID); break;
1881 case TopAbs_FACE: meshDS->SetNodeOnFace( node, myShapeID, u, v); break;
1882 case TopAbs_EDGE: meshDS->SetNodeOnEdge( node, myShapeID, u); break;
1883 case TopAbs_VERTEX: meshDS->SetNodeOnVertex( node, myShapeID); break;
1890 //=======================================================================
1891 //function : AddEdge
1892 //purpose : Creates quadratic or linear edge
1893 //=======================================================================
1895 SMDS_MeshEdge* SMESH_MesherHelper::AddEdge(const SMDS_MeshNode* n1,
1896 const SMDS_MeshNode* n2,
1900 SMESHDS_Mesh * meshDS = GetMeshDS();
1902 SMDS_MeshEdge* edge = 0;
1903 if (myCreateQuadratic) {
1904 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
1906 edge = meshDS->AddEdgeWithID(n1, n2, n12, id);
1908 edge = meshDS->AddEdge(n1, n2, n12);
1912 edge = meshDS->AddEdgeWithID(n1, n2, id);
1914 edge = meshDS->AddEdge(n1, n2);
1917 if ( mySetElemOnShape && myShapeID > 0 )
1918 meshDS->SetMeshElementOnShape( edge, myShapeID );
1923 //=======================================================================
1924 //function : AddFace
1925 //purpose : Creates quadratic or linear triangle
1926 //=======================================================================
1928 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
1929 const SMDS_MeshNode* n2,
1930 const SMDS_MeshNode* n3,
1934 SMESHDS_Mesh * meshDS = GetMeshDS();
1935 SMDS_MeshFace* elem = 0;
1937 if( n1==n2 || n2==n3 || n3==n1 )
1940 if(!myCreateQuadratic) {
1942 elem = meshDS->AddFaceWithID(n1, n2, n3, id);
1944 elem = meshDS->AddFace(n1, n2, n3);
1947 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_FACE );
1948 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_FACE );
1949 const SMDS_MeshNode* n31 = GetMediumNode( n3, n1, force3d, TopAbs_FACE );
1950 if(myCreateBiQuadratic)
1952 const SMDS_MeshNode* nCenter = GetCentralNode(n1, n2, n3, n12, n23, n31, force3d);
1954 elem = meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, nCenter, id);
1956 elem = meshDS->AddFace(n1, n2, n3, n12, n23, n31, nCenter);
1961 elem = meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, id);
1963 elem = meshDS->AddFace(n1, n2, n3, n12, n23, n31);
1966 if ( mySetElemOnShape && myShapeID > 0 )
1967 meshDS->SetMeshElementOnShape( elem, myShapeID );
1972 //=======================================================================
1973 //function : AddFace
1974 //purpose : Creates bi-quadratic, quadratic or linear quadrangle
1975 //=======================================================================
1977 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
1978 const SMDS_MeshNode* n2,
1979 const SMDS_MeshNode* n3,
1980 const SMDS_MeshNode* n4,
1984 SMESHDS_Mesh * meshDS = GetMeshDS();
1985 SMDS_MeshFace* elem = 0;
1988 return AddFace(n1,n3,n4,id,force3d);
1991 return AddFace(n1,n2,n4,id,force3d);
1994 return AddFace(n1,n2,n3,id,force3d);
1997 return AddFace(n1,n2,n4,id,force3d);
2000 return AddFace(n1,n2,n3,id,force3d);
2003 return AddFace(n1,n2,n3,id,force3d);
2006 if(!myCreateQuadratic) {
2008 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, id);
2010 elem = meshDS->AddFace(n1, n2, n3, n4);
2013 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_FACE );
2014 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_FACE );
2015 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_FACE );
2016 const SMDS_MeshNode* n41 = GetMediumNode( n4, n1, force3d, TopAbs_FACE );
2017 if(myCreateBiQuadratic)
2019 const SMDS_MeshNode* nCenter = GetCentralNode(n1, n2, n3, n4, n12, n23, n34, n41, force3d);
2021 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, nCenter, id);
2023 elem = meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41, nCenter);
2028 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, id);
2030 elem = meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41);
2033 if ( mySetElemOnShape && myShapeID > 0 )
2034 meshDS->SetMeshElementOnShape( elem, myShapeID );
2039 //=======================================================================
2040 //function : AddPolygonalFace
2041 //purpose : Creates polygon, with additional nodes in quadratic mesh
2042 //=======================================================================
2044 SMDS_MeshFace* SMESH_MesherHelper::AddPolygonalFace (const vector<const SMDS_MeshNode*>& nodes,
2048 SMESHDS_Mesh * meshDS = GetMeshDS();
2049 SMDS_MeshFace* elem = 0;
2051 if(!myCreateQuadratic)
2054 elem = meshDS->AddPolygonalFaceWithID(nodes, id);
2056 elem = meshDS->AddPolygonalFace(nodes);
2060 vector<const SMDS_MeshNode*> newNodes( nodes.size() * 2 );
2062 for ( int i = 0; i < nodes.size(); ++i )
2064 const SMDS_MeshNode* n1 = nodes[i];
2065 const SMDS_MeshNode* n2 = nodes[(i+1)%nodes.size()];
2066 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_FACE );
2067 newNodes.push_back( n12 );
2070 elem = meshDS->AddQuadPolygonalFaceWithID(newNodes, id);
2072 elem = meshDS->AddQuadPolygonalFace(newNodes);
2074 if ( mySetElemOnShape && myShapeID > 0 )
2075 meshDS->SetMeshElementOnShape( elem, myShapeID );
2080 //=======================================================================
2081 //function : AddVolume
2082 //purpose : Creates quadratic or linear prism
2083 //=======================================================================
2085 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2086 const SMDS_MeshNode* n2,
2087 const SMDS_MeshNode* n3,
2088 const SMDS_MeshNode* n4,
2089 const SMDS_MeshNode* n5,
2090 const SMDS_MeshNode* n6,
2094 SMESHDS_Mesh * meshDS = GetMeshDS();
2095 SMDS_MeshVolume* elem = 0;
2096 if(!myCreateQuadratic) {
2098 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, id);
2100 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6);
2103 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2104 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2105 const SMDS_MeshNode* n31 = GetMediumNode( n3, n1, force3d, TopAbs_SOLID );
2107 const SMDS_MeshNode* n45 = GetMediumNode( n4, n5, force3d, TopAbs_SOLID );
2108 const SMDS_MeshNode* n56 = GetMediumNode( n5, n6, force3d, TopAbs_SOLID );
2109 const SMDS_MeshNode* n64 = GetMediumNode( n6, n4, force3d, TopAbs_SOLID );
2111 const SMDS_MeshNode* n14 = GetMediumNode( n1, n4, force3d, TopAbs_SOLID );
2112 const SMDS_MeshNode* n25 = GetMediumNode( n2, n5, force3d, TopAbs_SOLID );
2113 const SMDS_MeshNode* n36 = GetMediumNode( n3, n6, force3d, TopAbs_SOLID );
2116 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6,
2117 n12, n23, n31, n45, n56, n64, n14, n25, n36, id);
2119 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6,
2120 n12, n23, n31, n45, n56, n64, n14, n25, n36);
2122 if ( mySetElemOnShape && myShapeID > 0 )
2123 meshDS->SetMeshElementOnShape( elem, myShapeID );
2128 //=======================================================================
2129 //function : AddVolume
2130 //purpose : Creates quadratic or linear tetrahedron
2131 //=======================================================================
2133 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2134 const SMDS_MeshNode* n2,
2135 const SMDS_MeshNode* n3,
2136 const SMDS_MeshNode* n4,
2140 SMESHDS_Mesh * meshDS = GetMeshDS();
2141 SMDS_MeshVolume* elem = 0;
2142 if(!myCreateQuadratic) {
2144 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, id);
2146 elem = meshDS->AddVolume(n1, n2, n3, n4);
2149 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2150 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2151 const SMDS_MeshNode* n31 = GetMediumNode( n3, n1, force3d, TopAbs_SOLID );
2153 const SMDS_MeshNode* n14 = GetMediumNode( n1, n4, force3d, TopAbs_SOLID );
2154 const SMDS_MeshNode* n24 = GetMediumNode( n2, n4, force3d, TopAbs_SOLID );
2155 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_SOLID );
2158 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34, id);
2160 elem = meshDS->AddVolume(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34);
2162 if ( mySetElemOnShape && myShapeID > 0 )
2163 meshDS->SetMeshElementOnShape( elem, myShapeID );
2168 //=======================================================================
2169 //function : AddVolume
2170 //purpose : Creates quadratic or linear pyramid
2171 //=======================================================================
2173 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2174 const SMDS_MeshNode* n2,
2175 const SMDS_MeshNode* n3,
2176 const SMDS_MeshNode* n4,
2177 const SMDS_MeshNode* n5,
2181 SMDS_MeshVolume* elem = 0;
2182 if(!myCreateQuadratic) {
2184 elem = GetMeshDS()->AddVolumeWithID(n1, n2, n3, n4, n5, id);
2186 elem = GetMeshDS()->AddVolume(n1, n2, n3, n4, n5);
2189 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2190 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2191 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_SOLID );
2192 const SMDS_MeshNode* n41 = GetMediumNode( n4, n1, force3d, TopAbs_SOLID );
2194 const SMDS_MeshNode* n15 = GetMediumNode( n1, n5, force3d, TopAbs_SOLID );
2195 const SMDS_MeshNode* n25 = GetMediumNode( n2, n5, force3d, TopAbs_SOLID );
2196 const SMDS_MeshNode* n35 = GetMediumNode( n3, n5, force3d, TopAbs_SOLID );
2197 const SMDS_MeshNode* n45 = GetMediumNode( n4, n5, force3d, TopAbs_SOLID );
2200 elem = GetMeshDS()->AddVolumeWithID ( n1, n2, n3, n4, n5,
2205 elem = GetMeshDS()->AddVolume( n1, n2, n3, n4, n5,
2207 n15, n25, n35, n45);
2209 if ( mySetElemOnShape && myShapeID > 0 )
2210 GetMeshDS()->SetMeshElementOnShape( elem, myShapeID );
2215 //=======================================================================
2216 //function : AddVolume
2217 //purpose : Creates tri-quadratic, quadratic or linear hexahedron
2218 //=======================================================================
2220 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2221 const SMDS_MeshNode* n2,
2222 const SMDS_MeshNode* n3,
2223 const SMDS_MeshNode* n4,
2224 const SMDS_MeshNode* n5,
2225 const SMDS_MeshNode* n6,
2226 const SMDS_MeshNode* n7,
2227 const SMDS_MeshNode* n8,
2231 SMESHDS_Mesh * meshDS = GetMeshDS();
2232 SMDS_MeshVolume* elem = 0;
2233 if(!myCreateQuadratic) {
2235 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8, id);
2237 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8);
2240 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2241 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2242 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_SOLID );
2243 const SMDS_MeshNode* n41 = GetMediumNode( n4, n1, force3d, TopAbs_SOLID );
2245 const SMDS_MeshNode* n56 = GetMediumNode( n5, n6, force3d, TopAbs_SOLID );
2246 const SMDS_MeshNode* n67 = GetMediumNode( n6, n7, force3d, TopAbs_SOLID );
2247 const SMDS_MeshNode* n78 = GetMediumNode( n7, n8, force3d, TopAbs_SOLID );
2248 const SMDS_MeshNode* n85 = GetMediumNode( n8, n5, force3d, TopAbs_SOLID );
2250 const SMDS_MeshNode* n15 = GetMediumNode( n1, n5, force3d, TopAbs_SOLID );
2251 const SMDS_MeshNode* n26 = GetMediumNode( n2, n6, force3d, TopAbs_SOLID );
2252 const SMDS_MeshNode* n37 = GetMediumNode( n3, n7, force3d, TopAbs_SOLID );
2253 const SMDS_MeshNode* n48 = GetMediumNode( n4, n8, force3d, TopAbs_SOLID );
2254 if ( myCreateBiQuadratic )
2256 const SMDS_MeshNode* n1234 = GetCentralNode( n1,n2,n3,n4,n12,n23,n34,n41,force3d );
2257 const SMDS_MeshNode* n1256 = GetCentralNode( n1,n2,n5,n6,n12,n26,n56,n15,force3d );
2258 const SMDS_MeshNode* n2367 = GetCentralNode( n2,n3,n6,n7,n23,n37,n67,n26,force3d );
2259 const SMDS_MeshNode* n3478 = GetCentralNode( n3,n4,n7,n8,n34,n48,n78,n37,force3d );
2260 const SMDS_MeshNode* n1458 = GetCentralNode( n1,n4,n5,n8,n41,n48,n15,n85,force3d );
2261 const SMDS_MeshNode* n5678 = GetCentralNode( n5,n6,n7,n8,n56,n67,n78,n85,force3d );
2263 vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
2265 pointsOnShapes[ SMESH_Block::ID_V000 ] = SMESH_TNodeXYZ( n4 );
2266 pointsOnShapes[ SMESH_Block::ID_V100 ] = SMESH_TNodeXYZ( n8 );
2267 pointsOnShapes[ SMESH_Block::ID_V010 ] = SMESH_TNodeXYZ( n3 );
2268 pointsOnShapes[ SMESH_Block::ID_V110 ] = SMESH_TNodeXYZ( n7 );
2269 pointsOnShapes[ SMESH_Block::ID_V001 ] = SMESH_TNodeXYZ( n1 );
2270 pointsOnShapes[ SMESH_Block::ID_V101 ] = SMESH_TNodeXYZ( n5 );
2271 pointsOnShapes[ SMESH_Block::ID_V011 ] = SMESH_TNodeXYZ( n2 );
2272 pointsOnShapes[ SMESH_Block::ID_V111 ] = SMESH_TNodeXYZ( n6 );
2274 pointsOnShapes[ SMESH_Block::ID_Ex00 ] = SMESH_TNodeXYZ( n48 );
2275 pointsOnShapes[ SMESH_Block::ID_Ex10 ] = SMESH_TNodeXYZ( n37 );
2276 pointsOnShapes[ SMESH_Block::ID_E0y0 ] = SMESH_TNodeXYZ( n15 );
2277 pointsOnShapes[ SMESH_Block::ID_E1y0 ] = SMESH_TNodeXYZ( n26 );
2278 pointsOnShapes[ SMESH_Block::ID_Ex01 ] = SMESH_TNodeXYZ( n34 );
2279 pointsOnShapes[ SMESH_Block::ID_Ex11 ] = SMESH_TNodeXYZ( n78 );
2280 pointsOnShapes[ SMESH_Block::ID_E0y1 ] = SMESH_TNodeXYZ( n12 );
2281 pointsOnShapes[ SMESH_Block::ID_E1y1 ] = SMESH_TNodeXYZ( n56 );
2282 pointsOnShapes[ SMESH_Block::ID_E00z ] = SMESH_TNodeXYZ( n41 );
2283 pointsOnShapes[ SMESH_Block::ID_E10z ] = SMESH_TNodeXYZ( n85 );
2284 pointsOnShapes[ SMESH_Block::ID_E01z ] = SMESH_TNodeXYZ( n23 );
2285 pointsOnShapes[ SMESH_Block::ID_E11z ] = SMESH_TNodeXYZ( n67 );
2287 pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = SMESH_TNodeXYZ( n3478 );
2288 pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = SMESH_TNodeXYZ( n1256 );
2289 pointsOnShapes[ SMESH_Block::ID_Fx0z ] = SMESH_TNodeXYZ( n1458 );
2290 pointsOnShapes[ SMESH_Block::ID_Fx1z ] = SMESH_TNodeXYZ( n2367 );
2291 pointsOnShapes[ SMESH_Block::ID_F0yz ] = SMESH_TNodeXYZ( n1234 );
2292 pointsOnShapes[ SMESH_Block::ID_F1yz ] = SMESH_TNodeXYZ( n5678 );
2294 gp_XYZ centerCube(0.5, 0.5, 0.5);
2296 SMESH_Block::ShellPoint( centerCube, pointsOnShapes, nCenterElem );
2297 const SMDS_MeshNode* nCenter =
2298 meshDS->AddNode( nCenterElem.X(), nCenterElem.Y(), nCenterElem.Z() );
2299 meshDS->SetNodeInVolume( nCenter, myShapeID );
2302 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
2303 n12, n23, n34, n41, n56, n67,
2304 n78, n85, n15, n26, n37, n48,
2305 n1234, n1256, n2367, n3478, n1458, n5678, nCenter, id);
2307 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
2308 n12, n23, n34, n41, n56, n67,
2309 n78, n85, n15, n26, n37, n48,
2310 n1234, n1256, n2367, n3478, n1458, n5678, nCenter);
2315 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
2316 n12, n23, n34, n41, n56, n67,
2317 n78, n85, n15, n26, n37, n48, id);
2319 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
2320 n12, n23, n34, n41, n56, n67,
2321 n78, n85, n15, n26, n37, n48);
2324 if ( mySetElemOnShape && myShapeID > 0 )
2325 meshDS->SetMeshElementOnShape( elem, myShapeID );
2330 //=======================================================================
2331 //function : AddVolume
2332 //purpose : Creates LINEAR!!!!!!!!! octahedron
2333 //=======================================================================
2335 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2336 const SMDS_MeshNode* n2,
2337 const SMDS_MeshNode* n3,
2338 const SMDS_MeshNode* n4,
2339 const SMDS_MeshNode* n5,
2340 const SMDS_MeshNode* n6,
2341 const SMDS_MeshNode* n7,
2342 const SMDS_MeshNode* n8,
2343 const SMDS_MeshNode* n9,
2344 const SMDS_MeshNode* n10,
2345 const SMDS_MeshNode* n11,
2346 const SMDS_MeshNode* n12,
2350 SMESHDS_Mesh * meshDS = GetMeshDS();
2351 SMDS_MeshVolume* elem = 0;
2353 elem = meshDS->AddVolumeWithID(n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,id);
2355 elem = meshDS->AddVolume(n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12);
2356 if ( mySetElemOnShape && myShapeID > 0 )
2357 meshDS->SetMeshElementOnShape( elem, myShapeID );
2361 //=======================================================================
2362 //function : AddPolyhedralVolume
2363 //purpose : Creates polyhedron. In quadratic mesh, adds medium nodes
2364 //=======================================================================
2367 SMESH_MesherHelper::AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
2368 const std::vector<int>& quantities,
2372 SMESHDS_Mesh * meshDS = GetMeshDS();
2373 SMDS_MeshVolume* elem = 0;
2374 if(!myCreateQuadratic)
2377 elem = meshDS->AddPolyhedralVolumeWithID(nodes, quantities, id);
2379 elem = meshDS->AddPolyhedralVolume(nodes, quantities);
2383 vector<const SMDS_MeshNode*> newNodes;
2384 vector<int> newQuantities;
2385 for ( int iFace=0, iN=0; iFace < quantities.size(); ++iFace)
2387 int nbNodesInFace = quantities[iFace];
2388 newQuantities.push_back(0);
2389 for ( int i = 0; i < nbNodesInFace; ++i )
2391 const SMDS_MeshNode* n1 = nodes[ iN + i ];
2392 newNodes.push_back( n1 );
2393 newQuantities.back()++;
2395 const SMDS_MeshNode* n2 = nodes[ iN + ( i+1==nbNodesInFace ? 0 : i+1 )];
2396 // if ( n1->GetPosition()->GetTypeOfPosition() != SMDS_TOP_3DSPACE &&
2397 // n2->GetPosition()->GetTypeOfPosition() != SMDS_TOP_3DSPACE )
2399 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2400 newNodes.push_back( n12 );
2401 newQuantities.back()++;
2404 iN += nbNodesInFace;
2407 elem = meshDS->AddPolyhedralVolumeWithID( newNodes, newQuantities, id );
2409 elem = meshDS->AddPolyhedralVolume( newNodes, newQuantities );
2411 if ( mySetElemOnShape && myShapeID > 0 )
2412 meshDS->SetMeshElementOnShape( elem, myShapeID );
2419 //================================================================================
2421 * \brief Check if a node belongs to any face of sub-mesh
2423 //================================================================================
2425 bool isNodeInSubMesh( const SMDS_MeshNode* n, const SMESHDS_SubMesh* sm )
2427 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator( SMDSAbs_Face );
2428 while ( fIt->more() )
2429 if ( sm->Contains( fIt->next() ))
2435 //=======================================================================
2436 //function : IsSameElemGeometry
2437 //purpose : Returns true if all elements of a sub-mesh are of same shape
2438 //=======================================================================
2440 bool SMESH_MesherHelper::IsSameElemGeometry(const SMESHDS_SubMesh* smDS,
2441 SMDSAbs_GeometryType shape,
2442 const bool nullSubMeshRes)
2444 if ( !smDS ) return nullSubMeshRes;
2446 SMDS_ElemIteratorPtr elemIt = smDS->GetElements();
2447 while ( elemIt->more() ) {
2448 const SMDS_MeshElement* e = elemIt->next();
2449 if ( e->GetGeomType() != shape )
2455 //=======================================================================
2456 //function : LoadNodeColumns
2457 //purpose : Load nodes bound to face into a map of node columns
2458 //=======================================================================
2460 bool SMESH_MesherHelper::LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
2461 const TopoDS_Face& theFace,
2462 const TopoDS_Edge& theBaseEdge,
2463 SMESHDS_Mesh* theMesh,
2464 SMESH_ProxyMesh* theProxyMesh)
2466 return LoadNodeColumns(theParam2ColumnMap,
2468 std::list<TopoDS_Edge>(1,theBaseEdge),
2473 //=======================================================================
2474 //function : LoadNodeColumns
2475 //purpose : Load nodes bound to face into a map of node columns
2476 //=======================================================================
2478 bool SMESH_MesherHelper::LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
2479 const TopoDS_Face& theFace,
2480 const std::list<TopoDS_Edge>& theBaseSide,
2481 SMESHDS_Mesh* theMesh,
2482 SMESH_ProxyMesh* theProxyMesh)
2484 // get a right sub-mesh of theFace
2486 const SMESHDS_SubMesh* faceSubMesh = 0;
2489 faceSubMesh = theProxyMesh->GetSubMesh( theFace );
2490 if ( !faceSubMesh ||
2491 faceSubMesh->NbElements() == 0 ||
2492 theProxyMesh->IsTemporary( faceSubMesh->GetElements()->next() ))
2494 // can use a proxy sub-mesh with not temporary elements only
2500 faceSubMesh = theMesh->MeshElements( theFace );
2501 if ( !faceSubMesh || faceSubMesh->NbElements() == 0 )
2504 if ( theParam2ColumnMap.empty() )
2506 // get data of edges for normalization of params
2507 vector< double > length;
2509 list<TopoDS_Edge>::const_iterator edge;
2511 for ( edge = theBaseSide.begin(); edge != theBaseSide.end(); ++edge )
2513 double len = std::max( 1e-10, SMESH_Algo::EdgeLength( *edge ));
2515 length.push_back( len );
2519 // get nodes on theBaseEdge sorted by param on edge and initialize theParam2ColumnMap with them
2520 edge = theBaseSide.begin();
2521 for ( int iE = 0; edge != theBaseSide.end(); ++edge, ++iE )
2523 map< double, const SMDS_MeshNode*> sortedBaseNN;
2524 SMESH_Algo::GetSortedNodesOnEdge( theMesh, *edge,/*noMedium=*/true, sortedBaseNN );
2526 map< double, const SMDS_MeshNode*>::iterator u_n;
2527 // pb with mesh_Projection_2D_00/A1 fixed by adding expectedSupport arg to GetMediumPos()
2528 // so the following solution is commented (hope forever :)
2530 // SMESH_Algo::GetSortedNodesOnEdge( theMesh, *edge,/*noMedium=*/true, sortedBaseNN,
2531 // // SMDSAbs_Edge here is needed to be coherent with
2532 // // StdMeshers_FaceSide used by Quadrangle to get nodes
2533 // // on EDGE; else pb in mesh_Projection_2D_00/A1 where a
2534 // // medium node on EDGE is medium in a triangle but not
2537 // if ( faceSubMesh->GetElements()->next()->IsQuadratic() )
2538 // // filter off nodes medium in faces on theFace (same pb with mesh_Projection_2D_00/A1)
2539 // for ( u_n = sortedBaseNN.begin(); u_n != sortedBaseNN.end() ; )
2541 // const SMDS_MeshNode* node = u_n->second;
2542 // SMDS_ElemIteratorPtr faceIt = node->GetInverseElementIterator( SMDSAbs_Face );
2543 // if ( faceIt->more() && node ) {
2544 // const SMDS_MeshElement* face = faceIt->next();
2545 // if ( faceSubMesh->Contains( face ) && face->IsMediumNode( node ))
2549 // sortedBaseNN.erase( u_n++ );
2553 if ( sortedBaseNN.empty() ) continue;
2555 u_n = sortedBaseNN.begin();
2556 if ( theProxyMesh ) // from sortedBaseNN remove nodes not shared by faces of faceSubMesh
2558 const SMDS_MeshNode* n1 = (++sortedBaseNN.begin())->second;
2559 const SMDS_MeshNode* n2 = (++sortedBaseNN.rbegin())->second;
2560 bool allNodesAreProxy = ( n1 != theProxyMesh->GetProxyNode( n1 ) &&
2561 n2 != theProxyMesh->GetProxyNode( n2 ));
2562 if ( allNodesAreProxy )
2563 for ( u_n = sortedBaseNN.begin(); u_n != sortedBaseNN.end(); u_n++ )
2564 u_n->second = theProxyMesh->GetProxyNode( u_n->second );
2566 if ( u_n = sortedBaseNN.begin(), !isNodeInSubMesh( u_n->second, faceSubMesh ))
2568 while ( ++u_n != sortedBaseNN.end() && !isNodeInSubMesh( u_n->second, faceSubMesh ));
2569 sortedBaseNN.erase( sortedBaseNN.begin(), u_n );
2571 if ( !sortedBaseNN.empty() )
2572 if ( u_n = --sortedBaseNN.end(), !isNodeInSubMesh( u_n->second, faceSubMesh ))
2574 while ( u_n != sortedBaseNN.begin() && !isNodeInSubMesh( (--u_n)->second, faceSubMesh ));
2575 sortedBaseNN.erase( ++u_n, sortedBaseNN.end() );
2577 if ( sortedBaseNN.empty() ) continue;
2581 BRep_Tool::Range( *edge, f, l );
2582 if ( edge->Orientation() == TopAbs_REVERSED ) std::swap( f, l );
2583 const double coeff = 1. / ( l - f ) * length[iE] / fullLen;
2584 const double prevPar = theParam2ColumnMap.empty() ? 0 : theParam2ColumnMap.rbegin()->first;
2585 for ( u_n = sortedBaseNN.begin(); u_n != sortedBaseNN.end(); u_n++ )
2587 double par = prevPar + coeff * ( u_n->first - f );
2588 TParam2ColumnMap::iterator u2nn =
2589 theParam2ColumnMap.insert( theParam2ColumnMap.end(), make_pair( par, TNodeColumn()));
2590 u2nn->second.push_back( u_n->second );
2593 if ( theParam2ColumnMap.size() < 2 )
2598 int prevNbRows = theParam2ColumnMap.begin()->second.size(); // current, at least 1 here
2599 int expectedNbRows = faceSubMesh->NbElements() / ( theParam2ColumnMap.size()-1 ); // to be added
2601 // fill theParam2ColumnMap column by column by passing from nodes on
2602 // theBaseEdge up via mesh faces on theFace
2604 TParam2ColumnMap::iterator par_nVec_1, par_nVec_2;
2605 par_nVec_2 = theParam2ColumnMap.begin();
2606 par_nVec_1 = par_nVec_2++;
2607 TIDSortedElemSet emptySet, avoidSet;
2608 for ( ; par_nVec_2 != theParam2ColumnMap.end(); ++par_nVec_1, ++par_nVec_2 )
2610 vector<const SMDS_MeshNode*>& nCol1 = par_nVec_1->second;
2611 vector<const SMDS_MeshNode*>& nCol2 = par_nVec_2->second;
2612 nCol1.resize( prevNbRows + expectedNbRows );
2613 nCol2.resize( prevNbRows + expectedNbRows );
2615 int i1, i2, foundNbRows = 0;
2616 const SMDS_MeshNode *n1 = nCol1[ prevNbRows-1 ];
2617 const SMDS_MeshNode *n2 = nCol2[ prevNbRows-1 ];
2618 // find face sharing node n1 and n2 and belonging to faceSubMesh
2619 while ( const SMDS_MeshElement* face =
2620 SMESH_MeshAlgos::FindFaceInSet( n1, n2, emptySet, avoidSet, &i1, &i2))
2622 if ( faceSubMesh->Contains( face ))
2624 int nbNodes = face->NbCornerNodes();
2627 if ( foundNbRows + 1 > expectedNbRows )
2629 n1 = face->GetNode( (i2+2) % 4 ); // opposite corner of quadrangle face
2630 n2 = face->GetNode( (i1+2) % 4 );
2631 nCol1[ prevNbRows + foundNbRows] = n1;
2632 nCol2[ prevNbRows + foundNbRows] = n2;
2635 avoidSet.insert( face );
2637 if ( foundNbRows != expectedNbRows )
2641 return ( theParam2ColumnMap.size() > 1 &&
2642 theParam2ColumnMap.begin()->second.size() == prevNbRows + expectedNbRows );
2647 //================================================================================
2649 * \brief Return true if a node is at a corner of a 2D structured mesh of FACE
2651 //================================================================================
2653 bool isCornerOfStructure( const SMDS_MeshNode* n,
2654 const SMESHDS_SubMesh* faceSM,
2655 SMESH_MesherHelper& faceAnalyser )
2657 int nbFacesInSM = 0;
2659 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator( SMDSAbs_Face );
2660 while ( fIt->more() )
2661 nbFacesInSM += faceSM->Contains( fIt->next() );
2663 if ( nbFacesInSM == 1 )
2666 if ( nbFacesInSM == 2 && n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
2668 return faceAnalyser.IsRealSeam( n->getshapeId() );
2674 //=======================================================================
2675 //function : IsStructured
2676 //purpose : Return true if 2D mesh on FACE is a structured rectangle
2677 //=======================================================================
2679 bool SMESH_MesherHelper::IsStructured( SMESH_subMesh* faceSM )
2681 SMESHDS_SubMesh* fSM = faceSM->GetSubMeshDS();
2682 if ( !fSM || fSM->NbElements() == 0 )
2685 list< TopoDS_Edge > edges;
2686 list< int > nbEdgesInWires;
2687 int nbWires = SMESH_Block::GetOrderedEdges( TopoDS::Face( faceSM->GetSubShape() ),
2688 edges, nbEdgesInWires );
2689 if ( nbWires != 1 /*|| nbEdgesInWires.front() != 4*/ ) // allow composite sides
2692 // algo: find corners of a structure and then analyze nb of faces and
2693 // length of structure sides
2695 SMESHDS_Mesh* meshDS = faceSM->GetFather()->GetMeshDS();
2696 SMESH_MesherHelper faceAnalyser( *faceSM->GetFather() );
2697 faceAnalyser.SetSubShape( faceSM->GetSubShape() );
2699 // rotate edges to get the first node being at corner
2700 // (in principle it's not necessary because so far none SALOME algo can make
2701 // such a structured mesh that all corner nodes are not on VERTEXes)
2702 bool isCorner = false;
2703 int nbRemainEdges = nbEdgesInWires.front();
2705 TopoDS_Vertex V = IthVertex( 0, edges.front() );
2706 isCorner = isCornerOfStructure( SMESH_Algo::VertexNode( V, meshDS ),
2709 edges.splice( edges.end(), edges, edges.begin() );
2713 while ( !isCorner && nbRemainEdges > 0 );
2718 // get all nodes from EDGEs
2719 list< const SMDS_MeshNode* > nodes;
2720 list< TopoDS_Edge >::iterator edge = edges.begin();
2721 for ( ; edge != edges.end(); ++edge )
2723 map< double, const SMDS_MeshNode* > u2Nodes;
2724 if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS, *edge,
2725 /*skipMedium=*/true, u2Nodes ))
2728 list< const SMDS_MeshNode* > edgeNodes;
2729 map< double, const SMDS_MeshNode* >::iterator u2n = u2Nodes.begin();
2730 for ( ; u2n != u2Nodes.end(); ++u2n )
2731 edgeNodes.push_back( u2n->second );
2732 if ( edge->Orientation() == TopAbs_REVERSED )
2733 edgeNodes.reverse();
2735 if ( !nodes.empty() && nodes.back() == edgeNodes.front() )
2736 edgeNodes.pop_front();
2737 nodes.splice( nodes.end(), edgeNodes, edgeNodes.begin(), edgeNodes.end() );
2740 // get length of structured sides
2741 vector<int> nbEdgesInSide;
2743 list< const SMDS_MeshNode* >::iterator n = ++nodes.begin();
2744 for ( ; n != nodes.end(); ++n )
2747 if ( isCornerOfStructure( *n, fSM, faceAnalyser )) {
2748 nbEdgesInSide.push_back( nbEdges );
2754 if ( nbEdgesInSide.size() != 4 )
2756 if ( nbEdgesInSide[0] != nbEdgesInSide[2] )
2758 if ( nbEdgesInSide[1] != nbEdgesInSide[3] )
2760 if ( nbEdgesInSide[0] * nbEdgesInSide[1] != fSM->NbElements() )
2766 //=======================================================================
2767 //function : IsDistorted2D
2768 //purpose : Return true if 2D mesh on FACE is ditorted
2769 //=======================================================================
2771 bool SMESH_MesherHelper::IsDistorted2D( SMESH_subMesh* faceSM,
2774 if ( !faceSM || faceSM->GetSubShape().ShapeType() != TopAbs_FACE )
2777 bool haveBadFaces = false;
2779 SMESH_MesherHelper helper( *faceSM->GetFather() );
2780 helper.SetSubShape( faceSM->GetSubShape() );
2782 const TopoDS_Face& F = TopoDS::Face( faceSM->GetSubShape() );
2783 SMESHDS_SubMesh* smDS = helper.GetMeshDS()->MeshElements( F );
2784 if ( !smDS || smDS->NbElements() == 0 ) return false;
2786 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2787 double prevArea = 0;
2788 vector< const SMDS_MeshNode* > nodes;
2790 bool* toCheckUV = checkUV ? & checkUV : 0;
2791 while ( faceIt->more() && !haveBadFaces )
2793 const SMDS_MeshElement* face = faceIt->next();
2796 nodes.resize( face->NbCornerNodes() );
2797 SMDS_MeshElement::iterator n = face->begin_nodes();
2798 for ( size_t i = 0; i < nodes.size(); ++n, ++i )
2801 // avoid elems on degenarate shapes as UV on them can be wrong
2802 if ( helper.HasDegeneratedEdges() )
2804 bool isOnDegen = false;
2805 for ( size_t i = 0; ( i < nodes.size() && !isOnDegen ); ++i )
2806 isOnDegen = helper.IsDegenShape( nodes[ i ]->getshapeId() );
2810 // prepare to getting UVs
2811 const SMDS_MeshNode* inFaceNode = 0;
2812 if ( helper.HasSeam() ) {
2813 for ( size_t i = 0; ( i < nodes.size() && !inFaceNode ); ++i )
2814 if ( !helper.IsSeamShape( nodes[ i ]->getshapeId() ))
2815 inFaceNode = nodes[ i ];
2820 uv.resize( nodes.size() );
2821 for ( size_t i = 0; i < nodes.size(); ++i )
2822 uv[ i ] = helper.GetNodeUV( F, nodes[ i ], inFaceNode, toCheckUV );
2824 // compare orientation of triangles
2825 double faceArea = 0;
2826 for ( int iT = 0, nbT = nodes.size()-2; iT < nbT; ++iT )
2828 gp_XY v1 = uv[ iT+1 ] - uv[ 0 ];
2829 gp_XY v2 = uv[ iT+2 ] - uv[ 0 ];
2830 faceArea += v2 ^ v1;
2832 haveBadFaces = ( faceArea * prevArea < 0 );
2833 prevArea = faceArea;
2836 return haveBadFaces;
2839 //================================================================================
2841 * \brief Find out elements orientation on a geometrical face
2842 * \param theFace - The face correctly oriented in the shape being meshed
2843 * \retval bool - true if the face normal and the normal of first element
2844 * in the correspoding submesh point in different directions
2846 //================================================================================
2848 bool SMESH_MesherHelper::IsReversedSubMesh (const TopoDS_Face& theFace)
2850 if ( theFace.IsNull() )
2853 // find out orientation of a meshed face
2854 int faceID = GetMeshDS()->ShapeToIndex( theFace );
2855 TopoDS_Shape aMeshedFace = GetMeshDS()->IndexToShape( faceID );
2856 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
2858 const SMESHDS_SubMesh * aSubMeshDSFace = GetMeshDS()->MeshElements( faceID );
2859 if ( !aSubMeshDSFace )
2862 // find an element on a bounday of theFace
2863 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
2864 const SMDS_MeshNode* nn[2];
2865 while ( iteratorElem->more() ) // loop on elements on theFace
2867 const SMDS_MeshElement* elem = iteratorElem->next();
2868 if ( ! elem ) continue;
2870 // look for 2 nodes on EDGE
2871 int nbNodes = elem->NbCornerNodes();
2872 nn[0] = elem->GetNode( nbNodes-1 );
2873 for ( int iN = 0; iN < nbNodes; ++iN )
2875 nn[1] = elem->GetNode( iN );
2876 if ( nn[0]->GetPosition()->GetDim() < 2 &&
2877 nn[1]->GetPosition()->GetDim() < 2 )
2879 TopoDS_Shape s0 = GetSubShapeByNode( nn[0], GetMeshDS() );
2880 TopoDS_Shape s1 = GetSubShapeByNode( nn[1], GetMeshDS() );
2881 TopoDS_Shape E = GetCommonAncestor( s0, s1, *myMesh, TopAbs_EDGE );
2882 if ( !E.IsNull() && !s0.IsSame( s1 ))
2886 for ( TopExp_Explorer exp( theFace, TopAbs_EDGE ); exp.More(); exp.Next() )
2887 if ( E.IsSame( exp.Current() )) {
2889 E = exp.Current(); // to know orientation
2894 double u0 = GetNodeU( TopoDS::Edge( E ), nn[0], nn[1], &ok );
2895 double u1 = GetNodeU( TopoDS::Edge( E ), nn[1], nn[0], &ok );
2898 isReversed = ( u0 > u1 );
2899 if ( E.Orientation() == TopAbs_REVERSED )
2900 isReversed = !isReversed;
2910 // find an element with a good normal
2912 bool normalOK = false;
2914 iteratorElem = aSubMeshDSFace->GetElements();
2915 while ( !normalOK && iteratorElem->more() ) // loop on elements on theFace
2917 const SMDS_MeshElement* elem = iteratorElem->next();
2918 if ( ! SMESH_MeshAlgos::FaceNormal( elem, const_cast<gp_XYZ&>( Ne.XYZ() ), /*normalized=*/0 ))
2922 // get UV of a node inside theFACE
2923 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
2924 const SMDS_MeshNode* nInFace = 0;
2925 int iPosDim = SMDS_TOP_VERTEX;
2926 while ( nodesIt->more() ) // loop on nodes
2928 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodesIt->next() );
2929 if ( n->GetPosition()->GetTypeOfPosition() >= iPosDim )
2932 iPosDim = n->GetPosition()->GetTypeOfPosition();
2935 uv = GetNodeUV( theFace, nInFace, 0, &normalOK );
2940 // face normal at node position
2941 TopLoc_Location loc;
2942 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
2943 // if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 )
2944 // some surfaces not detected as GeomAbs_C1 are nevertheless correct for meshing
2945 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C0 )
2948 gp_Vec d1u, d1v; gp_Pnt p;
2949 surf->D1( uv.X(), uv.Y(), p, d1u, d1v );
2950 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
2952 if ( theFace.Orientation() == TopAbs_REVERSED )
2955 return Ne * Nf < 0.;
2958 //=======================================================================
2960 //purpose : Count nb of sub-shapes
2961 //=======================================================================
2963 int SMESH_MesherHelper::Count(const TopoDS_Shape& shape,
2964 const TopAbs_ShapeEnum type,
2965 const bool ignoreSame)
2968 TopTools_IndexedMapOfShape map;
2969 TopExp::MapShapes( shape, type, map );
2970 return map.Extent();
2974 for ( TopExp_Explorer exp( shape, type ); exp.More(); exp.Next() )
2980 //=======================================================================
2981 //function : NbAncestors
2982 //purpose : Return number of unique ancestors of the shape
2983 //=======================================================================
2985 int SMESH_MesherHelper::NbAncestors(const TopoDS_Shape& shape,
2986 const SMESH_Mesh& mesh,
2987 TopAbs_ShapeEnum ancestorType/*=TopAbs_SHAPE*/)
2989 TopTools_MapOfShape ancestors;
2990 TopTools_ListIteratorOfListOfShape ansIt( mesh.GetAncestors(shape) );
2991 for ( ; ansIt.More(); ansIt.Next() ) {
2992 if ( ancestorType == TopAbs_SHAPE || ansIt.Value().ShapeType() == ancestorType )
2993 ancestors.Add( ansIt.Value() );
2995 return ancestors.Extent();
2998 //=======================================================================
2999 //function : GetSubShapeOri
3000 //purpose : Return orientation of sub-shape in the main shape
3001 //=======================================================================
3003 TopAbs_Orientation SMESH_MesherHelper::GetSubShapeOri(const TopoDS_Shape& shape,
3004 const TopoDS_Shape& subShape)
3006 TopAbs_Orientation ori = TopAbs_Orientation(-1);
3007 if ( !shape.IsNull() && !subShape.IsNull() )
3009 TopExp_Explorer e( shape, subShape.ShapeType() );
3010 if ( shape.Orientation() >= TopAbs_INTERNAL ) // TopAbs_INTERNAL or TopAbs_EXTERNAL
3011 e.Init( shape.Oriented(TopAbs_FORWARD), subShape.ShapeType() );
3012 for ( ; e.More(); e.Next())
3013 if ( subShape.IsSame( e.Current() ))
3016 ori = e.Current().Orientation();
3021 //=======================================================================
3022 //function : IsSubShape
3024 //=======================================================================
3026 bool SMESH_MesherHelper::IsSubShape( const TopoDS_Shape& shape,
3027 const TopoDS_Shape& mainShape )
3029 if ( !shape.IsNull() && !mainShape.IsNull() )
3031 for ( TopExp_Explorer exp( mainShape, shape.ShapeType());
3034 if ( shape.IsSame( exp.Current() ))
3037 SCRUTE((shape.IsNull()));
3038 SCRUTE((mainShape.IsNull()));
3042 //=======================================================================
3043 //function : IsSubShape
3045 //=======================================================================
3047 bool SMESH_MesherHelper::IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh )
3049 if ( shape.IsNull() || !aMesh )
3052 aMesh->GetMeshDS()->ShapeToIndex( shape ) ||
3054 (shape.ShapeType() == TopAbs_COMPOUND && aMesh->GetMeshDS()->IsGroupOfSubShapes( shape ));
3057 //=======================================================================
3058 //function : IsBlock
3060 //=======================================================================
3062 bool SMESH_MesherHelper::IsBlock( const TopoDS_Shape& shape )
3064 if ( shape.IsNull() )
3068 TopExp_Explorer exp( shape, TopAbs_SHELL );
3069 if ( !exp.More() ) return false;
3070 shell = TopoDS::Shell( exp.Current() );
3071 if ( exp.Next(), exp.More() ) return false;
3074 TopTools_IndexedMapOfOrientedShape map;
3075 return SMESH_Block::FindBlockShapes( shell, v, v, map );
3079 //================================================================================
3081 * \brief Return maximal tolerance of shape
3083 //================================================================================
3085 double SMESH_MesherHelper::MaxTolerance( const TopoDS_Shape& shape )
3087 double tol = Precision::Confusion();
3088 TopExp_Explorer exp;
3089 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next() )
3090 tol = Max( tol, BRep_Tool::Tolerance( TopoDS::Face( exp.Current())));
3091 for ( exp.Init( shape, TopAbs_EDGE ); exp.More(); exp.Next() )
3092 tol = Max( tol, BRep_Tool::Tolerance( TopoDS::Edge( exp.Current())));
3093 for ( exp.Init( shape, TopAbs_VERTEX ); exp.More(); exp.Next() )
3094 tol = Max( tol, BRep_Tool::Tolerance( TopoDS::Vertex( exp.Current())));
3099 //================================================================================
3101 * \brief Return MaxTolerance( face ), probably cached
3103 //================================================================================
3105 double SMESH_MesherHelper::getFaceMaxTol( const TopoDS_Shape& face ) const
3107 int faceID = GetMeshDS()->ShapeToIndex( face );
3109 SMESH_MesherHelper* me = const_cast< SMESH_MesherHelper* >( this );
3110 double & tol = me->myFaceMaxTol.insert( make_pair( faceID, -1. )).first->second;
3112 tol = MaxTolerance( face );
3117 //================================================================================
3119 * \brief Return an angle between two EDGEs sharing a common VERTEX with reference
3120 * of the FACE normal
3121 * \return double - the angle (between -Pi and Pi), negative if the angle is concave,
3122 * 1e100 in case of failure
3123 * \waring Care about order of the EDGEs and their orientation to be as they are
3124 * within the FACE! Don't pass degenerated EDGEs neither!
3126 //================================================================================
3128 double SMESH_MesherHelper::GetAngle( const TopoDS_Edge & theE1,
3129 const TopoDS_Edge & theE2,
3130 const TopoDS_Face & theFace,
3131 const TopoDS_Vertex & theCommonV,
3132 gp_Vec* theFaceNormal)
3134 double angle = 1e100;
3138 Handle(Geom_Curve) c1 = BRep_Tool::Curve( theE1, f,l );
3139 Handle(Geom_Curve) c2 = BRep_Tool::Curve( theE2, f,l );
3140 Handle(Geom2d_Curve) c2d1 = BRep_Tool::CurveOnSurface( theE1, theFace, f,l );
3141 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace );
3142 double p1 = BRep_Tool::Parameter( theCommonV, theE1 );
3143 double p2 = BRep_Tool::Parameter( theCommonV, theE2 );
3144 if ( c1.IsNull() || c2.IsNull() )
3146 gp_XY uv = c2d1->Value( p1 ).XY();
3147 gp_Vec du, dv; gp_Pnt p;
3148 surf->D1( uv.X(), uv.Y(), p, du, dv );
3149 gp_Vec vec1, vec2, vecRef = du ^ dv;
3152 while ( vecRef.SquareMagnitude() < 1e-25 )
3154 double dp = ( l - f ) / 1000.;
3155 p1tmp += dp * (( Abs( p1 - f ) > Abs( p1 - l )) ? -1. : +1.);
3156 uv = c2d1->Value( p1tmp ).XY();
3157 surf->D1( uv.X(), uv.Y(), p, du, dv );
3159 if ( ++nbLoops > 10 )
3162 cout << "SMESH_MesherHelper::GetAngle(): Captured in a sigularity" << endl;
3167 if ( theFace.Orientation() == TopAbs_REVERSED )
3169 if ( theFaceNormal ) *theFaceNormal = vecRef;
3171 c1->D1( p1, p, vec1 );
3172 c2->D1( p2, p, vec2 );
3173 // TopoDS_Face F = theFace;
3174 // if ( F.Orientation() == TopAbs_INTERNAL )
3175 // F.Orientation( TopAbs_FORWARD );
3176 if ( theE1.Orientation() /*GetSubShapeOri( F, theE1 )*/ == TopAbs_REVERSED )
3178 if ( theE2.Orientation() /*GetSubShapeOri( F, theE2 )*/ == TopAbs_REVERSED )
3180 angle = vec1.AngleWithRef( vec2, vecRef );
3182 if ( Abs ( angle ) >= 0.99 * M_PI )
3184 BRep_Tool::Range( theE1, f, l );
3185 p1 += 1e-7 * ( p1-f < l-p1 ? +1. : -1. );
3186 c1->D1( p1, p, vec1 );
3187 if ( theE1.Orientation() == TopAbs_REVERSED )
3189 BRep_Tool::Range( theE2, f, l );
3190 p2 += 1e-7 * ( p2-f < l-p2 ? +1. : -1. );
3191 c2->D1( p2, p, vec2 );
3192 if ( theE2.Orientation() == TopAbs_REVERSED )
3194 angle = vec1.AngleWithRef( vec2, vecRef );
3203 //================================================================================
3205 * \brief Check if the first and last vertices of an edge are the same
3206 * \param anEdge - the edge to check
3207 * \retval bool - true if same
3209 //================================================================================
3211 bool SMESH_MesherHelper::IsClosedEdge( const TopoDS_Edge& anEdge )
3213 if ( anEdge.Orientation() >= TopAbs_INTERNAL )
3214 return IsClosedEdge( TopoDS::Edge( anEdge.Oriented( TopAbs_FORWARD )));
3215 return TopExp::FirstVertex( anEdge ).IsSame( TopExp::LastVertex( anEdge ));
3218 //================================================================================
3220 * \brief Wrapper over TopExp::FirstVertex() and TopExp::LastVertex() fixing them
3221 * in the case of INTERNAL edge
3223 //================================================================================
3225 TopoDS_Vertex SMESH_MesherHelper::IthVertex( const bool is2nd,
3229 if ( anEdge.Orientation() >= TopAbs_INTERNAL )
3230 anEdge.Orientation( TopAbs_FORWARD );
3232 const TopAbs_Orientation tgtOri = is2nd ? TopAbs_REVERSED : TopAbs_FORWARD;
3233 TopoDS_Iterator vIt( anEdge, CumOri );
3234 while ( vIt.More() && vIt.Value().Orientation() != tgtOri )
3237 return ( vIt.More() ? TopoDS::Vertex(vIt.Value()) : TopoDS_Vertex() );
3240 //================================================================================
3242 * \brief Return type of shape contained in a group
3243 * \param group - a shape of type TopAbs_COMPOUND
3244 * \param avoidCompound - not to return TopAbs_COMPOUND
3246 //================================================================================
3248 TopAbs_ShapeEnum SMESH_MesherHelper::GetGroupType(const TopoDS_Shape& group,
3249 const bool avoidCompound)
3251 if ( !group.IsNull() )
3253 if ( group.ShapeType() != TopAbs_COMPOUND )
3254 return group.ShapeType();
3256 // iterate on a compound
3257 TopoDS_Iterator it( group );
3259 return avoidCompound ? GetGroupType( it.Value() ) : it.Value().ShapeType();
3261 return TopAbs_SHAPE;
3264 //================================================================================
3266 * \brief Returns a shape, to which a hypothesis used to mesh a given shape is assigned
3267 * \param [in] hyp - the hypothesis
3268 * \param [in] shape - the shape, for meshing which the \a hyp is used
3269 * \param [in] mesh - the mesh
3270 * \return TopoDS_Shape - the shape the \a hyp is assigned to
3272 //================================================================================
3274 TopoDS_Shape SMESH_MesherHelper::GetShapeOfHypothesis( const SMESHDS_Hypothesis * hyp,
3275 const TopoDS_Shape& shape,
3278 const SMESH_Hypothesis* h = static_cast<const SMESH_Hypothesis*>( hyp );
3279 SMESH_HypoFilter hypFilter( SMESH_HypoFilter::Is( h ));
3281 TopoDS_Shape shapeOfHyp;
3282 mesh->GetHypothesis( shape, hypFilter, /*checkAncestors=*/true, &shapeOfHyp );
3286 //=======================================================================
3287 //function : IsQuadraticMesh
3288 //purpose : Check mesh without geometry for: if all elements on this shape are quadratic,
3289 // quadratic elements will be created.
3290 // Used then generated 3D mesh without geometry.
3291 //=======================================================================
3293 SMESH_MesherHelper:: MType SMESH_MesherHelper::IsQuadraticMesh()
3295 int NbAllEdgsAndFaces=0;
3296 int NbQuadFacesAndEdgs=0;
3297 int NbFacesAndEdges=0;
3298 //All faces and edges
3299 NbAllEdgsAndFaces = myMesh->NbEdges() + myMesh->NbFaces();
3300 if ( NbAllEdgsAndFaces == 0 )
3301 return SMESH_MesherHelper::LINEAR;
3303 //Quadratic faces and edges
3304 NbQuadFacesAndEdgs = myMesh->NbEdges(ORDER_QUADRATIC) + myMesh->NbFaces(ORDER_QUADRATIC);
3306 //Linear faces and edges
3307 NbFacesAndEdges = myMesh->NbEdges(ORDER_LINEAR) + myMesh->NbFaces(ORDER_LINEAR);
3309 if (NbAllEdgsAndFaces == NbQuadFacesAndEdgs) {
3311 return SMESH_MesherHelper::QUADRATIC;
3313 else if (NbAllEdgsAndFaces == NbFacesAndEdges) {
3315 return SMESH_MesherHelper::LINEAR;
3318 //Mesh with both type of elements
3319 return SMESH_MesherHelper::COMP;
3322 //=======================================================================
3323 //function : GetOtherParam
3324 //purpose : Return an alternative parameter for a node on seam
3325 //=======================================================================
3327 double SMESH_MesherHelper::GetOtherParam(const double param) const
3329 int i = myParIndex & U_periodic ? 0 : 1;
3330 return fabs(param-myPar1[i]) < fabs(param-myPar2[i]) ? myPar2[i] : myPar1[i];
3335 //=======================================================================
3337 * \brief Iterator on ancestors of the given type
3339 //=======================================================================
3341 struct TAncestorsIterator : public SMDS_Iterator<const TopoDS_Shape*>
3343 TopTools_ListIteratorOfListOfShape _ancIter;
3344 TopAbs_ShapeEnum _type;
3345 TopTools_MapOfShape _encountered;
3346 TAncestorsIterator( const TopTools_ListOfShape& ancestors, TopAbs_ShapeEnum type)
3347 : _ancIter( ancestors ), _type( type )
3349 if ( _ancIter.More() ) {
3350 if ( _ancIter.Value().ShapeType() != _type ) next();
3351 else _encountered.Add( _ancIter.Value() );
3356 return _ancIter.More();
3358 virtual const TopoDS_Shape* next()
3360 const TopoDS_Shape* s = _ancIter.More() ? & _ancIter.Value() : 0;
3361 if ( _ancIter.More() )
3362 for ( _ancIter.Next(); _ancIter.More(); _ancIter.Next())
3363 if ( _ancIter.Value().ShapeType() == _type && _encountered.Add( _ancIter.Value() ))
3371 //=======================================================================
3373 * \brief Return iterator on ancestors of the given type
3375 //=======================================================================
3377 PShapeIteratorPtr SMESH_MesherHelper::GetAncestors(const TopoDS_Shape& shape,
3378 const SMESH_Mesh& mesh,
3379 TopAbs_ShapeEnum ancestorType)
3381 return PShapeIteratorPtr( new TAncestorsIterator( mesh.GetAncestors(shape), ancestorType));
3384 //=======================================================================
3385 //function : GetCommonAncestor
3386 //purpose : Find a common ancestors of two shapes of the given type
3387 //=======================================================================
3389 TopoDS_Shape SMESH_MesherHelper::GetCommonAncestor(const TopoDS_Shape& shape1,
3390 const TopoDS_Shape& shape2,
3391 const SMESH_Mesh& mesh,
3392 TopAbs_ShapeEnum ancestorType)
3394 TopoDS_Shape commonAnc;
3395 if ( !shape1.IsNull() && !shape2.IsNull() )
3397 if ( shape1.ShapeType() == ancestorType && IsSubShape( shape2, shape1 ))
3399 if ( shape2.ShapeType() == ancestorType && IsSubShape( shape1, shape2 ))
3402 PShapeIteratorPtr ancIt = GetAncestors( shape1, mesh, ancestorType );
3403 while ( const TopoDS_Shape* anc = ancIt->next() )
3404 if ( IsSubShape( shape2, *anc ))
3413 //#include <Perf_Meter.hxx>
3415 //=======================================================================
3416 namespace { // Structures used by FixQuadraticElements()
3417 //=======================================================================
3419 #define __DMP__(txt) \
3421 #define MSG(txt) __DMP__(txt<<endl)
3422 #define MSGBEG(txt) __DMP__(txt)
3424 //const double straightTol2 = 1e-33; // to detect straing links
3425 bool isStraightLink(double linkLen2, double middleNodeMove2)
3427 // straight if <node move> < 1/15 * <link length>
3428 return middleNodeMove2 < 1/15./15. * linkLen2;
3432 // ---------------------------------------
3434 * \brief Quadratic link knowing its faces
3436 struct QLink: public SMESH_TLink
3438 const SMDS_MeshNode* _mediumNode;
3439 mutable vector<const QFace* > _faces;
3440 mutable gp_Vec _nodeMove;
3441 mutable int _nbMoves;
3442 mutable bool _is2dFixed; // is moved along surface or in 3D
3444 QLink(const SMDS_MeshNode* n1, const SMDS_MeshNode* n2, const SMDS_MeshNode* nm):
3445 SMESH_TLink( n1,n2 ), _mediumNode(nm), _nodeMove(0,0,0), _nbMoves(0) {
3447 _nodeMove = MediumPnt() - MiddlePnt();
3448 _is2dFixed = ( MediumPos() != SMDS_TOP_FACE );
3450 void SetContinuesFaces() const;
3451 const QFace* GetContinuesFace( const QFace* face ) const;
3452 bool OnBoundary() const;
3453 gp_XYZ MiddlePnt() const { return ( XYZ( node1() ) + XYZ( node2() )) / 2.; }
3454 gp_XYZ MediumPnt() const { return XYZ( _mediumNode ); }
3456 SMDS_TypeOfPosition MediumPos() const
3457 { return _mediumNode->GetPosition()->GetTypeOfPosition(); }