Salome HOME
Remove Opencascade dependencies. Change to STL.
[modules/smesh.git] / src / SMESHDS / SMESHDS_Mesh.cxx
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   : SMESH_Mesh.cxx
25 //  Author : Yves FRICAUD, OCC
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHDS_Mesh.hxx"
30 #include "SMDS_VertexPosition.hxx"
31 #include "SMDS_EdgePosition.hxx"
32 #include "SMDS_FacePosition.hxx"
33 #include <TopExp_Explorer.hxx>
34 #include <TopExp.hxx>
35
36 #include "utilities.h"
37 //=======================================================================
38 //function : Create
39 //purpose  : 
40 //=======================================================================
41 SMESHDS_Mesh::SMESHDS_Mesh(int MeshID):myMeshID(MeshID)
42 {
43         myScript = new SMESHDS_Script();
44 }
45
46 //=======================================================================
47 //function : ShapeToMesh
48 //purpose  : 
49 //=======================================================================
50 void SMESHDS_Mesh::ShapeToMesh(const TopoDS_Shape & S)
51 {
52         myShape = S;
53         TopExp::MapShapes(myShape, myIndexToShape);
54 }
55
56 //=======================================================================
57 //function : AddHypothesis
58 //purpose  : 
59 //=======================================================================
60
61 bool SMESHDS_Mesh::AddHypothesis(const TopoDS_Shape & SS,
62         const SMESHDS_Hypothesis * H)
63 {
64         list<const SMESHDS_Hypothesis *>& alist=myShapeToHypothesis[SS];
65
66         //Check if the Hypothesis is still present
67         list<const SMESHDS_Hypothesis*>::iterator ith=alist.begin();
68
69         for (; ith!=alist.end(); ith++)
70                 if (H == *ith) return false;
71
72         alist.push_back(H);
73         return true;
74 }
75
76 //=======================================================================
77 //function : RemoveHypothesis
78 //purpose  : 
79 //=======================================================================
80
81 bool SMESHDS_Mesh::RemoveHypothesis(const TopoDS_Shape & S,
82         const SMESHDS_Hypothesis * H)
83 {
84         ShapeToHypothesis::iterator its=myShapeToHypothesis.find(S);
85         if(its!=myShapeToHypothesis.end())
86         {
87                 list<const SMESHDS_Hypothesis*>::iterator ith=(*its).second.begin();
88
89                 for (; ith!=(*its).second.end(); ith++)
90                         if (H == *ith)
91                         {
92                                 (*its).second.erase(ith);
93                                 return true;
94                         }
95         }
96         return false;
97 }
98
99 //=======================================================================
100 //function : AddNode
101 //purpose  : 
102 //=======================================================================
103 SMDS_MeshNode* SMESHDS_Mesh::AddNode(double x, double y, double z)
104 {
105         SMDS_MeshNode* node = SMDS_Mesh::AddNode(x, y, z);
106         if(node!=NULL) myScript->AddNode(node->GetID(), x, y, z);
107         return node;
108 }
109
110 //=======================================================================
111 //function : MoveNode
112 //purpose  : 
113 //=======================================================================
114 void SMESHDS_Mesh::MoveNode(int ID, double x, double y, double z)
115 {
116         SMDS_MeshNode * node=const_cast<SMDS_MeshNode*>(FindNode(ID));
117         node->setXYZ(x,y,z);
118         myScript->MoveNode(ID, x, y, z);
119 }
120
121 //=======================================================================
122 //function : AddEdge
123 //purpose  : 
124 //=======================================================================
125 SMDS_MeshEdge* SMESHDS_Mesh::AddEdge(int idnode1, int idnode2)
126 {
127         SMDS_MeshEdge* e = SMDS_Mesh::AddEdge(idnode1, idnode2);
128         if(e!=NULL) myScript->AddEdge(e->GetID(), idnode1, idnode2);
129         return e;
130 }
131
132 //=======================================================================
133 //function :AddFace
134 //purpose  : 
135 //=======================================================================
136 SMDS_MeshFace* SMESHDS_Mesh::AddFace(int idnode1, int idnode2, int idnode3)
137 {
138         SMDS_MeshFace *f = SMDS_Mesh::AddFace(idnode1, idnode2, idnode3);
139         if(f!=NULL) myScript->AddFace(f->GetID(), idnode1, idnode2, idnode3);
140         return f;
141 }
142
143 //=======================================================================
144 //function :AddFace
145 //purpose  : 
146 //=======================================================================
147 SMDS_MeshFace* SMESHDS_Mesh::AddFace(int idnode1, int idnode2, int idnode3,
148         int idnode4)
149 {
150         SMDS_MeshFace *f = SMDS_Mesh::AddFace(idnode1, idnode2, idnode3, idnode4);
151         if(f!=NULL)
152                 myScript->AddFace(f->GetID(), idnode1, idnode2, idnode3, idnode4);
153         return f;
154 }
155
156 //=======================================================================
157 //function :AddVolume
158 //purpose  : 
159 //=======================================================================
160 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
161         int idnode4)
162 {
163         SMDS_MeshVolume *f = SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3,
164                 idnode4);
165         if(f!=NULL)
166                 myScript->AddVolume(f->GetID(), idnode1, idnode2, idnode3, idnode4);
167         return f;
168 }
169
170 //=======================================================================
171 //function :AddVolume
172 //purpose  : 
173 //=======================================================================
174 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
175         int idnode4, int idnode5)
176 {
177         SMDS_MeshVolume *v = SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3,
178                 idnode4, idnode5);
179         if(v!=NULL)
180                 myScript->AddVolume(v->GetID(), idnode1, idnode2, idnode3, idnode4,
181                         idnode5);
182         return v;
183 }
184
185 //=======================================================================
186 //function :AddVolume
187 //purpose  : 
188 //=======================================================================
189 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
190         int idnode4, int idnode5, int idnode6)
191 {
192         SMDS_MeshVolume *v=
193                 SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3, idnode4, idnode5,
194                 idnode6);
195         if(v!=NULL) 
196                 myScript->AddVolume(v->GetID(), idnode1, idnode2, idnode3, idnode4,
197                         idnode5, idnode6);
198         return v;
199 }
200
201 //=======================================================================
202 //function :AddVolume
203 //purpose  : 
204 //=======================================================================
205 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
206         int idnode4, int idnode5, int idnode6, int idnode7, int idnode8)
207 {
208         SMDS_MeshVolume *v=
209                 SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3, idnode4, idnode5,
210                 idnode6, idnode7, idnode8);
211         if(v!=NULL)
212                 myScript->AddVolume(v->GetID(), idnode1, idnode2, idnode3, idnode4,
213                         idnode5, idnode6, idnode7, idnode8);
214         return v;
215 }
216
217 //=======================================================================
218 //function : RemoveNode
219 //purpose  : 
220 //=======================================================================
221 void SMESHDS_Mesh::RemoveNode(int ID)
222 {
223         SMDS_Mesh::RemoveNode(ID);
224         myScript->RemoveNode(ID);
225 }
226
227 //=======================================================================
228 //function : RemoveElement
229 //purpose  : 
230 //========================================================================
231 void SMESHDS_Mesh::RemoveElement(int ID)
232 {
233         SMDS_Mesh::RemoveElement(ID);
234         myScript->RemoveElement(ID);
235 }
236
237 //=======================================================================
238 //function : SetNodeOnVolume
239 //purpose  : 
240 //=======================================================================
241 void SMESHDS_Mesh::SetNodeInVolume(SMDS_MeshNode * aNode,
242         const TopoDS_Shell & S)
243 {
244         if (myShape.IsNull()) MESSAGE("myShape is NULL");
245
246         int Index = myIndexToShape.FindIndex(S);
247
248         //Set Position on Node
249         //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
250         //aNode->SetPosition(aPos);
251
252         //Update or build submesh
253         map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
254         if (it==myShapeIndexToSubMesh.end())
255                 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
256
257         myShapeIndexToSubMesh[Index]->AddNode(aNode);
258 }
259
260 //=======================================================================
261 //function : SetNodeOnFace
262 //purpose  : 
263 //=======================================================================
264 void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode * aNode,
265         const TopoDS_Face & S)
266 {
267         if (myShape.IsNull()) MESSAGE("myShape is NULL");
268
269         int Index = myIndexToShape.FindIndex(S);
270
271         //Set Position on Node
272         aNode->SetPosition(new SMDS_FacePosition(Index, 0., 0.));
273
274         //Update or build submesh
275         map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
276         if (it==myShapeIndexToSubMesh.end())
277                 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
278
279         myShapeIndexToSubMesh[Index]->AddNode(aNode);
280 }
281
282 //=======================================================================
283 //function : SetNodeOnEdge
284 //purpose  : 
285 //=======================================================================
286 void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode * aNode,
287         const TopoDS_Edge & S)
288 {
289         if (myShape.IsNull()) MESSAGE("myShape is NULL");
290
291         int Index = myIndexToShape.FindIndex(S);
292
293         //Set Position on Node
294         aNode->SetPosition(new SMDS_EdgePosition(Index, 0.));
295
296         //Update or build submesh
297         map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
298         if (it==myShapeIndexToSubMesh.end())
299                 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
300
301         myShapeIndexToSubMesh[Index]->AddNode(aNode);
302 }
303
304 //=======================================================================
305 //function : SetNodeOnVertex
306 //purpose  : 
307 //=======================================================================
308 void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode * aNode,
309         const TopoDS_Vertex & S)
310 {
311         if (myShape.IsNull()) MESSAGE("myShape is NULL");
312
313         int Index = myIndexToShape.FindIndex(S);
314
315         //Set Position on Node
316         aNode->SetPosition(new SMDS_VertexPosition(Index));
317
318         //Update or build submesh
319         map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
320         if (it==myShapeIndexToSubMesh.end())
321                 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
322
323         myShapeIndexToSubMesh[Index]->AddNode(aNode);
324 }
325
326 //=======================================================================
327 //function : UnSetNodeOnShape
328 //purpose  : 
329 //=======================================================================
330 void SMESHDS_Mesh::UnSetNodeOnShape(const SMDS_MeshNode* aNode)
331 {
332         MESSAGE("not implemented");
333 }
334
335 //=======================================================================
336 //function : SetMeshElementOnShape
337 //purpose  : 
338 //=======================================================================
339 void SMESHDS_Mesh::SetMeshElementOnShape(const SMDS_MeshElement * anElement,
340         const TopoDS_Shape & S)
341 {
342         if (myShape.IsNull()) MESSAGE("myShape is NULL");
343
344         int Index = myIndexToShape.FindIndex(S);
345
346         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
347                 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
348
349         myShapeIndexToSubMesh[Index]->AddElement(anElement);
350 }
351
352 //=======================================================================
353 //function : UnSetMeshElementOnShape
354 //purpose  : 
355 //=======================================================================
356 void SMESHDS_Mesh::
357 UnSetMeshElementOnShape(const SMDS_MeshElement * anElement,
358         const TopoDS_Shape & S)
359 {
360         if (myShape.IsNull()) MESSAGE("myShape is NULL");
361
362         int Index = myIndexToShape.FindIndex(S);
363
364         if (myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end())
365                 myShapeIndexToSubMesh[Index]->RemoveElement(anElement);
366 }
367
368 //=======================================================================
369 //function : ShapeToMesh
370 //purpose  : 
371 //=======================================================================
372 TopoDS_Shape SMESHDS_Mesh::ShapeToMesh() const
373 {
374         return myShape;
375 }
376
377 ///////////////////////////////////////////////////////////////////////////////
378 /// Return the sub mesh linked to the a given TopoDS_Shape or NULL if the given
379 /// TopoDS_Shape is unknown
380 ///////////////////////////////////////////////////////////////////////////////
381 SMESHDS_SubMesh * SMESHDS_Mesh::MeshElements(const TopoDS_Shape & S)
382 {
383         if (myShape.IsNull()) MESSAGE("myShape is NULL");
384
385         int Index = myIndexToShape.FindIndex(S);
386         if (myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end())
387                 return myShapeIndexToSubMesh[Index];
388         else
389                 return NULL;
390 }
391
392 //=======================================================================
393 //function : GetHypothesis
394 //purpose  : 
395 //=======================================================================
396
397 const list<const SMESHDS_Hypothesis*>& SMESHDS_Mesh::GetHypothesis(
398         const TopoDS_Shape & S) const
399 {
400         if (myShapeToHypothesis.find(S)!=myShapeToHypothesis.end())
401                 return myShapeToHypothesis.find(S)->second;
402
403         static list<const SMESHDS_Hypothesis*> empty;
404         return empty;
405 }
406
407 //=======================================================================
408 //function : GetScript
409 //purpose  : 
410 //=======================================================================
411 SMESHDS_Script* SMESHDS_Mesh::GetScript()
412 {
413         return myScript;
414 }
415
416 //=======================================================================
417 //function : ClearScript
418 //purpose  : 
419 //=======================================================================
420 void SMESHDS_Mesh::ClearScript()
421 {
422         myScript->Clear();
423 }
424
425 //=======================================================================
426 //function : HasMeshElements
427 //purpose  : 
428 //=======================================================================
429 bool SMESHDS_Mesh::HasMeshElements(const TopoDS_Shape & S)
430 {
431         if (myShape.IsNull()) MESSAGE("myShape is NULL");
432         int Index = myIndexToShape.FindIndex(S);
433         return myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end();
434 }
435
436 //=======================================================================
437 //function : HasHypothesis
438 //purpose  : 
439 //=======================================================================
440 bool SMESHDS_Mesh::HasHypothesis(const TopoDS_Shape & S)
441 {
442         return myShapeToHypothesis.find(S)!=myShapeToHypothesis.end();
443 }
444
445 //=======================================================================
446 //function : NewSubMesh 
447 //purpose  : 
448 //=======================================================================
449 void SMESHDS_Mesh::NewSubMesh(int Index)
450 {
451         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
452         {
453                 SMESHDS_SubMesh* SM = new SMESHDS_SubMesh(this);
454                 myShapeIndexToSubMesh[Index]=SM;
455         }
456 }
457
458 //=======================================================================
459 //function : IndexToShape
460 //purpose  : 
461 //=======================================================================
462 TopoDS_Shape SMESHDS_Mesh::IndexToShape(int ShapeIndex)
463 {
464         return myIndexToShape.FindKey(ShapeIndex);
465 }
466
467 //=======================================================================
468 //function : ShapeToIndex
469 //purpose  : 
470 //=======================================================================
471 int SMESHDS_Mesh::ShapeToIndex(const TopoDS_Shape & S)
472 {
473         if (myShape.IsNull()) MESSAGE("myShape is NULL");
474         return myIndexToShape.FindIndex(S);
475 }
476
477 //=======================================================================
478 //function : SetNodeOnVolume
479 //purpose  : 
480 //=======================================================================
481 void SMESHDS_Mesh::SetNodeInVolume(const SMDS_MeshNode* aNode, int Index)
482 {
483         //Set Position on Node
484         //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
485         //aNode->SetPosition(aPos);
486
487         //Update or build submesh
488         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
489                 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
490
491         myShapeIndexToSubMesh[Index]->AddNode(aNode);
492 }
493
494 //=======================================================================
495 //function : SetNodeOnFace
496 //purpose  : 
497 //=======================================================================
498 void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode* aNode, int Index)
499 {
500         //Set Position on Node
501         aNode->SetPosition(new SMDS_FacePosition(Index, 0., 0.));
502
503         //Update or build submesh
504         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
505                 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
506
507         myShapeIndexToSubMesh[Index]->AddNode(aNode);
508 }
509
510 //=======================================================================
511 //function : SetNodeOnEdge
512 //purpose  : 
513 //=======================================================================
514 void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode* aNode, int Index)
515 {
516         //Set Position on Node
517         aNode->SetPosition(new SMDS_EdgePosition(Index, 0.));
518
519         //Update or build submesh
520         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
521                 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
522
523         myShapeIndexToSubMesh[Index]->AddNode(aNode);
524 }
525
526 //=======================================================================
527 //function : SetNodeOnVertex
528 //purpose  : 
529 //=======================================================================
530 void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode* aNode, int Index)
531 {
532         //Set Position on Node
533         aNode->SetPosition(new SMDS_VertexPosition(Index));
534
535         //Update or build submesh
536         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
537                 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
538
539         myShapeIndexToSubMesh[Index]->AddNode(aNode);
540 }
541
542 //=======================================================================
543 //function : SetMeshElementOnShape
544 //purpose  : 
545 //=======================================================================
546 void SMESHDS_Mesh::SetMeshElementOnShape(const SMDS_MeshElement* anElement,
547         int Index)
548 {
549         if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
550                 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
551
552         myShapeIndexToSubMesh[Index]->AddElement(anElement);
553 }