VTK初学一,a Mesh from vtkImageData—球冠

#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
#include <vtkVersion.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkMath.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageData.h>
#include <vtkGreedyTerrainDecimation.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractionWidgetsModule.h>
void myShow(vtkGreedyTerrainDecimation* anInput)
{
    vtkSmartPointer<vtkPolyDataMapper> aMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
    aMapper->SetInputConnection(anInput->GetOutputPort());
    aMapper->ScalarVisibilityOn();
    vtkSmartPointer<vtkActor> anActor=vtkSmartPointer<vtkActor>::New();
    anActor->SetMapper(aMapper);
    anActor->GetProperty()->SetInterpolationToFlat();
    anActor->GetProperty()->EdgeVisibilityOn();
    anActor->GetProperty()->SetEdgeColor(1,0,0);
    vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin=vtkSmartPointer<vtkRenderWindow>::New();
    ren1->AddActor(anActor);
    ren1->ResetCamera();
    ren1->SetBackground(1,1,1);
    renWin->AddRenderer(ren1);
    renWin->SetSize(512,512);
    vtkSmartPointer<vtkRenderWindowInteractor> iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style=vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetRenderWindow(renWin);
    iren->SetInteractorStyle(style);
    iren->Start();
}
int main()
{
    vtkSmartPointer<vtkImageData> image=vtkSmartPointer<vtkImageData>::New();
    image->SetDimensions(120,120,1);
    image->AllocateScalars(VTK_UNSIGNED_CHAR,1);
    int dims[3];
//    double max=sqrt(dims[0]*dims[0]+dims[1]*dims[1]);
    image->GetDimensions(dims);
    double R=100;//球面半径
    double r=0;//任意x、y坐标点,到(0,0)点的距离
    double r2=r*r;
    double rCrown=dims[0]/2;
    double rCrown2=rCrown*rCrown;//球冠半径的平方
    double hmax2=R*R-rCrown2;//最大弧矢高的平方
    double hmax=R-sqrt(hmax2);//最大弧矢高
    for(double i=0;i<dims[0];i++)
    {
        for(double j=0;j<dims[1];j++)
        {
            unsigned char* pixel=static_cast<unsigned char*>(image->GetScalarPointer(i,j,0));
            double x=(i-dims[0]/2),y=(j-dims[1]/2);
            r2=x*x+y*y;
            r=sqrt(r2);
            if(r<rCrown)
                //不知道为什么,要在后面多加一个最大弧矢高hmax,才能得到正确的球冠,大概是坐标方向问题吧
                pixel[0]=sqrt(R*R-r2)-(R-hmax)+hmax;
            else
                pixel[0]=hmax;
        }
    }
    vtkSmartPointer<vtkGreedyTerrainDecimation>decimation=vtkSmartPointer<vtkGreedyTerrainDecimation>::New();
    decimation->SetInputData(image);
    decimation->Update();
    //可视化
    myShow(decimation);
    return 0;
}

 

 
 
 

转载于:https://www.cnblogs.com/phoenixdsg/p/6120500.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值