如何创建vtk七角棱柱(7个面4个顶点和2个面7个顶点)
How to create vtk heptagonal prism (7 faces with 4 vertices and 2 faces with 7 vertices)
在 vtk
中,您可以将一个六棱柱细胞添加到 vtkUnstructuredGrid
和 InsertNextCell (int type, vtkIdType npts, vtkIdType *ptIds)
中。这很好,但我想在 vtkUnstructuredGrid
中包含一个 七角形 和 八角形 棱镜。单元格类型未在 vtk 中定义...
只是为了消除一些疑虑,我正在使用 c#
,但是 c++
或 python
示例足以将其转换为 c#
.
回到问题上来,我先把代码in this example复制过来稍微修改一下,得到一个体素(8点,celltype=11)
vtkPoints points = vtkPoints.New();
points.InsertNextPoint(-1.0, -1.0, -1.0);
points.InsertNextPoint(1.0, -1.0, -1.0);
points.InsertNextPoint(1.0, 1.0, -1.0);
points.InsertNextPoint(-1.0, 1.0, -1.0);
points.InsertNextPoint(-1.0, -1.0, 1.0);
points.InsertNextPoint(1.0, -1.0, 1.0);
points.InsertNextPoint(1.0, 1.0, 1.0);
points.InsertNextPoint(-1.0, 1.0, 1.0);
vtkUnstructuredGrid unstructuredGrid1 = vtkUnstructuredGrid.New();
unstructuredGrid1.SetPoints(points);
int[] ptIds = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
IntPtr ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * 8);
Marshal.Copy(ptIds, 0, ptIdsPointer, 8);
unstructuredGrid1.InsertNextCell(11, 8, ptIdsPointer);
Marshal.FreeHGlobal(ptIdsPointer);
// Create a mapper and actor
vtkDataSetMapper mapper = vtkDataSetMapper.New();
mapper.SetInputConnection(unstructuredGrid1.GetProducerPort());
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
actor.GetProperty().SetPointSize(3);
actor.GetProperty().SetOpacity(1);
actor.GetProperty().SetLineWidth(2);
actor.GetProperty().EdgeVisibilityOn();
Renderer3D.AddActor(actor);
正确显示体素得到:
但因为我想要一个七面体,所以我尝试使用 VTK_POLYHEDRON
来调整这个想法,更改告诉类型 pf 单元格添加到非结构化网格的行
unstructuredGrid1.InsertNextCell(11, 8, ptIdsPointer);
到(42代表VTK_POLYHEDRON
,11代表VTK_VOXEL
)和分=14
unstructuredGrid1.InsertNextCell(42, 14, ptIdsPointer);
喜欢this example。我尝试了类似 C++ 的东西,但我没有得到任何结果
int numberOfVertices = 14;
int numberOfFaces = 9; //how to tell heptagonal prism has 2 faces with 7 vertices and 7 with 4 vertices?
int numberOfFaceVertices = 4;
vtkIdType heptagonPointsIds[14] =
{0, 1, 2, 3, 4, 5, 6
7, 8, 9, 10, 11, 12,13};
//points that only change in z
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(454.6, 678.534, 1.1009301757813); //id 0
points->InsertNextPoint(454.6, 678.545, 1.1500146484375);//id 1
points->InsertNextPoint(454.6, 678.556, 1.2255187988281);//id 2
points->InsertNextPoint(455.1, 678.556, 1.6478076171875);//id 3
points->InsertNextPoint(455.1, 678.534, 1.7926538085938);//id 4
points->InsertNextPoint(455.1, 678.545, 1.7166479492188);//id 5
points->InsertNextPoint(454.85, 678.534, 1.0092297363281);//id 6
points->InsertNextPoint(454.6, 678.534, 5.1009301757813);//id 7
points->InsertNextPoint(454.6, 678.545, 5.1500146484375);//id 8
points->InsertNextPoint(454.6, 678.556, 5.2255187988281);//id 9
points->InsertNextPoint(455.1, 678.556, 5.6478076171875);//id 10
points->InsertNextPoint(455.1, 678.534, 5.7926538085938);//id 11
points->InsertNextPoint(455.1, 678.545, 5.7166479492188);//id 12
points->InsertNextPoint(454.85, 678.534, 5.0092297363281);//id13
vtkIdType heptagonPrismFace[9][4] = {
{0, 1, 7, 8},
{1, 2, 8, 9},
{2, 3, 9, 10},
{3, 4, 10, 11},
{4, 5, 11, 12},
{5, 6, 12, 13},
{6, 1, 13, 8},
//last two faces how to tel they should be 0 1 2 3 4 5 6 and 7 8 9 10 11 12 13 ?
{0, 1, 2, 3},
{7, 8, 9, 10},
};
vtkSmartPointer<vtkCellArray> heptagonFaces=
vtkSmartPointer<vtkCellArray>::New();
for (int i = 0; i < numberOfFaces; i++)
{
heptagonFaces->InsertNextCell(numberOfFaceVertices, heptagonPrismFace[i]);
}
vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
uGrid->InsertNextCell(VTK_POLYHEDRON,
numberOfVertices, heptagonPointsIds,
numberOfFaces, heptagonFaces->GetPointer());
uGrid->SetPoints(points);
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputConnection(uGrid->GetProducerPort());
//add actor to render and stuff...
我怎样才能得到像图片这样的东西:
由于没有定义 七角形 或 八角形 棱镜,您必须手动定义所有面,然后将每个面添加到 vtkUnstructuredGrid
,例如检查这个八角棱柱定义(这应该让你开始):
vtkPoints points = vtkPoints.New();
//up
points.InsertNextPoint(0.0, 0.0, 0.0 );//0
points.InsertNextPoint(0.3, -0.5, 0.0);//1
points.InsertNextPoint(0.6, -.8, 0.0);//2
points.InsertNextPoint(0.7, -.9, 0.0);//3
points.InsertNextPoint(0.8, -0.8, 0.0);//4
points.InsertNextPoint(1.0, 0.0, 0.0);//5
points.InsertNextPoint(1.0, 1.0, 0.0);//6
points.InsertNextPoint(0.0, 1.0, 0.0);//7
//down
points.InsertNextPoint(0.0, 0.0, 1.0);//8
points.InsertNextPoint(0.3, -0.5, 1.0);//9
points.InsertNextPoint(0.6, -.8, 1.0);//10
points.InsertNextPoint(0.7, -0.9, 1.0);//11
points.InsertNextPoint(0.8, -0.8, 1.0);//12
points.InsertNextPoint(1.0, 0.0, 1.0);//13
points.InsertNextPoint(1.0, 1.0, 1.0);//14
points.InsertNextPoint(0.0, 1.0, 1.0);//15
vtkUnstructuredGrid ugrid = vtkUnstructuredGrid.New();
int[] ptIds; IntPtr ptIdsPointer;
//8 quad faces
ptIds = new int[] { 0, 1, 9, 8 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 1, 2, 10, 9 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 2, 3, 11, 10 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 3, 4, 12, 11 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 4, 5, 13, 12 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 5, 6, 14, 13 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 6, 7, 15, 14 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 7, 0, 8, 15 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
//2 octagonal faces
ptIds = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 8, 9, 10, 11, 12, 13, 14, 15 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ugrid.SetPoints(points);
vtkGeometryFilter geometryFilter = vtkGeometryFilter.New();
geometryFilter.SetInput(ugrid);
vtkActor loActor = new vtkActor();
vtkPolyDataMapper pdm = vtkPolyDataMapper.New();
pdm.SetInputConnection(geometryFilter.GetOutputPort());
loActor.SetMapper(pdm);
loActor.GetProperty().SetPointSize(5);
loActor.GetProperty().SetColor(.9, 0, 0);
Renderer3D.AddActor(loActor);
vtkIdFilter ids = new vtkIdFilter();
ids.SetInputConnection(ugrid.GetProducerPort());
ids.PointIdsOn();
ids.CellIdsOn();
ids.FieldDataOn();
vtkLabeledDataMapper ldm = new vtkLabeledDataMapper();
ldm.SetInputConnection(ids.GetOutputPort());
ldm.SetLabelModeToLabelFieldData();
vtkActor2D pointLabels = new vtkActor2D();
这将导致:
我想你可以使用 vtkPolyhedron
例如:
import vtk
import math
class Point:
def __init__(self, x, y, z):
self.x =float(x)
self.y = float(y)
self.z = float(z)
def like_list(self):
return [self.x,self.y,self.z]
def generate_points(n):
l = []
centre = Point(0.0,0.0,0.0)
angle = (360.0/float(n))
r_len = 1.0
l.append(Point(1.0,0.0,0.0))
for i in xrange(1,n):
alpha = math.radians(float(i)*angle)
x = r_len * math.cos(alpha)
y = r_len * math.sin(alpha)
l.append(Point(x, y, 0.0))
#down face simple translate points on 1 up
l2 = [Point(p.x,p.y,p.z + 1.0) for p in l]
l.extend(l2)
return l
def MakeHeptagonalPrism():
p_list = generate_points(7) # 7 is a number of vertices in base of polyhedron
points = vtk.vtkPoints()
for p in p_list:
points.InsertNextPoint(p.like_list())
#You should fix list of faces if change number of vertices but
#I think it's posible to write aloritm to make it automaticaly
heptagonalFace = [
[0, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12, 13],
[0, 1, 8, 7],
[1, 2, 9, 8],
[2, 3, 10, 9],
[3, 4, 11, 10],
[4, 5, 12, 11],
[5, 6, 13, 12],
[6, 0, 7, 13]
]
numberOfFaces = 9
heptagonalFacesIdList = vtk.vtkIdList()
# Number faces that make up the cell.
heptagonalFacesIdList.InsertNextId(numberOfFaces)
for face in heptagonalFace:
# Number of points in the face == numberOfFaceVertices
heptagonalFacesIdList.InsertNextId(len(face))
# Insert the pointIds for that face.
[heptagonalFacesIdList.InsertNextId(i) for i in face]
uGrid = vtk.vtkUnstructuredGrid()
uGrid.InsertNextCell(vtk.VTK_POLYHEDRON, heptagonalFacesIdList)
uGrid.SetPoints(points)
# Visualize
mapper = vtk.vtkDataSetMapper()
mapper.SetInput(uGrid)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(.2, .3, .4)
renderWindow.Render()
renderWindowInteractor.Start()
if __name__ == '__main__':
MakeHeptagonalPrism()
仅供参考,在我看来这真的很棘手,但你也可以使用 vtkCylinderSource
只是以 here 为例并将分辨率更改为 7 source.SetResolution(7.0)
(它应该像形状一样绘制棱镜, 但它将是一个圆柱体 :) )
希望对您有所帮助。
使用vtkLinearExtrusionFilter 更容易。我写了一些 C++ 代码来生成具有可变边的多边形棱镜。我希望这会有所帮助:
vtkSmartPointer<vtkVectorText> vecText =
vtkSmartPointer<vtkVectorText>::New();
vecText->SetText("Text!");
vecText->Update();
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
double Radius=1;
int NumberOfSides=7;
for (int i=0;i<NumberOfSides;i++)
points->InsertNextPoint(Radius*cos((double)i*2*vtkMath::Pi()/NumberOfSides), Radius*sin((double)i*2*vtkMath::Pi()/NumberOfSides), 0.0);
// Create the polygon
vtkSmartPointer<vtkPolygon> polygon =
vtkSmartPointer<vtkPolygon>::New();
polygon->GetPointIds()->SetNumberOfIds(NumberOfSides);
for(int i=0;i<NumberOfSides;i++)
polygon->GetPointIds()->SetId(i, i);
// Add the polygon to a list of polygons
vtkSmartPointer<vtkCellArray> polygons =
vtkSmartPointer<vtkCellArray>::New();
polygons->InsertNextCell(polygon);
// Create a PolyData
vtkSmartPointer<vtkPolyData> polygonPolyData =
vtkSmartPointer<vtkPolyData>::New();
polygonPolyData->SetPoints(points);
polygonPolyData->SetPolys(polygons);
// Apply linear extrusion
vtkSmartPointer<vtkLinearExtrusionFilter> extrude =
vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extrude->SetInputData( polygonPolyData);
extrude->SetExtrusionTypeToNormalExtrusion();
extrude->SetVector(0, 0, 1 );
extrude->SetScaleFactor (2);
extrude->Update();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New();
mapper->SetInputData( extrude->GetOutput());
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer=vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindow->SetInteractor( renderWindowInteractor);
renderWindow->Render();
我 运行 代码并得到了这个七棱柱的 3d 形状:
在 vtk
中,您可以将一个六棱柱细胞添加到 vtkUnstructuredGrid
和 InsertNextCell (int type, vtkIdType npts, vtkIdType *ptIds)
中。这很好,但我想在 vtkUnstructuredGrid
中包含一个 七角形 和 八角形 棱镜。单元格类型未在 vtk 中定义...
只是为了消除一些疑虑,我正在使用 c#
,但是 c++
或 python
示例足以将其转换为 c#
.
回到问题上来,我先把代码in this example复制过来稍微修改一下,得到一个体素(8点,celltype=11)
vtkPoints points = vtkPoints.New();
points.InsertNextPoint(-1.0, -1.0, -1.0);
points.InsertNextPoint(1.0, -1.0, -1.0);
points.InsertNextPoint(1.0, 1.0, -1.0);
points.InsertNextPoint(-1.0, 1.0, -1.0);
points.InsertNextPoint(-1.0, -1.0, 1.0);
points.InsertNextPoint(1.0, -1.0, 1.0);
points.InsertNextPoint(1.0, 1.0, 1.0);
points.InsertNextPoint(-1.0, 1.0, 1.0);
vtkUnstructuredGrid unstructuredGrid1 = vtkUnstructuredGrid.New();
unstructuredGrid1.SetPoints(points);
int[] ptIds = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
IntPtr ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * 8);
Marshal.Copy(ptIds, 0, ptIdsPointer, 8);
unstructuredGrid1.InsertNextCell(11, 8, ptIdsPointer);
Marshal.FreeHGlobal(ptIdsPointer);
// Create a mapper and actor
vtkDataSetMapper mapper = vtkDataSetMapper.New();
mapper.SetInputConnection(unstructuredGrid1.GetProducerPort());
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
actor.GetProperty().SetPointSize(3);
actor.GetProperty().SetOpacity(1);
actor.GetProperty().SetLineWidth(2);
actor.GetProperty().EdgeVisibilityOn();
Renderer3D.AddActor(actor);
正确显示体素得到:
但因为我想要一个七面体,所以我尝试使用 VTK_POLYHEDRON
来调整这个想法,更改告诉类型 pf 单元格添加到非结构化网格的行
unstructuredGrid1.InsertNextCell(11, 8, ptIdsPointer);
到(42代表VTK_POLYHEDRON
,11代表VTK_VOXEL
)和分=14
unstructuredGrid1.InsertNextCell(42, 14, ptIdsPointer);
喜欢this example。我尝试了类似 C++ 的东西,但我没有得到任何结果
int numberOfVertices = 14;
int numberOfFaces = 9; //how to tell heptagonal prism has 2 faces with 7 vertices and 7 with 4 vertices?
int numberOfFaceVertices = 4;
vtkIdType heptagonPointsIds[14] =
{0, 1, 2, 3, 4, 5, 6
7, 8, 9, 10, 11, 12,13};
//points that only change in z
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(454.6, 678.534, 1.1009301757813); //id 0
points->InsertNextPoint(454.6, 678.545, 1.1500146484375);//id 1
points->InsertNextPoint(454.6, 678.556, 1.2255187988281);//id 2
points->InsertNextPoint(455.1, 678.556, 1.6478076171875);//id 3
points->InsertNextPoint(455.1, 678.534, 1.7926538085938);//id 4
points->InsertNextPoint(455.1, 678.545, 1.7166479492188);//id 5
points->InsertNextPoint(454.85, 678.534, 1.0092297363281);//id 6
points->InsertNextPoint(454.6, 678.534, 5.1009301757813);//id 7
points->InsertNextPoint(454.6, 678.545, 5.1500146484375);//id 8
points->InsertNextPoint(454.6, 678.556, 5.2255187988281);//id 9
points->InsertNextPoint(455.1, 678.556, 5.6478076171875);//id 10
points->InsertNextPoint(455.1, 678.534, 5.7926538085938);//id 11
points->InsertNextPoint(455.1, 678.545, 5.7166479492188);//id 12
points->InsertNextPoint(454.85, 678.534, 5.0092297363281);//id13
vtkIdType heptagonPrismFace[9][4] = {
{0, 1, 7, 8},
{1, 2, 8, 9},
{2, 3, 9, 10},
{3, 4, 10, 11},
{4, 5, 11, 12},
{5, 6, 12, 13},
{6, 1, 13, 8},
//last two faces how to tel they should be 0 1 2 3 4 5 6 and 7 8 9 10 11 12 13 ?
{0, 1, 2, 3},
{7, 8, 9, 10},
};
vtkSmartPointer<vtkCellArray> heptagonFaces=
vtkSmartPointer<vtkCellArray>::New();
for (int i = 0; i < numberOfFaces; i++)
{
heptagonFaces->InsertNextCell(numberOfFaceVertices, heptagonPrismFace[i]);
}
vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
uGrid->InsertNextCell(VTK_POLYHEDRON,
numberOfVertices, heptagonPointsIds,
numberOfFaces, heptagonFaces->GetPointer());
uGrid->SetPoints(points);
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputConnection(uGrid->GetProducerPort());
//add actor to render and stuff...
我怎样才能得到像图片这样的东西:
由于没有定义 七角形 或 八角形 棱镜,您必须手动定义所有面,然后将每个面添加到 vtkUnstructuredGrid
,例如检查这个八角棱柱定义(这应该让你开始):
vtkPoints points = vtkPoints.New();
//up
points.InsertNextPoint(0.0, 0.0, 0.0 );//0
points.InsertNextPoint(0.3, -0.5, 0.0);//1
points.InsertNextPoint(0.6, -.8, 0.0);//2
points.InsertNextPoint(0.7, -.9, 0.0);//3
points.InsertNextPoint(0.8, -0.8, 0.0);//4
points.InsertNextPoint(1.0, 0.0, 0.0);//5
points.InsertNextPoint(1.0, 1.0, 0.0);//6
points.InsertNextPoint(0.0, 1.0, 0.0);//7
//down
points.InsertNextPoint(0.0, 0.0, 1.0);//8
points.InsertNextPoint(0.3, -0.5, 1.0);//9
points.InsertNextPoint(0.6, -.8, 1.0);//10
points.InsertNextPoint(0.7, -0.9, 1.0);//11
points.InsertNextPoint(0.8, -0.8, 1.0);//12
points.InsertNextPoint(1.0, 0.0, 1.0);//13
points.InsertNextPoint(1.0, 1.0, 1.0);//14
points.InsertNextPoint(0.0, 1.0, 1.0);//15
vtkUnstructuredGrid ugrid = vtkUnstructuredGrid.New();
int[] ptIds; IntPtr ptIdsPointer;
//8 quad faces
ptIds = new int[] { 0, 1, 9, 8 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 1, 2, 10, 9 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 2, 3, 11, 10 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 3, 4, 12, 11 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 4, 5, 13, 12 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 5, 6, 14, 13 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 6, 7, 15, 14 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 7, 0, 8, 15 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
//2 octagonal faces
ptIds = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ptIds = new int[] { 8, 9, 10, 11, 12, 13, 14, 15 }; ptIdsPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * ptIds.Length); Marshal.Copy(ptIds, 0, ptIdsPointer, ptIds.Length); ugrid.InsertNextCell(9, ptIds.Length, ptIdsPointer); Marshal.FreeHGlobal(ptIdsPointer);
ugrid.SetPoints(points);
vtkGeometryFilter geometryFilter = vtkGeometryFilter.New();
geometryFilter.SetInput(ugrid);
vtkActor loActor = new vtkActor();
vtkPolyDataMapper pdm = vtkPolyDataMapper.New();
pdm.SetInputConnection(geometryFilter.GetOutputPort());
loActor.SetMapper(pdm);
loActor.GetProperty().SetPointSize(5);
loActor.GetProperty().SetColor(.9, 0, 0);
Renderer3D.AddActor(loActor);
vtkIdFilter ids = new vtkIdFilter();
ids.SetInputConnection(ugrid.GetProducerPort());
ids.PointIdsOn();
ids.CellIdsOn();
ids.FieldDataOn();
vtkLabeledDataMapper ldm = new vtkLabeledDataMapper();
ldm.SetInputConnection(ids.GetOutputPort());
ldm.SetLabelModeToLabelFieldData();
vtkActor2D pointLabels = new vtkActor2D();
这将导致:
我想你可以使用 vtkPolyhedron
例如:
import vtk
import math
class Point:
def __init__(self, x, y, z):
self.x =float(x)
self.y = float(y)
self.z = float(z)
def like_list(self):
return [self.x,self.y,self.z]
def generate_points(n):
l = []
centre = Point(0.0,0.0,0.0)
angle = (360.0/float(n))
r_len = 1.0
l.append(Point(1.0,0.0,0.0))
for i in xrange(1,n):
alpha = math.radians(float(i)*angle)
x = r_len * math.cos(alpha)
y = r_len * math.sin(alpha)
l.append(Point(x, y, 0.0))
#down face simple translate points on 1 up
l2 = [Point(p.x,p.y,p.z + 1.0) for p in l]
l.extend(l2)
return l
def MakeHeptagonalPrism():
p_list = generate_points(7) # 7 is a number of vertices in base of polyhedron
points = vtk.vtkPoints()
for p in p_list:
points.InsertNextPoint(p.like_list())
#You should fix list of faces if change number of vertices but
#I think it's posible to write aloritm to make it automaticaly
heptagonalFace = [
[0, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12, 13],
[0, 1, 8, 7],
[1, 2, 9, 8],
[2, 3, 10, 9],
[3, 4, 11, 10],
[4, 5, 12, 11],
[5, 6, 13, 12],
[6, 0, 7, 13]
]
numberOfFaces = 9
heptagonalFacesIdList = vtk.vtkIdList()
# Number faces that make up the cell.
heptagonalFacesIdList.InsertNextId(numberOfFaces)
for face in heptagonalFace:
# Number of points in the face == numberOfFaceVertices
heptagonalFacesIdList.InsertNextId(len(face))
# Insert the pointIds for that face.
[heptagonalFacesIdList.InsertNextId(i) for i in face]
uGrid = vtk.vtkUnstructuredGrid()
uGrid.InsertNextCell(vtk.VTK_POLYHEDRON, heptagonalFacesIdList)
uGrid.SetPoints(points)
# Visualize
mapper = vtk.vtkDataSetMapper()
mapper.SetInput(uGrid)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.SetBackground(.2, .3, .4)
renderWindow.Render()
renderWindowInteractor.Start()
if __name__ == '__main__':
MakeHeptagonalPrism()
仅供参考,在我看来这真的很棘手,但你也可以使用 vtkCylinderSource
只是以 here 为例并将分辨率更改为 7 source.SetResolution(7.0)
(它应该像形状一样绘制棱镜, 但它将是一个圆柱体 :) )
希望对您有所帮助。
使用vtkLinearExtrusionFilter 更容易。我写了一些 C++ 代码来生成具有可变边的多边形棱镜。我希望这会有所帮助:
vtkSmartPointer<vtkVectorText> vecText =
vtkSmartPointer<vtkVectorText>::New();
vecText->SetText("Text!");
vecText->Update();
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
double Radius=1;
int NumberOfSides=7;
for (int i=0;i<NumberOfSides;i++)
points->InsertNextPoint(Radius*cos((double)i*2*vtkMath::Pi()/NumberOfSides), Radius*sin((double)i*2*vtkMath::Pi()/NumberOfSides), 0.0);
// Create the polygon
vtkSmartPointer<vtkPolygon> polygon =
vtkSmartPointer<vtkPolygon>::New();
polygon->GetPointIds()->SetNumberOfIds(NumberOfSides);
for(int i=0;i<NumberOfSides;i++)
polygon->GetPointIds()->SetId(i, i);
// Add the polygon to a list of polygons
vtkSmartPointer<vtkCellArray> polygons =
vtkSmartPointer<vtkCellArray>::New();
polygons->InsertNextCell(polygon);
// Create a PolyData
vtkSmartPointer<vtkPolyData> polygonPolyData =
vtkSmartPointer<vtkPolyData>::New();
polygonPolyData->SetPoints(points);
polygonPolyData->SetPolys(polygons);
// Apply linear extrusion
vtkSmartPointer<vtkLinearExtrusionFilter> extrude =
vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extrude->SetInputData( polygonPolyData);
extrude->SetExtrusionTypeToNormalExtrusion();
extrude->SetVector(0, 0, 1 );
extrude->SetScaleFactor (2);
extrude->Update();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New();
mapper->SetInputData( extrude->GetOutput());
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer=vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindow->SetInteractor( renderWindowInteractor);
renderWindow->Render();
我 运行 代码并得到了这个七棱柱的 3d 形状: