7 #include "fwVtkIO/helper/Mesh.hpp" 9 #include <fwData/Array.hpp> 11 #include <fwDataTools/helper/Array.hpp> 12 #include <fwDataTools/helper/Mesh.hpp> 13 #include <fwDataTools/helper/MeshGetter.hpp> 16 #include <vtkCellData.h> 17 #include <vtkDataArray.h> 18 #include <vtkExtractUnstructuredGrid.h> 19 #include <vtkFillHolesFilter.h> 20 #include <vtkFloatArray.h> 21 #include <vtkGeometryFilter.h> 22 #include <vtkMassProperties.h> 23 #include <vtkPointData.h> 24 #include <vtkPoints.h> 25 #include <vtkPolyDataNormals.h> 26 #include <vtkSmartPointer.h> 37 vtkPoints* points = polyData->GetPoints();
41 const vtkIdType numberOfPoints = points->GetNumberOfPoints();
42 const vtkIdType numberOfCells = polyData->GetNumberOfCells();
44 mesh->allocate(numberOfPoints, numberOfCells, numberOfCells*3);
48 ::fwData::Mesh::Id idx;
49 for (vtkIdType i = 0; i < numberOfPoints; ++i)
51 point = points->GetPoint(i);
53 SLM_ASSERT(
"Mesh index not correspond to VTK index point", idx == i);
59 for (vtkIdType i = 0; i < numberOfCells; ++i)
61 cell = polyData->GetCell(i);
62 idList = cell->GetPointIds();
63 cellType = cell->GetCellType();
68 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 1);
72 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 2);
76 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 3);
77 meshHelper.
insertNextCell( idList->GetId(0), idList->GetId(1), idList->GetId(2));
80 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 4);
81 meshHelper.
insertNextCell( idList->GetId(0), idList->GetId(1), idList->GetId(2), idList->GetId(3));
84 for(vtkIdType i = 0; i < idList->GetNumberOfIds(); ++i)
90 FW_RAISE(
"VTK Mesh type "<<cellType<<
" not supported.");
94 if(polyData->GetPointData()->HasArray(
"Colors"))
96 vtkSmartPointer<vtkUnsignedCharArray> colors;
97 colors = vtkUnsignedCharArray::SafeDownCast(polyData->GetPointData()->GetArray(
"Colors"));
98 FW_RAISE_IF(
"Only vtkUnsignedCharArray is supported to manage color.", !colors);
100 const size_t nbComponents = colors->GetNumberOfComponents();
101 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")",
102 nbComponents == 3 || nbComponents == 4);
103 mesh->allocatePointColors((::fwData::Mesh::ColorArrayTypes)nbComponents);
106 ::fwData::Mesh::Id nbPoints = mesh->getNumberOfPoints();
107 for (
size_t i = 0; i != nbPoints; ++i)
109 meshHelper.
setPointColor(i, colors->GetPointer(i*nbComponents));
113 if(polyData->GetCellData()->HasArray(
"Colors"))
115 vtkSmartPointer<vtkUnsignedCharArray> colors;
116 colors = vtkUnsignedCharArray::SafeDownCast(polyData->GetCellData()->GetArray(
"Colors"));
117 FW_RAISE_IF(
"Only vtkUnsignedCharArray is supported to manage color.", !colors);
119 size_t nbComponents = colors->GetNumberOfComponents();
120 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")",
121 nbComponents == 3 || nbComponents == 4);
122 mesh->allocateCellColors((::fwData::Mesh::ColorArrayTypes)nbComponents);
125 const ::fwData::Mesh::Id nbCells = mesh->getNumberOfCells();
126 for (
size_t i = 0; i != nbCells; ++i)
128 meshHelper.
setCellColor(i, colors->GetPointer(i*nbComponents));
132 if(polyData->GetPointData()->GetAttribute(vtkDataSetAttributes::NORMALS))
134 vtkSmartPointer<vtkFloatArray> normals;
135 normals = vtkFloatArray::SafeDownCast(polyData->GetPointData()->GetNormals());
136 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage normals.", !normals);
138 size_t nbComponents = normals->GetNumberOfComponents();
139 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 3);
141 mesh->allocatePointNormals();
144 const ::fwData::Mesh::Id nbPoints = mesh->getNumberOfPoints();
145 for (
size_t i = 0; i != nbPoints; ++i)
147 meshHelper.
setPointNormal(i, normals->GetPointer(i*nbComponents));
151 if(polyData->GetCellData()->GetAttribute(vtkDataSetAttributes::NORMALS))
153 vtkSmartPointer<vtkFloatArray> normals;
154 normals = vtkFloatArray::SafeDownCast(polyData->GetCellData()->GetNormals());
155 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage normals.", !normals);
157 size_t nbComponents = normals->GetNumberOfComponents();
158 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 3);
160 mesh->allocateCellNormals();
163 const ::fwData::Mesh::Id nbCells = mesh->getNumberOfCells();
164 for (
size_t i = 0; i != nbCells; ++i)
166 meshHelper.
setCellNormal(i, normals->GetPointer(i*nbComponents));
170 if(polyData->GetPointData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
172 vtkSmartPointer<vtkFloatArray> texCoords;
173 texCoords = vtkFloatArray::SafeDownCast(polyData->GetPointData()->GetTCoords());
174 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage texCoords.", !texCoords);
176 size_t nbComponents = texCoords->GetNumberOfComponents();
177 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 2);
179 mesh->allocatePointTexCoords();
182 const ::fwData::Mesh::Id nbPoints = mesh->getNumberOfPoints();
183 for (
size_t i = 0; i != nbPoints; ++i)
189 if(polyData->GetCellData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
191 vtkSmartPointer<vtkFloatArray> texCoords;
192 texCoords = vtkFloatArray::SafeDownCast(polyData->GetCellData()->GetTCoords());
193 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage texCoords.", !texCoords);
195 size_t nbComponents = texCoords->GetNumberOfComponents();
196 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 2);
198 mesh->allocateCellTexCoords();
201 const ::fwData::Mesh::Id nbCells = mesh->getNumberOfCells();
202 for (
size_t i = 0; i != nbCells; ++i)
208 mesh->adjustAllocatedMemory();
216 vtkPoints* points = grid->GetPoints();
220 const vtkIdType numberOfPoints = points->GetNumberOfPoints();
221 const vtkIdType numberOfCells = grid->GetNumberOfCells();
223 mesh->allocate(numberOfPoints, numberOfCells, numberOfCells*3);
226 ::fwData::Mesh::Id idx;
227 for (vtkIdType i = 0; i < numberOfPoints; ++i)
229 point = points->GetPoint(i);
231 SLM_ASSERT(
"Mesh index not correspond to VTK index point", idx == i);
237 for (vtkIdType i = 0; i < numberOfCells; ++i)
239 cell = grid->GetCell(i);
240 idList = cell->GetPointIds();
241 cellType = cell->GetCellType();
246 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 1);
250 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 2);
254 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 3);
255 meshHelper.
insertNextCell( idList->GetId(0), idList->GetId(1), idList->GetId(2));
258 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 4);
259 meshHelper.
insertNextCell( idList->GetId(0), idList->GetId(1), idList->GetId(2), idList->GetId(3));
262 SLM_ASSERT(
"Wrong number of ids: "<<idList->GetNumberOfIds(), idList->GetNumberOfIds() == 4);
263 meshHelper.
insertNextCell( idList->GetId(0), idList->GetId(1), idList->GetId(2), idList->GetId(3),
264 ::fwData::Mesh::TETRA);
267 FW_RAISE(
"VTK Mesh type "<<cellType<<
" not supported.");
271 if(grid->GetPointData()->HasArray(
"Colors"))
273 vtkSmartPointer<vtkUnsignedCharArray> colors;
274 colors = vtkUnsignedCharArray::SafeDownCast(grid->GetPointData()->GetArray(
"Colors"));
275 FW_RAISE_IF(
"Only vtkUnsignedCharArray is supported to manage color.", !colors);
277 size_t nbComponents = colors->GetNumberOfComponents();
278 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")",
279 nbComponents == 3 || nbComponents == 4);
280 mesh->allocatePointColors((::fwData::Mesh::ColorArrayTypes)nbComponents);
283 const ::fwData::Mesh::Id nbPoints = mesh->getNumberOfPoints();
284 for (
size_t i = 0; i != nbPoints; ++i)
286 meshHelper.
setPointColor(i, colors->GetPointer(i*nbComponents));
290 if(grid->GetCellData()->HasArray(
"Colors"))
292 vtkSmartPointer<vtkUnsignedCharArray> colors;
293 colors = vtkUnsignedCharArray::SafeDownCast(grid->GetCellData()->GetArray(
"Colors"));
294 FW_RAISE_IF(
"Only vtkUnsignedCharArray is supported to manage color.", !colors);
296 size_t nbComponents = colors->GetNumberOfComponents();
297 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")",
298 nbComponents == 3 || nbComponents == 4);
299 mesh->allocateCellColors((::fwData::Mesh::ColorArrayTypes)nbComponents);
302 const ::fwData::Mesh::Id nbCells = mesh->getNumberOfCells();
303 for (
size_t i = 0; i != nbCells; ++i)
305 meshHelper.
setCellColor(i, colors->GetPointer(i*nbComponents));
309 if(grid->GetPointData()->GetAttribute(vtkDataSetAttributes::NORMALS))
311 vtkSmartPointer<vtkFloatArray> normals;
312 normals = vtkFloatArray::SafeDownCast(grid->GetPointData()->GetNormals());
313 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage normals.", !normals);
315 size_t nbComponents = normals->GetNumberOfComponents();
316 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 3);
318 mesh->allocatePointNormals();
321 const ::fwData::Mesh::Id nbPoints = mesh->getNumberOfPoints();
322 for (
size_t i = 0; i != nbPoints; ++i)
324 meshHelper.
setPointNormal(i, normals->GetPointer(i*nbComponents));
328 if(grid->GetCellData()->GetAttribute(vtkDataSetAttributes::NORMALS))
330 vtkSmartPointer<vtkFloatArray> normals;
331 normals = vtkFloatArray::SafeDownCast(grid->GetCellData()->GetNormals());
332 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage normals.", !normals);
334 size_t nbComponents = normals->GetNumberOfComponents();
335 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 3);
337 mesh->allocateCellNormals();
340 const ::fwData::Mesh::Id nbCells = mesh->getNumberOfCells();
341 for (
size_t i = 0; i != nbCells; ++i)
343 meshHelper.
setCellNormal(i, normals->GetPointer(i*nbComponents));
347 if(grid->GetPointData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
349 vtkSmartPointer<vtkFloatArray> texCoords;
350 texCoords = vtkFloatArray::SafeDownCast(grid->GetPointData()->GetTCoords());
351 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage texCoords.", !texCoords);
353 size_t nbComponents = texCoords->GetNumberOfComponents();
354 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 2);
356 mesh->allocatePointTexCoords();
359 const ::fwData::Mesh::Id nbPoints = mesh->getNumberOfPoints();
360 for (
size_t i = 0; i != nbPoints; ++i)
366 if(grid->GetCellData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
368 vtkSmartPointer<vtkFloatArray> texCoords;
369 texCoords = vtkFloatArray::SafeDownCast(grid->GetCellData()->GetTCoords());
370 FW_RAISE_IF(
"Only vtkFloatArray is supported to manage texCoords.", !texCoords);
372 size_t nbComponents = texCoords->GetNumberOfComponents();
373 SLM_ASSERT(
"Wrong nb of components ("<<nbComponents<<
")", nbComponents == 2);
375 mesh->allocateCellTexCoords();
378 const ::fwData::Mesh::Id nbCells = mesh->getNumberOfCells();
379 for (
size_t i = 0; i != nbCells; ++i)
385 mesh->adjustAllocatedMemory();
393 void Mesh::toVTKMesh( const ::fwData::Mesh::csptr& mesh, vtkSmartPointer<vtkPolyData> polyData)
395 const vtkSmartPointer< vtkPoints > pts = vtkSmartPointer< vtkPoints >::New();
396 polyData->SetPoints(pts);
400 const unsigned int nbCells = mesh->getNumberOfCells();
402 const ::fwData::Mesh::ConstCellTypesMultiArrayType cellTypes = meshHelper.
getCellTypes();
403 const ::fwData::Mesh::ConstCellDataMultiArrayType cellData = meshHelper.
getCellData();
404 const ::fwData::Mesh::ConstCellDataOffsetsMultiArrayType cellDataOffsets = meshHelper.
getCellDataOffsets();
406 polyData->Allocate(static_cast< int >(nbCells));
408 vtkIdType typeVtkCell;
410 for(
unsigned int i = 0; i < nbCells; ++i )
412 const ::fwData::Mesh::CellTypes cellType = cellTypes[i];
413 const ::fwData::Mesh::Id offset = cellDataOffsets[i];
416 case ::fwData::Mesh::POINT:
417 typeVtkCell = VTK_VERTEX;
418 cell[0] = cellData[offset];
419 polyData->InsertNextCell( typeVtkCell, 1, cell );
421 case ::fwData::Mesh::EDGE:
422 typeVtkCell = VTK_LINE;
423 cell[0] = cellData[offset];
424 cell[1] = cellData[offset+1];
425 polyData->InsertNextCell( typeVtkCell, 2, cell );
427 case ::fwData::Mesh::TRIANGLE:
428 typeVtkCell = VTK_TRIANGLE;
429 cell[0] = cellData[offset];
430 cell[1] = cellData[offset+1];
431 cell[2] = cellData[offset+2];
432 polyData->InsertNextCell( typeVtkCell, 3, cell );
434 case ::fwData::Mesh::QUAD:
435 typeVtkCell = VTK_QUAD;
436 cell[0] = cellData[offset];
437 cell[1] = cellData[offset+1];
438 cell[2] = cellData[offset+2];
439 cell[3] = cellData[offset+3];
440 polyData->InsertNextCell( typeVtkCell, 4, cell );
442 case ::fwData::Mesh::TETRA:
443 typeVtkCell = VTK_LINE;
445 cell[0] = cellData[offset+1];
446 cell[1] = cellData[offset+2];
447 polyData->InsertNextCell( typeVtkCell, 2, cell );
449 cell[0] = cellData[offset+2];
450 cell[1] = cellData[offset+3];
451 polyData->InsertNextCell( typeVtkCell, 2, cell );
453 cell[0] = cellData[offset+3];
454 cell[1] = cellData[offset];
455 polyData->InsertNextCell( typeVtkCell, 2, cell );
457 cell[0] = cellData[offset+2];
458 cell[1] = cellData[offset];
459 polyData->InsertNextCell( typeVtkCell, 2, cell );
461 cell[0] = cellData[offset+1];
462 cell[1] = cellData[offset+3];
463 polyData->InsertNextCell( typeVtkCell, 2, cell );
480 void Mesh::toVTKGrid( const ::fwData::Mesh::csptr& mesh, vtkSmartPointer<vtkUnstructuredGrid> grid)
482 const vtkSmartPointer< vtkPoints > pts = vtkSmartPointer< vtkPoints >::New();
483 grid->SetPoints(pts);
487 const unsigned int nbCells = mesh->getNumberOfCells();
489 const ::fwData::Mesh::ConstCellTypesMultiArrayType cellTypes = meshHelper.
getCellTypes();
490 const ::fwData::Mesh::ConstCellDataMultiArrayType cellData = meshHelper.
getCellData();
491 const ::fwData::Mesh::ConstCellDataOffsetsMultiArrayType cellDataOffsets = meshHelper.
getCellDataOffsets();
493 grid->Allocate(static_cast<int>(nbCells));
495 vtkIdType typeVtkCell;
497 for(
unsigned int i = 0; i < nbCells; ++i )
499 const ::fwData::Mesh::CellTypes cellType = cellTypes[i];
500 const ::fwData::Mesh::Id offset = cellDataOffsets[i];
503 case ::fwData::Mesh::POINT:
504 typeVtkCell = VTK_VERTEX;
505 cell[0] = cellData[offset];
506 grid->InsertNextCell( typeVtkCell, 1, cell );
508 case ::fwData::Mesh::EDGE:
509 typeVtkCell = VTK_LINE;
510 cell[0] = cellData[offset];
511 cell[1] = cellData[offset+1];
512 grid->InsertNextCell( typeVtkCell, 2, cell );
514 case ::fwData::Mesh::TRIANGLE:
515 typeVtkCell = VTK_TRIANGLE;
516 cell[0] = cellData[offset];
517 cell[1] = cellData[offset+1];
518 cell[2] = cellData[offset+2];
519 grid->InsertNextCell( typeVtkCell, 3, cell );
521 case ::fwData::Mesh::QUAD:
522 typeVtkCell = VTK_QUAD;
523 cell[0] = cellData[offset];
524 cell[1] = cellData[offset+1];
525 cell[2] = cellData[offset+2];
526 cell[3] = cellData[offset+3];
527 grid->InsertNextCell( typeVtkCell, 4, cell );
529 case ::fwData::Mesh::TETRA:
530 typeVtkCell = VTK_TETRA;
531 cell[0] = cellData[offset];
532 cell[1] = cellData[offset+1];
533 cell[2] = cellData[offset+2];
534 cell[3] = cellData[offset+3];
535 grid->InsertNextCell( typeVtkCell, 4, cell );
552 const ::fwData::Mesh::csptr& meshSrc )
554 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
557 vtkPoints* polyDataPoints = polyDataDst->GetPoints();
558 const ::fwData::Mesh::Id nbPoints = meshSrc->getNumberOfPoints();
559 const ::fwData::Mesh::ConstPointsMultiArrayType points = meshHelper.
getPoints();
561 if (nbPoints != polyDataPoints->GetNumberOfPoints())
563 polyDataPoints->SetNumberOfPoints(nbPoints);
567 typedef ::fwData::Mesh::PointsMultiArrayType::index PointTypesIndex;
568 for (PointTypesIndex i = 0; i != nbPoints; ++i)
570 polyDataPoints->SetPoint(
id++, points[i][0], points[i][1], points[i][2]);
573 polyDataPoints->Modified();
579 const ::fwData::Mesh::csptr& meshSrc )
581 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
583 const ::fwData::Array::sptr pointColorArray = meshSrc->getPointColorsArray();
588 const vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
589 const size_t nbComponents = pointColorArray->getNumberOfComponents();
590 colors->SetNumberOfComponents(static_cast<int>(nbComponents));
591 colors->SetName(
"Colors");
593 const unsigned char* pointColor = arrayHelper.
begin<
unsigned char >();
594 const unsigned char* pointColorEnd = arrayHelper.end<
unsigned char >();
596 for (; pointColor != pointColorEnd; pointColor += nbComponents)
599 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 600 colors->InsertNextTypedTuple(pointColor);
602 colors->InsertNextTupleValue(pointColor);
605 polyDataDst->GetPointData()->SetScalars(colors);
606 polyDataDst->Modified();
610 if(polyDataDst->GetPointData()->HasArray(
"Colors"))
612 polyDataDst->GetPointData()->RemoveArray(
"Colors");
614 polyDataDst->Modified();
621 const ::fwData::Mesh::csptr& meshSrc )
623 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
625 const ::fwData::Array::sptr cellColorArray = meshSrc->getCellColorsArray();
630 const vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
631 const size_t nbComponents = cellColorArray->getNumberOfComponents();
632 colors->SetNumberOfComponents(static_cast<int>(nbComponents));
633 colors->SetName(
"Colors");
636 const unsigned char* cellColor = arrayHelper.
begin<
unsigned char >();
637 const unsigned char* cellColorEnd = arrayHelper.end<
unsigned char >();
639 for (; cellColor != cellColorEnd; cellColor += nbComponents)
642 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 643 colors->InsertNextTypedTuple(cellColor);
645 colors->InsertNextTupleValue(cellColor);
649 polyDataDst->GetCellData()->SetScalars(colors);
650 polyDataDst->Modified();
654 if(polyDataDst->GetCellData()->HasArray(
"Colors"))
656 polyDataDst->GetCellData()->RemoveArray(
"Colors");
658 polyDataDst->Modified();
666 const ::fwData::Mesh::csptr& meshSrc )
668 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
670 const ::fwData::Array::sptr pointNormalsArray = meshSrc->getPointNormalsArray();
671 if(pointNormalsArray)
675 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
676 const size_t nbComponents = pointNormalsArray->getNumberOfComponents();
677 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
679 const float* pointNormal = arrayHelper.
begin<
float >();
680 const float*
const pointNormalEnd = arrayHelper.end<
float >();
682 for (; pointNormal != pointNormalEnd; pointNormal += nbComponents)
685 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 686 normals->InsertNextTypedTuple(pointNormal);
688 normals->InsertNextTupleValue(pointNormal);
692 polyDataDst->GetPointData()->SetNormals(normals);
693 polyDataDst->Modified();
697 if(polyDataDst->GetPointData()->GetAttribute(vtkDataSetAttributes::NORMALS))
699 polyDataDst->GetPointData()->RemoveArray(vtkDataSetAttributes::NORMALS);
701 polyDataDst->Modified();
708 const ::fwData::Mesh::csptr& meshSrc )
710 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
712 const ::fwData::Array::sptr cellNormalsArray = meshSrc->getCellNormalsArray();
718 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
719 const size_t nbComponents = cellNormalsArray->getNumberOfComponents();
720 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
722 const float* cellNormal = arrayHelper.
begin<
float >();
723 const float*
const cellNormalEnd = arrayHelper.end<
float >();
725 for (; cellNormal != cellNormalEnd; cellNormal += nbComponents)
728 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 729 normals->InsertNextTypedTuple(cellNormal);
731 normals->InsertNextTupleValue(cellNormal);
735 polyDataDst->GetCellData()->SetNormals(normals);
736 polyDataDst->Modified();
740 if(polyDataDst->GetCellData()->GetAttribute(vtkDataSetAttributes::NORMALS))
742 polyDataDst->GetCellData()->RemoveArray(vtkDataSetAttributes::NORMALS);
744 polyDataDst->Modified();
751 const ::fwData::Mesh::csptr& meshSrc )
753 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
755 const ::fwData::Array::sptr pointTexCoordsArray = meshSrc->getPointTexCoordsArray();
756 if(pointTexCoordsArray)
760 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
761 const size_t nbComponents = pointTexCoordsArray->getNumberOfComponents();
762 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
764 const float* pointTexCoord = arrayHelper.
begin<
float >();
765 const float*
const pointTexCoordEnd = arrayHelper.end<
float >();
767 for (; pointTexCoord != pointTexCoordEnd; pointTexCoord += nbComponents)
770 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 771 normals->InsertNextTypedTuple(pointTexCoord);
773 normals->InsertNextTupleValue(pointTexCoord);
777 polyDataDst->GetPointData()->SetTCoords(normals);
778 polyDataDst->Modified();
782 if(polyDataDst->GetPointData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
784 polyDataDst->GetPointData()->RemoveArray(vtkDataSetAttributes::TCOORDS);
786 polyDataDst->Modified();
793 const ::fwData::Mesh::csptr& meshSrc )
795 SLM_ASSERT(
"vtkPolyData should not be NULL", polyDataDst);
797 const ::fwData::Array::sptr cellTexCoordsArray = meshSrc->getCellTexCoordsArray();
798 if(cellTexCoordsArray)
802 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
803 const size_t nbComponents = cellTexCoordsArray->getNumberOfComponents();
804 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
806 const float* cellTexCoord = arrayHelper.
begin<
float >();
807 const float*
const cellTexCoordEnd = arrayHelper.end<
float >();
809 for (; cellTexCoord != cellTexCoordEnd; cellTexCoord += nbComponents)
812 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 813 normals->InsertNextTypedTuple(cellTexCoord);
815 normals->InsertNextTupleValue(cellTexCoord);
819 polyDataDst->GetCellData()->SetTCoords(normals);
820 polyDataDst->Modified();
824 if(polyDataDst->GetCellData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
826 polyDataDst->GetCellData()->RemoveArray(vtkDataSetAttributes::TCOORDS);
828 polyDataDst->Modified();
837 vtkSmartPointer< vtkPolyData > vtkMeshRaw = vtkSmartPointer< vtkPolyData >::New();
841 vtkSmartPointer< vtkFillHolesFilter > holesFilter = vtkSmartPointer< vtkFillHolesFilter >::New();
842 holesFilter->SetHoleSize(2000);
843 holesFilter->SetInputData(vtkMeshRaw);
844 holesFilter->Update();
845 if (holesFilter->GetOutput()->GetNumberOfCells() > 0)
848 vtkMeshRaw = holesFilter->GetOutput();
852 const vtkSmartPointer< vtkPolyDataNormals > filter = vtkSmartPointer< vtkPolyDataNormals >::New();
853 filter->SetInputData(vtkMeshRaw);
854 filter->AutoOrientNormalsOn();
855 filter->FlipNormalsOff();
858 const vtkSmartPointer< vtkMassProperties > calculator = vtkSmartPointer< vtkMassProperties >::New();
859 calculator->SetInputConnection( filter->GetOutputPort() );
860 calculator->Update();
862 const double volume = calculator->GetVolume();
864 "GetVolume : " << volume <<
" vtkMassProperties::GetVolumeProjected = " <<
865 calculator->GetVolumeProjected() );
866 OSLM_DEBUG(
"Error : " << (calculator->GetVolume()- fabs(calculator->GetVolumeProjected()))*10000);
867 if ( (calculator->GetVolume()- fabs(calculator->GetVolumeProjected()))*10000 > calculator->GetVolume() )
869 std::stringstream ss;
870 ss <<
"volume - |volume projected| > volume/10000.0" << std::endl;
871 ss <<
"[volume = " << volume <<
"]" << std::endl;
872 ss <<
"[volume projected = " << calculator->GetVolumeProjected()<<
"]";
873 throw (std::out_of_range( ss.str() ));
882 const ::fwData::Mesh::csptr& meshSrc )
884 SLM_ASSERT(
"vtkUnstructuredGrid should not be NULL", gridDst);
886 const ::fwData::Array::sptr pointNormalsArray = meshSrc->getPointNormalsArray();
887 if(pointNormalsArray)
891 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
892 const size_t nbComponents = pointNormalsArray->getNumberOfComponents();
893 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
895 const float* pointNormal = arrayHelper.
begin<
float >();
896 const float*
const pointNormalEnd = arrayHelper.end<
float >();
898 for (; pointNormal != pointNormalEnd; pointNormal += nbComponents)
901 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 902 normals->InsertNextTypedTuple(pointNormal);
904 normals->InsertNextTupleValue(pointNormal);
908 gridDst->GetPointData()->SetNormals(normals);
913 if(gridDst->GetPointData()->GetAttribute(vtkDataSetAttributes::NORMALS))
915 gridDst->GetPointData()->RemoveArray(vtkDataSetAttributes::NORMALS);
925 const ::fwData::Mesh::csptr& meshSrc )
927 SLM_ASSERT(
"vtkPolyData should not be NULL", gridDst);
930 vtkPoints* polyDataPoints = gridDst->GetPoints();
931 const ::fwData::Mesh::Id nbPoints = meshSrc->getNumberOfPoints();
932 const ::fwData::Mesh::ConstPointsMultiArrayType points = meshHelper.
getPoints();
934 if (nbPoints != polyDataPoints->GetNumberOfPoints())
936 polyDataPoints->SetNumberOfPoints(nbPoints);
940 typedef ::fwData::Mesh::PointsMultiArrayType::index PointTypesIndex;
941 for (PointTypesIndex i = 0; i != nbPoints; ++i)
943 polyDataPoints->SetPoint(
id++, points[i][0], points[i][1], points[i][2]);
946 polyDataPoints->Modified();
951 const ::fwData::Mesh::csptr& meshSrc )
953 SLM_ASSERT(
"vtkPolyData should not be NULL", gridDst);
955 const ::fwData::Array::sptr pointColorArray = meshSrc->getPointColorsArray();
960 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
961 const size_t nbComponents = pointColorArray->getNumberOfComponents();
962 colors->SetNumberOfComponents(static_cast<int>(nbComponents));
963 colors->SetName(
"Colors");
965 const unsigned char* pointColor = arrayHelper.
begin<
unsigned char >();
966 const unsigned char*
const pointColorEnd = arrayHelper.end<
unsigned char >();
968 for (; pointColor != pointColorEnd; pointColor += nbComponents)
971 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 972 colors->InsertNextTypedTuple(pointColor);
974 colors->InsertNextTupleValue(pointColor);
977 gridDst->GetPointData()->SetScalars(colors);
982 if(gridDst->GetPointData()->HasArray(
"Colors"))
984 gridDst->GetPointData()->RemoveArray(
"Colors");
994 const ::fwData::Mesh::csptr& meshSrc )
996 SLM_ASSERT(
"vtkPolyData should not be NULL", gridDst);
998 const ::fwData::Array::sptr cellColorArray = meshSrc->getCellColorsArray();
1003 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
1004 const size_t nbComponents = cellColorArray->getNumberOfComponents();
1005 colors->SetNumberOfComponents(static_cast<int>(nbComponents));
1006 colors->SetName(
"Colors");
1008 const ::fwMemory::BufferObject::Lock lock = cellColorArray->getBufferObject()->lock();
1009 const unsigned char* cellColor = arrayHelper.
begin<
unsigned char >();
1010 const unsigned char*
const cellColorEnd = arrayHelper.end<
unsigned char >();
1012 for (; cellColor != cellColorEnd; cellColor += nbComponents)
1015 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 1016 colors->InsertNextTypedTuple(cellColor);
1018 colors->InsertNextTupleValue(cellColor);
1022 gridDst->GetCellData()->SetScalars(colors);
1023 gridDst->Modified();
1027 if(gridDst->GetCellData()->HasArray(
"Colors"))
1029 gridDst->GetCellData()->RemoveArray(
"Colors");
1031 gridDst->Modified();
1038 const ::fwData::Mesh::csptr& meshSrc )
1040 SLM_ASSERT(
"vtkPolyData should not be NULL", gridDst);
1042 const ::fwData::Array::sptr cellNormalsArray = meshSrc->getCellNormalsArray();
1044 if(cellNormalsArray)
1048 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
1049 const size_t nbComponents = cellNormalsArray->getNumberOfComponents();
1050 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
1052 const float* cellNormal = arrayHelper.
begin<
float >();
1053 const float*
const cellNormalEnd = arrayHelper.end<
float >();
1055 for (; cellNormal != cellNormalEnd; cellNormal += nbComponents)
1058 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 1059 normals->InsertNextTypedTuple(cellNormal);
1061 normals->InsertNextTupleValue(cellNormal);
1065 gridDst->GetCellData()->SetNormals(normals);
1066 gridDst->Modified();
1070 if(gridDst->GetCellData()->GetAttribute(vtkDataSetAttributes::NORMALS))
1072 gridDst->GetCellData()->RemoveArray(vtkDataSetAttributes::NORMALS);
1074 gridDst->Modified();
1082 const ::fwData::Mesh::csptr& meshSrc )
1084 SLM_ASSERT(
"vtkPolyData should not be NULL", gridDst);
1086 const ::fwData::Array::sptr pointTexCoordsArray = meshSrc->getPointTexCoordsArray();
1087 if(pointTexCoordsArray)
1091 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
1092 const size_t nbComponents = pointTexCoordsArray->getNumberOfComponents();
1093 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
1095 const float* pointTexCoord = arrayHelper.
begin<
float >();
1096 const float*
const pointTexCoordEnd = arrayHelper.end<
float >();
1098 for (; pointTexCoord != pointTexCoordEnd; pointTexCoord += nbComponents)
1101 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 1102 normals->InsertNextTypedTuple(pointTexCoord);
1104 normals->InsertNextTupleValue(pointTexCoord);
1108 gridDst->GetPointData()->SetTCoords(normals);
1109 gridDst->Modified();
1113 if(gridDst->GetPointData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
1115 gridDst->GetPointData()->RemoveArray(vtkDataSetAttributes::TCOORDS);
1117 gridDst->Modified();
1125 const ::fwData::Mesh::csptr& meshSrc )
1127 SLM_ASSERT(
"vtkPolyData should not be NULL", gridDst);
1129 const ::fwData::Array::sptr cellTexCoordsArray = meshSrc->getCellTexCoordsArray();
1131 if(cellTexCoordsArray)
1135 vtkSmartPointer<vtkFloatArray> normals = vtkSmartPointer<vtkFloatArray>::New();
1136 size_t nbComponents = cellTexCoordsArray->getNumberOfComponents();
1137 normals->SetNumberOfComponents(static_cast<int>(nbComponents));
1139 const float* cellTexCoord = arrayHelper.
begin<
float >();
1140 const float*
const cellTexCoordEnd = arrayHelper.end<
float >();
1142 for (; cellTexCoord != cellTexCoordEnd; cellTexCoord += nbComponents)
1145 #if (VTK_MAJOR_VERSION == 7 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION > 7 1146 normals->InsertNextTypedTuple(cellTexCoord);
1148 normals->InsertNextTupleValue(cellTexCoord);
1152 gridDst->GetCellData()->SetTCoords(normals);
1153 gridDst->Modified();
1157 if(gridDst->GetCellData()->GetAttribute(vtkDataSetAttributes::TCOORDS))
1159 gridDst->GetCellData()->RemoveArray(vtkDataSetAttributes::TCOORDS);
1161 gridDst->Modified();
static FWVTKIO_API void updatePolyDataPointColor(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with point color of fwData::Mesh.
static FWVTKIO_API void toVTKGrid(const ::fwData::Mesh::csptr &mesh, vtkSmartPointer< vtkUnstructuredGrid > grid)
Convert a ::fwData::Mesh::csptr to a vtkUnstructuredGrid.
static FWVTKIO_API void toVTKMesh(const ::fwData::Mesh::csptr &_mesh, vtkSmartPointer< vtkPolyData > _polyData)
Convert a ::fwData::Mesh::csptr to a vtkPolyData.
static FWVTKIO_API void updateGridCellColor(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with cell color of fwData::Mesh.
static FWVTKIO_API void updatePolyDataPoints(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with ::fwData::Mesh::sptr points.
base class for BufferObject Lock
static FWVTKIO_API void updateGridCellTexCoords(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with cell texCoords of fwData::Mesh.
static FWVTKIO_API void updateGridCellNormals(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with cell normals of fwData::Mesh.
static FWVTKIO_API void updateGridPoints(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with ::fwData::Mesh::sptr points.
static FWVTKIO_API void fromVTKGrid(vtkSmartPointer< vtkUnstructuredGrid > grid,::fwData::Mesh::sptr mesh)
Convert a vtkUnstructuredGrid to a ::fwData::Mesh::sptr.
static FWVTKIO_API void updatePolyDataCellColor(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with cell color of fwData::Mesh.
static FWVTKIO_API void updateGridPointColor(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with point color of fwData::Mesh.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
static FWVTKIO_API void updatePolyDataCellTexCoords(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with cell texCoords of fwData::Mesh.
static FWVTKIO_API void updatePolyDataCellNormals(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with cell normals of fwData::Mesh.
static FWVTKIO_API double computeVolume(const ::fwData::Mesh::csptr &mesh)
Compute the volume of the mesh using MassProperties vtk class.
static FWVTKIO_API void updateGridPointTexCoords(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with point texCoords of fwData::Mesh.
static FWVTKIO_API void updatePolyDataPointNormals(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with point normals of fwData::Mesh.
static FWVTKIO_API void fromVTKMesh(vtkSmartPointer< vtkPolyData > _polyData,::fwData::Mesh::sptr _mesh)
Convert a vtkPolyData to a ::fwData::Mesh::sptr.
static FWVTKIO_API void updatePolyDataPointTexCoords(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with point texCoords of fwData::Mesh.
static FWVTKIO_API void updateGridPointNormals(vtkSmartPointer< vtkUnstructuredGrid > gridDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkUnstructuredGrid with point normals of fwData::Mesh.
#define OSLM_DEBUG(message)