vtk画个手的骨架

2017-09-16  本文已影响0人  zjh3029
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include<vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkAutoInit.h>
#include <vtkLineSource.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)


using namespace std;

auto createball(float x, float y, float z, int r)
{
    vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetCenter(x, y, z);
    sphereSource->SetRadius(r);
    sphereSource->Update();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(sphereSource->GetOutputPort());

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

    return actor;
}

auto createline(double x1, double y1, double z1, double x2, double y2, double z2)
{
    vtkSmartPointer<vtkLineSource> lineSource =
        vtkSmartPointer<vtkLineSource>::New();
    lineSource->SetPoint1(x1, y1, z1);
    lineSource->SetPoint2(x2, y2, z2);
    lineSource->Update();

    // Visualize
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(lineSource->GetOutputPort());
    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetLineWidth(4);

    return actor;
}

int main()
{
    //设置圆
    auto actor11 = createball(-12.0, -10.0, 0.0, 2.0);
    auto actor12 = createball(-15.0, -4.0, 0.0, 2.0);
    auto actor13 = createball(-18.0, 3.0, 0.0, 2.0);

    auto actor21 = createball(-6.0, -2.0, 0.0, 2.0);
    auto actor22 = createball(-8.0, 10.0, 0.0, 2.0);
    auto actor23 = createball(-10.0, 25.0, 0.0, 2.0);

    auto actor31 = createball(0.0, 0.0, 0.0, 2.0);
    auto actor32 = createball(0.0, 12.0, 0.0, 2.0);
    auto actor33 = createball(0.0, 27.0, 0.0, 2.0);

    auto actor41 = createball(5.0, -2.0, 0.0, 2.0);
    auto actor42 = createball(7.0, 10.0, 0.0, 2.0);
    auto actor43 = createball(9.0, 23.0, 0.0, 2.0);

    auto actor51 = createball(10.0, -6.0, 0.0, 2.0);
    auto actor52 = createball(12.0, 0.0, 0.0, 2.0);
    auto actor53 = createball(14.0, 10.0, 0.0, 2.0);


    auto actor_ori = createball(0.0, -18.0, 0.0, 2.0);


    //设置线
    auto line_actor10 = createline(-12.0, -10.0, 0.0, 0.0, -18.0, 0.0);
    auto line_actor11 = createline(-12.0, -10.0, 0.0, -15.0, -4.0, 0.0);
    auto line_actor12 = createline(-15.0, -4.0, 0.0, -18.0, 3.0, 0.0);


    auto line_actor20 = createline(-6.0, -2.0, 0.0, 0.0, -18.0, 0.0);
    auto line_actor21 = createline(-6.0, -2.0, 0.0, -8.0, 10.0, 0.0);
    auto line_actor22 = createline(-8.0, 10.0, 0.0, -10.0, 25.0, 0.0);

    auto line_actor30 = createline(0.0, 0.0, 0.0, 0.0, -18.0, 0.0);
    auto line_actor31 = createline(0.0, 0.0, 0.0, 0.0, 12.0, 0.0);
    auto line_actor32 = createline(0.0, 12.0, 0.0, 0.0, 27.0, 0.0);

    auto line_actor40 = createline(5.0, -2.0, 0.0, 0.0, -18.0, 0.0);
    auto line_actor41 = createline(5.0, -2.0, 0.0, 7.0, 10.0, 0.0);
    auto line_actor42 = createline(7.0, 10.0, 0.0, 9.0, 23.0, 0.0);

    auto line_actor50 = createline(10.0, -6.0, 0.0, 0.0, -18.0, 0.0);
    auto line_actor51 = createline(10.0, -6.0, 0.0, 12.0, 0.0, 0.0);
    auto line_actor52 = createline(12.0, 0.0, 0.0, 14.0, 10.0, 0.0);

    // A renderer and render window
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    // An interactor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Add the actors to the scene

    renderer->AddActor(actor11);
    renderer->AddActor(actor12);
    renderer->AddActor(actor13);
    renderer->AddActor(actor21);
    renderer->AddActor(actor22);
    renderer->AddActor(actor23);
    renderer->AddActor(actor31);
    renderer->AddActor(actor32);
    renderer->AddActor(actor33);
    renderer->AddActor(actor41);
    renderer->AddActor(actor42);
    renderer->AddActor(actor43);
    renderer->AddActor(actor51);
    renderer->AddActor(actor52);
    renderer->AddActor(actor53);
    renderer->AddActor(actor_ori);

    renderer->AddActor(line_actor10);
    renderer->AddActor(line_actor11);
    renderer->AddActor(line_actor12);

    renderer->AddActor(line_actor20);
    renderer->AddActor(line_actor21);
    renderer->AddActor(line_actor22);

    renderer->AddActor(line_actor30);
    renderer->AddActor(line_actor31);
    renderer->AddActor(line_actor32);

    renderer->AddActor(line_actor40);
    renderer->AddActor(line_actor41);
    renderer->AddActor(line_actor42);

    renderer->AddActor(line_actor50);
    renderer->AddActor(line_actor51);
    renderer->AddActor(line_actor52);




    renderer->SetBackground(0, 0, 0); // Background color white

                                      // Render an image (lights and cameras are created automatically)
    renderWindow->Render();

    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
        vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

    renderWindowInteractor->SetInteractorStyle(style);

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

    return EXIT_SUCCESS;
}
上一篇下一篇

猜你喜欢

热点阅读