I’m creating a surface using a point cloud, and I want to control the color of each point.Just like this picture,
I have used Class vtkSurfaceReconstructionFilte to create surface,but I can’t control the color.I want more advice, thanks
here is my code
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
#endif
#include <vtkSmartPointer.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyData.h>
#include <vtkSurfaceReconstructionFilter.h>
#include <vtkContourFilter.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkOutputWindow.h>
#include
#define pi 3.1415926535
#include <vtkFloatArray.h>
#include <vtkLookupTable.h>
#include <vtkPoints.h>
#include <vtkPointData.h> //点数据
#include <vtkUnsignedCharArray.h>
#include <vtkDelaunay2D.h>
int main()
{
vtkOutputWindow::SetGlobalWarningDisplay(0);
std::vector<std::vector<double>>XX;
std::vector<double> X;
std::vector<std::vector<double>>YY;
std::vector<double> Y;
std::vector<std::vector<double>>ZZ;
std::vector<double> Z;
double wzj=180
;double r=100;
int m=73;int n=37;
/* for(int i=0;i<73;i++)
{
X.push_back(cos(((180-wzj)/2+180+wzji/(m-1))pi/180)r);
Y.push_back(sin(((180-wzj)/2+180+wzji/(m-1))pi/180)r);
}
for(int i=0;i<37;i++)
{
XX.push_back(X);
YY.push_back(Y);
}/
std::ifstream ifs_p1;
ifs_p1.open(“C:/Rbjse_work/untitled/module0/result_p1.txt”);
std::vector<std::vector>pp1;
std::vector pp1_tmp;
double tmp;
for(int i=0;i<72+1;i++)
{
for(int j=0;j<36+1;j++)
{
ifs_p1>>tmp;
pp1_tmp.push_back(tmp500);
}
pp1.push_back(pp1_tmp);
pp1_tmp.clear();
}
for(int i=0;i<37;i++)
{
for(int j=0;j<73;j++)
{
X.push_back(cos(((180-wzj)/2+wzjj/(m-1))pi/180)(r+pp1[j][i]));
Y.push_back(sin(((180-wzj)/2+wzjj/(m-1))pi/180)(r+pp1[j][i]));
}
XX.push_back(X);
YY.push_back(Y);
X.clear();
Y.clear();
}
for(int i=0;i<37;i++)
{
for(int j=0;j<73;j++)
{
Z.push_back(i*100/37);
}
ZZ.push_back(Z);
Z.clear();
}
vtkSmartPointer<vtkPoints> pts =
vtkSmartPointer<vtkPoints>::New();
for(int i=0;i<37;i++)
{
for(int j=0;j<73;j++)
{
pts->InsertNextPoint(XX[i][j],ZZ[i][j],YY[i][j]);
}
}
vtkSmartPointer<vtkFloatArray> scalars_surf = vtkSmartPointer<vtkFloatArray>::New(); // 创建存储顶点属性的float数组
scalars_surf->SetNumberOfValues(n*m);
for (int i = 0; i < n*m; i++) // 为属性数组中的每个元素设置标量值(这个标量值可以当作颜色值)
{
scalars_surf->SetValue(i, i);
}
vtkSmartPointer<vtkLookupTable> hueLut = vtkSmartPointer<vtkLookupTable>::New();
hueLut->SetNumberOfColors(n*m); // 指定颜色查找表中有多少种颜色
hueLut->SetHueRange(0.6667, 0.0); //蓝到红渐变
hueLut->Build();
vtkSmartPointer<vtkPolyData> points = vtkSmartPointer<vtkPolyData>::New();
points->SetPoints(pts); //获得网格模型中的几何数据:点集
points->GetPointData()->SetScalars(scalars_surf);
vtkSmartPointer<vtkSurfaceReconstructionFilter> surf =
vtkSmartPointer<vtkSurfaceReconstructionFilter>::New();
surf->SetInputData(points);
surf->SetNeighborhoodSize(10);
surf->SetSampleSpacing(2);
surf->Update();
vtkSmartPointer<vtkContourFilter> contour =
vtkSmartPointer<vtkContourFilter>::New();
contour->SetInputConnection(surf->GetOutputPort());
//contour->SetValue(0, 0.0);
contour->Update();
//
vtkSmartPointer <vtkVertexGlyphFilter> vertexGlyphFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
vertexGlyphFilter->AddInputData(points);
vertexGlyphFilter->Update();
vtkSmartPointer<vtkPolyDataMapper> contourMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
contourMapper->SetInputData(contour->GetOutput());
contourMapper->SetScalarRange(0, n*m); // 设置标量值的范围(0-100)
contourMapper->ScalarVisibilityOn();
contourMapper->SetColorModeToDefault(); // 默认的映射器行为,即把unsigned char类型的标量属性数据当作颜色值,不执行隐式。对于其他类型的标量数据,将通过查询表映射。
contourMapper->SetLookupTable(hueLut);
vtkSmartPointer<vtkActor> contourActor =
vtkSmartPointer<vtkActor>::New();
contourActor->SetMapper(contourMapper);
/// double pointView[4] = { 0, 0, 0.5, 1 };
double contourView[4] = { 0.5, 0, 1, 1 };
vtkSmartPointer<vtkRenderer> contourRender =
vtkSmartPointer<vtkRenderer>::New();
contourRender->AddActor(contourActor);
contourRender->SetViewport(contourView);
contourRender->SetBackground(1, 1, 1);
vtkSmartPointer<vtkRenderWindow> rw =
vtkSmartPointer<vtkRenderWindow>::New();
rw->AddRenderer(contourRender);
rw->SetSize(640, 320);
rw->SetWindowName("3D Surface Reconstruction ");
rw->Render();
vtkSmartPointer<vtkRenderWindowInteractor> rwi =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
rwi->SetRenderWindow(rw);
rwi->Initialize();
rwi->Start();
return 0;
}
here is my result