1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : 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 //gen->_mapAlgo[hypId] = this;
183 _onlyUnaryInput = _requireDiscreteBoundary = _requireShape = true;
184 _quadraticMesh = _supportSubmeshes = false;
186 for ( int i = 0; i < 4; ++i )
187 _neededLowerHyps[ i ] = false;
190 //=============================================================================
194 //=============================================================================
196 SMESH_Algo::~SMESH_Algo()
200 //=============================================================================
204 //=============================================================================
206 SMESH_0D_Algo::SMESH_0D_Algo(int hypId, int studyId, SMESH_Gen* gen)
207 : SMESH_Algo(hypId, studyId, gen)
209 _shapeType = (1 << TopAbs_VERTEX);
211 //gen->_map0D_Algo[hypId] = this;
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);
218 //gen->_map1D_Algo[hypId] = this;
220 SMESH_2D_Algo::SMESH_2D_Algo(int hypId, int studyId, SMESH_Gen* gen)
221 : SMESH_Algo(hypId, studyId, gen)
223 _shapeType = (1 << TopAbs_FACE);
225 //gen->_map2D_Algo[hypId] = this;
227 SMESH_3D_Algo::SMESH_3D_Algo(int hypId, int studyId, SMESH_Gen* gen)
228 : SMESH_Algo(hypId, studyId, gen)
230 _shapeType = (1 << TopAbs_SOLID);
232 //gen->_map3D_Algo[hypId] = this;
235 //=============================================================================
237 * Usually an algoritm has nothing to save
239 //=============================================================================
241 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
242 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
244 //=============================================================================
248 //=============================================================================
250 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
252 return _compatibleHypothesis;
255 //=============================================================================
257 * List the hypothesis used by the algorithm associated to the shape.
258 * Hypothesis associated to father shape -are- taken into account (see
259 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
260 * the algorithm. This method could be surcharged by specific algorithms, in
261 * case of several hypothesis simultaneously applicable.
263 //=============================================================================
265 const list <const SMESHDS_Hypothesis *> &
266 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
267 const TopoDS_Shape & aShape,
268 const bool ignoreAuxiliary) const
270 SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
271 me->_usedHypList.clear();
272 SMESH_HypoFilter filter;
273 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
275 aMesh.GetHypotheses( aShape, filter, me->_usedHypList, true );
276 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
277 me->_usedHypList.clear(); //only one compatible hypothesis allowed
282 //=============================================================================
284 * List the relevant hypothesis associated to the shape. Relevant hypothesis
285 * have a name (type) listed in the algorithm. Hypothesis associated to
286 * father shape -are not- taken into account (see GetUsedHypothesis)
288 //=============================================================================
290 const list<const SMESHDS_Hypothesis *> &
291 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
292 const TopoDS_Shape & aShape,
293 const bool ignoreAuxiliary) const
295 SMESH_Algo* me = const_cast< SMESH_Algo* >( this );
296 me->_appliedHypList.clear();
297 SMESH_HypoFilter filter;
298 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
299 aMesh.GetHypotheses( aShape, filter, me->_appliedHypList, false );
301 return _appliedHypList;
304 //=============================================================================
306 * Compute length of an edge
308 //=============================================================================
310 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
312 double UMin = 0, UMax = 0;
313 if (BRep_Tool::Degenerated(E))
316 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
317 GeomAdaptor_Curve AdaptCurve(C, UMin, UMax); //range is important for periodic curves
318 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
322 //================================================================================
324 * \brief Just return false as the algorithm does not hold parameters values
326 //================================================================================
328 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
329 const TopoDS_Shape& /*theShape*/)
333 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
337 //================================================================================
339 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
340 * \param theMesh - The mesh containing nodes
341 * \param theEdge - The geometrical edge of interest
342 * \param theParams - The resulting vector of sorted node parameters
343 * \retval bool - false if not all parameters are OK
345 //================================================================================
347 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
348 const TopoDS_Edge& theEdge,
349 vector< double > & theParams)
353 if ( !theMesh || theEdge.IsNull() )
356 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
357 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
358 return false; // edge is not meshed
360 //int nbEdgeNodes = 0;
361 set < double > paramSet;
364 // loop on nodes of an edge: sort them by param on edge
365 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
366 while ( nIt->more() )
368 const SMDS_MeshNode* node = nIt->next();
369 const SMDS_PositionPtr& pos = node->GetPosition();
370 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
372 const SMDS_EdgePosition* epos =
373 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
374 if ( !paramSet.insert( epos->GetUParameter() ).second )
375 return false; // equal parameters
378 // add vertex nodes params
380 TopExp::Vertices( theEdge, V1, V2);
381 if ( VertexNode( V1, theMesh ) &&
382 !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
383 return false; // there are equal parameters
384 if ( VertexNode( V2, theMesh ) &&
385 !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
386 return false; // there are equal parameters
389 theParams.resize( paramSet.size() );
390 set < double >::iterator par = paramSet.begin();
391 vector< double >::iterator vecPar = theParams.begin();
392 for ( ; par != paramSet.end(); ++par, ++vecPar )
395 return theParams.size() > 1;
398 //================================================================================
400 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
401 * \param theMesh - The mesh containing nodes
402 * \param theEdge - The geometrical edge of interest
403 * \param theParams - The resulting vector of sorted node parameters
404 * \retval bool - false if not all parameters are OK
406 //================================================================================
408 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh* theMesh,
409 const TopoDS_Edge& theEdge,
410 const bool ignoreMediumNodes,
411 map< double, const SMDS_MeshNode* > & theNodes)
415 if ( !theMesh || theEdge.IsNull() )
418 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
419 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
420 return false; // edge is not meshed
423 set < double > paramSet;
426 // loop on nodes of an edge: sort them by param on edge
427 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
428 while ( nIt->more() )
430 const SMDS_MeshNode* node = nIt->next();
431 if ( ignoreMediumNodes ) {
432 SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
433 if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
436 const SMDS_PositionPtr& pos = node->GetPosition();
437 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
439 const SMDS_EdgePosition* epos =
440 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
441 theNodes.insert( theNodes.end(), make_pair( epos->GetUParameter(), node ));
442 //MESSAGE("U " << epos->GetUParameter() << " ID " << node->GetID());
447 TopoDS_Vertex v1, v2;
448 TopExp::Vertices(theEdge, v1, v2);
449 const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
450 const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
451 //MESSAGE("Vertices ID " << n1->GetID() << " " << n2->GetID());
453 BRep_Tool::Range(theEdge, f, l);
454 if ( v1.Orientation() != TopAbs_FORWARD )
456 if ( n1 && ++nbNodes )
457 theNodes.insert( make_pair( f, n1 ));
458 if ( n2 && ++nbNodes )
459 theNodes.insert( make_pair( l, n2 ));
461 return theNodes.size() == nbNodes;
464 //================================================================================
466 * \brief Make filter recognize only compatible hypotheses
467 * \param theFilter - the filter to initialize
468 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
470 //================================================================================
472 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
473 const bool ignoreAuxiliary) const
475 if ( !_compatibleHypothesis.empty() )
477 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
478 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
479 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
481 if ( ignoreAuxiliary )
482 theFilter.AndNot( theFilter.IsAuxiliary() );
489 //================================================================================
491 * \brief Return continuity of two edges
492 * \param E1 - the 1st edge
493 * \param E2 - the 2nd edge
494 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
496 //================================================================================
498 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
501 //E1.Orientation(TopAbs_FORWARD), E2.Orientation(TopAbs_FORWARD); // avoid pb with internal edges
502 if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
503 E1.Orientation( TopAbs_FORWARD );
504 if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
505 E2.Orientation( TopAbs_FORWARD );
507 TopoDS_Vertex V, VV1[2], VV2[2];
508 TopExp::Vertices( E1, VV1[0], VV1[1], true );
509 TopExp::Vertices( E2, VV2[0], VV2[1], true );
510 if ( VV1[1].IsSame( VV2[0] )) { V = VV1[1]; }
511 else if ( VV1[0].IsSame( VV2[1] )) { V = VV1[0]; }
512 else if ( VV1[1].IsSame( VV2[1] )) { V = VV1[1]; E1.Reverse(); }
513 else if ( VV1[0].IsSame( VV2[0] )) { V = VV1[0]; E1.Reverse(); }
514 else { return GeomAbs_C0; }
516 Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
517 Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
518 BRepAdaptor_Curve C1( E1 ), C2( E2 );
519 Standard_Real tol = BRep_Tool::Tolerance( V );
520 Standard_Real angTol = 2e-3;
522 #if OCC_VERSION_LARGE > 0x06010000
525 return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
527 catch (Standard_Failure) {
532 //================================================================================
534 * \brief Return the node built on a vertex
535 * \param V - the vertex
536 * \param meshDS - mesh
537 * \retval const SMDS_MeshNode* - found node or NULL
539 //================================================================================
541 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
542 const SMESHDS_Mesh* meshDS)
544 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
545 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
552 //=======================================================================
553 //function : GetMeshError
554 //purpose : Finds topological errors of a sub-mesh
555 //WARNING : 1D check is NOT implemented so far
556 //=======================================================================
558 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
560 EMeshError err = MEr_OK;
562 SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
566 switch ( subMesh->GetSubShape().ShapeType() )
568 case TopAbs_FACE: { // ====================== 2D =====================
570 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
574 // We check that olny links on EDGEs encouter once, the rest links, twice
575 set< SMESH_TLink > links;
576 while ( fIt->more() )
578 const SMDS_MeshElement* f = fIt->next();
579 int nbNodes = f->NbCornerNodes(); // ignore medium nodes
580 for ( int i = 0; i < nbNodes; ++i )
582 const SMDS_MeshNode* n1 = f->GetNode( i );
583 const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
584 std::pair< set< SMESH_TLink >::iterator, bool > it_added =
585 links.insert( SMESH_TLink( n1, n2 ));
586 if ( !it_added.second )
587 // As we do NOT(!) check if mesh is manifold, we believe that a link can
588 // encounter once or twice only (not three times), we erase a link as soon
589 // as it encounters twice to speed up search in the <links> map.
590 links.erase( it_added.first );
593 // the links remaining in the <links> should all be on EDGE
594 set< SMESH_TLink >::iterator linkIt = links.begin();
595 for ( ; linkIt != links.end(); ++linkIt )
597 const SMESH_TLink& link = *linkIt;
598 if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
599 link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
602 // TODO: to check orientation
605 case TopAbs_SOLID: { // ====================== 3D =====================
607 SMDS_ElemIteratorPtr vIt = smDS->GetElements();
611 SMDS_VolumeTool vTool;
612 while ( !vIt->more() )
614 if (!vTool.Set( vIt->next() ))
617 for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
618 if ( vTool.IsFreeFace( iF ))
620 int nbN = vTool.NbFaceNodes( iF );
621 const SMDS_MeshNode** nodes = vTool.GetFaceNodes( iF );
622 for ( int i = 0; i < nbN; ++i )
623 if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
634 //================================================================================
636 * \brief Sets event listener to submeshes if necessary
637 * \param subMesh - submesh where algo is set
639 * After being set, event listener is notified on each event of a submesh.
640 * By default non listener is set
642 //================================================================================
644 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
648 //================================================================================
650 * \brief Allow algo to do something after persistent restoration
651 * \param subMesh - restored submesh
653 * This method is called only if a submesh has HYP_OK algo_state.
655 //================================================================================
657 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
661 //================================================================================
663 * \brief Computes mesh without geometry
664 * \param aMesh - the mesh
665 * \param aHelper - helper that must be used for adding elements to \aaMesh
666 * \retval bool - is a success
668 //================================================================================
670 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
672 return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
675 //=======================================================================
676 //function : CancelCompute
677 //purpose : Sets _computeCanceled to true. It's usage depends on
678 // * implementation of a particular mesher.
679 //=======================================================================
681 void SMESH_Algo::CancelCompute()
683 _computeCanceled = true;
684 _error = COMPERR_CANCELED;
687 //================================================================================
689 * If possible, returns progress of computation [0.,1.]
691 //================================================================================
693 double SMESH_Algo::GetProgress() const
698 //================================================================================
700 * \brief store error and comment and then return ( error == COMPERR_OK )
702 //================================================================================
704 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
708 return ( error == COMPERR_OK );
711 //================================================================================
713 * \brief store error and return ( error == COMPERR_OK )
715 //================================================================================
717 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
720 _error = error->myName;
721 _comment = error->myComment;
722 _badInputElements = error->myBadElements;
723 return error->IsOK();
728 //================================================================================
730 * \brief return compute error
732 //================================================================================
734 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
736 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
737 // hope this method is called by only SMESH_subMesh after this->Compute()
738 err->myBadElements.splice( err->myBadElements.end(),
739 (list<const SMDS_MeshElement*>&) _badInputElements );
743 //================================================================================
745 * \brief initialize compute error before call of Compute()
747 //================================================================================
749 void SMESH_Algo::InitComputeError()
753 list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
754 for ( ; elem != _badInputElements.end(); ++elem )
755 if ( (*elem)->GetID() < 1 )
757 _badInputElements.clear();
759 _computeCanceled = false;
764 //================================================================================
766 * \brief Return compute progress by nb of calls of this method
768 //================================================================================
770 double SMESH_Algo::GetProgressByTic() const
773 for ( size_t i = 0; i < _smToCompute.size(); ++i )
774 computeCost += _smToCompute[i]->GetComputeCost();
776 const_cast<SMESH_Algo*>( this )->_progressTic++;
778 double x = 5 * _progressTic;
779 x = ( x < computeCost ) ? ( x / computeCost ) : 1.;
780 return 0.9 * sin( x * M_PI / 2 );
783 //================================================================================
785 * \brief store a bad input element preventing computation,
786 * which may be a temporary one i.e. not residing the mesh,
787 * then it will be deleted by InitComputeError()
789 //================================================================================
791 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
794 _badInputElements.push_back( elem );
797 //=======================================================================
798 //function : addBadInputElements
799 //purpose : store a bad input elements or nodes preventing computation
800 //=======================================================================
802 void SMESH_Algo::addBadInputElements(const SMESHDS_SubMesh* sm,
809 SMDS_NodeIteratorPtr nIt = sm->GetNodes();
810 while ( nIt->more() ) addBadInputElement( nIt->next() );
814 SMDS_ElemIteratorPtr eIt = sm->GetElements();
815 while ( eIt->more() ) addBadInputElement( eIt->next() );
820 //=============================================================================
824 //=============================================================================
826 // int SMESH_Algo::NumberOfWires(const TopoDS_Shape& S)
829 // for (TopExp_Explorer exp(S,TopAbs_WIRE); exp.More(); exp.Next())
834 //=============================================================================
838 //=============================================================================
840 int SMESH_Algo::NumberOfPoints(SMESH_Mesh& aMesh, const TopoDS_Wire& W)
843 for (TopExp_Explorer exp(W,TopAbs_EDGE); exp.More(); exp.Next()) {
844 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
845 int nb = aMesh.GetSubMesh(E)->GetSubMeshDS()->NbNodes();
848 nbPoints += nb + 1; // internal points plus 1 vertex of 2 (last point ?)
854 //================================================================================
856 * Method in which an algorithm generating a structured mesh
857 * fixes positions of in-face nodes after there movement
858 * due to insertion of viscous layers.
860 //================================================================================
862 bool SMESH_2D_Algo::FixInternalNodes(const SMESH_ProxyMesh& mesh,
863 const TopoDS_Face& face)
865 const SMESHDS_SubMesh* smDS = mesh.GetSubMesh(face);
866 if ( !smDS || smDS->NbElements() < 1 )
869 SMESH_MesherHelper helper( *mesh.GetMesh() );
871 // get all faces from a proxy sub-mesh
872 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TIterator;
873 TIDSortedElemSet allFaces( TIterator( smDS->GetElements() ), TIterator() );
874 TIDSortedElemSet avoidSet, firstRowQuads;
876 // indices of nodes to pass to a neighbour quad using SMESH_MeshAlgos::FindFaceInSet()
879 // get two first rows of nodes by passing through the first row of faces
880 vector< vector< const SMDS_MeshNode* > > nodeRows;
881 int iRow1 = 0, iRow2 = 1;
882 const SMDS_MeshElement* quad;
884 // look for a corner quadrangle and it's corner node
885 const SMDS_MeshElement* cornerQuad = 0;
886 int cornerNodeInd = -1;
887 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
888 while ( !cornerQuad && fIt->more() )
890 cornerQuad = fIt->next();
891 if ( cornerQuad->NbCornerNodes() != 4 )
893 SMDS_NodeIteratorPtr nIt = cornerQuad->nodeIterator();
894 for ( int i = 0; i < 4; ++i )
896 int nbInverseQuads = 0;
897 SMDS_ElemIteratorPtr fIt = nIt->next()->GetInverseElementIterator(SMDSAbs_Face);
898 while ( fIt->more() )
899 nbInverseQuads += allFaces.count( fIt->next() );
900 if ( nbInverseQuads == 1 )
901 cornerNodeInd = i, i = 4;
903 if ( cornerNodeInd < 0 )
906 if ( !cornerQuad || cornerNodeInd < 0 )
909 iN1 = helper.WrapIndex( cornerNodeInd + 1, 4 );
910 iN2 = helper.WrapIndex( cornerNodeInd + 2, 4 );
911 int iN3 = helper.WrapIndex( cornerNodeInd + 3, 4 );
913 nodeRows[iRow1].push_back( cornerQuad->GetNode( cornerNodeInd ));
914 nodeRows[iRow1].push_back( cornerQuad->GetNode( iN1 ));
915 nodeRows[iRow2].push_back( cornerQuad->GetNode( iN3 ));
916 nodeRows[iRow2].push_back( cornerQuad->GetNode( iN2 ));
917 firstRowQuads.insert( cornerQuad );
919 // pass through the rest quads in a face row
924 avoidSet.insert( quad );
925 if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1].back(),
926 nodeRows[iRow2].back(),
927 allFaces, avoidSet, &iN1, &iN2)))
929 nodeRows[iRow1].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
930 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
931 if ( quad->NbCornerNodes() != 4 )
935 if ( nodeRows[iRow1].size() < 3 )
936 return true; // there is nothing to fix
939 nodeRows.reserve( smDS->NbElements() / nodeRows[iRow1].size() );
941 // get the rest node rows
946 // get the first quad in the next face row
947 if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][0],
949 allFaces, /*avoid=*/firstRowQuads,
952 if ( quad->NbCornerNodes() != 4 )
954 nodeRows.resize( iRow2+1 );
955 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN2 + 2, 4 )));
956 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
957 firstRowQuads.insert( quad );
961 break; // no more rows
964 // pass through the rest quads in a face row
968 avoidSet.insert( quad );
969 if (( quad = SMESH_MeshAlgos::FindFaceInSet( nodeRows[iRow1][ nodeRows[iRow2].size()-1 ],
970 nodeRows[iRow2].back(),
971 allFaces, avoidSet, &iN1, &iN2)))
973 if ( quad->NbCornerNodes() != 4 )
975 nodeRows[iRow2].push_back( quad->GetNode( helper.WrapIndex( iN1 + 2, 4 )));
978 if ( nodeRows[iRow1].size() != nodeRows[iRow2].size() )
981 if ( nodeRows.size() < 3 )
982 return true; // there is nothing to fix
984 // get params of the first (bottom) and last (top) node rows
985 UVPtStructVec uvB( nodeRows[0].size() ), uvT( nodeRows[0].size() );
986 for ( int isBot = 0; isBot < 2; ++isBot )
988 UVPtStructVec & uvps = isBot ? uvB : uvT;
989 vector< const SMDS_MeshNode* >& nodes = nodeRows[ isBot ? 0 : nodeRows.size()-1 ];
990 for ( size_t i = 0; i < nodes.size(); ++i )
992 uvps[i].node = nodes[i];
993 gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
994 uvps[i].u = uv.Coord(1);
995 uvps[i].v = uv.Coord(2);
998 // calculate x (normalized param)
999 for ( size_t i = 1; i < nodes.size(); ++i )
1000 uvps[i].x = uvps[i-1].x + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1001 for ( size_t i = 1; i < nodes.size(); ++i )
1002 uvps[i].x /= uvps.back().x;
1005 // get params of the left and right node rows
1006 UVPtStructVec uvL( nodeRows.size() ), uvR( nodeRows.size() );
1007 for ( int isLeft = 0; isLeft < 2; ++isLeft )
1009 UVPtStructVec & uvps = isLeft ? uvL : uvR;
1010 const int iCol = isLeft ? 0 : nodeRows[0].size() - 1;
1011 for ( size_t i = 0; i < nodeRows.size(); ++i )
1013 uvps[i].node = nodeRows[i][iCol];
1014 gp_XY uv = helper.GetNodeUV( face, uvps[i].node );
1015 uvps[i].u = uv.Coord(1);
1016 uvps[i].v = uv.Coord(2);
1019 // calculate y (normalized param)
1020 for ( size_t i = 1; i < nodeRows.size(); ++i )
1021 uvps[i].y = uvps[i-1].y + SMESH_TNodeXYZ( uvps[i-1].node ).Distance( uvps[i].node );
1022 for ( size_t i = 1; i < nodeRows.size(); ++i )
1023 uvps[i].y /= uvps.back().y;
1026 // update node coordinates
1027 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
1028 Handle(Geom_Surface) S = BRep_Tool::Surface( face );
1029 gp_XY a0 ( uvB.front().u, uvB.front().v );
1030 gp_XY a1 ( uvB.back().u, uvB.back().v );
1031 gp_XY a2 ( uvT.back().u, uvT.back().v );
1032 gp_XY a3 ( uvT.front().u, uvT.front().v );
1033 for ( size_t iRow = 1; iRow < nodeRows.size()-1; ++iRow )
1035 gp_XY p1 ( uvR[ iRow ].u, uvR[ iRow ].v );
1036 gp_XY p3 ( uvL[ iRow ].u, uvL[ iRow ].v );
1037 const double y0 = uvL[ iRow ].y;
1038 const double y1 = uvR[ iRow ].y;
1039 for ( size_t iCol = 1; iCol < nodeRows[0].size()-1; ++iCol )
1041 gp_XY p0 ( uvB[ iCol ].u, uvB[ iCol ].v );
1042 gp_XY p2 ( uvT[ iCol ].u, uvT[ iCol ].v );
1043 const double x0 = uvB[ iCol ].x;
1044 const double x1 = uvT[ iCol ].x;
1045 double x = (x0 + y0 * (x1 - x0)) / (1 - (y1 - y0) * (x1 - x0));
1046 double y = y0 + x * (y1 - y0);
1047 gp_XY uv = helper.calcTFI( x, y, a0,a1,a2,a3, p0,p1,p2,p3 );
1048 gp_Pnt p = S->Value( uv.Coord(1), uv.Coord(2));
1049 const SMDS_MeshNode* n = nodeRows[iRow][iCol];
1050 meshDS->MoveNode( n, p.X(), p.Y(), p.Z() );
1051 if ( SMDS_FacePosition* pos = dynamic_cast< SMDS_FacePosition*>( n->GetPosition() ))
1052 pos->SetParameters( uv.Coord(1), uv.Coord(2) );