Salome HOME
bos #18601 GMSH is missing from SMESH algorithms when 2D model is selected
[modules/smesh.git] / src / SMDS / SMDS_MeshVolume.cxx
1 // Copyright (C) 2007-2019  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 : implementation of Salome mesh data structure
24 //  File   : SMDS_MeshVolume.cxx
25 //  Author : Jean-Michel BOULCOURT
26 //  Module : SMESH
27 //
28
29 #include "SMDS_MeshVolume.hxx"
30
31 #include "SMDS_Mesh.hxx"
32 #include "SMDS_VolumeTool.hxx"
33 #include "SMDS_VtkCellIterator.hxx"
34
35 #include <boost/make_shared.hpp>
36
37 // init a polyherdon
38 void SMDS_MeshVolume::init( const std::vector<const SMDS_MeshNode*>& nodes,
39                             const std::vector<int>&                  nbNodesPerFace )
40 {
41   std::vector<vtkIdType> ptIds;
42   ptIds.reserve( nodes.size() + nbNodesPerFace.size() + 1 );
43
44   size_t nbFaces = nbNodesPerFace.size();
45   for ( size_t iN = 0, iF = 0; iF < nbFaces; iF++ )
46   {
47     int nf = nbNodesPerFace[iF];
48     ptIds.push_back(nf);
49     for (int n = 0; n < nf; n++)
50       ptIds.push_back( nodes[ iN++ ]->GetVtkID() );
51   }
52
53   int vtkID = getGrid()->InsertNextLinkedCell(VTK_POLYHEDRON, nbFaces, &ptIds[0]);
54   setVtkID( vtkID );
55 }
56
57 void SMDS_MeshVolume::init( const std::vector<vtkIdType>& vtkNodeIds )
58 {
59   SMDSAbs_EntityType aType = SMDSEntity_Tetra;
60   switch ( vtkNodeIds.size()) // cases are in order of usage frequency
61   {
62   case 4:  aType = SMDSEntity_Tetra;           break;
63   case 5:  aType = SMDSEntity_Pyramid;         break;
64   case 8:  aType = SMDSEntity_Hexa;            break;
65   case 6:  aType = SMDSEntity_Penta;           break;
66   case 10: aType = SMDSEntity_Quad_Tetra;      break;
67   case 20: aType = SMDSEntity_Quad_Hexa;       break;
68   case 13: aType = SMDSEntity_Quad_Pyramid;    break;
69   case 27: aType = SMDSEntity_TriQuad_Hexa;    break;
70   case 15: aType = SMDSEntity_Quad_Penta;      break;
71   case 18: aType = SMDSEntity_BiQuad_Penta;    break;
72   case 12: aType = SMDSEntity_Hexagonal_Prism; break;
73   default: throw SALOME_Exception("wrong volume nodes");
74   }
75   SMDS_MeshCell::init( aType, vtkNodeIds );
76 }
77
78 bool SMDS_MeshVolume::ChangeNodes(const std::vector<const SMDS_MeshNode*>& nodes,
79                                   const std::vector<int>&                  quantities) const
80 {
81   if ( !IsPoly() )
82     return false;
83
84   vtkIdType nFaces = 0;
85   vtkIdType* ptIds = 0;
86   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
87
88   // stream size and nb faces should not change
89
90   if ((int) quantities.size() != nFaces )
91   {
92     return false;
93   }
94   int id = 0, nbPoints = 0;
95   for ( int i = 0; i < nFaces; i++ )
96   {
97     int nodesInFace = ptIds[id];
98     nbPoints += nodesInFace;
99     id += (nodesInFace + 1);
100   }
101   if ((int) nodes.size() != nbPoints )
102   {
103     return false;
104   }
105
106   // update ptIds
107   size_t iP = 0, iN = 0;
108   for ( size_t i = 0; i < quantities.size(); ++i )
109   {
110     ptIds[ iP++ ] = quantities[ i ]; // nb face nodes
111     for ( int j = 0; j < quantities[ i ]; ++j )
112       ptIds[ iP++ ] = nodes[ iN++ ]->GetVtkID();
113   }
114   return true;
115 }
116
117 const SMDS_MeshNode* SMDS_MeshVolume::GetNode(const int ind) const
118 {
119   if ( !IsPoly() )
120     return SMDS_MeshCell::GetNode( ind );
121
122   vtkIdType nFaces = 0;
123   vtkIdType* ptIds = 0;
124   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
125   int id = 0, nbPoints = 0;
126   for (int i = 0; i < nFaces; i++)
127   {
128     int nodesInFace = ptIds[id];
129     if ( ind < nbPoints + nodesInFace )
130       return GetMesh()->FindNodeVtk( ptIds[ 1 + ind + i ]);
131     nbPoints += nodesInFace;
132     id += (nodesInFace + 1);
133   }
134   return 0;
135 }
136 int SMDS_MeshVolume::NbNodes() const
137 {
138   if ( !IsPoly() )
139     return SMDS_MeshCell::NbNodes();
140
141   vtkIdType nFaces = 0;
142   vtkIdType* ptIds = 0;
143   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
144   int id = 0, nbPoints = 0;
145   for (int i = 0; i < nFaces; i++)
146   {
147     int nodesInFace = ptIds[id];
148     nbPoints += nodesInFace;
149     id += (nodesInFace + 1);
150   }
151   return nbPoints;
152 }
153
154 int SMDS_MeshVolume::NbFaces() const
155 {
156   if ( !IsPoly() )
157     return SMDS_MeshCell::NbFaces();
158
159   vtkIdType nFaces = 0;
160   vtkIdType* ptIds = 0;
161   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
162   return nFaces;
163   
164 }
165 int SMDS_MeshVolume::NbEdges() const
166 {
167   if ( !IsPoly() )
168     return SMDS_MeshCell::NbEdges();
169
170   vtkIdType nFaces = 0;
171   vtkIdType* ptIds = 0;
172   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
173   int id = 0, nbEdges = 0;
174   for (int i = 0; i < nFaces; i++)
175   {
176     int edgesInFace = ptIds[id];
177     id += (edgesInFace + 1);
178     nbEdges += edgesInFace;
179   }
180   nbEdges = nbEdges / 2;
181   return nbEdges;
182 }
183
184 int SMDS_MeshVolume::GetNodeIndex( const SMDS_MeshNode* node ) const
185 {
186   if ( !IsPoly() )
187     return SMDS_MeshCell::GetNodeIndex( node );
188
189   vtkIdType nFaces = 0;
190   vtkIdType* ptIds = 0;
191   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
192   int id = 0;
193   for (int iF = 0; iF < nFaces; iF++)
194   {
195     int nodesInFace = ptIds[id];
196     for ( vtkIdType i = 0; i < nodesInFace; ++i )
197       if ( ptIds[id+i+1] == node->GetVtkID() )
198         return id+i-iF;
199     id += (nodesInFace + 1);
200   }
201   return -1;
202 }
203 bool SMDS_MeshVolume::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
204 {
205   return false;
206 }
207
208 bool SMDS_MeshVolume::IsMediumNode(const SMDS_MeshNode* node) const
209 {
210   if ( !IsPoly() )
211     return SMDS_MeshCell::IsMediumNode( node );
212
213   return false;
214 }
215
216 int SMDS_MeshVolume::NbCornerNodes() const
217 {
218   if ( !IsPoly() )
219     return SMDS_MeshCell::NbCornerNodes();
220
221   return NbNodes();
222 }
223
224 int SMDS_MeshVolume::NbFaceNodes (const int face_ind) const
225 {
226   if ( !IsPoly() )
227     return SMDS_VolumeTool( this ).NbFaceNodes( face_ind-1 );
228
229   vtkIdType nFaces = 0;
230   vtkIdType* ptIds = 0;
231   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
232   int id = 0, nbNodes = 0;
233   for (int i = 0; i < nFaces; i++)
234   {
235     int nodesInFace = ptIds[id];
236     id += (nodesInFace + 1);
237     if (i == face_ind - 1)
238     {
239       nbNodes = nodesInFace;
240       break;
241     }
242   }
243   return nbNodes;
244 }
245
246 const SMDS_MeshNode* SMDS_MeshVolume::GetFaceNode (const int face_ind, const int node_ind) const
247 {
248   if ( !IsPoly() )
249     return SMDS_VolumeTool( this ).GetFaceNodes( face_ind-1 )[ node_ind - 1 ];
250
251   vtkIdType nFaces = 0;
252   vtkIdType* ptIds = 0;
253   getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds);
254   int id = 0;
255   for (int i = 0; i < nFaces; i++)
256   {
257     int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
258     if (i == face_ind - 1) // first face is number 1
259     {
260       if ((node_ind > 0) && (node_ind <= nodesInFace))
261         return GetMesh()->FindNodeVtk(ptIds[id + node_ind]); // ptIds[id+1] : first node
262     }
263     id += (nodesInFace + 1);
264   }
265   return 0;
266 }
267
268 std::vector<int> SMDS_MeshVolume::GetQuantities() const
269 {
270   std::vector<int> quantities;
271   if ( IsPoly() )
272   {
273     vtkIdType nFaces = 0;
274     vtkIdType* ptIds = 0;
275     getGrid()->GetFaceStream( GetVtkID(), nFaces, ptIds );
276     int id = 0;
277     for (int i = 0; i < nFaces; i++)
278     {
279       int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
280       quantities.push_back( nodesInFace );
281       id += (nodesInFace + 1);
282     }
283   }
284   return quantities;
285 }
286
287 ///////////////////////////////////////////////////////////////////////////////
288 /// Create an iterator which iterate on nodes owned by the element.
289 ///////////////////////////////////////////////////////////////////////////////
290 SMDS_ElemIteratorPtr SMDS_MeshVolume::nodesIterator() const
291 {
292   if ( !IsPoly() )
293     return SMDS_MeshCell::nodesIterator();
294
295   return boost::make_shared< SMDS_VtkCellIteratorPolyH<> >( GetMesh(), GetVtkID(), GetEntityType());
296 }
297
298 ///////////////////////////////////////////////////////////////////////////////
299 /// Create an iterator which iterate on nodes owned by the element.
300 ///////////////////////////////////////////////////////////////////////////////
301 SMDS_NodeIteratorPtr SMDS_MeshVolume::nodeIterator() const
302 {
303   if ( !IsPoly() )
304     return SMDS_MeshCell::nodeIterator();
305
306   return boost::make_shared< SMDS_VtkCellIteratorPolyH< SMDS_NodeIterator> >( GetMesh(), GetVtkID(), GetEntityType());
307 }