【VTK】create spline points

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/theArcticOcean/article/details/84494471

定义5个点

在这里插入图片描述

    double p[5][3] = { {0.0, 4.0, 0.0},
                       {2.0, 0.0, 0.0},
                       {4.0, 2.0, 0.0},
                       {6.0, 0.0, 0.0},
                       {8.0, 4.0, 0.0} };

定义500个插值点

在这里插入图片描述

示例代码:

    double p[5][3] = { {0.0, 4.0, 0.0},
                       {2.0, 0.0, 0.0},
                       {4.0, 2.0, 0.0},
                       {6.0, 0.0, 0.0},
                       {8.0, 4.0, 0.0} };

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    for( int i = 0; i < 5; ++i )
    {
        points->InsertNextPoint( p[i] );
    }

    vtkSmartPointer<vtkParametricSpline> spline =
        vtkSmartPointer<vtkParametricSpline>::New();
    spline->SetPoints(points);

    vtkSmartPointer<vtkPoints> betaPoints = vtkSmartPointer<vtkPoints>::New();
    int index = 0;
    int countOfDeltaPoints = 500;
    double step = 1.0 / ( countOfDeltaPoints - 1 );
    for( double i = 0; i <= 1; i = i + step )
    {
        double tmp[3] = { i, 0, 0 };
        spline->Evaluate( tmp, tmp, NULL );
        betaPoints->InsertPoint( index++, tmp );
    }

使用vtkParametricFunctionSource的polyData,不用开发者自己来create points:

在这里插入图片描述

示例代码:

#include <vtkSmartPointer.h>
#include <vtkParametricFunctionSource.h>
#include <vtkParametricSpline.h>

#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkProperty.h>

#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTransform.h>

#include <vtkSphereSource.h>
#include <vtkNamedColors.h>
#include <vtkTextSource.h>
#include <vtkCoordinate.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkActor2D.h>

#include <QString>
#include <iostream>
using namespace std;

int main(int, char *[])
{
    vtkSmartPointer<vtkNamedColors> colors =
            vtkSmartPointer<vtkNamedColors>::New();

    double p[5][3] = { {0.0, 4.0, 0.0},
                       {2.0, 0.0, 0.0},
                       {4.0, 2.0, 0.0},
                       {6.0, 0.0, 0.0},
                       {8.0, 4.0, 0.0} };

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    for( int i = 0; i < 5; ++i )
    {
        points->InsertNextPoint( p[i] );
    }

    vtkSmartPointer<vtkParametricSpline> spline =
        vtkSmartPointer<vtkParametricSpline>::New();
    spline->SetPoints(points);

/*
 * we can use this way to create some delta points on line.
 *
    vtkSmartPointer<vtkPoints> betaPoints = vtkSmartPointer<vtkPoints>::New();
    int index = 0;
    int countOfDeltaPoints = 500;
    double step = 1.0 / ( countOfDeltaPoints - 1 );
    for( double i = 0; i <= 1; i = i + step )
    {
        double tmp[3] = { i, 0, 0 };
        spline->Evaluate( tmp, tmp, NULL );
        betaPoints->InsertPoint( index++, tmp );
    }

    spline->SetPoints( betaPoints );

*/
    vtkSmartPointer<vtkParametricFunctionSource> functionSource =
        vtkSmartPointer<vtkParametricFunctionSource>::New();
    functionSource->SetParametricFunction(spline);
    functionSource->Update();

    vtkPolyData* polyData = functionSource->GetOutput();
    vtkPoints* splinePoints = polyData->GetPoints();

    // Setup actor and mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(functionSource->GetOutputPort());

    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor( 1, 0, 0 );
    actor->GetProperty()->SetLineWidth(3.0);

    // Setup render window, renderer, and interactor
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderer->AddActor(actor);

    printf( "the count of spline points is %d\n", splinePoints->GetNumberOfPoints() );
    for( int i = 0; i < splinePoints->GetNumberOfPoints(); ++i )
    {
        vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
        sphere->SetRadius( 0.1 );

        vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        sphereMapper->SetInputConnection( sphere->GetOutputPort() );

        vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
        sphereActor->SetMapper( sphereMapper );
        sphereActor->GetProperty()->SetColor( 0, 0, 1 );
        sphereActor->VisibilityOn();

        sphereActor->SetPosition( splinePoints->GetPoint(i) );
        renderer->AddActor( sphereActor );

        // text 2D
        vtkSmartPointer<vtkTextSource> text2D =
                vtkSmartPointer<vtkTextSource>::New();
        text2D->SetText( QString::number( i+1 ).toStdString().c_str() );

        vtkSmartPointer<vtkTransform> text2DTransform =
                vtkSmartPointer<vtkTransform>::New();
        double *center = sphereActor->GetCenter();
        text2DTransform->Translate( center[0], center[1] + 0.1, center[2] );
        text2DTransform->Scale( 0.01, 0.01, 0.01 );

        vtkSmartPointer<vtkTransformPolyDataFilter> text2DDataFilter =
                vtkSmartPointer<vtkTransformPolyDataFilter>::New();
        text2DDataFilter->SetTransform( text2DTransform );
        text2DDataFilter->SetInputConnection( text2D->GetOutputPort() );

        vtkSmartPointer<vtkCoordinate> coords =
                vtkSmartPointer<vtkCoordinate>::New();
        coords->SetCoordinateSystemToWorld();

        vtkSmartPointer<vtkPolyDataMapper2D> text2DMapper =
                vtkSmartPointer<vtkPolyDataMapper2D>::New();
        text2DMapper->SetInputConnection( text2DDataFilter->GetOutputPort() );
        text2DMapper->SetTransformCoordinate( coords );

        vtkSmartPointer<vtkActor2D> text2DActor =
                vtkSmartPointer<vtkActor2D>::New();
        text2DActor->SetMapper( text2DMapper );

        renderer->AddActor( text2DActor );
    }

    renderer->SetBackground(colors->GetColor3d("Silver").GetData());

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

    return EXIT_SUCCESS;
}

现在,再创建一条线,使得两条线封闭。
接着,尝试将这些points作为平面的边线点,创造一个cell将points串起来,画出一个面。
这样沿着曲线画平面有一个问题:
曲面并不会100%的沿着边界点生成。
如下图:

在这里插入图片描述

在这里插入图片描述

实现代码:
#include <vtkSmartPointer.h>
#include <vtkParametricFunctionSource.h>
#include <vtkParametricSpline.h>

#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkProperty.h>

#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTransform.h>

#include <vtkSphereSource.h>
#include <vtkNamedColors.h>
#include <vtkTextSource.h>
#include <vtkCoordinate.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkActor2D.h>
#include <vtkPolyDataNormals.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>

#include <QString>
#include <iostream>
using namespace std;

int main(int, char *[])
{
    setbuf( stdout, NULL );
    vtkSmartPointer<vtkNamedColors> colors =
            vtkSmartPointer<vtkNamedColors>::New();

    double p[5][3] = { {0.0, 4.0, 0.0},
                       {2.0, 0.0, 0.0},
                       {4.0, 2.0, 0.0},
                       {6.0, 0.0, 0.0},
                       {8.0, 4.0, 0.0} };

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    for( int i = 0; i < 5; ++i )
    {
        points->InsertNextPoint( p[i] );
    }
    vtkSmartPointer<vtkParametricSpline> spline =
        vtkSmartPointer<vtkParametricSpline>::New();
    spline->SetPoints(points);

    double p2[5][3] = { {0.0, 4.0, 0.0},
                       {2.0, 4.0, 0.0},
                       {4.0, 4.0, 0.0},
                       {6.0, 4.0, 0.0},
                       {8.0, 4.0, 0.0} };
    for( int i = 0; i < 5; ++i )
    {
        points->InsertNextPoint( p2[4 - i] );
    }

/*
 * we can use this way to create some delta points on line.
 *
    vtkSmartPointer<vtkPoints> betaPoints = vtkSmartPointer<vtkPoints>::New();
    int index = 0;
    int countOfDeltaPoints = 500;
    double step = 1.0 / ( countOfDeltaPoints - 1 );
    for( double i = 0; i <= 1; i = i + step )
    {
        double tmp[3] = { i, 0, 0 };
        spline->Evaluate( tmp, tmp, NULL );
        betaPoints->InsertPoint( index++, tmp );
    }

    spline->SetPoints( betaPoints );

*/
    vtkSmartPointer<vtkParametricFunctionSource> functionSource =
        vtkSmartPointer<vtkParametricFunctionSource>::New();
    functionSource->SetParametricFunction(spline);
    functionSource->SetUResolution( 60 );   // generate 61 points.
    functionSource->GenerateNormalsOn();
    functionSource->Update();

    vtkPolyData* polyData = functionSource->GetOutput();
    vtkPoints* splinePoints = polyData->GetPoints();

    // Setup actor and mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(functionSource->GetOutputPort());

    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor( 1, 0, 0 );
    actor->GetProperty()->SetLineWidth(3.0);

    // Setup render window, renderer, and interactor
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderer->AddActor( actor );

    printf( "the count of spline points is %d\n", splinePoints->GetNumberOfPoints() );
    for( int i = 0; i < splinePoints->GetNumberOfPoints(); ++i )
    {
        vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
        sphere->SetRadius( 0.1 );

        vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        sphereMapper->SetInputConnection( sphere->GetOutputPort() );

        vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
        sphereActor->SetMapper( sphereMapper );
        sphereActor->GetProperty()->SetColor( 0, 0, 1 );
        sphereActor->VisibilityOn();

        sphereActor->SetPosition( splinePoints->GetPoint(i) );
        renderer->AddActor( sphereActor );

        // text 2D
        vtkSmartPointer<vtkTextSource> text2D =
                vtkSmartPointer<vtkTextSource>::New();
        text2D->SetText( QString::number( i+1 ).toStdString().c_str() );

        vtkSmartPointer<vtkTransform> text2DTransform =
                vtkSmartPointer<vtkTransform>::New();
        double *center = sphereActor->GetCenter();
        text2DTransform->Translate( center[0], center[1] + 0.1, center[2] );
        text2DTransform->Scale( 0.01, 0.01, 0.01 );

        vtkSmartPointer<vtkTransformPolyDataFilter> text2DDataFilter =
                vtkSmartPointer<vtkTransformPolyDataFilter>::New();
        text2DDataFilter->SetTransform( text2DTransform );
        text2DDataFilter->SetInputConnection( text2D->GetOutputPort() );

        vtkSmartPointer<vtkCoordinate> coords =
                vtkSmartPointer<vtkCoordinate>::New();
        coords->SetCoordinateSystemToWorld();

        vtkSmartPointer<vtkPolyDataMapper2D> text2DMapper =
                vtkSmartPointer<vtkPolyDataMapper2D>::New();
        text2DMapper->SetInputConnection( text2DDataFilter->GetOutputPort() );
        text2DMapper->SetTransformCoordinate( coords );

        vtkSmartPointer<vtkActor2D> text2DActor =
                vtkSmartPointer<vtkActor2D>::New();
        text2DActor->SetMapper( text2DMapper );

        renderer->AddActor( text2DActor );
    }

    // create cell to form plane
    vtkSmartPointer<vtkCellArray> poly = vtkSmartPointer<vtkCellArray>::New();
    const int length = splinePoints->GetNumberOfPoints();
    vtkIdType pts[ length ];
    for( int i = 0; i < length; ++i )
    {
        pts[i] = i;
    }
    poly->InsertNextCell( length, pts );

    vtkSmartPointer<vtkPolyData> planePd = vtkSmartPointer<vtkPolyData>::New();
    planePd->SetPoints( splinePoints );
    planePd->SetPolys( poly );
    vtkSmartPointer<vtkPolyDataMapper> planeMapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    planeMapper->SetInputData( planePd );

    vtkSmartPointer<vtkActor> planeActor =
            vtkSmartPointer<vtkActor>::New();
    planeActor->SetMapper( planeMapper );

    renderer->AddActor( planeActor );

    renderer->SetBackground(colors->GetColor3d("Silver").GetData());
    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

想要精确地画出曲面,可以使用微积分模型,利用这些边界点画出一个个小的四边形作为不同的cell,然后将这些不同的cell插入到vtkCellArray中。最后再构造vtkPolyData,mapper,actor等。

在这里插入图片描述

阅读更多

没有更多推荐了,返回首页