# How to make addClippingPlane match vtkResliceCursorWidget ?

**URL:** https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641
**Category:** Web
**Created:** [February 8, 2023, 8:52am UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641 "2023-02-08T08:52:38Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Javi-r](https://discourse.vtk.org/user_avatar/discourse.vtk.org/javi-r/32/4601_2.png) [@Javi-r](https://discourse.vtk.org/u/Javi-r)
#### Post date: [February 8, 2023, 8:52am UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/1 "2023-02-08T08:52:38Z")

</div>

I can’t get the clip plane to match the reslice cursor widget planes.

```auto
const cut3DPlane = vtkPlane.newInstance();
....
cubeMapper.addClippingPlane(cut3DPlane);
cut3DPlane.setNormal(...resliceWidgetState.getPlanes()[xyzToViewType[1]].normal); //this is working
cutPlane.setOrigin(...resliceWidgetState.getCenter()); //this does not work

```

 ![clip](https://discourse.vtk.org/uploads/default/original/2X/c/ccaeaa3fe9a8fec5a9ecc5aa63d846721dd55215.jpeg)  
How can I make the cut match ?

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [February 8, 2023, 1:40pm UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/2 "2023-02-08T13:40:20Z")

</div>

I do not see what you are doing wrong (except the typo `cutPlane` vs `cut3DPlane`)

---

<div class="post-metadata">

### Author: ![Javi-r](https://discourse.vtk.org/user_avatar/discourse.vtk.org/javi-r/32/4601_2.png) [@Javi-r](https://discourse.vtk.org/u/Javi-r)
#### Post date: [February 9, 2023, 9:50am UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/3 "2023-02-09T09:50:19Z")

</div>

The error of cutPlane has been when writing the code here in my code is fine, that is not the problem.  
The problem is that the range of values obtained from resliceWidgetState.getCenter()[1] goes from 0 to 181.  
These values are much higher than what cut3DPlane needs.  
For example if I set cut3DPlane.setOrigin(0, resliceWidgetState.getCenter()[1], 0) the cube disappears when moving resliceCursorWidget.  
the values that cut3DPlane.setOrigin needs are much smaller, for example cut3DPlane.setOrigin(0,-0.2,0) this will for example cut the cube approximately in half as seen in the picture above.

---

<div class="post-metadata">

### Author: ![Javi-r](https://discourse.vtk.org/user_avatar/discourse.vtk.org/javi-r/32/4601_2.png) [@Javi-r](https://discourse.vtk.org/u/Javi-r)
#### Post date: [February 9, 2023, 10:33am UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/4 "2023-02-09T10:33:35Z")

</div>

Here you can see the problem.  
On the lines:  
line 120 function updateCutPlanes  
line 216 call to updateCutPlanes  
line 372 - 383 add the cube and the clip plane

> **[elated-jones-7wo722 - CodeSandbox](https://codesandbox.io/s/elated-jones-7wo722?file=%2Fsrc%2Findex.js)**
>
> elated-jones-7wo722 by Javi-R using parcel-bundler, vtk.js

---

<div class="post-metadata">

### Author: ![Javi-r](https://discourse.vtk.org/user_avatar/discourse.vtk.org/javi-r/32/4601_2.png) [@Javi-r](https://discourse.vtk.org/u/Javi-r)
#### Post date: [February 13, 2023, 2:54pm UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/5 "2023-02-13T14:54:00Z")

</div>

Any solution ?

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [February 13, 2023, 10:24pm UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/6 "2023-02-13T22:24:44Z")

</div>

By playing with your code and example, it seems that the problem comes from the clipping plane equation being sent to the shader (in `getClippingPlaneInDataCoords()` or in `vtkPolyDataMapper.setMapperShaderParameters`).

It is as if the cube while being 100x100x100 gets cut with a plane origin between 0.5 and -0.5.

FYI @thewtex

---

<div class="post-metadata">

### Author: ![Javi-r](https://discourse.vtk.org/user_avatar/discourse.vtk.org/javi-r/32/4601_2.png) [@Javi-r](https://discourse.vtk.org/u/Javi-r)
#### Post date: [February 14, 2023, 8:04am UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/7 "2023-02-14T08:04:02Z")

</div>

With what function can I get that range -0.5, 0.5 ?

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [February 15, 2023, 9:41am UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/8 "2023-02-15T09:41:03Z")

</div>

I simply played with `setOrigin()` of vtkPlane and realized the range.

---

<div class="post-metadata">

### Author: ![Javi-r](https://discourse.vtk.org/user_avatar/discourse.vtk.org/javi-r/32/4601_2.png) [@Javi-r](https://discourse.vtk.org/u/Javi-r)
#### Post date: [February 15, 2023, 3:06pm UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/9 "2023-02-15T15:06:50Z")

</div>

Any solution for this why is it happening?

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [February 15, 2023, 6:00pm UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/10 "2023-02-15T18:00:11Z")

</div>

No solution, just an idea. Your problem seems closely related to this issue and fix:

> <https://github.com/Kitware/vtk-js/issues/2225>
>
> \<!--
> 👋 Hello, and thank you for starting this contribution!
> ❓ If you are tryin…g to submit a question for general support, please reach out on our discourse forum instead: https://discourse.vtk.org/c/web/9
> 🐞 If you do need to report a bug, please follow the template below to increase the chances of your report being looked at.
> 🧱 If adequate, assign one of the \`Module: XXX\` labels to your issue when creating it.
> \--\>
> 
> \### High-level description
> 
> Due to webgl vertex buffer precision limitations, polydata vertex coordinates are currently centered and scaled in CellArrayBufferObject when some size condition is met. However, clipping planes coordinates are computed without accounting for this eventual shift and scale, therefore leading to incorrect clipping.
> 
> \### Steps to reproduce
> \<!--
> Steps to reproduce the behavior. If applicable, use:
> \* lists
> \* code snippets
> \`\`\`js
> code here
> \`\`\`
> \--\>
> \* Create a large polydata source (e.g. sphere with radius of 30)
> \* Add a clipping plane to its mapper
> \* Notice that its clipping is incorrect
> 
> \### Detailed behavior
> \<!--
> A detailed description of what happens. If applicable, use:
> \* screenshots (drag and drop)
> \* live examples (ex: https://codepen.io/jourdain/pen/RQZWYa)
> \--\>
> \* Live example of the issue, on a sphere of radius 30, with a plane supposed to clip z \< 0.25: https://codepen.io/Lillypucien/pen/gOGZwwM
> \* Live reference example, on a smaller sphere (radius 5), on which there is no coordinate shift, and therefore no bug: https://codepen.io/Lillypucien/pen/RwLEGVr
> \* Screenshot of the issue with the clipping plane drawn, on a sphere of radius 30:
> !\[bigSphereClipped\](https://user-images.githubusercontent.com/25703961/149349760-e9bb87f0-11a4-44e6-8a9c-1f3aa50738b1.PNG)
> \* Reference screenshot on a smaller sphere (radius 5):
> !\[smallSphereClipped\](https://user-images.githubusercontent.com/25703961/149349813-ba4b68d4-5e86-4194-bdc2-81c4b4a3bd07.PNG)
> 
> \### Expected behavior
> 
> When large polydata coordinates are scaled and shifted, clipping planes should also account for this scale and shift in their clipping computations.
> 
> \### Environment
> \- \*\*vtk.js\*\*: 22.0.0
> \- \*\*OS\*\*: Windows 10
> \- \*\*Browser\*\*: Chrome 97.0.4692.71

I would suggest:

- to try by reverting this change and see if that fixes it
- to understand this change and see if there is a problem with the logic
- further debug that code that uses `getClippingPlaneInDataCoords`

Shall you have funding, we could investigate it for you…

Hth,  
Julien.

---

<div class="post-metadata">

### Author: ![finetjul](https://discourse.vtk.org/user_avatar/discourse.vtk.org/finetjul/32/154_2.png) [@finetjul](https://discourse.vtk.org/u/finetjul)
#### Post date: [February 25, 2023, 10:07pm UTC](https://discourse.vtk.org/t/how-to-make-addclippingplane-match-vtkreslicecursorwidget/10641/11 "2023-02-25T22:07:35Z")

</div>

Actually, what I suggested is correct, the bug you are experiencing is related to issue !2225 .  
That issue has been fixed, so if you update your code to master (v26), you won’t have the problem anymore.
