# Axes Direction Cosines - please help

**URL:** https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094
**Category:** Support
**Created:** [March 20, 2022, 7:40pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094 "2022-03-20T19:40:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jamsoft](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jamsoft](https://discourse.vtk.org/u/jamsoft)
#### Post date: [March 20, 2022, 7:40pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/1 "2022-03-20T19:40:38Z")

</div>

Hello,

I have a cube and vtkImageReslice in my code and 3 ordinary cutting planes (a.k.a axial, sagittal, coronal) that make slices of the cube content.

For these 3 planes I use the vtkImageReslice.SetResliceAxesDirectionCosines function.

I don’t know how to visualize this planes inside cube volume, so I don’t know if the following direction cosines are correct, so I kindly ask your help:

1. Check if the values of direction cosines are right, please:

axial: (1, 0, 0, 0, 1, 0, 0, 0, 1)  
sagittal: (0, 0, -1, 0, 1, 0, 1, 0, 0)  
coronal: (1, 0, 0, 0, 0, 1, 0, -1, 0)

As I’m really bad in analytic geometry, I’ve found and copied this values from somewhere.  
The vtkImageReslice dimensions are set to 2.

Further, I convert the content to 2D image using vtkImageViewer2 in my code. This works just fine providing that the above direction cosines are correct… Can you confirm it, please?

1. I need also direction cosines for all four cube diagonal planes. Can you help me to find this values, please?

Also, if you have an idea how to visualize the slice planes inside my cube, I’ll be very happy for any advices.

Thank you and have a nice day all!

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 21, 2022, 2:27pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/2 "2022-03-21T14:27:17Z")

</div>

Hi, friend,

Direction cosines is just a pompous name for a really simple concept. As the name implies, those 9 values you supply are 9 cosine values. These 9 cosine values are 3 triplets of cosine values. Each cosine triplet defines uniquely a direction arrow (vector) in 3D like this:

![image](https://discourse.vtk.org/uploads/default/original/2X/c/c4b8d180a07ade99313d1214d2dbd5e9c6aa5d88.png)

As you can see in the figure above, the direction vector _u_ can be defined by the three angles alpha, beta and gamma. One way to set these three angles are using their cosines. VTK designers could have used the angles themselves, or their sines, etc. What matters here is to express the 3 angles by using some measure (cosine is the most used, also the case of VTK).

Hence, if you have 3 cosine triplets, you have 3 vectors defining de angles of an arbitrary parallelepiped in 3D space:

![image](https://discourse.vtk.org/uploads/default/original/2X/6/606a28f8d4f0dafb9585ea758ca1a90dea4be273.png)

In the figure above, each of the vectors A, B and C can be defined by three cosine values, hence the 9 cosine values required. To fully define de parallelpiped, however, you need to use `SetResliceAxesOrigin()` too.

Using your example, the axial slice defined with the (1, 0, 0, 0, 1, 0, 0, 0, 1) cosine values, translates to:

1. A vector: 0° with respect to X axis and 90º with respect to Y and Z axes;
2. B vector: 0° with respect to Y axis and 90º with respect to X and Z axes;
3. C vector: 0° with respect to Z axis and 90º with respect to X and Y axes;

So, it is easy to visualize that those cosine values mean a cube parallel to the XYZ axes. Since you want a plane instead of a sub-volume, you need to call `SetOutputDimensionality(2)` on your `vtkImageReslice` object to collapse the Z extent of the slice to zero, resulting in a planar slice. In the example of the axial slice, that yields a 2D slice parallel to the XY plane that can be translated along the Z axis by calling `SetResliceAxesOrigin()` on your vtkImageReslice` object.

As for your 2nd question, a cube has six diagonal planes, which ones would you want? The cosine of 45º is 0.7071, so one can be defined with (1, 0.7071, 0.7071, 0.7071, 1, 0.7071, 0.7071, 0.7071, 1).

I hope this casts some light on the issue,

Paulo

---

<div class="post-metadata">

### Author: ![jamsoft](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jamsoft](https://discourse.vtk.org/u/jamsoft)
#### Post date: [March 21, 2022, 7:22pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/3 "2022-03-21T19:22:04Z")

</div>

Dear Paulo,

OMG, so wonderful answer. I didn’t have time yet to absorb this information but after brief reading, I think that you have helped me a lot with my issue and understanding.  
And of course, you are right - i need all 6 diagonal planes. I was tired when I’ve been writing my question and I wrote 4 instead 6 😄

I don’t know how to thank you for your awesome reply. I’m very, very, very grateful.

Thank you thousand times and have a nice days!

Jirka

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 21, 2022, 9:47pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/4 "2022-03-21T21:47:13Z")

</div>

Hello,

I made a figure to illustrate the case for one slice. The black cube represents your input volume. The red rectangle represents the slice volume. The Z extent is collapsed, so it becomes a plane, but you still need to define a parallelpiped per VTK API.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/7/7e9dacddb72761d039a977551c26207942190c4e.png)  
Where:  
A, B and C are the director vectors of the slicer volume (Z extent is collapsed, reducing it to a plane).  
Alpha, beta and gamma: the angles of the vectors with respect to the coordinate axes.

Now, your “homework” is to obtain the cosines for the other five slices. Tip: draw everything on paper first, so you won’t get lost.

You’re quite welcome. When you break down complex stuff into more didatic units you learn a lot.

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![jamsoft](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jamsoft](https://discourse.vtk.org/u/jamsoft)
#### Post date: [March 21, 2022, 10:48pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/5 "2022-03-21T22:48:25Z")

</div>

Dear Paulo,

thank you again for the awesome explanation. I understand the example clearly. We’ll se if I will successfully finish my “homework” 🙂

Because my 3D imagination is very poor, I’ve still problem to imagine how to rotate the vectors for some other diagonal planes but I’ll do my best, keep trying and not give up too quickly 😃

And again, you are right - divide and conquer is a good approach to understand and solve problems.

Thank you once more!

Jirka

---

<div class="post-metadata">

### Author: ![jamsoft](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jamsoft](https://discourse.vtk.org/u/jamsoft)
#### Post date: [March 22, 2022, 10:45pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/6 "2022-03-22T22:45:46Z")

</div>

Dear Paulo,

I did my “homework” but I don’t have a way to confirm that it is correct.

For easier explanation, the following are the A, B, C vectors alpha, beta, gamma angles for my other cutting planes. If they are correct, I will easily convert them to cosines.

1. 

A  
alpha: 45°  
beta: 90°  
gamma: 45°  
B  
alpha: 90°  
beta: 0°  
gamma: 90°  
C  
alpha: 135°  
beta: 90°  
gamma: 45°

1. 

A  
alpha: 0°  
beta: 90°  
gamma: 90°  
B  
alpha: 90°  
beta: 45°  
gamma: 45°  
C  
alpha: 90°  
beta: 135°  
gamma: 45°

1. 

A  
alpha: -45°  
beta: 90°  
gamma: 135°  
B  
alpha: 90°  
beta: 0°  
gamma: 90°  
C  
alpha: 45°  
beta: 90°  
gamma: 45°

1. 

A  
alpha: 0°  
beta: 90°  
gamma: 90°  
B  
alpha: 90°  
beta: 45°  
gamma: 45°  
C  
alpha: 90°  
beta: 135°  
gamma: 45°

1. 

A  
alpha: -45°  
beta: -135°  
gamma: 90°  
B  
alpha: 45°  
beta: 45°  
gamma: 90°  
C  
alpha: 90°  
beta: 90°  
gamma: 0°

What I’m not sure about the planes that don’t start at the x, y, z axes origin (as in your diagonal cutting plane example). I’m also not sure about the negative angles.

Can you be so kind and check it or give me some advice how I can do it by myself to know that my vectors are really correct and they will work in vtk properly, please (some online simulator where I can put the values or some other way)?

I’m also quite confused by the SetResliceAxesOrigin() function. Is it by its parameters that I set which of the axes will be collapsed?

Thank you for any advices and have a nice day!

Jirka

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 27, 2022, 2:15pm UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/7 "2022-03-27T14:15:12Z")

</div>

Hello,

Sorry for the late response. Whenever I have difficulty to visualize a solution or compute some fancy expression on the run, I use Wolfram-Alpha ([https://www.wolframalpha.com/](https://www.wolframalpha.com/)). Enter the nine cosines (from the angles in degrees) like this in the query text field:

```cpp
vectors {cos(0), cos(90), cos(90)}, {cos(90), cos(90), cos(0)}, {cos(90), cos(0), cos(90)}

```

You’ll get a figure like this (along other useful information):  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/b/b392526df0fd134bb3efbdcb14a9fc469ea19bce.png)  
Recall that since you are collapsing the Z extent, the slicer plane will be along the two first vectors.

I hope this helps,

Paulo

---

<div class="post-metadata">

### Author: ![jamsoft](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jamsoft](https://discourse.vtk.org/u/jamsoft)
#### Post date: [April 3, 2022, 11:05am UTC](https://discourse.vtk.org/t/axes-direction-cosines-please-help/8094/8 "2022-04-03T11:05:03Z")

</div>

Dear Paulo,

thank you for your helpful advices. In the end, I rotate slices using vtkTransform and vtkMatrix4x4. It change the cosines matrix by more comfortable way, But, your advice and link to Wolfram-Alpha are very helpful for me. I’ve learned many new things. Thank you once more!

Now, I need to solve another problem with the slice position. I’ve created separate thread: [https://discourse.vtk.org/t/problem-with-the-vtkimagereslice-position-help-please/8202](https://discourse.vtk.org/t/problem-with-the-vtkimagereslice-position-help-please/8202)

Have a nice days,

Jirka
