Salome HOME
PAL16229 (Issue in order to mesh with Hexahedron algorithms)
[modules/smesh.git] / src / StdMeshers / StdMeshers_RadialPrism_3D.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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 // File      : StdMeshers_RadialPrism_3D.cxx
25 // Module    : SMESH
26 // Created   : Fri Oct 20 11:37:07 2006
27 // Author    : Edward AGAPOV (eap)
28
29
30 #include "StdMeshers_RadialPrism_3D.hxx"
31
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "StdMeshers_NumberOfLayers.hxx"
34 #include "StdMeshers_LayerDistribution.hxx"
35 #include "StdMeshers_Prism_3D.hxx"
36 #include "StdMeshers_Regular_1D.hxx"
37
38 #include "SMDS_MeshNode.hxx"
39 #include "SMESHDS_SubMesh.hxx"
40 #include "SMESH_Gen.hxx"
41 #include "SMESH_Mesh.hxx"
42 #include "SMESH_MeshEditor.hxx"
43 #include "SMESH_MesherHelper.hxx"
44 #include "SMESH_subMesh.hxx"
45
46 #include "utilities.h"
47
48 #include <TopoDS_Solid.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <BRepTools.hxx>
51 #include <BRepAdaptor_Curve.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <BRepBuilderAPI_MakeEdge.hxx>
54 #include <gp.hxx>
55 #include <gp_Pnt.hxx>
56
57
58 using namespace std;
59
60 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
61 #define gpXYZ(n) gp_XYZ(n->X(),n->Y(),n->Z())
62
63 typedef StdMeshers_ProjectionUtils TAssocTool;
64
65 //=======================================================================
66 //function : StdMeshers_RadialPrism_3D
67 //purpose  : 
68 //=======================================================================
69
70 StdMeshers_RadialPrism_3D::StdMeshers_RadialPrism_3D(int hypId, int studyId, SMESH_Gen* gen)
71   :SMESH_3D_Algo(hypId, studyId, gen)
72 {
73   _name = "RadialPrism_3D";
74   _shapeType = (1 << TopAbs_SOLID);     // 1 bit per shape type
75
76   _compatibleHypothesis.push_back("LayerDistribution");
77   _compatibleHypothesis.push_back("NumberOfLayers");
78   myNbLayerHypo = 0;
79   myDistributionHypo = 0;
80 }
81
82 //================================================================================
83 /*!
84  * \brief Destructor
85  */
86 //================================================================================
87
88 StdMeshers_RadialPrism_3D::~StdMeshers_RadialPrism_3D()
89 {}
90
91 //=======================================================================
92 //function : CheckHypothesis
93 //purpose  : 
94 //=======================================================================
95
96 bool StdMeshers_RadialPrism_3D::CheckHypothesis(SMESH_Mesh&                          aMesh,
97                                                 const TopoDS_Shape&                  aShape,
98                                                 SMESH_Hypothesis::Hypothesis_Status& aStatus)
99 {
100   // check aShape that must have 2 shells
101 /*  PAL16229
102   if ( TAssocTool::Count( aShape, TopAbs_SOLID, 0 ) != 1 ||
103        TAssocTool::Count( aShape, TopAbs_SHELL, 0 ) != 2 )
104   {
105     aStatus = HYP_BAD_GEOMETRY;
106     return false;
107   }
108 */
109   myNbLayerHypo = 0;
110   myDistributionHypo = 0;
111
112   list <const SMESHDS_Hypothesis * >::const_iterator itl;
113
114   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
115   if ( hyps.size() == 0 )
116   {
117     aStatus = SMESH_Hypothesis::HYP_MISSING;
118     return false;  // can't work with no hypothesis
119   }
120
121   if ( hyps.size() > 1 )
122   {
123     aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
124     return false;
125   }
126
127   const SMESHDS_Hypothesis *theHyp = hyps.front();
128
129   string hypName = theHyp->GetName();
130
131   if (hypName == "NumberOfLayers")
132   {
133     myNbLayerHypo = static_cast<const StdMeshers_NumberOfLayers *>(theHyp);
134     aStatus = SMESH_Hypothesis::HYP_OK;
135     return true;
136   }
137   if (hypName == "LayerDistribution")
138   {
139     myDistributionHypo = static_cast<const StdMeshers_LayerDistribution *>(theHyp);
140     aStatus = SMESH_Hypothesis::HYP_OK;
141     return true;
142   }
143   aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
144   return true;
145 }
146
147 //=======================================================================
148 //function : Compute
149 //purpose  : 
150 //=======================================================================
151
152 bool StdMeshers_RadialPrism_3D::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
153 {
154   TopExp_Explorer exp;
155   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
156
157   myHelper = new SMESH_MesherHelper( aMesh );
158   myHelper->IsQuadraticSubMesh( aShape );
159   // to delete helper at exit from Compute()
160   std::auto_ptr<SMESH_MesherHelper> helperDeleter( myHelper );
161
162   // get 2 shells 
163   TopoDS_Solid solid = TopoDS::Solid( aShape );
164   TopoDS_Shell outerShell = BRepTools::OuterShell( solid );
165   TopoDS_Shape innerShell;
166   int nbShells = 0;
167   for ( TopoDS_Iterator It (solid); It.More(); It.Next(), ++nbShells )
168     if ( !outerShell.IsSame( It.Value() ))
169       innerShell = It.Value();
170   if ( nbShells != 2 )
171     return error(COMPERR_BAD_SHAPE, SMESH_Comment("Must be 2 shells but not ")<<nbShells);
172
173   // ----------------------------------
174   // Associate subshapes of the shells
175   // ----------------------------------
176
177   TAssocTool::TShapeShapeMap shape2ShapeMap;
178   if ( !TAssocTool::FindSubShapeAssociation( outerShell, &aMesh,
179                                              innerShell, &aMesh,
180                                              shape2ShapeMap) )
181     return error(COMPERR_BAD_SHAPE,"Topology of inner and outer shells seems different" );
182
183   // ------------------
184   // Make mesh
185   // ------------------
186
187   TNode2ColumnMap node2columnMap;
188   myLayerPositions.clear();
189
190   for ( exp.Init( outerShell, TopAbs_FACE ); exp.More(); exp.Next() )
191   {
192     // Corresponding subshapes
193     TopoDS_Face outFace = TopoDS::Face( exp.Current() );
194     TopoDS_Face inFace;
195     if ( !shape2ShapeMap.IsBound( outFace )) {
196       return error(SMESH_Comment("Corresponding inner face not found for face #" )
197                    << meshDS->ShapeToIndex( outFace ));
198     } else {
199       inFace = TopoDS::Face( shape2ShapeMap( outFace ));
200     }
201
202     // Find matching nodes of in and out faces
203     TNodeNodeMap nodeIn2OutMap;
204     if ( ! TAssocTool::FindMatchingNodesOnFaces( inFace, &aMesh, outFace, &aMesh,
205                                                  shape2ShapeMap, nodeIn2OutMap ))
206       return error(COMPERR_BAD_INPUT_MESH,SMESH_Comment("Mesh on faces #")
207                    << meshDS->ShapeToIndex( outFace ) << " and "
208                    << meshDS->ShapeToIndex( inFace ) << " seems different" );
209
210     // Create volumes
211
212     SMDS_ElemIteratorPtr faceIt = meshDS->MeshElements( inFace )->GetElements();
213     while ( faceIt->more() ) // loop on faces on inFace
214     {
215       const SMDS_MeshElement* face = faceIt->next();
216       if ( !face || face->GetType() != SMDSAbs_Face )
217         continue;
218       int nbNodes = face->NbNodes();
219       if ( face->IsQuadratic() )
220         nbNodes /= 2;
221
222       // find node columns for each node
223       vector< const TNodeColumn* > columns( nbNodes );
224       for ( int i = 0; i < nbNodes; ++i )
225       {
226         const SMDS_MeshNode* nIn = face->GetNode( i );
227         TNode2ColumnMap::iterator n_col = node2columnMap.find( nIn );
228         if ( n_col != node2columnMap.end() ) {
229           columns[ i ] = & n_col->second;
230         }
231         else {
232           TNodeNodeMap::iterator nInOut = nodeIn2OutMap.find( nIn );
233           if ( nInOut == nodeIn2OutMap.end() )
234             RETURN_BAD_RESULT("No matching node for "<< nIn->GetID() <<
235                               " in face "<< face->GetID());
236           columns[ i ] = makeNodeColumn( node2columnMap, nIn, nInOut->second );
237         }
238       }
239
240       StdMeshers_Prism_3D::AddPrisms( columns, myHelper );
241     }
242   } // loop on faces of out shell
243
244   return true;
245 }
246
247 //================================================================================
248 /*!
249  * \brief Create a column of nodes from outNode to inNode
250   * \param n2ColMap - map of node columns to add a created column
251   * \param outNode - botton node of a column
252   * \param inNode - top node of a column
253   * \retval const TNodeColumn* - a new column pointer
254  */
255 //================================================================================
256
257 TNodeColumn* StdMeshers_RadialPrism_3D::makeNodeColumn( TNode2ColumnMap&     n2ColMap,
258                                                         const SMDS_MeshNode* outNode,
259                                                         const SMDS_MeshNode* inNode)
260 {
261   SMESHDS_Mesh * meshDS = myHelper->GetMeshDS();
262   int shapeID = myHelper->GetSubShapeID();
263
264   if ( myLayerPositions.empty() ) {
265     gp_Pnt pIn = gpXYZ( inNode ), pOut = gpXYZ( outNode );
266     computeLayerPositions( pIn, pOut );
267   }
268   int nbSegments = myLayerPositions.size() + 1;
269
270   TNode2ColumnMap::iterator n_col =
271     n2ColMap.insert( make_pair( outNode, TNodeColumn() )).first;
272   TNodeColumn & column = n_col->second;
273   column.resize( nbSegments + 1 );
274   column.front() = outNode;
275   column.back() =  inNode;
276
277   gp_XYZ p1 = gpXYZ( outNode );
278   gp_XYZ p2 = gpXYZ( inNode );
279   for ( int z = 1; z < nbSegments; ++z )
280   {
281     double r = myLayerPositions[ z - 1 ];
282     gp_XYZ p = ( 1 - r ) * p1 + r * p2;
283     SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z() );
284     meshDS->SetNodeInVolume( n, shapeID );
285     column[ z ] = n;
286   }
287
288   return & column;
289 }
290
291 //================================================================================
292 //================================================================================
293 /*!
294  * \brief Class computing layers distribution using data of
295  *        StdMeshers_LayerDistribution hypothesis
296  */
297 //================================================================================
298 //================================================================================
299
300 class TNodeDistributor: public StdMeshers_Regular_1D
301 {
302   list <const SMESHDS_Hypothesis *> myUsedHyps;
303 public:
304   // -----------------------------------------------------------------------------
305   static TNodeDistributor* GetDistributor(SMESH_Mesh& aMesh)
306   {
307     const int myID = -1000;
308     map < int, SMESH_1D_Algo * > & algoMap = aMesh.GetGen()->_map1D_Algo;
309     map < int, SMESH_1D_Algo * >::iterator id_algo = algoMap.find( myID );
310     if ( id_algo == algoMap.end() )
311       return new TNodeDistributor( myID, 0, aMesh.GetGen() );
312     return static_cast< TNodeDistributor* >( id_algo->second );
313   }
314   // -----------------------------------------------------------------------------
315   bool Compute( vector< double > &                  positions,
316                 gp_Pnt                              pIn,
317                 gp_Pnt                              pOut,
318                 SMESH_Mesh&                         aMesh,
319                 const StdMeshers_LayerDistribution* hyp)
320   {
321     double len = pIn.Distance( pOut );
322     if ( len <= DBL_MIN ) return error("Too close points of inner and outer shells");
323
324     if ( !hyp || !hyp->GetLayerDistribution() )
325       return error( "Invalid LayerDistribution hypothesis");
326     myUsedHyps.clear();
327     myUsedHyps.push_back( hyp->GetLayerDistribution() );
328
329     TopoDS_Edge edge = BRepBuilderAPI_MakeEdge( pIn, pOut );
330     SMESH_Hypothesis::Hypothesis_Status aStatus;
331     if ( !StdMeshers_Regular_1D::CheckHypothesis( aMesh, edge, aStatus ))
332       return error( "StdMeshers_Regular_1D::CheckHypothesis() failed "
333                     "with LayerDistribution hypothesis");
334
335     BRepAdaptor_Curve C3D(edge);
336     double f = C3D.FirstParameter(), l = C3D.LastParameter();
337     list< double > params;
338     if ( !StdMeshers_Regular_1D::computeInternalParameters( C3D, len, f, l, params, false ))
339       return error("StdMeshers_Regular_1D failed to compute layers distribution");
340
341     positions.clear();
342     positions.reserve( params.size() );
343     for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
344       positions.push_back( *itU / len );
345     return true;
346   }
347 protected:
348   // -----------------------------------------------------------------------------
349   TNodeDistributor( int hypId, int studyId, SMESH_Gen* gen)
350     : StdMeshers_Regular_1D( hypId, studyId, gen)
351   {
352   }
353   // -----------------------------------------------------------------------------
354   virtual const list <const SMESHDS_Hypothesis *> &
355     GetUsedHypothesis(SMESH_Mesh &, const TopoDS_Shape &, const bool)
356   {
357     return myUsedHyps;
358   }
359   // -----------------------------------------------------------------------------
360 };
361
362 //================================================================================
363 /*!
364  * \brief Compute positions of nodes between the internal and the external surfaces
365   * \retval bool - is a success
366  */
367 //================================================================================
368
369 bool StdMeshers_RadialPrism_3D::computeLayerPositions(const gp_Pnt& pIn,
370                                                       const gp_Pnt& pOut)
371 {
372   if ( myNbLayerHypo )
373   {
374     int nbSegments = myNbLayerHypo->GetNumberOfLayers();
375     myLayerPositions.resize( nbSegments - 1 );
376     for ( int z = 1; z < nbSegments; ++z )
377       myLayerPositions[ z - 1 ] = double( z )/ double( nbSegments );
378     return true;
379   }
380   if ( myDistributionHypo ) {
381     SMESH_Mesh * mesh = myHelper->GetMesh();
382     if ( !TNodeDistributor::GetDistributor(*mesh)->Compute( myLayerPositions, pIn, pOut,
383                                                             *mesh, myDistributionHypo ))
384     {
385       error( TNodeDistributor::GetDistributor(*mesh)->GetComputeError() );
386       return false;
387     }
388   }
389   RETURN_BAD_RESULT("Bad hypothesis");
390 }