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