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