# Path: third_party/xiuminglib/xiuminglib/linalg.py
# def normalize(vecs, axis=0):
#     """Normalizes vectors.
# 
#     Args:
#         vecs (array_like): 1D array for a single vector, 2D array for multiple
#             vectors, 3D array for an "image" of vectors, etc.
#         axis (int, optional): Along which axis normalization is done.
# 
#     Returns:
#         numpy.ndarray: Normalized vector(s) of the same shape as input.
#     """
#     vecs = np.array(vecs)
# 
#     if axis < 0:
#         raise ValueError("Negative index not allowed for safety")
#     n_dims = vecs.ndim
#     if axis >= n_dims:
#         raise ValueError((
#             "Can't normalize along axis %d when you only have %d dimension(s)"
#         ) % (axis, n_dims))
# 
#     norms = np.linalg.norm(vecs, axis=axis)
#     if (norms == 0.).any():
#         raise ValueError("Found zero-norm vectors")
# 
#     broadcastable = list(vecs.shape)
#     broadcastable[axis] = 1
#     vecs_normalized = np.divide(vecs, norms.reshape(broadcastable))
# 
#     return vecs_normalized


import numpy as np
from ..linalg import normalize as normalize_vec



def normalize(normal_map, norm_thres=0.5):
    """Normalizes the normal vector at each pixel of the normal map.

    Args:
        normal_map (numpy.ndarray): H-by-W-by-3 array of normal vectors.
        norm_thres (float, optional): Normalize only vectors with a norm
            greater than this; helpful to avoid errors at the boundary or
            in the background.

    Returns:
        numpy.ndarray: Normalized normal map.
    """
    norm = np.linalg.norm(normal_map, axis=-1)
    valid = norm > norm_thres

# Path: third_party/xiuminglib/xiuminglib/linalg.py
# def normalize(vecs, axis=0):
#     """Normalizes vectors.
# 
#     Args:
#         vecs (array_like): 1D array for a single vector, 2D array for multiple
#             vectors, 3D array for an "image" of vectors, etc.
#         axis (int, optional): Along which axis normalization is done.
# 
#     Returns:
#         numpy.ndarray: Normalized vector(s) of the same shape as input.
#     """
#     vecs = np.array(vecs)
# 
#     if axis < 0:
#         raise ValueError("Negative index not allowed for safety")
#     n_dims = vecs.ndim
#     if axis >= n_dims:
#         raise ValueError((
#             "Can't normalize along axis %d when you only have %d dimension(s)"
#         ) % (axis, n_dims))
# 
#     norms = np.linalg.norm(vecs, axis=axis)
#     if (norms == 0.).any():
#         raise ValueError("Found zero-norm vectors")
# 
#     broadcastable = list(vecs.shape)
#     broadcastable[axis] = 1
#     vecs_normalized = np.divide(vecs, norms.reshape(broadcastable))
# 
#     return vecs_normalized


# Path: third_party/xiuminglib/xiuminglib/geometry/normal.py
import numpy as np
from ..linalg import normalize as normalize_vec




def normalize(normal_map, norm_thres=0.5):
    """Normalizes the normal vector at each pixel of the normal map.

    Args:
        normal_map (numpy.ndarray): H-by-W-by-3 array of normal vectors.
        norm_thres (float, optional): Normalize only vectors with a norm
            greater than this; helpful to avoid errors at the boundary or
            in the background.

    Returns:
        numpy.ndarray: Normalized normal map.
    """
    norm = np.linalg.norm(normal_map, axis=-1)
    valid = norm > norm_thres

    normal_map[valid] = normalize_vec(normal_map[valid], axis=1)