- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个点云,每个点都有 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();
最佳答案
我认为你弄乱了点数据数组:
在您的代码中,v
和 magnitude
指向同一个对象:
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()
为了完整起见,我还提供了其余代码。
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/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!