gpt4 book ai didi

c# - 如何从 vtkpolydata 制作切片轮廓

转载 作者:行者123 更新时间:2023-11-30 16:26:10 26 4
gpt4 key购买 nike

我已经设法从 vtk 中的设定点创建了一个曲面。现在我需要通过表面切割一个平面并制作一个 2D 轮廓,我可以将其输出为 vtkImageData。我制作的代码只能投影到平面上。谁能告诉我通过多数据获取剖切面我做错了什么?

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]];

最佳答案

我认为您得到的轮廓是正确的:http://www.vtk.org/Wiki/VTK/Examples/Cxx/Filtering/ContoursFromPolyData - 现在您希望如何将轮廓输出为图像?您只是想将“轮廓上的像素”设置为某个值,而“不在轮廓上的像素”设置为不同的值?这个怎么样? http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData

关于c# - 如何从 vtkpolydata 制作切片轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com