医站点医维基

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1113|回复: 4

[VTK] VTK例子8-彩色样条曲线CSpline

[复制链接]

336

主题

410

回帖

2806

积分

管理员

积分
2806

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

发表于 2015-12-4 22:31:11 | 显示全部楼层 |阅读模式
彩色样条曲线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>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkLookupTable.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);

       int N= profileData->GetNumberOfPoints();

      vtkFloatArray *Scalar=vtkFloatArray::New();
       Scalar-> SetNumberOfTuples(N);
       Scalar-> SetNumberOfComponents (1);
      for(i=0;i<N;i++)
      {
       Scalar-> SetValue (i,i/400.0);
      }
    //   ${cell}Scalar SetValue 0 4

      profileData->GetPointData()-> SetScalars (Scalar);



    // Add thickness to the resulting line.
    vtkTubeFilter *profileTubes = vtkTubeFilter::New();
    profileTubes->SetNumberOfSides(8);
    profileTubes->SetInput(profileData);
    profileTubes->SetRadius(0.005);

    vtkLookupTable *lut=vtkLookupTable::New();
         lut-> SetNumberOfColors (256);
         lut-> Build();
         for (i= 0;i<128;i++)
         {
             lut-> SetTableValue(i, i/128.0, (128.0-i)/128.0, (64.0-i)/128.0, 1);
         }
         for (i= 128;i<256;i++)
         {
             lut-> SetTableValue (i, (i-128.0)/128.0, (i-128.0)/128.0, (256.0-i)/128.0, 1);
         }

    vtkPolyDataMapper *profileMapper = vtkPolyDataMapper::New();
    profileMapper->SetInputConnection(profileTubes->GetOutputPort());
    profileMapper->SetLookupTable(lut);

    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
回复

使用道具 举报

0

主题

396

回帖

791

积分

高级会员

积分
791

最佳新人

发表于 2016-5-12 19:08:26 | 显示全部楼层
先看看先。。。
回复

使用道具 举报

24

主题

462

回帖

1231

积分

版主

积分
1231

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

发表于 2016-5-18 13:44:25 | 显示全部楼层
回复

使用道具 举报

0

主题

399

回帖

804

积分

高级会员

积分
804

最佳新人

发表于 2016-5-23 20:04:24 | 显示全部楼层
楼主太伟大了
回复

使用道具 举报

26

主题

419

回帖

1128

积分

版主

积分
1128

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

发表于 2016-5-24 01:31:21 | 显示全部楼层
很想看看啊!!!!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 12:00 , Processed in 0.350685 second(s), 40 queries .

Designed by Medical BBS

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