Salome HOME
Fix for problem: SIGSEGV appears if to select group after opening "Edit Group" dialog...
[modules/smesh.git] / src / SMDS / SMDS_MeshEdge.cxx
1 //  SMESH SMDS : implementaion of Salome mesh data structure
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   : SMDS_MeshEdge.cxx
25 //  Author : Jean-Michel BOULCOURT
26 //  Module : SMESH
27
28 #ifdef _MSC_VER
29 #pragma warning(disable:4786)
30 #endif
31
32 #include "SMDS_MeshEdge.hxx"
33 #include "SMDS_IteratorOfElements.hxx"
34 #include "SMDS_MeshNode.hxx"
35
36 using namespace std;
37
38 //=======================================================================
39 //function : SMDS_MeshEdge
40 //purpose  : 
41 //=======================================================================
42
43 SMDS_MeshEdge::SMDS_MeshEdge(const SMDS_MeshNode * node1,
44                              const SMDS_MeshNode * node2)
45 {       
46         myNodes[0]=node1;
47         myNodes[1]=node2;
48 }
49
50 //=======================================================================
51 //function : Print
52 //purpose  : 
53 //=======================================================================
54
55 void SMDS_MeshEdge::Print(ostream & OS) const
56 {
57         OS << "edge <" << GetID() << "> : (" << myNodes[0] << " , " << myNodes[1] <<
58                 ") " << endl;
59 }
60
61 int SMDS_MeshEdge::NbNodes() const
62 {
63         return 2;
64 }
65
66 int SMDS_MeshEdge::NbEdges() const
67 {
68         return 1;
69 }
70
71 SMDSAbs_ElementType SMDS_MeshEdge::GetType() const
72 {
73         return SMDSAbs_Edge;
74 }
75
76 class SMDS_MeshEdge_MyNodeIterator:public SMDS_ElemIterator
77 {
78   const SMDS_MeshNode *const* myNodes;
79   int myIndex;
80  public:
81   SMDS_MeshEdge_MyNodeIterator(const SMDS_MeshNode * const* nodes):
82     myNodes(nodes),myIndex(0) {}
83
84   bool more()
85   {
86     return myIndex<2;
87   }
88
89   const SMDS_MeshElement* next()
90   {
91     myIndex++;
92     return myNodes[myIndex-1];
93   }
94 };
95
96 SMDS_ElemIteratorPtr SMDS_MeshEdge::
97         elementsIterator(SMDSAbs_ElementType type) const
98 {
99   switch(type)
100   {
101   case SMDSAbs_Edge:
102     return SMDS_MeshElement::elementsIterator(SMDSAbs_Edge); 
103   case SMDSAbs_Node:
104     return SMDS_ElemIteratorPtr(new SMDS_MeshEdge_MyNodeIterator(myNodes));
105   default:
106     return SMDS_ElemIteratorPtr
107       (new SMDS_IteratorOfElements
108        (this,type, SMDS_ElemIteratorPtr(new SMDS_MeshEdge_MyNodeIterator(myNodes))));
109   }
110 }
111
112 bool operator<(const SMDS_MeshEdge & e1, const SMDS_MeshEdge & e2)
113 {
114         int id11=e1.myNodes[0]->GetID();
115         int id21=e2.myNodes[0]->GetID();
116         int id12=e1.myNodes[1]->GetID();
117         int id22=e2.myNodes[1]->GetID();
118         int tmp;
119
120         if(id11>=id12) 
121         {
122                 tmp=id11;
123                 id11=id12;
124                 id12=tmp;       
125         }
126         if(id21>=id22) 
127         {
128                 tmp=id21;
129                 id21=id22;
130                 id22=tmp;       
131         }
132
133         if(id11<id21) return true;
134         else if(id11==id21) return (id21<id22);
135         else return false;
136 }
137
138 //=======================================================================
139 //function : ChangeNodes
140 //purpose  : 
141 //=======================================================================
142
143 bool SMDS_MeshEdge::ChangeNodes(const SMDS_MeshNode * node1,
144                                 const SMDS_MeshNode * node2)
145 {
146   myNodes[0]=node1;
147   myNodes[1]=node2;
148   return true;
149 }
150