如何将颜色应用于从Blender 2.69导入到VTK 6.2 + Qt 5.3的网格

时间:2014-08-03 20:02:04

标签: qt vtk qvtkwidget

我是vtk和Qt的新手。我在搅拌机中创建了一个立方体,其中3个面为红色,1个面为绿色,并导出为cube.ply以保留颜色信息:

ply
format ascii 1.0
comment Created by Blender 2.69 (sub 0) - www.blender.org, source file: ''
element vertex 24
property float x
property float y
property float z
property float nx
property float ny
property float nz
element face 6
property list uchar uint vertex_indices
end_header
1.000000 1.000000 -1.000000 0.000000 0.000000 -1.000000
1.000000 -1.000000 -1.000000 0.000000 0.000000 -1.000000
-1.000000 -1.000000 -1.000000 0.000000 0.000000 -1.000000
-1.000000 1.000000 -1.000000 0.000000 0.000000 -1.000000
1.000000 0.999999 1.000000 0.000000 -0.000000 1.000000
-1.000000 1.000000 1.000000 0.000000 -0.000000 1.000000
-1.000000 -1.000000 1.000000 0.000000 -0.000000 1.000000
0.999999 -1.000001 1.000000 0.000000 -0.000000 1.000000
1.000000 1.000000 -1.000000 1.000000 -0.000000 0.000000
1.000000 0.999999 1.000000 1.000000 -0.000000 0.000000
0.999999 -1.000001 1.000000 1.000000 -0.000000 0.000000
1.000000 -1.000000 -1.000000 1.000000 -0.000000 0.000000
1.000000 -1.000000 -1.000000 -0.000000 -1.000000 -0.000000
0.999999 -1.000001 1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 -1.000000 -0.000000 -1.000000 -0.000000
-1.000000 -1.000000 -1.000000 -1.000000 0.000000 -0.000000
-1.000000 -1.000000 1.000000 -1.000000 0.000000 -0.000000
-1.000000 1.000000 1.000000 -1.000000 0.000000 -0.000000
-1.000000 1.000000 -1.000000 -1.000000 0.000000 -0.000000
1.000000 0.999999 1.000000 0.000000 1.000000 0.000000
1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
-1.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
-1.000000 1.000000 1.000000 0.000000 1.000000 0.000000
4 0 1 2 3
4 4 5 6 7
4 8 9 10 11
4 12 13 14 15
4 16 17 18 19
4 20 21 22 23

我在Qt Creator中创建了一个QVTK Widget,我的代码用灰色绘制了多维数据集。以下是我的mainwindow.cpp文件(经过各种修改以获得颜色显示):

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QVTKInteractor.h"
#include <vtkSTLReader.h>
#include <vtkPLYReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);



   vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
   vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
   vtkSmartPointer<vtkRenderWindow> renwin = vtkSmartPointer<vtkRenderWindow>::New();
   vtkSmartPointer<QVTKInteractor> interactor= vtkSmartPointer<QVTKInteractor>::New();
   vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
   vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();


    reader->SetFileName("../vtk2/media/cube.ply");
    reader->Update();
    mapper->SetColorModeToMapScalars();
    mapper->SetInputConnection(reader->GetOutputPort());

    mapper->ScalarVisibilityOn();
    actor->SetMapper(mapper);
    renderer->AddActor(actor);

       ui->qvtkWidget->SetRenderWindow(renwin);
       ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
       renderer->SetBackground(.3,.6,.3);
       actor->GetProperty()->SetFrontfaceCulling(true);
       QVTKInteractor* inter=ui->qvtkWidget->GetInteractor();
}

MainWindow::~MainWindow()
{
    delete ui;
}

它不显示任何颜色。尝试了各种VTK示例后,我还没有找到一个展示如何从导入的3d文件中映射颜色的示例。我确信我错过了一段代码。

副作用,立方体在QVTKWidget中加载看起来不对称。我想知道这是否与QVTK有关。我正在附上一张图片..

访问http://postimg.org/image/r8093cwbt/获取图片

1 个答案:

答案 0 :(得分:0)

您可以尝试访问vtkPolyData,然后访问pointData(或cellData)并设置颜色,如下所示

mapper->GetInput()->GetPointData()->SetScalars(colors);

在这种情况下,color是一个vtkUnsignedCharArray,颜色为r,g,b(更多细节Here)。

或者您也可以尝试修改这样的actor属性:

actor->GetProperty()SetColor(double r, double g, double b);

在这种情况下,请记住添加行#include <vtkProperty.h>以便能够访问actor的属性