Salome HOME
271d53fe08327e422c8410adcf8fe85667189fef
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.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 #include "SMDS_MeshNode.hxx"
24 #include "SMDS_SpacePosition.hxx"
25 #include "SMDS_IteratorOfElements.hxx"
26
27 //=======================================================================
28 //function : SMDS_MeshNode
29 //purpose  : 
30 //=======================================================================
31
32 SMDS_MeshNode::SMDS_MeshNode(double x, double y, double z):
33         myX(x), myY(y), myZ(z),
34         myPosition(SMDS_SpacePosition::originSpacePosition())
35 {
36 }
37
38 //=======================================================================
39 //function : RemoveInverseElement
40 //purpose  : 
41 //=======================================================================
42
43 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
44 {
45         myInverseElements.erase(parent);
46 }
47
48 //=======================================================================
49 //function : Print
50 //purpose  : 
51 //=======================================================================
52
53 void SMDS_MeshNode::Print(ostream & OS) const
54 {
55         OS << "Node <" << GetID() << "> : X = " << myX << " Y = "
56                 << myY << " Z = " << myZ << endl;
57 }
58
59 //=======================================================================
60 //function : SetPosition
61 //purpose  : 
62 //=======================================================================
63
64 void SMDS_MeshNode::SetPosition(SMDS_Position * aPos)
65 {
66         myPosition = aPos;
67 }
68
69 //=======================================================================
70 //function : GetPosition
71 //purpose  : 
72 //=======================================================================
73
74 SMDS_Position *SMDS_MeshNode::GetPosition()
75 {
76         return myPosition;
77 }
78
79 const SMDS_Position *SMDS_MeshNode::GetPosition() const
80 {
81         return myPosition;
82 }
83 /**
84 */
85 SMDS_Iterator<const SMDS_MeshElement*> * SMDS_MeshNode::
86         GetInverseElementIterator() const
87 {
88         class SMDS_InverseElementIterator:public SMDS_Iterator<const SMDS_MeshElement*>
89         {
90                 const set<const SMDS_MeshElement*>& mySet;
91                 set<const SMDS_MeshElement*>::iterator myIterator;
92           public:
93                 SMDS_InverseElementIterator(const set<const SMDS_MeshElement*>& s):mySet(s)
94                 {
95                         myIterator=mySet.begin();
96                 }
97
98                 bool more()
99                 {
100                         myIterator!=mySet.end();
101                 }
102
103                 const SMDS_MeshElement* next()
104                 {
105                         const SMDS_MeshElement* current=*myIterator;
106                         myIterator++;
107                         return current; 
108                 }       
109         };
110         return new SMDS_InverseElementIterator(myInverseElements);
111 }
112
113 SMDS_Iterator<const SMDS_MeshElement *> * SMDS_MeshNode::
114         elementsIterator(SMDSAbs_ElementType type) const
115 {
116         // Same as GetInverseElementIterator but the create iterator only return
117         // wanted type elements.
118         class MyIterator:public SMDS_Iterator<const SMDS_MeshElement*>
119         {
120                 set<const SMDS_MeshElement*> mySet;
121                 set<const SMDS_MeshElement*>::iterator myIterator;
122           public:
123                 MyIterator(SMDSAbs_ElementType type,
124                         const set<const SMDS_MeshElement*>& s) 
125                 {
126                         const SMDS_MeshElement * e;
127                         bool toInsert;
128                         set<const SMDS_MeshElement*>::iterator it=s.begin();
129                         while(it!=s.end())
130                         {
131                                 e=*it;
132                                 switch(type)
133                                 {
134                                 case SMDSAbs_Edge: toInsert=true; break;
135                                 case SMDSAbs_Face: toInsert=(e->GetType()!=SMDSAbs_Edge); break;
136                                 case SMDSAbs_Volume: toInsert=(e->GetType()==SMDSAbs_Volume); break;
137                                 }
138                                 if(toInsert) mySet.insert(e);
139                                 it++;
140                         }       
141                         myIterator=mySet.begin();
142                 }
143
144                 bool more()
145                 {
146                         myIterator!=mySet.end();
147                 }
148
149                 const SMDS_MeshElement* next()
150                 {
151                         const SMDS_MeshElement* current=*myIterator;
152                         myIterator++;
153                         return current; 
154                 }       
155         };
156
157         if(type==SMDSAbs_Node)
158                 return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
159         else
160                 return new SMDS_IteratorOfElements(this,type,
161                         new MyIterator(type, myInverseElements));
162 }
163
164 int SMDS_MeshNode::NbNodes() const
165 {
166         return 1;
167 }
168
169 double SMDS_MeshNode::X() const
170 {
171         return myX;
172 }
173
174 double SMDS_MeshNode::Y() const
175 {
176         return myY;
177 }
178
179 double SMDS_MeshNode::Z() const
180 {
181         return myZ;
182 }
183
184 void SMDS_MeshNode::setXYZ(double x, double y, double z)
185 {
186         myX=x;
187         myY=y;
188         myZ=z;  
189 }
190
191 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
192 {
193         return SMDSAbs_Node;
194 }
195
196 //=======================================================================
197 //function : AddInverseElement
198 //purpose  :
199 //=======================================================================
200 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
201 {
202         myInverseElements.insert(ME);
203 }
204
205 //=======================================================================
206 //function : ClearInverseElements
207 //purpose  :
208 //=======================================================================
209 void SMDS_MeshNode::ClearInverseElements()
210 {
211         myInverseElements.clear();
212 }
213
214 bool SMDS_MeshNode::emptyInverseElements()
215 {
216         return myInverseElements.empty();
217 }
218
219 ///////////////////////////////////////////////////////////////////////////////
220 /// To be used with STL set
221 ///////////////////////////////////////////////////////////////////////////////
222 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
223 {
224         return e1.GetID()<e2.GetID();
225         /*if(e1.myX<e2.myX) return true;
226         else if(e1.myX==e2.myX)
227         {
228                 if(e1.myY<e2.myY) return true;
229                 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
230                 else return false;
231         }
232         else return false;*/
233 }
234