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