1 // Copyright (C) 2010-2011 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.
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(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(std::vector<vtkIdType> nodeIds, SMDS_Mesh* mesh)
43 SMDS_MeshVolume::init();
44 vtkUnstructuredGrid* grid = mesh->getGrid();
45 myMeshId = mesh->getMeshId();
46 vtkIdType aType = VTK_TETRA;
47 switch (nodeIds.size())
59 aType = VTK_HEXAHEDRON;
62 aType = VTK_QUADRATIC_TETRA;
65 aType = VTK_QUADRATIC_PYRAMID;
68 aType = VTK_QUADRATIC_WEDGE;
71 aType = VTK_QUADRATIC_HEXAHEDRON;
74 aType = VTK_HEXAHEDRON;
77 myVtkID = grid->InsertNextLinkedCell(aType, nodeIds.size(), &nodeIds[0]);
78 mesh->setMyModified();
79 //MESSAGE("SMDS_VtkVolume::init myVtkID " << myVtkID);
82 //#ifdef VTK_HAVE_POLYHEDRON
83 void SMDS_VtkVolume::initPoly(std::vector<vtkIdType> nodeIds, std::vector<int> nbNodesPerFace, SMDS_Mesh* mesh)
85 SMDS_MeshVolume::init();
86 //MESSAGE("SMDS_VtkVolume::initPoly");
87 SMDS_UnstructuredGrid* grid = mesh->getGrid();
89 this->gravityCenter(grid, &nodeIds[0], nodeIds.size(), ¢er[0]);
90 vector<vtkIdType> ptIds;
92 vtkIdType nbFaces = nbNodesPerFace.size();
94 for (int i = 0; i < nbFaces; i++)
96 int nf = nbNodesPerFace[i];
98 // EAP: a right approach is:
99 // - either the user should care of order of nodes or
100 // - the user should use a service method arranging nodes if he
101 // don't want or can't to do it by him-self
102 // The method below works OK only with planar faces
107 // grid->GetPoints()->GetPoint(nodeIds[k], a);
108 // grid->GetPoints()->GetPoint(nodeIds[k + 1], b);
109 // grid->GetPoints()->GetPoint(nodeIds[k + 2], c);
110 // bool isFaceForward = this->isForward(a, b, c, center);
111 //MESSAGE("isFaceForward " << i << " " << isFaceForward);
112 vtkIdType *facePts = &nodeIds[k];
114 for (int n = 0; n < nf; n++)
115 ptIds.push_back(facePts[n]);
117 // for (int n = nf - 1; n >= 0; n--)
118 // ptIds.push_back(facePts[n]);
121 myVtkID = grid->InsertNextLinkedCell(VTK_POLYHEDRON, nbFaces, &ptIds[0]);
122 mesh->setMyModified();
126 bool SMDS_VtkVolume::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
128 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
131 grid->GetCellPoints(myVtkID, npts, pts);
134 MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
137 for (int i = 0; i < nbNodes; i++)
139 pts[i] = nodes[i]->getVtkId();
141 SMDS_Mesh::_meshList[myMeshId]->setMyModified();
146 * Reorder in VTK order a list of nodes given in SMDS order.
147 * To be used before ChangeNodes: lists are given or computed in SMDS order.
149 bool SMDS_VtkVolume::vtkOrder(const SMDS_MeshNode* nodes[], const int nbNodes)
151 if (nbNodes != this->NbNodes())
153 MESSAGE("vtkOrder, wrong number of nodes " << nbNodes << " instead of "<< this->NbNodes());
156 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
157 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
161 this->exchange(nodes, 1, 2);
163 case VTK_QUADRATIC_TETRA:
164 this->exchange(nodes, 1, 2);
165 this->exchange(nodes, 4, 6);
166 this->exchange(nodes, 8, 9);
169 this->exchange(nodes, 1, 3);
173 case VTK_QUADRATIC_PYRAMID:
174 this->exchange(nodes, 1, 3);
175 this->exchange(nodes, 5, 8);
176 this->exchange(nodes, 6, 7);
177 this->exchange(nodes, 10, 12);
179 case VTK_QUADRATIC_WEDGE:
182 this->exchange(nodes, 1, 3);
183 this->exchange(nodes, 5, 7);
185 case VTK_QUADRATIC_HEXAHEDRON:
186 this->exchange(nodes, 1, 3);
187 this->exchange(nodes, 5, 7);
188 this->exchange(nodes, 8, 11);
189 this->exchange(nodes, 9, 10);
190 this->exchange(nodes, 12, 15);
191 this->exchange(nodes, 13, 14);
192 this->exchange(nodes, 17, 19);
201 SMDS_VtkVolume::~SMDS_VtkVolume()
205 void SMDS_VtkVolume::Print(ostream & OS) const
207 OS << "volume <" << GetID() << "> : ";
210 int SMDS_VtkVolume::NbFaces() const
212 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
213 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
218 case VTK_QUADRATIC_TETRA:
223 case VTK_QUADRATIC_PYRAMID:
224 case VTK_QUADRATIC_WEDGE:
228 case VTK_QUADRATIC_HEXAHEDRON:
233 vtkIdType nFaces = 0;
234 vtkIdType* ptIds = 0;
235 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
240 MESSAGE("invalid volume type")
248 int SMDS_VtkVolume::NbNodes() const
250 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
251 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
253 if (aVtkType != VTK_POLYHEDRON)
255 nbPoints = grid->GetCell(myVtkID)->GetNumberOfPoints();
259 vtkIdType nFaces = 0;
260 vtkIdType* ptIds = 0;
261 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
263 for (int i = 0; i < nFaces; i++)
265 int nodesInFace = ptIds[id];
266 nbPoints += nodesInFace;
267 id += (nodesInFace + 1);
273 int SMDS_VtkVolume::NbEdges() const
275 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
276 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
281 case VTK_QUADRATIC_TETRA:
285 case VTK_QUADRATIC_PYRAMID:
289 case VTK_QUADRATIC_WEDGE:
293 case VTK_QUADRATIC_HEXAHEDRON:
298 vtkIdType nFaces = 0;
299 vtkIdType* ptIds = 0;
300 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
303 for (int i = 0; i < nFaces; i++)
305 int edgesInFace = ptIds[id];
306 id += (edgesInFace + 1);
307 nbEdges += edgesInFace;
309 nbEdges = nbEdges / 2;
313 MESSAGE("invalid volume type")
322 * 1 <= face_ind <= NbFaces()
324 int SMDS_VtkVolume::NbFaceNodes(const int face_ind) const
326 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
327 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
329 if (aVtkType == VTK_POLYHEDRON)
331 vtkIdType nFaces = 0;
332 vtkIdType* ptIds = 0;
333 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
335 for (int i = 0; i < nFaces; i++)
337 int nodesInFace = ptIds[id];
338 id += (nodesInFace + 1);
339 if (i == face_ind - 1)
341 nbNodes = nodesInFace;
350 * 1 <= face_ind <= NbFaces()
351 * 1 <= node_ind <= NbFaceNodes()
353 const SMDS_MeshNode* SMDS_VtkVolume::GetFaceNode(const int face_ind, const int node_ind) const
355 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
356 vtkUnstructuredGrid* grid = mesh->getGrid();
357 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
358 const SMDS_MeshNode* node = 0;
359 if (aVtkType == VTK_POLYHEDRON)
361 vtkIdType nFaces = 0;
362 vtkIdType* ptIds = 0;
363 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
365 for (int i = 0; i < nFaces; i++)
367 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
368 if (i == face_ind - 1) // first face is number 1
370 if ((node_ind > 0) && (node_ind <= nodesInFace))
371 node = mesh->FindNodeVtk(ptIds[id + node_ind]); // ptIds[id+1] : first node
374 id += (nodesInFace + 1);
381 * return number of nodes for each face
383 const std::vector<int> SMDS_VtkVolume::GetQuantities() const
385 vector<int> quantities;
387 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
388 vtkUnstructuredGrid* grid = mesh->getGrid();
389 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
390 if (aVtkType == VTK_POLYHEDRON)
392 vtkIdType nFaces = 0;
393 vtkIdType* ptIds = 0;
394 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
396 for (int i = 0; i < nFaces; i++)
398 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
399 quantities.push_back(nodesInFace);
400 id += (nodesInFace + 1);
406 SMDS_ElemIteratorPtr SMDS_VtkVolume::elementsIterator(SMDSAbs_ElementType type) const
412 SMDSAbs_EntityType aType = this->GetEntityType();
413 if (aType == SMDSEntity_Polyhedra)
414 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorPolyH(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
416 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
419 MESSAGE("ERROR : Iterator not implemented");
420 return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
424 SMDS_ElemIteratorPtr SMDS_VtkVolume::nodesIteratorToUNV() const
426 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
429 SMDS_ElemIteratorPtr SMDS_VtkVolume::interlacedNodesElemIterator() const
431 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
434 SMDSAbs_ElementType SMDS_VtkVolume::GetType() const
436 return SMDSAbs_Volume;
440 * \brief Return node by its index
441 * \param ind - node index
442 * \retval const SMDS_MeshNode* - the node
444 const SMDS_MeshNode* SMDS_VtkVolume::GetNode(const int ind) const
446 // TODO optimize if possible (vtkCellIterator)
447 return SMDS_MeshElement::GetNode(ind);
450 bool SMDS_VtkVolume::IsQuadratic() const
452 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
453 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
454 // TODO quadratic polyhedrons ?
457 case VTK_QUADRATIC_TETRA:
458 case VTK_QUADRATIC_PYRAMID:
459 case VTK_QUADRATIC_WEDGE:
460 case VTK_QUADRATIC_HEXAHEDRON:
468 bool SMDS_VtkVolume::IsPoly() const
470 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
471 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
472 return (aVtkType == VTK_POLYHEDRON);
475 bool SMDS_VtkVolume::IsMediumNode(const SMDS_MeshNode* node) const
477 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
478 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
479 int rankFirstMedium = 0;
482 case VTK_QUADRATIC_TETRA:
483 rankFirstMedium = 4; // medium nodes are of rank 4 to 9
485 case VTK_QUADRATIC_PYRAMID:
486 rankFirstMedium = 5; // medium nodes are of rank 5 to 12
488 case VTK_QUADRATIC_WEDGE:
489 rankFirstMedium = 6; // medium nodes are of rank 6 to 14
491 case VTK_QUADRATIC_HEXAHEDRON:
492 rankFirstMedium = 8; // medium nodes are of rank 8 to 19
499 grid->GetCellPoints(myVtkID, npts, pts);
500 vtkIdType nodeId = node->getVtkId();
501 for (int rank = 0; rank < npts; rank++)
503 if (pts[rank] == nodeId)
505 if (rank < rankFirstMedium)
511 //throw SALOME_Exception(LOCALIZED("node does not belong to this element"));
512 MESSAGE("======================================================");
513 MESSAGE("= IsMediumNode: node does not belong to this element =");
514 MESSAGE("======================================================");
518 SMDSAbs_EntityType SMDS_VtkVolume::GetEntityType() const
520 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
521 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
523 SMDSAbs_EntityType aType = SMDSEntity_Tetra;
527 aType = SMDSEntity_Tetra;
530 aType = SMDSEntity_Pyramid;
533 aType = SMDSEntity_Penta;
536 aType = SMDSEntity_Hexa;
538 case VTK_QUADRATIC_TETRA:
539 aType = SMDSEntity_Quad_Tetra;
541 case VTK_QUADRATIC_PYRAMID:
542 aType = SMDSEntity_Quad_Pyramid;
544 case VTK_QUADRATIC_WEDGE:
545 aType = SMDSEntity_Quad_Penta;
547 case VTK_QUADRATIC_HEXAHEDRON:
548 aType = SMDSEntity_Quad_Hexa;
550 //#ifdef VTK_HAVE_POLYHEDRON
552 aType = SMDSEntity_Polyhedra;
556 aType = SMDSEntity_Polyhedra;
562 vtkIdType SMDS_VtkVolume::GetVtkType() const
564 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
565 vtkIdType aType = grid->GetCellType(myVtkID);
569 void SMDS_VtkVolume::gravityCenter(SMDS_UnstructuredGrid* grid, vtkIdType *nodeIds, int nbNodes, double* result)
571 for (int j = 0; j < 3; j++)
575 for (int i = 0; i < nbNodes; i++)
577 double *coords = grid->GetPoint(nodeIds[i]);
578 for (int j = 0; j < 3; j++)
579 result[j] += coords[j];
581 for (int j = 0; j < 3; j++)
582 result[j] = result[j] / nbNodes;
583 //MESSAGE("center " << result[0] << " " << result[1] << " " << result[2]);
587 bool SMDS_VtkVolume::isForward(double* a, double* b, double* c, double* d)
589 double u[3], v[3], w[3];
590 for (int j = 0; j < 3; j++)
592 //MESSAGE("a,b,c,d " << a[j] << " " << b[j] << " " << c[j] << " " << d[j]);
596 //MESSAGE("u,v,w " << u[j] << " " << v[j] << " " << w[j]);
598 double prodmixte = (u[1]*v[2] - u[2]*v[1]) * w[0]
599 + (u[2]*v[0] - u[0]*v[2]) * w[1]
600 + (u[0]*v[1] - u[1]*v[0]) * w[2];
601 return (prodmixte < 0);
604 /*! For polyhedron only
605 * @return actual number of nodes (not the sum of nodes of all faces)
607 int SMDS_VtkVolume::NbUniqueNodes() const
609 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
610 return grid->GetCell(myVtkID)->GetNumberOfPoints();
613 /*! For polyhedron use only
614 * @return iterator on actual nodes (not through the faces)
616 SMDS_ElemIteratorPtr SMDS_VtkVolume::uniqueNodesIterator() const
618 MESSAGE("uniqueNodesIterator");
619 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));