Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMDS / SMDS_FaceOfNodes.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_SetIterator.hxx"
27 #include "SMDS_FaceOfNodes.hxx"
28 #include "SMDS_IteratorOfElements.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_Mesh.hxx"
31
32 #include "utilities.h"
33
34 using namespace std;
35
36 //=======================================================================
37 //function : NbEdges
38 //purpose  : 
39 //=======================================================================
40
41 int SMDS_FaceOfNodes::NbEdges() const
42 {
43         return NbNodes();
44 }
45
46 int SMDS_FaceOfNodes::NbFaces() const
47 {
48         return 1;
49 }
50
51 int SMDS_FaceOfNodes::NbNodes() const
52 {
53         return myNbNodes;
54 }
55
56 //=======================================================================
57 //function : Print
58 //purpose  : 
59 //=======================================================================
60
61 void SMDS_FaceOfNodes::Print(ostream & OS) const
62 {
63         OS << "face <" << GetID() << " > : ";
64         int i;
65         for (i = 0; i < NbNodes() - 1; i++) OS << myNodes[i] << ",";
66         OS << myNodes[i] << ") " << endl;
67 }
68
69 //=======================================================================
70 //function : elementsIterator
71 //purpose  : 
72 //=======================================================================
73
74 class SMDS_FaceOfNodes_MyIterator:public SMDS_NodeArrayElemIterator
75 {
76  public:
77   SMDS_FaceOfNodes_MyIterator(const SMDS_MeshNode* const *s, int l):
78     SMDS_NodeArrayElemIterator( s, & s[ l ] ) {}
79 };
80
81 /// ===================================================================
82 /*!
83  * \brief Iterator on edges of face
84  */
85 /// ===================================================================
86
87 class _MyEdgeIterator : public SMDS_ElemIterator
88 {
89   vector< const SMDS_MeshElement* > myElems;
90   int myIndex;
91 public:
92   _MyEdgeIterator(const SMDS_FaceOfNodes* face):myIndex(0) {
93     myElems.reserve( face->NbNodes() );
94     for ( int i = 0; i < face->NbNodes(); ++i ) {
95       const SMDS_MeshElement* edge =
96         SMDS_Mesh::FindEdge( face->GetNode( i ), face->GetNode( i + 1 ));
97       if ( edge )
98         myElems.push_back( edge );
99     }
100   }
101   /// Return true if and only if there are other object in this iterator
102   virtual bool more() { return myIndex < myElems.size(); }
103
104   /// Return the current object and step to the next one
105   virtual const SMDS_MeshElement* next() { return myElems[ myIndex++ ]; }
106 };
107
108 SMDS_ElemIteratorPtr SMDS_FaceOfNodes::elementsIterator
109                          (SMDSAbs_ElementType type) const
110 {
111   switch(type)
112   {
113   case SMDSAbs_Face:
114     return SMDS_MeshElement::elementsIterator(SMDSAbs_Face);
115   case SMDSAbs_Node:
116     return SMDS_ElemIteratorPtr(new SMDS_FaceOfNodes_MyIterator(myNodes,myNbNodes));
117   case SMDSAbs_Edge:
118     return SMDS_ElemIteratorPtr(new _MyEdgeIterator( this ));
119     break;
120   default:
121     return SMDS_ElemIteratorPtr
122       (new SMDS_IteratorOfElements
123        (this,type,SMDS_ElemIteratorPtr
124         (new SMDS_FaceOfNodes_MyIterator(myNodes,myNbNodes))));
125   }
126   return SMDS_ElemIteratorPtr();
127 }
128
129 SMDS_FaceOfNodes::SMDS_FaceOfNodes(const SMDS_MeshNode* node1,
130                                    const SMDS_MeshNode* node2,
131                                    const SMDS_MeshNode* node3)
132 {
133         myNbNodes = 3;
134         myNodes[0]=node1;
135         myNodes[1]=node2;
136         myNodes[2]=node3;
137         myNodes[3]=0;
138 }
139
140 SMDS_FaceOfNodes::SMDS_FaceOfNodes(const SMDS_MeshNode* node1,
141                                    const SMDS_MeshNode* node2,
142                                    const SMDS_MeshNode* node3,
143                                    const SMDS_MeshNode* node4)
144 {
145         myNbNodes = 4;
146         myNodes[0]=node1;
147         myNodes[1]=node2;
148         myNodes[2]=node3;
149         myNodes[3]=node4;       
150 }
151 bool SMDS_FaceOfNodes::ChangeNodes(const SMDS_MeshNode* nodes[],
152                                    const int            nbNodes)
153 {
154   myNbNodes = nbNodes;
155   myNodes[0]=nodes[0];
156   myNodes[1]=nodes[1];
157   myNodes[2]=nodes[2];
158   if (nbNodes == 4)
159     myNodes[3]=nodes[3];
160   else if (nbNodes != 3)
161     return false;
162
163   return true;
164 }
165
166 /*!
167  * \brief Return node by its index
168  * \param ind - node index
169  * \retval const SMDS_MeshNode* - the node
170  * 
171  * Index is wrapped if it is out of a valid range
172  */
173 const SMDS_MeshNode* SMDS_FaceOfNodes::GetNode(const int ind) const
174 {
175   return myNodes[ WrappedIndex( ind )];
176 }
177
178 /*bool operator<(const SMDS_FaceOfNodes& f1, const SMDS_FaceOfNodes& f2)
179 {
180         set<SMDS_MeshNode> set1,set2;
181         SMDS_ElemIteratorPtr it;
182         const SMDS_MeshNode * n;
183
184         it=f1.nodesIterator();
185
186         while(it->more())
187         {
188                 n=static_cast<const SMDS_MeshNode *>(it->next());
189                 set1.insert(*n);
190         }
191
192         delete it;
193         it=f2.nodesIterator();
194         
195         while(it->more())
196         {       
197                 n=static_cast<const SMDS_MeshNode *>(it->next());
198                 set2.insert(*n);
199         }
200
201         delete it;
202         return set1<set2;       
203
204 }*/
205