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