Salome HOME
OCCT dev version porting (6.7.2)
[modules/smesh.git] / src / StdMeshers / StdMeshers_Prism_3D.hxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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_TypeDefs.hxx"
41 #include "SMESH_subMesh.hxx"
42
43 #include <Adaptor2d_Curve2d.hxx>
44 #include <Adaptor3d_Curve.hxx>
45 #include <Adaptor3d_Surface.hxx>
46 #include <BRepAdaptor_Surface.hxx>
47 #include <TopTools_IndexedMapOfOrientedShape.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <gp_Trsf.hxx>
50 #include <gp_XYZ.hxx>
51
52 #include <vector>
53
54 namespace Prism_3D
55 {
56   struct TNode;
57   struct TPrismTopo;
58 }
59 class SMESHDS_SubMesh;
60 class TopoDS_Edge;
61
62 typedef TopTools_IndexedMapOfOrientedShape                       TBlockShapes;
63 typedef std::vector<const SMDS_MeshNode* >                       TNodeColumn;
64 typedef std::map< double,          TNodeColumn >                 TParam2ColumnMap;
65 typedef std::map< double,          TNodeColumn >::const_iterator TParam2ColumnIt;
66 // map of bottom nodes to the column of nodes above them
67 // (the column includes the bottom nodes)
68 typedef std::map< Prism_3D::TNode, TNodeColumn >                 TNode2ColumnMap;
69
70
71 namespace Prism_3D
72 {
73   // ===============================================
74   /*!
75    * \brief Structure containing node relative data
76    */
77   struct TNode
78   {
79     const SMDS_MeshNode* myNode;
80     mutable gp_XYZ       myParams;
81
82     gp_XYZ GetCoords() const { return gp_XYZ( myNode->X(), myNode->Y(), myNode->Z() ); }
83     gp_XYZ GetParams() const { return myParams; }
84     gp_XYZ& ChangeParams() const { return myParams; }
85     bool HasParams() const { return myParams.X() >= 0.0; }
86     SMDS_TypeOfPosition GetPositionType() const
87     { return myNode ? myNode->GetPosition()->GetTypeOfPosition() : SMDS_TOP_UNSPEC; }
88     bool IsNeighbor( const TNode& other ) const;
89
90     TNode(const SMDS_MeshNode* node = 0): myNode(node), myParams(-1,-1,-1) {}
91     bool operator < (const TNode& other) const { return myNode->GetID() < other.myNode->GetID(); }
92   };
93   // ===============================================
94   /*!
95    * \brief Topological data of the prism
96    */
97   typedef std::list< TFaceQuadStructPtr > TQuadList;
98
99   struct TPrismTopo
100   {
101     TopoDS_Shape             myShape3D;
102     TopoDS_Face              myBottom;
103     TopoDS_Face              myTop;
104     std::list< TopoDS_Edge > myBottomEdges;
105     std::vector< TQuadList>  myWallQuads; // wall sides can be vertically composite
106     std::vector< int >       myRightQuadIndex; // index of right neighbour wall quad
107     std::list< int >         myNbEdgesInWires;
108
109     bool                     myNotQuadOnTop;
110
111     void Clear();
112     void SetUpsideDown();
113   };
114 }
115
116 // ===============================================================
117 /*!
118  * \brief Tool analyzing and giving access to a prism geometry 
119  *  treating it like a block, i.e. the four side faces are
120  *  emulated by division/uniting of missing/excess faces.
121  *  It also manage associations between block sub-shapes and a mesh.
122  */
123 class STDMESHERS_EXPORT StdMeshers_PrismAsBlock: public SMESH_Block
124 {
125  public:
126   /*!
127    * \brief Constructor. Initialization is needed
128    */
129   StdMeshers_PrismAsBlock();
130
131   ~StdMeshers_PrismAsBlock();
132
133   /*!
134    * \brief Initialization.
135    * \param helper - helper loaded with mesh and 3D shape
136    * \param prism - prism topology
137    * \retval bool - false if a mesh or a shape are KO
138    */
139   bool Init(SMESH_MesherHelper* helper, const Prism_3D::TPrismTopo& prism);
140
141   /*!
142    * \brief Return problem description
143    */
144   SMESH_ComputeErrorPtr GetError() const { return myError; }
145
146   /*!
147    * \brief Free allocated memory
148    */
149   void Clear();
150
151   /*!
152    * \brief Return number of nodes on every vertical edge
153     * \retval int - number of nodes including end nodes
154    */
155   int VerticalSize() const { return myParam2ColumnMaps[0].begin()->second.size(); }
156
157   bool HasNotQuadElemOnTop() const { return myNotQuadOnTop; }
158
159   /*!
160    * \brief Return pointer to column of nodes
161     * \param node - bottom node from which the returned column goes up
162     * \retval const TNodeColumn* - the found column
163    */
164   const TNodeColumn* GetNodeColumn(const SMDS_MeshNode* node) const;
165
166   /*!
167    * \brief Return TParam2ColumnMap for a base edge
168     * \param baseEdgeID - base edge SMESHDS Index
169     * \param isReverse - columns in-block orientation
170     * \retval const TParam2ColumnMap* - map
171    */
172   const TParam2ColumnMap* GetParam2ColumnMap(const int baseEdgeID,
173                                              bool &    isReverse) const
174   {
175     std::map< int, std::pair< TParam2ColumnMap*, bool > >::const_iterator i_mo =
176       myShapeIndex2ColumnMap.find( baseEdgeID );
177     if ( i_mo == myShapeIndex2ColumnMap.end() ) return 0;
178
179     const std::pair< TParam2ColumnMap*, bool >& col_frw = i_mo->second;
180     isReverse = !col_frw.second;
181     return col_frw.first;
182   }
183
184   /*!
185    * \brief Return transformations to get coordinates of nodes of each internal layer
186    *        by nodes of the bottom. Layer is a set of nodes at a certain step
187    *        from bottom to top.
188    */
189   bool GetLayersTransformation(std::vector<gp_Trsf> &      trsf,
190                                const Prism_3D::TPrismTopo& prism) const;
191   
192   /*!
193    * \brief Return pointer to mesh
194     * \retval SMESH_Mesh - mesh
195    */
196   SMESH_Mesh* Mesh() const { return myHelper->GetMesh(); }
197
198   /*!
199    * \brief Return pointer to mesh DS
200     * \retval SMESHDS_Mesh - mesh DS
201    */
202   SMESHDS_Mesh* MeshDS() const { return Mesh()->GetMeshDS(); }
203
204   /*!
205    * \brief Return submesh of a shape
206     * \param shapeID - shape given by in-block index
207     * \retval SMESH_subMesh* - found submesh
208    */
209   SMESH_subMesh* SubMesh(const int shapeID) const
210   { return Mesh()->GetSubMesh( Shape( shapeID )); }
211
212   /*!
213    * \brief Return submesh DS of a shape
214     * \param shapeID - shape given by in-block index
215     * \retval SMESHDS_SubMesh* - found submesh DS
216    */
217   SMESHDS_SubMesh* SubMeshDS(const int shapeID) const
218   { return SubMesh(shapeID)->GetSubMeshDS(); }
219
220   /*!
221    * \brief Return a in-block shape
222     * \param shapeID - shape given by in-block index
223     * \retval SMESHDS_SubMesh* - found submesh
224    */
225   const TopoDS_Shape& Shape(const int shapeID) const
226   { return myShapeIDMap( shapeID ); }
227
228   /*!
229    * \brief Return in-block ID of a shape
230     * \param shape - block sub-shape
231     * \retval int - ID or zero if the shape has no ID
232    */
233   int ShapeID(const TopoDS_Shape& shape) const
234   { return myShapeIDMap.FindIndex( shape ); }
235
236   /*!
237    * \brief Check curve orientation of a bootom edge
238    *  \param meshDS - mesh DS
239    *  \param columnsMap - node columns map of side face
240    *  \param bottomEdge - the bootom edge
241    *  \param sideFaceID - side face in-block ID
242    *  \retval bool - true if orienation coinside with in-block froward orienation
243    */
244   static bool IsForwardEdge(SMESHDS_Mesh*           meshDS,
245                             const TParam2ColumnMap& columnsMap,
246                             const TopoDS_Edge &     bottomEdge,
247                             const int               sideFaceID);
248
249 private:
250
251   // --------------------------------------------------------------------
252   /*!
253    * \brief Class representing a part of a geom face or
254    * a union of seleral faces. Or just an ordinary geom face
255    *
256    * It's parametrization is within [0,1] range.
257    * It redefines Adaptor3d_Surface::Value(U,V) where U and V are within [0,1]
258    */
259   // --------------------------------------------------------------------
260   class TSideFace: public Adaptor3d_Surface
261   {
262     typedef boost::shared_ptr<BRepAdaptor_Surface> PSurface;
263
264     int                             myID; //!< in-block ID
265     // map used to find out real UV by it's normalized UV
266     TParam2ColumnMap*               myParamToColumnMap;
267     PSurface                        mySurface;
268     TopoDS_Edge                     myBaseEdge;
269     map< int, PSurface >            myShapeID2Surf;
270     // first and last normalized params and orientaion for each component or it-self
271     std::vector< std::pair< double, double> > myParams; // select my columns in myParamToColumnMap
272     bool                            myIsForward;
273     std::vector< TSideFace* >       myComponents;
274     SMESH_MesherHelper              myHelper;
275   public:
276     TSideFace( SMESH_Mesh&                mesh,
277                const int                  faceID,
278                const Prism_3D::TQuadList& quadList,
279                const TopoDS_Edge&         baseEdge,
280                TParam2ColumnMap*          columnsMap,
281                const double               first = 0.0,
282                const double               last  = 1.0);
283     TSideFace( SMESH_Mesh&                                       mesh,
284                const std::vector< TSideFace* >&                  components,
285                const std::vector< std::pair< double, double> > & params);
286     TSideFace( const TSideFace& other );
287     ~TSideFace();
288     bool IsComplex() const
289     { return ( NbComponents() > 0 || myParams[0].first != 0. || myParams[0].second != 1. ); }
290     int FaceID() const { return myID; }
291     SMESH_Mesh* GetMesh() const { return myHelper.GetMesh(); }
292     TParam2ColumnMap* GetColumns() const { return myParamToColumnMap; }
293     gp_XY GetNodeUV(const TopoDS_Face& F, const SMDS_MeshNode* n, const SMDS_MeshNode* n2=0) const
294     { return ((SMESH_MesherHelper&) myHelper).SetSubShape(F), myHelper.GetNodeUV( F, n, n2 ); }
295     const TopoDS_Edge & BaseEdge() const { return myBaseEdge; }
296     int ColumnHeight() const {
297       if ( NbComponents() ) return GetComponent(0)->GetColumns()->begin()->second.size();
298       else                  return GetColumns()->begin()->second.size(); }
299     double GetColumns(const double U, TParam2ColumnIt & col1, TParam2ColumnIt& col2 ) const;
300     void GetNodesAtZ(const int Z, std::map<double, const SMDS_MeshNode* >& nodes ) const;
301     int NbComponents() const { return myComponents.size(); }
302     TSideFace* GetComponent(const int i) const { return myComponents.at( i ); }
303     void SetComponent(const int i, TSideFace* c)
304     { if ( myComponents[i] ) delete myComponents[i]; myComponents[i]=c; }
305     TSideFace* GetComponent(const double U, double& localU) const;
306     bool IsForward() const { return myIsForward; }
307     // boundary geometry for a face
308     Adaptor3d_Surface* Surface() const { return new TSideFace( *this ); }
309     bool GetPCurves(Adaptor2d_Curve2d* pcurv[4]) const;
310     Adaptor2d_Curve2d* HorizPCurve(const bool isTop, const TopoDS_Face& horFace) const;
311     Adaptor3d_Curve* HorizCurve(const bool isTop) const;
312     Adaptor3d_Curve* VertiCurve(const bool isMax) const;
313     TopoDS_Edge GetEdge( const int edge ) const;
314     int InsertSubShapes( TBlockShapes& shapeMap ) const;
315     // redefine Adaptor methods
316     gp_Pnt Value(const Standard_Real U,const Standard_Real V) const;
317     // debug
318     void dumpNodes(int nbNodes) const;
319   };
320
321   // --------------------------------------------------------------------
322   /*!
323    * \brief Class emulating geometry of a vertical edge
324    */
325   // --------------------------------------------------------------------
326   class STDMESHERS_EXPORT TVerticalEdgeAdaptor: public Adaptor3d_Curve
327   {
328     const TNodeColumn* myNodeColumn;
329   public:
330     TVerticalEdgeAdaptor( const TParam2ColumnMap* columnsMap, const double parameter );
331     gp_Pnt Value(const Standard_Real U) const;
332     Standard_Real FirstParameter() const { return 0; }
333     Standard_Real LastParameter() const { return 1; }
334     // debug
335     void dumpNodes(int nbNodes) const;
336   };
337
338   // --------------------------------------------------------------------
339   /*!
340    * \brief Class emulating geometry of a hirizontal edge
341    */
342   // --------------------------------------------------------------------
343   class STDMESHERS_EXPORT THorizontalEdgeAdaptor: public Adaptor3d_Curve
344   {
345     const TSideFace* mySide;
346     double           myV;
347   public:
348     THorizontalEdgeAdaptor( const TSideFace* sideFace, const bool isTop)
349       :mySide(sideFace), myV( isTop ? 1.0 : 0.0 ) {}
350     gp_Pnt Value(const Standard_Real U) const;
351     Standard_Real FirstParameter() const { return 0; }
352     Standard_Real LastParameter() const { return 1; }
353     // debug
354     void dumpNodes(int nbNodes) const;
355   };
356
357   // --------------------------------------------------------------------
358   /*!
359    * \brief Class emulating pcurve on a hirizontal face
360    */
361   // --------------------------------------------------------------------
362   class STDMESHERS_EXPORT TPCurveOnHorFaceAdaptor: public Adaptor2d_Curve2d
363   {
364     std::map< double, gp_XY > myUVmap; // normalized parameter to UV on a horizontal face
365   public:
366     TPCurveOnHorFaceAdaptor( const TSideFace*   sideFace,
367                              const bool         isTop,
368                              const TopoDS_Face& horFace);
369     gp_Pnt2d Value(const Standard_Real U) const;
370     Standard_Real FirstParameter() const { return 0; }
371     Standard_Real LastParameter() const { return 1; }
372   };
373
374   bool                  myNotQuadOnTop;
375   SMESH_MesherHelper*   myHelper;
376   TBlockShapes          myShapeIDMap;
377   SMESH_ComputeErrorPtr myError;
378
379   // container of 4 side faces
380   TSideFace*            mySide; 
381   // node columns for each base edge
382   std::vector< TParam2ColumnMap >                       myParam2ColumnMaps;
383   // to find a column for a node by edge SMESHDS Index
384   std::map< int, std::pair< TParam2ColumnMap*, bool > > myShapeIndex2ColumnMap;
385
386   /*!
387    * \brief store error and comment and then return ( error == COMPERR_OK )
388    */
389   bool error(int error, const SMESH_Comment& comment = "") {
390     myError = SMESH_ComputeError::New(error,comment);
391     return myError->IsOK();
392   }
393   /*!
394    * \brief Prints a script creating a normal grid on the prism side
395    */
396   void faceGridToPythonDump(const SMESH_Block::TShapeID face,
397                             const int                   nb=10);
398
399 }; // class StdMeshers_PrismAsBlock
400
401 // =============================================
402 /*!
403  * \brief Algo building prisms on a prism shape
404  */
405 class STDMESHERS_EXPORT StdMeshers_Prism_3D: public SMESH_3D_Algo
406 {
407 public:
408   StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen);
409   virtual ~StdMeshers_Prism_3D();
410
411   virtual bool CheckHypothesis(SMESH_Mesh&                          aMesh,
412                                const TopoDS_Shape&                  aShape,
413                                SMESH_Hypothesis::Hypothesis_Status& aStatus);
414
415   virtual bool Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape);
416
417   virtual bool Evaluate(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape,
418                         MapShapeNbElems& aResMap);
419
420   /*!
421    * \brief Enable removal of quadrangles from the bottom face and
422    * triangles creation there by projection from the top
423    * (sole face meshed with triangles is considered to be a bottom one).
424    * If there are two faces with triangles, triangles must
425    * be of the same topology, else the algo fails.
426    * The method must be called before Compute()
427    */
428   void ProjectTriangles() { myProjectTriangles = true; }
429
430   /*!
431    * \brief Create prisms
432     * \param nodeColumns - columns of nodes generated from nodes of a mesh face
433     * \param helper - helper initialized by mesh and shape to add prisms to
434    */
435   static void AddPrisms( std::vector<const TNodeColumn*> & nodeColumns,
436                          SMESH_MesherHelper*               helper);
437
438   static bool IsApplicable(const TopoDS_Shape & aShape, bool toCheckAll);
439
440  private:
441
442   /*!
443    * \brief Analyse shape geometry and mesh.
444     * If there are triangles on one of faces, it becomes 'bottom'
445    */
446   bool initPrism(Prism_3D::TPrismTopo& thePrism, const TopoDS_Shape& theSolid);
447
448   /*!
449    * \brief Fill thePrism.myWallQuads and thePrism.myTopEdges
450    */
451   bool getWallFaces( Prism_3D::TPrismTopo& thePrism,
452                      const int             totalNbFaces);
453
454   /*!
455    * \brief Compute mesh on a SOLID
456    */
457   bool compute(const Prism_3D::TPrismTopo& thePrism);
458
459   /*!
460    * \brief Compute 2D mesh on walls FACEs of a prism
461    */
462   bool computeWalls(const Prism_3D::TPrismTopo& thePrism);
463
464   /*!
465    * \brief Returns a source EDGE of propagation to a given EDGE
466    */
467   TopoDS_Edge findPropagationSource( const TopoDS_Edge& E );
468
469   /*!
470    * \brief Find correspondence between bottom and top nodes.
471    *  If elements on the bottom and top faces are topologically different,
472    *  and projection is possible and allowed, perform the projection
473     * \retval bool - is a success or not
474    */
475   bool assocOrProjBottom2Top( const gp_Trsf & bottomToTopTrsf,
476                               const Prism_3D::TPrismTopo& thePrism);
477
478   /*!
479    * \brief Remove quadrangles from the top face and
480    *        create triangles there by projection from the bottom
481     * \retval bool - a success or not
482    */
483   bool projectBottomToTop( const gp_Trsf & bottomToTopTrsf );
484
485   /*!
486    * \brief Project mesh faces from a source FACE of one prism to
487    *        a source FACE of another prism
488    *  \retval bool - a success or not
489    */
490   bool project2dMesh(const TopoDS_Face& source, const TopoDS_Face& target);
491
492   /*!
493    * \brief Set projection coordinates of a node to a face and it's sub-shapes
494     * \param faceID - the face given by in-block ID
495     * \param params - node normalized parameters
496     * \retval bool - is a success
497    */
498   bool setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z );
499
500   /*!
501    * \brief If (!isOK), sets the error to a sub-mesh of a current SOLID
502    */
503   bool toSM( bool isOK );
504
505   /*!
506    * \brief Return index of a shape
507    */
508   int shapeID( const TopoDS_Shape& S );
509
510 private:
511
512   bool myProjectTriangles;
513   bool mySetErrorToSM;
514
515   StdMeshers_PrismAsBlock myBlock;
516   SMESH_MesherHelper*     myHelper;
517
518   std::vector<gp_XYZ>     myShapeXYZ; // point on each sub-shape of the block
519
520   // map of bottom nodes to the column of nodes above them
521   // (the column includes the bottom node)
522   TNode2ColumnMap         myBotToColumnMap;
523
524   TopTools_IndexedMapOfShape* myPropagChains;
525
526 }; // class StdMeshers_Prism_3D
527
528 #endif