From b65a61b6c969f1b4030fa7f8e5490e4433d74447 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Tue, 25 Jul 2017 12:04:40 +0100 Subject: [PATCH] New little function to normalise vectors. Sick of duplicating this code everywhere. --- fsl/utils/transform.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fsl/utils/transform.py b/fsl/utils/transform.py index 79ab8c414..481ae420f 100644 --- a/fsl/utils/transform.py +++ b/fsl/utils/transform.py @@ -22,6 +22,14 @@ spaces. The following functions are provided: axisBounds flirtMatrixToSform sformToFlirtMatrix + +And a few more functions are provided for working with vectors: + +.. autosummary:: + :nosignatures: + + veclength + normalise """ import numpy as np @@ -45,6 +53,16 @@ def concat(*xforms): return result +def veclength(vec): + """Returns the length of the given vector. """ + return np.sqrt(np.dot(vec, vec)) + + +def normalise(vec): + """Normalises the given vector to unit length. """ + return vec / veclength(vec) + + def scaleOffsetXform(scales, offsets): """Creates and returns an affine transformation matrix which encodes the specified scale(s) and offset(s). -- GitLab