# 3D coordinate from mouse cursor position

**URL:** <https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303>\
**Category:** ROOT\
**Tags:** graphics\
**Created:** [March 25, 2019, 11:03am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303 "2019-03-25T11:03:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 25, 2019, 11:03am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/1 "2019-03-25T11:03:15Z")

</div>

Dear ROOT experts,

I want to customize the `TH1:SetShowProjection` method for `TH3` histograms and write a more complex GUI. I found [DynamicSlice.C](https://root.cern.ch/root/html/tutorials/hist/DynamicSlice.C.html) and it’s a good starting point for me, essentially what I want to do is to extend it to the 3D case.

Coming to my question, I was wondering if there’s already an easy way to get the respective 3D point from a `(px, py)` mouse cursor position.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 25, 2019, 12:56pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/2 "2019-03-25T12:56:21Z")

</div>

I do not see a such function in TView or TView3D… If it exits it should be there seems to me…

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 25, 2019, 2:11pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/3 "2019-03-25T14:11:13Z")

</div>

Well, I thought that I could have a look to the current implementation of the 3D -\> 2D projection (via `SetShowProjection`) in ROOT, there should be the piece of code I’m looking for… but I could not find it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 25, 2019, 2:26pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/4 "2019-03-25T14:26:03Z")

</div>

Indeed going from px py to x,y,z is not really possible conceptually. The best you can get is a 3D line parallel to two of the axis.

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 25, 2019, 2:30pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/5 "2019-03-25T14:30:24Z")

</div>

But then how does ROOT find the position of the slice from the mouse position when displaying the 2D projection?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 25, 2019, 2:36pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/6 "2019-03-25T14:36:05Z")

</div>

In the example [DynamicSlice.C](https://root.cern.ch/root/html/tutorials/hist/DynamicSlice.C.html) the 2d histogram is plotted in 2D … do you have a example ?

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 25, 2019, 3:58pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/7 "2019-03-25T15:58:49Z")

</div>

I don’t have any working example, but this is what I want to achieve:

I have a `TH3D` and I want to write a GUI that displays the histogram along with two interactive 2D orthogonal slices (`xy` and `xz`, for example), all in the same canvas. The slices are taken from the position of the mouse cursor and I want to display the two current slicing planes onto the `TH3D`, just as it happens with one projection when enabling `SetShowProjection`.

It’s pretty clear in my mind how the implementation should be, having seen DynamicSlice.C, but the missing starting point is finding a way to obtain the position of the two slices (the `x` and the `z`, staying with the same example) from the mouse position.

I hope I explained myself 🙂

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 25, 2019, 4:13pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/8 "2019-03-25T16:13:27Z")

</div>

You can do something like in the gl example glbox.C.  
When you move the mouse on the back plaine it highlights then you can move the plane  
using the shift+mouse

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 25, 2019, 4:37pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/9 "2019-03-25T16:37:39Z")

</div>

Looks really nice, but frankly I don’t know where to start, the code in glbox.C is unexpectedly simple… How do I retrieve the current positions of the planes?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 26, 2019, 9:29am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/10 "2019-03-26T09:29:56Z")

</div>

It was just an example showing what can be done. But that’s done with OpenGL. I do not think you want that. You want to use the normal LEGo and SURF plots ? To draw in that space you can look at: [Plot mean and max values of TGraphs in a TMultiGraph](https://root-forum.cern.ch/t/plot-mean-and-max-values-of-tgraphs-in-a-tmultigraph/32517/5)

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 26, 2019, 4:34pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/11 "2019-03-26T16:34:28Z")

</div>

What I did in the end is mainly stealing code from `TH3::Project3D`, find the script and a test ROOT file attached here.

As you can see there are problems in visualizing the polylines (but I find them as well with `TH1::SetShowProjection`, so it might be a more general bug): the horizontal plane vanishes after the respective 2D slice is drawn and the vertical one stays on screen until the next mouse click.

What do you suggest to fix the behaviour?

[test.root](https://root-forum.cern.ch/uploads/default/original/3X/0/0/006c796f297c64a970046eaab4b3d19b26bb4963.root) (65.2 KB)  
[test\_proj.C](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efa0a97b340648676aa6770e89cf8573b9d78e18.C) (11.4 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 26, 2019, 4:48pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/12 "2019-03-26T16:48:57Z")

</div>

I am not sure… may be redraw the 3D scene ?

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 26, 2019, 4:54pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/13 "2019-03-26T16:54:48Z")

</div>

I cannot do that because the histograms I normally visualize are quite big (not like the test I sent you) and it takes a while to render them… redrawing the 3D scene every time would kill the performance.

I will try to play around with different implementations and see if I can fix the problem, I wanted in any case to switch to filled shapes instead of polylines, maybe the problem will vanish…

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 27, 2019, 2:20pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/14 "2019-03-27T14:20:10Z")

</div>

Do you know how or the point in DynamicSlice.C where the old line gets erased? I just see a double `DrawLine` but I don’t think it is responsible for that…

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 27, 2019, 2:26pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/15 "2019-03-27T14:26:29Z")

</div>

I guess the double drawline is done in Xor mode. In that mode the 2nd drawline erases the previous one.

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 27, 2019, 5:03pm UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/16 "2019-03-27T17:03:43Z")

</div>

`TVirtualX::kXor` or `TVirtualX::kInvert`? Does the following work for you?

```auto
auto c = new TCanvas;
c->FeedbackMode(true); // hides a call to gVirtualX->SetDrawMode(TVirtualX::kInvert)
// gVirtualX->SetDrawMode(TVirtualX::KXor)
gVirtualX->DrawLine(0, 0, 100, 100);
gVirtualX->DrawLine(0, 0, 100, 100); // does the line stay on screen?

```

But the tricky thing is that in DynamicSlice.C it works… I have no clue why.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 28, 2019, 8:24am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/17 "2019-03-28T08:24:02Z")

</div>

you are using X11 backend ?

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 28, 2019, 8:59am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/18 "2019-03-28T08:59:11Z")

</div>

Sure, I forgot to say that I’m on Linux.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 28, 2019, 9:25am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/19 "2019-03-28T09:25:46Z")

</div>

Yes in DynamicSlice the code to erase the old line and redraw the new one is:

```auto
   if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
   gVirtualX->DrawLine(pxmin,py,pxmax,py);

```

You need to keep track of the old/previous line.

---

<div class="post-metadata">

**Author:** ![luigipertoldi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luigipertoldi/32/12374_2.png) [@luigipertoldi](https://root-forum.cern.ch/u/luigipertoldi)\
**Post date:** [March 28, 2019, 9:43am UTC](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303/20 "2019-03-28T09:43:45Z")

</div>

Ok, but then I don’t know why it does not work in my macro, I’m doing the same thing! See attached an updated version:

[test\_proj.C](https://root-forum.cern.ch/uploads/default/original/3X/9/3/937c3ac594bc9d93cfa330ce5512b7e8c47c04d8.C) (12.7 KB)

In particular, at line 47 I do `canvas->FeedbackMode(true)`, at lines 136 and 138 I declare the `static` structures holding the drawn coordinates, so I can delete them with `DrawPolyLine` at lines 170 and 232 and finally I draw the new rectangles at lines 367 and 368.

But as you can see it does not work. Also, the simple code I quoted in one of my [previous comment](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-curson-position/33303/16) in an interactive ROOT session does not work for me…

[Next page](https://root-forum.cern.ch/t/3d-coordinate-from-mouse-cursor-position/33303.md?page=2)
