Salome HOME
const SMDS_MeshElement* next()
[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     if ( !more() ) return 0;
121     const SMDS_MeshElement* current=myIterator.Value();
122     myIterator.Next();
123     return current;
124   }     
125 };
126
127 SMDS_ElemIteratorPtr SMDS_MeshNode::
128         GetInverseElementIterator(SMDSAbs_ElementType type) const
129 {
130   return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(myInverseElements,type));
131 }
132
133 // Same as GetInverseElementIterator but the create iterator only return
134 // wanted type elements.
135 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
136 {
137   NCollection_List<const SMDS_MeshElement*> mySet;
138   NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
139  public:
140   SMDS_MeshNode_MyIterator(SMDSAbs_ElementType type,
141                            const NCollection_List<const SMDS_MeshElement*>& s)
142   {
143     const SMDS_MeshElement * e;
144     bool toInsert;
145     NCollection_List<const SMDS_MeshElement*>::Iterator it(s);
146     for(; it.More(); it.Next())
147     {
148       e=it.Value();
149       switch(type)
150       {
151       case SMDSAbs_Edge: toInsert=true; break;
152       case SMDSAbs_Face: toInsert=(e->GetType()!=SMDSAbs_Edge); break;
153       case SMDSAbs_Volume: toInsert=(e->GetType()==SMDSAbs_Volume); break;
154       }
155       if(toInsert) mySet.Append(e);
156     }
157     myIterator.Init(mySet);
158   }
159
160   bool more()
161   {
162     return myIterator.More() != Standard_False;
163   }
164
165   const SMDS_MeshElement* next()
166   {
167     const SMDS_MeshElement* current=myIterator.Value();
168     myIterator.Next();
169     return current;
170   }
171 };
172
173 SMDS_ElemIteratorPtr SMDS_MeshNode::
174         elementsIterator(SMDSAbs_ElementType type) const
175 {
176   if(type==SMDSAbs_Node)
177     return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
178   else
179     return SMDS_ElemIteratorPtr
180       (new SMDS_IteratorOfElements
181        (this,type,
182         SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(type, myInverseElements))));
183 }
184
185 int SMDS_MeshNode::NbNodes() const
186 {
187         return 1;
188 }
189
190 double SMDS_MeshNode::X() const
191 {
192         return myX;
193 }
194
195 double SMDS_MeshNode::Y() const
196 {
197         return myY;
198 }
199
200 double SMDS_MeshNode::Z() const
201 {
202         return myZ;
203 }
204
205 void SMDS_MeshNode::setXYZ(double x, double y, double z)
206 {
207         myX=x;
208         myY=y;
209         myZ=z;  
210 }
211
212 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
213 {
214         return SMDSAbs_Node;
215 }
216
217 //=======================================================================
218 //function : AddInverseElement
219 //purpose  :
220 //=======================================================================
221 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
222 {
223   NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
224   for (; it.More(); it.Next()) {
225     const SMDS_MeshElement* elem = it.Value();
226     if (elem == ME)
227       return;
228   }
229   myInverseElements.Append(ME);
230 }
231
232 //=======================================================================
233 //function : ClearInverseElements
234 //purpose  :
235 //=======================================================================
236 void SMDS_MeshNode::ClearInverseElements()
237 {
238   myInverseElements.Clear();
239 }
240
241 bool SMDS_MeshNode::emptyInverseElements()
242 {
243   return myInverseElements.IsEmpty() != Standard_False;
244 }
245
246 //================================================================================
247 /*!
248  * \brief Count inverse elements of given type
249  */
250 //================================================================================
251
252 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
253 {
254   if ( type == SMDSAbs_All )
255     return myInverseElements.Extent();
256   int nb = 0;
257   NCollection_List<const SMDS_MeshElement*>::Iterator it( myInverseElements );
258   for ( ; it.More(); it.Next() )
259     if ( it.Value()->GetType() == type )
260       nb++;
261   return nb;
262 }
263
264 ///////////////////////////////////////////////////////////////////////////////
265 /// To be used with STL set
266 ///////////////////////////////////////////////////////////////////////////////
267 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
268 {
269         return e1.GetID()<e2.GetID();
270         /*if(e1.myX<e2.myX) return true;
271         else if(e1.myX==e2.myX)
272         {
273                 if(e1.myY<e2.myY) return true;
274                 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
275                 else return false;
276         }
277         else return false;*/
278 }
279