找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 766|回复: 1

[VTK] VTK例子5-样条曲线CSpline

[复制链接]

291

主题

401

回帖

2545

积分

管理员

积分
2545

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2015-12-4 22:24:39 | 显示全部楼层 |阅读模式
样条曲线CSpline
#include "stdafx.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkPolyData.h"
#include "vtkBoxWidget.h"
#include "vtkCardinalSpline.h"
#include "vtkPoints.h"
#include "stdio.h"
#include <time.h>
#include <vtkGlyph3D.h>
#include <vtkProperty.h>
#include <vtkCellArray.h>
#include <vtkTubeFilter.h>
#include <vtkPolyLine.h>
void main()
{
  srand(time(NULL)) ;
//  rand()/RAND_MAX;//    0 - RAND_MAX
  vtkRenderer *ren = vtkRenderer::New();
  vtkRenderWindow *renWindow = vtkRenderWindow::New();
    renWindow->AddRenderer(ren);
  renWindow->SetSize( 600, 600 );
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWindow);
  int numberOfInputPoints = 30;
    vtkCardinalSpline *aSplineX = vtkCardinalSpline::New();
    vtkCardinalSpline *aSplineY = vtkCardinalSpline::New();
    vtkCardinalSpline *aSplineZ = vtkCardinalSpline::New();
vtkPoints *inputPoints = vtkPoints::New();
double x,y,z;
int i;
for(i=0;i<numberOfInputPoints;i++)
{
    x = (float)rand()/(float)RAND_MAX;
    y = (float)rand()/(float)RAND_MAX;
    z = (float)rand()/(float)RAND_MAX;
    printf(" x:%f,y:%f,z:%f\n",x,y,z);
    aSplineX->AddPoint(i, x);
    aSplineY->AddPoint(i, y);
    aSplineZ->AddPoint(i, z);
    inputPoints->InsertPoint(i, x, y, z);
}
vtkPolyData *inputData =vtkPolyData::New();
inputData->SetPoints(inputPoints);
vtkSphereSource *balls =vtkSphereSource::New();
balls->SetRadius(.01);
balls->SetPhiResolution(10);
balls->SetThetaResolution(10);
vtkGlyph3D *glyphPoints =vtkGlyph3D::New();
glyphPoints->SetInput(inputData);
glyphPoints->SetSource(balls->GetOutput());
vtkPolyDataMapper *glyphMapper = vtkPolyDataMapper::New();
glyphMapper->SetInputConnection(glyphPoints->GetOutputPort());
vtkActor *glyph = vtkActor::New();
glyph->SetMapper(glyphMapper);
glyph->GetProperty()->SetDiffuseColor(1,0,0);
glyph->GetProperty()->SetSpecular(.3);
glyph->GetProperty()->SetSpecularPower(30);
///////////////
vtkPoints *points = vtkPoints::New();
// Number of points on the spline
int numberOfOutputPoints = 400;
float t;
for(i=0;i<numberOfOutputPoints;i++)
{
    t = (numberOfInputPoints-1.0)/(numberOfOutputPoints-1.0)*i;
    points->InsertPoint(i, aSplineX->Evaluate(t), aSplineY->Evaluate(t),
                       aSplineZ->Evaluate(t));
    printf(" point:%f, %f, %f; \n",points->GetPoint(i)[0],
                                    points->GetPoint(i)[1],points->GetPoint(i)[2]);
}
vtkCellArray *lines = vtkCellArray::New();
    int a=lines->InsertNextCell(numberOfOutputPoints);
    for(i=0;i<numberOfOutputPoints;i++)
    {
         lines->InsertCellPoint(i);
    }
vtkPolyData *profileData = vtkPolyData::New();
profileData->SetPoints(points);
profileData->SetLines(lines);
// Add thickness to the resulting line.
vtkTubeFilter *profileTubes = vtkTubeFilter::New();
profileTubes->SetNumberOfSides(8);
profileTubes->SetInput(profileData);
profileTubes->SetRadius(0.005);
vtkPolyDataMapper *profileMapper = vtkPolyDataMapper::New();
profileMapper->SetInputConnection(profileTubes->GetOutputPort());
vtkActor *profile = vtkActor::New();
profile->SetMapper(profileMapper);
profile->GetProperty()->SetDiffuseColor(1,1,0);
profile->GetProperty()->SetSpecular(0.3);
profile->GetProperty()->SetSpecularPower(30);
//////////////
ren->AddActor(glyph);
ren->AddActor(profile);
iren->Initialize();
renWindow->Render();
iren->Start();
}

样条曲线CSpline

样条曲线CSpline
回复

使用道具 举报

62

主题

413

回帖

1345

积分

等待验证会员

积分
1345

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-12 11:05:46 | 显示全部楼层
好好好好哈
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|关于我们|医维基|网站地图|Archiver|手机版|医疗之家 ( 沪ICP备2023001278号-1 )

GMT+8, 2025-4-11 08:07 , Processed in 0.699591 second(s), 30 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表