gpt4 book ai didi

rendering - VTK 无法正确渲染多边形

转载 作者:行者123 更新时间:2023-12-02 22:52:55 33 4
gpt4 key购买 nike

我有一个 VTK 6.3 程序,我已将其精简为下面的列表。

当我运行它时,渲染的多边形不正确。它应该是一个四边形,侧面还有另一个四边形,而不是这个古怪的东西:

Bad polygon rendering

看起来多边形三角测量有误,并且当您旋转多边形时,一个流氓三角形会闪烁。

我相信我的多边形已正确指定:逆时针且非自相交。

我仔细研究了 VTK 代码,希望找到一种改变三角测量方法的方法。 VTKPolygon类包含几种不同的三角测量方法,但尚不清楚是否可以手动设置该方法。

如果有人提出解决此问题的建议,我将非常感激。

正如我所说,我相信该列表接近重现问题的最少量代码。

此外,正如评论中所指出的,此行为在 VTK 8.0 版中已修复。但是,我运行的是 RHEL 7.4,安装 VTK 版本 7 或 8 并不简单。 VTK 似乎需要比 RHEL 7.4 可用的版本更新的 OpenGL 版本。

#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolygon.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <iostream>

using namespace std;

int main (int argc, char **argv)
{
struct P { float x, y, z; };
vector<P> p;
p.push_back (P {8.0, 3.0, 0.0});
p.push_back (P {7.0, 5.0, 0.0});
p.push_back (P {1.0, 3.0, 0.0});
p.push_back (P {1.0, 2.0, 0.0});
p.push_back (P {0.0, 3.0, 0.0});
p.push_back (P {0.0, 1.0, 0.0});
p.push_back (P {1.0, 1.0, 0.0});
p.push_back (P {1.0, 0.0, 0.0});

vtkSmartPointer<vtkPolygon> poly = vtkSmartPointer<vtkPolygon>::New();
poly->GetPointIds()->SetNumberOfIds(p.size ());

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> polygons = vtkSmartPointer<vtkCellArray>::New();

for (size_t i = 0; i < p.size (); ++i)
{
points->InsertNextPoint (p[i].x, p[i].y, p[i].z);
poly->GetPointIds()->SetId(i, i);
}

polygons->InsertNextCell(poly);
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();

polydata->SetPoints(points);
polydata->SetPolys(polygons);

vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polydata);

vtkSmartPointer<vtkActor> buildingActor = vtkSmartPointer<vtkActor>::New();
buildingActor->SetMapper(mapper);

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(buildingActor);

vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start ();

return 0;
}

最佳答案

我认为VTK对坐标原点有一些技巧。我采用了你的VTK 8.1.1源代码。我更改了坐标集以获得两个在空间中具有不同方向的三角形

enter image description here

我将修改后的源代码放在下面

#include "vtkAutoInit.h"
#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)
#include "stdafx.h"


#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolygon.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
struct P { float x, y, z; };
vector<P> p;

p.push_back(P{ 8.0, 8.0, 3.0 });
p.push_back(P{ 8.0, 1.0, 0.0 });
p.push_back(P{ 1.0, 8.0, 0.0 });
p.push_back(P{ 1.0, 1.0, 3.0 });
p.push_back(P{ 8.0, 1.0, 0.0 });


vtkSmartPointer<vtkPolygon> poly = vtkSmartPointer<vtkPolygon>::New();
poly->GetPointIds()->SetNumberOfIds(p.size());

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> polygons =vtkSmartPointer<vtkCellArray>::New();

for (size_t i = 0; i < p.size(); ++i)
{
points->InsertNextPoint(p[i].x, p[i].y, p[i].z);
poly->GetPointIds()->SetId(i, i);
}

polygons->InsertNextCell(poly);
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();

polydata->SetPoints(points);
polydata->SetPolys(polygons);

vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polydata);

vtkSmartPointer<vtkActor> buildingActor = vtkSmartPointer<vtkActor>::New();
buildingActor->SetMapper(mapper);

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(buildingActor);

vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();

renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();

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

return 0;
}

关于rendering - VTK 无法正确渲染多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49581655/

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