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