gpt4 book ai didi

c++ - VTK:定位和缩放多架飞机

转载 作者:行者123 更新时间:2023-11-30 05:14:49 24 4
gpt4 key购买 nike

我正在创建和显示数以千计的 vtkPlaneSources,但图形一旦显示就变慢了。我正在尝试使用 vtkGlyph3D 或 vtkGlyph3DMapper 来解决这个问题,但平面方向存在问题。我怀疑这是由于飞机的定义方式所致。 vtkPlaneSource 由以下两种方法之一定义:

1) 原点,point1 & point2

2) 居中和正常

下面的代码可以正确缩放,但我不明白根据我指定的法线生成的平面方向。我假设如果我定义了一个法线 vector ,我就会有一个与法线正交的平面。在我看来,平面可能沿着每个轴定向。 vtkGlyph3D 和 vtkGlyph3DMapper 具有相同的行为。

在这种情况下,如何使用平面法线正确定向?

vtkSmartPointer<vtkPoints> glyphPoints = 
vtkSmartPointer<vtkPoints>::New();
glyphPoints->InsertNextPoint(0, 0, 0);
glyphPoints->InsertNextPoint(2, 0, 0);
glyphPoints->InsertNextPoint(4, 0, 0);

vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(glyphPoints);

vtkSmartPointer<vtkDoubleArray> planeNormals =
vtkSmartPointer<vtkDoubleArray>::New();
planeNormals->SetName("orientArray");
planeNormals->SetNumberOfComponents(3); //3d normals (ie x,y,z)
planeNormals->SetNumberOfTuples(polydata->GetNumberOfPoints());
// Construct the normal vectors
double pN1[3] = { 1.0,0.0,0.0 };
double pN2[3] = { 0.0,1.0,0.0 };
double pN3[3] = { 0.0,0.0,1.0 };
// Add the data to the normals array
planeNormals->SetTuple(0, pN1);
planeNormals->SetTuple(1, pN2);
planeNormals->SetTuple(2, pN3);
polydata->GetPointData()->SetNormals(planeNormals);

vtkSmartPointer<vtkDoubleArray> scaleVectors =
vtkSmartPointer<vtkDoubleArray>::New();
scaleVectors->SetName("scaleArray"); //3d scaling
scaleVectors->SetNumberOfComponents(3); //3d scaling (ie x,y,z)
scaleVectors->SetNumberOfTuples(polydata->GetNumberOfPoints());
// Construct the scale vectors
double sV1[3] = { 1.0,2.0,1.0 };
double sV2[3] = { 1.0,3.0,1.0 };
double sV3[3] = { 1.5,4.0,1.0};
// Add the data to the vector array
scaleVectors->SetTuple(0, sV1);
scaleVectors->SetTuple(1, sV2);
scaleVectors->SetTuple(2, sV3);
polydata->GetPointData()->SetVectors(scaleVectors);

vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();

// Visualize
vtkSmartPointer<vtkGlyph3DMapper> glyph3Dmapper =
vtkSmartPointer<vtkGlyph3DMapper>::New();
glyph3Dmapper->SetSourceConnection(planeSource->GetOutputPort());
glyph3Dmapper->SetInputData(polydata);
glyph3Dmapper->SetScaleArray("scaleArray");
glyph3Dmapper->SetScaleModeToScaleByVectorComponents();
glyph3Dmapper->SetOrientationArray("orientArray");
glyph3Dmapper->Update();

vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(glyph3Dmapper);

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();

附件是 (1) 个仅按比例缩放的平面的图像:

planes scaled only

(2) 平面缩放和“定向”(上面代码的结果):

planes scaled and "oriented"

感谢您的帮助。

最佳答案

我找到了使用 vtkProgrammableGlyphFilter 的替代方法感谢这些例子herehere .我正在旋转每个平面的 3 个坐标(原点、点 1、点 2),然后添加到多数据数组中。结果是响应速度更快的图形显示。

#include <vtkSmartPointer.h>
#include <vtkPlaneSource.h>
#include <vtkProgrammableFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPoints.h>
#include <vtkPolyData>
#include <vtkDoubleArray>

void Glyph(void *arg){

vtkProgrammableGlyphFilter *glyphFilter = (vtkProgrammableGlyphFilter*)arg;
double origin[3];
double point1[3];
double point2[3];
double center[3];
int pid = glyphFilter->GetPointId();
glyphFilter->GetPointData()->GetArray("originArray")->GetTuple(pid, origin);
glyphFilter->GetPointData()->GetArray("point1Array")->GetTuple(pid, point1);
glyphFilter->GetPointData()->GetArray("point2Array")->GetTuple(pid, point2);
glyphFilter->GetPointData()->GetArray("centerArray")->GetTuple(pid, center);

std::cout << endl << "point id: " << pid << std::endl;
std::cout << "origin: " << origin[0] << " " << origin[1] << " " << origin[2] << std::endl;
std::cout << "point1: " << point1[0] << " " << point1[1] << " " << point1[2] << std::endl;
std::cout << "point2: " << point2[0] << " " << point2[1] << " " << point2[2] << std::endl;
std::cout << "center: " << center[0] << " " << center[1] << " " << center[2] << std::endl;

vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
plane->SetOrigin(origin);
plane->SetPoint1(point1);
plane->SetPoint2(point2);
plane->SetCenter(center);
plane->Update();
glyphFilter->SetSourceData(plane->GetOutput());
}


int main(int, char *[])
{

vtkSmartPointer<vtkPoints> glyphPoints =
vtkSmartPointer<vtkPoints>::New();
glyphPoints->SetDataTypeToDouble();
glyphPoints->SetNumberOfPoints(table->GetNumberOfRows());

vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(glyphPoints);

vtkSmartPointer<vtkDoubleArray> originArray =
vtkSmartPointer<vtkDoubleArray>::New();
originArray->SetName("originArray");
originArray->SetNumberOfComponents(3);
originArray->SetNumberOfTuples(table->GetNumberOfRows());
// Construct the plane origin points
double o1[3] = { -1.0, -1.0, 1.0 };
double o2[3] = { -1.0, -1.0, 1.0 };
double o3[3] = { -1.0, -1.0, 1.0 };
// Add the data to the array
originArray->SetTuple(0, o1);
originArray->SetTuple(1, o2);
originArray->SetTuple(2, o3);
polydata->GetPointData()->AddArray(originArray);

vtkSmartPointer<vtkDoubleArray> point1Array =
vtkSmartPointer<vtkDoubleArray>::New();
point1Array->SetName("point1Array");
point1Array->SetNumberOfComponents(3);
point1Array->SetNumberOfTuples(table->GetNumberOfRows());
// Construct the points in 1st direction
double p11[3] = { -1.0, 1.0, 1.0 };
double p12[3] = { -1.0, 1.0, 1.0 };
double p13[3] = { -1.0, 1.0, 1.0 };
// Add the data to the array
point1Array->SetTuple(0, p11);
point1Array->SetTuple(1, p12);
point1Array->SetTuple(2, p13);
polydata->GetPointData()->AddArray(point1Array);

vtkSmartPointer<vtkDoubleArray> point2Array =
vtkSmartPointer<vtkDoubleArray>::New();
point2Array->SetName("point2Array");
point2Array->SetNumberOfComponents(3);
point2Array->SetNumberOfTuples(table->GetNumberOfRows());
// Construct the points in 2nd direction
double p21[3] = { -1.0, -1.0, -1.0 };
double p22[3] = { -1.0, -1.0, -1.0 };
double p23[3] = { -1.0, -1.0, -1.0 };
// Add the data to the array
point2Array->SetTuple(0, p21);
point2Array->SetTuple(1, p22);
point2Array->SetTuple(2, p23);
polydata->GetPointData()->AddArray(point2Array);

vtkSmartPointer<vtkDoubleArray> centerArray =
vtkSmartPointer<vtkDoubleArray>::New();
centerArray->SetName("centerArray");
centerArray->SetNumberOfComponents(3);
centerArray->SetNumberOfTuples(table->GetNumberOfRows());
// Construct the new plane center (translate to this location original center)
double c1[3] = { 1.0, 0.0, 0.0 };
double c2[3] = { 3.0, 0.0, 0.0 };
double c3[3] = { 5.0, 0.0, 0.0 };
// Add the data to the array
centerArray->SetTuple(0, c1);
centerArray->SetTuple(1, c2);
centerArray->SetTuple(2, c3);
polydata->GetPointData()->AddArray(centerArray);

vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();
planeSource->SetOutputPointsPrecision(vtkAlgorithm::DOUBLE_PRECISION);
planeSource->SetCenter(0, 0, 0);
planeSource->Update();

vtkSmartPointer<vtkProgrammableGlyphFilter> glypher =
vtkSmartPointer<vtkProgrammableGlyphFilter>::New();
glypher->SetInputData(polydata);
glypher->SetSourceData(planeSource->GetOutput());
glypher->SetGlyphMethod(Glyph, glypher);
glypher->Update();

vtkSmartPointer<vtkPolyDataMapper> glyphMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
glyphMapper->SetInputConnection(glypher->GetOutputPort());

vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(glyphMapper);

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();

return EXIT_SUCCESS;
}

如果不是完整示例,请见谅。我在一个更大的程序中运行这段代码。

关于c++ - VTK:定位和缩放多架飞机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288149/

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