Salome HOME
PAL9022. Improve methods binding nodes to shape, allow binding nodes to solid
[modules/smesh.git] / src / SMESHDS / SMESHDS_Mesh.hxx
1 //  SMESH SMESHDS : management of mesh data and SMESH document
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHDS_Mesh.hxx
25 //  Module : SMESH
26
27 #ifndef _SMESHDS_Mesh_HeaderFile
28 #define _SMESHDS_Mesh_HeaderFile
29
30 #include "SMDS_Mesh.hxx"
31 #include "SMDS_MeshNode.hxx"
32 #include "SMDS_MeshEdge.hxx"
33 #include "SMDS_MeshFace.hxx"
34 #include "SMDS_MeshVolume.hxx"
35 #include "SMESHDS_Hypothesis.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESHDS_Script.hxx"
38
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Solid.hxx>
42 #include <TopoDS_Shell.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <map>
47 #ifdef WNT
48 #include <hash_map>
49 #endif
50
51 //Not portable see http://gcc.gnu.org/onlinedocs/libstdc++/faq/index.html#5_4 to know more.
52 #ifdef __GNUC__
53   #if __GNUC__ < 3
54     #include <hash_map.h>
55     namespace gstd { using ::hash_map; }; // inherit globals
56   #else
57     #include <ext/hash_map>
58     #if __GNUC_MINOR__ == 0
59       namespace gstd = std;               // GCC 3.0
60     #else
61       namespace gstd = ::__gnu_cxx;       // GCC 3.1 and later
62     #endif
63   #endif
64 #else      // ...  there are other compilers, right?
65   namespace gstd = std;
66 #endif
67
68 #if defined WNT && defined WIN32 && defined SMESHDS_EXPORTS
69 #define SMESHDS_WNT_EXPORT __declspec( dllexport )
70 #else
71 #define SMESHDS_WNT_EXPORT
72 #endif
73
74 class SMESHDS_GroupBase;
75
76 class SMESHDS_WNT_EXPORT SMESHDS_Mesh:public SMDS_Mesh{
77 public:
78   SMESHDS_Mesh(int MeshID);
79   void ShapeToMesh(const TopoDS_Shape & S);
80   bool AddHypothesis(const TopoDS_Shape & SS, const SMESHDS_Hypothesis * H);
81   bool RemoveHypothesis(const TopoDS_Shape & S, const SMESHDS_Hypothesis * H);
82   
83   virtual SMDS_MeshNode* AddNodeWithID(double x, double y, double z, int ID);
84   virtual SMDS_MeshNode * AddNode(double x, double y, double z);
85   
86   virtual SMDS_MeshEdge* AddEdgeWithID(int n1, int n2, int ID);
87   virtual SMDS_MeshEdge* AddEdgeWithID(const SMDS_MeshNode * n1,
88                                        const SMDS_MeshNode * n2, 
89                                        int ID);
90   virtual SMDS_MeshEdge* AddEdge(const SMDS_MeshNode * n1,
91                                  const SMDS_MeshNode * n2);
92   
93   virtual SMDS_MeshFace* AddFaceWithID(int n1, int n2, int n3, int ID);
94   virtual SMDS_MeshFace* AddFaceWithID(const SMDS_MeshNode * n1,
95                                        const SMDS_MeshNode * n2,
96                                        const SMDS_MeshNode * n3, 
97                                        int ID);
98   virtual SMDS_MeshFace* AddFace(const SMDS_MeshNode * n1,
99                                  const SMDS_MeshNode * n2,
100                                  const SMDS_MeshNode * n3);
101
102   virtual SMDS_MeshFace* AddFaceWithID(int n1, int n2, int n3, int n4, int ID);
103   virtual SMDS_MeshFace* AddFaceWithID(const SMDS_MeshNode * n1,
104                                        const SMDS_MeshNode * n2,
105                                        const SMDS_MeshNode * n3,
106                                        const SMDS_MeshNode * n4, 
107                                        int ID);
108   virtual SMDS_MeshFace* AddFace(const SMDS_MeshNode * n1,
109                                  const SMDS_MeshNode * n2,
110                                  const SMDS_MeshNode * n3,
111                                  const SMDS_MeshNode * n4);
112
113   virtual SMDS_MeshVolume* AddVolumeWithID(int n1, int n2, int n3, int n4, int ID);
114   virtual SMDS_MeshVolume* AddVolumeWithID(const SMDS_MeshNode * n1,
115                                            const SMDS_MeshNode * n2,
116                                            const SMDS_MeshNode * n3,
117                                            const SMDS_MeshNode * n4, 
118                                            int ID);
119   virtual SMDS_MeshVolume* AddVolume(const SMDS_MeshNode * n1,
120                                      const SMDS_MeshNode * n2,
121                                      const SMDS_MeshNode * n3,
122                                      const SMDS_MeshNode * n4);
123
124   virtual SMDS_MeshVolume* AddVolumeWithID(int n1, int n2, int n3, int n4, int n5, int ID);
125   virtual SMDS_MeshVolume* AddVolumeWithID(const SMDS_MeshNode * n1,
126                                            const SMDS_MeshNode * n2,
127                                            const SMDS_MeshNode * n3,
128                                            const SMDS_MeshNode * n4,
129                                            const SMDS_MeshNode * n5, 
130                                            int ID);
131   virtual SMDS_MeshVolume* AddVolume(const SMDS_MeshNode * n1,
132                                      const SMDS_MeshNode * n2,
133                                      const SMDS_MeshNode * n3,
134                                      const SMDS_MeshNode * n4,
135                                      const SMDS_MeshNode * n5);
136
137   virtual SMDS_MeshVolume* AddVolumeWithID(int n1, int n2, int n3, int n4, int n5, int n6, int ID);
138   virtual SMDS_MeshVolume* AddVolumeWithID(const SMDS_MeshNode * n1,
139                                            const SMDS_MeshNode * n2,
140                                            const SMDS_MeshNode * n3,
141                                            const SMDS_MeshNode * n4,
142                                            const SMDS_MeshNode * n5,
143                                            const SMDS_MeshNode * n6, 
144                                            int ID);
145   virtual SMDS_MeshVolume* AddVolume(const SMDS_MeshNode * n1,
146                                      const SMDS_MeshNode * n2,
147                                      const SMDS_MeshNode * n3,
148                                      const SMDS_MeshNode * n4,
149                                      const SMDS_MeshNode * n5,
150                                      const SMDS_MeshNode * n6);
151
152   virtual SMDS_MeshVolume* AddVolumeWithID(int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int ID);
153   virtual SMDS_MeshVolume* AddVolumeWithID(const SMDS_MeshNode * n1,
154                                            const SMDS_MeshNode * n2,
155                                            const SMDS_MeshNode * n3,
156                                            const SMDS_MeshNode * n4,
157                                            const SMDS_MeshNode * n5,
158                                            const SMDS_MeshNode * n6,
159                                            const SMDS_MeshNode * n7,
160                                            const SMDS_MeshNode * n8, 
161                                            int ID);
162   virtual SMDS_MeshVolume* AddVolume(const SMDS_MeshNode * n1,
163                                      const SMDS_MeshNode * n2,
164                                      const SMDS_MeshNode * n3,
165                                      const SMDS_MeshNode * n4,
166                                      const SMDS_MeshNode * n5,
167                                      const SMDS_MeshNode * n6,
168                                      const SMDS_MeshNode * n7,
169                                      const SMDS_MeshNode * n8);
170   
171   virtual SMDS_MeshFace* AddPolygonalFaceWithID (std::vector<int> nodes_ids,
172                                                  const int        ID);
173
174   virtual SMDS_MeshFace* AddPolygonalFaceWithID (std::vector<const SMDS_MeshNode*> nodes,
175                                                  const int                         ID);
176
177   virtual SMDS_MeshFace* AddPolygonalFace (std::vector<const SMDS_MeshNode*> nodes);
178
179   virtual SMDS_MeshVolume* AddPolyhedralVolumeWithID
180                            (std::vector<int> nodes_ids,
181                             std::vector<int> quantities,
182                             const int        ID);
183
184   virtual SMDS_MeshVolume* AddPolyhedralVolumeWithID
185                            (std::vector<const SMDS_MeshNode*> nodes,
186                             std::vector<int>                  quantities,
187                             const int                         ID);
188
189   virtual SMDS_MeshVolume* AddPolyhedralVolume
190                            (std::vector<const SMDS_MeshNode*> nodes,
191                             std::vector<int>                  quantities);
192
193   void MoveNode(const SMDS_MeshNode *, double x, double y, double z);
194   virtual void RemoveNode(const SMDS_MeshNode *);
195   void RemoveElement(const SMDS_MeshElement *);
196   bool ChangeElementNodes(const SMDS_MeshElement * elem,
197                           const SMDS_MeshNode    * nodes[],
198                           const int                nbnodes);
199   bool ChangePolygonNodes(const SMDS_MeshElement * elem,
200                           std::vector<const SMDS_MeshNode*> nodes);
201   bool ChangePolyhedronNodes(const SMDS_MeshElement * elem,
202                              std::vector<const SMDS_MeshNode*> nodes,
203                              std::vector<int>                  quantities);
204   void Renumber (const bool isNodes, const int startID=1, const int deltaID=1);
205
206   void SetNodeInVolume(SMDS_MeshNode * aNode, const TopoDS_Shell & S);
207   void SetNodeInVolume(SMDS_MeshNode * aNode, const TopoDS_Solid & S);
208   void SetNodeOnFace(SMDS_MeshNode * aNode, const TopoDS_Face & S, double u=0., double v=0.);
209   void SetNodeOnEdge(SMDS_MeshNode * aNode, const TopoDS_Edge & S, double u=0.);
210   void SetNodeOnVertex(SMDS_MeshNode * aNode, const TopoDS_Vertex & S);
211   void UnSetNodeOnShape(const SMDS_MeshNode * aNode);
212   void SetMeshElementOnShape(const SMDS_MeshElement * anElt,
213                              const TopoDS_Shape & S);
214   void UnSetMeshElementOnShape(const SMDS_MeshElement * anElt,
215                                const TopoDS_Shape & S);
216   TopoDS_Shape ShapeToMesh() const;
217   bool HasMeshElements(const TopoDS_Shape & S);
218   SMESHDS_SubMesh * MeshElements(const TopoDS_Shape & S) const;
219   SMESHDS_SubMesh * MeshElements(const int Index);
220   std::list<int> SubMeshIndices();
221   const std::map<int,SMESHDS_SubMesh*>& SubMeshes()
222   { return myShapeIndexToSubMesh; }
223
224   bool HasHypothesis(const TopoDS_Shape & S);
225   const std::list<const SMESHDS_Hypothesis*>& GetHypothesis(const TopoDS_Shape & S) const;
226   SMESHDS_Script * GetScript();
227   void ClearScript();
228   int ShapeToIndex(const TopoDS_Shape & aShape) const;
229   TopoDS_Shape IndexToShape(int ShapeIndex);
230
231   SMESHDS_SubMesh * NewSubMesh(int Index);
232   int AddCompoundSubmesh(const TopoDS_Shape& S, TopAbs_ShapeEnum type = TopAbs_SHAPE);
233   void SetNodeInVolume(const SMDS_MeshNode * aNode, int Index);
234   void SetNodeOnFace(SMDS_MeshNode * aNode, int Index , double u=0., double v=0.);
235   void SetNodeOnEdge(SMDS_MeshNode * aNode, int Index , double u=0.);
236   void SetNodeOnVertex(SMDS_MeshNode * aNode, int Index);
237   void SetMeshElementOnShape(const SMDS_MeshElement * anElt, int Index);
238
239   void AddGroup (SMESHDS_GroupBase* theGroup)      { myGroups.insert(theGroup); }
240   void RemoveGroup (SMESHDS_GroupBase* theGroup)   { myGroups.erase(theGroup); }
241   int GetNbGroups() const                      { return myGroups.size(); }
242   const std::set<SMESHDS_GroupBase*>& GetGroups() const { return myGroups; }
243
244   bool IsGroupOfSubShapes (const TopoDS_Shape& aSubShape) const;
245
246   ~SMESHDS_Mesh();
247   
248 private:
249 #ifndef WNT
250   struct HashTopoDS_Shape{
251     size_t operator()(const TopoDS_Shape& S) const {
252       return S.HashCode(2147483647);
253     }
254   };
255 #else
256   typedef gstd::hash_compare< TopoDS_Shape, less<TopoDS_Shape> > HashTopoDS;
257
258   class HashTopoDS_Shape : public HashTopoDS {
259   public:
260   
261     size_t operator()(const TopoDS_Shape& S) const {
262       return S.HashCode(2147483647);
263     }
264
265         bool operator()(const TopoDS_Shape& S1,const TopoDS_Shape& S2) const {
266                 return S1==S2;
267         }
268   };
269
270
271
272 #endif
273
274   void addNodeToSubmesh( const SMDS_MeshNode* aNode, int Index )
275   {
276     //Update or build submesh
277     map<int,SMESHDS_SubMesh*>::iterator it = myShapeIndexToSubMesh.find( Index );
278     if ( it == myShapeIndexToSubMesh.end() )
279       it = myShapeIndexToSubMesh.insert( make_pair(Index, new SMESHDS_SubMesh() )).first;
280     it->second->AddNode( aNode ); // add aNode to submesh
281   }
282
283   typedef std::list<const SMESHDS_Hypothesis*> THypList;
284
285 #ifndef WNT
286   typedef gstd::hash_map<TopoDS_Shape,THypList,HashTopoDS_Shape> ShapeToHypothesis;
287 #else
288   typedef gstd::hash_map<TopoDS_Shape,THypList,HashTopoDS_Shape> ShapeToHypothesis;
289 #endif
290
291   ShapeToHypothesis          myShapeToHypothesis;
292
293   int                        myMeshID;
294   TopoDS_Shape               myShape;
295
296   typedef std::map<int,SMESHDS_SubMesh*> TShapeIndexToSubMesh;
297   TShapeIndexToSubMesh myShapeIndexToSubMesh;
298
299   TopTools_IndexedMapOfShape myIndexToShape;
300
301   typedef std::set<SMESHDS_GroupBase*> TGroups;
302   TGroups myGroups;
303
304   SMESHDS_Script*            myScript;
305 };
306
307
308 #endif