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