I tried to construct a unstructured grid using vtk and draw streamlines. But it always fails. What went wrong?
If the streamline uses point data, it will prompt:
vtkFloatArray (00000179BFA4C130): Number of components for input and output do not match.
Source: 1
Destination: 3
If the streamline uses cell data, there is nothing:
code:
int main()
{
std::vector<std::array<double, 3>> pointCoordinates;
// Face 1
pointCoordinates.push_back({ {0, 0, 0} }); // Node 0
pointCoordinates.push_back({ {10, 0, 0} }); // Node 1
pointCoordinates.push_back({ {10, 10, 0} }); // Node 2
pointCoordinates.push_back({ {0, 10, 0} }); // Node 3
// Face 2
pointCoordinates.push_back({ {10, 0, 0} }); // Node 4
pointCoordinates.push_back({ {20, 0, 0} }); // Node 5
pointCoordinates.push_back({ {20, 10, 0} }); // Node 6
pointCoordinates.push_back({ {10, 10, 0} }); // Node 7
// Face 3
pointCoordinates.push_back({ {20, 0, 0} }); // Node 4
pointCoordinates.push_back({ {30, 0, 0} }); // Node 5
pointCoordinates.push_back({ {30, 10, 0} }); // Node 6
pointCoordinates.push_back({ {20, 10, 0} }); // Node 7
// Face 4
pointCoordinates.push_back({ {30, 0, 0} }); // Node 4
pointCoordinates.push_back({ {40, 0, 0} }); // Node 5
pointCoordinates.push_back({ {40, 10, 0} }); // Node 6
pointCoordinates.push_back({ {30, 10, 0} }); // Node 7
// Face 5
pointCoordinates.push_back({ {40, 0, 0} }); // Node 4
pointCoordinates.push_back({ {50, 0, 0} }); // Node 5
pointCoordinates.push_back({ {50, 10, 0} }); // Node 6
pointCoordinates.push_back({ {40, 10, 0} }); // Node 7
// Face 6
pointCoordinates.push_back({ {50, 0, 0} }); // Node 4
pointCoordinates.push_back({ {60, 0, 0} }); // Node 5
pointCoordinates.push_back({ {60, 10, 0} }); // Node 6
pointCoordinates.push_back({ {50, 10, 0} }); // Node 7
// Face 7
pointCoordinates.push_back({ {60, 0, 0} }); // Node 4
pointCoordinates.push_back({ {70, 0, 0} }); // Node 5
pointCoordinates.push_back({ {70, 10, 0} }); // Node 6
pointCoordinates.push_back({ {60, 10, 0} }); // Node 7
// Face 8
pointCoordinates.push_back({ {70, 0, 0} }); // Node 4
pointCoordinates.push_back({ {80, 0, 0} }); // Node 5
pointCoordinates.push_back({ {80, 10, 0} }); // Node 6
pointCoordinates.push_back({ {70, 10, 0} }); // Node 7
// Create the points.
vtkNew<vtkPoints> points;
for (auto i = 0; i < pointCoordinates.size(); ++i)
{
points->InsertNextPoint(pointCoordinates[i].data());
}
vtkNew<vtkUnstructuredGrid> uGrid;
uGrid->SetPoints(points);
vtkIdType ptid[4] = { 0, 1, 2, 3 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid);
vtkIdType ptid2[4] = { 4, 5, 6, 7 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid2);
vtkIdType ptid3[4] = { 8, 9, 10, 11 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid3);
vtkIdType ptid4[4] = { 12, 13, 14, 15 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid4);
vtkIdType ptid5[4] = { 16, 17, 18, 19 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid5);
vtkIdType ptid6[4] = { 20, 21, 22, 23 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid6);
vtkIdType ptid7[4] = { 24, 25, 26, 27 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid7);
vtkIdType ptid8[4] = { 28, 29, 30, 31 };
uGrid->InsertNextCell(VTK_QUAD, 4, ptid8);
vtkSmartPointer<vtkFloatArray> vtkArrayV =
vtkSmartPointer<vtkFloatArray>::New();
vtkArrayV->SetName("V");
vtkArrayV->SetNumberOfComponents(2);
for (int i = 0; i < 8; i++)
{
float vector[2] = { i*0.5, i * 0.50 };
vtkArrayV->InsertNextTuple(vector);
}
uGrid->GetCellData()->AddArray(vtkArrayV);
vtkSmartPointer<vtkFloatArray> vtkArrayU =
vtkSmartPointer<vtkFloatArray>::New();
vtkArrayU->SetName("U");
vtkArrayU->SetNumberOfComponents(1);
cout << "number of tuples: " << vtkArrayV->GetNumberOfTuples() << endl;
for (int i = 0; i < uGrid->GetNumberOfPoints(); i++)
{
vtkArrayU->InsertNextValue(i*0.5);
}
uGrid->GetPointData()->AddArray(vtkArrayU);
vtkNew<vtkUnstructuredGrid> lineUg;
lineUg->ShallowCopy(uGrid);
vtkNew<vtkLineSource> startline;
startline->SetPoint1(0, 0, 0);
startline->SetPoint2(0, 10, 0);
startline->SetResolution(5);
vtkNew<vtkStreamTracer> lineStreamline;
lineStreamline->SurfaceStreamlinesOn();
lineStreamline->SetInputData(lineUg);
lineStreamline->SetSourceConnection(startline->GetOutputPort());
lineStreamline->SetIntegratorTypeToRungeKutta45();
lineStreamline->SetMaximumPropagation(100000);
lineStreamline->SetIntegrationStepUnit(2);
lineStreamline->SetInitialIntegrationStep(0.2);
lineStreamline->SetMinimumIntegrationStep(0.01);
lineStreamline->SetMaximumIntegrationStep(0.5);
lineStreamline->SetMaximumNumberOfSteps(2000);
lineStreamline->SetIntegrationDirectionToBoth();
lineStreamline->SetTerminalSpeed(1e-12);
lineStreamline->SetMaximumError(1e-06);
lineStreamline->SetComputeVorticity(1);
lineStreamline->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, "U");
//pointStreamline->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_CELLS, "V");
lineStreamline->Print(std::cout);
lineStreamline->GetOutput()->Print(std::cout);
vtkSmartPointer<vtkDataSetMapper> pointMapper = vtkSmartPointer<vtkDataSetMapper>::New();
pointMapper->SetInputConnection(lineStreamline->GetOutputPort());
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
pointActor->SetMapper(pointMapper);
vtkNew<vtkDataSetMapper> lineBodyMapper;
lineBodyMapper->SetInputData(uGrid);
lineBodyMapper->SetScalarModeToUseCellFieldData();
lineBodyMapper->SelectColorArray("V");
lineBodyMapper->SetArrayComponent(1);
lineBodyMapper->SetScalarRange(vtkArrayV->GetRange());
lineBodyMapper->Update();
vtkNew<vtkActor> lineBodyActor;
lineBodyActor->SetMapper(lineBodyMapper);
lineBodyActor->GetProperty()->SetOpacity(0.3);
vtkNew<vtkLookupTable> lookupTable;
lookupTable->SetTableRange(vtkArrayV->GetRange());
lookupTable->Build();
lineBodyMapper->SetLookupTable(lookupTable);
vtkNew<vtkScalarBarActor> scalarBarActor;
scalarBarActor->SetLookupTable(lookupTable);
scalarBarActor->SetTitle("U");
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(pointActor);
renderer->AddActor(lineBodyActor);
renderer->AddActor(scalarBarActor);
renderer->GradientBackgroundOn();
renderer->SetBackground(1, 1, 1);
renderer->SetBackground2(0.4, 0.55, 0.75);
renderer->ResetCamera();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return 0;
}
and StreamLine printf:
vtkStreamTracer (000001CEFE8E58F0)
Debug: Off
Modified Time: 221
Reference Count: 2
Registered Events: (none)
Executive: 000001CEFE8E3340
ErrorCode: No error
Information: 000001CEFE8CC310
AbortExecute: Off
Progress: 0
Progress Text: (None)
Start position: 0 0 0
Terminal speed: 1e-12
Maximum propagation: 100000 unit: length.
Integration step unit: cell length.
Initial integration step: 0.2
Minimum integration step: 0.01
Maximum integration step: 0.5
Integration direction: both directions.
Integrator: 000001CEFE8C5550
Maximum error: 1e-06
Maximum number of steps: 2000
Vorticity computation: On
Rotation scale: 1
vtkPolyData (000001CEFE8E5F70)
Debug: Off
Modified Time: 273
Reference Count: 1
Registered Events: (none)
Information: 000001CEFE8CCE90
Data Released: False
Global Release Data: Off
UpdateTime: 0
Field Data:
Debug: Off
Modified Time: 270
Reference Count: 1
Registered Events: (none)
Number Of Arrays: 0
Number Of Components: 0
Number Of Tuples: 0
Number Of Points: 0
Number Of Cells: 0
Cell Data:
Debug: Off
Modified Time: 273
Reference Count: 1
Registered Events:
Registered Observers:
vtkObserver (000001CEFE8E9EF0)
Event: 33
EventName: ModifiedEvent
Command: 000001CEFE8F7F80
Priority: 0
Tag: 1
Number Of Arrays: 0
Number Of Components: 0
Number Of Tuples: 0
Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 1 1 1 )
Interpolate Flags: ( 1 1 1 1 1 0 0 1 1 1 1 )
Pass Through Flags: ( 1 1 1 1 1 1 1 1 1 1 1 )
Scalars: (none)
Vectors: (none)
Normals: (none)
TCoords: (none)
Tensors: (none)
GlobalIds: (none)
PedigreeIds: (none)
EdgeFlag: (none)
Tangents: (none)
RationalWeights: (none)
HigherOrderDegrees: (none)
Point Data:
Debug: Off
Modified Time: 272
Reference Count: 1
Registered Events:
Registered Observers:
vtkObserver (000001CEFE8EA3D0)
Event: 33
EventName: ModifiedEvent
Command: 000001CEFE8F7F80
Priority: 0
Tag: 1
Number Of Arrays: 0
Number Of Components: 0
Number Of Tuples: 0
Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 1 1 1 )
Interpolate Flags: ( 1 1 1 1 1 0 0 1 1 1 1 )
Pass Through Flags: ( 1 1 1 1 1 1 1 1 1 1 1 )
Scalars: (none)
Vectors: (none)
Normals: (none)
TCoords: (none)
Tensors: (none)
GlobalIds: (none)
PedigreeIds: (none)
EdgeFlag: (none)
Tangents: (none)
RationalWeights: (none)
HigherOrderDegrees: (none)
Bounds:
Xmin,Xmax: (1, -1)
Ymin,Ymax: (1, -1)
Zmin,Zmax: (1, -1)
Compute Time: 0
Editable: false
Number Of Points: 0
Point Coordinates: 0000000000000000
PointLocator: 0000000000000000
CellLocator: 0000000000000000
Number Of Vertices: 0
Number Of Lines: 0
Number Of Polygons: 0
Number Of Triangle Strips: 0
Number Of Pieces: 1
Piece: -1
Ghost Level: 0