1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_Algo.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_Algo.hxx"
30 #include "SMDS_EdgePosition.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMESH_Gen.hxx"
39 #include "SMESH_HypoFilter.hxx"
40 #include "SMESH_Mesh.hxx"
41 #include "SMESH_MeshAlgos.hxx"
42 #include "SMESH_TypeDefs.hxx"
43 #include "SMESH_subMesh.hxx"
45 #include <Basics_OCCTVersion.hxx>
47 #include <BRepAdaptor_Curve.hxx>
48 #include <BRepLProp.hxx>
49 #include <BRep_Tool.hxx>
50 #include <GCPnts_AbscissaPoint.hxx>
51 #include <GeomAdaptor_Curve.hxx>
52 #include <Geom_Surface.hxx>
53 #include <LDOMParser.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopLoc_Location.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #include <TopTools_ListOfShape.hxx>
60 #include <TopoDS_Edge.hxx>
61 #include <TopoDS_Face.hxx>
62 #include <TopoDS_Vertex.hxx>
63 #include <TopoDS_Wire.hxx>
65 #include <gp_Pnt2d.hxx>
68 #include <Standard_ErrorHandler.hxx>
69 #include <Standard_Failure.hxx>
71 #include "utilities.h"
75 #include "SMESH_ProxyMesh.hxx"
76 #include "SMESH_MesherHelper.hxx"
80 //================================================================================
82 * \brief Returns \a true if two algorithms (described by \a this and the given
83 * algo data) are compatible by their output and input types of elements.
85 //================================================================================
87 bool SMESH_Algo::Features::IsCompatible( const SMESH_Algo::Features& algo2 ) const
89 if ( _dim > algo2._dim ) return algo2.IsCompatible( *this );
90 // algo2 is of highter dimension
91 if ( _outElemTypes.empty() || algo2._inElemTypes.empty() )
93 bool compatible = true;
94 set<SMDSAbs_GeometryType>::const_iterator myOutType = _outElemTypes.begin();
95 for ( ; myOutType != _outElemTypes.end() && compatible; ++myOutType )
96 compatible = algo2._inElemTypes.count( *myOutType );
100 //================================================================================
102 * \brief Return Data of the algorithm
104 //================================================================================
106 const SMESH_Algo::Features& SMESH_Algo::GetFeatures( const std::string& algoType )
108 static map< string, SMESH_Algo::Features > theFeaturesByName;
109 if ( theFeaturesByName.empty() )
111 // Read Plugin.xml files
112 vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
113 LDOMParser xmlParser;
114 for ( size_t iXML = 0; iXML < xmlPaths.size(); ++iXML )
116 bool error = xmlParser.parse( xmlPaths[iXML].c_str() );
119 TCollection_AsciiString data;
120 INFOS( xmlParser.GetError(data) );
123 // <algorithm type="Regular_1D"
126 // output="QUAD,TRIA">
128 LDOM_Document xmlDoc = xmlParser.getDocument();
129 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
130 for ( int i = 0; i < algoNodeList.getLength(); ++i )
132 LDOM_Node algoNode = algoNodeList.item( i );
133 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
134 TCollection_AsciiString algoType = algoElem.getAttribute("type");
135 TCollection_AsciiString input = algoElem.getAttribute("input");
136 TCollection_AsciiString output = algoElem.getAttribute("output");
137 TCollection_AsciiString dim = algoElem.getAttribute("dim");
138 TCollection_AsciiString label = algoElem.getAttribute("label-id");
139 if ( algoType.IsEmpty() ) continue;
141 Features & data = theFeaturesByName[ algoType.ToCString() ];
142 data._dim = dim.IntegerValue();
143 data._label = label.ToCString();
144 for ( int isInput = 0; isInput < 2; ++isInput )
146 TCollection_AsciiString& typeStr = isInput ? input : output;
147 set<SMDSAbs_GeometryType>& typeSet = isInput ? data._inElemTypes : data._outElemTypes;
149 while ( beg <= typeStr.Length() )
151 while ( beg < typeStr.Length() && !isalpha( typeStr.Value( beg ) ))
154 while ( end < typeStr.Length() && isalpha( typeStr.Value( end + 1 ) ))
158 TCollection_AsciiString typeName = typeStr.SubString( beg, end );
159 if ( typeName == "EDGE" ) typeSet.insert( SMDSGeom_EDGE );
160 else if ( typeName == "TRIA" ) typeSet.insert( SMDSGeom_TRIANGLE );
161 else if ( typeName == "QUAD" ) typeSet.insert( SMDSGeom_QUADRANGLE );
169 return theFeaturesByName[ algoType ];
172 //=============================================================================
176 //=============================================================================
178 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
179 : SMESH_Hypothesis(hypId, studyId, gen)
181 _compatibleAllHypFilter = _compatibleNoAuxHypFilter = NULL;
182 _onlyUnaryInput = _requireDiscreteBoundary = _requireShape = true;
183 _quadraticMesh = _supportSubmeshes = false;
185 for ( int i = 0; i < 4; ++i )
186 _neededLowerHyps[ i ] = false;
189 //=============================================================================
193 //=============================================================================
195 SMESH_Algo::~SMESH_Algo()
197 delete _compatibleNoAuxHypFilter;
198 // delete _compatibleAllHypFilter; -- _compatibleNoAuxHypFilter does it!!!
201 //=============================================================================
205 //=============================================================================
207 SMESH_0D_Algo::SMESH_0D_Algo(int hypId, int studyId, SMESH_Gen* gen)
208 : SMESH_Algo(hypId, studyId, gen)
210 _shapeType = (1 << TopAbs_VERTEX);
213 SMESH_1D_Algo::SMESH_1D_Algo(int hypId, int studyId, SMESH_Gen* gen)
214 : SMESH_Algo(hypId, studyId, gen)
216 _shapeType = (1 << TopAbs_EDGE);
219 SMESH_2D_Algo::SMESH_2D_Algo(int hypId, int studyId, SMESH_Gen* gen)
220 : SMESH_Algo(hypId, studyId, gen)
222 _shapeType = (1 << TopAbs_FACE);
225 SMESH_3D_Algo::SMESH_3D_Algo(int hypId, int studyId, SMESH_Gen* gen)
226 : SMESH_Algo(hypId, studyId, gen)
228 _shapeType = (1 << TopAbs_SOLID);
232 //=============================================================================
234 * Usually an algoritm has nothing to save
236 //=============================================================================
238 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
239 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
241 //=============================================================================
245 //=============================================================================
247 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
249 return _compatibleHypothesis;
252 //=============================================================================
254 * List the hypothesis used by the algorithm associated to the shape.
255 * Hypothesis associated to father shape -are- taken into account (see
256 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
257 * the algorithm. This method could be surcharged by specific algorithms, in
258 * case of several hypothesis simultaneously applicable.
260 //=============================================================================
262 const list <const SMESHDS_Hypothesis *> &
263 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
264 const TopoDS_Shape & aShape,
265 const bool ignoreAuxiliary) const
267 SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
268 me->_usedHypList.clear();
269 if ( const SMESH_HypoFilter* filter = GetCompatibleHypoFilter( ignoreAuxiliary ))
271 aMesh.GetHypotheses( aShape, *filter, me->_usedHypList, true );
272 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
273 me->_usedHypList.clear(); //only one compatible hypothesis allowed
278 //=============================================================================
280 * List the relevant hypothesis associated to the shape. Relevant hypothesis
281 * have a name (type) listed in the algorithm. Hypothesis associated to
282 * father shape -are not- taken into account (see GetUsedHypothesis)
284 //=============================================================================
286 const list<const SMESHDS_Hypothesis *> &
287 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
288 const TopoDS_Shape & aShape,
289 const bool ignoreAuxiliary) const
291 SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
292 me->_appliedHypList.clear();
293 if ( const SMESH_HypoFilter* filter = GetCompatibleHypoFilter( ignoreAuxiliary ))
294 aMesh.GetHypotheses( aShape, *filter, me->_appliedHypList, false );
296 return _appliedHypList;
299 //=============================================================================
301 * Compute length of an edge
303 //=============================================================================
305 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
307 double UMin = 0, UMax = 0;
309 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
312 GeomAdaptor_Curve AdaptCurve(C, UMin, UMax); //range is important for periodic curves
313 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
317 //================================================================================
319 * \brief Just return false as the algorithm does not hold parameters values
321 //================================================================================
323 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
324 const TopoDS_Shape& /*theShape*/)
328 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
332 //================================================================================
334 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
335 * \param theMesh - The mesh containing nodes
336 * \param theEdge - The geometrical edge of interest
337 * \param theParams - The resulting vector of sorted node parameters
338 * \retval bool - false if not all parameters are OK
340 //================================================================================
342 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
343 const TopoDS_Edge& theEdge,
344 vector< double > & theParams)
348 if ( !theMesh || theEdge.IsNull() )
351 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
352 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
353 return false; // edge is not meshed
355 //int nbEdgeNodes = 0;
356 set < double > paramSet;
359 // loop on nodes of an edge: sort them by param on edge
360 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
361 while ( nIt->more() )
363 const SMDS_MeshNode* node = nIt->next();
364 const SMDS_PositionPtr& pos = node->GetPosition();
365 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
367 const SMDS_EdgePosition* epos =
368 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
369 if ( !paramSet.insert( epos->GetUParameter() ).second )
370 return false; // equal parameters
373 // add vertex nodes params
375 TopExp::Vertices( theEdge, V1, V2);
376 if ( VertexNode( V1, theMesh ) &&
377 !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
378 return false; // there are equal parameters
379 if ( VertexNode( V2, theMesh ) &&
380 !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
381 return false; // there are equal parameters
384 theParams.resize( paramSet.size() );
385 set < double >::iterator par = paramSet.begin();
386 vector< double >::iterator vecPar = theParams.begin();
387 for ( ; par != paramSet.end(); ++par, ++vecPar )
390 return theParams.size() > 1;
393 //================================================================================
395 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
396 * \param theMesh - The mesh containing nodes
397 * \param theEdge - The geometrical edge of interest
398 * \param theParams - The resulting vector of sorted node parameters
399 * \retval bool - false if not all parameters are OK
401 //================================================================================
403 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh* theMesh,
404 const TopoDS_Edge& theEdge,
405 const bool ignoreMediumNodes,
406 map< double, const SMDS_MeshNode* > & theNodes)
410 if ( !theMesh || theEdge.IsNull() )
413 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
414 if ( !eSubMesh || ( eSubMesh->NbElements()==0 && eSubMesh->NbNodes() == 0))
415 return false; // edge is not meshed
418 set < double > paramSet;
421 // loop on nodes of an edge: sort them by param on edge
422 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
423 while ( nIt->more() )
425 const SMDS_MeshNode* node = nIt->next();
426 if ( ignoreMediumNodes ) {
427 SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
428 if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
431 const SMDS_PositionPtr& pos = node->GetPosition();
432 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
434 const SMDS_EdgePosition* epos =
435 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
436 theNodes.insert( theNodes.end(), make_pair( epos->GetUParameter(), node ));
437 //MESSAGE("U " << epos->GetUParameter() << " ID " << node->GetID());
442 TopoDS_Vertex v1, v2;
443 TopExp::Vertices(theEdge, v1, v2);
444 const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
445 const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
446 //MESSAGE("Vertices ID " << n1->GetID() << " " << n2->GetID());
448 BRep_Tool::Range(theEdge, f, l);
449 if ( v1.Orientation() != TopAbs_FORWARD )
451 if ( n1 && ++nbNodes )
452 theNodes.insert( make_pair( f, n1 ));
453 if ( n2 && ++nbNodes )
454 theNodes.insert( make_pair( l, n2 ));
456 return theNodes.size() == nbNodes;
459 //================================================================================
461 * \brief Returns the filter recognizing only compatible hypotheses
462 * \param ignoreAuxiliary - make filter ignore auxiliary hypotheses
463 * \retval SMESH_HypoFilter* - the filter that can be NULL
465 //================================================================================
467 const SMESH_HypoFilter*
468 SMESH_Algo::GetCompatibleHypoFilter(const bool ignoreAuxiliary) const
470 if ( !_compatibleHypothesis.empty() )
472 if ( !_compatibleAllHypFilter )
474 SMESH_HypoFilter* filter = new SMESH_HypoFilter();
475 filter->Init( filter->HasName( _compatibleHypothesis[0] ));
476 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
477 filter->Or( filter->HasName( _compatibleHypothesis[ i ] ));
479 SMESH_HypoFilter* filterNoAux = new SMESH_HypoFilter( filter );
480 filterNoAux->AndNot( filterNoAux->IsAuxiliary() );
482 // _compatibleNoAuxHypFilter will detele _compatibleAllHypFilter!!!
483 SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
484 me->_compatibleAllHypFilter = filter;
485 me->_compatibleNoAuxHypFilter = filterNoAux;
487 return ignoreAuxiliary ? _compatibleNoAuxHypFilter : _compatibleAllHypFilter;
492 //================================================================================
494 * \brief Return continuity of two edges
495 * \param E1 - the 1st edge
496 * \param E2 - the 2nd edge
497 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
499 //================================================================================
501 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
504 //E1.Orientation(TopAbs_FORWARD), E2.Orientation(TopAbs_FORWARD); // avoid pb with internal edges
505 if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
506 E1.Orientation( TopAbs_FORWARD );
507 if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
508 E2.Orientation( TopAbs_FORWARD );
510 TopoDS_Vertex V, VV1[2], VV2[2];
511 TopExp::Vertices( E1, VV1[0], VV1[1], true );
512 TopExp::Vertices( E2, VV2[0], VV2[1], true );
513 if ( VV1[1].IsSame( VV2[0] )) { V = VV1[1]; }
514 else if ( VV1[0].IsSame( VV2[1] )) { V = VV1[0]; }
515 else if ( VV1[1].IsSame( VV2[1] )) { V = VV1[1]; E1.Reverse(); }
516 else if ( VV1[0].IsSame( VV2[0] )) { V = VV1[0]; E1.Reverse(); }
517 else { return GeomAbs_C0; }
519 Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
520 Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
521 BRepAdaptor_Curve C1( E1 ), C2( E2 );
522 Standard_Real tol = BRep_Tool::Tolerance( V );
523 Standard_Real angTol = 2e-3;
526 return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
528 catch (Standard_Failure) {
533 //================================================================================
535 * \brief Return true if an edge can be considered straight
537 //================================================================================
539 bool SMESH_Algo::IsStraight( const TopoDS_Edge & E,
540 const bool degenResult)
544 if ( BRep_Tool::Curve( E, f, l ).IsNull())
547 BRepAdaptor_Curve curve( E );
548 switch( curve.GetType() )
553 case GeomAbs_Ellipse:
554 case GeomAbs_Hyperbola:
555 case GeomAbs_Parabola:
557 // case GeomAbs_BezierCurve:
558 // case GeomAbs_BSplineCurve:
559 // case GeomAbs_OtherCurve:
562 const double f = curve.FirstParameter();
563 const double l = curve.LastParameter();
564 const gp_Pnt pf = curve.Value( f );
565 const gp_Pnt pl = curve.Value( l );
566 const gp_Vec v1( pf, pl );
567 const double v1Len = v1.Magnitude();
568 if ( v1Len < std::numeric_limits< double >::min() )
569 return false; // E seems closed
570 const double tol = Min( 10 * curve.Tolerance(), v1Len * 1e-2 );
571 const int nbSamples = 7;
572 for ( int i = 0; i < nbSamples; ++i )
574 const double r = ( i + 1 ) / nbSamples;
575 const gp_Pnt pi = curve.Value( f * r + l * ( 1 - r ));
576 const gp_Vec vi( pf, pi );
577 const double h = 0.5 * v1.Crossed( vi ).Magnitude() / v1Len;
584 //================================================================================
586 * \brief Return true if an edge has no 3D curve
588 //================================================================================
590 bool SMESH_Algo::isDegenerated( const TopoDS_Edge & E )
594 Handle(Geom_Curve) C = BRep_Tool::Curve( E, loc, f,l );
598 //================================================================================
600 * \brief Return the node built on a vertex
601 * \param V - the vertex
602 * \param meshDS - mesh
603 * \retval const SMDS_MeshNode* - found node or NULL
604 * \sa SMESH_MesherHelper::GetSubShapeByNode( const SMDS_MeshNode*, SMESHDS_Mesh* )
606 //================================================================================
608 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
609 const SMESHDS_Mesh* meshDS)
611 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
612 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
619 //=======================================================================
620 //function : GetMeshError
621 //purpose : Finds topological errors of a sub-mesh
622 //WARNING : 1D check is NOT implemented so far
623 //=======================================================================
625 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
627 EMeshError err = MEr_OK;
629 SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
633 switch ( subMesh->GetSubShape().ShapeType() )
635 case TopAbs_FACE: { // ====================== 2D =====================
637 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
641 // We check that olny links on EDGEs encouter once, the rest links, twice
642 set< SMESH_TLink > links;
643 while ( fIt->more() )
645 const SMDS_MeshElement* f = fIt->next();
646 int nbNodes = f->NbCornerNodes(); // ignore medium nodes
647 for ( int i = 0; i < nbNodes; ++i )
649 const SMDS_MeshNode* n1 = f->GetNode( i );
650 const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
651 std::pair< set< SMESH_TLink >::iterator, bool > it_added =
652 links.insert( SMESH_TLink( n1, n2 ));
653 if ( !it_added.second )
654 // As we do NOT(!) check if mesh is manifold, we believe that a link can
655 // encounter once or twice only (not three times), we erase a link as soon
656 // as it encounters twice to speed up search in the <links> map.
657 links.erase( it_added.first );
660 // the links remaining in the <links> should all be on EDGE
661 set< SMESH_TLink >::iterator linkIt = links.begin();
662 for ( ; linkIt != links.end(); ++linkIt )
664 const SMESH_TLink& link = *linkIt;
665 if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
666 link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
669 // TODO: to check orientation
672 case TopAbs_SOLID: { // ====================== 3D =====================
674 SMDS_ElemIteratorPtr vIt = smDS->GetElements();
678 SMDS_VolumeTool vTool;
679 while ( !vIt->more() )
681 if (!vTool.Set( vIt->next() ))
684 for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
685 if ( vTool.IsFreeFace( iF ))
687 int nbN = vTool.NbFaceNodes( iF );
688 const SMDS_MeshNode** nodes = vTool.GetFaceNodes( iF );
689 for ( int i = 0; i < nbN; ++i )
690 if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
701 //================================================================================
703 * \brief Sets event listener to submeshes if necessary
704 * \param subMesh - submesh where algo is set
706 * After being set, event listener is notified on each event of a submesh.
707 * By default non listener is set
709 //================================================================================
711 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
715 //================================================================================
717 * \brief Allow algo to do something after persistent restoration
718 * \param subMesh - restored submesh
720 * This method is called only if a submesh has HYP_OK algo_state.
722 //================================================================================
724 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
728 //================================================================================
730 * \brief Computes mesh without geometry
731 * \param aMesh - the mesh
732 * \param aHelper - helper that must be used for adding elements to \aaMesh
733 * \retval bool - is a success
735 //================================================================================
737 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
739 return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
742 //=======================================================================
743 //function : CancelCompute
744 //purpose : Sets _computeCanceled to true. It's usage depends on
745 // * implementation of a particular mesher.
746 //=======================================================================
748 void SMESH_Algo::CancelCompute()
750 _computeCanceled = true;
751 _error = COMPERR_CANCELED;
754 //================================================================================
756 * If possible, returns progress of computation [0.,1.]
758 //================================================================================
760 double SMESH_Algo::GetProgress() const
765 //================================================================================
767 * \brief store error and comment and then return ( error == COMPERR_OK )
769 //================================================================================
771 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
775 return ( error == COMPERR_OK );
778 //================================================================================
780 * \brief store error and return ( error == COMPERR_OK )
782 //================================================================================
784 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
787 _error = error->myName;
788 _comment = error->myComment;
789 _badInputElements = error->myBadElements;
790 return error->IsOK();
795 //================================================================================
797 * \brief return compute error
799 //================================================================================
801 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
803 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
804 // hope this method is called by only SMESH_subMesh after this->Compute()
805 err->myBadElements.splice( err->myBadElements.end(),
806 (list<const SMDS_MeshElement*>&) _badInputElements );
810 //================================================================================
812 * \brief initialize compute error before call of Compute()
814 //================================================================================
816 void SMESH_Algo::InitComputeError()
820 list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
821 for ( ; elem != _badInputElements.end(); ++elem )
822 if ( (*elem)->GetID() < 1 )
824 _badInputElements.clear();
826 _computeCanceled = false;
831 //================================================================================
833 * \brief Return compute progress by nb of calls of this method
835 //================================================================================
837 double SMESH_Algo::GetProgressByTic() const
840 for ( size_t i = 0; i < _smToCompute.size(); ++i )
841 computeCost += _smToCompute[i]->GetComputeCost();
843 const_cast<SMESH_Algo*>( this )->_progressTic++;
845 double x = 5 * _progressTic;
846 x = ( x < computeCost ) ? ( x / computeCost ) : 1.;
847 return 0.9 * sin( x * M_PI / 2 );
850 //================================================================================
852 * \brief store a bad input element preventing computation,
853 * which may be a temporary one i.e. not residing the mesh,
854 * then it will be deleted by InitComputeError()
856 //================================================================================
858 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
861 _badInputElements.push_back( elem );
864 //=======================================================================
865 //function : addBadInputElements
866 //purpose : store a bad input elements or nodes preventing computation
867 //=======================================================================
869 void SMESH_Algo::addBadInputElements(const SMESHDS_SubMesh* sm,
876 SMDS_NodeIteratorPtr nIt = sm->GetNodes();
877 while ( nIt->more() ) addBadInputElement( nIt->next() );
881 SMDS_ElemIteratorPtr eIt = sm->GetElements();
882 while ( eIt->more() ) addBadInputElement( eIt->next() );
887 //=============================================================================
891 //=============================================================================
893 // int SMESH_Algo::NumberOfWires(const TopoDS_Shape& S)
896 // for (TopExp_Explorer exp(S,TopAbs_WIRE); exp.More(); exp.Next())
901 //=============================================================================
905 //=============================================================================
907 int SMESH_Algo::NumberOfPoints(SMESH_Mesh& aMesh, const TopoDS_Wire& W)
910 for (TopExp_Explorer exp(W,TopAbs_EDGE); exp.More(); exp.Next()) {
911 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
912 int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
915 nbPoints += nb + 1; // internal points plus 1 vertex of 2 (last point ?)
921 //================================================================================
923 * Method in which an algorithm generating a structured mesh
924 * fixes positions of in-face nodes after there movement
925 * due to insertion of viscous layers.
927 //================================================================================
929 bool SMESH_2D_Algo::FixInternalNodes(const SMESH_ProxyMesh& mesh,
930 const TopoDS_Face& face)
932 const SMESHDS_SubMesh* smDS = mesh.GetSubMesh(face);
933 if ( !smDS || smDS->NbElements() < 1 )
936 SMESH_MesherHelper helper( *mesh.GetMesh() );
938 // get all faces from a proxy sub-mesh
939 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TIterator;
940 TIDSortedElemSet allFaces( TIterator( smDS->GetElements() ), TIterator() );
941 TIDSortedElemSet avoidSet, firstRowQuads;
943 // indices of nodes to pass to a neighbour quad using SMESH_MeshAlgos::FindFaceInSet()
946 // get two first rows of nodes by passing through the first row of faces
947 vector< vector< const SMDS_MeshNode* > > nodeRows;
948 int iRow1 = 0, iRow2 = 1;
949 const SMDS_MeshElement* quad;
951 // look for a corner quadrangle and it's corner node
952 const SMDS_MeshElement* cornerQuad = 0;
953 int cornerNodeInd = -1;
954 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
955 while ( !cornerQuad && fIt->more() )
957 cornerQuad = fIt->next();
958 if ( cornerQuad->NbCornerNodes() != 4 )
960 SMDS_NodeIteratorPtr nIt = cornerQuad->nodeIterator();
961 for ( int i = 0; i < 4; ++i )
963 int nbInverseQuads = 0;
964 SMDS_ElemIteratorPtr fIt = nIt->next()->GetInverseElementIterator(SMDSAbs_Face);
965 while ( fIt->more() )
966 nbInverseQuads += allFaces.count( fIt->next() );
967 if ( nbInverseQuads == 1 )
968 cornerNodeInd = i, i = 4;
970 if ( cornerNodeInd < 0 )
973 if ( !cornerQuad || cornerNodeInd < 0 )
976 iN1 = helper.WrapIndex( cornerNodeInd + 1, 4 );
977 iN2 = helper.WrapIndex( cornerNodeInd + 2, 4 );
978 int iN3 = helper.WrapIndex( cornerNodeInd + 3, 4 );
980 nodeRows[iRow1].push_back( cornerQuad->GetNode( cornerNodeInd ));
981 nodeRows[iRow1].push_back( cornerQuad->GetNode( iN1 ));
982 nodeRows[iRow2].push_back( cornerQuad->GetNode( iN3 ));
983 nodeRows[iRow2].push_back( cornerQuad->GetNode( iN2 ));
984 firstRowQuads.insert( cornerQuad );
986 // pass through the rest quads in a face row
991 avoidSet.insert( quad );
992 if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1].back(),
993 nodeRows[iRow2].back(),
994 allFaces, avoidSet, &iN1, &iN2)))
996 nodeRows[iRow1].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
997 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
998 if ( quad->NbCornerNodes() != 4 )
1002 if ( nodeRows[iRow1].size() < 3 )
1003 return true; // there is nothing to fix
1006 nodeRows.reserve( smDS->NbElements() / nodeRows[iRow1].size() );
1008 // get the rest node rows
1013 // get the first quad in the next face row
1014 if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][0],
1016 allFaces, /*avoid=*/firstRowQuads,
1019 if ( quad->NbCornerNodes() != 4 )
1021 nodeRows.resize( iRow2+1 );
1022 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
1023 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1024 firstRowQuads.insert( quad );
1028 break; // no more rows
1031 // pass through the rest quads in a face row
1035 avoidSet.insert( quad );
1036 if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][ nodeRows[iRow2].size()-1 ],
1037 nodeRows[iRow2].back(),
1038 allFaces, avoidSet, &iN1, &iN2)))
1040 if ( quad->NbCornerNodes() != 4 )
1042 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
1045 if ( nodeRows[iRow1].size() != nodeRows[iRow2].size() )
1048 if ( nodeRows.size() < 3 )
1049 return true; // there is nothing to fix
1051 // get params of the first (bottom) and last (top) node rows
1052 UVPtStructVec uvB( nodeRows[0].size() ), uvT( nodeRows[0].size() );
1053 for ( int isBot = 0; isBot < 2; ++isBot )
1055 UVPtStructVec & uvps = isBot ? uvB : uvT;
1056 vector< const SMDS_MeshNode* >& nodes = nodeRows[ isBot ? 0 : nodeRows.size()-1 ];
1057 for ( size_t i = 0; i < nodes.size(); ++i )
1059 uvps[i].node = nodes[i];
1060 gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1061 uvps[i].u = uv.Coord(1);
1062 uvps[i].v = uv.Coord(2);
1065 // calculate x (normalized param)
1066 for ( size_t i = 1; i < nodes.size(); ++i )
1067 uvps[i].x = uvps[i-1].x + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1068 for ( size_t i = 1; i < nodes.size(); ++i )
1069 uvps[i].x /= uvps.back().x;
1072 // get params of the left and right node rows
1073 UVPtStructVec uvL( nodeRows.size() ), uvR( nodeRows.size() );
1074 for ( int isLeft = 0; isLeft < 2; ++isLeft )
1076 UVPtStructVec & uvps = isLeft ? uvL : uvR;
1077 const int iCol = isLeft ? 0 : nodeRows[0].size() - 1;
1078 for ( size_t i = 0; i < nodeRows.size(); ++i )
1080 uvps[i].node = nodeRows[i][iCol];
1081 gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1082 uvps[i].u = uv.Coord(1);
1083 uvps[i].v = uv.Coord(2);
1086 // calculate y (normalized param)
1087 for ( size_t i = 1; i < nodeRows.size(); ++i )
1088 uvps[i].y = uvps[i-1].y + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1089 for ( size_t i = 1; i < nodeRows.size(); ++i )
1090 uvps[i].y /= uvps.back().y;
1093 // update node coordinates
1094 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
1095 Handle(Geom_Surface) S = BRep_Tool::Surface( face );
1096 gp_XY a0 ( uvB.front().u, uvB.front().v );
1097 gp_XY a1 ( uvB.back().u, uvB.back().v );
1098 gp_XY a2 ( uvT.back().u, uvT.back().v );
1099 gp_XY a3 ( uvT.front().u, uvT.front().v );
1100 for ( size_t iRow = 1; iRow < nodeRows.size()-1; ++iRow )
1102 gp_XY p1 ( uvR[ iRow ].u, uvR[ iRow ].v );
1103 gp_XY p3 ( uvL[ iRow ].u, uvL[ iRow ].v );
1104 const double y0 = uvL[ iRow ].y;
1105 const double y1 = uvR[ iRow ].y;
1106 for ( size_t iCol = 1; iCol < nodeRows[0].size()-1; ++iCol )
1108 gp_XY p0 ( uvB[ iCol ].u, uvB[ iCol ].v );
1109 gp_XY p2 ( uvT[ iCol ].u, uvT[ iCol ].v );
1110 const double x0 = uvB[ iCol ].x;
1111 const double x1 = uvT[ iCol ].x;
1112 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1113 double y = y0 + x * (y1 - y0);
1114 gp_XY uv = helper.calcTFI( x, y, a0,a1,a2,a3, p0,p1,p2,p3 );
1115 gp_Pnt p = S->Value( uv.Coord(1), uv.Coord(2));
1116 const SMDS_MeshNode* n = nodeRows[iRow][iCol];
1117 meshDS->MoveNode( n, p.X(), p.Y(), p.Z() );
1118 if ( SMDS_FacePosition* pos = dynamic_cast< SMDS_FacePosition*>( n->GetPosition() ))
1119 pos->SetParameters( uv.Coord(1), uv.Coord(2) );