1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : SMESH_Algo.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Algo.hxx"
30 #include "SMESH_Comment.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMDS_FacePosition.hxx"
35 #include "SMDS_EdgePosition.hxx"
36 #include "SMDS_MeshElement.hxx"
37 #include "SMDS_MeshNode.hxx"
38 #include "SMESHDS_Mesh.hxx"
39 #include "SMESHDS_SubMesh.hxx"
41 #include <BRepAdaptor_Curve.hxx>
42 #include <BRepLProp.hxx>
43 #include <BRep_Tool.hxx>
44 #include <GCPnts_AbscissaPoint.hxx>
45 #include <GeomAdaptor_Curve.hxx>
46 #include <Geom_Surface.hxx>
48 #include <TopLoc_Location.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Vertex.hxx>
56 #include <gp_Pnt2d.hxx>
59 #include <Standard_ErrorHandler.hxx>
60 #include <Standard_Failure.hxx>
62 #include "utilities.h"
68 //=============================================================================
72 //=============================================================================
74 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
75 SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
77 gen->_mapAlgo[hypId] = this;
79 _onlyUnaryInput = _requireDescretBoundary = _requireShape = true;
80 _quadraticMesh = false;
84 //=============================================================================
88 //=============================================================================
90 SMESH_Algo::~SMESH_Algo()
94 //=============================================================================
96 * Usually an algoritm has nothing to save
98 //=============================================================================
100 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
101 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
103 //=============================================================================
107 //=============================================================================
109 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
111 return _compatibleHypothesis;
114 //=============================================================================
116 * List the hypothesis used by the algorithm associated to the shape.
117 * Hypothesis associated to father shape -are- taken into account (see
118 * GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
119 * the algorithm. This method could be surcharged by specific algorithms, in
120 * case of several hypothesis simultaneously applicable.
122 //=============================================================================
124 const list <const SMESHDS_Hypothesis *> &
125 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh & aMesh,
126 const TopoDS_Shape & aShape,
127 const bool ignoreAuxiliary)
129 _usedHypList.clear();
130 SMESH_HypoFilter filter;
131 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
133 aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
134 if ( ignoreAuxiliary && _usedHypList.size() > 1 )
135 _usedHypList.clear(); //only one compatible hypothesis allowed
140 //=============================================================================
142 * List the relevant hypothesis associated to the shape. Relevant hypothesis
143 * have a name (type) listed in the algorithm. Hypothesis associated to
144 * father shape -are not- taken into account (see GetUsedHypothesis)
146 //=============================================================================
148 const list<const SMESHDS_Hypothesis *> &
149 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh & aMesh,
150 const TopoDS_Shape & aShape,
151 const bool ignoreAuxiliary)
153 _appliedHypList.clear();
154 SMESH_HypoFilter filter;
155 if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
156 aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
158 return _appliedHypList;
161 //=============================================================================
163 * Compute length of an edge
165 //=============================================================================
167 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
169 double UMin = 0, UMax = 0;
170 if (BRep_Tool::Degenerated(E))
173 Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
174 GeomAdaptor_Curve AdaptCurve(C);
175 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
179 //================================================================================
181 * \brief Find out elements orientation on a geometrical face
182 * \param theFace - The face correctly oriented in the shape being meshed
183 * \param theMeshDS - The mesh data structure
184 * \retval bool - true if the face normal and the normal of first element
185 * in the correspoding submesh point in different directions
187 //================================================================================
189 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face& theFace,
190 SMESHDS_Mesh* theMeshDS)
192 if ( theFace.IsNull() || !theMeshDS )
195 // find out orientation of a meshed face
196 int faceID = theMeshDS->ShapeToIndex( theFace );
197 TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
198 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
200 const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
201 if ( !aSubMeshDSFace )
204 // find element with node located on face and get its normal
205 const SMDS_FacePosition* facePos = 0;
209 bool normalOK = false;
210 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
211 while ( iteratorElem->more() ) // loop on elements on theFace
213 const SMDS_MeshElement* elem = iteratorElem->next();
214 if ( elem && elem->NbNodes() > 2 ) {
215 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
216 const SMDS_FacePosition* fPos = 0;
218 while ( nodesIt->more() ) { // loop on nodes
219 const SMDS_MeshNode* node
220 = static_cast<const SMDS_MeshNode *>(nodesIt->next());
222 nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
224 const SMDS_PositionPtr& pos = node->GetPosition();
225 if ( !pos ) continue;
226 if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
227 fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
229 else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
230 vID = pos->GetShapeId();
233 if ( fPos || ( !normalOK && vID )) {
235 gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
236 if ( v01.SquareMagnitude() > RealSmall() &&
237 v02.SquareMagnitude() > RealSmall() )
240 normalOK = ( Ne.SquareMagnitude() > RealSmall() );
242 // we need position on theFace or at least on vertex
245 if ((facePos = fPos))
254 // node position on face
257 u = facePos->GetUParameter();
258 v = facePos->GetVParameter();
260 else if ( vertexID ) {
261 TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
262 if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
264 gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
273 // face normal at node position
275 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
276 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
278 surf->D1( u, v, nPnt[0], d1u, d1v );
279 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
281 if ( theFace.Orientation() == TopAbs_REVERSED )
287 //================================================================================
289 * \brief Initialize my parameter values by the mesh built on the geometry
290 * \param theMesh - the built mesh
291 * \param theShape - the geometry of interest
292 * \retval bool - true if parameter values have been successfully defined
294 * Just return false as the algorithm does not hold parameters values
296 //================================================================================
298 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
299 const TopoDS_Shape& /*theShape*/)
304 //================================================================================
306 * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
307 * \param theMesh - The mesh containing nodes
308 * \param theEdge - The geometrical edge of interest
309 * \param theParams - The resulting vector of sorted node parameters
310 * \retval bool - false if not all parameters are OK
312 //================================================================================
314 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
315 const TopoDS_Edge& theEdge,
316 vector< double > & theParams)
320 if ( !theMesh || theEdge.IsNull() )
323 SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
324 if ( !eSubMesh || !eSubMesh->GetElements()->more() )
325 return false; // edge is not meshed
328 set < double > paramSet;
331 // loop on nodes of an edge: sort them by param on edge
332 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
333 while ( nIt->more() )
335 const SMDS_MeshNode* node = nIt->next();
336 const SMDS_PositionPtr& pos = node->GetPosition();
337 if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
339 const SMDS_EdgePosition* epos =
340 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
341 paramSet.insert( epos->GetUParameter() );
345 // add vertex nodes params
347 BRep_Tool::Range(theEdge, f, l);
348 paramSet.insert( f );
349 paramSet.insert( l );
350 if ( paramSet.size() != nbEdgeNodes + 2 )
351 return false; // there are equal parameters
354 theParams.resize( paramSet.size() );
355 set < double >::iterator par = paramSet.begin();
356 vector< double >::iterator vecPar = theParams.begin();
357 for ( ; par != paramSet.end(); ++par, ++vecPar )
360 return theParams.size() > 1;
363 //================================================================================
365 * \brief Make filter recognize only compatible hypotheses
366 * \param theFilter - the filter to initialize
367 * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
369 //================================================================================
371 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
372 const bool ignoreAuxiliary) const
374 if ( !_compatibleHypothesis.empty() )
376 theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
377 for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
378 theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
380 if ( ignoreAuxiliary )
381 theFilter.AndNot( theFilter.IsAuxiliary() );
388 //================================================================================
390 * \brief Return continuity of two edges
391 * \param E1 - the 1st edge
392 * \param E2 - the 2nd edge
393 * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
395 //================================================================================
397 GeomAbs_Shape SMESH_Algo::Continuity(const TopoDS_Edge & E1,
398 const TopoDS_Edge & E2)
400 TopoDS_Vertex V = TopExp::LastVertex (E1, true);
401 if ( !V.IsSame( TopExp::FirstVertex(E2, true )))
402 if ( !TopExp::CommonVertex( E1, E2, V ))
404 Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
405 Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
406 BRepAdaptor_Curve C1( E1 ), C2( E2 );
408 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
411 return BRepLProp::Continuity(C1, C2, u1, u2);
413 catch (Standard_Failure) {
418 //================================================================================
420 * \brief Return the node built on a vertex
421 * \param V - the vertex
422 * \param meshDS - mesh
423 * \retval const SMDS_MeshNode* - found node or NULL
425 //================================================================================
427 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
428 SMESHDS_Mesh* meshDS)
430 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
431 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
438 //================================================================================
440 * \brief Sets event listener to submeshes if necessary
441 * \param subMesh - submesh where algo is set
443 * After being set, event listener is notified on each event of a submesh.
444 * By default non listener is set
446 //================================================================================
448 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
452 //================================================================================
454 * \brief Allow algo to do something after persistent restoration
455 * \param subMesh - restored submesh
457 * This method is called only if a submesh has HYP_OK algo_state.
459 //================================================================================
461 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
465 //================================================================================
467 * \brief Computes mesh without geometry
468 * \param aMesh - the mesh
469 * \param aHelper - helper that must be used for adding elements to \aaMesh
470 * \retval bool - is a success
472 //================================================================================
474 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
476 return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
479 //================================================================================
481 * \brief store error and comment and then return ( error == COMPERR_OK )
483 //================================================================================
485 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
489 return ( error == COMPERR_OK );
492 //================================================================================
494 * \brief store error and return ( error == COMPERR_OK )
496 //================================================================================
498 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
501 _error = error->myName;
502 _comment = error->myComment;
503 return error->IsOK();
508 //================================================================================
510 * \brief return compute error
512 //================================================================================
514 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
516 return SMESH_ComputeError::New( _error, _comment, this );
519 //================================================================================
521 * \brief initialize compute error
523 //================================================================================
525 void SMESH_Algo::InitComputeError()