Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/smesh.git] / src / StdMeshers / StdMeshers_Prism_3D.hxx
1 //  Copyright (C) 2007-2010  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
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_Prism_3D.hxx
25 //  Module : SMESH
26 //
27 #ifndef _SMESH_Prism_3D_HXX_
28 #define _SMESH_Prism_3D_HXX_
29
30 #include "SMESH_StdMeshers.hxx"
31
32 #include "SMESH_3D_Algo.hxx"
33 #include "SMDS_TypeOfPosition.hxx"
34 #include "SMDS_MeshNode.hxx"
35 #include "SMESH_Block.hxx"
36 #include "SMESH_Mesh.hxx"
37 #include "SMESHDS_Mesh.hxx"
38 #include "SMESH_subMesh.hxx"
39 #include "SMESH_MesherHelper.hxx"
40 #include "SMESH_Comment.hxx"
41
42 #include <vector>
43
44 #include <Adaptor3d_Curve.hxx>
45 #include <Adaptor3d_Surface.hxx>
46 #include <Adaptor2d_Curve2d.hxx>
47 #include <BRepAdaptor_Surface.hxx>
48 #include <TopTools_IndexedMapOfOrientedShape.hxx>
49 #include <gp_XYZ.hxx>
50 #include <gp_Trsf.hxx>
51
52
53 class SMESHDS_SubMesh;
54 class TopoDS_Edge;
55 class TopoDS_Faces;
56 struct TNode;
57
58 //typedef std::map<const SMDS_MeshNode*, const SMDS_MeshNode*> TNodeNodeMap;
59 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
60
61 // map of bottom nodes to the column of nodes above them
62 // (the column includes the bottom nodes)
63 typedef std::map< TNode, TNodeColumn >  TNode2ColumnMap;
64 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
65 typedef std::map< double, TNodeColumn >::const_iterator TParam2ColumnIt;
66
67 typedef TopTools_IndexedMapOfOrientedShape TBlockShapes;
68
69 // ===============================================
70 /*!
71  * \brief Structure containing node relative data
72  */
73 // ===============================================
74
75 struct TNode
76 {
77   const SMDS_MeshNode* myNode;
78   mutable gp_XYZ       myParams;
79
80   gp_XYZ GetCoords() const { return gp_XYZ( myNode->X(), myNode->Y(), myNode->Z() ); }
81   gp_XYZ GetParams() const { return myParams; }
82   gp_XYZ& ChangeParams() const { return myParams; }
83   bool HasParams() const { return myParams.X() >= 0.0; }
84   SMDS_TypeOfPosition GetPositionType() const
85   { return myNode ? myNode->GetPosition()->GetTypeOfPosition() : SMDS_TOP_UNSPEC; }
86   bool IsNeighbor( const TNode& other ) const;
87
88   TNode(const SMDS_MeshNode* node = 0): myNode(node), myParams(-1,-1,-1) {}
89   bool operator < (const TNode& other) const { return myNode->GetID() < other.myNode->GetID(); }
90 };
91
92 // ===============================================================
93 /*!
94  * \brief Tool analyzing and giving access to a prism geometry 
95  *  treating it like a block, i.e. the four side faces are
96  *  emulated by division/uniting of missing/excess faces.
97  *  It also manage associations between block subshapes and a mesh.
98  */
99 // ===============================================================
100
101 class STDMESHERS_EXPORT StdMeshers_PrismAsBlock: public SMESH_Block
102 {
103 public:
104   /*!
105    * \brief Constructor. Initialization is needed
106    */
107   StdMeshers_PrismAsBlock();
108
109   ~StdMeshers_PrismAsBlock();
110
111   /*!
112    * \brief Initialization.
113     * \param helper - helper loaded with mesh and 3D shape
114     * \param shape3D - a closed shell or solid
115     * \retval bool - false if a mesh or a shape are KO
116     *
117     * Analyse shape geometry and mesh.
118     * If there are triangles on one of faces, it becomes 'bottom'
119    */
120   bool Init(SMESH_MesherHelper* helper, const TopoDS_Shape& shape3D);
121
122   /*!
123    * \brief Return problem description
124    */
125   SMESH_ComputeErrorPtr GetError() const { return myError; }
126
127   /*!
128    * \brief Return number of nodes on every vertical edge
129     * \retval int - number of nodes including end nodes
130    */
131   int VerticalSize() const { return myParam2ColumnMaps[0].begin()->second.size(); }
132
133   bool HasNotQuadElemOnTop() const { return myNotQuadOnTop; }
134
135   /*!
136    * \brief Return pointer to column of nodes
137     * \param node - bottom node from which the returned column goes up
138     * \retval const TNodeColumn* - the found column
139    */
140   const TNodeColumn* GetNodeColumn(const SMDS_MeshNode* node) const;
141
142   /*!
143    * \brief Return TParam2ColumnMap for a base edge
144     * \param baseEdgeID - base edge SMESHDS Index
145     * \param isReverse - columns in-block orientation
146     * \retval const TParam2ColumnMap& - map
147    */
148   const TParam2ColumnMap& GetParam2ColumnMap(const int baseEdgeID,
149                                              bool &    isReverse) const
150   {
151     std::pair< TParam2ColumnMap*, bool > col_frw =
152       myShapeIndex2ColumnMap.find( baseEdgeID )->second;
153     isReverse = !col_frw.second;
154     return * col_frw.first;
155   }
156
157   /*!
158    * \brief Return transformations to get coordinates of nodes of each internal layer
159    *        by nodes of the bottom. Layer is a set of nodes at a certain step
160    *        from bottom to top.
161    */
162   bool GetLayersTransformation(std::vector<gp_Trsf> & trsf) const;
163   
164   /*!
165    * \brief Return pointer to mesh
166     * \retval SMESH_Mesh - mesh
167    */
168   SMESH_Mesh* Mesh() const { return myHelper->GetMesh(); }
169
170   /*!
171    * \brief Return pointer to mesh DS
172     * \retval SMESHDS_Mesh - mesh DS
173    */
174   SMESHDS_Mesh* MeshDS() const { return Mesh()->GetMeshDS(); }
175
176   /*!
177    * \brief Return submesh of a shape
178     * \param shapeID - shape given by in-block index
179     * \retval SMESH_subMesh* - found submesh
180    */
181   SMESH_subMesh* SubMesh(const int shapeID) const
182   { return Mesh()->GetSubMesh( Shape( shapeID )); }
183
184   /*!
185    * \brief Return submesh DS of a shape
186     * \param shapeID - shape given by in-block index
187     * \retval SMESHDS_SubMesh* - found submesh DS
188    */
189   SMESHDS_SubMesh* SubMeshDS(const int shapeID) const
190   { return SubMesh(shapeID)->GetSubMeshDS(); }
191
192   /*!
193    * \brief Return a in-block shape
194     * \param shapeID - shape given by in-block index
195     * \retval SMESHDS_SubMesh* - found submesh
196    */
197   const TopoDS_Shape& Shape(const int shapeID) const
198   { return myShapeIDMap( shapeID ); }
199
200   /*!
201    * \brief Return in-block ID of a shape
202     * \param shape - block subshape
203     * \retval int - ID or zero if the shape has no ID
204    */
205   int ShapeID(const TopoDS_Shape& shape) const
206   { return myShapeIDMap.FindIndex( shape ); }
207
208   /*!
209    * \brief Check curve orientation of a bootom edge
210    *  \param meshDS - mesh DS
211    *  \param columnsMap - node columns map of side face
212    *  \param bottomEdge - the bootom edge
213    *  \param sideFaceID - side face in-block ID
214    *  \retval bool - true if orienation coinside with in-block froward orienation
215    */
216   static bool IsForwardEdge(SMESHDS_Mesh*           meshDS,
217                             const TParam2ColumnMap& columnsMap,
218                             const TopoDS_Edge &     bottomEdge,
219                             const int               sideFaceID);
220   /*!
221    * \brief Find wall faces by bottom edges
222     * \param mesh - the mesh
223     * \param mainShape - the prism
224     * \param bottomFace - the bottom face
225     * \param bottomEdges - edges bounding the bottom face
226     * \param wallFaces - faces list to fill in
227    */
228   static bool GetWallFaces( SMESH_Mesh*                     mesh,
229                             const TopoDS_Shape &            mainShape,
230                             const TopoDS_Shape &            bottomFace,
231                             const std::list< TopoDS_Edge >& bottomEdges,
232                             std::list< TopoDS_Face >&       wallFaces);
233
234 private:
235
236   // --------------------------------------------------------------------
237   /*!
238    * \brief Class representing a part of a geom face or
239    * a union of seleral faces. Or just an ordinary geom face
240    *
241    * It's parametrization is within [0,1] range.
242    * It redefines Adaptor3d_Surface::Value(U,V) where U and V are within [0,1]
243    */
244   // --------------------------------------------------------------------
245   class TSideFace: public Adaptor3d_Surface
246   {
247     int                             myID; //!< in-block ID
248     // map used to find out real UV by it's normalized UV
249     TParam2ColumnMap*               myParamToColumnMap;
250     BRepAdaptor_Surface             mySurface;
251     TopoDS_Edge                     myBaseEdge;
252     // first and last normalized params and orientaion for each component or it-self
253     std::vector< std::pair< double, double> > myParams;
254     bool                            myIsForward;
255     std::vector< TSideFace* >       myComponents;
256     SMESH_MesherHelper *            myHelper;
257   public:
258     TSideFace( SMESH_MesherHelper* helper,
259                const int           faceID,
260                const TopoDS_Face&  face,
261                const TopoDS_Edge&  baseEdge,
262                TParam2ColumnMap*   columnsMap,
263                const double        first = 0.0,
264                const double        last = 1.0);
265     TSideFace( const std::vector< TSideFace* >&             components,
266                const std::vector< std::pair< double, double> > & params);
267     TSideFace( const TSideFace& other );
268     ~TSideFace();
269     bool IsComplex() const
270     { return ( NbComponents() > 0 || myParams[0].first != 0. || myParams[0].second != 1. ); }
271     int FaceID() const { return myID; }
272     TParam2ColumnMap* GetColumns() const { return myParamToColumnMap; }
273     gp_XY GetNodeUV(const TopoDS_Face& F, const SMDS_MeshNode* n) const
274     { return myHelper->GetNodeUV( F, n ); }
275     const TopoDS_Edge & BaseEdge() const { return myBaseEdge; }
276     int ColumnHeight() const {
277       if ( NbComponents() ) return GetComponent(0)->GetColumns()->begin()->second.size();
278       else                  return GetColumns()->begin()->second.size(); }
279     double GetColumns(const double U, TParam2ColumnIt & col1, TParam2ColumnIt& col2 ) const;
280     int NbComponents() const { return myComponents.size(); }
281     TSideFace* GetComponent(const int i) const { return myComponents.at( i ); }
282     void SetComponent(const int i, TSideFace* c)
283     { if ( myComponents[i] ) delete myComponents[i]; myComponents[i]=c; }
284     TSideFace* GetComponent(const double U, double& localU) const;
285     bool IsForward() const { return myIsForward; }
286     // boundary geometry for a face
287     Adaptor3d_Surface* Surface() const { return new TSideFace( *this ); }
288     bool GetPCurves(Adaptor2d_Curve2d* pcurv[4]) const;
289     Adaptor2d_Curve2d* HorizPCurve(const bool isTop, const TopoDS_Face& horFace) const;
290     Adaptor3d_Curve* HorizCurve(const bool isTop) const;
291     Adaptor3d_Curve* VertiCurve(const bool isMax) const;
292     TopoDS_Edge GetEdge( const int edge ) const;
293     int InsertSubShapes( TBlockShapes& shapeMap ) const;
294     // redefine Adaptor methods
295     gp_Pnt Value(const Standard_Real U,const Standard_Real V) const;
296     // debug
297     void dumpNodes(int nbNodes) const;
298   };
299
300   // --------------------------------------------------------------------
301   /*!
302    * \brief Class emulating geometry of a vertical edge
303    */
304   // --------------------------------------------------------------------
305   class STDMESHERS_EXPORT TVerticalEdgeAdaptor: public Adaptor3d_Curve
306   {
307     const TNodeColumn* myNodeColumn;
308   public:
309     TVerticalEdgeAdaptor( const TParam2ColumnMap* columnsMap, const double parameter );
310     gp_Pnt Value(const Standard_Real U) const;
311     Standard_Real FirstParameter() const { return 0; }
312     Standard_Real LastParameter() const { return 1; }
313     // debug
314     void dumpNodes(int nbNodes) const;
315   };
316
317   // --------------------------------------------------------------------
318   /*!
319    * \brief Class emulating geometry of a hirizontal edge
320    */
321   // --------------------------------------------------------------------
322   class STDMESHERS_EXPORT THorizontalEdgeAdaptor: public Adaptor3d_Curve
323   {
324     const TSideFace* mySide;
325     double           myV;
326   public:
327     THorizontalEdgeAdaptor( const TSideFace* sideFace, const bool isTop)
328       :mySide(sideFace), myV( isTop ? 1.0 : 0.0 ) {}
329     gp_Pnt Value(const Standard_Real U) const;
330     Standard_Real FirstParameter() const { return 0; }
331     Standard_Real LastParameter() const { return 1; }
332     // debug
333     void dumpNodes(int nbNodes) const;
334   };
335
336   // --------------------------------------------------------------------
337   /*!
338    * \brief Class emulating pcurve on a hirizontal face
339    */
340   // --------------------------------------------------------------------
341   class STDMESHERS_EXPORT TPCurveOnHorFaceAdaptor: public Adaptor2d_Curve2d
342   {
343     const TSideFace*  mySide;
344     int               myZ;
345     TopoDS_Face       myFace;
346   public:
347     TPCurveOnHorFaceAdaptor( const TSideFace*   sideFace,
348                              const bool         isTop,
349                              const TopoDS_Face& horFace)
350       : mySide(sideFace), myFace(horFace), myZ(isTop ? mySide->ColumnHeight() - 1 : 0 ) {}
351     gp_Pnt2d Value(const Standard_Real U) const;
352     Standard_Real FirstParameter() const { return 0; }
353     Standard_Real LastParameter() const { return 1; }
354   };
355
356   bool                  myNotQuadOnTop;
357   SMESH_MesherHelper*   myHelper;
358   TBlockShapes          myShapeIDMap;
359   SMESH_ComputeErrorPtr myError;
360
361   // container of 4 side faces
362   TSideFace*            mySide; 
363   // node columns for each base edge
364   std::vector< TParam2ColumnMap >                       myParam2ColumnMaps;
365   // to find a column for a node by edge SMESHDS Index
366   std::map< int, std::pair< TParam2ColumnMap*, bool > > myShapeIndex2ColumnMap;
367
368   /*!
369    * \brief store error and comment and then return ( error == COMPERR_OK )
370    */
371   bool error(int error, const SMESH_Comment& comment = "") {
372     myError = SMESH_ComputeError::New(error,comment);
373     return myError->IsOK();
374   }
375 };
376
377 // =============================================
378 /*!
379  * \brief Algo building prisms on a prism shape
380  */
381 // =============================================
382
383 class STDMESHERS_EXPORT StdMeshers_Prism_3D: public SMESH_3D_Algo
384 {
385 public:
386   StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen);
387   virtual ~StdMeshers_Prism_3D();
388
389   virtual bool CheckHypothesis(SMESH_Mesh&                          aMesh,
390                                const TopoDS_Shape&                  aShape,
391                                SMESH_Hypothesis::Hypothesis_Status& aStatus);
392
393   virtual bool Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape);
394
395   virtual bool Evaluate(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape,
396                         MapShapeNbElems& aResMap);
397
398   /*!
399    * \brief Enable removal of quadrangles from the bottom face and
400    * triangles creation there by projection from the top
401    * (sole face meshed with triangles is considered to be a bottom one).
402    * If there are two faces with triangles, triangles must
403    * be of the same topology, else the algo fails.
404    * The method must be called before Compute()
405    */
406   void ProjectTriangles() { myProjectTriangles = true; }
407
408   /*!
409    * \brief Create prisms
410     * \param nodeColumns - columns of nodes generated from nodes of a mesh face
411     * \param helper - helper initialized by mesh and shape to add prisms to
412    */
413   static void AddPrisms( std::vector<const TNodeColumn*> & nodeColumns,
414                          SMESH_MesherHelper*          helper);
415
416 private:
417
418   /*!
419    * \brief Find correspondence between bottom and top nodes.
420    *  If elements on the bottom and top faces are topologically different,
421    *  and projection is possible and allowed, perform the projection
422     * \retval bool - is a success or not
423    */
424   bool assocOrProjBottom2Top();
425
426   /*!
427    * \brief Remove quadrangles from the top face and
428    * create triangles there by projection from the bottom
429     * \retval bool - a success or not
430    */
431   bool projectBottomToTop();
432
433   /*!
434    * \brief Set projection coordinates of a node to a face and it's subshapes
435     * \param faceID - the face given by in-block ID
436     * \param params - node normalized parameters
437     * \retval bool - is a success
438    */
439   bool setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z );
440
441 private:
442
443   bool myProjectTriangles;
444
445   StdMeshers_PrismAsBlock myBlock;
446   SMESH_MesherHelper*     myHelper;
447
448   std::vector<gp_XYZ>     myShapeXYZ; // point on each sub-shape of the block
449
450   // map of bottom nodes to the column of nodes above them
451   // (the column includes the bottom node)
452   TNode2ColumnMap         myBotToColumnMap;
453 };
454
455 #endif