在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单元格添加到unstructuredgrid的行
unstructuredGrid1.InsertNextCell(11, 8, ptIdsPointer);
to(42代表VTK_POLYHEDRON
,11代表VTK_VOXEL
)和 points = 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...
答案 0 :(得分:2)
使用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();
我运行了代码并获得了七角形棱镜的三维形状:
答案 1 :(得分:1)
由于没有七边形或八边形棱镜的定义,您必须手动定义所有面,然后将每个面添加到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();
将导致:
答案 2 :(得分:1)
我认为你可以使用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)
(它应该像棱镜一样绘制棱镜,但是它将是一个圆柱:))
希望它会有所帮助。