7 #ifndef VTKFIG_DATASET_H
8 #define VTKFIG_DATASET_H
14 #include <vtkSmartPointer.h>
15 #include <vtkUnstructuredGrid.h>
16 #include <vtkRectilinearGrid.h>
17 #include <vtkDoubleArray.h>
18 #include <vtkPointData.h>
19 #include <vtkCellData.h>
20 #include <vtkCellType.h>
21 #include <vtkIdList.h>
44 static std::shared_ptr<DataSet>
New();
66 template <
class V,
class IV>
196 void SetPointVector(
const V&u,
const V& v,
const std::string name);
212 void SetPointVector(
const V&u,
const V& v,
const V& w,
const std::string name);
224 void SetPointVector(
const V&uvw,
const int dim,
const std::string name);
289 void WriteVTK(
const std::string fname,
const std::string filetype);
333 vtkSmartPointer<vtkIdList>
GetCellList(std::string name) {
return masks[name];}
345 vtkSmartPointer<vtkDataSet> data=NULL;
346 vtkSmartPointer<vtkDataSet> boundary_data=NULL;
347 double coordinate_scale_factor=1.0;
348 double coordinate_scale_factor_xyz[3]={1.0,1.0,1.0};
351 template<
class DATA,
class WRITER>
352 void WriteVTK(vtkSmartPointer<DATA> data,
const std::string fname,
const std::string filetype);
354 std::map<std::string,vtkSmartPointer<vtkIdList>> masks;
360 template <
class V,
class IV>
366 if (this->data==NULL)
367 this->data=vtkSmartPointer<vtkUnstructuredGrid>::New();
369 auto udata=vtkUnstructuredGrid::SafeDownCast(this->data);
373 auto gridpoints = vtkSmartPointer<vtkPoints>::New();
374 udata->SetPoints(gridpoints);
376 if (this->spacedim==2)
378 for (
size_t icell=0;icell<cells.size(); icell+=3)
380 vtkIdType c[3]={cells[icell+0],cells[icell+1],cells[icell+2]};
381 udata->InsertNextCell(VTK_TRIANGLE,3,c);
384 for (
size_t ipoint=0;ipoint<points.size(); ipoint+=2)
386 gridpoints->InsertNextPoint(
387 points[ipoint+0]*coordinate_scale_factor*coordinate_scale_factor_xyz[0],
388 points[ipoint+1]*coordinate_scale_factor*coordinate_scale_factor_xyz[1],
394 for (
size_t icell=0;icell<cells.size(); icell+=4)
396 vtkIdType c[4]={cells[icell+0],cells[icell+1],cells[icell+2],cells[icell+3]};
397 udata->InsertNextCell(VTK_TETRA,4,c);
400 for (
size_t ipoint=0;ipoint<points.size(); ipoint+=3)
402 gridpoints->InsertNextPoint(points[ipoint+0]*coordinate_scale_factor*coordinate_scale_factor_xyz[0],
403 points[ipoint+1]*coordinate_scale_factor*coordinate_scale_factor_xyz[1],
404 points[ipoint+2]*coordinate_scale_factor*coordinate_scale_factor_xyz[2]
414 if (this->boundary_data==NULL)
415 this->boundary_data=vtkSmartPointer<vtkUnstructuredGrid>::New();
417 auto budata=vtkUnstructuredGrid::SafeDownCast(this->boundary_data);
420 auto bgridpoints = vtkSmartPointer<vtkPoints>::New();
421 budata->SetPoints(bgridpoints);
423 auto udata=vtkUnstructuredGrid::SafeDownCast(this->data);
424 auto pdata=udata->GetPoints();
426 int np=pdata->GetNumberOfPoints();
427 std::vector<int>pmask(np);
428 for (
size_t i=0;i<pmask.size(); i++)
436 for (
int icell=0;icell<cells.size();icell+=this->spacedim)
438 for (
int id=0;
id<this->spacedim;
id++)
440 if (pmask[cells[icell+
id]]==-1)
442 pmask[cells[icell+id]]=ip++;
444 pdata->GetPoint(cells[icell+
id],point);
445 bgridpoints->InsertNextPoint(point[0],point[1],point[2]);
449 if (this->spacedim==2)
451 vtkIdType c[2]={pmask[cells[icell+0]],pmask[cells[icell+1]]};
452 budata->InsertNextCell(VTK_LINE,2,c);
456 vtkIdType c[3]={pmask[cells[icell+0]],pmask[cells[icell+1]],pmask[cells[icell+2]]};
457 budata->InsertNextCell(VTK_TRIANGLE,3,c);
471 vtkSmartPointer<vtkDoubleArray> xcoord;
472 vtkSmartPointer<vtkDoubleArray> ycoord;
478 if (this->data==NULL)
480 this->data=vtkSmartPointer<vtkRectilinearGrid>::New();
481 auto rdata=vtkRectilinearGrid::SafeDownCast(this->data);
483 xcoord = vtkSmartPointer<vtkDoubleArray>::New();
484 ycoord = vtkSmartPointer<vtkDoubleArray>::New();
485 rdata->SetXCoordinates(xcoord);
486 rdata->SetYCoordinates(ycoord);
487 rdata->SetDimensions(Nx, Ny, 1);
491 auto rdata=vtkRectilinearGrid::SafeDownCast(this->data);
493 xcoord=vtkDoubleArray::SafeDownCast(rdata->GetXCoordinates());
494 ycoord=vtkDoubleArray::SafeDownCast(rdata->GetYCoordinates());
495 xcoord->Initialize();
496 ycoord->Initialize();
497 rdata->SetDimensions(Nx, Ny, 1);
500 xcoord->SetNumberOfComponents(1);
501 xcoord->SetNumberOfTuples(Nx);
503 ycoord->SetNumberOfComponents(1);
504 ycoord->SetNumberOfTuples(Ny);
506 for (
int i=0; i<Nx; i++)
507 xcoord->InsertComponent(i, 0, x[i]*coordinate_scale_factor*coordinate_scale_factor_xyz[0]);
508 for (
int i=0; i<Ny; i++)
509 ycoord->InsertComponent(i, 0, y[i]*coordinate_scale_factor*coordinate_scale_factor_xyz[1]);
511 this->data->Modified();
519 vtkSmartPointer<vtkDoubleArray> xcoord;
520 vtkSmartPointer<vtkDoubleArray> ycoord;
521 vtkSmartPointer<vtkDoubleArray> zcoord;
528 if (this->data==NULL)
530 this->data=vtkSmartPointer<vtkRectilinearGrid>::New();
531 auto rdata=vtkRectilinearGrid::SafeDownCast(this->data);
533 xcoord = vtkSmartPointer<vtkDoubleArray>::New();
534 ycoord = vtkSmartPointer<vtkDoubleArray>::New();
535 zcoord = vtkSmartPointer<vtkDoubleArray>::New();
536 rdata->SetXCoordinates(xcoord);
537 rdata->SetYCoordinates(ycoord);
538 rdata->SetZCoordinates(zcoord);
539 rdata->SetDimensions(Nx, Ny, Nz );
543 auto rdata=vtkRectilinearGrid::SafeDownCast(this->data);
545 xcoord=vtkDoubleArray::SafeDownCast(rdata->GetXCoordinates());
546 ycoord=vtkDoubleArray::SafeDownCast(rdata->GetYCoordinates());
547 zcoord=vtkDoubleArray::SafeDownCast(rdata->GetZCoordinates());
548 xcoord->Initialize();
549 ycoord->Initialize();
550 zcoord->Initialize();
551 rdata->SetDimensions(Nx, Ny, Nz );
554 xcoord->SetNumberOfComponents(1);
555 xcoord->SetNumberOfTuples(Nx);
557 ycoord->SetNumberOfComponents(1);
558 ycoord->SetNumberOfTuples(Ny);
560 zcoord->SetNumberOfComponents(1);
561 zcoord->SetNumberOfTuples(Nz);
563 for (
int i=0; i<Nx; i++)
564 xcoord->InsertComponent(i, 0, x[i]*coordinate_scale_factor*coordinate_scale_factor_xyz[0]);
565 for (
int i=0; i<Ny; i++)
566 ycoord->InsertComponent(i, 0, y[i]*coordinate_scale_factor*coordinate_scale_factor_xyz[1]);
567 for (
int i=0; i<Nz; i++)
568 zcoord->InsertComponent(i, 0, z[i]*coordinate_scale_factor*coordinate_scale_factor_xyz[2]);
570 this->data->Modified();
594 assert(this->data!=NULL);
595 auto ncells=this->data->GetNumberOfCells();
596 assert(ncells==values.size());
597 vtkSmartPointer<vtkDoubleArray>gridvalues;
599 if (this->data->GetPointData()->HasArray(name.c_str()))
600 gridvalues=vtkDoubleArray::SafeDownCast(this->data->GetPointData()->GetAbstractArray(name.c_str()));
603 gridvalues=vtkSmartPointer<vtkDoubleArray>::New();
604 gridvalues->SetNumberOfComponents(1);
605 gridvalues->SetNumberOfTuples(ncells);
606 gridvalues->SetName(name.c_str());
607 this->data->GetCellData()->AddArray(gridvalues);
611 for (
int i=0;i<ncells; i++)
612 gridvalues->InsertComponent(i,0,values[i]);
614 gridvalues->Modified();
622 assert(this->boundary_data!=NULL);
623 auto ncells=this->boundary_data->GetNumberOfCells();
624 assert(ncells==values.size());
625 vtkSmartPointer<vtkDoubleArray>gridvalues;
627 if (this->boundary_data->GetPointData()->HasArray(name.c_str()))
628 gridvalues=vtkDoubleArray::SafeDownCast(this->boundary_data->GetPointData()->GetAbstractArray(name.c_str()));
631 gridvalues=vtkSmartPointer<vtkDoubleArray>::New();
632 gridvalues->SetNumberOfComponents(1);
633 gridvalues->SetNumberOfTuples(ncells);
634 gridvalues->SetName(name.c_str());
635 this->boundary_data->GetCellData()->AddArray(gridvalues);
639 for (
int i=0;i<ncells; i++)
640 gridvalues->InsertComponent(i,0,values[i]);
642 gridvalues->Modified();
651 auto celllist=vtkSmartPointer<vtkIdList>::New();
652 auto cellregions=vtkDoubleArray::SafeDownCast(this->data->GetCellData()->GetAbstractArray(
"cellregions"));
654 int ncells=this->data->GetNumberOfCells();
656 for (
int icell=0;icell<ncells;icell++)
658 int ireg=cellregions->GetComponent(icell,0);
661 for (
int io=0;io<regions_omitted.size();io++)
662 if (ireg==regions_omitted[io])
668 celllist->InsertId(icelllist++,icell);
670 masks[name]=celllist;
679 assert(cells_in.size()==this->data->GetNumberOfCells());
680 auto celllist=vtkSmartPointer<vtkIdList>::New();
682 for (
int i=0;i<cells_in.size();i++)
683 if (cells_in[i]) celllist->InsertId(j++,i);
684 masks[name]=celllist;
691 auto celllist=vtkSmartPointer<vtkIdList>::New();
693 for (
int i=0;i<cells_in.size();i++)
694 celllist->InsertId(j++,cells_in[i]);
695 masks[name]=celllist;
705 assert(this->data!=NULL);
706 size_t npoints=this->data->GetNumberOfPoints();
707 assert(npoints==values.size());
708 vtkSmartPointer<vtkDoubleArray>gridvalues;
710 if (this->data->GetPointData()->HasArray(name.c_str()))
711 gridvalues=vtkDoubleArray::SafeDownCast(this->data->GetPointData()->GetAbstractArray(name.c_str()));
714 gridvalues=vtkSmartPointer<vtkDoubleArray>::New();
715 gridvalues->SetNumberOfComponents(1);
716 gridvalues->SetNumberOfTuples(npoints);
717 gridvalues->SetName(name.c_str());
718 this->data->GetPointData()->AddArray(gridvalues);
721 for (
size_t i=0;i<npoints; i++)
722 gridvalues->InsertComponent(i,0,values[i]);
723 gridvalues->Modified();
730 assert(this->spacedim==2);
731 assert(this->data!=NULL);
732 size_t npoints=this->data->GetNumberOfPoints();
733 assert(npoints==u.size());
734 assert(npoints==v.size());
735 vtkSmartPointer<vtkDoubleArray>gridvalues;
737 if (this->data->GetPointData()->HasArray(name.c_str()))
738 gridvalues=vtkDoubleArray::SafeDownCast(this->data->GetPointData()->GetAbstractArray(name.c_str()));
741 gridvalues=vtkSmartPointer<vtkDoubleArray>::New();
742 gridvalues->SetNumberOfComponents(3);
743 gridvalues->SetNumberOfTuples(npoints);
744 gridvalues->SetName(name.c_str());
745 this->data->GetPointData()->AddArray(gridvalues);
748 for (
size_t i=0;i<npoints; i++)
749 gridvalues->InsertTuple3(i,u[i],v[i],0);
752 gridvalues->Modified();
761 assert(this->spacedim==3);
762 assert(this->data!=NULL);
763 size_t npoints=this->data->GetNumberOfPoints();
764 assert(npoints==u.size());
765 assert(npoints==v.size());
766 assert(npoints==w.size());
767 vtkSmartPointer<vtkDoubleArray>gridvalues;
769 if (this->data->GetPointData()->HasArray(name.c_str()))
770 gridvalues=vtkDoubleArray::SafeDownCast(this->data->GetPointData()->GetAbstractArray(name.c_str()));
773 gridvalues=vtkSmartPointer<vtkDoubleArray>::New();
774 gridvalues->SetNumberOfComponents(3);
775 gridvalues->SetNumberOfTuples(npoints);
776 gridvalues->SetName(name.c_str());
777 this->data->GetPointData()->AddArray(gridvalues);
781 for (
size_t i=0;i<npoints; i++)
782 gridvalues->InsertTuple3(i,u[i],v[i],w[i]);
783 gridvalues->Modified();
791 assert(this->spacedim==dim);
792 assert(this->data!=NULL);
793 size_t npoints=this->data->GetNumberOfPoints();
794 assert(npoints==uvw.size()/dim);
796 vtkSmartPointer<vtkDoubleArray>gridvalues;
798 if (this->data->GetPointData()->HasArray(name.c_str()))
800 gridvalues=vtkDoubleArray::SafeDownCast(this->data->GetPointData()->GetAbstractArray(name.c_str()));
804 gridvalues=vtkSmartPointer<vtkDoubleArray>::New();
805 gridvalues->SetNumberOfComponents(3);
806 gridvalues->SetNumberOfTuples(npoints);
807 gridvalues->SetName(name.c_str());
808 this->data->GetPointData()->AddArray(gridvalues);
814 for (
size_t i=0,j=0;i<npoints; i++,j+=2)
815 gridvalues->InsertTuple3(i,uvw[j],uvw[j+1],0);
818 for (
size_t i=0,j=0;i<npoints; i++,j+=3)
819 gridvalues->InsertTuple3(i,uvw[j],uvw[j+1],uvw[j+2]);
824 gridvalues->Modified();