⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vtk.cpp

📁 VTK入门样例(种类齐全)
💻 CPP
📖 第 1 页 / 共 3 页
字号:

//自行完成了线的实现
#include "stdafx.h"

//#include <cstdio>
#include <iostream>
#include <string>
#include "vtkScalarsToColors.h"
#include "vtkActor.h"
#include "vtkInteractorEventRecorder.h"
#include <vtkOrientationMarkerWidget.h>
#include <vtkPropAssembly.h>
#include <vtkPlaneSource.h>
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkAppendPolyData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkAxesActor.h"
#include <vtkCamera.h>
#include <vtkTextProperty.h>
#include <vtkProperty.h>
#include <vtkCaptionActor2D.h>
#include <vtkTextActor.h>
#include <vtkLogLookupTable.h>
#include <vtkScalarBarActor.h>
#include <vtkWarpScalar.h>
#include <vtkGeometryFilter.h>
#include <vtkOutlineFilter.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkElevationFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkBandedPolyDataContourFilter.h>
#include <vtkCubeAxesActor2D.h>
#include <vtkLODActor.h>
#include <vtkPlane.h>
#include "vtkOrientationMarkerWidget.h"
#include <vtkClipPolyData.h>
#include <vtkImplicitPlaneWidget.h>
#include "vtkCommand.h"
#include "vtkRegressionTestImage.h"
#include "vtkDebugLeaks.h"
#include <vtkXYPlotActor.h>
#include <vtkProperty2D.h>
#include <vtkCutter.h>
#include <vtkOutlineFilter.h>
#include <vtkProgrammableFilter.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkElevationFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkBandedPolyDataContourFilter.h>
#include <vtkCubeAxesActor2D.h>
#include "vtkPlaneWidget.h"
#include "vtkXYPlotActor.h"
#include <vtkRectilinearGrid.h> 
#include "vtkSphereSource.h"
#include "vtkGlyph3D.h"

using namespace std;

void show(double x[],double y[],double z[],double sxb[],double syb[],double szb[],double sxend[],double syend[],double szend[])
{
	double xyz[3];
	vtkRenderer *ren1 = vtkRenderer::New();
	vtkRenderWindow *renWin = vtkRenderWindow::New();
	renWin->AddRenderer(ren1);
	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWin);
	/////////////////////////////////////点可视化
	vtkSphereSource *sphere = vtkSphereSource::New();
	sphere->SetThetaResolution(7); 
	sphere->SetPhiResolution(7);
	vtkPoints *newPts = vtkPoints::New();
	newPts->SetNumberOfPoints(13);
	for (int i = 0 ;i<13;i++)
	{
		xyz[0] =x[i];
		xyz[1] =y[i];
		xyz[2] =z[i];
		newPts->SetPoint(i,xyz);
	}


	vtkPolyData *model=vtkPolyData::New();
	model-> SetPoints( newPts);

	vtkGlyph3D *glyph = vtkGlyph3D::New();
	glyph->SetInput(model);
	glyph->SetSource(sphere->GetOutput());
	glyph->SetVectorModeToUseNormal();
	glyph->SetScaleModeToScaleByVector();
	glyph->SetScaleFactor(20.25);
	vtkPolyDataMapper *spikeMapper = vtkPolyDataMapper::New();
	spikeMapper->SetInput(glyph->GetOutput());
	vtkActor *spikeActor = vtkActor::New();
	spikeActor->SetMapper(spikeMapper);
	spikeActor-> GetProperty() ->SetDiffuseColor( 1 ,0 ,0);

	vtkPoints *points =vtkPoints::New();
	points->SetNumberOfPoints(108);
	int index = 0;
	double xyz1[3],xyz2[3];
	for (int i = 0 ;i<54;i++)
	{
		xyz1[0] = sxb[i];
		xyz1[1] = syb[i];
		xyz1[2] =  szb[i];
		xyz2[0] = sxend[i];
		xyz2[1] = syend[i];
		xyz2[2] = szend [i];
		points->SetPoint(index,xyz1);
		index++;
		points->SetPoint(index,xyz2);
		index++;
	}
	vtkCellArray *line = vtkCellArray ::New();
	line->InsertNextCell(108);
	for (int i = 0 ;i<108;i++)
	{
		line->InsertCellPoint(i);
	}
	vtkPolyData *model1=vtkPolyData::New();
	model1-> SetPoints( points);
	model1->SetLines(line);
	vtkPolyDataMapper *spikeMapper1 = vtkPolyDataMapper::New();
	spikeMapper1->SetInput(model1);
	vtkActor *spikeActor1 = vtkActor::New();
	spikeActor1->SetMapper(spikeMapper1);
	spikeActor1-> GetProperty() ->SetDiffuseColor( 0 ,0 ,0);
	//////////////////////////////////////////////////////点
	vtkPolyData *model2=vtkPolyData::New();
	model2-> SetPoints( points);
	vtkSphereSource *sphere1 = vtkSphereSource::New();
	sphere1->SetThetaResolution(7); 
	sphere1->SetPhiResolution(7);

	vtkGlyph3D *glyph2 = vtkGlyph3D::New();
	glyph2->SetInput(model2);
	glyph2->SetSource(sphere1->GetOutput());
	glyph2->SetVectorModeToUseNormal();
	glyph2->SetScaleModeToScaleByVector();
	glyph2->SetScaleFactor(20.25);
	vtkPolyDataMapper *spikeMapper2 = vtkPolyDataMapper::New();
	spikeMapper2->SetInput(glyph2->GetOutput());
	vtkActor *spikeActor2 = vtkActor::New();
	spikeActor2->SetMapper(spikeMapper2);
	spikeActor2-> GetProperty() ->SetDiffuseColor(0 ,0 ,1);


	ren1->AddActor(spikeActor);
	ren1->AddActor(spikeActor1);
	ren1->AddActor(spikeActor2);
	ren1->SetBackground(1,1,1);
	renWin->SetSize(600,400);

	renWin->Render();
	ren1->GetActiveCamera()->Zoom(0.4);
	renWin->Render();

	iren->Start();

	ren1->Delete();
	renWin->Delete();
	iren->Delete();
	sphere->Delete();
	glyph->Delete();
	spikeMapper->Delete();
	spikeActor->Delete();
}



int main()
{
////////////读取pop3dout.txt
	FILE	*infp;
	if ((infp	=	fopen("POP3Dout.txt","r"))	==	NULL)
	{
		printf("Read file error.");
		system("pause");
		exit(0);
	}
	int index=0;
	int i=0 ;
	char	line[1024];
	char	*str;
	double  sxb[54];
	double  syb[54];
	double  szb[54];
	double  sxend[54];
	double  syend[54];
	double  szend[54];
	while(!feof(infp))
	{
		fgets(line, 1024, infp);
		//fscanf(infp,	"%s", &line);
		if (line[0] == 'R')
		{
			/*const char *split1 = "Ray:(,)to{}";
			str	=	strtok(line,split1);
			while(str!=NULL)
			{
			cout << str << endl;
			str	=	strtok(NULL,split1);
			}*/
		}
		else if (line[0] == 'S')
		{
			const char *split1 = "Segment:(,)to";
			str	=	strtok(line,split1);
			while(str!=NULL)
			{
				if (index ==1)
				{
					sxb[i]=atof(str);
					if (sxb[i]>200)
					{
						sxb[i]=200;
					}
					if (sxb[i]<-200)
					{
						sxb[i]=-200;
					}
					cout<<sxb[i]<<endl;
				}
					else if (index ==2)
				 {
					 syb[i]=atof(str);
					 if (syb[i]>200)
					 {
						 syb[i]=200;
					 }
					 if (syb[i]<-200)
					 {
						 syb[i]=-200;
					 }				
					 cout<<syb[i]<<endl;
				 }
					else if (index ==3)
				 {
					 szb[i]=atof(str);
					 if (szb[i]>200)
					 {
						 szb[i]=200;
					 }
					 if (szb[i]<-200)
					 {
						 szb[i]=-200;
					 }
					 cout<<szb[i]<<endl;
				 }
					else if (index ==6)
					{
						sxend[i] =atof(str);
						if (sxend[i]>200)
						{
							sxend[i]=200;
						}
						if (sxend[i]<-200)
						{
							sxend[i]=-200;
						}
						 cout<<sxend[i]<<endl;
					}
					else if (index ==7)
					{
						syend[i] =atof(str);
						if (syend[i]>200)
						{
							syend[i]=200;
						}
						if (syend[i]<-200)
						{
							syend[i]=-200;
						}
						cout<<syend[i]<<endl;
					}
					else if (index ==8)
					{
						szend[i] =atof(str);
						if (szend[i]>200)
						{
							szend[i]=200;
						}
						if (szend[i]<-200)
						{
							szend[i]=-200;
						}
						cout<<szend[i]<<endl;
					}
				str =strtok(NULL,split1);
				++index;
			 }
			index = 0;
			i++;
		}
	
	}
	///////////////读取pop3d.txt
	FILE	*infp1;
	if ((infp1	=	fopen("POP3D.txt","r"))	==	NULL)
	{
		printf("Read file error.");
		system("pause");
		exit(0);
	}
	char	line1[1024];
	char	*str1;

	double x[13];
	double y[13];
	double z[13];
 i=0 ;
	while(!feof(infp1))
	{
		fgets(line1, 1024, infp1);
		index = 0;
		str1=strtok(line1," ");
		while(str1!=NULL)
		{
			if (index ==0)
			{
			  x[i]=atof(str1);
			   cout<<x[i]<<endl;
			}
			else if (index ==1)
			 {
				 y[i]=atof(str1);
				 cout<<y[i]<<endl;
			 }
			 else if (index ==2)
			 {
				z[i]=atof(str1);
				cout<<z[i]<<endl;
			 }
	
			str1 =strtok(NULL," ");
			++index;
		}
		i++;
		
	}
   show(x,y,z,sxb,syb,szb,sxend,syend,szend);
	return 0;
}

//#include "vtkRenderer.h"
//#include "vtkRenderWindow.h"
//#include "vtkRenderWindowInteractor.h"
//#include "vtkSphereSource.h"
//#include "vtkPolyDataMapper.h"
//#include "vtkActor.h"
//#include "vtkConeSource.h"
//#include "vtkGlyph3D.h"
//#include "vtkCamera.h"
//#include "vtkPoints.h"
//#include "vtkPolyData.h"
//#include "vtkProperty.h"
//#include <vtkCellArray.h> 
//
//void main( int argc, char *argv[] )
//{
//	// create the rendering objects
//	vtkRenderer *ren1 = vtkRenderer::New();
//	vtkRenderWindow *renWin = vtkRenderWindow::New();
//	renWin->AddRenderer(ren1);
//	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
//	iren->SetRenderWindow(renWin);
//
//	// create the pipline, ball and spikes
//	vtkSphereSource *sphere = vtkSphereSource::New();
//	//sphere->SetThetaResolution(7); 
//	//sphere->SetPhiResolution(7);
//
//	vtkPoints *points=vtkPoints::New();
//	/*points->InsertNextPoint( 10.48, -187.4, -126.9);
//	points->InsertNextPoint( 12.81, -187.7, -126.4 );
//	points->InsertNextPoint( 15.44 ,-187.9, -125.9 );
//	points->InsertNextPoint( 18.02, -187.7, -125.7 );
//	points->InsertNextPoint( 20.23, -187.6, -125.5 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -