用vtkLine创建两条直线并设置各自直线的颜色

引言

本例来自于vtk官网。创建两条不同颜色的直线。下面直接上代码。

示例

开发环境

使用QtCreator4.11.2,Qt5.14.2。使用的vtk9.2的库及其头文件。创建空项目。

示例代码

.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11 vtk9.2

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOUPDIR = $$PWD/../../SOUPdependency
vtk9.2 {
    contains(QT_ARCH, x86_64) {
        include($$SOUPDIR/vtk-9.2/vtk-9.2.pri)
    } else {
        include($$SOUPDIR/vtk-9.2-2017-omp-win32/vtk-9.2.pri)
    }
    DEFINES += vtkEventDataButton3D=vtkEventDataDevice3D
    DEFINES += vtkEventDataMove3D=vtkEventDataDevice3D
}

include($$SOUPDIR/dcmtk-3.6.4/dcmtk-3.6.4.pri)

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    VtkWidget.h

SOURCES += \
    VtkWidget.cpp \
    main.cpp

main.cpp

#include <QApplication>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkLine.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkUnsignedCharArray.h>
#include <vtkDataSet.h>
#include <vtkCellData.h>
#include <vtkProperty.h>
#include "VtkWidget.h"

int main(int argc,char *argv[])
{
    QApplication a(argc, argv);
    double origin[3] = {0.0,0.0,0.0};
    double p0[3] = {1.0,0.0,0.0};
    double p1[3] = {0.0,1.0,0.0};

    vtkNew<vtkPoints> points;//多用于局部变量,为智能指针,相对于vtkSmartPointer,vtkNew不能直接获取指针
    points->InsertNextPoint(origin);
    points->InsertNextPoint(p0);
    points->InsertNextPoint(p1);

    vtkNew<vtkLine> line1;
    line1->GetPointIds()->SetId(0,0);
    line1->GetPointIds()->SetId(1,1);

    vtkNew<vtkLine> line2;
    line2->GetPointIds()->SetId(0,0);
    line2->GetPointIds()->SetId(1,2);

    vtkNew<vtkCellArray> cellArray;
    cellArray->InsertNextCell(line1);
    cellArray->InsertNextCell(line2);

    vtkNew<vtkPolyData> polyData;
    polyData->SetPoints(points);
    polyData->SetLines(cellArray);


   vtkNew<vtkNamedColors> colors;
   vtkNew<vtkUnsignedCharArray> charArray;
   charArray->SetNumberOfComponents(3);//设置数组维度
   charArray->InsertNextTypedTuple(colors->GetColor3ub("Tomato").GetData());
   charArray->InsertNextTypedTuple(colors->GetColor3ub("Mint").GetData());
   polyData->GetCellData()->SetScalars(charArray);//设置线的颜色

   vtkNew<vtkPolyDataMapper> mapper;
   mapper->SetInputData(polyData);

   vtkNew<vtkActor> actor;
   actor->SetMapper(mapper);
   actor->GetProperty()->SetLineWidth(3);

   VtkWidget w;
   w.setRenderBackGroundColor("SlateGray");
   w.addRenderActor(actor);
   w.addRenderToRenderWindow();
   w.show();

    return a.exec();
}

运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
是的,您是正确的,addLine()函数不是pcl::visualization::PCLVisualizer类的成员函数。如果您想将一条线添加到PCLVisualizer中,您可以使用addLine()函数的替代方法,例如addLine()函数是vtkRenderer类的成员函数,这是PCLVisualizer类的基础。您可以使用以下代码将一条线添加到PCLVisualizer中: ``` pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Viewer")); viewer->addCoordinateSystem(1.0); viewer->setBackgroundColor(0, 0, 0); // Define line parameters Eigen::Vector3f p1(0.0, 0.0, 0.0); Eigen::Vector3f p2(1.0, 1.0, 1.0); pcl::PointXYZ pt1(p1[0], p1[1], p1[2]); pcl::PointXYZ pt2(p2[0], p2[1], p2[2]); // Create a vtkLine object vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New(); line->GetPointIds()->SetId(0, 0); line->GetPointIds()->SetId(1, 1); vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New(); lines->InsertNextCell(line); // Create a vtkPolyData object and add the vtkLine to it vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New(); polyData->SetPoints(points); polyData->SetLines(lines); // Create a vtkPolyDataMapper object and set the input polyData vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputData(polyData); // Create a vtkActor object and set the mapper vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); // Add the actor to the PCLVisualizer viewer->getRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor); // Display the PCLVisualizer while (!viewer->wasStopped()) { viewer->spinOnce(); } ``` 在这个例子中,我们首先创建了一个PCLVisualizer对象,并设置了一些基本的属性,例如坐标系和背景颜色。然后,我们定义了一条线的参数,即起点和终点。接下来,我们创建了一个vtkLine对象并将其添加到vtkCellArray中。然后,我们创建一个vtkPolyData对象,并将vtkLine添加到其中。然后,我们创建一个vtkPolyDataMapper对象,并将其设置vtkPolyData的输入。最后,我们创建一个vtkActor对象,并将其设置vtkPolyDataMapper的输入。最后,我们将vtkActor添加到PCLVisualizer中,并显示它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值