Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMDS : implementaion of Salome mesh data structure
23 //
24 #ifdef _MSC_VER
25 #pragma warning(disable:4786)
26 #endif
27
28 #include "SMDS_MeshNode.hxx"
29 #include "SMDS_SpacePosition.hxx"
30 #include "SMDS_IteratorOfElements.hxx"
31
32 using namespace std;
33
34 //=======================================================================
35 //function : SMDS_MeshNode
36 //purpose  : 
37 //=======================================================================
38
39 SMDS_MeshNode::SMDS_MeshNode(double x, double y, double z):
40         myX(x), myY(y), myZ(z),
41         myPosition(SMDS_SpacePosition::originSpacePosition())
42 {
43 }
44
45 //=======================================================================
46 //function : RemoveInverseElement
47 //purpose  : 
48 //=======================================================================
49
50 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
51 {
52   NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
53   while (it.More()) {
54     const SMDS_MeshElement* elem = it.Value();
55     if (elem == parent)
56       myInverseElements.Remove(it);
57     else
58       it.Next();
59   }
60 }
61
62 //=======================================================================
63 //function : Print
64 //purpose  : 
65 //=======================================================================
66
67 void SMDS_MeshNode::Print(ostream & OS) const
68 {
69         OS << "Node <" << GetID() << "> : X = " << myX << " Y = "
70                 << myY << " Z = " << myZ << endl;
71 }
72
73 //=======================================================================
74 //function : SetPosition
75 //purpose  : 
76 //=======================================================================
77
78 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
79 {
80         myPosition = aPos;
81 }
82
83 //=======================================================================
84 //function : GetPosition
85 //purpose  : 
86 //=======================================================================
87
88 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
89 {
90         return myPosition;
91 }
92
93 //=======================================================================
94 /*!
95  * \brief Iterator on list of elements
96  */
97 //=======================================================================
98
99 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
100 {
101   NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
102   SMDSAbs_ElementType                                 myType;
103  public:
104   SMDS_MeshNode_MyInvIterator(const NCollection_List<const SMDS_MeshElement*>& s,
105                               SMDSAbs_ElementType type):
106     myIterator(s), myType(type)
107   {}
108
109   bool more()
110   {
111     if ( myType != SMDSAbs_All ) {
112       while ( myIterator.More() && myIterator.Value()->GetType() != myType)
113         myIterator.Next();
114     }
115     return myIterator.More() != Standard_False;
116   }
117
118   const SMDS_MeshElement* next()
119   {
120     const SMDS_MeshElement* current=myIterator.Value();
121     myIterator.Next();
122     return current;
123   }     
124 };
125
126 SMDS_ElemIteratorPtr SMDS_MeshNode::
127         GetInverseElementIterator(SMDSAbs_ElementType type) const
128 {
129   return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(myInverseElements,type));
130 }
131
132 // Same as GetInverseElementIterator but the create iterator only return
133 // wanted type elements.
134 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
135 {
136   NCollection_List<const SMDS_MeshElement*> mySet;
137   NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
138  public:
139   SMDS_MeshNode_MyIterator(SMDSAbs_ElementType type,
140                            const NCollection_List<const SMDS_MeshElement*>& s)
141   {
142     const SMDS_MeshElement * e;
143     bool toInsert;
144     NCollection_List<const SMDS_MeshElement*>::Iterator it(s);
145     for(; it.More(); it.Next())
146     {
147       e=it.Value();
148       switch(type)
149       {
150       case SMDSAbs_Edge: toInsert=true; break;
151       case SMDSAbs_Face: toInsert=(e->GetType()!=SMDSAbs_Edge); break;
152       case SMDSAbs_Volume: toInsert=(e->GetType()==SMDSAbs_Volume); break;
153       }
154       if(toInsert) mySet.Append(e);
155     }
156     myIterator.Init(mySet);
157   }
158
159   bool more()
160   {
161     return myIterator.More() != Standard_False;
162   }
163
164   const SMDS_MeshElement* next()
165   {
166     const SMDS_MeshElement* current=myIterator.Value();
167     myIterator.Next();
168     return current;
169   }
170 };
171
172 SMDS_ElemIteratorPtr SMDS_MeshNode::
173         elementsIterator(SMDSAbs_ElementType type) const
174 {
175   if(type==SMDSAbs_Node)
176     return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
177   else
178     return SMDS_ElemIteratorPtr
179       (new SMDS_IteratorOfElements
180        (this,type,
181         SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(type, myInverseElements))));
182 }
183
184 int SMDS_MeshNode::NbNodes() const
185 {
186         return 1;
187 }
188
189 double SMDS_MeshNode::X() const
190 {
191         return myX;
192 }
193
194 double SMDS_MeshNode::Y() const
195 {
196         return myY;
197 }
198
199 double SMDS_MeshNode::Z() const
200 {
201         return myZ;
202 }
203
204 void SMDS_MeshNode::setXYZ(double x, double y, double z)
205 {
206         myX=x;
207         myY=y;
208         myZ=z;  
209 }
210
211 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
212 {
213         return SMDSAbs_Node;
214 }
215
216 //=======================================================================
217 //function : AddInverseElement
218 //purpose  :
219 //=======================================================================
220 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
221 {
222   NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
223   for (; it.More(); it.Next()) {
224     const SMDS_MeshElement* elem = it.Value();
225     if (elem == ME)
226       return;
227   }
228   myInverseElements.Append(ME);
229 }
230
231 //=======================================================================
232 //function : ClearInverseElements
233 //purpose  :
234 //=======================================================================
235 void SMDS_MeshNode::ClearInverseElements()
236 {
237   myInverseElements.Clear();
238 }
239
240 bool SMDS_MeshNode::emptyInverseElements()
241 {
242   return myInverseElements.IsEmpty() != Standard_False;
243 }
244
245 //================================================================================
246 /*!
247  * \brief Count inverse elements of given type
248  */
249 //================================================================================
250
251 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
252 {
253   if ( type == SMDSAbs_All )
254     return myInverseElements.Extent();
255   int nb = 0;
256   NCollection_List<const SMDS_MeshElement*>::Iterator it( myInverseElements );
257   for ( ; it.More(); it.Next() )
258     if ( it.Value()->GetType() == type )
259       nb++;
260   return nb;
261 }
262
263 ///////////////////////////////////////////////////////////////////////////////
264 /// To be used with STL set
265 ///////////////////////////////////////////////////////////////////////////////
266 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
267 {
268         return e1.GetID()<e2.GetID();
269         /*if(e1.myX<e2.myX) return true;
270         else if(e1.myX==e2.myX)
271         {
272                 if(e1.myY<e2.myY) return true;
273                 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
274                 else return false;
275         }
276         else return false;*/
277 }
278