c# - 如何创建vtk七角棱柱(7个面4个顶点和2个面7个顶点)

标签 c# python c++ vtk

vtk 中,您可以使用 InsertNextCell (int type, vtkIdType npts, vtkIdType *ptIds) 将六角棱柱单元格添加到 vtkUnstructuredGrid >。这很好,但我想在 vtkUnstructuredGrid 中包含一个 heptagonaloctagonal 棱镜。单元格类型未在 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);

正确显示体素得到:enter image description here

但因为我想要一个七面体,所以我尝试使用 VTK_POLYHEDRON 来调整这个想法,更改告诉类型 pf 单元格添加到非结构化网格的行

            unstructuredGrid1.InsertNextCell(11, 8, ptIdsPointer);

到(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...

我怎样才能得到像图像这样的东西:enter image description here

最佳答案

使用 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 形状:

NumberOfSides=7

关于c# - 如何创建vtk七角棱柱(7个面4个顶点和2个面7个顶点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35932279/

相关文章:

c++ - 将密码保存在应用程序中

c# - 将控件添加到 C# 2015 中,未找到 this.Controls.Add(bla) 方法?

c# - 在 C# 中读取包含大量行的文本文件

python - 从另一台机器访问digitalocean服务器上的mongodb

python - 使用pyclutter编程

python - 提供存储在 SQLAlchemy LargeBinary 列中的图像

C++ 程序返回默认值 0

c++ - g++ 编译器忽略 const 返回类型

c# - 比较两个大小相等的位图以确定它们是否相同的最快方法是什么?

c# - 以编程方式在 Azure Function 中设置 RunOnStartup?