VTK不均匀样条插值

Posted theArcticOcean

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VTK不均匀样条插值相关的知识,希望对你有一定的参考价值。

和上一篇文章【VTK】create spline points 一样,讨论spline points的生成。这一次,利用方法 void Evaluate(double u[3], double Pt[3], double Du[9]) override; 由于,u[0]代表线段与线段长累积的比率,通过控制U,达到控制插值点密度的目的。也可以保证特定的几个点位置不变,在其他部分插值。

#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>
#include "tool.h"
using namespace std;

int main(int, char *[])

    vtkSmartPointer<vtkNamedColors> colors =
            vtkSmartPointer<vtkNamedColors>::New();

    PointStruct p[3];
    p[0] = PointStruct( 0.0, 0.0, 0.0 );
    p[1] = PointStruct( 2.0, 4.0, 0.0 );
    p[2] = PointStruct( 20.0, 0.0, 0.0 );
    double len1 = PointStruct( p[0] - p[1] ).Length();
    double len2 = PointStruct( p[1] - p[2] ).Length();
    double u1 = len1 / (len1 + len2);
    double u2 = len2 / (len1 + len2);

    printf( "u1: %lf, u2: %lf\\n", u1, u2 );

    int rowCount = 32;
    double divider = rowCount / 2;
    double du1 = u1 / divider;
    double du2 = u2 / (divider - 1.0);
    printf( "du1: %lf, du2: %lf\\n", du1, du2 );

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    for( int i = 0; i < 3; ++i )
    
        points->InsertNextPoint( p[i].point );
    

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

    vtkSmartPointer<vtkPoints> betaPoints =
            vtkSmartPointer<vtkPoints>::New();

    double u = 0;
    int count = 0;

    betaPoints->InsertNextPoint( p[0].point );
    cout << count++ << ", " << p[0] << endl;
    u = du1;

    for( int i = 0; i < rowCount / 2 - 1; ++i )
    
        PointStruct tmp( u, 0, 0 );
        spline->Evaluate( tmp.point, tmp.point, NULL );
        betaPoints->InsertNextPoint( tmp.point );
        cout << count++ << ", " << tmp << endl;

        u = u + du1;
    

    betaPoints->InsertNextPoint( p[1].point );
    cout << count++ << ", " << p[1] << endl;
    u = u1 + du2;

    for( int i = 0; i < rowCount / 2 - 2; ++i )
    
        PointStruct tmp( u, 0, 0 );
        spline->Evaluate( tmp.point, tmp.point, NULL );
        betaPoints->InsertNextPoint( tmp.point );
        cout << count++ << ", " << tmp << endl;

        u = u + du2;
    
    betaPoints->InsertNextPoint( p[2].point );
    cout << count++ << ", " << p[2] << endl;

    vtkSmartPointer<vtkPolyData> polyData =
            vtkSmartPointer<vtkPolyData>::New();
    vtkPoints* splinePoints = betaPoints;
    vtkSmartPointer<vtkCellArray> lines =
            vtkSmartPointer<vtkCellArray>::New();
    for( int i = 0; i < rowCount-1; ++i )
    
        vtkIdType pts[2] =  i, i+1 ;
        lines->InsertNextCell(2, pts );
    

    polyData->SetPoints( betaPoints );
    polyData->SetLines( lines );

    // Setup actor and mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData( polyData );

    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.03, 0.03, 0.03 );

        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;

output:

u1: 0.195194, u2: 0.804806
du1: 0.012200, du2: 0.053654
0, PointStruct [0, 0, 0]
1, PointStruct [0.00893809, 0.0282784, 0]
2, PointStruct [0.0354522, 0.109739, 0]
3, PointStruct [0.0790921, 0.239321, 0]
...
30, PointStruct [19.8338, 0.128694, 0]
31, PointStruct [20, 0, 0]
the count of spline points is 32

以上是关于VTK不均匀样条插值的主要内容,如果未能解决你的问题,请参考以下文章

三次hermite样条曲线 和 三次B样条曲线有啥区别和联系

(5)三次样条和B样条

三次样条插值法

图形学插值函数理解与联系

图形学插值函数理解与联系

图形学插值函数理解与联系