Salome HOME
23422: EDF 14312 - Strange behavior of Viscous Layer Surface offset + smooth
[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     // return source boundary nodes. 0-th node is zero
163     const std::vector< const SMDS_MeshNode* >& GetBndNodes() const { return _bndSrcNodes; }
164
165     // return UV of the i-th source boundary node
166     gp_XY GetBndUV(const int iNode) const;
167   };
168
169   /*!
170    * \brief Looks for association of all sub-shapes of two shapes
171    * \param theShape1 - shape 1
172    * \param theMesh1 - mesh built on shape 1
173    * \param theShape2 - shape 2
174    * \param theMesh2 - mesh built on shape 2
175    * \param theAssociation - association map to be filled that may
176    *                         contain association of one or two pairs of vertices
177    * \retval bool - true if association found
178    */
179   bool FindSubShapeAssociation(const TopoDS_Shape& theShape1,
180                                SMESH_Mesh*         theMesh1,
181                                const TopoDS_Shape& theShape2,
182                                SMESH_Mesh*         theMesh2,
183                                TShapeShapeMap &    theAssociationMap);
184
185   /*!
186    * \brief Find association of edges of faces
187    *  \param face1 - face 1
188    *  \param VV1 - vertices of face 1
189    *  \param face2 - face 2
190    *  \param VV2 - vertices of face 2 associated with oned of face 1
191    *  \param edges1 - out list of edges of face 1
192    *  \param edges2 - out list of edges of face 2
193    *  \param isClosenessAssoc - is association starting by VERTEX closeness
194    *  \retval int - nb of edges in an outer wire in a success case, else zero
195    */
196   int FindFaceAssociation(const TopoDS_Face&         face1,
197                           TopoDS_Vertex              VV1[2],
198                           const TopoDS_Face&         face2,
199                           TopoDS_Vertex              VV2[2],
200                           std::list< TopoDS_Edge > & edges1,
201                           std::list< TopoDS_Edge > & edges2,
202                           const bool                 isClosenessAssoc=false);
203
204   /*!
205    * \brief Insert vertex association defined by a hypothesis into a map
206    * \param theHyp - hypothesis
207    * \param theAssociationMap - association map
208    * \param theTargetShape - the shape theHyp assigned to
209    */
210   void InitVertexAssociation( const SMESH_Hypothesis* theHyp,
211                               TShapeShapeMap &        theAssociationMap);
212
213   /*!
214    * \brief Inserts association theShape1 <-> theShape2 to TShapeShapeMap
215    * \param theShape1 - target shape
216    * \param theShape2 - source shape
217    * \param theAssociationMap - association map 
218    * \param theBidirectional - if false, inserts theShape1 -> theShape2 association
219    * \retval bool - true if there was no association for these shapes before
220    */
221   bool InsertAssociation( const TopoDS_Shape& theShape1, // target
222                           const TopoDS_Shape& theShape2, // source
223                           TShapeShapeMap &    theAssociationMap);
224
225   /*!
226    * \brief Finds an edge by its vertices in a main shape of the mesh
227    */
228   TopoDS_Edge GetEdgeByVertices( SMESH_Mesh*          aMesh,
229                                  const TopoDS_Vertex& V1,
230                                  const TopoDS_Vertex& V2);
231
232   /*!
233    * \brief Return another face sharing an edge
234    * \param edgeToFaces - data map of descendants to ancestors
235    */
236   TopoDS_Face GetNextFace( const TAncestorMap& edgeToFaces,
237                            const TopoDS_Edge&  edge,
238                            const TopoDS_Face&  face);
239   /*!
240    * \brief Return other vertex of an edge
241    */
242   TopoDS_Vertex GetNextVertex(const TopoDS_Edge&   edge,
243                               const TopoDS_Vertex& vertex);
244
245   /*!
246    * \brief Return an oriented propagation edge
247    * \param aMesh - mesh
248    * \param fromEdge - start edge for propagation
249    * \param chain - return, if provided, a propagation chain passed till
250    *        anEdge; if anEdge.IsNull() then a full propagation chain is returned
251    * \retval pair<int,TopoDS_Edge> - propagation step and found edge
252    */
253   std::pair<int,TopoDS_Edge> GetPropagationEdge( SMESH_Mesh*                 aMesh,
254                                                  const TopoDS_Edge&          anEdge,
255                                                  const TopoDS_Edge&          fromEdge,
256                                                  TopTools_IndexedMapOfShape* chain=0);
257
258   /*!
259    * \brief Find corresponding nodes on two faces
260    * \param face1 - the first face
261    * \param mesh1 - mesh containing elements on the first face
262    * \param face2 - the second face
263    * \param mesh2 - mesh containing elements on the second face
264    * \param assocMap - map associating sub-shapes of the faces
265    * \param nodeIn2OutMap - map containing found matching nodes
266    * \retval bool - is a success
267    */
268   bool FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
269                                  SMESH_Mesh*            mesh1,
270                                  const TopoDS_Face&     face2,
271                                  SMESH_Mesh*            mesh2,
272                                  const TShapeShapeMap & assocMap,
273                                  TNodeNodeMap &         nodeIn2OutMap);
274   /*!
275    * \brief Return any sub-shape of a face belonging to the outer wire
276    * \param face - the face
277    * \param type - type of sub-shape to return
278    * \retval TopoDS_Shape - the found sub-shape
279    */
280   TopoDS_Shape OuterShape( const TopoDS_Face& face,
281                            TopAbs_ShapeEnum   type);
282
283   /*!
284    * \brief Check that submeshis is computed and try to compute it if is not
285    * \param sm - submesh to compute
286    * \param iterationNb - int used to stop infinite recursive call
287    * \retval bool - true if computed
288    */
289   bool MakeComputed(SMESH_subMesh * sm, const int iterationNb = 0);
290
291   /*!
292    * \brief Returns an error message to show in case if MakeComputed( sm ) fails.
293    */
294   std::string SourceNotComputedError( SMESH_subMesh * sm = 0,
295                                       SMESH_Algo*     projAlgo=0);
296
297   /*!
298    * \brief Set event listeners to submesh with projection algo
299    * \param subMesh - submesh with projection algo
300    * \param srcShape - source shape
301    * \param srcMesh - source mesh
302    */
303   void SetEventListener(SMESH_subMesh* subMesh,
304                         TopoDS_Shape   srcShape,
305                         SMESH_Mesh*    srcMesh);
306
307   /*!
308    * \brief Return a boundary EDGE (or all boundary EDGEs) of edgeContainer
309    */
310   TopoDS_Edge GetBoundaryEdge(const TopoDS_Shape&       edgeContainer,
311                               const SMESH_Mesh&         mesh,
312                               std::list< TopoDS_Edge >* allBndEdges = 0 );
313 };
314
315 #endif