0

In my program(python based), a point need to be converted from world coordinate([x,y,z]) to view coordinate([j,k,t],j and k are between -1 and 1,t is the depth) in VTK. I find the vtkCoordinate class with SetCoordinateSystemToView() method. But it does not work .

coordinate = vtk.vtkCoordinate()
coordinate.SetCoordinateSystemToWorld()  
coordinate.SetValue(x,y,z)
coordinate.SetCoordinateSystemToDisplay() 
viewCoord=coordinate.GetComputedValue(renderer)

The result is very odd and definitely wrong. There are some methods like GetComputedDisplayValue() or GetComputedViewportValue() that can get the corresponding result from a coordinate system to display or viewport coordinate system, but there is no method like GetComputedViewValue() . Very confused, need help, thank you.

2 Answers2

1

This works:

import vtk

coordinate = vtk.vtkCoordinate()
coordinate.SetCoordinateSystemToWorld()
coordinate.SetValue(1,2,1)

# test:
from vedo import *
plt = Plotter()
print("press shift-I on the red dot, then press q")
plt.show(Cube(), Point([1,2,1], r=20), axes=1)

viewCoord = coordinate.GetComputedViewportValue(plt.renderer)
print(viewCoord) # matches!
mmusy
  • 1,292
  • 9
  • 10
  • Thank you. But in my program(vtk 9.0.3), the results of `GetComputedViewportValue()`and `GetComputedDisplayValue()` are the same (pixel coordinate). what I want is normalized display coordinate( [0,1]) with depth information. – Junda Zhang Oct 17 '21 at 14:23
0

There is a method world2ViewportMatrix=GetCompositeProjectionTransformMatrix(aspect,nearz,farz) in class vtkCamera which can get the matrix that convert world coordinates to viewport coordinates.

Then viewPortCoord=world2ViewportMatrix.MultiplyPoint([worldPosition[0],worldPosition[1],worldPosition[2],1])

[viewPortCoord[0]/viewPortCoord[3], viewPortCoord[1]/viewPortCoord[3]] is the viewport coordinate( [-1,1]*[-1,1]),[viewPortCoord[2]/viewPortCoord[3] is the depth([nearZ,farZ])