// 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>
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);
int main()
{
//double p1[] = { 0,100, 0};
//double p2[] = { 0, 0, 0};
//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;
//}
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("Stressdfsfsdfffff");
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("s");
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()->SetScalars(scalars);
//polyData->GetPointData()->AddArray(scalars);
//polyData->GetPointData()->AddArray(scalars2);
//for (int i = 0; i < ids->GetOutput()->GetNumberOfPoints(); i++) {
//ids->Update();
//}
//
//定義顏色映射表
vtkLookupTable *pColorTable = vtkLookupTable::New();
//設置顏色表中的顏色
int names = 2048;
pColorTable->SetNumberOfTableValues(names);
pColorTable->SetHueRange(0.67, 0);
//pColorTable->SetSaturationRange(0.1, 1);
pColorTable->Build();
//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);
////下面為正常的可視化流程,可設置的點雲顏色、大小等已注釋
vtkDataSetMapper *mapper = vtkDataSetMapper::New();
mapper->SetInputData(polyData);
//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);
mapper->SetScalarRange(3.5, 7.5);
mapper->SetScalarModeToUsePointData();
//mapper->CreateDefaultLookupTable();
//mapper->GetFieldDataTupleId();
//mapper->SetScalarModeToUsePointData();
//mapper->SetScalarModeToUseFieldData();
//mapper->SetUseLookupTableScalarRange(0);
//mapper->Update();
vtkSmartPointer<vtkUnstructuredGridWriter> vtu = vtkUnstructuredGridWriter::New();
vtu->SetInputData(polyData);
vtu->SetFileName("./xxx.vtk");
vtu->Write();
vtkActor *actor = vtkActor::New();
actor->SetMapper(mapper);
//設置顏色與點大小
//actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
//actor->GetProperty()->SetPointSize(17);
//actor->GetProperty()->SetRepresentationToPoints();
//
vtkRenderer *renderer = vtkRenderer::New();
renderer->AddActor(actor);
//// 設置背景顏色
renderer->SetBackground(0.1, 0.2, 0.4);
vtkRenderWindow *renderWindow = vtkRenderWindow::New();
renderWindow->AddRenderer(renderer);
vtkScalarBarWidget *bar = vtkScalarBarWidget::New();
vtkScalarBarActor *barActor = vtkScalarBarActor::New();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renderWindow);
iren->Initialize();
barActor->SetLookupTable(pColorTable);
barActor->SetTitle(polyData->GetPointData()->GetScalars()->GetName());
//barActor->SetMaximumWidthInPixels(24);
barActor->SetWidth(1);
barActor->SetWidth(24);
barActor->SetHeight(24);
barActor->GetTitleTextProperty()->SetFontSize(12);
barActor->SetNumberOfLabels(polyData->GetNumberOfPoints());
//barActor->SetOrientationToVertical();
bar->SetInteractor(iren);
bar->SetScalarBarActor(barActor);
//bar->SetDefaultRenderer(renderer);
//bar->SetRepositionable(0);
//bar->SetResizable(0);
bar->On();
iren->Start();
return 0;
}