Salome HOME
6faae56c3204e19af317a5353c3606bda897ae84
[modules/smesh.git] / src / SMESH / SMESH_Algo.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_Algo.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMDS_FacePosition.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDS_MeshNode.hxx"
37 #include "SMESHDS_Mesh.hxx"
38 #include "SMESHDS_SubMesh.hxx"
39
40 #include <BRep_Tool.hxx>
41 #include <GCPnts_AbscissaPoint.hxx>
42 #include <GeomAdaptor_Curve.hxx>
43 #include <Geom_Surface.hxx>
44 #include <TopLoc_Location.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_ListOfShape.hxx>
47 #include <TopoDS.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <gp_Pnt.hxx>
50 #include <gp_Pnt2d.hxx>
51 #include <gp_Vec.hxx>
52
53 #include "utilities.h"
54
55 #include <algorithm>
56
57 //=============================================================================
58 /*!
59  *  
60  */
61 //=============================================================================
62
63 SMESH_Algo::SMESH_Algo(int hypId, int studyId,
64         SMESH_Gen * gen):SMESH_Hypothesis(hypId, studyId, gen)
65 {
66 //   _compatibleHypothesis.push_back("hypothese_bidon");
67         _type = ALGO;
68         gen->_mapAlgo[hypId] = this;
69
70         _onlyUnaryInput = _requireDescretBoundary = true;
71         _quadraticMesh = false;
72 }
73
74 //=============================================================================
75 /*!
76  *  
77  */
78 //=============================================================================
79
80 SMESH_Algo::~SMESH_Algo()
81 {
82 }
83
84 //=============================================================================
85 /*!
86  *  
87  */
88 //=============================================================================
89
90 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
91 {
92         return _compatibleHypothesis;
93 }
94
95 //=============================================================================
96 /*!
97  *  List the hypothesis used by the algorithm associated to the shape.
98  *  Hypothesis associated to father shape -are- taken into account (see
99  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
100  *  the algorithm. This method could be surcharged by specific algorithms, in 
101  *  case of several hypothesis simultaneously applicable.
102  */
103 //=============================================================================
104
105 const list <const SMESHDS_Hypothesis *> &
106 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
107                               const TopoDS_Shape & aShape,
108                               const bool           ignoreAuxiliary)
109 {
110   _usedHypList.clear();
111   SMESH_HypoFilter filter;
112   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
113   {
114     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
115     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
116       _usedHypList.clear();     //only one compatible hypothesis allowed
117   }
118   return _usedHypList;
119 }
120
121 //=============================================================================
122 /*!
123  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
124  *  have a name (type) listed in the algorithm. Hypothesis associated to
125  *  father shape -are not- taken into account (see GetUsedHypothesis)
126  */
127 //=============================================================================
128
129 const list<const SMESHDS_Hypothesis *> &
130 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
131                                  const TopoDS_Shape & aShape,
132                                  const bool           ignoreAuxiliary)
133 {
134   _appliedHypList.clear();
135   SMESH_HypoFilter filter;
136   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
137     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
138
139   return _appliedHypList;
140 }
141
142 //=============================================================================
143 /*!
144  *  Compute length of an edge
145  */
146 //=============================================================================
147
148 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
149 {
150         double UMin = 0, UMax = 0;
151         TopLoc_Location L;
152         if (BRep_Tool::Degenerated(E))
153                 return 0;
154         Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
155         GeomAdaptor_Curve AdaptCurve(C);
156         GCPnts_AbscissaPoint gabs;
157         double length = gabs.Length(AdaptCurve, UMin, UMax);
158         return length;
159 }
160
161 //================================================================================
162 /*!
163  * \brief Find out elements orientation on a geometrical face
164   * \param theFace - The face correctly oriented in the shape being meshed
165   * \param theMeshDS - The mesh data structure
166   * \retval bool - true if the face normal and the normal of first element
167   *                in the correspoding submesh point in different directions
168  */
169 //================================================================================
170
171 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
172                                     SMESHDS_Mesh*       theMeshDS)
173 {
174   if ( theFace.IsNull() || !theMeshDS )
175     return false;
176
177   // find out orientation of a meshed face
178   int faceID = theMeshDS->ShapeToIndex( theFace );
179   TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
180   bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
181
182   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
183   if ( !aSubMeshDSFace )
184     return isReversed;
185
186   // find element with node located on face and get its normal
187   const SMDS_FacePosition* facePos = 0;
188   int vertexID = 0;
189   gp_Pnt nPnt[3];
190   gp_Vec Ne;
191   bool normalOK = false;
192   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
193   while ( iteratorElem->more() ) // loop on elements on theFace
194   {
195     const SMDS_MeshElement* elem = iteratorElem->next();
196     if ( elem && elem->NbNodes() > 2 ) {
197       SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
198       const SMDS_FacePosition* fPos = 0;
199       int i = 0, vID = 0;
200       while ( nodesIt->more() ) { // loop on nodes
201         const SMDS_MeshNode* node
202           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
203         if ( i == 3 ) i = 2;
204         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
205         // check position
206         const SMDS_PositionPtr& pos = node->GetPosition();
207         if ( !pos ) continue;
208         if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
209           fPos = dynamic_cast< const SMDS_FacePosition* >( pos.get() );
210         }
211         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
212           vID = pos->GetShapeId();
213         }
214       }
215       if ( fPos || ( !normalOK && vID )) {
216         // compute normal
217         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
218         if ( v01.SquareMagnitude() > RealSmall() &&
219              v02.SquareMagnitude() > RealSmall() )
220         {
221           Ne = v01 ^ v02;
222           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
223         }
224         // we need position on theFace or at least on vertex
225         if ( normalOK ) {
226           vertexID = vID;
227           if ((facePos = fPos))
228             break;
229         }
230       }
231     }
232   }
233   if ( !normalOK )
234     return isReversed;
235
236   // node position on face
237   double u,v;
238   if ( facePos ) {
239     u = facePos->GetUParameter();
240     v = facePos->GetVParameter();
241   }
242   else if ( vertexID ) {
243     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
244     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
245       return isReversed;
246     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
247     u = uv.X();
248     v = uv.Y();
249   }
250   else
251   {
252     return isReversed;
253   }
254
255   // face normal at node position
256   TopLoc_Location loc;
257   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
258   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
259   gp_Vec d1u, d1v;
260   surf->D1( u, v, nPnt[0], d1u, d1v );
261   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
262
263   if ( theFace.Orientation() == TopAbs_REVERSED )
264     Nf.Reverse();
265
266   return Ne * Nf < 0.;
267 }
268
269 /*!
270  * \brief Make filter recognize only compatible hypotheses
271  * \param theFilter - the filter to initialize
272  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
273  */
274 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
275                                            const bool         ignoreAuxiliary) const
276 {
277   if ( !_compatibleHypothesis.empty() )
278   {
279     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
280     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
281       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
282
283     if ( ignoreAuxiliary )
284       theFilter.AndNot( theFilter.IsAuxiliary() );
285
286     return true;
287   }
288   return false;
289 }
290