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