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