From 53a830a6917b511c811b09be8641ba0ca7b488a2 Mon Sep 17 00:00:00 2001 From: Matthew Tancik Date: Wed, 1 Feb 2023 13:22:32 -0800 Subject: [PATCH] Safe normalize (#1332) --- nerfstudio/utils/math.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nerfstudio/utils/math.py b/nerfstudio/utils/math.py index 9c2077f4b..937866dcc 100644 --- a/nerfstudio/utils/math.py +++ b/nerfstudio/utils/math.py @@ -269,3 +269,19 @@ def intersect_aabb( t_min, t_max = _intersect_aabb(origins, directions, aabb, max_bound=1e10, invalid_value=1e10) return t_min, t_max + + +def safe_normalize( + vectors: TensorType["batch_dim":..., "N"], + eps: float = 1e-10, +) -> TensorType["batch_dim":..., "N"]: + """Normalizes vectors. + + Args: + vectors: Vectors to normalize. + eps: Epsilon value to avoid division by zero. + + Returns: + Normalized vectors. + """ + return vectors / (torch.norm(vectors, dim=-1, keepdim=True) + eps)