# Define pyramid with cross-section of polygon

**URL:** <https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600>\
**Category:** Support\
**Created:** [July 16, 2019, 2:16am UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600 "2019-07-16T02:16:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahrokh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shahrokh/32/1499_2.png) [@shahrokh](https://discourse.slicer.org/u/shahrokh)\
**Post date:** [July 16, 2019, 2:16am UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/1 "2019-07-16T02:16:05Z")

</div>

Hi Dear 3DSlicer developers

I want to define one pyramid with cross-section of polygon (for example polygon with 160 vertices) using VTK.  
I think that I must use vtkFrustumSource. Is it ok?  
How can I do it?  
Please guide me.  
Shahrokh

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 16, 2019, 11:58am UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/2 "2019-07-16T11:58:28Z")

</div>

Yes, any vtkPolyData from vtk (either a source or the result of a pipeline) should be usable with a vtkMRMLModelNode.

[https://lorensen.github.io/VTKExamples/site/Python/#geometric-objects](https://lorensen.github.io/VTKExamples/site/Python/#geometric-objects)

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Show\_a\_simple\_surface\_mesh\_as\_a\_model\_node](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Show_a_simple_surface_mesh_as_a_model_node)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 16, 2019, 12:12pm UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/3 "2019-07-16T12:12:18Z")

</div>

> [@shahrokh](#):
>
> pyramid with cross-section of polygon (for example polygon with 160 vertices)

`vtkFrustumSource` can only create rectangular pyramid. If you need to extrude arbitrary polygons then you may use [`vtkLinearExtrusionFilter`](https://vtk.org/doc/nightly/html/classvtkLinearExtrusionFilter.html).

---

<div class="post-metadata">

**Author:** ![shahrokh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shahrokh/32/1499_2.png) [@shahrokh](https://discourse.slicer.org/u/shahrokh)\
**Post date:** [July 22, 2019, 3:57am UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/4 "2019-07-22T03:57:16Z")

</div>

Thanks a lot for your guidance.

To implement a polygon in Python interactor, I wrote the code to run in 3DSlicer.  
In short, I mention to the geometry considered with me and it is shown in the following figures (IDs and spatial coordinates).

 ![Picture16](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/1/01dbf0a6c56b9e84a25ec7f83fab76506a2e3185.png)

 ![Picture17](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/8/78d78c195be779bea028f5c7fdf29c54aec9aa0c.png)

As you see, I have 23 points in RAS coordinate system (RS plane). After doing it, I want to create a polygon model and access to it in Data module. The following figure is shown these points and the polygon.

 ![Picture18](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/6/764a6441cb3cdb91f0622d4cec191e1eee4d8011.png)

The following lines are the code I wrote.

> import vtk  
> polygonPoints = vtk.vtkPoints()  
> polygonPoints.SetNumberOfPoints(23)  
> polygonPoints.InsertPoint(0,4,0,0)  
> polygonPoints.InsertPoint(1,1,0,1)  
> polygonPoints.InsertPoint(2,2,0,1.5)  
> polygonPoints.InsertPoint(3,3,0,2.5)  
> polygonPoints.InsertPoint(4,2,0,3.5)  
> polygonPoints.InsertPoint(5,1,0,4.5)  
> polygonPoints.InsertPoint(6,-1,0,5)  
> polygonPoints.InsertPoint(7,-2,0,4)  
> polygonPoints.InsertPoint(8,-3,0,3.5)  
> polygonPoints.InsertPoint(9,-4,0,3)  
> polygonPoints.InsertPoint(10,-4.5,0,2)  
> polygonPoints.InsertPoint(11,-5,0,1)  
> polygonPoints.InsertPoint(12,-5,0,0)  
> polygonPoints.InsertPoint(13,-4.5,0,-1)  
> polygonPoints.InsertPoint(14,-3,0,-2)  
> polygonPoints.InsertPoint(15,-2,0,-2.5)  
> polygonPoints.InsertPoint(16,-1,0,-3)  
> polygonPoints.InsertPoint(17,0,0,-5)  
> polygonPoints.InsertPoint(18,1,0,-5)  
> polygonPoints.InsertPoint(19,2,0,-4.5)  
> polygonPoints.InsertPoint(20,3,0,-3)  
> polygonPoints.InsertPoint(21,3,0,-2)  
> polygonPoints.InsertPoint(22,3,0,-1)  
> polygonPolyData = vtk.vtkPolyData()  
> polygonPolyData.SetPoints(polygonPoints)  
> model = slicer.vtkMRMLModelNode()  
> model.SetAndObservePolyData(polygonPolyData)  
> modelDisplay = slicer.vtkMRMLModelDisplayNode()  
> modelDisplay.SetColor(1, 1, 0)  
> modelDisplay.BackfaceCullingOff()  
> modelDisplay.SetOpacity(0.5)  
> modelDisplay.SetPointSize(3)  
> modelDisplay.SetSliceIntersectionVisibility(True)  
> modelDisplay.SetVisibility(True)  
> slicer.mrmlScene.AddNode(modelDisplay)  
> model.SetAndObserveDisplayNodeID(modelDisplay.GetID())  
> modelDisplay.SetInputPolyDataConnection(model.GetPolyDataConnection())  
> slicer.mrmlScene.AddNode(model)

When I enter this code in Python interactor of 3DSlicer, I see that a new node is created in Data module as is shown in the following figure.

 ![Screenshot%20from%202019-07-22%2008-20-20](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/9/a9942f0cfcec52f8041d7d2008e005c69cc5ad46.png)

Unfortunately, I do not see this model (polygon with 23 sides) in **3D window** and I can not change it (for example rotation and translation).  
Why?  
What’s wrong with me?  
Please guide me.  
Thanks a lot.  
Shahrokh.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 22, 2019, 4:32am UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/5 "2019-07-22T04:32:53Z")

</div>

You added points, but you’ve forgot to add a polygon cell. You need to do something like this:

> <https://github.com/Slicer/Slicer/blob/9d730b3ef2987b69d12689a084c5a0b420a6dacf/Modules/Loadable/Markups/Logic/vtkSlicerMarkupsLogic.cxx#L1419-L1428>

You can find many examples of how to create polydata manually on [VTK examples site](https://lorensen.github.io/VTKExamples/site/).

---

<div class="post-metadata">

**Author:** ![shahrokh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/shahrokh/32/1499_2.png) [@shahrokh](https://discourse.slicer.org/u/shahrokh)\
**Post date:** [July 23, 2019, 6:28am UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/6 "2019-07-23T06:28:18Z")

</div>

Thanks a lot for your help.  
As mentioned you, I add the following lines

> …  
> polygonPolyData = vtk.vtkPolyData()  
> polygonPolyData.SetPoints(polygonPoints)
> 
> **#Create Cell**  
> **polygons = vtk.vtkCellArray()**  
> **polygons.InsertNextCell(23)**  
> **for i in range(0,22):**  
> **polygons.InsertCellPoint(i)**  
> **polygons.Modified()**  
> **polygonPolyData.SetPolys(polygons)**
> 
> model = slicer.vtkMRMLModelNode()  
> model.SetAndObservePolyData(polygonPolyData)  
> …

At now, I can see my polygon model in _Wireframe Representation_ as following figure:

 ![ScreenshotWireFrame](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/9/b93b4356b00771e51f4fee0f1b4d17f83fc45158.png)

It is ok.

Unfortunately, this varies slightly when I see it in _Surface_ with _WireFrame_ representations as following figures:

 ![ScreenshotSurface](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/c/7c3169b6489bf76a658534be7fcbac0f0a09ba30.png)

Or

 ![ScreenshotSurfaceWithEdges](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/8/68a89f39a7a8f3f2945e89394159aaf2f4215cae.png)

As see you, Why do not they match (edges (black color wireframe) with the edges of polygon surface (yellow surface))?

Is this a visual error or I made a mistake again?  
Thanks a lot for your support.  
Shahrokh.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 23, 2019, 12:32pm UTC](https://discourse.slicer.org/t/define-pyramid-with-cross-section-of-polygon/7600/7 "2019-07-23T12:32:16Z")

</div>

OpenGL can only render _convex_ filled polygons. If you need to display non-convex polygon then you can triangulate it using vtkTriangleFilter.
