1 // Copyright (C) 2010-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SMDS_VtkVolume.hxx"
21 #include "SMDS_MeshNode.hxx"
22 #include "SMDS_Mesh.hxx"
23 #include "SMDS_VtkCellIterator.hxx"
25 #include "utilities.h"
29 SMDS_VtkVolume::SMDS_VtkVolume()
33 SMDS_VtkVolume::SMDS_VtkVolume(const std::vector<vtkIdType>& nodeIds, SMDS_Mesh* mesh)
38 * typed used are vtk types (@see vtkCellType.h)
39 * see GetEntityType() for conversion in SMDS type (@see SMDSAbs_ElementType.hxx)
41 void SMDS_VtkVolume::init(const std::vector<vtkIdType>& nodeIds, SMDS_Mesh* mesh)
43 SMDS_MeshVolume::init();
44 myMeshId = mesh->getMeshId();
45 vtkIdType aType = VTK_TETRA;
46 switch (nodeIds.size()) // cases are in order of usage frequency
48 case 4: aType = VTK_TETRA; break;
49 case 8: aType = VTK_HEXAHEDRON; break;
50 case 5: aType = VTK_PYRAMID; break;
51 case 6: aType = VTK_WEDGE; break;
52 case 10: aType = VTK_QUADRATIC_TETRA; break;
53 case 20: aType = VTK_QUADRATIC_HEXAHEDRON; break;
54 case 13: aType = VTK_QUADRATIC_PYRAMID; break;
55 case 15: aType = VTK_QUADRATIC_WEDGE; break;
56 case 12: aType = VTK_HEXAGONAL_PRISM; break;
57 case 27: aType = VTK_TRIQUADRATIC_HEXAHEDRON;break;
58 default: aType = VTK_HEXAHEDRON;
60 myVtkID = mesh->getGrid()->InsertNextLinkedCell(aType, nodeIds.size(), (vtkIdType *) &nodeIds[0]);
61 mesh->setMyModified();
64 void SMDS_VtkVolume::initPoly(const std::vector<vtkIdType>& nodeIds,
65 const std::vector<int>& nbNodesPerFace,
68 SMDS_MeshVolume::init();
69 SMDS_UnstructuredGrid* grid = mesh->getGrid();
71 //this->gravityCenter(grid, &nodeIds[0], nodeIds.size(), ¢er[0]);
72 std::vector<vtkIdType> ptIds;
73 vtkIdType nbFaces = nbNodesPerFace.size();
75 for (int i = 0; i < nbFaces; i++)
77 int nf = nbNodesPerFace[i];
79 // EAP: a right approach is:
80 // - either the user should care of order of nodes or
81 // - the user should use a service method arranging nodes if he
82 // don't want or can't to do it by him-self
83 // The method below works OK only with planar faces and convex polyhedrones
88 // grid->GetPoints()->GetPoint(nodeIds[k], a);
89 // grid->GetPoints()->GetPoint(nodeIds[k + 1], b);
90 // grid->GetPoints()->GetPoint(nodeIds[k + 2], c);
91 // bool isFaceForward = this->isForward(a, b, c, center);
92 const vtkIdType *facePts = &nodeIds[k];
94 for (int n = 0; n < nf; n++)
95 ptIds.push_back(facePts[n]);
97 // for (int n = nf - 1; n >= 0; n--)
98 // ptIds.push_back(facePts[n]);
101 myVtkID = grid->InsertNextLinkedCell(VTK_POLYHEDRON, nbFaces, &ptIds[0]);
102 mesh->setMyModified();
105 bool SMDS_VtkVolume::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
107 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
110 grid->GetCellPoints(myVtkID, npts, pts);
113 MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
116 for (int i = 0; i < nbNodes; i++)
118 pts[i] = nodes[i]->getVtkId();
120 SMDS_Mesh::_meshList[myMeshId]->setMyModified();
125 * Reorder in VTK order a list of nodes given in SMDS order.
126 * To be used before ChangeNodes: lists are given or computed in SMDS order.
128 bool SMDS_VtkVolume::vtkOrder(const SMDS_MeshNode* nodes[], const int nbNodes)
130 if (nbNodes != this->NbNodes())
132 MESSAGE("vtkOrder, wrong number of nodes " << nbNodes << " instead of "<< this->NbNodes());
135 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
136 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
137 const std::vector<int>& interlace = SMDS_MeshCell::toVtkOrder( VTKCellType( aVtkType ));
138 if ( !interlace.empty() )
140 ASSERT( (int)interlace.size() == nbNodes );
141 std::vector<const SMDS_MeshNode*> initNodes( nodes, nodes+nbNodes );
142 for ( size_t i = 0; i < interlace.size(); ++i )
143 nodes[i] = initNodes[ interlace[i] ];
148 SMDS_VtkVolume::~SMDS_VtkVolume()
152 void SMDS_VtkVolume::Print(ostream & OS) const
154 OS << "volume <" << GetID() << "> : ";
157 int SMDS_VtkVolume::NbFaces() const
159 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
160 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
165 case VTK_QUADRATIC_TETRA:
170 case VTK_QUADRATIC_PYRAMID:
171 case VTK_QUADRATIC_WEDGE:
175 case VTK_QUADRATIC_HEXAHEDRON:
176 case VTK_TRIQUADRATIC_HEXAHEDRON:
181 vtkIdType nFaces = 0;
182 vtkIdType* ptIds = 0;
183 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
187 case VTK_HEXAGONAL_PRISM:
191 MESSAGE("invalid volume type")
199 int SMDS_VtkVolume::NbNodes() const
201 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
202 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
203 vtkIdType nbPoints = 0;
204 if (aVtkType != VTK_POLYHEDRON)
207 grid->GetCellPoints( myVtkID, nbPoints, pts );
211 vtkIdType nFaces = 0;
212 vtkIdType* ptIds = 0;
213 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
215 for (int i = 0; i < nFaces; i++)
217 int nodesInFace = ptIds[id];
218 nbPoints += nodesInFace;
219 id += (nodesInFace + 1);
225 int SMDS_VtkVolume::NbEdges() const
227 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
228 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
233 case VTK_QUADRATIC_TETRA:
237 case VTK_QUADRATIC_PYRAMID:
241 case VTK_QUADRATIC_WEDGE:
245 case VTK_QUADRATIC_HEXAHEDRON:
246 case VTK_TRIQUADRATIC_HEXAHEDRON:
251 vtkIdType nFaces = 0;
252 vtkIdType* ptIds = 0;
253 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
256 for (int i = 0; i < nFaces; i++)
258 int edgesInFace = ptIds[id];
259 id += (edgesInFace + 1);
260 nbEdges += edgesInFace;
262 nbEdges = nbEdges / 2;
265 case VTK_HEXAGONAL_PRISM:
269 MESSAGE("invalid volume type")
278 * 1 <= face_ind <= NbFaces()
280 int SMDS_VtkVolume::NbFaceNodes(const int face_ind) const
282 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
283 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
285 if (aVtkType == VTK_POLYHEDRON)
287 vtkIdType nFaces = 0;
288 vtkIdType* ptIds = 0;
289 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
291 for (int i = 0; i < nFaces; i++)
293 int nodesInFace = ptIds[id];
294 id += (nodesInFace + 1);
295 if (i == face_ind - 1)
297 nbNodes = nodesInFace;
306 * 1 <= face_ind <= NbFaces()
307 * 1 <= node_ind <= NbFaceNodes()
309 const SMDS_MeshNode* SMDS_VtkVolume::GetFaceNode(const int face_ind, const int node_ind) const
311 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
312 vtkUnstructuredGrid* grid = mesh->getGrid();
313 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
314 const SMDS_MeshNode* node = 0;
315 if (aVtkType == VTK_POLYHEDRON)
317 vtkIdType nFaces = 0;
318 vtkIdType* ptIds = 0;
319 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
321 for (int i = 0; i < nFaces; i++)
323 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
324 if (i == face_ind - 1) // first face is number 1
326 if ((node_ind > 0) && (node_ind <= nodesInFace))
327 node = mesh->FindNodeVtk(ptIds[id + node_ind]); // ptIds[id+1] : first node
330 id += (nodesInFace + 1);
337 * return number of nodes for each face
339 std::vector<int> SMDS_VtkVolume::GetQuantities() const
341 std::vector<int> quantities;
342 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
343 vtkUnstructuredGrid* grid = mesh->getGrid();
344 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
345 if (aVtkType == VTK_POLYHEDRON)
347 vtkIdType nFaces = 0;
348 vtkIdType* ptIds = 0;
349 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
351 for (int i = 0; i < nFaces; i++)
353 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
354 quantities.push_back(nodesInFace);
355 id += (nodesInFace + 1);
361 SMDS_ElemIteratorPtr SMDS_VtkVolume::elementsIterator(SMDSAbs_ElementType type) const
367 SMDSAbs_EntityType aType = this->GetEntityType();
368 if (aType == SMDSEntity_Polyhedra)
369 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorPolyH(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
371 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
374 MESSAGE("ERROR : Iterator not implemented");
375 return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
379 SMDS_NodeIteratorPtr SMDS_VtkVolume::nodesIteratorToUNV() const
381 return SMDS_NodeIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
384 SMDS_NodeIteratorPtr SMDS_VtkVolume::interlacedNodesIterator() const
386 return SMDS_NodeIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
389 SMDSAbs_ElementType SMDS_VtkVolume::GetType() const
391 return SMDSAbs_Volume;
395 * \brief Return node by its index
396 * \param ind - node index
397 * \retval const SMDS_MeshNode* - the node
399 const SMDS_MeshNode* SMDS_VtkVolume::GetNode(const int ind) const
401 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
402 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
403 if ( aVtkType == VTK_POLYHEDRON)
405 vtkIdType nFaces = 0;
406 vtkIdType* ptIds = 0;
407 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
408 int id = 0, nbPoints = 0;
409 for (int i = 0; i < nFaces; i++)
411 int nodesInFace = ptIds[id];
412 if ( ind < nbPoints + nodesInFace )
413 return SMDS_Mesh::_meshList[myMeshId]->FindNodeVtk( ptIds[ ind + i ]);
414 nbPoints += nodesInFace;
415 id += (nodesInFace + 1);
419 vtkIdType npts, *pts;
420 grid->GetCellPoints( this->myVtkID, npts, pts );
421 const std::vector<int>& interlace = SMDS_MeshCell::fromVtkOrder( VTKCellType( aVtkType ));
422 return SMDS_Mesh::_meshList[myMeshId]->FindNodeVtk( pts[ interlace.empty() ? ind : interlace[ind]] );
425 * \brief Check if a node belongs to the element
426 * \param node - the node to check
427 * \retval int - node index within the element, -1 if not found
429 int SMDS_VtkVolume::GetNodeIndex( const SMDS_MeshNode* node ) const
431 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
432 const vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
433 if ( aVtkType == VTK_POLYHEDRON)
435 vtkIdType nFaces = 0;
436 vtkIdType* ptIds = 0;
437 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
439 for (int iF = 0; iF < nFaces; iF++)
441 int nodesInFace = ptIds[id];
442 for ( vtkIdType i = 0; i < nodesInFace; ++i )
443 if ( ptIds[id+i+1] == node->getVtkId() )
445 id += (nodesInFace + 1);
449 vtkIdType npts, *pts;
450 grid->GetCellPoints( this->myVtkID, npts, pts );
451 for ( vtkIdType i = 0; i < npts; ++i )
452 if ( pts[i] == node->getVtkId() )
454 const std::vector<int>& interlace = SMDS_MeshCell::toVtkOrder( VTKCellType( aVtkType ));
455 return interlace.empty() ? i : interlace[i];
460 bool SMDS_VtkVolume::IsQuadratic() const
462 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
463 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
464 // TODO quadratic polyhedrons ?
467 case VTK_QUADRATIC_TETRA:
468 case VTK_QUADRATIC_PYRAMID:
469 case VTK_QUADRATIC_WEDGE:
470 case VTK_QUADRATIC_HEXAHEDRON:
471 case VTK_TRIQUADRATIC_HEXAHEDRON:
479 bool SMDS_VtkVolume::IsPoly() const
481 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
482 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
483 return (aVtkType == VTK_POLYHEDRON);
486 bool SMDS_VtkVolume::IsMediumNode(const SMDS_MeshNode* node) const
488 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
489 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
490 int rankFirstMedium = 0;
493 case VTK_QUADRATIC_TETRA:
494 rankFirstMedium = 4; // medium nodes are of rank 4 to 9
496 case VTK_QUADRATIC_PYRAMID:
497 rankFirstMedium = 5; // medium nodes are of rank 5 to 12
499 case VTK_QUADRATIC_WEDGE:
500 rankFirstMedium = 6; // medium nodes are of rank 6 to 14
502 case VTK_QUADRATIC_HEXAHEDRON:
503 case VTK_TRIQUADRATIC_HEXAHEDRON:
504 rankFirstMedium = 8; // medium nodes are of rank 8 to 19
511 grid->GetCellPoints(myVtkID, npts, pts);
512 vtkIdType nodeId = node->getVtkId();
513 for (int rank = 0; rank < npts; rank++)
515 if (pts[rank] == nodeId)
517 if (rank < rankFirstMedium)
523 MESSAGE("======================================================");
524 MESSAGE("= IsMediumNode: node does not belong to this element =");
525 MESSAGE("======================================================");
529 int SMDS_VtkVolume::NbCornerNodes() const
531 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
532 vtkIdType aVtkType = grid->GetCellType(myVtkID);
535 case VTK_QUADRATIC_TETRA: return 4;
536 case VTK_QUADRATIC_PYRAMID: return 5;
537 case VTK_QUADRATIC_WEDGE: return 6;
538 case VTK_QUADRATIC_HEXAHEDRON:
539 case VTK_TRIQUADRATIC_HEXAHEDRON: return 8;
545 SMDSAbs_EntityType SMDS_VtkVolume::GetEntityType() const
547 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
548 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
550 SMDSAbs_EntityType aType = SMDSEntity_Tetra;
554 aType = SMDSEntity_Tetra;
557 aType = SMDSEntity_Pyramid;
560 aType = SMDSEntity_Penta;
563 aType = SMDSEntity_Hexa;
565 case VTK_QUADRATIC_TETRA:
566 aType = SMDSEntity_Quad_Tetra;
568 case VTK_QUADRATIC_PYRAMID:
569 aType = SMDSEntity_Quad_Pyramid;
571 case VTK_QUADRATIC_WEDGE:
572 aType = SMDSEntity_Quad_Penta;
574 case VTK_QUADRATIC_HEXAHEDRON:
575 aType = SMDSEntity_Quad_Hexa;
577 case VTK_TRIQUADRATIC_HEXAHEDRON:
578 aType = SMDSEntity_TriQuad_Hexa;
580 case VTK_HEXAGONAL_PRISM:
581 aType = SMDSEntity_Hexagonal_Prism;
584 aType = SMDSEntity_Polyhedra;
587 aType = SMDSEntity_Polyhedra;
593 SMDSAbs_GeometryType SMDS_VtkVolume::GetGeomType() const
595 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
596 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
598 SMDSAbs_GeometryType aType = SMDSGeom_NONE;
602 case VTK_QUADRATIC_TETRA:
603 aType = SMDSGeom_TETRA;
606 case VTK_QUADRATIC_PYRAMID:
607 aType = SMDSGeom_PYRAMID;
610 case VTK_QUADRATIC_WEDGE:
611 aType = SMDSGeom_PENTA;
614 case VTK_QUADRATIC_HEXAHEDRON:
615 case VTK_TRIQUADRATIC_HEXAHEDRON:
616 aType = SMDSGeom_HEXA;
618 case VTK_HEXAGONAL_PRISM:
619 aType = SMDSGeom_HEXAGONAL_PRISM;
622 aType = SMDSGeom_POLYHEDRA;
625 aType = SMDSGeom_POLYHEDRA;
631 vtkIdType SMDS_VtkVolume::GetVtkType() const
633 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
634 vtkIdType aType = grid->GetCellType(myVtkID);
638 void SMDS_VtkVolume::gravityCenter(SMDS_UnstructuredGrid* grid,
639 const vtkIdType * nodeIds,
643 for (int j = 0; j < 3; j++)
647 for (int i = 0; i < nbNodes; i++)
649 double *coords = grid->GetPoint(nodeIds[i]);
650 for (int j = 0; j < 3; j++)
651 result[j] += coords[j];
653 for (int j = 0; j < 3; j++)
654 result[j] = result[j] / nbNodes;
658 bool SMDS_VtkVolume::isForward(double* a, double* b, double* c, double* d)
660 double u[3], v[3], w[3];
661 for (int j = 0; j < 3; j++)
667 double prodmixte = ((u[1]*v[2] - u[2]*v[1]) * w[0]
668 + (u[2]*v[0] - u[0]*v[2]) * w[1]
669 + (u[0]*v[1] - u[1]*v[0]) * w[2] );
670 return (prodmixte < 0);
673 /*! For polyhedron only
674 * @return actual number of nodes (not the sum of nodes of all faces)
676 int SMDS_VtkVolume::NbUniqueNodes() const
678 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
679 return grid->GetCell(myVtkID)->GetNumberOfPoints();
682 /*! For polyhedron use only
683 * @return iterator on actual nodes (not through the faces)
685 SMDS_ElemIteratorPtr SMDS_VtkVolume::uniqueNodesIterator() const
687 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));