Salome HOME
dc42ed22f24068e44835f8308ece6c271cdba7e6
[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 "SMDS_MeshElement.hxx"
34
35 #include <TopTools_DataMapOfShapeShape.hxx>
36 #include <TopTools_IndexedMapOfShape.hxx>
37 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <gp_GTrsf.hxx>
42 #include <gp_GTrsf2d.hxx>
43
44 #include <list>
45 #include <map>
46
47 class SMDS_MeshNode;
48 class SMESH_Algo;
49 class SMESH_Hypothesis;
50 class SMESH_Mesh;
51 class SMESH_subMesh;
52 class TopoDS_Shape;
53
54 /*!
55  * \brief Struct used instead of a sole TopTools_DataMapOfShapeShape to avoid
56  *        problems with bidirectional bindings
57  */
58 struct StdMeshers_ShapeShapeBiDirectionMap
59 {
60   TopTools_DataMapOfShapeShape _map1to2, _map2to1;
61
62   enum EAssocType {
63     UNDEF, INIT_VERTEX, PROPAGATION, PARTNER, CLOSE_VERTEX, COMMON_VERTEX, FEW_EF };
64   EAssocType _assocType;
65
66   // convention: s1 - target, s2 - source
67   bool Bind( const TopoDS_Shape& s1, const TopoDS_Shape& s2 )
68   { _map1to2.Bind( s1, s2 ); return _map2to1.Bind( s2, s1 ); }
69   bool IsBound( const TopoDS_Shape& s, const bool isShape2=false ) const 
70   { return (isShape2 ? _map2to1 : _map1to2).IsBound( s ); }
71   bool IsEmpty() const { return _map1to2.IsEmpty(); }
72   int  Extent()  const { return _map1to2.Extent(); }
73   void Clear() { _map1to2.Clear(); _map2to1.Clear(); }
74   const TopoDS_Shape& operator()( const TopoDS_Shape& s, const bool isShape2=false ) const
75   { // if we get a Standard_NoSuchObject here, it means that the calling code
76     // passes incorrect isShape2
77     return (isShape2 ? _map2to1 : _map1to2)( s );
78   }
79   StdMeshers_ShapeShapeBiDirectionMap() : _assocType( UNDEF ) {}
80   void SetAssocType( EAssocType type ) { if ( _assocType == UNDEF ) _assocType = type; }
81 };
82
83 /*!
84  * \brief Methods common to Projection algorithms
85  */
86 namespace StdMeshers_ProjectionUtils
87 {
88   typedef StdMeshers_ShapeShapeBiDirectionMap                  TShapeShapeMap;
89   typedef TopTools_IndexedDataMapOfShapeListOfShape            TAncestorMap;
90   typedef std::map<const SMDS_MeshNode*, const SMDS_MeshNode*,
91                    TIDCompare>                                 TNodeNodeMap;
92
93
94   /*!
95    * \brief Finds transformation beween two sets of 2D points using
96    *        a least square approximation
97    */
98   class TrsfFinder2D
99   {
100     gp_GTrsf2d _trsf;
101     gp_XY      _srcOrig;
102   public:
103     TrsfFinder2D(): _srcOrig(0,0) {}
104
105     void Set( const gp_GTrsf2d& t ) { _trsf = t; } // it's an alternative to Solve()
106
107     bool Solve( const std::vector< gp_XY >& srcPnts,
108                 const std::vector< gp_XY >& tgtPnts );
109
110     gp_XY Transform( const gp_Pnt2d& srcUV ) const;
111
112     bool IsIdentity() const { return ( _trsf.Form() == gp_Identity ); }
113   };
114   /*!
115    * \brief Finds transformation beween two sets of 3D points using
116    *        a least square approximation
117    */
118   class TrsfFinder3D
119   {
120     gp_GTrsf _trsf;
121     gp_XYZ   _srcOrig;
122   public:
123     TrsfFinder3D(): _srcOrig(0,0,0) {}
124
125     void Set( const gp_GTrsf& t ) { _trsf = t; } // it's an alternative to Solve()
126
127     bool Solve( const std::vector< gp_XYZ > & srcPnts,
128                 const std::vector< gp_XYZ > & tgtPnts );
129
130     gp_XYZ Transform( const gp_Pnt& srcP ) const;
131
132     gp_XYZ TransformVec( const gp_Vec& v ) const;
133
134     bool IsIdentity() const { return ( _trsf.Form() == gp_Identity ); }
135
136     bool Invert();
137   };
138
139   /*!
140    * \brief Looks for association of all sub-shapes of two shapes
141    * \param theShape1 - shape 1
142    * \param theMesh1 - mesh built on shape 1
143    * \param theShape2 - shape 2
144    * \param theMesh2 - mesh built on shape 2
145    * \param theAssociation - association map to be filled that may
146    *                         contain association of one or two pairs of vertices
147    * \retval bool - true if association found
148    */
149   bool FindSubShapeAssociation(const TopoDS_Shape& theShape1,
150                                SMESH_Mesh*         theMesh1,
151                                const TopoDS_Shape& theShape2,
152                                SMESH_Mesh*         theMesh2,
153                                TShapeShapeMap &    theAssociationMap);
154
155   /*!
156    * \brief Find association of edges of faces
157    *  \param face1 - face 1
158    *  \param VV1 - vertices of face 1
159    *  \param face2 - face 2
160    *  \param VV2 - vertices of face 2 associated with oned of face 1
161    *  \param edges1 - out list of edges of face 1
162    *  \param edges2 - out list of edges of face 2
163    *  \param isClosenessAssoc - is association starting by VERTEX closeness
164    *  \retval int - nb of edges in an outer wire in a success case, else zero
165    */
166   int FindFaceAssociation(const TopoDS_Face&         face1,
167                           TopoDS_Vertex              VV1[2],
168                           const TopoDS_Face&         face2,
169                           TopoDS_Vertex              VV2[2],
170                           std::list< TopoDS_Edge > & edges1,
171                           std::list< TopoDS_Edge > & edges2,
172                           const bool                 isClosenessAssoc=false);
173
174   /*!
175    * \brief Insert vertex association defined by a hypothesis into a map
176    * \param theHyp - hypothesis
177    * \param theAssociationMap - association map
178    * \param theTargetShape - the shape theHyp assigned to
179    */
180   void InitVertexAssociation( const SMESH_Hypothesis* theHyp,
181                               TShapeShapeMap &        theAssociationMap);
182
183   /*!
184    * \brief Inserts association theShape1 <-> theShape2 to TShapeShapeMap
185    * \param theShape1 - target shape
186    * \param theShape2 - source shape
187    * \param theAssociationMap - association map 
188    * \param theBidirectional - if false, inserts theShape1 -> theShape2 association
189    * \retval bool - true if there was no association for these shapes before
190    */
191   bool InsertAssociation( const TopoDS_Shape& theShape1, // target
192                           const TopoDS_Shape& theShape2, // source
193                           TShapeShapeMap &    theAssociationMap);
194
195   /*!
196    * \brief Finds an edge by its vertices in a main shape of the mesh
197    */
198   TopoDS_Edge GetEdgeByVertices( SMESH_Mesh*          aMesh,
199                                  const TopoDS_Vertex& V1,
200                                  const TopoDS_Vertex& V2);
201
202   /*!
203    * \brief Return another face sharing an edge
204    * \param edgeToFaces - data map of descendants to ancestors
205    */
206   TopoDS_Face GetNextFace( const TAncestorMap& edgeToFaces,
207                            const TopoDS_Edge&  edge,
208                            const TopoDS_Face&  face);
209   /*!
210    * \brief Return other vertex of an edge
211    */
212   TopoDS_Vertex GetNextVertex(const TopoDS_Edge&   edge,
213                               const TopoDS_Vertex& vertex);
214
215   /*!
216    * \brief Return an oriented propagation edge
217    * \param aMesh - mesh
218    * \param fromEdge - start edge for propagation
219    * \param chain - return, if provided, a propagation chain passed till
220    *        anEdge; if anEdge.IsNull() then a full propagation chain is returned
221    * \retval pair<int,TopoDS_Edge> - propagation step and found edge
222    */
223   std::pair<int,TopoDS_Edge> GetPropagationEdge( SMESH_Mesh*                 aMesh,
224                                                  const TopoDS_Edge&          anEdge,
225                                                  const TopoDS_Edge&          fromEdge,
226                                                  TopTools_IndexedMapOfShape* chain=0);
227
228   /*!
229    * \brief Find corresponding nodes on two faces
230    * \param face1 - the first face
231    * \param mesh1 - mesh containing elements on the first face
232    * \param face2 - the second face
233    * \param mesh2 - mesh containing elements on the second face
234    * \param assocMap - map associating sub-shapes of the faces
235    * \param nodeIn2OutMap - map containing found matching nodes
236    * \retval bool - is a success
237    */
238   bool FindMatchingNodesOnFaces( const TopoDS_Face&     face1,
239                                  SMESH_Mesh*            mesh1,
240                                  const TopoDS_Face&     face2,
241                                  SMESH_Mesh*            mesh2,
242                                  const TShapeShapeMap & assocMap,
243                                  TNodeNodeMap &         nodeIn2OutMap);
244   /*!
245    * \brief Return any sub-shape of a face belonging to the outer wire
246    * \param face - the face
247    * \param type - type of sub-shape to return
248    * \retval TopoDS_Shape - the found sub-shape
249    */
250   TopoDS_Shape OuterShape( const TopoDS_Face& face,
251                            TopAbs_ShapeEnum   type);
252
253   /*!
254    * \brief Check that submeshis is computed and try to compute it if is not
255    * \param sm - submesh to compute
256    * \param iterationNb - int used to stop infinite recursive call
257    * \retval bool - true if computed
258    */
259   bool MakeComputed(SMESH_subMesh * sm, const int iterationNb = 0);
260
261   /*!
262    * \brief Returns an error message to show in case if MakeComputed( sm ) fails.
263    */
264   std::string SourceNotComputedError( SMESH_subMesh * sm = 0,
265                                       SMESH_Algo*     projAlgo=0);
266
267   /*!
268    * \brief Set event listeners to submesh with projection algo
269    * \param subMesh - submesh with projection algo
270    * \param srcShape - source shape
271    * \param srcMesh - source mesh
272    */
273   void SetEventListener(SMESH_subMesh* subMesh,
274                         TopoDS_Shape   srcShape,
275                         SMESH_Mesh*    srcMesh);
276
277   /*!
278    * \brief Return a boundary EDGE (or all boundary EDGEs) of edgeContainer
279    */
280   TopoDS_Edge GetBoundaryEdge(const TopoDS_Shape&       edgeContainer,
281                               const SMESH_Mesh&         mesh,
282                               std::list< TopoDS_Edge >* allBndEdges = 0 );
283 };
284
285 #endif