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