C# 如何从vtkpolydata生成切片轮廓

C# 如何从vtkpolydata生成切片轮廓,c#,vtk,geometry-surface,C#,Vtk,Geometry Surface,我已设法从vtk中的设定点生成曲面。现在,我需要在曲面上切割一个平面,并制作一个二维轮廓,可以输出为vtkImageData。我制作了只在平面上投影的代码。有人能告诉我,我在通过polydata得到一个剖切面时做错了什么吗 vtkSphereSource sphereSource = vtkSphereSource.New(); sphereSource.SetPhiResolution(30); sphereSource.SetThetaResolution(30); sphereSource

我已设法从vtk中的设定点生成曲面。现在,我需要在曲面上切割一个平面,并制作一个二维轮廓,可以输出为vtkImageData。我制作了只在平面上投影的代码。有人能告诉我,我在通过polydata得到一个剖切面时做错了什么吗

vtkSphereSource sphereSource = vtkSphereSource.New();
sphereSource.SetPhiResolution(30);
sphereSource.SetThetaResolution(30);
sphereSource.SetCenter(40, 40, 0);
sphereSource.SetRadius(20);


vtkDataSetSurfaceFilter surfaceFilter = vtkDataSetSurfaceFilter.New();
surfaceFilter.SetInputConnection(sphereSource.GetOutputPort());
surfaceFilter.Update();

// generate circle by cutting the sphere with an implicit plane
// (through its center, axis-aligned)
vtkCutter circleCutter = vtkCutter.New();
circleCutter.SetInputConnection(sphereSource.GetOutputPort());

vtkPlane cutPlane = vtkPlane.New();
double[] Origin = sphereSource.GetCenter();
cutPlane.SetOrigin(Origin[0], Origin[1], Origin[2]);
cutPlane.SetNormal(0, 0, 1);

circleCutter.SetCutFunction(cutPlane);

vtkStripper stripper = vtkStripper.New();
stripper.SetInputConnection(circleCutter.GetOutputPort()); // valid circle
stripper.Update();
// that's our circle
vtkPolyData circle = stripper.GetOutput();


// prepare the binary image's voxel grid
vtkImageData whiteImage = vtkImageData.New();

double[] bounds;
bounds = circle.GetBounds();

whiteImage.SetNumberOfScalarComponents(1);
// whiteImage.SetScalarTypeToChar();
whiteImage.SetScalarType(3);
whiteImage.SetSpacing(.5, .5, .5);

// compute dimensions
int[] dim = new int[3];
for (int i = 0; i < 3; i++)
{
    dim[i] = (int)Math.Ceiling((bounds[i * 2 + 1] - bounds[i * 2]) / .5) + 1;
    if (dim[i] < 1)
        dim[i] = 1;
}
whiteImage.SetDimensions(dim[0], dim[1], dim[2]);
whiteImage.SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);

whiteImage.SetOrigin(bounds[0], bounds[2], bounds[4]);

whiteImage.AllocateScalars();

// fill the image with foreground voxels:
byte inval = 255;
byte outval = 0;
int count = whiteImage.GetNumberOfPoints();
for (int i = 0; i < count; ++i)
{
    whiteImage.GetPointData().GetScalars().SetTuple1(i, inval);
}

// sweep polygonal data (this is the important thing with contours!)
vtkLinearExtrusionFilter extruder = vtkLinearExtrusionFilter.New();
extruder.SetInput(circle);  ///todo warning this maybe setinputconnection

extruder.SetScaleFactor(1.0);
extruder.SetExtrusionTypeToNormalExtrusion();
extruder.SetVector(0, 0, 1);
extruder.Update();

// polygonal data -. image stencil:
vtkPolyDataToImageStencil pol2stenc = vtkPolyDataToImageStencil.New();
pol2stenc.SetTolerance(0); // important if extruder.SetVector(0, 0, 1) !!!
pol2stenc.SetInputConnection(extruder.GetOutputPort());
pol2stenc.SetOutputOrigin(bounds[0], bounds[2], bounds[4]);
pol2stenc.SetOutputSpacing(.5, .5, .5);

int[] Extent = whiteImage.GetExtent();
pol2stenc.SetOutputWholeExtent(Extent[0], Extent[1], Extent[2], Extent[3], Extent[4], Extent[5]);
pol2stenc.Update();

// cut the corresponding white image and set the background:
vtkImageStencil imgstenc = vtkImageStencil.New();
imgstenc.SetInput(whiteImage);
imgstenc.SetStencil(pol2stenc.GetOutput());

imgstenc.ReverseStencilOff();
imgstenc.SetBackgroundValue(outval);
imgstenc.Update();

vtkImageData stencil = imgstenc.GetOutput();
int[] Dims = stencil.GetDimensions();
int[,] DataMap = new int[Dims[0], Dims[1]];
vtkSphereSource sphereSource=vtkSphereSource.New();
球形资源的分辨率(30);
球形资源。设置溶液(30);
球形资源设置中心(40,40,0);
球形资源设置半径(20);
vtkDataSetSurfaceFilter surfaceFilter=vtkDataSetSurfaceFilter.New();
SetInputConnection(sphereSource.GetOutputPort());
surfaceFilter.Update();
//通过使用隐式平面切割球体生成圆
//(通过其中心,轴对齐)
vtkCutter circleCutter=vtkCutter.New();
circleCutter.SetInputConnection(sphereSource.GetOutputPort());
vtkPlane cutPlane=vtkPlane.New();
double[]Origin=sphereSource.GetCenter();
割平面.SetOrigin(原点[0],原点[1],原点[2]);
割平面。设置法线(0,0,1);
circleCutter.SetCutFunction(切割平面);
vtkStripper剥离器=vtkStripper.New();
stripper.SetInputConnection(circleCutter.GetOutputPort());//有效圆
stripper.Update();
//那是我们的圈子
vtkPolyData circle=stripper.GetOutput();
//准备二值图像的体素网格
vtkImageData whiteImage=vtkImageData.New();
双[]界;
bounds=circle.GetBounds();
whiteImage.SetNumberOfScalarComponents(1);
//whiteImage.SetScalarTypeToChar();
whiteImage.SetScalarType(3);
设置间距(.5,5,5);
//计算尺寸
int[]dim=新的int[3];
对于(int i=0;i<3;i++)
{
dim[i]=(int)数学上限((界[i*2+1]-界[i*2])/.5)+1;
if(dim[i]<1)
dim[i]=1;
}
设置维度(维度[0]、维度[1]、维度[2]);
设置范围(0,dim[0]-1,0,dim[1]-1,0,dim[2]-1);
SetOrigin(界限[0],界限[2],界限[4]);
whiteImage.AllocateScalars();
//用前景体素填充图像:
字节无效=255;
字节outval=0;
int count=whiteImage.GetNumberOfPoints();
对于(int i=0;i
我认为您获得的轮廓是正确的:-现在您希望如何将轮廓输出为图像?您只想将“轮廓上的像素”设置为某个值,而将“轮廓上的像素”设置为另一个值?这个怎么样