# Create a cube with the certain vertices

**URL:** <https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362>\
**Category:** Support\
**Created:** [June 30, 2019, 9:33am UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362 "2019-06-30T09:33:52Z")\
**Posts on this page:** 8\
**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:** [June 30, 2019, 9:33am UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/1 "2019-06-30T09:33:52Z")

</div>

Hi Dears 3DSlicer developers

I want to create a cube with the certain vertices. For example, I have 8 points (vertices) with the following spatial coordinates in RAS.

| Vertex | R | A | S |
| --- | --- | --- | --- |
| superiorUpRight | -731.523242270663 | -74.7326192431123 | -1012.2 |
| superiorUpLeft | -531.523242270663 | -421.142780756888 | -1012.2 |
| superiorDownRight | -818.125782649107 | -124.732619243112 | -1012.2 |
| superiorDownLeft | -618.125782649107 | -471.142780756888 | -1012.2 |
| inferiorUpRight | -731.523242270663 | -74.7326192431123 | -1412.2 |
| inferiorUpLeft | -531.523242270663 | -421.142780756888 | -1412.2 |
| inferiorDownRight | -818.125782649107 | -124.732619243112 | -1412.2 |
| inferiorDownLeft | -618.125782649107 | -471.142780756888 | -1412.2 |
| | | | |
| Min | -818.125782649107 | -471.142780756888 | -1412.2 |
| Max | -531.523242270663 | -74.7326192431123 | -1012.2 |

How can I define one cube with these points?  
I do not find the fuction about setting up vertices in vtk.vtkCubeSource(), then I used the function of vtk.vtkCubeSource().SetBounds (double xMin, double xMax, double yMin, double yMax, double zMin, double zMax) as following lines:

> EPID = vtk.vtkCubeSource()  
> EPID.SetBounds(-818.125782649107, -531.523242270663, -471.142780756888, -74.7326192431123, -1412.2, -1012.2)  
> modelsLogicEPID = slicer.modules.models.logic()  
> modelEPID = modelsLogicEPID.AddModel(EPID.GetOutput())  
> modelEPID.GetDisplayNode().SetSliceIntersectionVisibility(True)  
> modelEPID.GetDisplayNode().SetColor(0,1,0)  
> modelEPID.GetDisplayNode().SetSliceIntersectionThickness(3)  
> EPID.Update()

As seen in the following figure, the red cube is not correct. The vertices are specified with colored spheres.

 ![Screenshot](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/8/68accae6fcac5238a1b9091df33ccbf8c7764f06.png)  
Please guide me.  
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:** [June 30, 2019, 12:38pm UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/2 "2019-06-30T12:38:43Z")

</div>

vtkCubeSource creates cube with non-rotated axes. I would recommend to create a cube with the right size, centered around the origin. Then position and orient the node by [specifying a parent transform](https://www.slicer.org/wiki/Documentation/Nightly/Modules/Transforms#Examples).

---

<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 1, 2019, 1:45pm UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/3 "2019-07-01T13:45:17Z")

</div>

Hi Dear Andras

Thanks a lot for your gudance. I create a cube model as mentioned you in the center of coordinate system with the following commands:

> EPIDmodel = vtk.vtkCubeSource()  
> EPIDmodel.SetXLength(400)  
> EPIDmodel.SetYLength(50)  
> EPIDmodel.SetZLength(400)  
> modelsLogic = slicer.modules.models.logic()  
> modelEPID = modelsLogic.AddModel(EPIDmodel.GetOutput())  
> modelEPID.SetName(‘EPIDmodel’)  
> modelEPID.GetDisplayNode().SetSliceIntersectionVisibility(True)  
> modelEPID.GetDisplayNode().SetSliceIntersectionThickness(3)  
> modelEPID.GetDisplayNode().SetColor(0,1,0)  
> EPIDmodel.Update()

I really want to apologize. Unfortunately I did not find a simple example in moving a model (vtkMRMLModelNode) including as Translation + Rotation.  
In the link you mentioned, Unfortunately, I did not see about the transformation of model node.  
Using the file of procrustesAlignment.py in VTK, I enter the following commands:

> transform1 = vtk.vtkTransform()  
> transform1.Translate(200, 100, 400)  
> transform1.RotateX(45)
> 
> transformer1 = vtk.vtkTransformPolyDataFilter()  
> transformer1.SetInputConnection(EPIDmodel.GetOutputPort())  
> transformer1.SetTransform(transform1)  
> EPIDmodel.Update()

I do not see any changes in the 3D window. Why?  
I must mention that after creating the model, I can transfer it to any direction using Transforms module, but I would like to learn this from Python Interactor. Please guide me to do it from command line of Python.

Please guide me.  
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 1, 2019, 3:33pm UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/4 "2019-07-01T15:33:24Z")

</div>

No need to apply transform polydata filter manually. Instead, you can set a parent transform node for the model node and then call `SetMatrixTransformToParent` to set a transformation matrix.

---

<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 4, 2019, 5:11pm UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/5 "2019-07-04T17:11:56Z")

</div>

Thank you very much for guidance. I do as you mentioned. Unfortunately, the cube model is not transfered with the following commands.

> transform = slicer.vtkMRMLTransformNode()  
> transform.SetName(‘Transformation’)  
> slicer.mrmlScene.AddNode(transform)  
> matrix = vtk.vtkMatrix4x4()  
> matrix.SetElement(0, 0, 1)  
> matrix.SetElement(0, 1, 0)  
> matrix.SetElement(0, 2, 0)  
> matrix.SetElement(0, 3, 0)  
> matrix.SetElement(1, 0, 0)  
> matrix.SetElement(1, 1, 1)  
> matrix.SetElement(1, 2, 0)  
> matrix.SetElement(1, 3, 0)  
> matrix.SetElement(2, 0, 0)  
> matrix.SetElement(2, 1, 0)  
> matrix.SetElement(2, 2, -1212.200)  
> matrix.SetElement(2, 3, 0)  
> matrix.SetElement(3, 0, 0)  
> matrix.SetElement(3, 1, 0)  
> matrix.SetElement(3, 2, 0)  
> matrix.SetElement(3, 3, 0)  
> transform.SetMatrixTransformToParent(matrix)  
> modelNode = slicer.util.getNode(‘EPIDmodel’)
> 
> modelNode.SetAndObserveTransformNodeID(transform.GetName())  
> modelNode.SetAndObserveTransformNodeID(transform.GetID())

Where else are I wrong?  
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 4, 2019, 5:22pm UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/6 "2019-07-04T17:22:20Z")

</div>

> [@shahrokh](#):
>
> matrix.SetElement(3, 3, 0)

You must leave the row 3 of the matrix as is: `(0,0,0,1)`. Calling `matrix.SetElement(3, 3, 0)` makes the transform invalid.

vtkMatrix4x4 is already initialized to identity, so there is no need to set the orientation (top-left 3x3 matrix) to identity again, just leave it as is.

Translation part of the transform is in column 3, not column 2, so this is incorrect: `matrix.SetElement(2, 2, -1212.200)` (it would ruin your model by scaling it by a factor of 1000 and turning it inside out). Use `matrix.SetElement(..., 3, ...)` instead.

> [@shahrokh](#):
>
> modelNode.SetAndObserveTransformNodeID(transform.GetName())

Delete this line. `SetAndObserveTransformNodeID` method requires a node ID, not a node name and you set the node ID correctly in the line below.

---

<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 7, 2019, 5:43am UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/7 "2019-07-07T05:43:29Z")

</div>

Dear Andras

At now, I can define transform matrix, including as Translation and Rotation, with the following commands.

> …  
> #Define transformation matrix (Translation and Rotation)  
> transform = slicer.vtkMRMLTransformNode()  
> transform.SetName(‘Transformation’)  
> #Add node Transormation to “Data” module  
> slicer.mrmlScene.AddNode(transform)  
> matrix = vtk.vtkMatrix4x4()  
> #Translation  
> #Shifting along the axis LR  
> matrix.SetElement(0, 3, translateLR)  
> #Shifting along the axis PA  
> matrix.SetElement(1, 3, translatePA)  
> #Shifting along the axis IS  
> matrix.SetElement(2, 3, translateIS)  
> #Rotation  
> #Rotation around IS (-60 degree)  
> matrix.SetElement(0, 0, math.cos(math.radians(300-360)))  
> matrix.SetElement(0, 1, -math.sin(math.radians(300-360)))  
> matrix.SetElement(1, 0, math.sin(math.radians(300-360)))  
> matrix.SetElement(1, 1, math.cos(math.radians(300-360)))
> 
> #Set IS parameter in “Transforms” module  
> transform.SetMatrixTransformToParent(matrix)
> 
> #Move “EPIDmodel” from “Transformable” to “Transormed” table in “Transforms” module and then apply to it.  
> modelNode = slicer.util.getNode(‘EPIDmodel’)  
> modelNode.SetAndObserveTransformNodeID(transform.GetID())

Thanks a lot for your guidance.  
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 7, 2019, 12:01pm UTC](https://discourse.slicer.org/t/create-a-cube-with-the-certain-vertices/7362/8 "2019-07-07T12:01:33Z")

</div>

It is great to hear this. Note that you can compute transformation matrix by using translate and rotate methods of vtkTransform and retrieve the matrix by calling `GetMatrix()`.
