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