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