Search code examples
paraviewatan2

ParaView Calculator atan2


I'm currently trying to convert given Cartesian coordinates (x,y,z) to spherical coordinates (r, theta, phi) using the ParaView Calculator-Filter, where theta is the polar angle and phi the azimuthal angle. This I want to do over a domain of a quarter-sphere:

(r in [r_inn, r_out], theta in [0, pi], phi in [0, 2pi].

So far I defined the following result variables that give the expected results:

r = sqrt(coordsX^2 + coordsY^2 + coordsZ^2)

theta = acos(coordsZ/r)

For the azimuthal vector I'm aware that I would have to take care of the quadrant of (x,y) when using

phi = atan(y/x).

This usually is achieved using an extra function like atan2 in C. Such a function does not seem to be supplied by the Calculator Filter or the Python Calculator Filter.

Is there any easy way to implementing something like the atan2 using the graphical interface?

Any comments are much appreciated, thanks!

UPDATE:

After Neil Twist pointed out, that in the Python Calculator the inverse tangent function can be called as arctan2(y, x), I'm now facing the problem that I can't access the coordinates of a cell via the variables coordsX/Y/Z, that are available in the simple Calculator filter.

Now, the question is: How can I access the cell coordinates in the Python calculator?


Solution

  • You can use the numpy extensions of the Python Calculator in ParaView, but numpy have called the function arctan2 rather than atan2.

    There are numpy docs for trigonometric functions, but annoyingly you can't use all the functions directly, for example you can do arctan2(x1, x2), but you can't do pi and have to use numpy.pi.

    For context, there are PythonCalculator docs too.

    To access the coordsX and coordsY is a bit tricky, but can be achieved using the points variable. This is actually an array of all the points, each of which is an array of x, y and z coords.

    To use the co-ordinates you need to extract them like so:

    [point[0] for point in points]
    [point[1] for point in points]
    [point[2] for point in points]
    

    So to use the arctan function with Y and X coords, you could do the following:

    arctan2([point[1] for point in points], [point[0] for point in points])
    

    Update: After a little more investigation, there may be a slightly nicer way to get coordsX/Y/Z:

    points[:,0]
    points[:,1]
    points[:,2]
    

    giving

    arctan2(points[:,1], points[:,0])
    

    Another helpful reference is the numpy_interface algorithms.