VTK画直线带箭头和长度文字vtkLineWidget2

40 篇文章 7 订阅

 

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkLineRepresentation.h>
#include <vtkLineWidget2.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)

namespace {
    // This does the actual work.
    // Callback for the interaction
    class vtkLineCallback : public vtkCommand
    {
    public:
        static vtkLineCallback* New()
        {
            return new vtkLineCallback;
        }

        virtual void Execute(vtkObject* caller, unsigned long, void*)
        {

            vtkLineWidget2* lineWidget = reinterpret_cast<vtkLineWidget2*>(caller);

            // Get the actual box coordinates of the line
            vtkNew<vtkPolyData> polydata;
            static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
                ->GetPolyData(polydata);

            // Display one of the points, just so we know it's working
            double p[3];
            polydata->GetPoint(0, p);
            std::cout << "P: " << p[0] << " " << p[1] << " " << p[2] << std::endl;
        }
        vtkLineCallback()
        {
        }
    };
} // namespace

int main(int, char*[])
{
    vtkNew<vtkNamedColors> colors;

    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->Update();

    // Create a mapper and actor
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(sphereSource->GetOutputPort());
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

    // A renderer and render window
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("LineWidget2");

    renderer->AddActor(actor);
    //renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
    renderer->SetBackground(colors->GetColor3d("Black").GetData());

    // An interactor
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    vtkNew<vtkLineWidget2> lineWidget;
    lineWidget->SetInteractor(renderWindowInteractor);
    lineWidget->CreateDefaultRepresentation();

    // You could do this if you want to set properties at this point:
    vtkNew<vtkLineRepresentation> lineRepresentation;
    lineRepresentation->SetDistanceAnnotationVisibility(true);
    lineRepresentation->SetDistanceAnnotationFormat("%-#6.2f mm");
    lineRepresentation->SetLineColor(1, 0, 0);
    lineRepresentation->SetDirectionalLine(true);

    lineRepresentation->GetEndPointProperty()->SetColor(1, 0, 0);
    //lineRepresentation->GetEndPointProperty()->SetLineWidth(.5);
    lineRepresentation->GetEndPointProperty()->SetOpacity(0.1);

    //lineRepresentation->GetEndPoint2Property()->SetLineWidth(10.);
    lineRepresentation->GetEndPoint2Property()->SetPointSize(20.0);
    
    lineRepresentation->GetEndPoint2Property()->SetColor(1, 0, 0);


    lineWidget->SetRepresentation(lineRepresentation);

    vtkNew<vtkLineCallback> lineCallback;

    lineWidget->AddObserver(vtkCommand::InteractionEvent, lineCallback);

    // Render
    renderWindow->Render();

    renderWindowInteractor->Initialize();
    renderWindow->Render();
    lineWidget->On();

    // Begin mouse interaction
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

 

效果:

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值