26
26
from tensorflow_graphics .util import shape
27
27
28
28
29
- def cartesian_to_spherical_coordinates (point_cartesian , name = None ):
29
+ def cartesian_to_spherical_coordinates (point_cartesian , eps = None , name = None ):
30
30
"""Function to transform Cartesian coordinates to spherical coordinates.
31
31
32
32
This function assumes a right handed coordinate system with `z` pointing up.
@@ -39,6 +39,8 @@ def cartesian_to_spherical_coordinates(point_cartesian, name=None):
39
39
Args:
40
40
point_cartesian: A tensor of shape `[A1, ..., An, 3]`. In the last
41
41
dimension, the data follows the `x`, `y`, `z` order.
42
+ eps: A small `float`, to be added to the denominator. If left as `None`,
43
+ its value is automatically selected using `point_cartesian.dtype`.
42
44
name: A name for this op. Defaults to `cartesian_to_spherical_coordinates`.
43
45
44
46
Returns:
@@ -57,7 +59,7 @@ def cartesian_to_spherical_coordinates(point_cartesian, name=None):
57
59
58
60
x , y , z = tf .unstack (point_cartesian , axis = - 1 )
59
61
radius = tf .norm (tensor = point_cartesian , axis = - 1 )
60
- theta = tf .acos (safe_ops .safe_unsigned_div (z , radius ))
62
+ theta = tf .acos (safe_ops .safe_unsigned_div (z , radius , eps ))
61
63
phi = tf .atan2 (y , x )
62
64
return tf .stack ((radius , theta , phi ), axis = - 1 )
63
65
0 commit comments