建立polydata
创建cube和上一个博文中创建虚线的方法差不多。把cell从2换成4
vtkLookupTable的SetTableValue定义颜色表,分别是映射数值和颜色RGB值。
double[][] x = new double[][]{new double[]{0,0,0},new double[]{5,0,0},new double[]{5,5,0},new double[]{0,5,0},new double[]{0,0,5},new double[]{5,0,5},new double[]{5,5,5},new double[]{0,5,5}};
int[][] pts = new int[][]{ new int[] { 0, 1, 2, 3 }, new int[] { 4, 5, 6, 7 }, new int[] { 0, 1, 5, 4 },
new int[] { 1, 2, 6, 5 }, new int[]{ 2, 3, 7, 6 }, new int[]{ 3, 0, 4, 7 } };
vtkPolyData cube = new vtkPolyData();
vtkCellArray polys = new vtkCellArray();
vtkFloatArray scalars = new vtkFloatArray();
vtkPoints points = new vtkPoints();
for (int i = 0; i < 8; i++)
{
points.InsertPoint(i, x[i][0], x[i][1], x[i][2]);
}
for (int i = 0; i < 6; i++)
{
polys.InsertNextCell(4);
for (int j = 0; j < 4; j++)
{
polys.InsertCellPoint(pts[i][j]);
}
}
//定义6个面颜色
scalars.InsertTuple1(0, 0);
scalars.InsertTuple1(1, 0);
scalars.InsertTuple1(2, 0);
scalars.InsertTuple1(3, 5);
scalars.InsertTuple1(4, 5);
scalars.InsertTuple1(5, 5);
cube.SetPoints(points);
cube.SetPolys(polys);
cube.GetCellData().SetScalars(scalars);
vtkPolyDataMapper cubeMapper = vtkPolyDataMapper.New();
cubeMapper.SetInput(cube);
//上面是颜色手动定义6种
vtkLookupTable pColorTable = vtkLookupTable.New();
pColorTable.SetNumberOfColors(6);
pColorTable.SetTableValue(0, 1.0, 0.0, 1.0, 1.0);
pColorTable.SetTableValue(1, 0.0, 1.0, 1.0, 1.0);
pColorTable.SetTableValue(2, 1.0, 1.0, 1.0, 1.0);
pColorTable.SetTableValue(3, 1.0, 0.0, 1.0, 1.0);
pColorTable.SetTableValue(4, 0.0, 0.0, 1.0, 1.0);
pColorTable.SetTableValue(5, 1.0, 1.0, 0.0, 1.0);
pColorTable.Build();
cubeMapper.SetScalarRange(0, 6);
vtkActor cubeActor = new vtkActor();
cubeActor.SetMapper(cubeMapper);
效果如下:设置了三个面红色三个面蓝色
在立方体一个面上贴图:
新建了一个平面,然后利用vtkTextureMapToPlane将图片映射上去,通过平移旋转缩放使其在一个面上。文字类图像还需要考虑正反面的问题,因此在这里进行了翻转。
vtkActor planeActor = vtkActor.New();
vtkPlaneSource planeSource = vtkPlaneSource.New();//根据点法式创建
planeSource.SetCenter(0, 0, 6);
planeSource.SetPoint1(5, 0, 6);
planeSource.SetPoint2(0, 5, 6);
planeSource.SetXResolution(1);
planeSource.SetYResolution(1);
planeSource.SetNormal(0, 0, 1);
planeSource.Update();
vtkTextureMapToPlane texturePlane = vtkTextureMapToPlane.New();
texturePlane.SetInputConnection(planeSource.GetOutputPort());
vtkTexture texture = vtkTexture.New();
texture.InterpolateOn();
vtkPolyDataMapper polydatamapper = vtkPolyDataMapper.New();
polydatamapper.SetInputConnection(texturePlane.GetOutputPort());
vtkPNGReader read = vtkPNGReader.New();
read.SetFileName("C:\\Users\\Administrator\\Desktop\\2020.12PMWS项目\\12.9\\3D-Real-Time-Pilling-Monitoring-System\\PMWS\\PMWS\\PMWS\\bin\\Debug\\打桩船1.png");
texture.SetInput(read.GetOutput());
planeActor.SetMapper(polydatamapper);
planeActor.SetTexture(texture);
//这个是进行了平面的旋转平移 这个写法主要是为了方便整体移动
renderer.AddActor(planeActor);
double[] a0 = new double[] { 0, 5 };
double[] a1 = new double[] { 0, 0 };
double[] a2 = new double[] { 5, 0 };
double[] a3 = new double[] { 5, 5 };
double[] center = new double[] { (a0[0]+ a1[0]+ a2[0]+ a3[0])/4,
(a0[1] + a1[1] + a2[1] + a3[1]) / 4 };
//找到旋转轴
planeActor.SetOrigin(center[0], center[1], 6);
planeActor.RotateWXYZ(180, 0, 5, 0);
planeActor.SetScale(0.8, 0.8, 1);
效果如图所示: