#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
#include "vtkPolyDataMapper.h"
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPoints.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkFloatArray.h"
#include "vtkPolyData.h"
#include "vtkDataSetMapper.h"
#include "vtkActor2D.h"
#include "vtkContourFilter.h"
#include "vtkContourValues.h"
#include "vtkUnstructuredGrid.h"
#include "vtkPointData.h"
#include "vtkPolyVertex.h"
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCellArray.h>
#include "vtkDelaunay2D.h"
#include "vtkMath.h"
void myShow(vtkDelaunay2D* 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()->SetPointSize(1);
anActor->GetProperty()->SetColor(1,0,1);
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<vtkPoints> points=vtkSmartPointer<vtkPoints>::New();
unsigned int gridXSize=60,gridYSize=60;
double R=60;//球面曲率半径
double r=0;//任意x、y坐标点,到(0,0)点的距离
double r2=r*r;
double rCrown=gridXSize/2;
double rCrown2=rCrown*rCrown;//球冠半径的平方
double hmax2=R*R-rCrown2;//最大弧矢高的平方
double hmax=R-sqrt(hmax2);//最大弧矢高
for(double i=0;i<gridXSize;i++)
{
for(double j=0;j<gridYSize;j++)
{
double x=(i-gridXSize/2),y=(j-gridYSize/2);
r2=x*x+y*y;//r2表示r的平方
r=sqrt(r2);
double z=0;
if(r<rCrown)
//不知道为什么,要在后面多加一个最大弧矢高hmax,才能得到正确的球冠,大概是坐标方向问题吧
{
z=sqrt(R*R-r2)-(R-hmax)+hmax;
points->InsertNextPoint(i,j,z);
}
else
z=hmax;
}
}
vtkSmartPointer<vtkPolyData> polydata=vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
vtkSmartPointer<vtkDelaunay2D> delaunay=vtkSmartPointer<vtkDelaunay2D>::New();
delaunay->SetInputData(polydata);
delaunay->Update();
//在窗口中显示
myShow(delaunay);
return 0;
}
时间: 2024-10-15 02:47:06