Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[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(const SMDS_PositionPtr& aPos)
65 {
66         myPosition = aPos;
67 }
68
69 //=======================================================================
70 //function : GetPosition
71 //purpose  : 
72 //=======================================================================
73
74 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
75 {
76         return myPosition;
77 }
78
79 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
80 {
81   const set<const SMDS_MeshElement*>& mySet;
82   set<const SMDS_MeshElement*>::iterator myIterator;
83  public:
84   SMDS_MeshNode_MyInvIterator(const set<const SMDS_MeshElement*>& s):
85     mySet(s)
86   {
87     myIterator=mySet.begin();
88   }
89
90   bool more()
91   {
92     return myIterator!=mySet.end();
93   }
94
95   const SMDS_MeshElement* next()
96   {
97     const SMDS_MeshElement* current=*myIterator;
98     myIterator++;
99     return current;     
100   }     
101 };
102
103 SMDS_ElemIteratorPtr SMDS_MeshNode::
104         GetInverseElementIterator() const
105 {
106   return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(myInverseElements));
107 }
108
109 // Same as GetInverseElementIterator but the create iterator only return
110 // wanted type elements.
111 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
112 {
113   set<const SMDS_MeshElement*> mySet;
114   set<const SMDS_MeshElement*>::iterator myIterator;
115  public:
116   SMDS_MeshNode_MyIterator(SMDSAbs_ElementType type,
117                            const set<const SMDS_MeshElement*>& s)
118   {
119     const SMDS_MeshElement * e;
120     bool toInsert;
121     set<const SMDS_MeshElement*>::iterator it=s.begin();
122     while(it!=s.end())
123     {
124       e=*it;
125       switch(type)
126       {
127       case SMDSAbs_Edge: toInsert=true; break;
128       case SMDSAbs_Face: toInsert=(e->GetType()!=SMDSAbs_Edge); break;
129       case SMDSAbs_Volume: toInsert=(e->GetType()==SMDSAbs_Volume); break;
130       }
131       if(toInsert) mySet.insert(e);
132       it++;
133     }
134     myIterator=mySet.begin();
135   }
136
137   bool more()
138   {
139     return myIterator!=mySet.end();
140   }
141
142   const SMDS_MeshElement* next()
143   {
144     const SMDS_MeshElement* current=*myIterator;
145     myIterator++;
146     return current;
147   }
148 };
149
150 SMDS_ElemIteratorPtr SMDS_MeshNode::
151         elementsIterator(SMDSAbs_ElementType type) const
152 {
153   if(type==SMDSAbs_Node)
154     return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
155   else
156     return SMDS_ElemIteratorPtr
157       (new SMDS_IteratorOfElements
158        (this,type,
159         SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(type, myInverseElements))));
160 }
161
162 int SMDS_MeshNode::NbNodes() const
163 {
164         return 1;
165 }
166
167 double SMDS_MeshNode::X() const
168 {
169         return myX;
170 }
171
172 double SMDS_MeshNode::Y() const
173 {
174         return myY;
175 }
176
177 double SMDS_MeshNode::Z() const
178 {
179         return myZ;
180 }
181
182 void SMDS_MeshNode::setXYZ(double x, double y, double z)
183 {
184         myX=x;
185         myY=y;
186         myZ=z;  
187 }
188
189 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
190 {
191         return SMDSAbs_Node;
192 }
193
194 //=======================================================================
195 //function : AddInverseElement
196 //purpose  :
197 //=======================================================================
198 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
199 {
200         myInverseElements.insert(ME);
201 }
202
203 //=======================================================================
204 //function : ClearInverseElements
205 //purpose  :
206 //=======================================================================
207 void SMDS_MeshNode::ClearInverseElements()
208 {
209         myInverseElements.clear();
210 }
211
212 bool SMDS_MeshNode::emptyInverseElements()
213 {
214         return myInverseElements.empty();
215 }
216
217 ///////////////////////////////////////////////////////////////////////////////
218 /// To be used with STL set
219 ///////////////////////////////////////////////////////////////////////////////
220 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
221 {
222         return e1.GetID()<e2.GetID();
223         /*if(e1.myX<e2.myX) return true;
224         else if(e1.myX==e2.myX)
225         {
226                 if(e1.myY<e2.myY) return true;
227                 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
228                 else return false;
229         }
230         else return false;*/
231 }
232