vtksample

// Demo_3_Show_PointCloud.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 功能:显示简单的三维坐标点集

#include <iostream>
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkPolyDataMapper.h"
#include "vtkInteractorStyle.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCellArray.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkNew.h"

#include <vtkLineSource.h>
#include <vtkIdFilter.h>
#include <vtkCellData.h>
#include <vtkCellLocator.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkUnstructuredGridWriter.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>
#include <vtkLookupTable.h>
#include <vtkColorTransferFunction.h>
#include <vtkScalarBarWidget.h>
#include <vtkScalarBarActor.h>
#include <vtkTextProperty.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkScalarBarRepresentation.h>
#include <vtkcommand.h>
#include <vtkCoordinate.h>

template<typename T>
class vtkNew1 :public vtkNew<T> {
public:
	using vtkNew::vtkNew;
	//
	operator T*() const {
		return vtkNew::operator->();
	}
};

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);

#include <windows.h>
#include <vtkUnicodeString.h>
#include <string>
#include <vtkPolyDataReader.h>
#include <vtkPolyDataNormals.h>
#include <vtkVectorDot.h>
#include <vtkWarpVector.h>
#include <vtkVector.h>
#include <vtkPNGWriter.h>
#include <vtkWindowToImageFilter.h>
using namespace std;

string GbkToUtf8(const char *src_str)
{
	int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	string strTemp = str;
	if (wstr) delete[] wstr;
	if (str) delete[] str;
	return strTemp;
}

string Utf8ToGbk(const char *src_str)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
	wchar_t* wszGBK = new wchar_t[len + 1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
	len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
	char* szGBK = new char[len + 1];
	memset(szGBK, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
	string strTemp(szGBK);
	if (wszGBK) delete[] wszGBK;
	if (szGBK) delete[] szGBK;
	return strTemp;
}

class vtkTimerCallback : public vtkCommand
{
public:
	vtkTimerCallback() = default;

	static vtkTimerCallback* New()
	{
		vtkTimerCallback* cb = new vtkTimerCallback;
		cb->TimerCount = 0;
		return cb;
	}

	virtual void Execute(vtkObject* caller, unsigned long eventId,
		void* vtkNotUsed(callData))
	{
		const char*vecs[] = { "mode1","mode2","mode3","mode4","mode8" };
		if (vtkCommand::TimerEvent == eventId)
		{
			//
			//vtkReader->SetVectorsName(vecs[TimerCount % 5]);
			//vtkReader->Update();

			//
			auto iren = dynamic_cast<vtkRenderWindowInteractor*>(caller);

	//保存图片
			vtkWindowToImageFilter *imgFilter = vtkWindowToImageFilter::New();
			imgFilter->SetInput(iren->GetRenderWindow());
			imgFilter->SetInputBufferTypeToRGB();
		
			imgFilter->ReadFrontBufferOff();
			imgFilter->Update();

			vtkPNGWriter *pngWriter = vtkPNGWriter::New();
			pngWriter->SetInputConnection(imgFilter->GetOutputPort());
			pngWriter->SetFileName("hello.png");
			pngWriter->Write();

			
		
			iren->Render();
			++this->TimerCount;
		}
		std::cout << this->TimerCount << std::endl;
		if (this->TimerCount >= this->maxCount)
		{
			auto iren = dynamic_cast<vtkRenderWindowInteractor*>(caller);
			if (this->timerId > -1)
			{
				iren->DestroyTimer(this->timerId);
			}
		}
	}

private:
	int TimerCount = 0;

public:
	int maxCount = 100000;
	int timerId = -1;
	vtkSmartPointer<vtkPolyDataReader> vtkReader;
};

namespace {
	void MakeLUT(int const& colorScheme, vtkLookupTable* lut)
	{
		// See: [Diverging Color Maps for Scientific Visualization]
		//      (http://www.kennethmoreland.com/color-maps/)
		auto nc = 9;
		vtkNew<vtkColorTransferFunction> ctf;
		switch (colorScheme)
		{
		case 0:
		default:
			// Cool to warm diverging.
			ctf->SetColorSpaceToDiverging();
			ctf->AddRGBPoint(0.0, 0.230, 0.299, 0.754);
			ctf->AddRGBPoint(1.0, 0.706, 0.016, 0.150);
			lut->SetNumberOfTableValues(nc);
			lut->Build();
			for (auto i = 0; i < nc; ++i)
			{
				double rgb[4] = { 0.0, 0.0, 0.0, 1.0 };
				ctf->GetColor(static_cast<double>(i) / nc, rgb);
				lut->SetTableValue(i, rgb);
			}
			break;
		case 1:
			// Green to purple diverging.
			ctf->SetColorSpaceToDiverging();
			ctf->AddRGBPoint(0.0, 0.085, 0.532, 0.201);
			ctf->AddRGBPoint(1.0, 0.436, 0.308, 0.631);
			lut->SetNumberOfTableValues(nc);
			lut->Build();
			for (auto i = 0; i < nc; ++i)
			{
				double rgb[4] = { 0.0, 0.0, 0.0, 1.0 };
				ctf->GetColor(static_cast<double>(i) / nc, rgb);
				lut->SetTableValue(i, rgb);
			}
			break;
		case 2: {
			// Make a lookup table, black in the centre with bright areas
			//   at the beginning and end of the table.
			// This is from the original code.
			auto nc2 = nc / 2;
			lut->SetNumberOfColors(nc);
			lut->Build();
			for (auto i = 0; i < nc2; ++i)
			{
				// White to black.
				auto v = (double(nc2) - i) / double(nc2);
				lut->SetTableValue(i, v, v, v, 1);
			}
			for (auto i = nc2; i < nc; ++i)
			{
				// Black to white.
				auto v = (i - double(nc2)) / double(nc2);
				lut->SetTableValue(i, v, v, v, 1);
			}
			break;
		}
		}
	}
} // namespace



int main()
{

	//测试位移

	vtkSmartPointer<vtkPolyDataReader> vtkRD1 = vtkPolyDataReader::New();
	vtkRD1->SetFileName("C:\\Users\\qq\\Desktop\\P4-VTK\\plate.vtk");
	//vtkRD1->ReadAllVectorsOn();
	vtkRD1->SetVectorsName("mode8");

	vtkRD1->Update();

	//
	vtkSmartPointer<vtkWarpVector> warp = vtkWarpVector::New();
	//warp->SetInputData(vtkRD1->GetOutput());
	warp->SetInputConnection(vtkRD1->GetOutputPort());
	warp->SetScaleFactor(1);
	warp->Update();


	//
	vtkSmartPointer<vtkPolyDataNormals> normals = vtkPolyDataNormals::New();
	normals->SetInputConnection(warp->GetOutputPort());
	normals->Update();

	vtkSmartPointer<vtkVectorDot> vecDot = vtkVectorDot::New();
	vecDot->SetInputConnection(normals->GetOutputPort());
	vecDot->Update();

	//double p1[] = { 0,100, 1};
	//double p2[] = { 0, 0, 1};

	//vtkSmartPointer<vtkLineSource> line = vtkLineSource::New();
	//line->SetPoint1(p1);
	//line->SetPoint2(p2);
	//line->SetOutputPointsPrecision(vtkAlgorithm::DOUBLE_PRECISION);
	//line->SetResolution(10);
	//line->Update();

	//vtkPolyData *p = line->GetOutput();

	//std::cout << p->GetNumberOfPoints() << std::endl;
	//for (int i = 0; i < p->GetNumberOfPoints(); i++) {
	//	double p3[3];
	//	p->GetPoint(i, p3);
	//	std::cout << "  " << p3[0]<<","<<  p3[1]<<","<<  p3[2]<<","<<  std::endl;
	//}

	//return 0;

	//vtkPoints *points = vtkPoints::New();

	//points->InsertNextPoint(0, 0, 0);
	//points->InsertNextPoint(1, 0, 0);
	//points->InsertNextPoint(0, 1, 0);
	//points->InsertNextPoint(1, 1, 0);
	////
	//points->InsertNextPoint(0.5, 0.5, 0.5);
	//vtkIdType list1[] = { 2,3,1,0,4};

	//vtkNew<vtkCellArray> cell;
	//cell->InsertNextCell(5, list1);

	//// 渲染机制未知,需要同时设置点坐标与点坐标对应的verts
	//// verts中的id必须与点坐标对应
	////vtkPolyData *polyData = vtkPolyData::New();
	//vtkNew1<vtkUnstructuredGrid> polyData;
	//polyData->SetPoints(points);
	//int types = 14;
	//polyData->SetCells(&types, cell.Get());
	////polyData->SetPolys(cell.Get());

	//std::cout << " " << polyData->GetNumberOfCells()  << std::endl;;

	//double p3[3] = { .5,.5,.6 };
	////vtkSmartPointer<vtkIdList> pts = vtkIdList::New();
	////std::cout << " " << polyData->GetCell(0)->CellBoundary(0, p3,pts) << std::endl;

	////vtkSmartPointer<vtkCellLocator> cellLocat = vtkCellLocator::New();
	////cellLocat->SetDataSet(polyData);
	////cellLocat->BuildLocator();

	////std::cout << " FindCell" <<cellLocat->FindCell(p3);

	////vtkSmartPointer<vtkIdFilter> ids = vtkIdFilter::New();
	////ids->SetPointIds(1);
	////ids->SetCellIds(0);
	////ids->SetFieldData(0);
	////ids->SetInputData(polyData);	
	//vtkFloatArray *scalars = vtkFloatArray::New();
	//scalars->SetName("Stress");
	//scalars->SetNumberOfComponents(1);
	//scalars->InsertNextTuple1(3.5);
	//scalars->InsertNextTuple1(4.5);
	//scalars->InsertNextTuple1(5.5);
	//scalars->InsertNextTuple1(6.5);
	//scalars->InsertNextTuple1(7.5);

	//vtkFloatArray *scalars2= vtkFloatArray::New();
	//scalars2->SetName("serwrwe");
	//scalars2->SetNumberOfComponents(1);
	//scalars2->InsertNextTuple1(3.5);
	//scalars2->InsertNextTuple1(4.5);
	//scalars2->InsertNextTuple1(5.5);
	//scalars2->InsertNextTuple1(6.5);
	//scalars2->InsertNextTuple1(7.5);

	////

	//
	////polyData->GetPointData()->AddArray(scalars);
	////polyData->GetPointData()->AddArray(scalars2);
	//
	////for (int i = 0; i < ids->GetOutput()->GetNumberOfPoints(); i++) {
	//
	////ids->Update();

	////}
	////
	//

	//定义颜色映射表
	vtkLookupTable *pColorTable = vtkLookupTable::New();
	//
	//MakeLUT(0, pColorTable);

	//


	//设置颜色表中的颜色
	int names = 9;
	pColorTable->SetNumberOfTableValues(names);
	pColorTable->SetHueRange(0.667,0);
	//////pColorTable->SetRampToLinear();
	//////pColorTable->SetAlphaRange(1,1);
	//////pColorTable->SetSaturationRange(0.1, 1);
	pColorTable->Build();
	////

	//vtkVector3<double> red = { 1,0,0 };
	//vtkVector3<double> green = { 0,1,0 };
	//vtkVector3<double> blue = { 0,0,1 };
	//double alpha = 1;

	////
	//pColorTable->SetTableValue(0, red.GetX(), red.GetY(), red.GetZ());
	//pColorTable->SetTableValue(1, red.GetX(), red.GetY(), red.GetZ());
	//pColorTable->SetTableValue(2, red.GetX(), red.GetY(), red.GetZ());
	//pColorTable->SetTableValue(3, green.GetX(), green.GetY(), green.GetZ());
	//pColorTable->SetTableValue(4, green.GetX(), green.GetY(), green.GetZ());
	//pColorTable->SetTableValue(5, green.GetX(), green.GetY(), green.GetZ());
	//pColorTable->SetTableValue(6, blue.GetX(), blue.GetY(), blue.GetZ());
	//pColorTable->SetTableValue(7, blue.GetX(), blue.GetY(), blue.GetZ());
	//pColorTable->SetTableValue(8, blue.GetX(), blue.GetY(), blue.GetZ());


	//scalars->SetLookupTable(pColorTable);
	//scalars2->SetLookupTable(pColorTable);
	////polyData->GetPointData()->SetScalars(scalars);
	///*scalars2->SetLookupTable(pColorTable);
	//polyData->GetPointData()->AddArray(scalars2);*/
	//polyData->GetPointData()->AddArray(scalars);
	//polyData->GetPointData()->AddArray(scalars2);


	//

	//vtkSmartPointer<vtkUnstructuredGridWriter> vtu = vtkUnstructuredGridWriter::New();
	//vtu->SetInputData(polyData);
	//vtu->SetFileName("./xxx.vtk");
	//vtu->Write();

	////vtkSmartPointer<vtkColorTransferFunction> cfn = vtkColorTransferFunction::New();
	////cfn->SetColorSpaceToDiverging();
	////cfn->AddRGBPoint(0, 0, 0, 1);
	////cfn->AddRGBPoint(1., 1., 0, 0);
	////for (int i = 0; i < 256; i++) {
	////	double rgba[4];
	////	double *x;
	////	x = cfn->GetColor((double)i / names);

	////	rgba[0] = x[0];
	////	rgba[1] = x[1];
	////	rgba[2] = x[2];
	////	rgba[3] = 0;
	////	pColorTable->SetTableValue(i, rgba);
	////}
	////pColorTable->Build();
	////pColorTable->ForceBuild();
	/////*pColorTable->SetTableValue(0,1.0,0.0,0.0,1.0);*/
	////pColorTable->SetTableValue(0, 1.0, 0.0, 0.0, 1.0);
	////pColorTable->SetTableValue(1, 0.0, 1.0, 0.0, 1.0);
	////pColorTable->SetTableValue(2, 1.0, 1.0, 0.0, 1.0);
	////pColorTable->SetTableValue(3, 0.0, 0.0, 1.0, 1.0);
	////pColorTable->SetTableValue(4, 1.0, 0.0, 1.0, 1.0);
	////pColorTable->SetTableValue(5, 0.0, 1.0, 1.0, 1.0);
	////pColorTable->SetTableValue(6, 1.0, 1.0, 1.0, 1.0);
	////pColorTable->SetTableValue(7, 0.0, 0.0, 0.0, 1.0);
	//

	//vtkUnstructuredGridReader *reader = vtkUnstructuredGridReader::New();
	//reader->SetFileName("C:\\Users\\qq\\Desktop\\P4-VTK\\fuzha.cdb_UX_STRESS.vtk");
	//reader->Update();

	//


	//////下面为正常的可视化流程,可设置的点云颜色、大小等已注释
	//vtkDataSetMapper *mapper = vtkDataSetMapper::New();
	//mapper->SetInputData(reader->GetOutput());

vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInputConnection(vecDot->GetOutputPort());

	// 
	//std::cout<<"GetNumberOfArrays "<<reader->GetOutput()->GetPointData()->GetArrayName(0);
	//double *dRange = reader->GetOutput()->GetPointData()->GetScalars(reader->GetOutput()->GetPointData()->GetArrayName(0))->GetRange();
	//mapper->CreateDefaultLookupTable();
//	mapper->SetLookupTable(pColorTable);
	//mapper->GetLookupTable()->SetRange(3.5, 7.5);
	//vtkLookupTable::SafeDownCast(mapper->GetLookupTable())->SetNumberOfColors(256);
	//vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
	//mapper->SetInputData(polyData);

	mapper->SetLookupTable(pColorTable);

	//线性插值,达到ansys后处理效果
	mapper->InterpolateScalarsBeforeMappingOn();

//	mapper->SetColorModeToDirectScalars();

	//
	////mapper->SetScalarRange(-10087.4, 8154.17);
	//mapper->SetScalarRange(dRange[0], dRange[1]);
	//mapper->SetScalarModeToUsePointData();
	////mapper->CreateDefaultLookupTable();
	////mapper->GetFieldDataTupleId();
	//mapper->SetScalarModeToUsePointData();
	////mapper->SetScalarModeToUseFieldData();
	mapper->SetScalarRange(vecDot->GetOutput()->GetScalarRange());
	//
	mapper->GlobalImmediateModeRenderingOff();
	mapper->Update();


	vtkActor *actor = vtkActor::New();
	actor->SetMapper(mapper);
	//设置颜色与点大小
	//actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
	//actor->GetProperty()->SetPointSize(17);
	//actor->GetProperty()->SetRepresentationToPoints();
	actor->GetProperty()->SetRepresentationToSurface();

	//


	vtkRenderer *renderer = vtkRenderer::New();
	
	renderer->AddActor(actor);
	//actor->GetProperty()->SetRepresentationToSurface();
	//// 设置背景颜色
	//renderer->SetBackground(0.1, 0.2, 0.4);
	//反锯齿
	renderer->UseFXAAOn();
	vtkRenderWindow *renderWindow = vtkRenderWindow::New();
	//renderWindow->SetPointSmoothing(1);
	//renderWindow->SetLineSmoothing(1);
	//
	renderWindow->AddRenderer(renderer);
//
	vtkScalarBarWidget *bar = vtkScalarBarWidget::New();
//	//vtkScalarBarActor *barActor = vtkScalarBarActor::New();
//
//
	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renderWindow);
	iren->Initialize();
	//
	//int timeId = iren->CreateRepeatingTimer(3000);
	int timeId = iren->CreateOneShotTimer(3000);
	vtkTimerCallback * vtkTime = vtkTimerCallback::New();
	vtkTime->timerId = timeId;
	vtkTime->vtkReader = vtkRD1;
	iren->AddObserver(vtkCommand::TimerEvent, vtkTime);

//	//bar->GetScalarBarActor()->FixedAnnotationLeaderLineColorOn();
	bar->GetScalarBarActor()->DrawFrameOff();
	bar->GetScalarBarActor()->SetLabelFormat("%7.3f");
	bar->GetScalarBarActor()->SetLookupTable(pColorTable);
//	//bar->GetScalarBarActor()->DrawTickLabelsOff();
//	//bar->GetScalarBarActor()->SetTitle(reader->GetOutput()->GetPointData()->GetScalars()->GetName());
//	//barActor->SetMaximumWidthInPixels(24);
//	//barActor->SetWidth(1);
//	//barActor->SetWidth(24);
//	//barActor->SetHeight(24);
////	barActor->GetLabelTextProperty()->SetFontFamilyAsString("宋体");
	//bar->GetScalarBarActor()->GetTitleTextProperty()->SetFontSize(12);
	//bar->GetScalarBarActor()->SetNumberOfLabels(polyData->GetNumberOfPoints());
	bar->GetScalarBarActor()->SetNumberOfLabels(9);
	bar->GetScalarBarActor()->SetMaximumHeightInPixels(30);
	bar->RepositionableOff();
//
	vtkScalarBarRepresentation  *scalarBarRep = vtkScalarBarRepresentation::SafeDownCast(bar->GetRepresentation());
//
//	
	scalarBarRep->SetOrientation(0);  // 0 = Horizontal, 1 = Vertical
	scalarBarRep->GetPositionCoordinate()->SetValue(0.07, 0.07);
	//scalarBarRep->GetPosition2Coordinate()->SetValue(0.15, 0.25);
	scalarBarRep->SetShowBorderToOff();
//
//	//scalarBarRep->SetMaximumSize(10, 10);
//	
//	
	//barActor->SetOrientationToVertical();
	bar->SetInteractor(iren);
	
	bar->SetDefaultRenderer(renderer);
	//bar->SetRepositionableOff();
	bar->SetResizable(0);
	bar->On();


	iren->Render();
	iren->Start();


	return 0;
}
posted @ 2021-07-20 10:32  iwetuan  阅读(226)  评论(0)    收藏  举报