Salome HOME
IPAL52499: Prismatic mesh is not computed on a prismatic shape
[modules/smesh.git] / src / StdMeshers / StdMeshers_ProjectionUtils.hxx
1 // Copyright (C) 2007-2016  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 : idl implementation based on 'SMESH' unit's calsses
24 // File      : StdMeshers_ProjectionUtils.hxx
25 // Created   : Thu Oct 26 15:37:24 2006
26 // Author    : Edward AGAPOV (eap)
27 //
28 #ifndef StdMeshers_ProjectionUtils_HeaderFile
29 #define StdMeshers_ProjectionUtils_HeaderFile
30
31 #include "SMESH_StdMeshers.hxx"
32
33 #include "StdMeshers_FaceSide.hxx"
34 #include "SMDS_MeshElement.hxx"
35
36 #include <BRepMesh_DataStructureOfDelaun.hxx>
37 #include <ShapeAnalysis_Surface.hxx>
38 #include <TopTools_DataMapOfShapeShape.hxx>
39 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <gp_GTrsf.hxx>
45 #include <gp_GTrsf2d.hxx>
46
47 #include <list>
48 #include <map>
49
50 class SMDS_MeshNode;
51 class SMESH_Algo;
52 class SMESH_Hypothesis;
53 class SMESH_Mesh;
54 class SMESH_subMesh;
55 class TopoDS_Shape;
56
57 /*!
58  * \brief Struct used instead of a sole TopTools_DataMapOfShapeShape to avoid
59  *        problems with bidirectional bindings
60  */
61 struct StdMeshers_ShapeShapeBiDirectionMap
62 {
63   TopTools_DataMapOfShapeShape _map1to2, _map2to1;
64
65   enum EAssocType {
66     UNDEF, INIT_VERTEX, PROPAGATION, PARTNER, CLOSE_VERTEX, COMMON_VERTEX, FEW_EF };
67   EAssocType _assocType;
68
69   // convention: s1 - target, s2 - source
70   bool Bind( const TopoDS_Shape& s1, const TopoDS_Shape& s2 )
71   { _map1to2.Bind( s1, s2 ); return _map2to1.Bind( s2, s1 ); }
72   bool IsBound( const TopoDS_Shape& s, const bool isShape2=false ) const 
73   { return (isShape2 ? _map2to1 : _map1to2).IsBound( s ); }
74   bool IsEmpty() const { return _map1to2.IsEmpty(); }
75   int  Extent()  const { return _map1to2.Extent(); }
76   void Clear() { _map1to2.Clear(); _map2to1.Clear(); }
77   const TopoDS_Shape& operator()( const TopoDS_Shape& s, const bool isShape2=false ) const
78   { // if we get a Standard_NoSuchObject here, it means that the calling code
79     // passes incorrect isShape2
80     return (isShape2 ? _map2to1 : _map1to2)( s );
81   }
82   StdMeshers_ShapeShapeBiDirectionMap() : _assocType( UNDEF ) {}
83   void SetAssocType( EAssocType type ) { if ( _assocType == UNDEF ) _assocType = type; }
84 };
85
86 /*!
87  * \brief Methods common to Projection algorithms
88  */
89 namespace StdMeshers_ProjectionUtils
90 {
91   typedef StdMeshers_ShapeShapeBiDirectionMap                  TShapeShapeMap;
92   typedef TopTools_IndexedDataMapOfShapeListOfShape            TAncestorMap;
93   typedef std::map<const SMDS_MeshNode*, const SMDS_MeshNode*,
94                    TIDCompare>                                 TNodeNodeMap;
95
96
97   /*!
98    * \brief Finds transformation between two sets of 2D points using
99    *        a least square approximation
100    */
101   class TrsfFinder2D
102   {
103     gp_GTrsf2d _trsf;
104     gp_XY      _srcOrig;
105   public:
106     TrsfFinder2D(): _srcOrig(0,0) {}
107
108     void Set( const gp_GTrsf2d& t ) { _trsf = t; } // it's an alternative to Solve()
109
110     bool Solve( const std::vector< gp_XY >& srcPnts,
111                 const std::vector< gp_XY >& tgtPnts );
112
113     gp_XY Transform( const gp_Pnt2d& srcUV ) const;
114
115     bool IsIdentity() const { return ( _trsf.Form() == gp_Identity ); }
116   };
117   /*!
118    * \brief Finds transformation between two sets of 3D points using
119    *        a least square approximation
120    */
121   class TrsfFinder3D
122   {
123     gp_GTrsf _trsf;
124     gp_XYZ   _srcOrig;
125   public:
126     TrsfFinder3D(): _srcOrig(0,0,0) {}
127
128     void Set( const gp_GTrsf& t ) { _trsf = t; } // it's an alternative to Solve()
129
130     bool Solve( const std::vector< gp_XYZ > & srcPnts,
131                 const std::vector< gp_XYZ > & tgtPnts );
132
133     gp_XYZ Transform( const gp_Pnt& srcP ) const;
134
135     gp_XYZ TransformVec( const gp_Vec& v ) const;
136
137     bool IsIdentity() const { return ( _trsf.Form() == gp_Identity ); }
138
139     bool Invert();
140   };
141
142   /*!
143    * \brief Morph mesh on the target FACE to lie within FACE boundary w/o distortion
144    */
145   class Morph
146   {
147     std::vector< const SMDS_MeshNode* >    _bndSrcNodes;
148     Handle(BRepMesh_DataStructureOfDelaun) _triaDS;
149     SMESH_subMesh*                         _srcSubMesh;
150     bool                                   _moveAll;
151     gp_XY                                  _scale;
152   public:
153
154     Morph(const TSideVector& srcWires);
155
156     bool Perform(SMESH_MesherHelper&           tgtHelper,
157                  const TSideVector&            tgtWires,
158                  Handle(ShapeAnalysis_Surface) tgtSurface,
159                  const TNodeNodeMap&           src2tgtNodes,
160                  const bool                    moveAll);
161
162     // find a triangle containing an UV starting from a given triangle;
163     // return barycentric coordinates of the UV in the found triangle
164     const BRepMesh_Triangle* FindTriangle( const gp_XY&             uv,
165                                            const BRepMesh_Triangle* bmTria,
166                                            double                   bc[3],
167                                            int                      triaNodes[3]);
168
169     // return any delauney triangle neighboring a given boundary node
170     const BRepMesh_Triangle* GetTriangleNear( int iBndNode );
171
172     // return source boundary nodes. 0-th node is NULL so that indices of
173     // boundary nodes correspond to indices used by Delauney mesh
174     const std::vector< const SMDS_MeshNode* >& GetBndNodes() const { return _bndSrcNodes; }
175
176     // return UV of the i-th source boundary node
177     gp_XY GetBndUV(const int iNode) const;
178
179     // return scale factor to convert real UV to/from UV used for Delauney meshing:
180     // delauney_UV = real_UV * scale
181     const gp_XY& GetScale() const { return _scale; }
182
183     typedef std::list< std::pair< const SMDS_MeshNode*, const BRepMesh_Triangle* > > TNodeTriaList;
184
185     // add non-marked nodes surrounding a given one to a list
186     static void AddCloseNodes( const SMDS_MeshNode*     node,
187                                const BRepMesh_Triangle* bmTria,
188                                const int                faceID,
189                                TNodeTriaList &          noTriQueue );
190   };
191
192   /*!
193    * \brief Looks for association of all sub-shapes of two shapes
194    * \param theShape1 - shape 1
195    * \param theMesh1 - mesh built on shape 1
196    * \param theShape2 - shape 2
197    * \param theMesh2 - mesh built on shape 2
198    * \param theAssociation - association map to be filled that may
199    *                         contain association of one or two pairs of vertices
200    * \retval bool - true if association found
201    */
202   bool FindSubShapeAssociation(const TopoDS_Shape& theShape1,
203                                SMESH_Mesh*         theMesh1,
204                                const TopoDS_Shape& theShape2,
205                                SMESH_Mesh*         theMesh2,
206                                TShapeShapeMap &    theAssociationMap);
207
208   /*!
209    * \brief Find association of edges of faces
210    *  \param face1 - face 1
211    *  \param VV1 - vertices of face 1
212    *  \param face2 - face 2
213    *  \param VV2 - vertices of face 2 associated with oned of face 1
214    *  \param edges1 - out list of edges of face 1
215    *  \param edges2 - out list of edges of face 2
216    *  \param isClosenessAssoc - is association starting by VERTEX closeness
217    *  \retval int - nb of edges in an outer wire in a success case, else zero
218    */
219   int FindFaceAssociation(const TopoDS_Face&         face1,
220                           TopoDS_Vertex              VV1[2],
221                           const TopoDS_Face&         face2,
222                           TopoDS_Vertex              VV2[2],
223                           std::list< TopoDS_Edge > & edges1,
224                           std::list< TopoDS_Edge > & edges2,
225                           const bool                 isClosenessAssoc=false);
226
227   /*!
228    * \brief Insert vertex association defined by a hypothesis into a map
229    * \param theHyp - hypothesis
230    * \param theAssociationMap - association map
231    * \param theTargetShape - the shape theHyp assigned to
232    */
233   void InitVertexAssociation( const SMESH_Hypothesis* theHyp,
234                               TShapeShapeMap &        theAssociationMap);
235
236   /*!
237    * \brief Inserts association theShape1 <-> theShape2 to TShapeShapeMap
238    * \param theShape1 - target shape
239    * \param theShape2 - source shape
240    * \param theAssociationMap - association map 
241    * \param theBidirectional - if false, inserts theShape1 -> theShape2 association
242    * \retval bool - true if there was no association for these shapes before
243    */
244   bool InsertAssociation( const TopoDS_Shape& theShape1, // target
245                           const TopoDS_Shape& theShape2, // source
246                           TShapeShapeMap &    theAssociationMap);
247
248   /*!
249    * \brief Finds an edge by its vertices in a main shape of the mesh
250    */
251   TopoDS_Edge GetEdgeByVertices( SMESH_Mesh*          aMesh,
252                                  const TopoDS_Vertex& V1,
253                                  const TopoDS_Vertex& V2);
254
255   /*!
256    * \brief Return another face sharing an edge
257    * \param edgeToFaces - data map of descendants to ancestors
258    */
259   TopoDS_Face GetNextFace( const TAncestorMap& edgeToFaces,
260                            const TopoDS_Edge&  edge,
261                            const TopoDS_Face&  face);
262   /*!
263    * \brief Return other vertex of an edge
264    */
265   TopoDS_Vertex GetNextVertex(const TopoDS_Edge&   edge,
266                               const TopoDS_Vertex& vertex);
267
268   /*!
269    * \brief Return an oriented propagation edge
270    * \param aMesh - mesh
271    * \param fromEdge - start edge for propagation
272    * \param chain - return, if provided, a propagation chain passed till
273    *        anEdge; if anEdge.IsNull() then a full propagation chain is returned
274    * \retval pair<int,TopoDS_Edge> - propagation step and found edge
275    */
276   std::pair<int,TopoDS_Edge> GetPropagationEdge( SMESH_Mesh*                 aMesh,
277                                                  const TopoDS_Edge&          anEdge,
278                                                  const TopoDS_Edge&          fromEdge,
279                                                  TopTools_IndexedMapOfShape* chain=0);
280
281   /*!
282    * \brief Find corresponding nodes on two faces
283    * \param face1 - the first face
284    * \param mesh1 - mesh containing elements on the first face
285    * \param face2 - the second face
286    * \param mesh2 - mesh containing elements on the second face
287    * \param assocMap - map associating sub-shapes of the faces
288    * \param nodeIn2OutMap - map containing found matching nodes
289    * \retval bool - is a success
290    */
291   bool FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
292                                  SMESH_Mesh*            mesh1,
293                                  const TopoDS_Face&     face2,
294                                  SMESH_Mesh*            mesh2,
295                                  const TShapeShapeMap & assocMap,
296                                  TNodeNodeMap &         nodeIn2OutMap);
297   /*!
298    * \brief Return any sub-shape of a face belonging to the outer wire
299    * \param face - the face
300    * \param type - type of sub-shape to return
301    * \retval TopoDS_Shape - the found sub-shape
302    */
303   TopoDS_Shape OuterShape( const TopoDS_Face& face,
304                            TopAbs_ShapeEnum   type);
305
306   /*!
307    * \brief Check that submeshis is computed and try to compute it if is not
308    * \param sm - submesh to compute
309    * \param iterationNb - int used to stop infinite recursive call
310    * \retval bool - true if computed
311    */
312   bool MakeComputed(SMESH_subMesh * sm, const int iterationNb = 0);
313
314   /*!
315    * \brief Returns an error message to show in case if MakeComputed( sm ) fails.
316    */
317   std::string SourceNotComputedError( SMESH_subMesh * sm = 0,
318                                       SMESH_Algo*     projAlgo=0);
319
320   /*!
321    * \brief Set event listeners to submesh with projection algo
322    * \param subMesh - submesh with projection algo
323    * \param srcShape - source shape
324    * \param srcMesh - source mesh
325    */
326   void SetEventListener(SMESH_subMesh* subMesh,
327                         TopoDS_Shape   srcShape,
328                         SMESH_Mesh*    srcMesh);
329
330   /*!
331    * \brief Return a boundary EDGE (or all boundary EDGEs) of edgeContainer
332    */
333   TopoDS_Edge GetBoundaryEdge(const TopoDS_Shape&       edgeContainer,
334                               const SMESH_Mesh&         mesh,
335                               std::list< TopoDS_Edge >* allBndEdges = 0 );
336 };
337
338 #endif