【VTK】不均匀样条插值

网友投稿 594 2022-08-27

【VTK】不均匀样条插值

【VTK】不均匀样条插值

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

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "tool.h"using namespace std;int main(int, char *[]){ vtkSmartPointer colors = vtkSmartPointer::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 points = vtkSmartPointer::New(); for( int i = 0; i < 3; ++i ) { points->InsertNextPoint( p[i].point ); } vtkSmartPointer spline = vtkSmartPointer::New(); spline->SetPoints( points ); vtkSmartPointer betaPoints = vtkSmartPointer::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 polyData = vtkSmartPointer::New(); vtkPoints* splinePoints = betaPoints; vtkSmartPointer lines = vtkSmartPointer::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 mapper = vtkSmartPointer::New(); mapper->SetInputData( polyData ); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper( mapper ); actor->GetProperty()->SetColor( 1, 0, 0 ); actor->GetProperty()->SetLineWidth(3.0); // Setup render window, renderer, and interactor vtkSmartPointer renderer = vtkSmartPointer::New(); vtkSmartPointer renderWindow = vtkSmartPointer::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer renderWindowInteractor = vtkSmartPointer::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 sphere = vtkSmartPointer::New(); sphere->SetRadius( 0.1 ); vtkSmartPointer sphereMapper = vtkSmartPointer::New(); sphereMapper->SetInputConnection( sphere->GetOutputPort() ); vtkSmartPointer sphereActor = vtkSmartPointer::New(); sphereActor->SetMapper( sphereMapper ); sphereActor->GetProperty()->SetColor( 0, 0, 1 ); sphereActor->VisibilityOn(); sphereActor->SetPosition( splinePoints->GetPoint(i) ); renderer->AddActor( sphereActor ); // text 2D vtkSmartPointer text2D = vtkSmartPointer::New(); text2D->SetText( QString::number( i+1 ).toStdString().c_str() ); vtkSmartPointer text2DTransform = vtkSmartPointer::New(); double *center = sphereActor->GetCenter(); text2DTransform->Translate( center[0], center[1] + 0.1, center[2] ); text2DTransform->Scale( 0.03, 0.03, 0.03 ); vtkSmartPointer text2DDataFilter = vtkSmartPointer::New(); text2DDataFilter->SetTransform( text2DTransform ); text2DDataFilter->SetInputConnection( text2D->GetOutputPort() ); vtkSmartPointer coords = vtkSmartPointer::New(); coords->SetCoordinateSystemToWorld(); vtkSmartPointer text2DMapper = vtkSmartPointer::New(); text2DMapper->SetInputConnection( text2DDataFilter->GetOutputPort() ); text2DMapper->SetTransformCoordinate( coords ); vtkSmartPointer text2DActor = vtkSmartPointer::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.804806du1: 0.012200, du2: 0.0536540, 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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:趣文:编程语言伪简史(伪代码是编程语言吗)
下一篇:【QT】Create subdirs project with qt creator
相关文章

 发表评论

暂时没有评论,来抢沙发吧~