gpt4 book ai didi

c++ - 将速度场可视化为定向箭头

转载 作者:行者123 更新时间:2023-12-01 14:43:56 24 4
gpt4 key购买 nike

我有一个点云,每个点都有 11 个数据数组,代表 11 个属性,包括 vx、vy、vz。我创建了一个新的数据数组,将 vx、vy、vz 存储为每个点的元组,并将其命名为 v(即速度)。我想通过使用 vtkGlyph3D 将每个点的速度可视化为定向箭头,但它们都指向一个方向。我尝试了所有相关示例,但它们仍然朝着同一个方向发展。我对 vtk 和 C++ 很陌生。如果有人可以帮助我,我将不胜感激。

提前致谢!

这是我的代码:

//get vx, vy, vz
vtkDataArray* vx0 = polyData->GetPointData()->GetArray("vx");
vtkDataArray* vy0 = polyData->GetPointData()->GetArray("vy");
vtkDataArray* vz0 = polyData->GetPointData()->GetArray("vz");

//define and initialize v and magnitude
vtkDataArray* v = polyData->GetPointData()->GetArray("vz") ;
vtkDataArray* magnitude = polyData->GetPointData()->GetArray("vz");
v->SetName("v");

//insert v and magnitude to each point
int numPoints = polyData->GetNumberOfPoints();
for (vtkIdType i = 0; i < numPoints; i++) { //define i; set loop condition
double VX[3];
double VY[3];
double VZ[3];
vx0->GetTuple(i, VX);
vy0->GetTuple(i, VY);
vz0->GetTuple(i, VZ);
v->InsertTuple3(i, VX[0], VY[0], VZ[0]);

double m = 0.0;
m = sqrt(VX[0]*VX[0]+VY[0]*VY[0]+VZ[0]*VZ[0]);
magnitude->InsertTuple1(i, m);
}

polyData->GetPointData()->AddArray(v);
polyData->GetPointData()->SetVectors(v);
polyData->GetPointData()->AddArray(magnitude);
polyData->GetPointData()->SetScalars(magnitude);
polyData->GetPointData()->SetActiveVectors("v");

//visualize
vtkSmartPointer<vtkArrowSource> arrowSource = vtkSmartPointer<vtkArrowSource>::New();
vtkSmartPointer<vtkGlyph3D> glyph3D = vtkSmartPointer<vtkGlyph3D>::New();
glyph3D->SetSourceConnection(arrowSource->GetOutputPort());
glyph3D->SetInputData(polyData);
glyph3D->SetScaleFactor(0.01);
glyph3D->OrientOn();
glyph3D->SetVectorModeToUseVector();
glyph3D->SetColorModeToColorByScalar();
glyph3D->Update();


vtkSmartPointer<vtkPolyDataMapper> sMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sMapper->SetInputConnection(glyph3D->GetOutputPort());
sMapper->ScalarVisibilityOn();

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(sMapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderer->AddActor(actor);
renderer->SetBackground(.3, .6, .3); // Background color green

renderWindow->Render();
renderWindowInteractor->Start();

最佳答案

我认为你弄乱了点数据数组:

在您的代码中,vmagnitude 指向同一个对象:

vtkDataArray* v = polyData->GetPointData()->GetArray("vz") ;
vtkDataArray* magnitude = polyData->GetPointData()->GetArray("vz");

然后您向该数组添加两次内容 - 首先是三元组,然后是一元组。这肯定会破坏你的阵列。 (实际上,它应该产生错误消息或产生垃圾,因为 InsertTuple()SetTuple 仅适用于数据数组已初始化的确切组件数。)

v->InsertTuple3(i, VX[0], VY[0], VZ[0]);
magnitude->InsertTuple1(i, m);

您应该使用两个不同的数据数组。这是一些 python 代码,说明了用法。

import vtk
import numpy as np

# Create input.
# poly: vtk.vtkPolyData()
# vel: np.ndarray with shape (nPoints, 3)
# mag: np.ndarray with shape (nPoints,)
poly, vel, mag = createData()

# Create data arrays.
velocity = vtk.vtkDoubleArray()
velocity.SetName("velocity")
velocity.SetNumberOfComponents(3)
velocity.SetNumberOfTuples(nPoints)
for i in range(nPoints):
velocity.SetTuple(i, list(vel[i]))
magnitude = vtk.vtkDoubleArray()
# Similar as SetNumberOfValues(1) + SetNumberOfTuples(nPoints):
magnitude.SetNumberOfValues(nPoints)
magnitude.SetName("magnitude")
for i in range(nPoints):
magnitude.SetValue(i, mag[i])

# Add to point data array.
poly.GetPointData().AddArray(velocity)
poly.GetPointData().AddArray(magnitude)
poly.GetPointData().SetActiveScalars("magnitude")
poly.GetPointData().SetActiveVectors("velocity")

# Create glyph.
glyph = vtk.vtkGlyph3D()
glyph.SetInputData(poly)
glyph.SetSourceConnection(arrow.GetOutputPort())
glyph.SetScaleFactor(0.1)
# glyph.OrientOn()
glyph.SetVectorModeToUseVector()
glyph.SetColorModeToColorByScalar()
glyph.Update()

visualize()

enter image description here

为了完整起见,我还提供了其余代码。

def createData():
source = vtk.vtkSphereSource()
source.SetPhiResolution(30)
source.SetThetaResolution(30)
source.Update()
poly = source.GetOutput()
arrow = vtk.vtkArrowSource()
arrow.Update()
# Compute some velocity field. This data you already got.
# I'm creating here some data myself.
nPoints = poly.GetNumberOfPoints()
points = [poly.GetPoint(i) for i in range(nPoints)]
points = np.asarray(points)
x,y,z = points.T
vel = np.c_[x*np.cos(y*np.pi), y*np.sin(2*x*np.pi), np.cos(2*z*np.pi)]
mag = np.linalg.norm(vel, axis=1)
return poly, vel, mag

def visualize():
# Visualization (my own toolbox)
from vtkutils.item_renderer import ItemRenderer
ren = ItemRenderer()
ren.backgroundColor = "black"
ren.addObject(glyph, showScalars=True, showArray="magnitude")
ren.show()

关于c++ - 将速度场可视化为定向箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59223211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com