vtk中没有可以直接在三维空间中绘制三维空间圆的类以及函数,在这里,编写了以函数以任意的三维点为中心,任意半径,绘制平行于Z=0平面的圆,
可以用于批量绘制三维空间的圆。也可改写为平行为X=0或者Y=0的平面。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define M_PI 3.14159265358979323846
typedef struct My3Dpoint
{
double x;
double y;
double z;
};
//m_point为三维点,radius为绘制半径,polydata为返回的值
void GenerateCircle(My3Dpoint &m_point,const double& radius,vtkPolyData* polyData)
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cells = vtkSmartPointer::New();
vectoryuanzhoudian;
My3Dpoint temp;
temp.x=0;
temp.y=0;
temp.z=0;
double Pi=M_PI;
double Xtranslate,Ytranslate;
Xtranslate=m_point.x+radius;
Ytranslate=m_point.y;
double a=0;
for (int i=0;i<360;++i)
{
temp.x=(Xtranslate-m_point.x)*cos((a*Pi)/180)+(Ytranslate-m_point.y)*sin((a*Pi)/180)+m_point.x;
temp.y=(Xtranslate-m_point.x)*sin((a*Pi)/180)+(Ytranslate-m_point.y)*cos((a*Pi)/180)+m_point.y;
temp.z=m_point.z;
cout<InsertPoint(i,temp.x,temp.y,temp.z);
cells->InsertNextCell(1);
cells->InsertCellPoint(i);
a=a+1;
}
vtkSmartPointerlines=vtkSmartPointer::New();
for (int i=0;i line =vtkSmartPointer::New();
line->GetPointIds()->SetId(0,i);
line->GetPointIds()->SetId(1,i+1);
lines->InsertNextCell(line);
}
else
{
vtkSmartPointer line =vtkSmartPointer::New();
line->GetPointIds()->SetId(0,i);
line->GetPointIds()->SetId(1,1);
lines->InsertNextCell(line);
}
}
polyData->SetPoints(points);
polyData->SetVerts(cells);
polyData->SetLines(lines);
}
int main()
{
vtkSmartPointerren1=vtkSmartPointer::New();
vtkSmartPointerrenWin=vtkSmartPointer::New();
vtkSmartPointeriren=vtkSmartPointer::New();
vtkSmartPointerm_polydata=vtkSmartPointer::New();
My3Dpoint mypoint;
mypoint.x=100;
mypoint.y=20;
mypoint.z=50;
GenerateCircle(mypoint,20,m_polydata);
vtkSmartPointerm_polydataMapper=vtkSmartPointer::New();
m_polydataMapper->SetInput(m_polydata);
vtkSmartPointerm_Actor=vtkSmartPointer::New();
m_Actor->SetMapper(m_polydataMapper);
m_Actor->GetProperty()->SetColor(1,0,0);
ren1->AddActor(m_Actor);
renWin->AddRenderer(ren1);
renWin->SetSize(500,500);
iren->SetRenderWindow(renWin);
renWin->Render();
iren->Start();
return 0;
}
参考代码为:在三维空间中Z=给定值平面绘制半径为给定值的圆,圆内部填充
//z为Z坐标值,radius为半径,resolution为分辨率
void CreateCircle( const double& z, const double& radius, const int& resolution, vtkPolyData* polyData )
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cells = vtkSmartPointer::New();
points->SetNumberOfPoints( resolution );
cells->Allocate( 1, resolution );
cells->InsertNextCell( resolution );
for( int i = 0 ; i < resolution; ++i ) {
double theta = vtkMath::RadiansFromDegrees(360.*i/double(resolution));
double x = radius*cos(theta);
double y = radius*sin(theta);
points->SetPoint( i, x, y, z );
cells->InsertCellPoint( i );
}
polyData->Initialize();
polyData->SetPolys( cells );
polyData->SetPoints( points );
}
当我同时想在三维空间中绘制多个圆时,可以采用下列代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define M_PI 3.14159265358979323846
typedef struct My3Dpoint
{
double x;
double y;
double z;
};
//m_point为三维点,radius为绘制半径,polydata为返回的值
void CreateCircle( const double& z, const double& radius, const int& resolution, vtkPolyData* polyData )
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cells = vtkSmartPointer::New();
points->SetNumberOfPoints( resolution );
cells->Allocate( 1, resolution );
cells->InsertNextCell( resolution );
for( int i = 0 ; i < resolution; ++i ) {
double theta = vtkMath::RadiansFromDegrees(360.*i/double(resolution));
double x = radius*cos(theta);
double y = radius*sin(theta);
points->SetPoint( i, x, y, z );
cells->InsertCellPoint( i );
}
polyData->Initialize();
polyData->SetPolys( cells );
polyData->SetPoints( points );
}
void GenerateCircle(My3Dpoint &m_point,const double& radius,vtkPolyData* polyData)
{
vtkSmartPointer points = vtkSmartPointer::New();
vtkSmartPointer cells = vtkSmartPointer::New();
vectoryuanzhoudian;
My3Dpoint temp;
temp.x=0;
temp.y=0;
temp.z=0;
double Pi=M_PI;
double Xtranslate,Ytranslate;
Xtranslate=m_point.x+radius;
Ytranslate=m_point.y;
double a=0;
for (int i=0;i<360;++i)
{
temp.x=(Xtranslate-m_point.x)*cos((a*Pi)/180)+(Ytranslate-m_point.y)*sin((a*Pi)/180)+m_point.x;
temp.y=(Xtranslate-m_point.x)*sin((a*Pi)/180)+(Ytranslate-m_point.y)*cos((a*Pi)/180)+m_point.y;
temp.z=m_point.z;
cout<InsertPoint(i,temp.x,temp.y,temp.z);
cells->InsertNextCell(1);
cells->InsertCellPoint(i);
a=a+1;
}
vtkSmartPointerlines=vtkSmartPointer::New();
for (int i=0;i line =vtkSmartPointer::New();
line->GetPointIds()->SetId(0,i);
line->GetPointIds()->SetId(1,i+1);
lines->InsertNextCell(line);
}
else
{
vtkSmartPointer line =vtkSmartPointer::New();
line->GetPointIds()->SetId(0,i);
line->GetPointIds()->SetId(1,1);
lines->InsertNextCell(line);
}
}
polyData->SetPoints(points);
polyData->SetVerts(cells);
polyData->SetLines(lines);
}
int main()
{
vtkSmartPointerren1=vtkSmartPointer::New();
vtkSmartPointerrenWin=vtkSmartPointer::New();
vtkSmartPointeriren=vtkSmartPointer::New();
for (int i=0;i<100;i=i+5)
{
vtkSmartPointerm_polydata=vtkSmartPointer::New();
My3Dpoint mypoint;
mypoint.x=100;
mypoint.y=20;
mypoint.z=50;
GenerateCircle(mypoint,i,m_polydata);
//CreateCircle(100,100,30,m_polydata);
vtkSmartPointerm_polydataMapper=vtkSmartPointer::New();
m_polydataMapper->SetInput(m_polydata);
vtkSmartPointerm_Actor=vtkSmartPointer::New();
m_Actor->SetMapper(m_polydataMapper);
m_Actor->GetProperty()->SetColor(1,0,0);
ren1->AddActor(m_Actor);
}
renWin->AddRenderer(ren1);
renWin->SetSize(500,500);
iren->SetRenderWindow(renWin);
renWin->Render();
iren->Start();
return 0;
}