# positioning and scaling of Actor2D with image

**URL:** https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754
**Category:** Support
**Created:** [October 2, 2021, 3:04pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754 "2021-10-02T15:04:08Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 2, 2021, 3:04pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/1 "2021-10-02T15:04:08Z")

</div>

I’m trying to display a static image in on of the corners of the screen. Like a company logo or some additional information (see [Adding a logo-widget · Issue #474 · marcomusy/vedo (github.com)](https://github.com/marcomusy/vedo/issues/474)) .

Easiest seems to use a Actor2D in combination vtkImageMapper. But I can not figure out how to scale or position the actor. SetWidth, SetHeight do not seem to have any effect, SetPosition seems to use pixels rather than normalized screen coordinates.

I found [https://stackoverflow.com/questions/9216211/how-to-position-a-vtkactor2d-in-the-upper-right-corner](https://stackoverflow.com/questions/9216211/how-to-position-a-vtkactor2d-in-the-upper-right-corner) but I can not imagine that that is the best way to do this.

thanks!

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 2, 2021, 8:50pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/2 "2021-10-02T20:50:38Z")

</div>

we got a bit furher with setting imagemapper.RenderToRectangleOn() to enable scaling and using vtkCoordinates for the position.

However I fail to get the expected results when setting Position2 to Viewport-coordinates, relative to Position and providing the size of the image in pixels.

I would expect the following code to define the position of the upper-right corner relative to the  
lower-left in viewport-pixels. However it acts like the coordinate system is still set to NormalizedViewport?

```python
position2_coordinate = image.GetPosition2Coordinate()
position2_coordinate.SetReferenceCoordinate(image.GetPositionCoordinate())
position2_coordinate.SetCoordinateSystemToViewport() # x-y pixel values in viewport
position2_coordinate.SetValue(200,100) # fails SetValue(0.2,0.1) does show the image, but keeps it relative to viewport

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [October 4, 2021, 9:01am UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/3 "2021-10-04T09:01:26Z")

</div>

[https://vtk.org/doc/nightly/html/classvtkLogoWidget.html](https://vtk.org/doc/nightly/html/classvtkLogoWidget.html)

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 4, 2021, 10:23am UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/4 "2021-10-04T10:23:37Z")

</div>

thanks Mathieu, that widget was actually the first thing I tried, but there I ran into the issue that I want the image to be static (ie, no user interaction).

.SelectableOff() should take care of that, but on my machine they don’t. ResizableOff() does work but then I can still move it.

So using an actor2d seemed to make more sense.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [October 4, 2021, 11:32am UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/5 "2021-10-04T11:32:37Z")

</div>

> [@rdbrn](#):
>
> .SelectableOff() should take care of that, but on my machine they don’t. ResizableOff() does work but then I can still move it.

Sounds like a bug, probably simple to fix.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2021, 1:35pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/6 "2021-10-04T13:35:30Z")

</div>

Callbacks can be used for relative positioning and scaling, using the StartEvent of the vtkRenderer. With an observer for StartEvent, you can do whatever math is needed to position the image correctly in display coordinates.

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 4, 2021, 1:39pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/7 "2021-10-04T13:39:29Z")

</div>

@mwestphal Its not a bug. I just misread the documentation.  
the widget is either movable OR selectable. So SelectableOff() enables the possibility to move it. See also the example [https://kitware.github.io/vtk-examples/site/Cxx/Interaction/ImageRegion/](https://kitware.github.io/vtk-examples/site/Cxx/Interaction/ImageRegion/) where borderWidget-\>SelectableOff() is called to enable movement.

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 4, 2021, 1:51pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/8 "2021-10-04T13:51:26Z")

</div>

@dgobbi Thanks, but that does not solve the issue that I have. I can not figure out the math that is needed to position the actor correctly.

My idea was to

- define the position2 (upper-right) relative to position (lower-left)
- set its coordinate-system is to Viewport.  
In my understanding that would keep the actor size constant in pixels. But is doesn’t.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2021, 2:03pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/9 "2021-10-04T14:03:25Z")

</div>

> [@rdbrn](#):
>
> set its coordinate-system is to Viewport.  
> In my understanding that would keep the actor size constant in pixels. But is doesn’t.

I don’t understand. Viewport coordinates are relative to the size of the ~~window~~ renderer, so different window size means different image size.

Display coordinates have a one-to-one mapping to screen pixels. So if you set the image size in display coordinates, then that’s the size that will be used.

The only difficulty that I can see with display coordinates is that they are measured from the lower-left, not the upper-right. But the math to do that in display coordinates isn’t difficult:

```
x = window_width - image_width - x_distance_from_right_edge - 1
y = window_height - image_height - y_distance_from_top_edge - 1

```

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 4, 2021, 2:16pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/10 "2021-10-04T14:16:06Z")

</div>

@dgobbi , thanks again for the help.

> “Display coordinates have a one-to-one mapping to screen pixels. So if you set the image size in display coordinates, then that’s the size that will be used.”

So I need to use Display coordinates instead of Viewport.

then what am I doing wrong when I do:

```auto
// Read the image
  vtkNew<vtkJPEGReader> jPEGReader;
  jPEGReader->SetFileName(InputFilename.c_str());

  vtkNew<vtkImageActor> actor;
  actor->GetMapper()->SetInputConnection(jPEGReader->GetOutputPort());
  
  // Actor2D with image
  vtkNew<vtkActor2D> actor2D;
  vtkNew< vtkImageMapper>imageMapper;

  imageMapper->SetInputConnection(jPEGReader->GetOutputPort());
  imageMapper->SetColorWindow(255);
  imageMapper->SetColorLevel(127.5);
  imageMapper->RenderToRectangleOn();

// Actor2D with image
  vtkNew<vtkActor2D> actor2D;
  vtkNew< vtkImageMapper>imageMapper;

  imageMapper->SetInputConnection(jPEGReader->GetOutputPort());
  imageMapper->SetColorWindow(255);
  imageMapper->SetColorLevel(127.5);
  imageMapper->RenderToRectangleOn();

  actor2D->SetMapper(imageMapper);

  actor2D->GetPositionCoordinate()->SetValue(0, 0);

  vtkCoordinate* pos2{ actor2D->GetPosition2Coordinate() };
  pos2->SetCoordinateSystemToDisplay();
  pos2->SetValue(100, 100);

```

I’m expecting a 100x100 pixels image in the lower,left corner. But I get something that is stretched way beyond the screen. Like the 100 is interpreted as a normalized value.

> The only difficulty that I can see with display coordinates is that they are measured from the lower-left, not the upper-right. But the math to do that in display coordinates isn’t difficult:

I just define a coordinate in the normalized viewport at (1,1) and then define position (lower,left) at (-width, -height) display-coordinates relative to that. That part actually works.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2021, 2:16pm UTC](https://discourse.vtk.org/t/positioning-and-scaling-of-actor2d-with-image/6754/11 "2021-10-04T14:16:11Z")

</div>

Nevermind, I think I’m confusing “viewport coordinates” and “normalized viewport coordinates”. So I’m just going to keep quiet for a while until I can find time to go back and study the docs.
