/* Generated by Cython 0.19 on Tue Dec 24 01:38:16 2013 */

#define PY_SSIZE_T_CLEAN
#ifndef CYTHON_USE_PYLONG_INTERNALS
#ifdef PYLONG_BITS_IN_DIGIT
#define CYTHON_USE_PYLONG_INTERNALS 0
#else
#include "pyconfig.h"
#ifdef PYLONG_BITS_IN_DIGIT
#define CYTHON_USE_PYLONG_INTERNALS 1
#else
#define CYTHON_USE_PYLONG_INTERNALS 0
#endif
#endif
#endif
#include "Python.h"
#ifndef Py_PYTHON_H
    #error Python headers needed to compile C extensions, please install development version of Python.
#elif PY_VERSION_HEX < 0x02040000
    #error Cython requires Python 2.4+.
#else
#include <stddef.h> /* For offsetof */
#ifndef offsetof
#define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
#endif
#if !defined(WIN32) && !defined(MS_WINDOWS)
  #ifndef __stdcall
    #define __stdcall
  #endif
  #ifndef __cdecl
    #define __cdecl
  #endif
  #ifndef __fastcall
    #define __fastcall
  #endif
#endif
#ifndef DL_IMPORT
  #define DL_IMPORT(t) t
#endif
#ifndef DL_EXPORT
  #define DL_EXPORT(t) t
#endif
#ifndef PY_LONG_LONG
  #define PY_LONG_LONG LONG_LONG
#endif
#ifndef Py_HUGE_VAL
  #define Py_HUGE_VAL HUGE_VAL
#endif
#ifdef PYPY_VERSION
#define CYTHON_COMPILING_IN_PYPY 1
#define CYTHON_COMPILING_IN_CPYTHON 0
#else
#define CYTHON_COMPILING_IN_PYPY 0
#define CYTHON_COMPILING_IN_CPYTHON 1
#endif
#if PY_VERSION_HEX < 0x02050000
  typedef int Py_ssize_t;
  #define PY_SSIZE_T_MAX INT_MAX
  #define PY_SSIZE_T_MIN INT_MIN
  #define PY_FORMAT_SIZE_T ""
  #define CYTHON_FORMAT_SSIZE_T ""
  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
  #define PyInt_AsSsize_t(o)   __Pyx_PyInt_AsInt(o)
  #define PyNumber_Index(o)    ((PyNumber_Check(o) && !PyFloat_Check(o)) ? PyNumber_Int(o) : \
                                (PyErr_Format(PyExc_TypeError, \
                                              "expected index value, got %.200s", Py_TYPE(o)->tp_name), \
                                 (PyObject*)0))
  #define __Pyx_PyIndex_Check(o) (PyNumber_Check(o) && !PyFloat_Check(o) && \
                                  !PyComplex_Check(o))
  #define PyIndex_Check __Pyx_PyIndex_Check
  #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
  #define __PYX_BUILD_PY_SSIZE_T "i"
#else
  #define __PYX_BUILD_PY_SSIZE_T "n"
  #define CYTHON_FORMAT_SSIZE_T "z"
  #define __Pyx_PyIndex_Check PyIndex_Check
#endif
#if PY_VERSION_HEX < 0x02060000
  #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
  #define Py_TYPE(ob)   (((PyObject*)(ob))->ob_type)
  #define Py_SIZE(ob)   (((PyVarObject*)(ob))->ob_size)
  #define PyVarObject_HEAD_INIT(type, size) \
          PyObject_HEAD_INIT(type) size,
  #define PyType_Modified(t)
  typedef struct {
     void *buf;
     PyObject *obj;
     Py_ssize_t len;
     Py_ssize_t itemsize;
     int readonly;
     int ndim;
     char *format;
     Py_ssize_t *shape;
     Py_ssize_t *strides;
     Py_ssize_t *suboffsets;
     void *internal;
  } Py_buffer;
  #define PyBUF_SIMPLE 0
  #define PyBUF_WRITABLE 0x0001
  #define PyBUF_FORMAT 0x0004
  #define PyBUF_ND 0x0008
  #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
  #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
  #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
  #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
  #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
  #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_FORMAT | PyBUF_WRITABLE)
  #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_FORMAT | PyBUF_WRITABLE)
  typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
  typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
#endif
#if PY_MAJOR_VERSION < 3
  #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
  #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \
          PyCode_New(a, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)
#else
  #define __Pyx_BUILTIN_MODULE_NAME "builtins"
  #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \
          PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)
#endif
#if PY_MAJOR_VERSION < 3 && PY_MINOR_VERSION < 6
  #define PyUnicode_FromString(s) PyUnicode_Decode(s, strlen(s), "UTF-8", "strict")
#endif
#if PY_MAJOR_VERSION >= 3
  #define Py_TPFLAGS_CHECKTYPES 0
  #define Py_TPFLAGS_HAVE_INDEX 0
#endif
#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
  #define Py_TPFLAGS_HAVE_NEWBUFFER 0
#endif
#if PY_VERSION_HEX < 0x02060000
  #define Py_TPFLAGS_HAVE_VERSION_TAG 0
#endif
#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND)
  #define CYTHON_PEP393_ENABLED 1
  #define __Pyx_PyUnicode_READY(op)       (likely(PyUnicode_IS_READY(op)) ? \
                                              0 : _PyUnicode_Ready((PyObject *)(op)))
  #define __Pyx_PyUnicode_GET_LENGTH(u)   PyUnicode_GET_LENGTH(u)
  #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i)
  #define __Pyx_PyUnicode_READ(k, d, i)   PyUnicode_READ(k, d, i)
#else
  #define CYTHON_PEP393_ENABLED 0
  #define __Pyx_PyUnicode_READY(op)       (0)
  #define __Pyx_PyUnicode_GET_LENGTH(u)   PyUnicode_GET_SIZE(u)
  #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i]))
  #define __Pyx_PyUnicode_READ(k, d, i)   ((k=k), (Py_UCS4)(((Py_UNICODE*)d)[i]))
#endif
#if PY_MAJOR_VERSION >= 3
  #define PyBaseString_Type            PyUnicode_Type
  #define PyStringObject               PyUnicodeObject
  #define PyString_Type                PyUnicode_Type
  #define PyString_Check               PyUnicode_Check
  #define PyString_CheckExact          PyUnicode_CheckExact
#endif
#if PY_VERSION_HEX < 0x02060000
  #define PyBytesObject                PyStringObject
  #define PyBytes_Type                 PyString_Type
  #define PyBytes_Check                PyString_Check
  #define PyBytes_CheckExact           PyString_CheckExact
  #define PyBytes_FromString           PyString_FromString
  #define PyBytes_FromStringAndSize    PyString_FromStringAndSize
  #define PyBytes_FromFormat           PyString_FromFormat
  #define PyBytes_DecodeEscape         PyString_DecodeEscape
  #define PyBytes_AsString             PyString_AsString
  #define PyBytes_AsStringAndSize      PyString_AsStringAndSize
  #define PyBytes_Size                 PyString_Size
  #define PyBytes_AS_STRING            PyString_AS_STRING
  #define PyBytes_GET_SIZE             PyString_GET_SIZE
  #define PyBytes_Repr                 PyString_Repr
  #define PyBytes_Concat               PyString_Concat
  #define PyBytes_ConcatAndDel         PyString_ConcatAndDel
#endif
#if PY_MAJOR_VERSION >= 3
  #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj)
  #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj)
#else
  #define __Pyx_PyBaseString_Check(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj) || \
                                         PyString_Check(obj) || PyUnicode_Check(obj))
  #define __Pyx_PyBaseString_CheckExact(obj) (Py_TYPE(obj) == &PyBaseString_Type)
#endif
#if PY_VERSION_HEX < 0x02060000
  #define PySet_Check(obj)             PyObject_TypeCheck(obj, &PySet_Type)
  #define PyFrozenSet_Check(obj)       PyObject_TypeCheck(obj, &PyFrozenSet_Type)
#endif
#ifndef PySet_CheckExact
  #define PySet_CheckExact(obj)        (Py_TYPE(obj) == &PySet_Type)
#endif
#define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)
#if PY_MAJOR_VERSION >= 3
  #define PyIntObject                  PyLongObject
  #define PyInt_Type                   PyLong_Type
  #define PyInt_Check(op)              PyLong_Check(op)
  #define PyInt_CheckExact(op)         PyLong_CheckExact(op)
  #define PyInt_FromString             PyLong_FromString
  #define PyInt_FromUnicode            PyLong_FromUnicode
  #define PyInt_FromLong               PyLong_FromLong
  #define PyInt_FromSize_t             PyLong_FromSize_t
  #define PyInt_FromSsize_t            PyLong_FromSsize_t
  #define PyInt_AsLong                 PyLong_AsLong
  #define PyInt_AS_LONG                PyLong_AS_LONG
  #define PyInt_AsSsize_t              PyLong_AsSsize_t
  #define PyInt_AsUnsignedLongMask     PyLong_AsUnsignedLongMask
  #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
#endif
#if PY_MAJOR_VERSION >= 3
  #define PyBoolObject                 PyLongObject
#endif
#if PY_VERSION_HEX < 0x03020000
  typedef long Py_hash_t;
  #define __Pyx_PyInt_FromHash_t PyInt_FromLong
  #define __Pyx_PyInt_AsHash_t   PyInt_AsLong
#else
  #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t
  #define __Pyx_PyInt_AsHash_t   PyInt_AsSsize_t
#endif
#if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300)
  #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b)
  #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value)
  #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b)
#else
  #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \
        (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), (PyObject*)0) : \
        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \
            (PyErr_Format(PyExc_TypeError, "'%.200s' object is unsliceable", (obj)->ob_type->tp_name), (PyObject*)0)))
  #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \
        (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \
        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \
            (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice assignment", (obj)->ob_type->tp_name), -1)))
  #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \
        (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \
        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \
            (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice deletion", (obj)->ob_type->tp_name), -1)))
#endif
#if PY_MAJOR_VERSION >= 3
  #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func))
#endif
#if PY_VERSION_HEX < 0x02050000
  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),((char *)(n)))
  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))
  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),((char *)(n)))
#else
  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),(n))
  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))
  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),(n))
#endif
#if PY_VERSION_HEX < 0x02050000
  #define __Pyx_NAMESTR(n) ((char *)(n))
  #define __Pyx_DOCSTR(n)  ((char *)(n))
#else
  #define __Pyx_NAMESTR(n) (n)
  #define __Pyx_DOCSTR(n)  (n)
#endif
#ifndef CYTHON_INLINE
  #if defined(__GNUC__)
    #define CYTHON_INLINE __inline__
  #elif defined(_MSC_VER)
    #define CYTHON_INLINE __inline
  #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
    #define CYTHON_INLINE inline
  #else
    #define CYTHON_INLINE
  #endif
#endif
#ifndef CYTHON_RESTRICT
  #if defined(__GNUC__)
    #define CYTHON_RESTRICT __restrict__
  #elif defined(_MSC_VER)
    #define CYTHON_RESTRICT __restrict
  #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
    #define CYTHON_RESTRICT restrict
  #else
    #define CYTHON_RESTRICT
  #endif
#endif
#ifdef NAN
#define __PYX_NAN() ((float) NAN)
#else
static CYTHON_INLINE float __PYX_NAN() {
  /* Initialize NaN. The sign is irrelevant, an exponent with all bits 1 and
   a nonzero mantissa means NaN. If the first bit in the mantissa is 1, it is
   a quiet NaN. */
  float value;
  memset(&value, 0xFF, sizeof(value));
  return value;
}
#endif


#if PY_MAJOR_VERSION >= 3
  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)
  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceTrueDivide(x,y)
#else
  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_Divide(x,y)
  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceDivide(x,y)
#endif

#ifndef __PYX_EXTERN_C
  #ifdef __cplusplus
    #define __PYX_EXTERN_C extern "C"
  #else
    #define __PYX_EXTERN_C extern
  #endif
#endif

#if defined(WIN32) || defined(MS_WINDOWS)
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#define __PYX_HAVE__dipy__tracking__distances
#define __PYX_HAVE_API__dipy__tracking__distances
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "dpy_math.h"
#ifdef _OPENMP
#include <omp.h>
#endif /* _OPENMP */

#ifdef PYREX_WITHOUT_ASSERTIONS
#define CYTHON_WITHOUT_ASSERTIONS
#endif

#ifndef CYTHON_UNUSED
# if defined(__GNUC__)
#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
#     define CYTHON_UNUSED __attribute__ ((__unused__))
#   else
#     define CYTHON_UNUSED
#   endif
# elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER))
#   define CYTHON_UNUSED __attribute__ ((__unused__))
# else
#   define CYTHON_UNUSED
# endif
#endif
typedef struct {PyObject **p; char *s; const Py_ssize_t n; const char* encoding;
                const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/

#define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0
#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 0
#define __PYX_DEFAULT_STRING_ENCODING ""
#define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString
#define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize
static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*);
static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);
#define __Pyx_PyBytes_FromString        PyBytes_FromString
#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char*);
#if PY_MAJOR_VERSION < 3
    #define __Pyx_PyStr_FromString        __Pyx_PyBytes_FromString
    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize
#else
    #define __Pyx_PyStr_FromString        __Pyx_PyUnicode_FromString
    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize
#endif
#define __Pyx_PyObject_AsUString(s)    ((unsigned char*) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_FromUString(s)  __Pyx_PyObject_FromString((char*)s)
#define __Pyx_PyBytes_FromUString(s)   __Pyx_PyBytes_FromString((char*)s)
#define __Pyx_PyStr_FromUString(s)     __Pyx_PyStr_FromString((char*)s)
#define __Pyx_PyUnicode_FromUString(s) __Pyx_PyUnicode_FromString((char*)s)
#if PY_MAJOR_VERSION < 3
static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)
{
    const Py_UNICODE *u_end = u;
    while (*u_end++) ;
    return u_end - u - 1;
}
#else
#define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen
#endif
#define __Pyx_PyUnicode_FromUnicode(u)       PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u))
#define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode
#define __Pyx_PyUnicode_AsUnicode            PyUnicode_AsUnicode
#define __Pyx_Owned_Py_None(b) (Py_INCREF(Py_None), Py_None)
#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*);
#if CYTHON_COMPILING_IN_CPYTHON
#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
#else
#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
#endif
#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))
#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
static int __Pyx_sys_getdefaultencoding_not_ascii;
static int __Pyx_init_sys_getdefaultencoding_params() {
    PyObject* sys = NULL;
    PyObject* default_encoding = NULL;
    PyObject* ascii_chars_u = NULL;
    PyObject* ascii_chars_b = NULL;
    sys = PyImport_ImportModule("sys");
    if (sys == NULL) goto bad;
    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
    if (default_encoding == NULL) goto bad;
    if (strcmp(PyBytes_AsString(default_encoding), "ascii") == 0) {
        __Pyx_sys_getdefaultencoding_not_ascii = 0;
    } else {
        const char* default_encoding_c = PyBytes_AS_STRING(default_encoding);
        char ascii_chars[128];
        int c;
        for (c = 0; c < 128; c++) {
            ascii_chars[c] = c;
        }
        __Pyx_sys_getdefaultencoding_not_ascii = 1;
        ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);
        if (ascii_chars_u == NULL) goto bad;
        ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);
        if (ascii_chars_b == NULL || strncmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {
            PyErr_Format(
                PyExc_ValueError,
                "This module compiled with c_string_encoding=ascii, but default encoding '%s' is not a superset of ascii.",
                default_encoding_c);
            goto bad;
        }
    }
    Py_XDECREF(sys);
    Py_XDECREF(default_encoding);
    Py_XDECREF(ascii_chars_u);
    Py_XDECREF(ascii_chars_b);
    return 0;
bad:
    Py_XDECREF(sys);
    Py_XDECREF(default_encoding);
    Py_XDECREF(ascii_chars_u);
    Py_XDECREF(ascii_chars_b);
    return -1;
}
#endif
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)
#else
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
static char* __PYX_DEFAULT_STRING_ENCODING;
static int __Pyx_init_sys_getdefaultencoding_params() {
    PyObject* sys = NULL;
    PyObject* default_encoding = NULL;
    char* default_encoding_c;
    sys = PyImport_ImportModule("sys");
    if (sys == NULL) goto bad;
    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
    if (default_encoding == NULL) goto bad;
    default_encoding_c = PyBytes_AS_STRING(default_encoding);
    __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c));
    strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
    Py_DECREF(sys);
    Py_DECREF(default_encoding);
    return 0;
bad:
    Py_XDECREF(sys);
    Py_XDECREF(default_encoding);
    return -1;
}
#endif
#endif


#ifdef __GNUC__
  /* Test for GCC > 2.95 */
  #if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
    #define likely(x)   __builtin_expect(!!(x), 1)
    #define unlikely(x) __builtin_expect(!!(x), 0)
  #else /* __GNUC__ > 2 ... */
    #define likely(x)   (x)
    #define unlikely(x) (x)
  #endif /* __GNUC__ > 2 ... */
#else /* __GNUC__ */
  #define likely(x)   (x)
  #define unlikely(x) (x)
#endif /* __GNUC__ */

static PyObject *__pyx_m;
static PyObject *__pyx_d;
static PyObject *__pyx_b;
static PyObject *__pyx_empty_tuple;
static PyObject *__pyx_empty_bytes;
static int __pyx_lineno;
static int __pyx_clineno = 0;
static const char * __pyx_cfilenm= __FILE__;
static const char *__pyx_filename;

#if !defined(CYTHON_CCOMPLEX)
  #if defined(__cplusplus)
    #define CYTHON_CCOMPLEX 1
  #elif defined(_Complex_I)
    #define CYTHON_CCOMPLEX 1
  #else
    #define CYTHON_CCOMPLEX 0
  #endif
#endif
#if CYTHON_CCOMPLEX
  #ifdef __cplusplus
    #include <complex>
  #else
    #include <complex.h>
  #endif
#endif
#if CYTHON_CCOMPLEX && !defined(__cplusplus) && defined(__sun__) && defined(__GNUC__)
  #undef _Complex_I
  #define _Complex_I 1.0fj
#endif


static const char *__pyx_f[] = {
  "distances.pyx",
  "numpy.pxd",
  "type.pxd",
};
#define IS_UNSIGNED(type) (((type) -1) > 0)
struct __Pyx_StructField_;
#define __PYX_BUF_FLAGS_PACKED_STRUCT (1 << 0)
typedef struct {
  const char* name; /* for error messages only */
  struct __Pyx_StructField_* fields;
  size_t size;     /* sizeof(type) */
  size_t arraysize[8]; /* length of array in each dimension */
  int ndim;
  char typegroup; /* _R_eal, _C_omplex, Signed _I_nt, _U_nsigned int, _S_truct, _P_ointer, _O_bject, c_H_ar */
  char is_unsigned;
  int flags;
} __Pyx_TypeInfo;
typedef struct __Pyx_StructField_ {
  __Pyx_TypeInfo* type;
  const char* name;
  size_t offset;
} __Pyx_StructField;
typedef struct {
  __Pyx_StructField* field;
  size_t parent_offset;
} __Pyx_BufFmt_StackElem;
typedef struct {
  __Pyx_StructField root;
  __Pyx_BufFmt_StackElem* head;
  size_t fmt_offset;
  size_t new_count, enc_count;
  size_t struct_alignment;
  int is_complex;
  char enc_type;
  char new_packmode;
  char enc_packmode;
  char is_valid_array;
} __Pyx_BufFmt_Context;


/* "numpy.pxd":723
 * # in Cython to enable them only on the right systems.
 * 
 * ctypedef npy_int8       int8_t             # <<<<<<<<<<<<<<
 * ctypedef npy_int16      int16_t
 * ctypedef npy_int32      int32_t
 */
typedef npy_int8 __pyx_t_5numpy_int8_t;

/* "numpy.pxd":724
 * 
 * ctypedef npy_int8       int8_t
 * ctypedef npy_int16      int16_t             # <<<<<<<<<<<<<<
 * ctypedef npy_int32      int32_t
 * ctypedef npy_int64      int64_t
 */
typedef npy_int16 __pyx_t_5numpy_int16_t;

/* "numpy.pxd":725
 * ctypedef npy_int8       int8_t
 * ctypedef npy_int16      int16_t
 * ctypedef npy_int32      int32_t             # <<<<<<<<<<<<<<
 * ctypedef npy_int64      int64_t
 * #ctypedef npy_int96      int96_t
 */
typedef npy_int32 __pyx_t_5numpy_int32_t;

/* "numpy.pxd":726
 * ctypedef npy_int16      int16_t
 * ctypedef npy_int32      int32_t
 * ctypedef npy_int64      int64_t             # <<<<<<<<<<<<<<
 * #ctypedef npy_int96      int96_t
 * #ctypedef npy_int128     int128_t
 */
typedef npy_int64 __pyx_t_5numpy_int64_t;

/* "numpy.pxd":730
 * #ctypedef npy_int128     int128_t
 * 
 * ctypedef npy_uint8      uint8_t             # <<<<<<<<<<<<<<
 * ctypedef npy_uint16     uint16_t
 * ctypedef npy_uint32     uint32_t
 */
typedef npy_uint8 __pyx_t_5numpy_uint8_t;

/* "numpy.pxd":731
 * 
 * ctypedef npy_uint8      uint8_t
 * ctypedef npy_uint16     uint16_t             # <<<<<<<<<<<<<<
 * ctypedef npy_uint32     uint32_t
 * ctypedef npy_uint64     uint64_t
 */
typedef npy_uint16 __pyx_t_5numpy_uint16_t;

/* "numpy.pxd":732
 * ctypedef npy_uint8      uint8_t
 * ctypedef npy_uint16     uint16_t
 * ctypedef npy_uint32     uint32_t             # <<<<<<<<<<<<<<
 * ctypedef npy_uint64     uint64_t
 * #ctypedef npy_uint96     uint96_t
 */
typedef npy_uint32 __pyx_t_5numpy_uint32_t;

/* "numpy.pxd":733
 * ctypedef npy_uint16     uint16_t
 * ctypedef npy_uint32     uint32_t
 * ctypedef npy_uint64     uint64_t             # <<<<<<<<<<<<<<
 * #ctypedef npy_uint96     uint96_t
 * #ctypedef npy_uint128    uint128_t
 */
typedef npy_uint64 __pyx_t_5numpy_uint64_t;

/* "numpy.pxd":737
 * #ctypedef npy_uint128    uint128_t
 * 
 * ctypedef npy_float32    float32_t             # <<<<<<<<<<<<<<
 * ctypedef npy_float64    float64_t
 * #ctypedef npy_float80    float80_t
 */
typedef npy_float32 __pyx_t_5numpy_float32_t;

/* "numpy.pxd":738
 * 
 * ctypedef npy_float32    float32_t
 * ctypedef npy_float64    float64_t             # <<<<<<<<<<<<<<
 * #ctypedef npy_float80    float80_t
 * #ctypedef npy_float128   float128_t
 */
typedef npy_float64 __pyx_t_5numpy_float64_t;

/* "numpy.pxd":747
 * # The int types are mapped a bit surprising --
 * # numpy.int corresponds to 'l' and numpy.long to 'q'
 * ctypedef npy_long       int_t             # <<<<<<<<<<<<<<
 * ctypedef npy_longlong   long_t
 * ctypedef npy_longlong   longlong_t
 */
typedef npy_long __pyx_t_5numpy_int_t;

/* "numpy.pxd":748
 * # numpy.int corresponds to 'l' and numpy.long to 'q'
 * ctypedef npy_long       int_t
 * ctypedef npy_longlong   long_t             # <<<<<<<<<<<<<<
 * ctypedef npy_longlong   longlong_t
 * 
 */
typedef npy_longlong __pyx_t_5numpy_long_t;

/* "numpy.pxd":749
 * ctypedef npy_long       int_t
 * ctypedef npy_longlong   long_t
 * ctypedef npy_longlong   longlong_t             # <<<<<<<<<<<<<<
 * 
 * ctypedef npy_ulong      uint_t
 */
typedef npy_longlong __pyx_t_5numpy_longlong_t;

/* "numpy.pxd":751
 * ctypedef npy_longlong   longlong_t
 * 
 * ctypedef npy_ulong      uint_t             # <<<<<<<<<<<<<<
 * ctypedef npy_ulonglong  ulong_t
 * ctypedef npy_ulonglong  ulonglong_t
 */
typedef npy_ulong __pyx_t_5numpy_uint_t;

/* "numpy.pxd":752
 * 
 * ctypedef npy_ulong      uint_t
 * ctypedef npy_ulonglong  ulong_t             # <<<<<<<<<<<<<<
 * ctypedef npy_ulonglong  ulonglong_t
 * 
 */
typedef npy_ulonglong __pyx_t_5numpy_ulong_t;

/* "numpy.pxd":753
 * ctypedef npy_ulong      uint_t
 * ctypedef npy_ulonglong  ulong_t
 * ctypedef npy_ulonglong  ulonglong_t             # <<<<<<<<<<<<<<
 * 
 * ctypedef npy_intp       intp_t
 */
typedef npy_ulonglong __pyx_t_5numpy_ulonglong_t;

/* "numpy.pxd":755
 * ctypedef npy_ulonglong  ulonglong_t
 * 
 * ctypedef npy_intp       intp_t             # <<<<<<<<<<<<<<
 * ctypedef npy_uintp      uintp_t
 * 
 */
typedef npy_intp __pyx_t_5numpy_intp_t;

/* "numpy.pxd":756
 * 
 * ctypedef npy_intp       intp_t
 * ctypedef npy_uintp      uintp_t             # <<<<<<<<<<<<<<
 * 
 * ctypedef npy_double     float_t
 */
typedef npy_uintp __pyx_t_5numpy_uintp_t;

/* "numpy.pxd":758
 * ctypedef npy_uintp      uintp_t
 * 
 * ctypedef npy_double     float_t             # <<<<<<<<<<<<<<
 * ctypedef npy_double     double_t
 * ctypedef npy_longdouble longdouble_t
 */
typedef npy_double __pyx_t_5numpy_float_t;

/* "numpy.pxd":759
 * 
 * ctypedef npy_double     float_t
 * ctypedef npy_double     double_t             # <<<<<<<<<<<<<<
 * ctypedef npy_longdouble longdouble_t
 * 
 */
typedef npy_double __pyx_t_5numpy_double_t;

/* "numpy.pxd":760
 * ctypedef npy_double     float_t
 * ctypedef npy_double     double_t
 * ctypedef npy_longdouble longdouble_t             # <<<<<<<<<<<<<<
 * 
 * ctypedef npy_cfloat      cfloat_t
 */
typedef npy_longdouble __pyx_t_5numpy_longdouble_t;
#if CYTHON_CCOMPLEX
  #ifdef __cplusplus
    typedef ::std::complex< float > __pyx_t_float_complex;
  #else
    typedef float _Complex __pyx_t_float_complex;
  #endif
#else
    typedef struct { float real, imag; } __pyx_t_float_complex;
#endif

#if CYTHON_CCOMPLEX
  #ifdef __cplusplus
    typedef ::std::complex< double > __pyx_t_double_complex;
  #else
    typedef double _Complex __pyx_t_double_complex;
  #endif
#else
    typedef struct { double real, imag; } __pyx_t_double_complex;
#endif


/*--- Type declarations ---*/

/* "numpy.pxd":762
 * ctypedef npy_longdouble longdouble_t
 * 
 * ctypedef npy_cfloat      cfloat_t             # <<<<<<<<<<<<<<
 * ctypedef npy_cdouble     cdouble_t
 * ctypedef npy_clongdouble clongdouble_t
 */
typedef npy_cfloat __pyx_t_5numpy_cfloat_t;

/* "numpy.pxd":763
 * 
 * ctypedef npy_cfloat      cfloat_t
 * ctypedef npy_cdouble     cdouble_t             # <<<<<<<<<<<<<<
 * ctypedef npy_clongdouble clongdouble_t
 * 
 */
typedef npy_cdouble __pyx_t_5numpy_cdouble_t;

/* "numpy.pxd":764
 * ctypedef npy_cfloat      cfloat_t
 * ctypedef npy_cdouble     cdouble_t
 * ctypedef npy_clongdouble clongdouble_t             # <<<<<<<<<<<<<<
 * 
 * ctypedef npy_cdouble     complex_t
 */
typedef npy_clongdouble __pyx_t_5numpy_clongdouble_t;

/* "numpy.pxd":766
 * ctypedef npy_clongdouble clongdouble_t
 * 
 * ctypedef npy_cdouble     complex_t             # <<<<<<<<<<<<<<
 * 
 * cdef inline object PyArray_MultiIterNew1(a):
 */
typedef npy_cdouble __pyx_t_5numpy_complex_t;
struct __pyx_t_4dipy_8tracking_9distances_LSC_Cluster;
typedef struct __pyx_t_4dipy_8tracking_9distances_LSC_Cluster __pyx_t_4dipy_8tracking_9distances_LSC_Cluster;

/* "dipy/tracking/distances.pyx":1460
 * 
 * 
 * ctypedef struct LSC_Cluster:             # <<<<<<<<<<<<<<
 *     long *indices
 *     float *hidden
 */
struct __pyx_t_4dipy_8tracking_9distances_LSC_Cluster {
  long *indices;
  float *hidden;
  long N;
};
#ifndef CYTHON_REFNANNY
  #define CYTHON_REFNANNY 0
#endif
#if CYTHON_REFNANNY
  typedef struct {
    void (*INCREF)(void*, PyObject*, int);
    void (*DECREF)(void*, PyObject*, int);
    void (*GOTREF)(void*, PyObject*, int);
    void (*GIVEREF)(void*, PyObject*, int);
    void* (*SetupContext)(const char*, int, const char*);
    void (*FinishContext)(void**);
  } __Pyx_RefNannyAPIStruct;
  static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;
  static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); /*proto*/
  #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL;
#ifdef WITH_THREAD
  #define __Pyx_RefNannySetupContext(name, acquire_gil) \
          if (acquire_gil) { \
              PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); \
              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \
              PyGILState_Release(__pyx_gilstate_save); \
          } else { \
              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \
          }
#else
  #define __Pyx_RefNannySetupContext(name, acquire_gil) \
          __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
#endif
  #define __Pyx_RefNannyFinishContext() \
          __Pyx_RefNanny->FinishContext(&__pyx_refnanny)
  #define __Pyx_INCREF(r)  __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
  #define __Pyx_DECREF(r)  __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
  #define __Pyx_GOTREF(r)  __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
  #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
  #define __Pyx_XINCREF(r)  do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0)
  #define __Pyx_XDECREF(r)  do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0)
  #define __Pyx_XGOTREF(r)  do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0)
  #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0)
#else
  #define __Pyx_RefNannyDeclarations
  #define __Pyx_RefNannySetupContext(name, acquire_gil)
  #define __Pyx_RefNannyFinishContext()
  #define __Pyx_INCREF(r) Py_INCREF(r)
  #define __Pyx_DECREF(r) Py_DECREF(r)
  #define __Pyx_GOTREF(r)
  #define __Pyx_GIVEREF(r)
  #define __Pyx_XINCREF(r) Py_XINCREF(r)
  #define __Pyx_XDECREF(r) Py_XDECREF(r)
  #define __Pyx_XGOTREF(r)
  #define __Pyx_XGIVEREF(r)
#endif /* CYTHON_REFNANNY */
#define __Pyx_CLEAR(r)    do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0)
#define __Pyx_XCLEAR(r)   do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0)

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) {
    PyTypeObject* tp = Py_TYPE(obj);
    if (likely(tp->tp_getattro))
        return tp->tp_getattro(obj, attr_name);
#if PY_MAJOR_VERSION < 3
    if (likely(tp->tp_getattr))
        return tp->tp_getattr(obj, PyString_AS_STRING(attr_name));
#endif
    return PyObject_GetAttr(obj, attr_name);
}
#else
#define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)
#endif

static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/

static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/

static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/

static CYTHON_INLINE int  __Pyx_GetBufferAndValidate(Py_buffer* buf, PyObject* obj,
    __Pyx_TypeInfo* dtype, int flags, int nd, int cast, __Pyx_BufFmt_StackElem* stack);
static CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info);

static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/

#ifndef __PYX_FORCE_INIT_THREADS
  #define __PYX_FORCE_INIT_THREADS 0
#endif

static void __Pyx_WriteUnraisable(const char *name, int clineno,
                                  int lineno, const char *filename); /*proto*/

static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/

static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/

static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
    PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
    const char* function_name); /*proto*/

static void __Pyx_RaiseBufferFallbackError(void); /*proto*/

#define __Pyx_GetItemInt(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
    (((size) <= sizeof(Py_ssize_t)) ? \
    __Pyx_GetItemInt_Fast(o, i, is_list, wraparound, boundscheck) : \
    __Pyx_GetItemInt_Generic(o, to_py_func(i)))
#define __Pyx_GetItemInt_List(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
    (((size) <= sizeof(Py_ssize_t)) ? \
    __Pyx_GetItemInt_List_Fast(o, i, wraparound, boundscheck) : \
    __Pyx_GetItemInt_Generic(o, to_py_func(i)))
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
                                                              int wraparound, int boundscheck);
#define __Pyx_GetItemInt_Tuple(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
    (((size) <= sizeof(Py_ssize_t)) ? \
    __Pyx_GetItemInt_Tuple_Fast(o, i, wraparound, boundscheck) : \
    __Pyx_GetItemInt_Generic(o, to_py_func(i)))
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
                                                              int wraparound, int boundscheck);
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j);
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
                                                     int is_list, int wraparound, int boundscheck);

static void __Pyx_RaiseBufferIndexError(int axis); /*proto*/

#define __Pyx_BufPtrStrided1d(type, buf, i0, s0) (type)((char*)buf + i0 * s0)
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
    PyListObject* L = (PyListObject*) list;
    Py_ssize_t len = Py_SIZE(list);
    if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {
        Py_INCREF(x);
        PyList_SET_ITEM(list, len, x);
        Py_SIZE(list) = len+1;
        return 0;
    }
    return PyList_Append(list, x);
}
#else
#define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
#endif

static PyObject* __Pyx_PyObject_CallMethodTuple(PyObject* obj, PyObject* method_name, PyObject* args) {
    PyObject *method, *result = NULL;
    if (unlikely(!args)) return NULL;
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
    if (unlikely(!method)) goto bad;
    result = PyObject_Call(method, args, NULL);
    Py_DECREF(method);
bad:
    Py_DECREF(args);
    return result;
}
#define __Pyx_PyObject_CallMethod3(obj, name, arg1, arg2, arg3) \
    __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(3, arg1, arg2, arg3))
#define __Pyx_PyObject_CallMethod2(obj, name, arg1, arg2) \
    __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(2, arg1, arg2))
#define __Pyx_PyObject_CallMethod1(obj, name, arg1) \
    __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(1, arg1))
#define __Pyx_PyObject_CallMethod0(obj, name) \
    __Pyx_PyObject_CallMethodTuple(obj, name, (Py_INCREF(__pyx_empty_tuple), __pyx_empty_tuple))

static CYTHON_INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x); /*proto*/

#define __Pyx_SetItemInt(o, i, v, size, to_py_func, is_list, wraparound, boundscheck) \
    (((size) <= sizeof(Py_ssize_t)) ? \
    __Pyx_SetItemInt_Fast(o, i, v, is_list, wraparound, boundscheck) : \
    __Pyx_SetItemInt_Generic(o, to_py_func(i), v))
static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v);
static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v,
                                               int is_list, int wraparound, int boundscheck);

static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(
        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,
        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,
        int has_cstart, int has_cstop, int wraparound);

static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/

#define __Pyx_BufPtrStrided2d(type, buf, i0, s0, i1, s1) (type)((char*)buf + i0 * s0 + i1 * s1)
#if CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
static CYTHON_INLINE int __Pyx_PyObject_SetAttrStr(PyObject* obj, PyObject* attr_name, PyObject* value) {
    PyTypeObject* tp = Py_TYPE(obj);
    if (likely(tp->tp_setattro))
        return tp->tp_setattro(obj, attr_name, value);
#if PY_MAJOR_VERSION < 3
    if (likely(tp->tp_setattr))
        return tp->tp_setattr(obj, PyString_AS_STRING(attr_name), value);
#endif
    return PyObject_SetAttr(obj, attr_name, value);
}
#else
#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
#endif

#if PY_MAJOR_VERSION >= 3
static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d); /*proto*/
#else
#define __Pyx_PyDict_Keys(d) PyDict_Keys(d)
#endif

#if PY_MAJOR_VERSION >= 3
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
    PyObject *value;
    value = PyDict_GetItemWithError(d, key);
    if (unlikely(!value)) {
        if (!PyErr_Occurred()) {
            PyObject* args = PyTuple_Pack(1, key);
            if (likely(args))
                PyErr_SetObject(PyExc_KeyError, args);
            Py_XDECREF(args);
        }
        return NULL;
    }
    Py_INCREF(value);
    return value;
}
#else
    #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
#endif

static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname);

#define __Pyx_DelItemInt(o, i, size, to_py_func, is_list, wraparound, boundscheck) \
    (((size) <= sizeof(Py_ssize_t)) ? \
    __Pyx_DelItemInt_Fast(o, i, is_list, wraparound) : \
    __Pyx_DelItem_Generic(o, to_py_func(i)))
static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j);
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
                                               CYTHON_UNUSED int is_list, int wraparound);

static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
    const char *name, int exact); /*proto*/

static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);

static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);

static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);

static CYTHON_INLINE int __Pyx_IterFinish(void); /*proto*/

static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected); /*proto*/

typedef struct {
  Py_ssize_t shape, strides, suboffsets;
} __Pyx_Buf_DimInfo;
typedef struct {
  size_t refcount;
  Py_buffer pybuffer;
} __Pyx_Buffer;
typedef struct {
  __Pyx_Buffer *rcbuffer;
  char *data;
  __Pyx_Buf_DimInfo diminfo[8];
} __Pyx_LocalBuf_ND;

#if PY_MAJOR_VERSION < 3
    static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags);
    static void __Pyx_ReleaseBuffer(Py_buffer *view);
#else
    #define __Pyx_GetBuffer PyObject_GetBuffer
    #define __Pyx_ReleaseBuffer PyBuffer_Release
#endif


static Py_ssize_t __Pyx_zeros[] = {0, 0, 0, 0, 0, 0, 0, 0};
static Py_ssize_t __Pyx_minusones[] = {-1, -1, -1, -1, -1, -1, -1, -1};

static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/

#if CYTHON_CCOMPLEX
  #ifdef __cplusplus
    #define __Pyx_CREAL(z) ((z).real())
    #define __Pyx_CIMAG(z) ((z).imag())
  #else
    #define __Pyx_CREAL(z) (__real__(z))
    #define __Pyx_CIMAG(z) (__imag__(z))
  #endif
#else
    #define __Pyx_CREAL(z) ((z).real)
    #define __Pyx_CIMAG(z) ((z).imag)
#endif
#if defined(_WIN32) && defined(__cplusplus) && CYTHON_CCOMPLEX
    #define __Pyx_SET_CREAL(z,x) ((z).real(x))
    #define __Pyx_SET_CIMAG(z,y) ((z).imag(y))
#else
    #define __Pyx_SET_CREAL(z,x) __Pyx_CREAL(z) = (x)
    #define __Pyx_SET_CIMAG(z,y) __Pyx_CIMAG(z) = (y)
#endif

static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float, float);

#if CYTHON_CCOMPLEX
    #define __Pyx_c_eqf(a, b)   ((a)==(b))
    #define __Pyx_c_sumf(a, b)  ((a)+(b))
    #define __Pyx_c_difff(a, b) ((a)-(b))
    #define __Pyx_c_prodf(a, b) ((a)*(b))
    #define __Pyx_c_quotf(a, b) ((a)/(b))
    #define __Pyx_c_negf(a)     (-(a))
  #ifdef __cplusplus
    #define __Pyx_c_is_zerof(z) ((z)==(float)0)
    #define __Pyx_c_conjf(z)    (::std::conj(z))
    #if 1
        #define __Pyx_c_absf(z)     (::std::abs(z))
        #define __Pyx_c_powf(a, b)  (::std::pow(a, b))
    #endif
  #else
    #define __Pyx_c_is_zerof(z) ((z)==0)
    #define __Pyx_c_conjf(z)    (conjf(z))
    #if 1
        #define __Pyx_c_absf(z)     (cabsf(z))
        #define __Pyx_c_powf(a, b)  (cpowf(a, b))
    #endif
 #endif
#else
    static CYTHON_INLINE int __Pyx_c_eqf(__pyx_t_float_complex, __pyx_t_float_complex);
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sumf(__pyx_t_float_complex, __pyx_t_float_complex);
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_difff(__pyx_t_float_complex, __pyx_t_float_complex);
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prodf(__pyx_t_float_complex, __pyx_t_float_complex);
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quotf(__pyx_t_float_complex, __pyx_t_float_complex);
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_negf(__pyx_t_float_complex);
    static CYTHON_INLINE int __Pyx_c_is_zerof(__pyx_t_float_complex);
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conjf(__pyx_t_float_complex);
    #if 1
        static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex);
        static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_powf(__pyx_t_float_complex, __pyx_t_float_complex);
    #endif
#endif

static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double, double);

#if CYTHON_CCOMPLEX
    #define __Pyx_c_eq(a, b)   ((a)==(b))
    #define __Pyx_c_sum(a, b)  ((a)+(b))
    #define __Pyx_c_diff(a, b) ((a)-(b))
    #define __Pyx_c_prod(a, b) ((a)*(b))
    #define __Pyx_c_quot(a, b) ((a)/(b))
    #define __Pyx_c_neg(a)     (-(a))
  #ifdef __cplusplus
    #define __Pyx_c_is_zero(z) ((z)==(double)0)
    #define __Pyx_c_conj(z)    (::std::conj(z))
    #if 1
        #define __Pyx_c_abs(z)     (::std::abs(z))
        #define __Pyx_c_pow(a, b)  (::std::pow(a, b))
    #endif
  #else
    #define __Pyx_c_is_zero(z) ((z)==0)
    #define __Pyx_c_conj(z)    (conj(z))
    #if 1
        #define __Pyx_c_abs(z)     (cabs(z))
        #define __Pyx_c_pow(a, b)  (cpow(a, b))
    #endif
 #endif
#else
    static CYTHON_INLINE int __Pyx_c_eq(__pyx_t_double_complex, __pyx_t_double_complex);
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum(__pyx_t_double_complex, __pyx_t_double_complex);
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff(__pyx_t_double_complex, __pyx_t_double_complex);
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod(__pyx_t_double_complex, __pyx_t_double_complex);
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot(__pyx_t_double_complex, __pyx_t_double_complex);
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg(__pyx_t_double_complex);
    static CYTHON_INLINE int __Pyx_c_is_zero(__pyx_t_double_complex);
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj(__pyx_t_double_complex);
    #if 1
        static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex);
        static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow(__pyx_t_double_complex, __pyx_t_double_complex);
    #endif
#endif

static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject *);

static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject *);

static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject *);

static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject *);

static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject *);

static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject *);

static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject *);

static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject *);

static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject *);

static CYTHON_INLINE int __Pyx_PyInt_AsLongDouble(PyObject *);

static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject *);

static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject *);

static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject *);

static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject *);

static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject *);

static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject *);

static int __Pyx_check_binary_version(void);

#if !defined(__Pyx_PyIdentifier_FromString)
#if PY_MAJOR_VERSION < 3
  #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)
#else
  #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)
#endif
#endif

static PyObject *__Pyx_ImportModule(const char *name); /*proto*/

static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict);  /*proto*/

typedef struct {
    int code_line;
    PyCodeObject* code_object;
} __Pyx_CodeObjectCacheEntry;
struct __Pyx_CodeObjectCache {
    int count;
    int max_count;
    __Pyx_CodeObjectCacheEntry* entries;
};
static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL};
static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line);
static PyCodeObject *__pyx_find_code_object(int code_line);
static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object);

static void __Pyx_AddTraceback(const char *funcname, int c_line,
                               int py_line, const char *filename); /*proto*/

static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/


/* Module declarations from 'cython' */

/* Module declarations from 'cpython.buffer' */

/* Module declarations from 'cpython.ref' */

/* Module declarations from 'libc.string' */

/* Module declarations from 'libc.stdio' */

/* Module declarations from 'cpython.object' */

/* Module declarations from '__builtin__' */

/* Module declarations from 'cpython.type' */
static PyTypeObject *__pyx_ptype_7cpython_4type_type = 0;

/* Module declarations from 'libc.stdlib' */

/* Module declarations from 'numpy' */

/* Module declarations from 'numpy' */
static PyTypeObject *__pyx_ptype_5numpy_dtype = 0;
static PyTypeObject *__pyx_ptype_5numpy_flatiter = 0;
static PyTypeObject *__pyx_ptype_5numpy_broadcast = 0;
static PyTypeObject *__pyx_ptype_5numpy_ndarray = 0;
static PyTypeObject *__pyx_ptype_5numpy_ufunc = 0;
static CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *, char *, char *, int *); /*proto*/

/* Module declarations from 'dipy.tracking.distances' */
static PyArray_Descr *__pyx_v_4dipy_8tracking_9distances_f32_dt = 0;
static __pyx_t_5numpy_float32_t __pyx_v_4dipy_8tracking_9distances_inf;
static CYTHON_INLINE PyArrayObject *__pyx_f_4dipy_8tracking_9distances_as_float_3vec(PyObject *); /*proto*/
static CYTHON_INLINE float *__pyx_f_4dipy_8tracking_9distances_asfp(PyArrayObject *); /*proto*/
static CYTHON_INLINE float __pyx_f_4dipy_8tracking_9distances_cnorm_3vec(float *); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cnormalized_3vec(float *, float *); /*proto*/
static CYTHON_INLINE float __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(float *, float *); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_csub_3vecs(float *, float *, float *); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cadd_3vecs(float *, float *, float *); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cmul_3vecs(float *, float *, float *); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cmul_3vec(float, float *, float *); /*proto*/
static CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_4dipy_8tracking_9distances_czhang(size_t, __pyx_t_5numpy_float32_t *, size_t, __pyx_t_5numpy_float32_t *, __pyx_t_5numpy_float32_t *, int); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_min_distances(size_t, __pyx_t_5numpy_float32_t *, size_t, __pyx_t_5numpy_float32_t *, __pyx_t_5numpy_float32_t *, __pyx_t_5numpy_float32_t *); /*proto*/
static float __pyx_f_4dipy_8tracking_9distances_clee_perpendicular_distance(float *, float *, float *, float *); /*proto*/
static float __pyx_f_4dipy_8tracking_9distances_clee_angle_distance(float *, float *, float *, float *); /*proto*/
static float __pyx_f_4dipy_8tracking_9distances_cintersect_segment_cylinder(float *, float *, float *, float *, float, float *); /*proto*/
static CYTHON_INLINE float __pyx_f_4dipy_8tracking_9distances_cpoint_segment_sq_dist(float *, float *, float *); /*proto*/
static void __pyx_f_4dipy_8tracking_9distances_track_direct_flip_dist(float *, float *, long, float *); /*proto*/
static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3dist(float *, float *, float *, float *, float *, float *, float *); /*proto*/
static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t = { "float32_t", NULL, sizeof(__pyx_t_5numpy_float32_t), { 0 }, 0, 'R', 0, 0 };
static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_uint64_t = { "uint64_t", NULL, sizeof(__pyx_t_5numpy_uint64_t), { 0 }, 0, IS_UNSIGNED(__pyx_t_5numpy_uint64_t) ? 'U' : 'I', IS_UNSIGNED(__pyx_t_5numpy_uint64_t), 0 };
static __Pyx_TypeInfo __Pyx_TypeInfo_object = { "Python object", NULL, sizeof(PyObject *), { 0 }, 0, 'O', 0, 0 };
static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_double_t = { "double_t", NULL, sizeof(__pyx_t_5numpy_double_t), { 0 }, 0, 'R', 0, 0 };
static __Pyx_TypeInfo __Pyx_TypeInfo_float = { "float", NULL, sizeof(float), { 0 }, 0, 'R', 0, 0 };
#define __Pyx_MODULE_NAME "dipy.tracking.distances"
int __pyx_module_is_main_dipy__tracking__distances = 0;

/* Implementation of 'dipy.tracking.distances' */
static PyObject *__pyx_builtin_range;
static PyObject *__pyx_builtin_ValueError;
static PyObject *__pyx_builtin_object;
static PyObject *__pyx_builtin_RuntimeError;
static PyObject *__pyx_pf_4dipy_8tracking_9distances_normalized_3vec(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_2norm_3vec(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_4inner_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_6sub_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_8add_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_10mul_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_12mul_3vec(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_vec); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_14cut_plane(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_ref); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_16most_similar_track_mam(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_metric); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_18bundles_distances_mam(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracksA, PyObject *__pyx_v_tracksB, PyObject *__pyx_v_metric); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_20bundles_distances_mdf(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracksA, PyObject *__pyx_v_tracksB); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_22mam_distances(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz1, PyObject *__pyx_v_xyz2, PyObject *__pyx_v_metric); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_24minimum_closest_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz1, PyObject *__pyx_v_xyz2); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_26lee_perpendicular_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_start0, PyObject *__pyx_v_end0, PyObject *__pyx_v_start1, PyObject *__pyx_v_end1); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_28lee_angle_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_start0, PyObject *__pyx_v_end0, PyObject *__pyx_v_start1, PyObject *__pyx_v_end1); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_30approx_polygon_track(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz, PyObject *__pyx_v_alpha); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_32approximate_mdl_trajectory(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz, PyObject *__pyx_v_alpha); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_34intersect_segment_cylinder(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_sa, PyObject *__pyx_v_sb, PyObject *__pyx_v_p, PyObject *__pyx_v_q, PyObject *__pyx_v_r); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_36point_segment_sq_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_38track_dist_3pts(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracka, PyObject *__pyx_v_trackb); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_40local_skeleton_clustering(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_d_thr); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_42local_skeleton_clustering_3pts(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_d_thr); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_44larch_3split(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_indices, PyObject *__pyx_v_thr); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_46larch_3merge(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_C, PyObject *__pyx_v_thr); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_48point_track_sq_distance_check(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_track, PyArrayObject *__pyx_v_point, double __pyx_v_sq_dist_thr); /* proto */
static PyObject *__pyx_pf_4dipy_8tracking_9distances_50track_roi_intersection_check(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_track, PyArrayObject *__pyx_v_roi, double __pyx_v_sq_dist_thr); /* proto */
static int __pyx_pf_5numpy_7ndarray___getbuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /* proto */
static void __pyx_pf_5numpy_7ndarray_2__releasebuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info); /* proto */
static char __pyx_k_9[] = "Metric should be one of avg, min, max";
static char __pyx_k_12[] = "Wrong argument for metric";
static char __pyx_k_29[] = "ndarray is not C contiguous";
static char __pyx_k_31[] = "ndarray is not Fortran contiguous";
static char __pyx_k_33[] = "Non-native byte order not supported";
static char __pyx_k_35[] = "unknown dtype code in numpy.pxd (%d)";
static char __pyx_k_36[] = "Format string allocated too short, see comment in numpy.pxd";
static char __pyx_k_39[] = "Format string allocated too short.";
static char __pyx_k_41[] = " Optimized track distances, similarities and clustering algorithms using\ntrack distances\n";
static char __pyx_k_44[] = "/home/eleftherios/Devel/dipy/dipy/tracking/distances.pyx";
static char __pyx_k_45[] = "dipy.tracking.distances";
static char __pyx_k_62[] = "most_similar_track_mam";
static char __pyx_k_65[] = "bundles_distances_mam";
static char __pyx_k_68[] = "bundles_distances_mdf";
static char __pyx_k_73[] = "minimum_closest_distance";
static char __pyx_k_76[] = "lee_perpendicular_distance";
static char __pyx_k_79[] = "characteristic_points";
static char __pyx_k_82[] = "approx_polygon_track";
static char __pyx_k_85[] = "approximate_mdl_trajectory";
static char __pyx_k_88[] = "intersect_segment_cylinder";
static char __pyx_k_91[] = "point_segment_sq_distance";
static char __pyx_k_96[] = "local_skeleton_clustering";
static char __pyx_k_99[] = "local_skeleton_clustering_3pts";
static char __pyx_k__B[] = "B";
static char __pyx_k__C[] = "C";
static char __pyx_k__H[] = "H";
static char __pyx_k__I[] = "I";
static char __pyx_k__L[] = "L";
static char __pyx_k__N[] = "N";
static char __pyx_k__O[] = "O";
static char __pyx_k__Q[] = "Q";
static char __pyx_k__a[] = "a";
static char __pyx_k__b[] = "b";
static char __pyx_k__c[] = "c";
static char __pyx_k__d[] = "d";
static char __pyx_k__f[] = "f";
static char __pyx_k__g[] = "g";
static char __pyx_k__h[] = "h";
static char __pyx_k__i[] = "i";
static char __pyx_k__j[] = "j";
static char __pyx_k__k[] = "k";
static char __pyx_k__l[] = "l";
static char __pyx_k__p[] = "p";
static char __pyx_k__q[] = "q";
static char __pyx_k__r[] = "r";
static char __pyx_k__t[] = "t";
static char __pyx_k_106[] = "point_track_sq_distance_check";
static char __pyx_k_109[] = "track_roi_intersection_check";
static char __pyx_k_110[] = "cut_plane (line 191)";
static char __pyx_k_111[] = " Extract divergence vectors and points of intersection\n    between planes normal to the reference fiber and other tracks\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape (N1,3) .. (Nm,3)\n    ref : array, shape (N,3)\n        reference track\n\n    Returns\n    -------\n    hits : sequence\n       list of points and rcds (radial coefficient of divergence)``\n\n    Notes\n    -----\n    The orthogonality relationship\n    np.inner(hits[p][q][0:3]-ref[p+1],ref[p+2]-ref[r][p+1]) will hold throughout\n    for every point q in the hits plane at point (p+1) on the reference track.\n\n    Examples\n    --------\n    >>> refx = np.array([[0,0,0],[1,0,0],[2,0,0],[3,0,0]],dtype='float32')\n    >>> bundlex = [np.array([[0.5,1,0],[1.5,2,0],[2.5,3,0]],dtype='float32')]\n    >>> res = cut_plane(bundlex,refx)\n    >>> len(res)\n    2\n    >>> print(res[0])\n    [[ 1.          1.5         0.          0.70710683  0.        ]]\n    >>> print(res[1])\n    [[ 2.          2.5         0.          0.70710677  0.        ]]\n    ";
static char __pyx_k_112[] = "lee_perpendicular_distance (line 838)";
static char __pyx_k_113[] = " Calculates perpendicular distance metric for the distance between two line segments\n\n    Based on Lee , Han & Whang SIGMOD07.\n\n    This function assumes that norm(end0-start0)>norm(end1-start1) i.e. that the\n    first segment will be bigger than the second one.\n\n    Parameters\n    ----------\n    start0 : float array(3,)\n    end0 : float array(3,)\n    start1 : float array(3,)\n    end1 : float array(3,)\n\n    Returns\n    -------\n    perpendicular_distance: float\n\n    Notes\n    -----\n    l0 = np.inner(end0-start0,end0-start0)\n    l1 = np.inner(end1-start1,end1-start1)\n\n    k0=end0-start0\n\n    u1 = np.inner(start1-start0,k0)/l0\n    u2 = np.inner(end1-start0,k0)/l0\n\n    ps = start0+u1*k0\n    pe = start0+u2*k0\n\n    lperp1 = np.sqrt(np.inner(ps-start1,ps-start1))\n    lperp2 = np.sqrt(np.inner(pe-end1,pe-end1))\n\n    if lperp1+lperp2 > 0.:\n        return (lperp1**2+lperp2**2)/(lperp1+lperp2)\n    else:\n        return 0.\n\n    Examples\n    --------\n    >>> d = lee_perpendicular_distance([0,0,0],[1,0,0],[3,4,5],[5,4,3])\n    >>> print('%.6f' % d)\n    5.787888\n    ";
static char __pyx_k_114[] = "lee_angle_distance (line 941)";
static char __pyx_k_115[] = " Calculates angle distance metric for the distance between two line segments\n\n    Based on Lee , Han & Whang SIGMOD07.\n\n    This function assumes that norm(end0-start0)>norm(end1-start1) i.e. that the\n    first segment will be bigger than the second one.\n\n    Parameters\n    ----------\n    start0 : float array(3,)\n    end0 : float array(3,)\n    start1 : float array(3,)\n    end1 : float array(3,)\n\n    Returns\n    -------\n    angle_distance : float\n\n    Notes\n    -----\n    l_0 = np.inner(end0-start0,end0-start0)\n    l_1 = np.inner(end1-start1,end1-start1)\n\n    cos_theta_squared = np.inner(end0-start0,end1-start1)**2/ (l_0*l_1)\n    return np.sqrt((1-cos_theta_squared)*l_1)\n\n    Examples\n    --------\n    >>> lee_angle_distance([0,0,0],[1,0,0],[3,4,5],[5,4,3])\n    2.0\n    ";
static char __pyx_k_116[] = "approx_polygon_track (line 1007)";
static char __pyx_k_117[] = " Fast and simple trajectory approximation algorithm by Eleftherios and Ian\n\n    It will reduce the number of points of the track by keeping\n    intact the start and endpoints of the track and trying to remove\n    as many points as possible without distorting much the shape of\n    the track\n\n    Parameters\n    ----------\n    xyz : array(N,3)\n        initial trajectory\n    alpha : float\n        smoothing parameter (<0.392 smoother, >0.392 rougher) if the\n        trajectory was a smooth circle then with alpha =0.393\n        ~=pi/8. the circle would be approximated with an decahexagon if\n        alpha = 0.7853 ~=pi/4. with an octagon.\n\n    Returns\n    -------\n    characteristic_points: list of M array(3,) points\n\n    Examples\n    --------\n\n    Approximating a helix:\n\n    >>> t=np.linspace(0,1.75*2*np.pi,100)\n    >>> x = np.sin(t)\n    >>> y = np.cos(t)\n    >>> z = t\n    >>> xyz=np.vstack((x,y,z)).T\n    >>> xyza = approx_polygon_track(xyz)\n    >>> len(xyza) < len(xyz)\n    True\n\n    Notes\n    -----\n    Assuming that a good approximation for a circle is an octagon then\n    that means that the points of the octagon will have angle alpha =\n    2*pi/8 = pi/4 . We calculate the angle between every two neighbour\n    segments of a trajectory and if the angle is higher than pi/4 we\n    choose that point as a characteristic point otherwise we move at the\n    next point.\n    ";
static char __pyx_k_118[] = "intersect_segment_cylinder (line 1158)";
static char __pyx_k_119[] = " Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r\n\n    See p.197 from Real Time Collision Detection by C. Ericson\n\n    Examples\n    --------\n    Define cylinder using a segment defined by\n\n    >>> p=np.array([0,0,0],dtype=np.float32)\n    >>> q=np.array([1,0,0],dtype=np.float32)\n    >>> r=0.5\n\n    Define segment\n\n    >>> sa=np.array([0.5,1 ,0],dtype=np.float32)\n    >>> sb=np.array([0.5,-1,0],dtype=np.float32)\n\n    Intersection\n\n    >>> intersect_segment_cylinder(sa, sb, p, q, r)\n    (1.0, 0.25, 0.75)\n    ";
static char __pyx_k_120[] = "point_segment_sq_distance (line 1271)";
static char __pyx_k_121[] = " Calculate the squared distance from a point c to a finite line segment ab.\n\n    Examples\n    --------\n    >>> a=np.array([0,0,0], dtype=np.float32)\n    >>> b=np.array([1,0,0], dtype=np.float32)\n    >>> c=np.array([0,1,0], dtype=np.float32)\n    >>> point_segment_sq_distance(a, b, c)\n    1.0\n    >>> c = np.array([0,3,0], dtype=np.float32)\n    >>> point_segment_sq_distance(a,b,c)\n    9.0\n    >>> c = np.array([-1,1,0], dtype=np.float32)\n    >>> point_segment_sq_distance(a, b, c)\n    2.0\n    ";
static char __pyx_k_122[] = "track_dist_3pts (line 1324)";
static char __pyx_k_123[] = " Calculate the euclidean distance between two 3pt tracks\n\n    Both direct and flip distances are calculated but only the smallest is returned\n\n    Parameters\n    ----------\n    a : array, shape (3,3)\n    a three point track\n    b : array, shape (3,3)\n    a three point track\n\n    Returns\n    -------\n    dist :float\n\n    Examples\n    --------\n    >>> a = np.array([[0,0,0],[1,0,0,],[2,0,0]])\n    >>> b = np.array([[3,0,0],[3.5,1,0],[4,2,0]])\n    >>> c = track_dist_3pts(a, b)\n    >>> print('%.6f' % c)\n    2.721573\n    ";
static char __pyx_k_124[] = "local_skeleton_clustering (line 1471)";
static char __pyx_k_125[] = "Efficient tractography clustering\n\n    Every track can needs to have the same number of points.\n    Use `dipy.tracking.metrics.downsample` to restrict the number of points\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape (N,3) .. (N,3) where N=points\n    d_thr : float\n        average euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n        Clusters.\n\n    Examples\n    --------\n    >>> tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]]),\n    ...         np.array([[3,0,0],[3.5,1,0],[4,2,0]]),\n    ...         np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]]),\n    ...         np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]]),\n    ...         np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]]),\n    ...         np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]]),\n    ...         np.array([[0,0,0],[0,1,0],[0,2,0]])]\n    >>> C = local_skeleton_clustering(tracks, d_thr=0.5)\n\n    Notes\n    -----\n    The distance calculated between two tracks::\n\n        t_1       t_2\n\n        0*   a    *0\n          \\       |\n           \\      |\n           1*     |\n            |  b  *1\n            |      \\\n            2*      \\\n                c    *2\n\n    is equal to $(a+b+c)/3$ where $a$ the euclidean distance between ``t_1[0]``\n    and ``t_2[0]``, $b$ between ``t_1[1]`` and ``t_2[1]`` and $c$ between\n    ``t_1[2]`` and ``t_2[2]``. Also the same with t2 flipped (so ``t_1[0]``\n    compared to ``t_2[2]`` etc).\n\n    Visualization:\n\n    It is possible to visualize the clustering C from the example\n    above using the fvtk module::\n\n        from dipy.viz import fvtk\n        r=fvtk.ren()\n        for c in C:\n            color=np.random.rand(3)\n            for i in C[c]['indices']:\n                fvtk.add(r,fvtk.line(tracks[i],color))\n        fvtk.show(r)\n\n    See Also\n    --------\n    dipy.tracking.metrics.downsample\n    ";
static char __pyx_k_126[] = "local_skeleton_clustering_3pts (line 1656)";
static char __pyx_k_127[] = " Does a first pass clustering\n\n    Every track can only have 3 pts neither less or more.\n    Use `dipy.tracking.metrics.downsample` to restrict the number of points\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape (N,3) .. (N,3) where N=3\n    d_thr : float\n        Average euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n       Clusters.\n\n    Examples\n    --------\n    >>> tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]]),\n    ...         np.array([[3,0,0],[3.5,1,0],[4,2,0]]),\n    ...         np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]]),\n    ...         np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]]),\n    ...         np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]]),\n    ...         np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]]),\n    ...         np.array([[0,0,0],[0,1,0],[0,2,0]])]\n    >>> C=local_skeleton_clustering_3pts(tracks,d_thr=0.5)\n\n    Notes\n    -----\n    It is possible to visualize the clustering C from the example\n    above using the fvtk module::\n\n        r=fvtk.ren()\n        for c in C:\n            color=np.random.rand(3)\n            for i in C[c]['indices']:\n                fvtk.add(r,fos.line(tracks[i],color))\n        fvtk.show(r)\n\n    ";
static char __pyx_k_128[] = "larch_3split (line 1786)";
static char __pyx_k_129[] = "Generate a first pass clustering using 3 points on the tracks only.\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape ``(N1,3) .. (Nm,3)``, where 3 points are\n        (first, mid and last)\n    indices : None or sequence, optional\n        Sequence of integer indices of tracks\n    trh : float, optional\n        squared euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n        A tree graph containing the clusters.\n\n    Notes\n    -----\n    If a 3 point track (3track) is far away from all clusters then add a new\n    cluster and assign this 3track as the rep(representative) track for the new\n    cluster. Otherwise the rep 3track of each cluster is the average track of\n    the cluster.\n\n    Examples\n    --------\n    >>> tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]],dtype=np.float32),\n    ...         np.array([[3,0,0],[3.5,1,0],[4,2,0]],dtype=np.float32),\n    ...         np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]],dtype=np.float32),\n    ...         np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]],dtype=np.float32),\n    ...         np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]],dtype=np.float32),\n    ...         np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]],dtype=np.float32),\n    ...         np.array([[0,0,0],[0,1,0],[0,2,0]],dtype=np.float32),\n    ...         np.array([[0.2,0,0],[0.2,1,0],[0.2,2,0]],dtype=np.float32),\n    ...         np.array([[-0.2,0,0],[-0.2,1,0],[-0.2,2,0]],dtype=np.float32)]\n    >>> C = larch_3split(tracks, None, 0.5)\n\n    Here is an example of how to visualize the clustering above::\n\n        from dipy.viz import fvtk\n        r=fvtk.ren()\n        fvtk.add(r,fvtk.line(tracks,fvtk.red))\n        fvtk.show(r)\n        for c in C:\n            color=np.random.rand(3)\n            for i in C[c]['indices']:\n                fos.add(r,fvtk.line(tracks[i],color))\n        fvtk.show(r)\n        for c in C:\n            fvtk.add(r,fos.line(C[c]['rep3']/C[c]['N'],fos.white))\n        fvtk.show(""r)\n    ";
static char __pyx_k_130[] = "point_track_sq_distance_check (line 1953)";
static char __pyx_k_131[] = " Check if square distance of track from point is smaller than threshold\n\n    Parameters\n    ----------\n    track: array,float32, shape (N,3)\n    point: array,float32, shape (3,)\n    sq_dist_thr: double, threshold\n\n    Returns\n    -------\n    bool: True, if sq_distance <= sq_dist_thr, otherwise False.\n\n    Examples\n    --------\n    >>> t=np.random.rand(10,3).astype(np.float32)\n    >>> p=np.array([0.5,0.5,0.5],dtype=np.float32)\n    >>> point_track_sq_distance_check(t,p,2**2)\n    True\n    >>> t=np.array([[0,0,0],[1,1,1],[2,2,2]],dtype='f4')\n    >>> p=np.array([-1,-1.,-1],dtype='f4')\n    >>> point_track_sq_distance_check(t,p,.2**2)\n    False\n    >>> point_track_sq_distance_check(t,p,2**2)\n    True\n    ";
static char __pyx_k_132[] = "track_roi_intersection_check (line 2011)";
static char __pyx_k_133[] = " Check if a track is intersecting a region of interest\n\n    Parameters\n    ----------\n    track: array,float32, shape (N,3)\n    roi: array,float32, shape (M,3)\n    sq_dist_thr: double, threshold, check squared euclidean distance from every roi point\n\n    Returns\n    -------\n    bool: True, if sq_distance <= sq_dist_thr, otherwise False.\n\n    Examples\n    --------\n    >>> roi=np.array([[0,0,0],[1,0,0],[2,0,0]],dtype='f4')\n    >>> t=np.array([[0,0,0],[1,1,1],[2,2,2]],dtype='f4')\n    >>> track_roi_intersection_check(t,roi,1)\n    True\n    >>> track_roi_intersection_check(t,np.array([[10,0,0]],dtype='f4'),1)\n    False\n    ";
static char __pyx_k__C2[] = "C2";
static char __pyx_k__DM[] = "DM";
static char __pyx_k__Zd[] = "Zd";
static char __pyx_k__Zf[] = "Zf";
static char __pyx_k__Zg[] = "Zg";
static char __pyx_k__ca[] = "ca";
static char __pyx_k__cb[] = "cb";
static char __pyx_k__cc[] = "cc";
static char __pyx_k__ch[] = "ch";
static char __pyx_k__cp[] = "cp";
static char __pyx_k__cq[] = "cq";
static char __pyx_k__cr[] = "cr";
static char __pyx_k__ct[] = "ct";
static char __pyx_k__it[] = "it";
static char __pyx_k__ld[] = "ld";
static char __pyx_k__mn[] = "mn";
static char __pyx_k__np[] = "np";
static char __pyx_k__sa[] = "sa";
static char __pyx_k__sb[] = "sb";
static char __pyx_k__si[] = "si";
static char __pyx_k__t1[] = "t1";
static char __pyx_k__t2[] = "t2";
static char __pyx_k__ts[] = "ts";
static char __pyx_k__Hit[] = "Hit";
static char __pyx_k__all[] = "all";
static char __pyx_k__avg[] = "avg";
static char __pyx_k__cit[] = "cit";
static char __pyx_k__csa[] = "csa";
static char __pyx_k__csb[] = "csb";
static char __pyx_k__dim[] = "dim";
static char __pyx_k__hid[] = "hid";
static char __pyx_k__hit[] = "hit";
static char __pyx_k__i_k[] = "i_k";
static char __pyx_k__inf[] = "inf";
static char __pyx_k__lhp[] = "lhp";
static char __pyx_k__lrq[] = "lrq";
static char __pyx_k__m_d[] = "m_d";
static char __pyx_k__m_k[] = "m_k";
static char __pyx_k__max[] = "max";
static char __pyx_k__min[] = "min";
static char __pyx_k__pMq[] = "pMq";
static char __pyx_k__ptr[] = "ptr";
static char __pyx_k__qMp[] = "qMp";
static char __pyx_k__rMp[] = "rMp";
static char __pyx_k__rMq[] = "rMq";
static char __pyx_k__rcd[] = "rcd";
static char __pyx_k__ref[] = "ref";
static char __pyx_k__roi[] = "roi";
static char __pyx_k__thr[] = "thr";
static char __pyx_k__tmp[] = "tmp";
static char __pyx_k__vec[] = "vec";
static char __pyx_k__xyz[] = "xyz";
static char __pyx_k__alld[] = "alld";
static char __pyx_k__beta[] = "beta";
static char __pyx_k__copy[] = "copy";
static char __pyx_k__curr[] = "curr";
static char __pyx_k__dist[] = "dist";
static char __pyx_k__end0[] = "end0";
static char __pyx_k__end1[] = "end1";
static char __pyx_k__flip[] = "flip";
static char __pyx_k__fvec[] = "fvec";
static char __pyx_k__hits[] = "hits";
static char __pyx_k__keys[] = "keys";
static char __pyx_k__klen[] = "klen";
static char __pyx_k__lenC[] = "lenC";
static char __pyx_k__lent[] = "lent";
static char __pyx_k__p_no[] = "p_no";
static char __pyx_k__q_no[] = "q_no";
static char __pyx_k__rep3[] = "rep3";
static char __pyx_k__rlen[] = "rlen";
static char __pyx_k__rows[] = "rows";
static char __pyx_k__t_no[] = "t_no";
static char __pyx_k__time[] = "time";
static char __pyx_k__tlen[] = "tlen";
static char __pyx_k__vec0[] = "vec0";
static char __pyx_k__vec1[] = "vec1";
static char __pyx_k__vec2[] = "vec2";
static char __pyx_k__xyz1[] = "xyz1";
static char __pyx_k__xyz2[] = "xyz2";
static char __pyx_k__N_ref[] = "N_ref";
static char __pyx_k__along[] = "along";
static char __pyx_k__alpha[] = "alpha";
static char __pyx_k__angle[] = "angle";
static char __pyx_k__array[] = "array";
static char __pyx_k__currp[] = "currp";
static char __pyx_k__d_thr[] = "d_thr";
static char __pyx_k__delta[] = "delta";
static char __pyx_k__dtype[] = "dtype";
static char __pyx_k__empty[] = "empty";
static char __pyx_k__fvec0[] = "fvec0";
static char __pyx_k__fvec1[] = "fvec1";
static char __pyx_k__fvec2[] = "fvec2";
static char __pyx_k__fvec3[] = "fvec3";
static char __pyx_k__fvec4[] = "fvec4";
static char __pyx_k__hitMp[] = "hitMp";
static char __pyx_k__int64[] = "int64";
static char __pyx_k__lentA[] = "lentA";
static char __pyx_k__lentB[] = "lentB";
static char __pyx_k__numpy[] = "numpy";
static char __pyx_k__point[] = "point";
static char __pyx_k__range[] = "range";
static char __pyx_k__ref32[] = "ref32";
static char __pyx_k__shape[] = "shape";
static char __pyx_k__t1_pi[] = "t1_pi";
static char __pyx_k__t2_pi[] = "t2_pi";
static char __pyx_k__t_len[] = "t_len";
static char __pyx_k__track[] = "track";
static char __pyx_k__zeros[] = "zeros";
static char __pyx_k__alphac[] = "alphac";
static char __pyx_k__append[] = "append";
static char __pyx_k__argmin[] = "argmin";
static char __pyx_k__cd_thr[] = "cd_thr";
static char __pyx_k__double[] = "double";
static char __pyx_k__hidden[] = "hidden";
static char __pyx_k__hit_no[] = "hit_no";
static char __pyx_k__krange[] = "krange";
static char __pyx_k__length[] = "length";
static char __pyx_k__metric[] = "metric";
static char __pyx_k__n_hits[] = "n_hits";
static char __pyx_k__normal[] = "normal";
static char __pyx_k__object[] = "object";
static char __pyx_k__points[] = "points";
static char __pyx_k__start0[] = "start0";
static char __pyx_k__start1[] = "start1";
static char __pyx_k__t1_len[] = "t1_len";
static char __pyx_k__t1_ptr[] = "t1_ptr";
static char __pyx_k__t2_len[] = "t2_len";
static char __pyx_k__t2_ptr[] = "t2_ptr";
static char __pyx_k__track1[] = "track1";
static char __pyx_k__track2[] = "track2";
static char __pyx_k__tracka[] = "tracka";
static char __pyx_k__trackb[] = "trackb";
static char __pyx_k__tracks[] = "tracks";
static char __pyx_k__uint64[] = "uint64";
static char __pyx_k__vec_in[] = "vec_in";
static char __pyx_k__N_track[] = "N_track";
static char __pyx_k__asarray[] = "asarray";
static char __pyx_k__cluster[] = "cluster";
static char __pyx_k__float32[] = "float32";
static char __pyx_k__hit_arr[] = "hit_arr";
static char __pyx_k__hit_ptr[] = "hit_ptr";
static char __pyx_k__indices[] = "indices";
static char __pyx_k__itrange[] = "itrange";
static char __pyx_k__one_hit[] = "one_hit";
static char __pyx_k__squeeze[] = "squeeze";
static char __pyx_k__tracksA[] = "tracksA";
static char __pyx_k__tracksB[] = "tracksB";
static char __pyx_k__vec_out[] = "vec_out";
static char __pyx_k__N_tracks[] = "N_tracks";
static char __pyx_k____main__[] = "__main__";
static char __pyx_k____test__[] = "__test__";
static char __pyx_k__cost_par[] = "cost_par";
static char __pyx_k__distance[] = "distance";
static char __pyx_k__min_t1t2[] = "min_t1t2";
static char __pyx_k__min_t2t1[] = "min_t2t1";
static char __pyx_k__mul_3vec[] = "mul_3vec";
static char __pyx_k__tracks32[] = "tracks32";
static char __pyx_k__add_3vecs[] = "add_3vecs";
static char __pyx_k__cut_plane[] = "cut_plane";
static char __pyx_k__mean_t1t2[] = "mean_t1t2";
static char __pyx_k__mean_t2t1[] = "mean_t2t1";
static char __pyx_k__mid_index[] = "mid_index";
static char __pyx_k__mul_3vecs[] = "mul_3vecs";
static char __pyx_k__norm_3vec[] = "norm_3vec";
static char __pyx_k__sub_3vecs[] = "sub_3vecs";
static char __pyx_k__track_len[] = "track_len";
static char __pyx_k__tracksA32[] = "tracksA32";
static char __pyx_k__tracksB32[] = "tracksB32";
static char __pyx_k__ValueError[] = "ValueError";
static char __pyx_k____import__[] = "__import__";
static char __pyx_k__cost_nopar[] = "cost_nopar";
static char __pyx_k__intersects[] = "intersects";
static char __pyx_k__min_buffer[] = "min_buffer";
static char __pyx_k__next_ref_p[] = "next_ref_p";
static char __pyx_k__next_trk_p[] = "next_trk_p";
static char __pyx_k__this_ref_p[] = "this_ref_p";
static char __pyx_k__this_trk_p[] = "this_trk_p";
static char __pyx_k__inner_3vecs[] = "inner_3vecs";
static char __pyx_k__max_hit_len[] = "max_hit_len";
static char __pyx_k__metric_type[] = "metric_type";
static char __pyx_k__sq_dist_thr[] = "sq_dist_thr";
static char __pyx_k__start_index[] = "start_index";
static char __pyx_k__RuntimeError[] = "RuntimeError";
static char __pyx_k__larch_3merge[] = "larch_3merge";
static char __pyx_k__larch_3split[] = "larch_3split";
static char __pyx_k__min_min_t1t2[] = "min_min_t1t2";
static char __pyx_k__min_min_t2t1[] = "min_min_t2t1";
static char __pyx_k__track2others[] = "track2others";
static char __pyx_k__current_index[] = "current_index";
static char __pyx_k__mam_distances[] = "mam_distances";
static char __pyx_k__track_lengths[] = "track_lengths";
static char __pyx_k____pyx_getbuffer[] = "__pyx_getbuffer";
static char __pyx_k__normalized_3vec[] = "normalized_3vec";
static char __pyx_k__track_dist_3pts[] = "track_dist_3pts";
static char __pyx_k__distances_buffer[] = "distances_buffer";
static char __pyx_k__sum_track2others[] = "sum_track2others";
static char __pyx_k__ascontiguousarray[] = "ascontiguousarray";
static char __pyx_k__longest_track_len[] = "longest_track_len";
static char __pyx_k__lee_angle_distance[] = "lee_angle_distance";
static char __pyx_k__longest_track_lenA[] = "longest_track_lenA";
static char __pyx_k__longest_track_lenB[] = "longest_track_lenB";
static char __pyx_k____pyx_releasebuffer[] = "__pyx_releasebuffer";
static PyObject *__pyx_n_s_106;
static PyObject *__pyx_n_s_109;
static PyObject *__pyx_kp_u_110;
static PyObject *__pyx_kp_u_111;
static PyObject *__pyx_kp_u_112;
static PyObject *__pyx_kp_u_113;
static PyObject *__pyx_kp_u_114;
static PyObject *__pyx_kp_u_115;
static PyObject *__pyx_kp_u_116;
static PyObject *__pyx_kp_u_117;
static PyObject *__pyx_kp_u_118;
static PyObject *__pyx_kp_u_119;
static PyObject *__pyx_kp_s_12;
static PyObject *__pyx_kp_u_120;
static PyObject *__pyx_kp_u_121;
static PyObject *__pyx_kp_u_122;
static PyObject *__pyx_kp_u_123;
static PyObject *__pyx_kp_u_124;
static PyObject *__pyx_kp_u_125;
static PyObject *__pyx_kp_u_126;
static PyObject *__pyx_kp_u_127;
static PyObject *__pyx_kp_u_128;
static PyObject *__pyx_kp_u_129;
static PyObject *__pyx_kp_u_130;
static PyObject *__pyx_kp_u_131;
static PyObject *__pyx_kp_u_132;
static PyObject *__pyx_kp_u_133;
static PyObject *__pyx_kp_u_29;
static PyObject *__pyx_kp_u_31;
static PyObject *__pyx_kp_u_33;
static PyObject *__pyx_kp_u_35;
static PyObject *__pyx_kp_u_36;
static PyObject *__pyx_kp_u_39;
static PyObject *__pyx_kp_s_44;
static PyObject *__pyx_n_s_45;
static PyObject *__pyx_n_s_62;
static PyObject *__pyx_n_s_65;
static PyObject *__pyx_n_s_68;
static PyObject *__pyx_n_s_73;
static PyObject *__pyx_n_s_76;
static PyObject *__pyx_n_s_79;
static PyObject *__pyx_n_s_82;
static PyObject *__pyx_n_s_85;
static PyObject *__pyx_n_s_88;
static PyObject *__pyx_kp_s_9;
static PyObject *__pyx_n_s_91;
static PyObject *__pyx_n_s_96;
static PyObject *__pyx_n_s_99;
static PyObject *__pyx_n_s__C;
static PyObject *__pyx_n_s__C2;
static PyObject *__pyx_n_s__DM;
static PyObject *__pyx_n_s__Hit;
static PyObject *__pyx_n_s__N;
static PyObject *__pyx_n_s__N_ref;
static PyObject *__pyx_n_s__N_track;
static PyObject *__pyx_n_s__N_tracks;
static PyObject *__pyx_n_s__RuntimeError;
static PyObject *__pyx_n_s__ValueError;
static PyObject *__pyx_n_s____import__;
static PyObject *__pyx_n_s____main__;
static PyObject *__pyx_n_s____pyx_getbuffer;
static PyObject *__pyx_n_s____pyx_releasebuffer;
static PyObject *__pyx_n_s____test__;
static PyObject *__pyx_n_s__a;
static PyObject *__pyx_n_s__add_3vecs;
static PyObject *__pyx_n_s__all;
static PyObject *__pyx_n_s__alld;
static PyObject *__pyx_n_s__along;
static PyObject *__pyx_n_s__alpha;
static PyObject *__pyx_n_s__alphac;
static PyObject *__pyx_n_s__angle;
static PyObject *__pyx_n_s__append;
static PyObject *__pyx_n_s__argmin;
static PyObject *__pyx_n_s__array;
static PyObject *__pyx_n_s__asarray;
static PyObject *__pyx_n_s__ascontiguousarray;
static PyObject *__pyx_n_s__avg;
static PyObject *__pyx_n_s__b;
static PyObject *__pyx_n_s__beta;
static PyObject *__pyx_n_s__c;
static PyObject *__pyx_n_s__ca;
static PyObject *__pyx_n_s__cb;
static PyObject *__pyx_n_s__cc;
static PyObject *__pyx_n_s__cd_thr;
static PyObject *__pyx_n_s__ch;
static PyObject *__pyx_n_s__cit;
static PyObject *__pyx_n_s__cluster;
static PyObject *__pyx_n_s__copy;
static PyObject *__pyx_n_s__cost_nopar;
static PyObject *__pyx_n_s__cost_par;
static PyObject *__pyx_n_s__cp;
static PyObject *__pyx_n_s__cq;
static PyObject *__pyx_n_s__cr;
static PyObject *__pyx_n_s__csa;
static PyObject *__pyx_n_s__csb;
static PyObject *__pyx_n_s__ct;
static PyObject *__pyx_n_s__curr;
static PyObject *__pyx_n_s__current_index;
static PyObject *__pyx_n_s__currp;
static PyObject *__pyx_n_s__cut_plane;
static PyObject *__pyx_n_s__d;
static PyObject *__pyx_n_s__d_thr;
static PyObject *__pyx_n_s__delta;
static PyObject *__pyx_n_s__dim;
static PyObject *__pyx_n_s__dist;
static PyObject *__pyx_n_s__distance;
static PyObject *__pyx_n_s__distances_buffer;
static PyObject *__pyx_n_s__double;
static PyObject *__pyx_n_s__dtype;
static PyObject *__pyx_n_s__empty;
static PyObject *__pyx_n_s__end0;
static PyObject *__pyx_n_s__end1;
static PyObject *__pyx_n_s__flip;
static PyObject *__pyx_n_s__float32;
static PyObject *__pyx_n_s__fvec;
static PyObject *__pyx_n_s__fvec0;
static PyObject *__pyx_n_s__fvec1;
static PyObject *__pyx_n_s__fvec2;
static PyObject *__pyx_n_s__fvec3;
static PyObject *__pyx_n_s__fvec4;
static PyObject *__pyx_n_s__h;
static PyObject *__pyx_n_s__hid;
static PyObject *__pyx_n_s__hidden;
static PyObject *__pyx_n_s__hit;
static PyObject *__pyx_n_s__hitMp;
static PyObject *__pyx_n_s__hit_arr;
static PyObject *__pyx_n_s__hit_no;
static PyObject *__pyx_n_s__hit_ptr;
static PyObject *__pyx_n_s__hits;
static PyObject *__pyx_n_s__i;
static PyObject *__pyx_n_s__i_k;
static PyObject *__pyx_n_s__indices;
static PyObject *__pyx_n_s__inf;
static PyObject *__pyx_n_s__inner_3vecs;
static PyObject *__pyx_n_s__int64;
static PyObject *__pyx_n_s__intersects;
static PyObject *__pyx_n_s__it;
static PyObject *__pyx_n_s__itrange;
static PyObject *__pyx_n_s__j;
static PyObject *__pyx_n_s__k;
static PyObject *__pyx_n_s__keys;
static PyObject *__pyx_n_s__klen;
static PyObject *__pyx_n_s__krange;
static PyObject *__pyx_n_s__larch_3merge;
static PyObject *__pyx_n_s__larch_3split;
static PyObject *__pyx_n_s__ld;
static PyObject *__pyx_n_s__lee_angle_distance;
static PyObject *__pyx_n_s__lenC;
static PyObject *__pyx_n_s__length;
static PyObject *__pyx_n_s__lent;
static PyObject *__pyx_n_s__lentA;
static PyObject *__pyx_n_s__lentB;
static PyObject *__pyx_n_s__lhp;
static PyObject *__pyx_n_s__longest_track_len;
static PyObject *__pyx_n_s__longest_track_lenA;
static PyObject *__pyx_n_s__longest_track_lenB;
static PyObject *__pyx_n_s__lrq;
static PyObject *__pyx_n_s__m_d;
static PyObject *__pyx_n_s__m_k;
static PyObject *__pyx_n_s__mam_distances;
static PyObject *__pyx_n_s__max;
static PyObject *__pyx_n_s__max_hit_len;
static PyObject *__pyx_n_s__mean_t1t2;
static PyObject *__pyx_n_s__mean_t2t1;
static PyObject *__pyx_n_s__metric;
static PyObject *__pyx_n_s__metric_type;
static PyObject *__pyx_n_s__mid_index;
static PyObject *__pyx_n_s__min;
static PyObject *__pyx_n_s__min_buffer;
static PyObject *__pyx_n_s__min_min_t1t2;
static PyObject *__pyx_n_s__min_min_t2t1;
static PyObject *__pyx_n_s__min_t1t2;
static PyObject *__pyx_n_s__min_t2t1;
static PyObject *__pyx_n_s__mn;
static PyObject *__pyx_n_s__mul_3vec;
static PyObject *__pyx_n_s__mul_3vecs;
static PyObject *__pyx_n_s__n_hits;
static PyObject *__pyx_n_s__next_ref_p;
static PyObject *__pyx_n_s__next_trk_p;
static PyObject *__pyx_n_s__norm_3vec;
static PyObject *__pyx_n_s__normal;
static PyObject *__pyx_n_s__normalized_3vec;
static PyObject *__pyx_n_s__np;
static PyObject *__pyx_n_s__numpy;
static PyObject *__pyx_n_s__object;
static PyObject *__pyx_n_s__one_hit;
static PyObject *__pyx_n_s__p;
static PyObject *__pyx_n_s__pMq;
static PyObject *__pyx_n_s__p_no;
static PyObject *__pyx_n_s__point;
static PyObject *__pyx_n_s__points;
static PyObject *__pyx_n_s__ptr;
static PyObject *__pyx_n_s__q;
static PyObject *__pyx_n_s__qMp;
static PyObject *__pyx_n_s__q_no;
static PyObject *__pyx_n_s__r;
static PyObject *__pyx_n_s__rMp;
static PyObject *__pyx_n_s__rMq;
static PyObject *__pyx_n_s__range;
static PyObject *__pyx_n_s__rcd;
static PyObject *__pyx_n_s__ref;
static PyObject *__pyx_n_s__ref32;
static PyObject *__pyx_n_s__rep3;
static PyObject *__pyx_n_s__rlen;
static PyObject *__pyx_n_s__roi;
static PyObject *__pyx_n_s__rows;
static PyObject *__pyx_n_s__sa;
static PyObject *__pyx_n_s__sb;
static PyObject *__pyx_n_s__shape;
static PyObject *__pyx_n_s__si;
static PyObject *__pyx_n_s__sq_dist_thr;
static PyObject *__pyx_n_s__squeeze;
static PyObject *__pyx_n_s__start0;
static PyObject *__pyx_n_s__start1;
static PyObject *__pyx_n_s__start_index;
static PyObject *__pyx_n_s__sub_3vecs;
static PyObject *__pyx_n_s__sum_track2others;
static PyObject *__pyx_n_s__t;
static PyObject *__pyx_n_s__t1;
static PyObject *__pyx_n_s__t1_len;
static PyObject *__pyx_n_s__t1_pi;
static PyObject *__pyx_n_s__t1_ptr;
static PyObject *__pyx_n_s__t2;
static PyObject *__pyx_n_s__t2_len;
static PyObject *__pyx_n_s__t2_pi;
static PyObject *__pyx_n_s__t2_ptr;
static PyObject *__pyx_n_s__t_len;
static PyObject *__pyx_n_s__t_no;
static PyObject *__pyx_n_s__this_ref_p;
static PyObject *__pyx_n_s__this_trk_p;
static PyObject *__pyx_n_s__thr;
static PyObject *__pyx_n_s__time;
static PyObject *__pyx_n_s__tlen;
static PyObject *__pyx_n_s__tmp;
static PyObject *__pyx_n_s__track;
static PyObject *__pyx_n_s__track1;
static PyObject *__pyx_n_s__track2;
static PyObject *__pyx_n_s__track2others;
static PyObject *__pyx_n_s__track_dist_3pts;
static PyObject *__pyx_n_s__track_len;
static PyObject *__pyx_n_s__track_lengths;
static PyObject *__pyx_n_s__tracka;
static PyObject *__pyx_n_s__trackb;
static PyObject *__pyx_n_s__tracks;
static PyObject *__pyx_n_s__tracks32;
static PyObject *__pyx_n_s__tracksA;
static PyObject *__pyx_n_s__tracksA32;
static PyObject *__pyx_n_s__tracksB;
static PyObject *__pyx_n_s__tracksB32;
static PyObject *__pyx_n_s__ts;
static PyObject *__pyx_n_s__uint64;
static PyObject *__pyx_n_s__vec;
static PyObject *__pyx_n_s__vec0;
static PyObject *__pyx_n_s__vec1;
static PyObject *__pyx_n_s__vec2;
static PyObject *__pyx_n_s__vec_in;
static PyObject *__pyx_n_s__vec_out;
static PyObject *__pyx_n_s__xyz;
static PyObject *__pyx_n_s__xyz1;
static PyObject *__pyx_n_s__xyz2;
static PyObject *__pyx_n_s__zeros;
static PyObject *__pyx_int_0;
static PyObject *__pyx_int_1;
static PyObject *__pyx_int_3;
static PyObject *__pyx_int_5;
static PyObject *__pyx_int_10;
static PyObject *__pyx_int_15;
static PyObject *__pyx_k_14;
static PyObject *__pyx_k_15;
static PyObject *__pyx_k_18;
static PyObject *__pyx_k_22;
static PyObject *__pyx_k_slice_8;
static PyObject *__pyx_k_tuple_1;
static PyObject *__pyx_k_tuple_2;
static PyObject *__pyx_k_tuple_3;
static PyObject *__pyx_k_tuple_4;
static PyObject *__pyx_k_tuple_5;
static PyObject *__pyx_k_tuple_6;
static PyObject *__pyx_k_tuple_7;
static PyObject *__pyx_k_slice_19;
static PyObject *__pyx_k_tuple_10;
static PyObject *__pyx_k_tuple_11;
static PyObject *__pyx_k_tuple_13;
static PyObject *__pyx_k_tuple_16;
static PyObject *__pyx_k_tuple_17;
static PyObject *__pyx_k_tuple_20;
static PyObject *__pyx_k_tuple_21;
static PyObject *__pyx_k_tuple_23;
static PyObject *__pyx_k_tuple_24;
static PyObject *__pyx_k_tuple_25;
static PyObject *__pyx_k_tuple_26;
static PyObject *__pyx_k_tuple_27;
static PyObject *__pyx_k_tuple_28;
static PyObject *__pyx_k_tuple_30;
static PyObject *__pyx_k_tuple_32;
static PyObject *__pyx_k_tuple_34;
static PyObject *__pyx_k_tuple_37;
static PyObject *__pyx_k_tuple_38;
static PyObject *__pyx_k_tuple_40;
static PyObject *__pyx_k_tuple_42;
static PyObject *__pyx_k_tuple_46;
static PyObject *__pyx_k_tuple_48;
static PyObject *__pyx_k_tuple_50;
static PyObject *__pyx_k_tuple_52;
static PyObject *__pyx_k_tuple_54;
static PyObject *__pyx_k_tuple_56;
static PyObject *__pyx_k_tuple_58;
static PyObject *__pyx_k_tuple_60;
static PyObject *__pyx_k_tuple_63;
static PyObject *__pyx_k_tuple_66;
static PyObject *__pyx_k_tuple_69;
static PyObject *__pyx_k_tuple_71;
static PyObject *__pyx_k_tuple_74;
static PyObject *__pyx_k_tuple_77;
static PyObject *__pyx_k_tuple_80;
static PyObject *__pyx_k_tuple_83;
static PyObject *__pyx_k_tuple_86;
static PyObject *__pyx_k_tuple_89;
static PyObject *__pyx_k_tuple_92;
static PyObject *__pyx_k_tuple_94;
static PyObject *__pyx_k_tuple_97;
static PyObject *__pyx_k_tuple_100;
static PyObject *__pyx_k_tuple_102;
static PyObject *__pyx_k_tuple_104;
static PyObject *__pyx_k_tuple_107;
static PyObject *__pyx_k_codeobj_43;
static PyObject *__pyx_k_codeobj_47;
static PyObject *__pyx_k_codeobj_49;
static PyObject *__pyx_k_codeobj_51;
static PyObject *__pyx_k_codeobj_53;
static PyObject *__pyx_k_codeobj_55;
static PyObject *__pyx_k_codeobj_57;
static PyObject *__pyx_k_codeobj_59;
static PyObject *__pyx_k_codeobj_61;
static PyObject *__pyx_k_codeobj_64;
static PyObject *__pyx_k_codeobj_67;
static PyObject *__pyx_k_codeobj_70;
static PyObject *__pyx_k_codeobj_72;
static PyObject *__pyx_k_codeobj_75;
static PyObject *__pyx_k_codeobj_78;
static PyObject *__pyx_k_codeobj_81;
static PyObject *__pyx_k_codeobj_84;
static PyObject *__pyx_k_codeobj_87;
static PyObject *__pyx_k_codeobj_90;
static PyObject *__pyx_k_codeobj_93;
static PyObject *__pyx_k_codeobj_95;
static PyObject *__pyx_k_codeobj_98;
static PyObject *__pyx_k_codeobj_101;
static PyObject *__pyx_k_codeobj_103;
static PyObject *__pyx_k_codeobj_105;
static PyObject *__pyx_k_codeobj_108;

/* "dipy/tracking/distances.pyx":41
 * DEF biggest_float = 3.4028235e+38 #np.finfo('f4').max
 * 
 * cdef inline cnp.ndarray[cnp.float32_t, ndim=1] as_float_3vec(object vec):             # <<<<<<<<<<<<<<
 *     ''' Utility function to convert object to 3D float vector '''
 *     return np.squeeze(np.asarray(vec, dtype=np.float32))
 */

static CYTHON_INLINE PyArrayObject *__pyx_f_4dipy_8tracking_9distances_as_float_3vec(PyObject *__pyx_v_vec) {
  PyArrayObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyObject *__pyx_t_6 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("as_float_3vec", 0);

  /* "dipy/tracking/distances.pyx":43
 * cdef inline cnp.ndarray[cnp.float32_t, ndim=1] as_float_3vec(object vec):
 *     ''' Utility function to convert object to 3D float vector '''
 *     return np.squeeze(np.asarray(vec, dtype=np.float32))             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(((PyObject *)__pyx_r));
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__squeeze); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__asarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_v_vec);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_vec);
  __Pyx_GIVEREF(__pyx_v_vec);
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__float32); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __pyx_t_6 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_6);
  __Pyx_GIVEREF(__pyx_t_6);
  __pyx_t_6 = 0;
  __pyx_t_6 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_6) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_6, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_r = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;
  goto __pyx_L0;

  __pyx_r = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __Pyx_AddTraceback("dipy.tracking.distances.as_float_3vec", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF((PyObject *)__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":46
 * 
 * 
 * cdef inline float* asfp(cnp.ndarray pt):             # <<<<<<<<<<<<<<
 *     return <float *>pt.data
 * 
 */

static CYTHON_INLINE float *__pyx_f_4dipy_8tracking_9distances_asfp(PyArrayObject *__pyx_v_pt) {
  float *__pyx_r;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("asfp", 0);

  /* "dipy/tracking/distances.pyx":47
 * 
 * cdef inline float* asfp(cnp.ndarray pt):
 *     return <float *>pt.data             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = ((float *)__pyx_v_pt->data);
  goto __pyx_L0;

  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_1normalized_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_normalized_3vec[] = " Return normalized 3D vector\n\n    Vector divided by Euclidean (L2) norm\n\n    Parameters\n    ----------\n    vec : array-like shape (3,)\n\n    Returns\n    -------\n    vec_out : array shape (3,)\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_1normalized_3vec = {__Pyx_NAMESTR("normalized_3vec"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_1normalized_3vec, METH_O, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_normalized_3vec)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_1normalized_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec) {
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("normalized_3vec (wrapper)", 0);
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_normalized_3vec(__pyx_self, ((PyObject *)__pyx_v_vec));
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":50
 * 
 * 
 * def normalized_3vec(vec):             # <<<<<<<<<<<<<<
 *     ''' Return normalized 3D vector
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_normalized_3vec(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec) {
  PyArrayObject *__pyx_v_vec_in = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_in;
  __Pyx_Buffer __pyx_pybuffer_vec_in;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_out;
  __Pyx_Buffer __pyx_pybuffer_vec_out;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyArrayObject *__pyx_t_4 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("normalized_3vec", 0);
  __pyx_pybuffer_vec_in.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_in.refcount = 0;
  __pyx_pybuffernd_vec_in.data = NULL;
  __pyx_pybuffernd_vec_in.rcbuffer = &__pyx_pybuffer_vec_in;
  __pyx_pybuffer_vec_out.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_out.refcount = 0;
  __pyx_pybuffernd_vec_out.data = NULL;
  __pyx_pybuffernd_vec_out.rcbuffer = &__pyx_pybuffer_vec_out;

  /* "dipy/tracking/distances.pyx":63
 *     vec_out : array shape (3,)
 *     '''
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_in = as_float_3vec(vec)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cnormalized_3vec(<float *>vec_in.data, <float*>vec_out.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_in.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_in = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_in.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_in.diminfo[0].strides = __pyx_pybuffernd_vec_in.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_in.diminfo[0].shape = __pyx_pybuffernd_vec_in.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_vec_in = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":64
 *     '''
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_in = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cnormalized_3vec(<float *>vec_in.data, <float*>vec_out.data)
 *     return vec_out
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(((PyObject *)__pyx_k_tuple_1));
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_k_tuple_1));
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_1));
  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_out = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_out.diminfo[0].strides = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_out.diminfo[0].shape = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_t_4 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":65
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_in = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cnormalized_3vec(<float *>vec_in.data, <float*>vec_out.data)             # <<<<<<<<<<<<<<
 *     return vec_out
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_cnormalized_3vec(((float *)__pyx_v_vec_in->data), ((float *)__pyx_v_vec_out->data));

  /* "dipy/tracking/distances.pyx":66
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cnormalized_3vec(<float *>vec_in.data, <float*>vec_out.data)
 *     return vec_out             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_vec_out));
  __pyx_r = ((PyObject *)__pyx_v_vec_out);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_in.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.normalized_3vec", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_in.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_in);
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_out);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_3norm_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_2norm_3vec[] = " Euclidean (L2) norm of length 3 vector\n\n    Parameters\n    ----------\n    vec : array-like shape (3,)\n\n    Returns\n    -------\n    norm : float\n       Euclidean norm\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_3norm_3vec = {__Pyx_NAMESTR("norm_3vec"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_3norm_3vec, METH_O, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_2norm_3vec)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_3norm_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec) {
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("norm_3vec (wrapper)", 0);
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_2norm_3vec(__pyx_self, ((PyObject *)__pyx_v_vec));
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":69
 * 
 * 
 * def norm_3vec(vec):             # <<<<<<<<<<<<<<
 *     ''' Euclidean (L2) norm of length 3 vector
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_2norm_3vec(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec) {
  PyArrayObject *__pyx_v_vec_in = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_in;
  __Pyx_Buffer __pyx_pybuffer_vec_in;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("norm_3vec", 0);
  __pyx_pybuffer_vec_in.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_in.refcount = 0;
  __pyx_pybuffernd_vec_in.data = NULL;
  __pyx_pybuffernd_vec_in.rcbuffer = &__pyx_pybuffer_vec_in;

  /* "dipy/tracking/distances.pyx":81
 *        Euclidean norm
 *     '''
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_in = as_float_3vec(vec)             # <<<<<<<<<<<<<<
 *     return cnorm_3vec(<float *>vec_in.data)
 * 
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_in.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_in = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_in.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_in.diminfo[0].strides = __pyx_pybuffernd_vec_in.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_in.diminfo[0].shape = __pyx_pybuffernd_vec_in.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_vec_in = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":82
 *     '''
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_in = as_float_3vec(vec)
 *     return cnorm_3vec(<float *>vec_in.data)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyFloat_FromDouble(__pyx_f_4dipy_8tracking_9distances_cnorm_3vec(((float *)__pyx_v_vec_in->data))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_in.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.norm_3vec", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_in.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_in);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":85
 * 
 * 
 * cdef inline float cnorm_3vec(float *vec):             # <<<<<<<<<<<<<<
 *     ''' Calculate Euclidean norm of input vector
 * 
 */

static CYTHON_INLINE float __pyx_f_4dipy_8tracking_9distances_cnorm_3vec(float *__pyx_v_vec) {
  float __pyx_v_v0;
  float __pyx_v_v1;
  float __pyx_v_v2;
  float __pyx_r;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cnorm_3vec", 0);

  /* "dipy/tracking/distances.pyx":99
 *     '''
 *     cdef float v0, v1, v2
 *     v0 = vec[0]             # <<<<<<<<<<<<<<
 *     v1 = vec[1]
 *     v2 = vec[2]
 */
  __pyx_v_v0 = (__pyx_v_vec[0]);

  /* "dipy/tracking/distances.pyx":100
 *     cdef float v0, v1, v2
 *     v0 = vec[0]
 *     v1 = vec[1]             # <<<<<<<<<<<<<<
 *     v2 = vec[2]
 *     return sqrt(v0 * v0 + v1*v1 + v2*v2)
 */
  __pyx_v_v1 = (__pyx_v_vec[1]);

  /* "dipy/tracking/distances.pyx":101
 *     v0 = vec[0]
 *     v1 = vec[1]
 *     v2 = vec[2]             # <<<<<<<<<<<<<<
 *     return sqrt(v0 * v0 + v1*v1 + v2*v2)
 * 
 */
  __pyx_v_v2 = (__pyx_v_vec[2]);

  /* "dipy/tracking/distances.pyx":102
 *     v1 = vec[1]
 *     v2 = vec[2]
 *     return sqrt(v0 * v0 + v1*v1 + v2*v2)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = sqrt((((__pyx_v_v0 * __pyx_v_v0) + (__pyx_v_v1 * __pyx_v_v1)) + (__pyx_v_v2 * __pyx_v_v2)));
  goto __pyx_L0;

  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":105
 * 
 * 
 * cdef inline void cnormalized_3vec(float *vec_in, float *vec_out):             # <<<<<<<<<<<<<<
 *     ''' Calculate and fill normalized 3D vector
 * 
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cnormalized_3vec(float *__pyx_v_vec_in, float *__pyx_v_vec_out) {
  float __pyx_v_norm;
  int __pyx_v_i;
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("cnormalized_3vec", 0);

  /* "dipy/tracking/distances.pyx":116
 * 
 *     '''
 *     cdef float norm = cnorm_3vec(vec_in)             # <<<<<<<<<<<<<<
 *     cdef int i
 *     for i in range(3):
 */
  __pyx_v_norm = __pyx_f_4dipy_8tracking_9distances_cnorm_3vec(__pyx_v_vec_in);

  /* "dipy/tracking/distances.pyx":118
 *     cdef float norm = cnorm_3vec(vec_in)
 *     cdef int i
 *     for i in range(3):             # <<<<<<<<<<<<<<
 *         vec_out[i] = vec_in[i] / norm
 * 
 */
  for (__pyx_t_1 = 0; __pyx_t_1 < 3; __pyx_t_1+=1) {
    __pyx_v_i = __pyx_t_1;

    /* "dipy/tracking/distances.pyx":119
 *     cdef int i
 *     for i in range(3):
 *         vec_out[i] = vec_in[i] / norm             # <<<<<<<<<<<<<<
 * 
 * 
 */
    if (unlikely(__pyx_v_norm == 0)) {
      #ifdef WITH_THREAD
      PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
      #endif
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      #ifdef WITH_THREAD
      PyGILState_Release(__pyx_gilstate_save);
      #endif
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    (__pyx_v_vec_out[__pyx_v_i]) = ((__pyx_v_vec_in[__pyx_v_i]) / __pyx_v_norm);
  }

  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_WriteUnraisable("dipy.tracking.distances.cnormalized_3vec", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_5inner_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_5inner_3vecs = {__Pyx_NAMESTR("inner_3vecs"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_5inner_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_5inner_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("inner_3vecs (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("inner_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "inner_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("inner_3vecs", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.inner_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_4inner_3vecs(__pyx_self, __pyx_v_vec1, __pyx_v_vec2);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":122
 * 
 * 
 * def inner_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_4inner_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2) {
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("inner_3vecs", 0);
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;

  /* "dipy/tracking/distances.pyx":123
 * 
 * def inner_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     return cinner_3vecs(<float *>fvec1.data, <float*>fvec2.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":124
 * def inner_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)             # <<<<<<<<<<<<<<
 *     return cinner_3vecs(<float *>fvec1.data, <float*>fvec2.data)
 * 
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":125
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     return cinner_3vecs(<float *>fvec1.data, <float*>fvec2.data)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyFloat_FromDouble(__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.inner_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":128
 * 
 * 
 * cdef inline float cinner_3vecs(float *vec1, float *vec2) nogil:             # <<<<<<<<<<<<<<
 *     cdef int i
 *     cdef float ip = 0
 */

static CYTHON_INLINE float __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(float *__pyx_v_vec1, float *__pyx_v_vec2) {
  int __pyx_v_i;
  float __pyx_v_ip;
  float __pyx_r;

  /* "dipy/tracking/distances.pyx":130
 * cdef inline float cinner_3vecs(float *vec1, float *vec2) nogil:
 *     cdef int i
 *     cdef float ip = 0             # <<<<<<<<<<<<<<
 *     for i from 0<=i<3:
 *         ip += vec1[i]*vec2[i]
 */
  __pyx_v_ip = 0.0;

  /* "dipy/tracking/distances.pyx":131
 *     cdef int i
 *     cdef float ip = 0
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         ip += vec1[i]*vec2[i]
 *     return ip
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":132
 *     cdef float ip = 0
 *     for i from 0<=i<3:
 *         ip += vec1[i]*vec2[i]             # <<<<<<<<<<<<<<
 *     return ip
 * 
 */
    __pyx_v_ip = (__pyx_v_ip + ((__pyx_v_vec1[__pyx_v_i]) * (__pyx_v_vec2[__pyx_v_i])));
  }

  /* "dipy/tracking/distances.pyx":133
 *     for i from 0<=i<3:
 *         ip += vec1[i]*vec2[i]
 *     return ip             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = __pyx_v_ip;
  goto __pyx_L0;

  __pyx_r = 0;
  __pyx_L0:;
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_7sub_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_7sub_3vecs = {__Pyx_NAMESTR("sub_3vecs"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_7sub_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_7sub_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("sub_3vecs (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("sub_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "sub_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("sub_3vecs", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.sub_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_6sub_3vecs(__pyx_self, __pyx_v_vec1, __pyx_v_vec2);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":136
 * 
 * 
 * def sub_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_6sub_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2) {
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_out;
  __Pyx_Buffer __pyx_pybuffer_vec_out;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyArrayObject *__pyx_t_4 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("sub_3vecs", 0);
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;
  __pyx_pybuffer_vec_out.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_out.refcount = 0;
  __pyx_pybuffernd_vec_out.data = NULL;
  __pyx_pybuffernd_vec_out.rcbuffer = &__pyx_pybuffer_vec_out;

  /* "dipy/tracking/distances.pyx":137
 * 
 * def sub_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 137; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":138
 * def sub_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     csub_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 138; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":139
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     csub_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(((PyObject *)__pyx_k_tuple_2));
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_k_tuple_2));
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_2));
  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_out = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_out.diminfo[0].strides = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_out.diminfo[0].shape = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_t_4 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":140
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     csub_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)             # <<<<<<<<<<<<<<
 *     return vec_out
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data), ((float *)__pyx_v_vec_out->data));

  /* "dipy/tracking/distances.pyx":141
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     csub_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_vec_out));
  __pyx_r = ((PyObject *)__pyx_v_vec_out);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.sub_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_out);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":144
 * 
 * 
 * cdef inline void csub_3vecs(float *vec1, float *vec2, float *vec_out) nogil:             # <<<<<<<<<<<<<<
 *     cdef int i
 *     for i from 0<=i<3:
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_csub_3vecs(float *__pyx_v_vec1, float *__pyx_v_vec2, float *__pyx_v_vec_out) {
  int __pyx_v_i;

  /* "dipy/tracking/distances.pyx":146
 * cdef inline void csub_3vecs(float *vec1, float *vec2, float *vec_out) nogil:
 *     cdef int i
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         vec_out[i] = vec1[i]-vec2[i]
 * 
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":147
 *     cdef int i
 *     for i from 0<=i<3:
 *         vec_out[i] = vec1[i]-vec2[i]             # <<<<<<<<<<<<<<
 * 
 * 
 */
    (__pyx_v_vec_out[__pyx_v_i]) = ((__pyx_v_vec1[__pyx_v_i]) - (__pyx_v_vec2[__pyx_v_i]));
  }

}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_9add_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_9add_3vecs = {__Pyx_NAMESTR("add_3vecs"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_9add_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_9add_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("add_3vecs (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("add_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "add_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("add_3vecs", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.add_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_8add_3vecs(__pyx_self, __pyx_v_vec1, __pyx_v_vec2);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":150
 * 
 * 
 * def add_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_8add_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2) {
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_out;
  __Pyx_Buffer __pyx_pybuffer_vec_out;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyArrayObject *__pyx_t_4 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("add_3vecs", 0);
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;
  __pyx_pybuffer_vec_out.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_out.refcount = 0;
  __pyx_pybuffernd_vec_out.data = NULL;
  __pyx_pybuffernd_vec_out.rcbuffer = &__pyx_pybuffer_vec_out;

  /* "dipy/tracking/distances.pyx":151
 * 
 * def add_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":152
 * def add_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cadd_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":153
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cadd_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(((PyObject *)__pyx_k_tuple_3));
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_k_tuple_3));
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_3));
  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_out = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_out.diminfo[0].strides = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_out.diminfo[0].shape = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_t_4 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":154
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cadd_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)             # <<<<<<<<<<<<<<
 *     return vec_out
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_cadd_3vecs(((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data), ((float *)__pyx_v_vec_out->data));

  /* "dipy/tracking/distances.pyx":155
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cadd_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_vec_out));
  __pyx_r = ((PyObject *)__pyx_v_vec_out);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.add_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_out);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":158
 * 
 * 
 * cdef inline void cadd_3vecs(float *vec1, float *vec2, float *vec_out) nogil:             # <<<<<<<<<<<<<<
 *     cdef int i
 *     for i from 0<=i<3:
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cadd_3vecs(float *__pyx_v_vec1, float *__pyx_v_vec2, float *__pyx_v_vec_out) {
  int __pyx_v_i;

  /* "dipy/tracking/distances.pyx":160
 * cdef inline void cadd_3vecs(float *vec1, float *vec2, float *vec_out) nogil:
 *     cdef int i
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         vec_out[i] = vec1[i]+vec2[i]
 * 
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":161
 *     cdef int i
 *     for i from 0<=i<3:
 *         vec_out[i] = vec1[i]+vec2[i]             # <<<<<<<<<<<<<<
 * 
 * def mul_3vecs(vec1, vec2):
 */
    (__pyx_v_vec_out[__pyx_v_i]) = ((__pyx_v_vec1[__pyx_v_i]) + (__pyx_v_vec2[__pyx_v_i]));
  }

}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_11mul_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_11mul_3vecs = {__Pyx_NAMESTR("mul_3vecs"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_11mul_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_11mul_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("mul_3vecs (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("mul_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "mul_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("mul_3vecs", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_10mul_3vecs(__pyx_self, __pyx_v_vec1, __pyx_v_vec2);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":163
 *         vec_out[i] = vec1[i]+vec2[i]
 * 
 * def mul_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_10mul_3vecs(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_vec1, PyObject *__pyx_v_vec2) {
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_out;
  __Pyx_Buffer __pyx_pybuffer_vec_out;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyArrayObject *__pyx_t_4 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("mul_3vecs", 0);
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;
  __pyx_pybuffer_vec_out.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_out.refcount = 0;
  __pyx_pybuffernd_vec_out.data = NULL;
  __pyx_pybuffernd_vec_out.rcbuffer = &__pyx_pybuffer_vec_out;

  /* "dipy/tracking/distances.pyx":164
 * 
 * def mul_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 164; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":165
 * def mul_3vecs(vec1, vec2):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cmul_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":166
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cmul_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(((PyObject *)__pyx_k_tuple_4));
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_k_tuple_4));
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_4));
  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_out = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_out.diminfo[0].strides = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_out.diminfo[0].shape = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_t_4 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":167
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cmul_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)             # <<<<<<<<<<<<<<
 *     return vec_out
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_cmul_3vecs(((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data), ((float *)__pyx_v_vec_out->data));

  /* "dipy/tracking/distances.pyx":168
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cmul_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out             # <<<<<<<<<<<<<<
 * 
 * cdef inline void cmul_3vecs(float *vec1, float *vec2, float *vec_out) nogil:
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_vec_out));
  __pyx_r = ((PyObject *)__pyx_v_vec_out);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vecs", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_out);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":170
 *     return vec_out
 * 
 * cdef inline void cmul_3vecs(float *vec1, float *vec2, float *vec_out) nogil:             # <<<<<<<<<<<<<<
 *     cdef int i
 *     for i from 0<=i<3:
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cmul_3vecs(float *__pyx_v_vec1, float *__pyx_v_vec2, float *__pyx_v_vec_out) {
  int __pyx_v_i;

  /* "dipy/tracking/distances.pyx":172
 * cdef inline void cmul_3vecs(float *vec1, float *vec2, float *vec_out) nogil:
 *     cdef int i
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         vec_out[i] = vec1[i]*vec2[i]
 * 
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":173
 *     cdef int i
 *     for i from 0<=i<3:
 *         vec_out[i] = vec1[i]*vec2[i]             # <<<<<<<<<<<<<<
 * 
 * def mul_3vec(a, vec):
 */
    (__pyx_v_vec_out[__pyx_v_i]) = ((__pyx_v_vec1[__pyx_v_i]) * (__pyx_v_vec2[__pyx_v_i]));
  }

}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_13mul_3vec(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_13mul_3vec = {__Pyx_NAMESTR("mul_3vec"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_13mul_3vec, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_13mul_3vec(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_a = 0;
  PyObject *__pyx_v_vec = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("mul_3vec (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__a,&__pyx_n_s__vec,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__a)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("mul_3vec", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "mul_3vec") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_a = values[0];
    __pyx_v_vec = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("mul_3vec", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vec", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_12mul_3vec(__pyx_self, __pyx_v_a, __pyx_v_vec);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":175
 *         vec_out[i] = vec1[i]*vec2[i]
 * 
 * def mul_3vec(a, vec):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_12mul_3vec(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_vec) {
  PyArrayObject *__pyx_v_fvec = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec;
  __Pyx_Buffer __pyx_pybuffer_fvec;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_vec_out;
  __Pyx_Buffer __pyx_pybuffer_vec_out;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyArrayObject *__pyx_t_4 = NULL;
  float __pyx_t_5;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("mul_3vec", 0);
  __pyx_pybuffer_fvec.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec.refcount = 0;
  __pyx_pybuffernd_fvec.data = NULL;
  __pyx_pybuffernd_fvec.rcbuffer = &__pyx_pybuffer_fvec;
  __pyx_pybuffer_vec_out.pybuffer.buf = NULL;
  __pyx_pybuffer_vec_out.refcount = 0;
  __pyx_pybuffernd_vec_out.data = NULL;
  __pyx_pybuffernd_vec_out.rcbuffer = &__pyx_pybuffer_vec_out;

  /* "dipy/tracking/distances.pyx":176
 * 
 * def mul_3vec(a, vec):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cmul_3vec(a,<float *>fvec.data, <float *>vec_out.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_vec)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_fvec = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_fvec.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_fvec.diminfo[0].strides = __pyx_pybuffernd_fvec.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec.diminfo[0].shape = __pyx_pybuffernd_fvec.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_v_fvec = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":177
 * def mul_3vec(a, vec):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cmul_3vec(a,<float *>fvec.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(((PyObject *)__pyx_k_tuple_5));
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_k_tuple_5));
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_5));
  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
      __pyx_v_vec_out = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_vec_out.diminfo[0].strides = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_vec_out.diminfo[0].shape = __pyx_pybuffernd_vec_out.rcbuffer->pybuffer.shape[0];
    }
  }
  __pyx_t_4 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":178
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cmul_3vec(a,<float *>fvec.data, <float *>vec_out.data)             # <<<<<<<<<<<<<<
 *     return vec_out
 * 
 */
  __pyx_t_5 = __pyx_PyFloat_AsFloat(__pyx_v_a); if (unlikely((__pyx_t_5 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_f_4dipy_8tracking_9distances_cmul_3vec(__pyx_t_5, ((float *)__pyx_v_fvec->data), ((float *)__pyx_v_vec_out->data));

  /* "dipy/tracking/distances.pyx":179
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 *     cmul_3vec(a,<float *>fvec.data, <float *>vec_out.data)
 *     return vec_out             # <<<<<<<<<<<<<<
 * 
 * cdef inline void cmul_3vec(float a, float *vec, float *vec_out) nogil:
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_vec_out));
  __pyx_r = ((PyObject *)__pyx_v_vec_out);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vec", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_vec_out.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec);
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_out);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":181
 *     return vec_out
 * 
 * cdef inline void cmul_3vec(float a, float *vec, float *vec_out) nogil:             # <<<<<<<<<<<<<<
 *     cdef int i
 *     for i from 0<=i<3:
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_cmul_3vec(float __pyx_v_a, float *__pyx_v_vec, float *__pyx_v_vec_out) {
  int __pyx_v_i;

  /* "dipy/tracking/distances.pyx":183
 * cdef inline void cmul_3vec(float a, float *vec, float *vec_out) nogil:
 *     cdef int i
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         vec_out[i] = a*vec[i]
 * 
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":184
 *     cdef int i
 *     for i from 0<=i<3:
 *         vec_out[i] = a*vec[i]             # <<<<<<<<<<<<<<
 * 
 * 
 */
    (__pyx_v_vec_out[__pyx_v_i]) = (__pyx_v_a * (__pyx_v_vec[__pyx_v_i]));
  }

}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_15cut_plane(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_14cut_plane[] = " Extract divergence vectors and points of intersection\n    between planes normal to the reference fiber and other tracks\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape (N1,3) .. (Nm,3)\n    ref : array, shape (N,3)\n        reference track\n\n    Returns\n    -------\n    hits : sequence\n       list of points and rcds (radial coefficient of divergence)``\n\n    Notes\n    -----\n    The orthogonality relationship\n    np.inner(hits[p][q][0:3]-ref[p+1],ref[p+2]-ref[r][p+1]) will hold throughout\n    for every point q in the hits plane at point (p+1) on the reference track.\n\n    Examples\n    --------\n    >>> refx = np.array([[0,0,0],[1,0,0],[2,0,0],[3,0,0]],dtype='float32')\n    >>> bundlex = [np.array([[0.5,1,0],[1.5,2,0],[2.5,3,0]],dtype='float32')]\n    >>> res = cut_plane(bundlex,refx)\n    >>> len(res)\n    2\n    >>> print(res[0])\n    [[ 1.          1.5         0.          0.70710683  0.        ]]\n    >>> print(res[1])\n    [[ 2.          2.5         0.          0.70710677  0.        ]]\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_15cut_plane = {__Pyx_NAMESTR("cut_plane"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_15cut_plane, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_14cut_plane)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_15cut_plane(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_ref = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cut_plane (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__ref,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__ref)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("cut_plane", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "cut_plane") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_tracks = values[0];
    __pyx_v_ref = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("cut_plane", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.cut_plane", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_14cut_plane(__pyx_self, __pyx_v_tracks, __pyx_v_ref);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":191
 * 
 * 
 * def cut_plane(tracks,ref):             # <<<<<<<<<<<<<<
 *     ''' Extract divergence vectors and points of intersection
 *     between planes normal to the reference fiber and other tracks
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_14cut_plane(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_ref) {
  size_t __pyx_v_n_hits;
  size_t __pyx_v_hit_no;
  size_t __pyx_v_max_hit_len;
  float __pyx_v_alpha;
  float __pyx_v_beta;
  float __pyx_v_rcd;
  float __pyx_v_lhp;
  float __pyx_v_ld;
  PyArrayObject *__pyx_v_ref32 = 0;
  PyArrayObject *__pyx_v_track = 0;
  PyObject *__pyx_v_hits = 0;
  PyArrayObject *__pyx_v_one_hit = 0;
  float *__pyx_v_hit_ptr;
  PyArrayObject *__pyx_v_hit_arr = 0;
  PyObject *__pyx_v_Hit = 0;
  size_t __pyx_v_N_tracks;
  PyArrayObject *__pyx_v_track_lengths = 0;
  size_t __pyx_v_t_no;
  size_t __pyx_v_N_track;
  PyObject *__pyx_v_tracks32 = 0;
  size_t __pyx_v_N_ref;
  size_t __pyx_v_p_no;
  size_t __pyx_v_q_no;
  float *__pyx_v_this_ref_p;
  float *__pyx_v_next_ref_p;
  float *__pyx_v_this_trk_p;
  float *__pyx_v_next_trk_p;
  float __pyx_v_along[3];
  float __pyx_v_normal[3];
  float __pyx_v_qMp[3];
  float __pyx_v_rMp[3];
  float __pyx_v_rMq[3];
  float __pyx_v_pMq[3];
  float __pyx_v_hit[3];
  float __pyx_v_hitMp[3];
  float *__pyx_v_delta;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_hit_arr;
  __Pyx_Buffer __pyx_pybuffer_hit_arr;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_one_hit;
  __Pyx_Buffer __pyx_pybuffer_one_hit;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_ref32;
  __Pyx_Buffer __pyx_pybuffer_ref32;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track_lengths;
  __Pyx_Buffer __pyx_pybuffer_track_lengths;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyArrayObject *__pyx_t_4 = NULL;
  int __pyx_t_5;
  PyObject *__pyx_t_6 = NULL;
  PyObject *__pyx_t_7 = NULL;
  PyObject *__pyx_t_8 = NULL;
  Py_ssize_t __pyx_t_9;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyArrayObject *__pyx_t_12 = NULL;
  size_t __pyx_t_13;
  size_t __pyx_t_14;
  PyArrayObject *__pyx_t_15 = NULL;
  size_t __pyx_t_16;
  size_t __pyx_t_17;
  size_t __pyx_t_18;
  size_t __pyx_t_19;
  size_t __pyx_t_20;
  size_t __pyx_t_21;
  size_t __pyx_t_22;
  int __pyx_t_23;
  float __pyx_t_24;
  float __pyx_t_25;
  PyArrayObject *__pyx_t_26 = NULL;
  PyArrayObject *__pyx_t_27 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("cut_plane", 0);
  __pyx_pybuffer_ref32.pybuffer.buf = NULL;
  __pyx_pybuffer_ref32.refcount = 0;
  __pyx_pybuffernd_ref32.data = NULL;
  __pyx_pybuffernd_ref32.rcbuffer = &__pyx_pybuffer_ref32;
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;
  __pyx_pybuffer_one_hit.pybuffer.buf = NULL;
  __pyx_pybuffer_one_hit.refcount = 0;
  __pyx_pybuffernd_one_hit.data = NULL;
  __pyx_pybuffernd_one_hit.rcbuffer = &__pyx_pybuffer_one_hit;
  __pyx_pybuffer_hit_arr.pybuffer.buf = NULL;
  __pyx_pybuffer_hit_arr.refcount = 0;
  __pyx_pybuffernd_hit_arr.data = NULL;
  __pyx_pybuffernd_hit_arr.rcbuffer = &__pyx_pybuffer_hit_arr;
  __pyx_pybuffer_track_lengths.pybuffer.buf = NULL;
  __pyx_pybuffer_track_lengths.refcount = 0;
  __pyx_pybuffernd_track_lengths.data = NULL;
  __pyx_pybuffernd_track_lengths.rcbuffer = &__pyx_pybuffer_track_lengths;

  /* "dipy/tracking/distances.pyx":234
 *         float *hit_ptr
 *         cnp.ndarray[cnp.float32_t, ndim=2] hit_arr
 *         object Hit=[]             # <<<<<<<<<<<<<<
 *     # make reference fiber usable type
 *     ref32 = np.ascontiguousarray(ref, f32_dt)
 */
  __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_v_Hit = ((PyObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":236
 *         object Hit=[]
 *     # make reference fiber usable type
 *     ref32 = np.ascontiguousarray(ref, f32_dt)             # <<<<<<<<<<<<<<
 *     # convert all the tracks to something we can work with.  Get track
 *     # lengths
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_v_ref);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_ref);
  __Pyx_GIVEREF(__pyx_v_ref);
  __Pyx_INCREF(((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
  PyTuple_SET_ITEM(__pyx_t_1, 1, ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
  __Pyx_GIVEREF(((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_ref32.rcbuffer->pybuffer);
    __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_ref32.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_5 < 0)) {
      PyErr_Fetch(&__pyx_t_6, &__pyx_t_7, &__pyx_t_8);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_ref32.rcbuffer->pybuffer, (PyObject*)__pyx_v_ref32, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_6); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_6, __pyx_t_7, __pyx_t_8);
      }
    }
    __pyx_pybuffernd_ref32.diminfo[0].strides = __pyx_pybuffernd_ref32.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_ref32.diminfo[0].shape = __pyx_pybuffernd_ref32.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_ref32.diminfo[1].strides = __pyx_pybuffernd_ref32.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_ref32.diminfo[1].shape = __pyx_pybuffernd_ref32.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 236; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_4 = 0;
  __pyx_v_ref32 = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":240
 *     # lengths
 *     cdef:
 *         size_t N_tracks=len(tracks)             # <<<<<<<<<<<<<<
 *         cnp.ndarray[cnp.uint64_t, ndim=1] track_lengths
 *         size_t t_no, N_track
 */
  __pyx_t_9 = PyObject_Length(__pyx_v_tracks); if (unlikely(__pyx_t_9 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 240; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_N_tracks = __pyx_t_9;

  /* "dipy/tracking/distances.pyx":243
 *         cnp.ndarray[cnp.uint64_t, ndim=1] track_lengths
 *         size_t t_no, N_track
 *     cdef object tracks32 = []             # <<<<<<<<<<<<<<
 *     track_lengths = np.empty((N_tracks,), dtype=np.uint64)
 *     for t_no in range(N_tracks):
 */
  __pyx_t_3 = PyList_New(0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 243; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_v_tracks32 = ((PyObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":244
 *         size_t t_no, N_track
 *     cdef object tracks32 = []
 *     track_lengths = np.empty((N_tracks,), dtype=np.uint64)             # <<<<<<<<<<<<<<
 *     for t_no in range(N_tracks):
 *         track = np.ascontiguousarray(tracks[t_no], f32_dt)
 */
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__empty); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = __Pyx_PyInt_FromSize_t(__pyx_v_N_tracks); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_2));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_2));
  __pyx_t_2 = 0;
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_10 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_10);
  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_10, __pyx_n_s__uint64); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
  if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), __pyx_t_11) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  __pyx_t_11 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_3), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_11);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track_lengths.rcbuffer->pybuffer);
    __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track_lengths.rcbuffer->pybuffer, (PyObject*)__pyx_t_12, &__Pyx_TypeInfo_nn___pyx_t_5numpy_uint64_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_5 < 0)) {
      PyErr_Fetch(&__pyx_t_8, &__pyx_t_7, &__pyx_t_6);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track_lengths.rcbuffer->pybuffer, (PyObject*)__pyx_v_track_lengths, &__Pyx_TypeInfo_nn___pyx_t_5numpy_uint64_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_6);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_8, __pyx_t_7, __pyx_t_6);
      }
    }
    __pyx_pybuffernd_track_lengths.diminfo[0].strides = __pyx_pybuffernd_track_lengths.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track_lengths.diminfo[0].shape = __pyx_pybuffernd_track_lengths.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __pyx_v_track_lengths = ((PyArrayObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "dipy/tracking/distances.pyx":245
 *     cdef object tracks32 = []
 *     track_lengths = np.empty((N_tracks,), dtype=np.uint64)
 *     for t_no in range(N_tracks):             # <<<<<<<<<<<<<<
 *         track = np.ascontiguousarray(tracks[t_no], f32_dt)
 *         track_lengths[t_no] = track.shape[0]
 */
  __pyx_t_13 = __pyx_v_N_tracks;
  for (__pyx_t_14 = 0; __pyx_t_14 < __pyx_t_13; __pyx_t_14+=1) {
    __pyx_v_t_no = __pyx_t_14;

    /* "dipy/tracking/distances.pyx":246
 *     track_lengths = np.empty((N_tracks,), dtype=np.uint64)
 *     for t_no in range(N_tracks):
 *         track = np.ascontiguousarray(tracks[t_no], f32_dt)             # <<<<<<<<<<<<<<
 *         track_lengths[t_no] = track.shape[0]
 *         tracks32.append(track)
 */
    __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
    __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_tracks, __pyx_v_t_no, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_11);
    __Pyx_GIVEREF(__pyx_t_11);
    __Pyx_INCREF(((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
    PyTuple_SET_ITEM(__pyx_t_3, 1, ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
    __Pyx_GIVEREF(((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
    __pyx_t_11 = 0;
    __pyx_t_11 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_15 = ((PyArrayObject *)__pyx_t_11);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
      __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_15, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_5 < 0)) {
        PyErr_Fetch(&__pyx_t_6, &__pyx_t_7, &__pyx_t_8);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_6); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_6, __pyx_t_7, __pyx_t_8);
        }
      }
      __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_15 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_11);
    __pyx_t_11 = 0;

    /* "dipy/tracking/distances.pyx":247
 *     for t_no in range(N_tracks):
 *         track = np.ascontiguousarray(tracks[t_no], f32_dt)
 *         track_lengths[t_no] = track.shape[0]             # <<<<<<<<<<<<<<
 *         tracks32.append(track)
 *     # set up loop across reference fiber points
 */
    __pyx_t_16 = __pyx_v_t_no;
    __pyx_t_5 = -1;
    if (unlikely(__pyx_t_16 >= (size_t)__pyx_pybuffernd_track_lengths.diminfo[0].shape)) __pyx_t_5 = 0;
    if (unlikely(__pyx_t_5 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_5);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 247; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    *__Pyx_BufPtrStrided1d(__pyx_t_5numpy_uint64_t *, __pyx_pybuffernd_track_lengths.rcbuffer->pybuffer.buf, __pyx_t_16, __pyx_pybuffernd_track_lengths.diminfo[0].strides) = (__pyx_v_track->dimensions[0]);

    /* "dipy/tracking/distances.pyx":248
 *         track = np.ascontiguousarray(tracks[t_no], f32_dt)
 *         track_lengths[t_no] = track.shape[0]
 *         tracks32.append(track)             # <<<<<<<<<<<<<<
 *     # set up loop across reference fiber points
 *     cdef:
 */
    __pyx_t_11 = __Pyx_PyObject_Append(__pyx_v_tracks32, ((PyObject *)__pyx_v_track)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  }

  /* "dipy/tracking/distances.pyx":251
 *     # set up loop across reference fiber points
 *     cdef:
 *         size_t N_ref = ref32.shape[0]             # <<<<<<<<<<<<<<
 *         size_t p_no, q_no
 *         float *this_ref_p, *next_ref_p, *this_trk_p, *next_trk_p
 */
  __pyx_v_N_ref = (__pyx_v_ref32->dimensions[0]);

  /* "dipy/tracking/distances.pyx":260
 *     # small numpy arrays, and reuse them over the reference track point
 *     # loops.
 *     max_hit_len = 0             # <<<<<<<<<<<<<<
 *     hits = []
 *     # for every point along the reference track
 */
  __pyx_v_max_hit_len = 0;

  /* "dipy/tracking/distances.pyx":261
 *     # loops.
 *     max_hit_len = 0
 *     hits = []             # <<<<<<<<<<<<<<
 *     # for every point along the reference track
 *     next_ref_p = asfp(ref32[0])
 */
  __pyx_t_11 = PyList_New(0); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 261; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __pyx_v_hits = ((PyObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "dipy/tracking/distances.pyx":263
 *     hits = []
 *     # for every point along the reference track
 *     next_ref_p = asfp(ref32[0])             # <<<<<<<<<<<<<<
 *     for p_no in range(N_ref-1):
 *         # extract point to point vector into `along`
 */
  __pyx_t_11 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ref32), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_next_ref_p = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_11));
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;

  /* "dipy/tracking/distances.pyx":264
 *     # for every point along the reference track
 *     next_ref_p = asfp(ref32[0])
 *     for p_no in range(N_ref-1):             # <<<<<<<<<<<<<<
 *         # extract point to point vector into `along`
 *         this_ref_p = next_ref_p
 */
  __pyx_t_13 = (__pyx_v_N_ref - 1);
  for (__pyx_t_14 = 0; __pyx_t_14 < __pyx_t_13; __pyx_t_14+=1) {
    __pyx_v_p_no = __pyx_t_14;

    /* "dipy/tracking/distances.pyx":266
 *     for p_no in range(N_ref-1):
 *         # extract point to point vector into `along`
 *         this_ref_p = next_ref_p             # <<<<<<<<<<<<<<
 *         next_ref_p = asfp(ref32[p_no+1])
 *         csub_3vecs(next_ref_p, this_ref_p, along)
 */
    __pyx_v_this_ref_p = __pyx_v_next_ref_p;

    /* "dipy/tracking/distances.pyx":267
 *         # extract point to point vector into `along`
 *         this_ref_p = next_ref_p
 *         next_ref_p = asfp(ref32[p_no+1])             # <<<<<<<<<<<<<<
 *         csub_3vecs(next_ref_p, this_ref_p, along)
 *         # normalize
 */
    __pyx_t_17 = (__pyx_v_p_no + 1);
    __pyx_t_11 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ref32), __pyx_t_17, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_next_ref_p = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_11));
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;

    /* "dipy/tracking/distances.pyx":268
 *         this_ref_p = next_ref_p
 *         next_ref_p = asfp(ref32[p_no+1])
 *         csub_3vecs(next_ref_p, this_ref_p, along)             # <<<<<<<<<<<<<<
 *         # normalize
 *         cnormalized_3vec(along, normal)
 */
    __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_next_ref_p, __pyx_v_this_ref_p, __pyx_v_along);

    /* "dipy/tracking/distances.pyx":270
 *         csub_3vecs(next_ref_p, this_ref_p, along)
 *         # normalize
 *         cnormalized_3vec(along, normal)             # <<<<<<<<<<<<<<
 *         # initialize index for hits
 *         hit_no = 0
 */
    __pyx_f_4dipy_8tracking_9distances_cnormalized_3vec(__pyx_v_along, __pyx_v_normal);

    /* "dipy/tracking/distances.pyx":272
 *         cnormalized_3vec(along, normal)
 *         # initialize index for hits
 *         hit_no = 0             # <<<<<<<<<<<<<<
 *         # for every track
 *         for t_no in range(N_tracks):
 */
    __pyx_v_hit_no = 0;

    /* "dipy/tracking/distances.pyx":274
 *         hit_no = 0
 *         # for every track
 *         for t_no in range(N_tracks):             # <<<<<<<<<<<<<<
 *             track=tracks32[t_no]
 *             N_track = track_lengths[t_no]
 */
    __pyx_t_17 = __pyx_v_N_tracks;
    for (__pyx_t_18 = 0; __pyx_t_18 < __pyx_t_17; __pyx_t_18+=1) {
      __pyx_v_t_no = __pyx_t_18;

      /* "dipy/tracking/distances.pyx":275
 *         # for every track
 *         for t_no in range(N_tracks):
 *             track=tracks32[t_no]             # <<<<<<<<<<<<<<
 *             N_track = track_lengths[t_no]
 *             # for every point on the track
 */
      __pyx_t_11 = __Pyx_GetItemInt(__pyx_v_tracks32, __pyx_v_t_no, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_11);
      if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_15 = ((PyArrayObject *)__pyx_t_11);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
        __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_15, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_5 < 0)) {
          PyErr_Fetch(&__pyx_t_8, &__pyx_t_7, &__pyx_t_6);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_6);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_8, __pyx_t_7, __pyx_t_6);
          }
        }
        __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_15 = 0;
      __Pyx_XDECREF(((PyObject *)__pyx_v_track));
      __pyx_v_track = ((PyArrayObject *)__pyx_t_11);
      __pyx_t_11 = 0;

      /* "dipy/tracking/distances.pyx":276
 *         for t_no in range(N_tracks):
 *             track=tracks32[t_no]
 *             N_track = track_lengths[t_no]             # <<<<<<<<<<<<<<
 *             # for every point on the track
 *             next_trk_p = asfp(track[0])
 */
      __pyx_t_19 = __pyx_v_t_no;
      __pyx_t_5 = -1;
      if (unlikely(__pyx_t_19 >= (size_t)__pyx_pybuffernd_track_lengths.diminfo[0].shape)) __pyx_t_5 = 0;
      if (unlikely(__pyx_t_5 != -1)) {
        __Pyx_RaiseBufferIndexError(__pyx_t_5);
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_v_N_track = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_uint64_t *, __pyx_pybuffernd_track_lengths.rcbuffer->pybuffer.buf, __pyx_t_19, __pyx_pybuffernd_track_lengths.diminfo[0].strides));

      /* "dipy/tracking/distances.pyx":278
 *             N_track = track_lengths[t_no]
 *             # for every point on the track
 *             next_trk_p = asfp(track[0])             # <<<<<<<<<<<<<<
 *             for q_no in range(N_track-1):
 *                 # p = ref32[p_no]
 */
      __pyx_t_11 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_11);
      if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_v_next_trk_p = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_11));
      __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;

      /* "dipy/tracking/distances.pyx":279
 *             # for every point on the track
 *             next_trk_p = asfp(track[0])
 *             for q_no in range(N_track-1):             # <<<<<<<<<<<<<<
 *                 # p = ref32[p_no]
 *                 # q = track[q_no]
 */
      __pyx_t_20 = (__pyx_v_N_track - 1);
      for (__pyx_t_21 = 0; __pyx_t_21 < __pyx_t_20; __pyx_t_21+=1) {
        __pyx_v_q_no = __pyx_t_21;

        /* "dipy/tracking/distances.pyx":284
 *                 # r = track[q_no+1]
 *                 # float* versions of above: p == this_ref_p
 *                 this_trk_p = next_trk_p # q             # <<<<<<<<<<<<<<
 *                 next_trk_p = asfp(track[q_no+1]) # r
 *                 #if np.inner(normal,q-p)*np.inner(normal,r-p) <= 0:
 */
        __pyx_v_this_trk_p = __pyx_v_next_trk_p;

        /* "dipy/tracking/distances.pyx":285
 *                 # float* versions of above: p == this_ref_p
 *                 this_trk_p = next_trk_p # q
 *                 next_trk_p = asfp(track[q_no+1]) # r             # <<<<<<<<<<<<<<
 *                 #if np.inner(normal,q-p)*np.inner(normal,r-p) <= 0:
 *                 csub_3vecs(this_trk_p, this_ref_p, qMp) # q-p
 */
        __pyx_t_22 = (__pyx_v_q_no + 1);
        __pyx_t_11 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_t_22, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 285; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_11);
        if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 285; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __pyx_v_next_trk_p = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_11));
        __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;

        /* "dipy/tracking/distances.pyx":287
 *                 next_trk_p = asfp(track[q_no+1]) # r
 *                 #if np.inner(normal,q-p)*np.inner(normal,r-p) <= 0:
 *                 csub_3vecs(this_trk_p, this_ref_p, qMp) # q-p             # <<<<<<<<<<<<<<
 *                 csub_3vecs(next_trk_p, this_ref_p, rMp) # r-p
 *                 if (cinner_3vecs(normal, qMp) * cinner_3vecs(normal, rMp)) <=0:
 */
        __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_this_trk_p, __pyx_v_this_ref_p, __pyx_v_qMp);

        /* "dipy/tracking/distances.pyx":288
 *                 #if np.inner(normal,q-p)*np.inner(normal,r-p) <= 0:
 *                 csub_3vecs(this_trk_p, this_ref_p, qMp) # q-p
 *                 csub_3vecs(next_trk_p, this_ref_p, rMp) # r-p             # <<<<<<<<<<<<<<
 *                 if (cinner_3vecs(normal, qMp) * cinner_3vecs(normal, rMp)) <=0:
 *                     #if np.inner((r-q),normal) != 0:
 */
        __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_next_trk_p, __pyx_v_this_ref_p, __pyx_v_rMp);

        /* "dipy/tracking/distances.pyx":289
 *                 csub_3vecs(this_trk_p, this_ref_p, qMp) # q-p
 *                 csub_3vecs(next_trk_p, this_ref_p, rMp) # r-p
 *                 if (cinner_3vecs(normal, qMp) * cinner_3vecs(normal, rMp)) <=0:             # <<<<<<<<<<<<<<
 *                     #if np.inner((r-q),normal) != 0:
 *                     csub_3vecs(next_trk_p, this_trk_p, rMq)
 */
        __pyx_t_23 = (((__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_normal, __pyx_v_qMp) * __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_normal, __pyx_v_rMp)) <= 0.0) != 0);
        if (__pyx_t_23) {

          /* "dipy/tracking/distances.pyx":291
 *                 if (cinner_3vecs(normal, qMp) * cinner_3vecs(normal, rMp)) <=0:
 *                     #if np.inner((r-q),normal) != 0:
 *                     csub_3vecs(next_trk_p, this_trk_p, rMq)             # <<<<<<<<<<<<<<
 *                     beta = cinner_3vecs(rMq, normal)
 *                     if beta !=0:
 */
          __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_next_trk_p, __pyx_v_this_trk_p, __pyx_v_rMq);

          /* "dipy/tracking/distances.pyx":292
 *                     #if np.inner((r-q),normal) != 0:
 *                     csub_3vecs(next_trk_p, this_trk_p, rMq)
 *                     beta = cinner_3vecs(rMq, normal)             # <<<<<<<<<<<<<<
 *                     if beta !=0:
 *                         #alpha = np.inner((p-q),normal)/np.inner((r-q),normal)
 */
          __pyx_v_beta = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_rMq, __pyx_v_normal);

          /* "dipy/tracking/distances.pyx":293
 *                     csub_3vecs(next_trk_p, this_trk_p, rMq)
 *                     beta = cinner_3vecs(rMq, normal)
 *                     if beta !=0:             # <<<<<<<<<<<<<<
 *                         #alpha = np.inner((p-q),normal)/np.inner((r-q),normal)
 *                         csub_3vecs(this_ref_p, this_trk_p, pMq)
 */
          __pyx_t_23 = ((__pyx_v_beta != 0.0) != 0);
          if (__pyx_t_23) {

            /* "dipy/tracking/distances.pyx":295
 *                     if beta !=0:
 *                         #alpha = np.inner((p-q),normal)/np.inner((r-q),normal)
 *                         csub_3vecs(this_ref_p, this_trk_p, pMq)             # <<<<<<<<<<<<<<
 *                         alpha = (cinner_3vecs(pMq, normal) /
 *                                   cinner_3vecs(rMq, normal))
 */
            __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_this_ref_p, __pyx_v_this_trk_p, __pyx_v_pMq);

            /* "dipy/tracking/distances.pyx":296
 *                         #alpha = np.inner((p-q),normal)/np.inner((r-q),normal)
 *                         csub_3vecs(this_ref_p, this_trk_p, pMq)
 *                         alpha = (cinner_3vecs(pMq, normal) /             # <<<<<<<<<<<<<<
 *                                   cinner_3vecs(rMq, normal))
 *                         if alpha < 1:
 */
            __pyx_t_24 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_pMq, __pyx_v_normal);

            /* "dipy/tracking/distances.pyx":297
 *                         csub_3vecs(this_ref_p, this_trk_p, pMq)
 *                         alpha = (cinner_3vecs(pMq, normal) /
 *                                   cinner_3vecs(rMq, normal))             # <<<<<<<<<<<<<<
 *                         if alpha < 1:
 *                             # hit = q+alpha*(r-q)
 */
            __pyx_t_25 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_rMq, __pyx_v_normal);
            if (unlikely(__pyx_t_25 == 0)) {
              #ifdef WITH_THREAD
              PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
              #endif
              PyErr_Format(PyExc_ZeroDivisionError, "float division");
              #ifdef WITH_THREAD
              PyGILState_Release(__pyx_gilstate_save);
              #endif
              {__pyx_filename = __pyx_f[0]; __pyx_lineno = 296; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
            }
            __pyx_v_alpha = (__pyx_t_24 / __pyx_t_25);

            /* "dipy/tracking/distances.pyx":298
 *                         alpha = (cinner_3vecs(pMq, normal) /
 *                                   cinner_3vecs(rMq, normal))
 *                         if alpha < 1:             # <<<<<<<<<<<<<<
 *                             # hit = q+alpha*(r-q)
 *                             hit[0] = this_trk_p[0]+alpha*rMq[0]
 */
            __pyx_t_23 = ((__pyx_v_alpha < 1.0) != 0);
            if (__pyx_t_23) {

              /* "dipy/tracking/distances.pyx":300
 *                         if alpha < 1:
 *                             # hit = q+alpha*(r-q)
 *                             hit[0] = this_trk_p[0]+alpha*rMq[0]             # <<<<<<<<<<<<<<
 *                             hit[1] = this_trk_p[1]+alpha*rMq[1]
 *                             hit[2] = this_trk_p[2]+alpha*rMq[2]
 */
              (__pyx_v_hit[0]) = ((__pyx_v_this_trk_p[0]) + (__pyx_v_alpha * (__pyx_v_rMq[0])));

              /* "dipy/tracking/distances.pyx":301
 *                             # hit = q+alpha*(r-q)
 *                             hit[0] = this_trk_p[0]+alpha*rMq[0]
 *                             hit[1] = this_trk_p[1]+alpha*rMq[1]             # <<<<<<<<<<<<<<
 *                             hit[2] = this_trk_p[2]+alpha*rMq[2]
 *                             # h-p
 */
              (__pyx_v_hit[1]) = ((__pyx_v_this_trk_p[1]) + (__pyx_v_alpha * (__pyx_v_rMq[1])));

              /* "dipy/tracking/distances.pyx":302
 *                             hit[0] = this_trk_p[0]+alpha*rMq[0]
 *                             hit[1] = this_trk_p[1]+alpha*rMq[1]
 *                             hit[2] = this_trk_p[2]+alpha*rMq[2]             # <<<<<<<<<<<<<<
 *                             # h-p
 *                             csub_3vecs(hit, this_ref_p, hitMp)
 */
              (__pyx_v_hit[2]) = ((__pyx_v_this_trk_p[2]) + (__pyx_v_alpha * (__pyx_v_rMq[2])));

              /* "dipy/tracking/distances.pyx":304
 *                             hit[2] = this_trk_p[2]+alpha*rMq[2]
 *                             # h-p
 *                             csub_3vecs(hit, this_ref_p, hitMp)             # <<<<<<<<<<<<<<
 *                             # |h-p|
 *                             lhp = cnorm_3vec(hitMp)
 */
              __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_hit, __pyx_v_this_ref_p, __pyx_v_hitMp);

              /* "dipy/tracking/distances.pyx":306
 *                             csub_3vecs(hit, this_ref_p, hitMp)
 *                             # |h-p|
 *                             lhp = cnorm_3vec(hitMp)             # <<<<<<<<<<<<<<
 *                             delta = rMq # just renaming
 *                             # |r-q| == |delta|
 */
              __pyx_v_lhp = __pyx_f_4dipy_8tracking_9distances_cnorm_3vec(__pyx_v_hitMp);

              /* "dipy/tracking/distances.pyx":307
 *                             # |h-p|
 *                             lhp = cnorm_3vec(hitMp)
 *                             delta = rMq # just renaming             # <<<<<<<<<<<<<<
 *                             # |r-q| == |delta|
 *                             ld = cnorm_3vec(delta)
 */
              __pyx_v_delta = __pyx_v_rMq;

              /* "dipy/tracking/distances.pyx":309
 *                             delta = rMq # just renaming
 *                             # |r-q| == |delta|
 *                             ld = cnorm_3vec(delta)             # <<<<<<<<<<<<<<
 *                             ''' # Summary of stuff in comments
 *                             # divergence =((r-q)-inner(r-q,normal)*normal)/|r-q|
 */
              __pyx_v_ld = __pyx_f_4dipy_8tracking_9distances_cnorm_3vec(__pyx_v_delta);

              /* "dipy/tracking/distances.pyx":319
 *                             # radial divergence
 *                             # np.inner(delta, (hit-p)) / (ld * lhp)
 *                             if lhp > 0:             # <<<<<<<<<<<<<<
 *                                 rcd = fabs(cinner_3vecs(delta, hitMp)
 *                                            / (ld*lhp))
 */
              __pyx_t_23 = ((__pyx_v_lhp > 0.0) != 0);
              if (__pyx_t_23) {

                /* "dipy/tracking/distances.pyx":320
 *                             # np.inner(delta, (hit-p)) / (ld * lhp)
 *                             if lhp > 0:
 *                                 rcd = fabs(cinner_3vecs(delta, hitMp)             # <<<<<<<<<<<<<<
 *                                            / (ld*lhp))
 *                             else:
 */
                __pyx_t_25 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_delta, __pyx_v_hitMp);

                /* "dipy/tracking/distances.pyx":321
 *                             if lhp > 0:
 *                                 rcd = fabs(cinner_3vecs(delta, hitMp)
 *                                            / (ld*lhp))             # <<<<<<<<<<<<<<
 *                             else:
 *                                 rcd=0
 */
                __pyx_t_24 = (__pyx_v_ld * __pyx_v_lhp);
                if (unlikely(__pyx_t_24 == 0)) {
                  #ifdef WITH_THREAD
                  PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
                  #endif
                  PyErr_Format(PyExc_ZeroDivisionError, "float division");
                  #ifdef WITH_THREAD
                  PyGILState_Release(__pyx_gilstate_save);
                  #endif
                  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 321; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                }
                __pyx_v_rcd = fabs((__pyx_t_25 / __pyx_t_24));
                goto __pyx_L14;
              }
              /*else*/ {

                /* "dipy/tracking/distances.pyx":323
 *                                            / (ld*lhp))
 *                             else:
 *                                 rcd=0             # <<<<<<<<<<<<<<
 *                             # hit data into array
 *                             if hit_no >= max_hit_len:
 */
                __pyx_v_rcd = 0.0;
              }
              __pyx_L14:;

              /* "dipy/tracking/distances.pyx":325
 *                                 rcd=0
 *                             # hit data into array
 *                             if hit_no >= max_hit_len:             # <<<<<<<<<<<<<<
 *                                 one_hit = np.empty((5,), dtype=f32_dt)
 *                                 hits.append(one_hit)
 */
              __pyx_t_23 = ((__pyx_v_hit_no >= __pyx_v_max_hit_len) != 0);
              if (__pyx_t_23) {

                /* "dipy/tracking/distances.pyx":326
 *                             # hit data into array
 *                             if hit_no >= max_hit_len:
 *                                 one_hit = np.empty((5,), dtype=f32_dt)             # <<<<<<<<<<<<<<
 *                                 hits.append(one_hit)
 *                             else:
 */
                __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_11);
                __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s__empty); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_3);
                __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
                __pyx_t_11 = PyDict_New(); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(((PyObject *)__pyx_t_11));
                if (PyDict_SetItem(__pyx_t_11, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __pyx_t_2 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_k_tuple_7), ((PyObject *)__pyx_t_11)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_2);
                __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
                __Pyx_DECREF(((PyObject *)__pyx_t_11)); __pyx_t_11 = 0;
                if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __pyx_t_26 = ((PyArrayObject *)__pyx_t_2);
                {
                  __Pyx_BufFmt_StackElem __pyx_stack[1];
                  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer);
                  __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer, (PyObject*)__pyx_t_26, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
                  if (unlikely(__pyx_t_5 < 0)) {
                    PyErr_Fetch(&__pyx_t_6, &__pyx_t_7, &__pyx_t_8);
                    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer, (PyObject*)__pyx_v_one_hit, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
                      Py_XDECREF(__pyx_t_6); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8);
                      __Pyx_RaiseBufferFallbackError();
                    } else {
                      PyErr_Restore(__pyx_t_6, __pyx_t_7, __pyx_t_8);
                    }
                  }
                  __pyx_pybuffernd_one_hit.diminfo[0].strides = __pyx_pybuffernd_one_hit.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_one_hit.diminfo[0].shape = __pyx_pybuffernd_one_hit.rcbuffer->pybuffer.shape[0];
                  if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                }
                __pyx_t_26 = 0;
                __Pyx_XDECREF(((PyObject *)__pyx_v_one_hit));
                __pyx_v_one_hit = ((PyArrayObject *)__pyx_t_2);
                __pyx_t_2 = 0;

                /* "dipy/tracking/distances.pyx":327
 *                             if hit_no >= max_hit_len:
 *                                 one_hit = np.empty((5,), dtype=f32_dt)
 *                                 hits.append(one_hit)             # <<<<<<<<<<<<<<
 *                             else:
 *                                 one_hit = hits[hit_no]
 */
                __pyx_t_2 = __Pyx_PyObject_Append(__pyx_v_hits, ((PyObject *)__pyx_v_one_hit)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_2);
                __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
                goto __pyx_L15;
              }
              /*else*/ {

                /* "dipy/tracking/distances.pyx":329
 *                                 hits.append(one_hit)
 *                             else:
 *                                 one_hit = hits[hit_no]             # <<<<<<<<<<<<<<
 *                             hit_ptr = <float *>one_hit.data
 *                             hit_ptr[0] = hit[0]
 */
                __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_hits, __pyx_v_hit_no, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_2);
                if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __pyx_t_26 = ((PyArrayObject *)__pyx_t_2);
                {
                  __Pyx_BufFmt_StackElem __pyx_stack[1];
                  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer);
                  __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer, (PyObject*)__pyx_t_26, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
                  if (unlikely(__pyx_t_5 < 0)) {
                    PyErr_Fetch(&__pyx_t_8, &__pyx_t_7, &__pyx_t_6);
                    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer, (PyObject*)__pyx_v_one_hit, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
                      Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_6);
                      __Pyx_RaiseBufferFallbackError();
                    } else {
                      PyErr_Restore(__pyx_t_8, __pyx_t_7, __pyx_t_6);
                    }
                  }
                  __pyx_pybuffernd_one_hit.diminfo[0].strides = __pyx_pybuffernd_one_hit.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_one_hit.diminfo[0].shape = __pyx_pybuffernd_one_hit.rcbuffer->pybuffer.shape[0];
                  if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 329; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                }
                __pyx_t_26 = 0;
                __Pyx_XDECREF(((PyObject *)__pyx_v_one_hit));
                __pyx_v_one_hit = ((PyArrayObject *)__pyx_t_2);
                __pyx_t_2 = 0;
              }
              __pyx_L15:;

              /* "dipy/tracking/distances.pyx":330
 *                             else:
 *                                 one_hit = hits[hit_no]
 *                             hit_ptr = <float *>one_hit.data             # <<<<<<<<<<<<<<
 *                             hit_ptr[0] = hit[0]
 *                             hit_ptr[1] = hit[1]
 */
              __pyx_v_hit_ptr = ((float *)__pyx_v_one_hit->data);

              /* "dipy/tracking/distances.pyx":331
 *                                 one_hit = hits[hit_no]
 *                             hit_ptr = <float *>one_hit.data
 *                             hit_ptr[0] = hit[0]             # <<<<<<<<<<<<<<
 *                             hit_ptr[1] = hit[1]
 *                             hit_ptr[2] = hit[2]
 */
              (__pyx_v_hit_ptr[0]) = (__pyx_v_hit[0]);

              /* "dipy/tracking/distances.pyx":332
 *                             hit_ptr = <float *>one_hit.data
 *                             hit_ptr[0] = hit[0]
 *                             hit_ptr[1] = hit[1]             # <<<<<<<<<<<<<<
 *                             hit_ptr[2] = hit[2]
 *                             hit_ptr[3] = rcd
 */
              (__pyx_v_hit_ptr[1]) = (__pyx_v_hit[1]);

              /* "dipy/tracking/distances.pyx":333
 *                             hit_ptr[0] = hit[0]
 *                             hit_ptr[1] = hit[1]
 *                             hit_ptr[2] = hit[2]             # <<<<<<<<<<<<<<
 *                             hit_ptr[3] = rcd
 *                             hit_ptr[4] = t_no
 */
              (__pyx_v_hit_ptr[2]) = (__pyx_v_hit[2]);

              /* "dipy/tracking/distances.pyx":334
 *                             hit_ptr[1] = hit[1]
 *                             hit_ptr[2] = hit[2]
 *                             hit_ptr[3] = rcd             # <<<<<<<<<<<<<<
 *                             hit_ptr[4] = t_no
 *                             hit_no += 1
 */
              (__pyx_v_hit_ptr[3]) = __pyx_v_rcd;

              /* "dipy/tracking/distances.pyx":335
 *                             hit_ptr[2] = hit[2]
 *                             hit_ptr[3] = rcd
 *                             hit_ptr[4] = t_no             # <<<<<<<<<<<<<<
 *                             hit_no += 1
 *         # convert hits list to hits array
 */
              (__pyx_v_hit_ptr[4]) = __pyx_v_t_no;

              /* "dipy/tracking/distances.pyx":336
 *                             hit_ptr[3] = rcd
 *                             hit_ptr[4] = t_no
 *                             hit_no += 1             # <<<<<<<<<<<<<<
 *         # convert hits list to hits array
 *         n_hits = hit_no
 */
              __pyx_v_hit_no = (__pyx_v_hit_no + 1);
              goto __pyx_L13;
            }
            __pyx_L13:;
            goto __pyx_L12;
          }
          __pyx_L12:;
          goto __pyx_L11;
        }
        __pyx_L11:;
      }
    }

    /* "dipy/tracking/distances.pyx":338
 *                             hit_no += 1
 *         # convert hits list to hits array
 *         n_hits = hit_no             # <<<<<<<<<<<<<<
 *         if n_hits > max_hit_len:
 *             max_hit_len = n_hits
 */
    __pyx_v_n_hits = __pyx_v_hit_no;

    /* "dipy/tracking/distances.pyx":339
 *         # convert hits list to hits array
 *         n_hits = hit_no
 *         if n_hits > max_hit_len:             # <<<<<<<<<<<<<<
 *             max_hit_len = n_hits
 *         hit_arr = np.empty((n_hits,5), dtype=f32_dt)
 */
    __pyx_t_23 = ((__pyx_v_n_hits > __pyx_v_max_hit_len) != 0);
    if (__pyx_t_23) {

      /* "dipy/tracking/distances.pyx":340
 *         n_hits = hit_no
 *         if n_hits > max_hit_len:
 *             max_hit_len = n_hits             # <<<<<<<<<<<<<<
 *         hit_arr = np.empty((n_hits,5), dtype=f32_dt)
 *         for hit_no in range(n_hits):
 */
      __pyx_v_max_hit_len = __pyx_v_n_hits;
      goto __pyx_L16;
    }
    __pyx_L16:;

    /* "dipy/tracking/distances.pyx":341
 *         if n_hits > max_hit_len:
 *             max_hit_len = n_hits
 *         hit_arr = np.empty((n_hits,5), dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         for hit_no in range(n_hits):
 *             hit_arr[hit_no] = hits[hit_no]
 */
    __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__empty); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __pyx_t_2 = __Pyx_PyInt_FromSize_t(__pyx_v_n_hits); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2);
    __Pyx_GIVEREF(__pyx_t_2);
    __Pyx_INCREF(__pyx_int_5);
    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_int_5);
    __Pyx_GIVEREF(__pyx_int_5);
    __pyx_t_2 = 0;
    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_3));
    __Pyx_GIVEREF(((PyObject *)__pyx_t_3));
    __pyx_t_3 = 0;
    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_3));
    if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_1 = PyObject_Call(__pyx_t_11, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_27 = ((PyArrayObject *)__pyx_t_1);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_hit_arr.rcbuffer->pybuffer);
      __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_hit_arr.rcbuffer->pybuffer, (PyObject*)__pyx_t_27, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_5 < 0)) {
        PyErr_Fetch(&__pyx_t_6, &__pyx_t_7, &__pyx_t_8);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_hit_arr.rcbuffer->pybuffer, (PyObject*)__pyx_v_hit_arr, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_6); Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_6, __pyx_t_7, __pyx_t_8);
        }
      }
      __pyx_pybuffernd_hit_arr.diminfo[0].strides = __pyx_pybuffernd_hit_arr.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_hit_arr.diminfo[0].shape = __pyx_pybuffernd_hit_arr.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_hit_arr.diminfo[1].strides = __pyx_pybuffernd_hit_arr.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_hit_arr.diminfo[1].shape = __pyx_pybuffernd_hit_arr.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_27 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_hit_arr));
    __pyx_v_hit_arr = ((PyArrayObject *)__pyx_t_1);
    __pyx_t_1 = 0;

    /* "dipy/tracking/distances.pyx":342
 *             max_hit_len = n_hits
 *         hit_arr = np.empty((n_hits,5), dtype=f32_dt)
 *         for hit_no in range(n_hits):             # <<<<<<<<<<<<<<
 *             hit_arr[hit_no] = hits[hit_no]
 *         Hit.append(hit_arr)
 */
    __pyx_t_17 = __pyx_v_n_hits;
    for (__pyx_t_18 = 0; __pyx_t_18 < __pyx_t_17; __pyx_t_18+=1) {
      __pyx_v_hit_no = __pyx_t_18;

      /* "dipy/tracking/distances.pyx":343
 *         hit_arr = np.empty((n_hits,5), dtype=f32_dt)
 *         for hit_no in range(n_hits):
 *             hit_arr[hit_no] = hits[hit_no]             # <<<<<<<<<<<<<<
 *         Hit.append(hit_arr)
 *         #Div.append(divs[1:])
 */
      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_hits, __pyx_v_hit_no, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      if (__Pyx_SetItemInt(((PyObject *)__pyx_v_hit_arr), __pyx_v_hit_no, __pyx_t_1, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 343; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    }

    /* "dipy/tracking/distances.pyx":344
 *         for hit_no in range(n_hits):
 *             hit_arr[hit_no] = hits[hit_no]
 *         Hit.append(hit_arr)             # <<<<<<<<<<<<<<
 *         #Div.append(divs[1:])
 *     return Hit[1:]
 */
    __pyx_t_1 = __Pyx_PyObject_Append(__pyx_v_Hit, ((PyObject *)__pyx_v_hit_arr)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  }

  /* "dipy/tracking/distances.pyx":346
 *         Hit.append(hit_arr)
 *         #Div.append(divs[1:])
 *     return Hit[1:]             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = __Pyx_PyObject_GetSlice(__pyx_v_Hit, 1, 0, NULL, NULL, &__pyx_k_slice_8, 1, 0, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_10);
  __Pyx_XDECREF(__pyx_t_11);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_hit_arr.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_ref32.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track_lengths.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.cut_plane", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_hit_arr.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_one_hit.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_ref32.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track_lengths.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_ref32);
  __Pyx_XDECREF((PyObject *)__pyx_v_track);
  __Pyx_XDECREF(__pyx_v_hits);
  __Pyx_XDECREF((PyObject *)__pyx_v_one_hit);
  __Pyx_XDECREF((PyObject *)__pyx_v_hit_arr);
  __Pyx_XDECREF(__pyx_v_Hit);
  __Pyx_XDECREF((PyObject *)__pyx_v_track_lengths);
  __Pyx_XDECREF(__pyx_v_tracks32);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_17most_similar_track_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_16most_similar_track_mam[] = " Find the most similar track in a bundle\n    using distances calculated from Zhang et. al 2008.\n\n    Parameters\n    ----------\n    tracks : sequence\n       of tracks as arrays, shape (N1,3) .. (Nm,3)\n    metric : str\n       'avg', 'min', 'max'\n\n    Returns\n    -------\n    si : int\n       index of the most similar track in tracks. This can be used as a\n       reference track for a bundle.\n    s : array, shape (len(tracks),)\n        similarities between tracks[si] and the rest of the tracks in\n        the bundle\n\n    Notes\n    -----\n    A vague description of this function is given below:\n\n    for (i,j) in tracks_combinations_of_2:\n\n        calculate the mean_closest_distance from i to j  (mcd_i)\n        calculate the mean_closest_distance from j to i  (mcd_j)\n\n        if 'avg':\n            s holds the average similarities\n        if 'min':\n            s holds the minimum similarities\n        if 'max':\n            s holds the maximum similarities\n\n    si holds the index of the track with min {avg,min,max} average metric\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_17most_similar_track_mam = {__Pyx_NAMESTR("most_similar_track_mam"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_17most_similar_track_mam, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_16most_similar_track_mam)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_17most_similar_track_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_metric = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("most_similar_track_mam (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__metric,0};
    PyObject* values[2] = {0,0};
    values[1] = ((PyObject *)__pyx_n_s__avg);
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__metric);
          if (value) { values[1] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "most_similar_track_mam") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_tracks = values[0];
    __pyx_v_metric = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("most_similar_track_mam", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.most_similar_track_mam", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_16most_similar_track_mam(__pyx_self, __pyx_v_tracks, __pyx_v_metric);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":352
 * 
 * 
 * def most_similar_track_mam(tracks,metric='avg'):             # <<<<<<<<<<<<<<
 *     ''' Find the most similar track in a bundle
 *     using distances calculated from Zhang et. al 2008.
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_16most_similar_track_mam(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_metric) {
  size_t __pyx_v_i;
  size_t __pyx_v_j;
  size_t __pyx_v_lent;
  int __pyx_v_metric_type;
  size_t __pyx_v_longest_track_len;
  size_t __pyx_v_track_len;
  PyArrayObject *__pyx_v_tracks32 = 0;
  PyArrayObject *__pyx_v_track2others = 0;
  __pyx_t_5numpy_double_t *__pyx_v_sum_track2others;
  PyArrayObject *__pyx_v_distances_buffer = 0;
  __pyx_t_5numpy_float32_t *__pyx_v_t1_ptr;
  __pyx_t_5numpy_float32_t *__pyx_v_t2_ptr;
  __pyx_t_5numpy_float32_t *__pyx_v_min_buffer;
  __pyx_t_5numpy_float32_t __pyx_v_distance;
  PyArrayObject *__pyx_v_t1 = 0;
  PyArrayObject *__pyx_v_t2 = 0;
  size_t __pyx_v_t1_len;
  size_t __pyx_v_t2_len;
  double __pyx_v_mn;
  size_t __pyx_v_si;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_distances_buffer;
  __Pyx_Buffer __pyx_pybuffer_distances_buffer;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_t1;
  __Pyx_Buffer __pyx_pybuffer_t1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_t2;
  __Pyx_Buffer __pyx_pybuffer_t2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track2others;
  __Pyx_Buffer __pyx_pybuffer_track2others;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_tracks32;
  __Pyx_Buffer __pyx_pybuffer_tracks32;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  Py_ssize_t __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyObject *__pyx_t_6 = NULL;
  PyArrayObject *__pyx_t_7 = NULL;
  int __pyx_t_8;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  size_t __pyx_t_12;
  size_t __pyx_t_13;
  size_t __pyx_t_14;
  PyObject **__pyx_t_15;
  size_t __pyx_t_16;
  size_t __pyx_t_17;
  PyObject *__pyx_t_18 = NULL;
  PyArrayObject *__pyx_t_19 = NULL;
  PyArrayObject *__pyx_t_20 = NULL;
  PyArrayObject *__pyx_t_21 = NULL;
  size_t __pyx_t_22;
  size_t __pyx_t_23;
  size_t __pyx_t_24;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("most_similar_track_mam", 0);
  __pyx_pybuffer_tracks32.pybuffer.buf = NULL;
  __pyx_pybuffer_tracks32.refcount = 0;
  __pyx_pybuffernd_tracks32.data = NULL;
  __pyx_pybuffernd_tracks32.rcbuffer = &__pyx_pybuffer_tracks32;
  __pyx_pybuffer_track2others.pybuffer.buf = NULL;
  __pyx_pybuffer_track2others.refcount = 0;
  __pyx_pybuffernd_track2others.data = NULL;
  __pyx_pybuffernd_track2others.rcbuffer = &__pyx_pybuffer_track2others;
  __pyx_pybuffer_distances_buffer.pybuffer.buf = NULL;
  __pyx_pybuffer_distances_buffer.refcount = 0;
  __pyx_pybuffernd_distances_buffer.data = NULL;
  __pyx_pybuffernd_distances_buffer.rcbuffer = &__pyx_pybuffer_distances_buffer;
  __pyx_pybuffer_t1.pybuffer.buf = NULL;
  __pyx_pybuffer_t1.refcount = 0;
  __pyx_pybuffernd_t1.data = NULL;
  __pyx_pybuffernd_t1.rcbuffer = &__pyx_pybuffer_t1;
  __pyx_pybuffer_t2.pybuffer.buf = NULL;
  __pyx_pybuffer_t2.refcount = 0;
  __pyx_pybuffernd_t2.data = NULL;
  __pyx_pybuffernd_t2.rcbuffer = &__pyx_pybuffer_t2;

  /* "dipy/tracking/distances.pyx":393
 *         size_t i, j, lent
 *         int metric_type
 *     if metric=='avg':             # <<<<<<<<<<<<<<
 *         metric_type = 0
 *     elif metric == 'min':
 */
  __pyx_t_1 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__avg), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":394
 *         int metric_type
 *     if metric=='avg':
 *         metric_type = 0             # <<<<<<<<<<<<<<
 *     elif metric == 'min':
 *         metric_type = 1
 */
    __pyx_v_metric_type = 0;
    goto __pyx_L3;
  }

  /* "dipy/tracking/distances.pyx":395
 *     if metric=='avg':
 *         metric_type = 0
 *     elif metric == 'min':             # <<<<<<<<<<<<<<
 *         metric_type = 1
 *     elif metric == 'max':
 */
  __pyx_t_1 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__min), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":396
 *         metric_type = 0
 *     elif metric == 'min':
 *         metric_type = 1             # <<<<<<<<<<<<<<
 *     elif metric == 'max':
 *         metric_type = 2
 */
    __pyx_v_metric_type = 1;
    goto __pyx_L3;
  }

  /* "dipy/tracking/distances.pyx":397
 *     elif metric == 'min':
 *         metric_type = 1
 *     elif metric == 'max':             # <<<<<<<<<<<<<<
 *         metric_type = 2
 *     else:
 */
  __pyx_t_1 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__max), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 397; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":398
 *         metric_type = 1
 *     elif metric == 'max':
 *         metric_type = 2             # <<<<<<<<<<<<<<
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')
 */
    __pyx_v_metric_type = 2;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":400
 *         metric_type = 2
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')             # <<<<<<<<<<<<<<
 *     # preprocess tracks
 *     cdef:
 */
    __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_10), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_L3:;

  /* "dipy/tracking/distances.pyx":403
 *     # preprocess tracks
 *     cdef:
 *         size_t longest_track_len = 0, track_len             # <<<<<<<<<<<<<<
 *         cnp.ndarray[object, ndim=1] tracks32
 *     lent = len(tracks)
 */
  __pyx_v_longest_track_len = 0;

  /* "dipy/tracking/distances.pyx":405
 *         size_t longest_track_len = 0, track_len
 *         cnp.ndarray[object, ndim=1] tracks32
 *     lent = len(tracks)             # <<<<<<<<<<<<<<
 *     tracks32 = np.zeros((lent,), dtype=object)
 *     # process tracks to predictable memory layout, find longest track
 */
  __pyx_t_3 = PyObject_Length(__pyx_v_tracks); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 405; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_3;

  /* "dipy/tracking/distances.pyx":406
 *         cnp.ndarray[object, ndim=1] tracks32
 *     lent = len(tracks)
 *     tracks32 = np.zeros((lent,), dtype=object)             # <<<<<<<<<<<<<<
 *     # process tracks to predictable memory layout, find longest track
 *     for i in range(lent):
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_PyInt_FromSize_t(__pyx_v_lent); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_5));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_5));
  __pyx_t_5 = 0;
  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_5));
  if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), __pyx_builtin_object) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  if (!(likely(((__pyx_t_6) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_6, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_6);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracks32.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracks32.rcbuffer->pybuffer, (PyObject*)__pyx_t_7, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracks32.rcbuffer->pybuffer, (PyObject*)__pyx_v_tracks32, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
      }
    }
    __pyx_pybuffernd_tracks32.diminfo[0].strides = __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_tracks32.diminfo[0].shape = __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_7 = 0;
  __pyx_v_tracks32 = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;

  /* "dipy/tracking/distances.pyx":408
 *     tracks32 = np.zeros((lent,), dtype=object)
 *     # process tracks to predictable memory layout, find longest track
 *     for i in range(lent):             # <<<<<<<<<<<<<<
 *         tracks32[i] = np.ascontiguousarray(tracks[i], dtype=f32_dt)
 *         track_len = tracks32[i].shape[0]
 */
  __pyx_t_12 = __pyx_v_lent;
  for (__pyx_t_13 = 0; __pyx_t_13 < __pyx_t_12; __pyx_t_13+=1) {
    __pyx_v_i = __pyx_t_13;

    /* "dipy/tracking/distances.pyx":409
 *     # process tracks to predictable memory layout, find longest track
 *     for i in range(lent):
 *         tracks32[i] = np.ascontiguousarray(tracks[i], dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         track_len = tracks32[i].shape[0]
 *         if track_len > longest_track_len:
 */
    __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_tracks, __pyx_v_i, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 1); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_6);
    __Pyx_GIVEREF(__pyx_t_6);
    __pyx_t_6 = 0;
    __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_6));
    if (PyDict_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_4 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
    __pyx_t_14 = __pyx_v_i;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_14 >= (size_t)__pyx_pybuffernd_tracks32.diminfo[0].shape)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 409; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_15 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.buf, __pyx_t_14, __pyx_pybuffernd_tracks32.diminfo[0].strides);
    __Pyx_GOTREF(*__pyx_t_15);
    __Pyx_INCREF(__pyx_t_4); __Pyx_DECREF(*__pyx_t_15);
    *__pyx_t_15 = __pyx_t_4;
    __Pyx_GIVEREF(*__pyx_t_15);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":410
 *     for i in range(lent):
 *         tracks32[i] = np.ascontiguousarray(tracks[i], dtype=f32_dt)
 *         track_len = tracks32[i].shape[0]             # <<<<<<<<<<<<<<
 *         if track_len > longest_track_len:
 *             longest_track_len = track_len
 */
    __pyx_t_16 = __pyx_v_i;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_16 >= (size_t)__pyx_pybuffernd_tracks32.diminfo[0].shape)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_4 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.buf, __pyx_t_16, __pyx_pybuffernd_tracks32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_4);
    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__shape); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = __Pyx_GetItemInt(__pyx_t_6, 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_17 = __Pyx_PyInt_AsSize_t(__pyx_t_4); if (unlikely((__pyx_t_17 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 410; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_v_track_len = __pyx_t_17;

    /* "dipy/tracking/distances.pyx":411
 *         tracks32[i] = np.ascontiguousarray(tracks[i], dtype=f32_dt)
 *         track_len = tracks32[i].shape[0]
 *         if track_len > longest_track_len:             # <<<<<<<<<<<<<<
 *             longest_track_len = track_len
 *     # buffer for distances of found track to other tracks
 */
    __pyx_t_2 = ((__pyx_v_track_len > __pyx_v_longest_track_len) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":412
 *         track_len = tracks32[i].shape[0]
 *         if track_len > longest_track_len:
 *             longest_track_len = track_len             # <<<<<<<<<<<<<<
 *     # buffer for distances of found track to other tracks
 *     cdef:
 */
      __pyx_v_longest_track_len = __pyx_v_track_len;
      goto __pyx_L6;
    }
    __pyx_L6:;
  }

  /* "dipy/tracking/distances.pyx":416
 *     cdef:
 *         cnp.ndarray[cnp.double_t, ndim=1] track2others
 *     track2others = np.zeros((lent,), dtype=np.double)             # <<<<<<<<<<<<<<
 *     # use this buffer also for working space containing summed distances
 *     # of candidate track to all other tracks
 */
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = __Pyx_PyInt_FromSize_t(__pyx_v_lent); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_4 = 0;
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_t_1));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_1));
  __pyx_t_1 = 0;
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_18 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__double); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), __pyx_t_18) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
  __pyx_t_18 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_t_4), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_18) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_18, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_19 = ((PyArrayObject *)__pyx_t_18);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2others.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track2others.rcbuffer->pybuffer, (PyObject*)__pyx_t_19, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track2others.rcbuffer->pybuffer, (PyObject*)__pyx_v_track2others, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_track2others.diminfo[0].strides = __pyx_pybuffernd_track2others.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track2others.diminfo[0].shape = __pyx_pybuffernd_track2others.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 416; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_19 = 0;
  __pyx_v_track2others = ((PyArrayObject *)__pyx_t_18);
  __pyx_t_18 = 0;

  /* "dipy/tracking/distances.pyx":419
 *     # use this buffer also for working space containing summed distances
 *     # of candidate track to all other tracks
 *     cdef cnp.double_t *sum_track2others = <cnp.double_t *>track2others.data             # <<<<<<<<<<<<<<
 *     # preallocate buffer array for track distance calculations
 *     cdef:
 */
  __pyx_v_sum_track2others = ((__pyx_t_5numpy_double_t *)__pyx_v_track2others->data);

  /* "dipy/tracking/distances.pyx":424
 *         cnp.ndarray [cnp.float32_t, ndim=1] distances_buffer
 *         cnp.float32_t *t1_ptr, *t2_ptr, *min_buffer, distance
 *     distances_buffer = np.zeros((longest_track_len*2,), dtype=np.float32)             # <<<<<<<<<<<<<<
 *     min_buffer = <cnp.float32_t *> distances_buffer.data
 *     # cycle over tracks
 */
  __pyx_t_18 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_18, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
  __pyx_t_18 = __Pyx_PyInt_FromSize_t((__pyx_v_longest_track_len * 2)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_18);
  __Pyx_GIVEREF(__pyx_t_18);
  __pyx_t_18 = 0;
  __pyx_t_18 = PyTuple_New(1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  PyTuple_SET_ITEM(__pyx_t_18, 0, ((PyObject *)__pyx_t_4));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_4));
  __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s__float32); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_18), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_18)); __pyx_t_18 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_20 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_t_20, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_v_distances_buffer, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
      }
    }
    __pyx_pybuffernd_distances_buffer.diminfo[0].strides = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_distances_buffer.diminfo[0].shape = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 424; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_20 = 0;
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "dipy/tracking/distances.pyx":425
 *         cnp.float32_t *t1_ptr, *t2_ptr, *min_buffer, distance
 *     distances_buffer = np.zeros((longest_track_len*2,), dtype=np.float32)
 *     min_buffer = <cnp.float32_t *> distances_buffer.data             # <<<<<<<<<<<<<<
 *     # cycle over tracks
 *     cdef:
 */
  __pyx_v_min_buffer = ((__pyx_t_5numpy_float32_t *)__pyx_v_distances_buffer->data);

  /* "dipy/tracking/distances.pyx":430
 *         cnp.ndarray [cnp.float32_t, ndim=2] t1, t2
 *         size_t t1_len, t2_len
 *     for i from 0 <= i < lent-1:             # <<<<<<<<<<<<<<
 *         t1 = tracks32[i]
 *         t1_len = t1.shape[0]
 */
  __pyx_t_12 = (__pyx_v_lent - 1);
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_12; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":431
 *         size_t t1_len, t2_len
 *     for i from 0 <= i < lent-1:
 *         t1 = tracks32[i]             # <<<<<<<<<<<<<<
 *         t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data
 */
    __pyx_t_13 = __pyx_v_i;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_13 >= (size_t)__pyx_pybuffernd_tracks32.diminfo[0].shape)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_5 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.buf, __pyx_t_13, __pyx_pybuffernd_tracks32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_5);
    if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_t_21, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_8 < 0)) {
        PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_v_t1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
        }
      }
      __pyx_pybuffernd_t1.diminfo[0].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t1.diminfo[0].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t1.diminfo[1].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t1.diminfo[1].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 431; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_21 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_t1));
    __pyx_v_t1 = ((PyArrayObject *)__pyx_t_5);
    __pyx_t_5 = 0;

    /* "dipy/tracking/distances.pyx":432
 *     for i from 0 <= i < lent-1:
 *         t1 = tracks32[i]
 *         t1_len = t1.shape[0]             # <<<<<<<<<<<<<<
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from i+1 <= j < lent:
 */
    __pyx_v_t1_len = (__pyx_v_t1->dimensions[0]);

    /* "dipy/tracking/distances.pyx":433
 *         t1 = tracks32[i]
 *         t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data             # <<<<<<<<<<<<<<
 *         for j from i+1 <= j < lent:
 *             t2 = tracks32[j]
 */
    __pyx_v_t1_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t1->data);

    /* "dipy/tracking/distances.pyx":434
 *         t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from i+1 <= j < lent:             # <<<<<<<<<<<<<<
 *             t2 = tracks32[j]
 *             t2_len = t2.shape[0]
 */
    __pyx_t_17 = __pyx_v_lent;
    for (__pyx_v_j = (__pyx_v_i + 1); __pyx_v_j < __pyx_t_17; __pyx_v_j++) {

      /* "dipy/tracking/distances.pyx":435
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from i+1 <= j < lent:
 *             t2 = tracks32[j]             # <<<<<<<<<<<<<<
 *             t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data
 */
      __pyx_t_22 = __pyx_v_j;
      __pyx_t_8 = -1;
      if (unlikely(__pyx_t_22 >= (size_t)__pyx_pybuffernd_tracks32.diminfo[0].shape)) __pyx_t_8 = 0;
      if (unlikely(__pyx_t_8 != -1)) {
        __Pyx_RaiseBufferIndexError(__pyx_t_8);
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_5 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.buf, __pyx_t_22, __pyx_pybuffernd_tracks32.diminfo[0].strides);
      __Pyx_INCREF((PyObject*)__pyx_t_5);
      if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
        __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_t_21, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_8 < 0)) {
          PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_v_t2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
          }
        }
        __pyx_pybuffernd_t2.diminfo[0].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t2.diminfo[0].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t2.diminfo[1].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t2.diminfo[1].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 435; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_21 = 0;
      __Pyx_XDECREF(((PyObject *)__pyx_v_t2));
      __pyx_v_t2 = ((PyArrayObject *)__pyx_t_5);
      __pyx_t_5 = 0;

      /* "dipy/tracking/distances.pyx":436
 *         for j from i+1 <= j < lent:
 *             t2 = tracks32[j]
 *             t2_len = t2.shape[0]             # <<<<<<<<<<<<<<
 *             t2_ptr = <cnp.float32_t *>t2.data
 *             distance = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 */
      __pyx_v_t2_len = (__pyx_v_t2->dimensions[0]);

      /* "dipy/tracking/distances.pyx":437
 *             t2 = tracks32[j]
 *             t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data             # <<<<<<<<<<<<<<
 *             distance = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *             # get metric
 */
      __pyx_v_t2_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t2->data);

      /* "dipy/tracking/distances.pyx":438
 *             t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data
 *             distance = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)             # <<<<<<<<<<<<<<
 *             # get metric
 *             sum_track2others[i]+=distance
 */
      __pyx_v_distance = __pyx_f_4dipy_8tracking_9distances_czhang(__pyx_v_t1_len, __pyx_v_t1_ptr, __pyx_v_t2_len, __pyx_v_t2_ptr, __pyx_v_min_buffer, __pyx_v_metric_type);

      /* "dipy/tracking/distances.pyx":440
 *             distance = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *             # get metric
 *             sum_track2others[i]+=distance             # <<<<<<<<<<<<<<
 *             sum_track2others[j]+=distance
 *     # find track with smallest summed metric with other tracks
 */
      __pyx_t_23 = __pyx_v_i;
      (__pyx_v_sum_track2others[__pyx_t_23]) = ((__pyx_v_sum_track2others[__pyx_t_23]) + __pyx_v_distance);

      /* "dipy/tracking/distances.pyx":441
 *             # get metric
 *             sum_track2others[i]+=distance
 *             sum_track2others[j]+=distance             # <<<<<<<<<<<<<<
 *     # find track with smallest summed metric with other tracks
 *     cdef double mn = sum_track2others[0]
 */
      __pyx_t_23 = __pyx_v_j;
      (__pyx_v_sum_track2others[__pyx_t_23]) = ((__pyx_v_sum_track2others[__pyx_t_23]) + __pyx_v_distance);
    }
  }

  /* "dipy/tracking/distances.pyx":443
 *             sum_track2others[j]+=distance
 *     # find track with smallest summed metric with other tracks
 *     cdef double mn = sum_track2others[0]             # <<<<<<<<<<<<<<
 *     cdef size_t si = 0
 *     for i in range(lent):
 */
  __pyx_v_mn = (__pyx_v_sum_track2others[0]);

  /* "dipy/tracking/distances.pyx":444
 *     # find track with smallest summed metric with other tracks
 *     cdef double mn = sum_track2others[0]
 *     cdef size_t si = 0             # <<<<<<<<<<<<<<
 *     for i in range(lent):
 *         if sum_track2others[i] < mn:
 */
  __pyx_v_si = 0;

  /* "dipy/tracking/distances.pyx":445
 *     cdef double mn = sum_track2others[0]
 *     cdef size_t si = 0
 *     for i in range(lent):             # <<<<<<<<<<<<<<
 *         if sum_track2others[i] < mn:
 *             si = i
 */
  __pyx_t_12 = __pyx_v_lent;
  for (__pyx_t_17 = 0; __pyx_t_17 < __pyx_t_12; __pyx_t_17+=1) {
    __pyx_v_i = __pyx_t_17;

    /* "dipy/tracking/distances.pyx":446
 *     cdef size_t si = 0
 *     for i in range(lent):
 *         if sum_track2others[i] < mn:             # <<<<<<<<<<<<<<
 *             si = i
 *             mn = sum_track2others[i]
 */
    __pyx_t_2 = (((__pyx_v_sum_track2others[__pyx_v_i]) < __pyx_v_mn) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":447
 *     for i in range(lent):
 *         if sum_track2others[i] < mn:
 *             si = i             # <<<<<<<<<<<<<<
 *             mn = sum_track2others[i]
 *     # recalculate distance of this track from the others
 */
      __pyx_v_si = __pyx_v_i;

      /* "dipy/tracking/distances.pyx":448
 *         if sum_track2others[i] < mn:
 *             si = i
 *             mn = sum_track2others[i]             # <<<<<<<<<<<<<<
 *     # recalculate distance of this track from the others
 *     t1 = tracks32[si]
 */
      __pyx_v_mn = (__pyx_v_sum_track2others[__pyx_v_i]);
      goto __pyx_L13;
    }
    __pyx_L13:;
  }

  /* "dipy/tracking/distances.pyx":450
 *             mn = sum_track2others[i]
 *     # recalculate distance of this track from the others
 *     t1 = tracks32[si]             # <<<<<<<<<<<<<<
 *     t1_len = t1.shape[0]
 *     t1_ptr = <cnp.float32_t *>t1.data
 */
  __pyx_t_12 = __pyx_v_si;
  __pyx_t_8 = -1;
  if (unlikely(__pyx_t_12 >= (size_t)__pyx_pybuffernd_tracks32.diminfo[0].shape)) __pyx_t_8 = 0;
  if (unlikely(__pyx_t_8 != -1)) {
    __Pyx_RaiseBufferIndexError(__pyx_t_8);
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.buf, __pyx_t_12, __pyx_pybuffernd_tracks32.diminfo[0].strides);
  __Pyx_INCREF((PyObject*)__pyx_t_5);
  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_t_21, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_v_t1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_t1.diminfo[0].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t1.diminfo[0].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t1.diminfo[1].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t1.diminfo[1].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 450; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_21 = 0;
  __Pyx_XDECREF(((PyObject *)__pyx_v_t1));
  __pyx_v_t1 = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "dipy/tracking/distances.pyx":451
 *     # recalculate distance of this track from the others
 *     t1 = tracks32[si]
 *     t1_len = t1.shape[0]             # <<<<<<<<<<<<<<
 *     t1_ptr = <cnp.float32_t *>t1.data
 *     for j from 0 <= j < lent:
 */
  __pyx_v_t1_len = (__pyx_v_t1->dimensions[0]);

  /* "dipy/tracking/distances.pyx":452
 *     t1 = tracks32[si]
 *     t1_len = t1.shape[0]
 *     t1_ptr = <cnp.float32_t *>t1.data             # <<<<<<<<<<<<<<
 *     for j from 0 <= j < lent:
 *         t2 = tracks32[j]
 */
  __pyx_v_t1_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t1->data);

  /* "dipy/tracking/distances.pyx":453
 *     t1_len = t1.shape[0]
 *     t1_ptr = <cnp.float32_t *>t1.data
 *     for j from 0 <= j < lent:             # <<<<<<<<<<<<<<
 *         t2 = tracks32[j]
 *         t2_len = t2.shape[0]
 */
  __pyx_t_17 = __pyx_v_lent;
  for (__pyx_v_j = 0; __pyx_v_j < __pyx_t_17; __pyx_v_j++) {

    /* "dipy/tracking/distances.pyx":454
 *     t1_ptr = <cnp.float32_t *>t1.data
 *     for j from 0 <= j < lent:
 *         t2 = tracks32[j]             # <<<<<<<<<<<<<<
 *         t2_len = t2.shape[0]
 *         t2_ptr = <cnp.float32_t *>t2.data
 */
    __pyx_t_23 = __pyx_v_j;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_23 >= (size_t)__pyx_pybuffernd_tracks32.diminfo[0].shape)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_5 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracks32.rcbuffer->pybuffer.buf, __pyx_t_23, __pyx_pybuffernd_tracks32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_5);
    if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_t_21, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_8 < 0)) {
        PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_v_t2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
        }
      }
      __pyx_pybuffernd_t2.diminfo[0].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t2.diminfo[0].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t2.diminfo[1].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t2.diminfo[1].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 454; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_21 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_t2));
    __pyx_v_t2 = ((PyArrayObject *)__pyx_t_5);
    __pyx_t_5 = 0;

    /* "dipy/tracking/distances.pyx":455
 *     for j from 0 <= j < lent:
 *         t2 = tracks32[j]
 *         t2_len = t2.shape[0]             # <<<<<<<<<<<<<<
 *         t2_ptr = <cnp.float32_t *>t2.data
 *         track2others[j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 */
    __pyx_v_t2_len = (__pyx_v_t2->dimensions[0]);

    /* "dipy/tracking/distances.pyx":456
 *         t2 = tracks32[j]
 *         t2_len = t2.shape[0]
 *         t2_ptr = <cnp.float32_t *>t2.data             # <<<<<<<<<<<<<<
 *         track2others[j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *     return si, track2others
 */
    __pyx_v_t2_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t2->data);

    /* "dipy/tracking/distances.pyx":457
 *         t2_len = t2.shape[0]
 *         t2_ptr = <cnp.float32_t *>t2.data
 *         track2others[j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)             # <<<<<<<<<<<<<<
 *     return si, track2others
 * 
 */
    __pyx_t_24 = __pyx_v_j;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_24 >= (size_t)__pyx_pybuffernd_track2others.diminfo[0].shape)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 457; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    *__Pyx_BufPtrStrided1d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_track2others.rcbuffer->pybuffer.buf, __pyx_t_24, __pyx_pybuffernd_track2others.diminfo[0].strides) = __pyx_f_4dipy_8tracking_9distances_czhang(__pyx_v_t1_len, __pyx_v_t1_ptr, __pyx_v_t2_len, __pyx_v_t2_ptr, __pyx_v_min_buffer, __pyx_v_metric_type);
  }

  /* "dipy/tracking/distances.pyx":458
 *         t2_ptr = <cnp.float32_t *>t2.data
 *         track2others[j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *     return si, track2others             # <<<<<<<<<<<<<<
 * 
 * @cython.boundscheck(False)
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_5 = __Pyx_PyInt_FromSize_t(__pyx_v_si); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 458; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __Pyx_INCREF(((PyObject *)__pyx_v_track2others));
  PyTuple_SET_ITEM(__pyx_t_4, 1, ((PyObject *)__pyx_v_track2others));
  __Pyx_GIVEREF(((PyObject *)__pyx_v_track2others));
  __pyx_t_5 = 0;
  __pyx_r = ((PyObject *)__pyx_t_4);
  __pyx_t_4 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __Pyx_XDECREF(__pyx_t_18);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2others.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracks32.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.most_similar_track_mam", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2others.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracks32.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_tracks32);
  __Pyx_XDECREF((PyObject *)__pyx_v_track2others);
  __Pyx_XDECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_XDECREF((PyObject *)__pyx_v_t1);
  __Pyx_XDECREF((PyObject *)__pyx_v_t2);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_19bundles_distances_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_18bundles_distances_mam[] = " Calculate distances between list of tracks A and list of tracks B\n\n    Parameters\n    ----------\n    tracksA : sequence\n       of tracks as arrays, shape (N1,3) .. (Nm,3)\n    tracksB : sequence\n       of tracks as arrays, shape (N1,3) .. (Nm,3)\n    metric : str\n       'avg', 'min', 'max'\n\n    Returns\n    -------\n    DM : array, shape (len(tracksA), len(tracksB))\n        distances between tracksA and tracksB according to metric\n\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_19bundles_distances_mam = {__Pyx_NAMESTR("bundles_distances_mam"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_19bundles_distances_mam, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_18bundles_distances_mam)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_19bundles_distances_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracksA = 0;
  PyObject *__pyx_v_tracksB = 0;
  PyObject *__pyx_v_metric = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("bundles_distances_mam (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracksA,&__pyx_n_s__tracksB,&__pyx_n_s__metric,0};
    PyObject* values[3] = {0,0,0};
    values[2] = ((PyObject *)__pyx_n_s__avg);
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracksA)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracksB)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("bundles_distances_mam", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__metric);
          if (value) { values[2] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "bundles_distances_mam") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_tracksA = values[0];
    __pyx_v_tracksB = values[1];
    __pyx_v_metric = values[2];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("bundles_distances_mam", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.bundles_distances_mam", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_18bundles_distances_mam(__pyx_self, __pyx_v_tracksA, __pyx_v_tracksB, __pyx_v_metric);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":462
 * @cython.boundscheck(False)
 * @cython.wraparound(False)
 * def bundles_distances_mam(tracksA, tracksB, metric='avg'):             # <<<<<<<<<<<<<<
 *     ''' Calculate distances between list of tracks A and list of tracks B
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_18bundles_distances_mam(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracksA, PyObject *__pyx_v_tracksB, PyObject *__pyx_v_metric) {
  size_t __pyx_v_i;
  size_t __pyx_v_j;
  size_t __pyx_v_lentA;
  size_t __pyx_v_lentB;
  int __pyx_v_metric_type;
  CYTHON_UNUSED size_t __pyx_v_longest_track_len;
  size_t __pyx_v_track_len;
  size_t __pyx_v_longest_track_lenA;
  size_t __pyx_v_longest_track_lenB;
  PyArrayObject *__pyx_v_tracksA32 = 0;
  PyArrayObject *__pyx_v_tracksB32 = 0;
  PyArrayObject *__pyx_v_DM = 0;
  PyArrayObject *__pyx_v_distances_buffer = 0;
  __pyx_t_5numpy_float32_t *__pyx_v_t1_ptr;
  __pyx_t_5numpy_float32_t *__pyx_v_t2_ptr;
  __pyx_t_5numpy_float32_t *__pyx_v_min_buffer;
  PyArrayObject *__pyx_v_t1 = 0;
  PyArrayObject *__pyx_v_t2 = 0;
  size_t __pyx_v_t1_len;
  size_t __pyx_v_t2_len;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_DM;
  __Pyx_Buffer __pyx_pybuffer_DM;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_distances_buffer;
  __Pyx_Buffer __pyx_pybuffer_distances_buffer;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_t1;
  __Pyx_Buffer __pyx_pybuffer_t1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_t2;
  __Pyx_Buffer __pyx_pybuffer_t2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_tracksA32;
  __Pyx_Buffer __pyx_pybuffer_tracksA32;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_tracksB32;
  __Pyx_Buffer __pyx_pybuffer_tracksB32;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  Py_ssize_t __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyObject *__pyx_t_6 = NULL;
  PyArrayObject *__pyx_t_7 = NULL;
  int __pyx_t_8;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyArrayObject *__pyx_t_12 = NULL;
  PyObject *__pyx_t_13 = NULL;
  PyArrayObject *__pyx_t_14 = NULL;
  size_t __pyx_t_15;
  size_t __pyx_t_16;
  size_t __pyx_t_17;
  PyObject **__pyx_t_18;
  size_t __pyx_t_19;
  size_t __pyx_t_20;
  size_t __pyx_t_21;
  size_t __pyx_t_22;
  PyArrayObject *__pyx_t_23 = NULL;
  PyArrayObject *__pyx_t_24 = NULL;
  size_t __pyx_t_25;
  size_t __pyx_t_26;
  size_t __pyx_t_27;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("bundles_distances_mam", 0);
  __pyx_pybuffer_tracksA32.pybuffer.buf = NULL;
  __pyx_pybuffer_tracksA32.refcount = 0;
  __pyx_pybuffernd_tracksA32.data = NULL;
  __pyx_pybuffernd_tracksA32.rcbuffer = &__pyx_pybuffer_tracksA32;
  __pyx_pybuffer_tracksB32.pybuffer.buf = NULL;
  __pyx_pybuffer_tracksB32.refcount = 0;
  __pyx_pybuffernd_tracksB32.data = NULL;
  __pyx_pybuffernd_tracksB32.rcbuffer = &__pyx_pybuffer_tracksB32;
  __pyx_pybuffer_DM.pybuffer.buf = NULL;
  __pyx_pybuffer_DM.refcount = 0;
  __pyx_pybuffernd_DM.data = NULL;
  __pyx_pybuffernd_DM.rcbuffer = &__pyx_pybuffer_DM;
  __pyx_pybuffer_distances_buffer.pybuffer.buf = NULL;
  __pyx_pybuffer_distances_buffer.refcount = 0;
  __pyx_pybuffernd_distances_buffer.data = NULL;
  __pyx_pybuffernd_distances_buffer.rcbuffer = &__pyx_pybuffer_distances_buffer;
  __pyx_pybuffer_t1.pybuffer.buf = NULL;
  __pyx_pybuffer_t1.refcount = 0;
  __pyx_pybuffernd_t1.data = NULL;
  __pyx_pybuffernd_t1.rcbuffer = &__pyx_pybuffer_t1;
  __pyx_pybuffer_t2.pybuffer.buf = NULL;
  __pyx_pybuffer_t2.refcount = 0;
  __pyx_pybuffernd_t2.data = NULL;
  __pyx_pybuffernd_t2.rcbuffer = &__pyx_pybuffer_t2;

  /* "dipy/tracking/distances.pyx":483
 *         size_t i, j, lentA, lentB
 *         int metric_type
 *     if metric=='avg':             # <<<<<<<<<<<<<<
 *         metric_type = 0
 *     elif metric == 'min':
 */
  __pyx_t_1 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__avg), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 483; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":484
 *         int metric_type
 *     if metric=='avg':
 *         metric_type = 0             # <<<<<<<<<<<<<<
 *     elif metric == 'min':
 *         metric_type = 1
 */
    __pyx_v_metric_type = 0;
    goto __pyx_L3;
  }

  /* "dipy/tracking/distances.pyx":485
 *     if metric=='avg':
 *         metric_type = 0
 *     elif metric == 'min':             # <<<<<<<<<<<<<<
 *         metric_type = 1
 *     elif metric == 'max':
 */
  __pyx_t_1 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__min), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 485; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":486
 *         metric_type = 0
 *     elif metric == 'min':
 *         metric_type = 1             # <<<<<<<<<<<<<<
 *     elif metric == 'max':
 *         metric_type = 2
 */
    __pyx_v_metric_type = 1;
    goto __pyx_L3;
  }

  /* "dipy/tracking/distances.pyx":487
 *     elif metric == 'min':
 *         metric_type = 1
 *     elif metric == 'max':             # <<<<<<<<<<<<<<
 *         metric_type = 2
 *     else:
 */
  __pyx_t_1 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__max), Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":488
 *         metric_type = 1
 *     elif metric == 'max':
 *         metric_type = 2             # <<<<<<<<<<<<<<
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')
 */
    __pyx_v_metric_type = 2;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":490
 *         metric_type = 2
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')             # <<<<<<<<<<<<<<
 *     # preprocess tracks
 *     cdef:
 */
    __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_11), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_Raise(__pyx_t_1, 0, 0, 0);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_L3:;

  /* "dipy/tracking/distances.pyx":493
 *     # preprocess tracks
 *     cdef:
 *         size_t longest_track_len = 0, track_len             # <<<<<<<<<<<<<<
 *         size_t longest_track_lenA = 0, longest_track_lenB = 0
 *         cnp.ndarray[object, ndim=1] tracksA32
 */
  __pyx_v_longest_track_len = 0;

  /* "dipy/tracking/distances.pyx":494
 *     cdef:
 *         size_t longest_track_len = 0, track_len
 *         size_t longest_track_lenA = 0, longest_track_lenB = 0             # <<<<<<<<<<<<<<
 *         cnp.ndarray[object, ndim=1] tracksA32
 *         cnp.ndarray[object, ndim=1] tracksB32
 */
  __pyx_v_longest_track_lenA = 0;
  __pyx_v_longest_track_lenB = 0;

  /* "dipy/tracking/distances.pyx":498
 *         cnp.ndarray[object, ndim=1] tracksB32
 *         cnp.ndarray[cnp.double_t, ndim=2] DM
 *     lentA = len(tracksA)             # <<<<<<<<<<<<<<
 *     lentB = len(tracksB)
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 */
  __pyx_t_3 = PyObject_Length(__pyx_v_tracksA); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lentA = __pyx_t_3;

  /* "dipy/tracking/distances.pyx":499
 *         cnp.ndarray[cnp.double_t, ndim=2] DM
 *     lentA = len(tracksA)
 *     lentB = len(tracksB)             # <<<<<<<<<<<<<<
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 *     tracksB32 = np.zeros((lentB,), dtype=object)
 */
  __pyx_t_3 = PyObject_Length(__pyx_v_tracksB); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lentB = __pyx_t_3;

  /* "dipy/tracking/distances.pyx":500
 *     lentA = len(tracksA)
 *     lentB = len(tracksB)
 *     tracksA32 = np.zeros((lentA,), dtype=object)             # <<<<<<<<<<<<<<
 *     tracksB32 = np.zeros((lentB,), dtype=object)
 *     DM = np.zeros((lentA,lentB), dtype=np.double)
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_PyInt_FromSize_t(__pyx_v_lentA); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_5));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_5));
  __pyx_t_5 = 0;
  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_5));
  if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), __pyx_builtin_object) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  if (!(likely(((__pyx_t_6) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_6, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_6);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer, (PyObject*)__pyx_t_7, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer, (PyObject*)__pyx_v_tracksA32, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
      }
    }
    __pyx_pybuffernd_tracksA32.diminfo[0].strides = __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_tracksA32.diminfo[0].shape = __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 500; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_7 = 0;
  __pyx_v_tracksA32 = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;

  /* "dipy/tracking/distances.pyx":501
 *     lentB = len(tracksB)
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 *     tracksB32 = np.zeros((lentB,), dtype=object)             # <<<<<<<<<<<<<<
 *     DM = np.zeros((lentA,lentB), dtype=np.double)
 *     # process tracks to predictable memory layout, find longest track
 */
  __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __pyx_t_6 = __Pyx_PyInt_FromSize_t(__pyx_v_lentB); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_6);
  __Pyx_GIVEREF(__pyx_t_6);
  __pyx_t_6 = 0;
  __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_t_1));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_1));
  __pyx_t_1 = 0;
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), __pyx_builtin_object) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_6), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer, (PyObject*)__pyx_t_12, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer, (PyObject*)__pyx_v_tracksB32, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_tracksB32.diminfo[0].strides = __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_tracksB32.diminfo[0].shape = __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 501; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __pyx_v_tracksB32 = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":502
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 *     tracksB32 = np.zeros((lentB,), dtype=object)
 *     DM = np.zeros((lentA,lentB), dtype=np.double)             # <<<<<<<<<<<<<<
 *     # process tracks to predictable memory layout, find longest track
 *     for i in range(lentA):
 */
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = __Pyx_PyInt_FromSize_t(__pyx_v_lentA); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_6 = __Pyx_PyInt_FromSize_t(__pyx_v_lentB); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_6);
  __Pyx_GIVEREF(__pyx_t_6);
  __pyx_t_4 = 0;
  __pyx_t_6 = 0;
  __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  PyTuple_SET_ITEM(__pyx_t_6, 0, ((PyObject *)__pyx_t_5));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_5));
  __pyx_t_5 = 0;
  __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_5));
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__double); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), __pyx_t_13) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  __pyx_t_13 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_6), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  if (!(likely(((__pyx_t_13) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_13, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_14 = ((PyArrayObject *)__pyx_t_13);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_DM.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_DM.rcbuffer->pybuffer, (PyObject*)__pyx_t_14, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_DM.rcbuffer->pybuffer, (PyObject*)__pyx_v_DM, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
      }
    }
    __pyx_pybuffernd_DM.diminfo[0].strides = __pyx_pybuffernd_DM.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_DM.diminfo[0].shape = __pyx_pybuffernd_DM.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_DM.diminfo[1].strides = __pyx_pybuffernd_DM.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_DM.diminfo[1].shape = __pyx_pybuffernd_DM.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_14 = 0;
  __pyx_v_DM = ((PyArrayObject *)__pyx_t_13);
  __pyx_t_13 = 0;

  /* "dipy/tracking/distances.pyx":504
 *     DM = np.zeros((lentA,lentB), dtype=np.double)
 *     # process tracks to predictable memory layout, find longest track
 *     for i in range(lentA):             # <<<<<<<<<<<<<<
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)
 *         track_len = tracksA32[i].shape[0]
 */
  __pyx_t_15 = __pyx_v_lentA;
  for (__pyx_t_16 = 0; __pyx_t_16 < __pyx_t_15; __pyx_t_16+=1) {
    __pyx_v_i = __pyx_t_16;

    /* "dipy/tracking/distances.pyx":505
 *     # process tracks to predictable memory layout, find longest track
 *     for i in range(lentA):
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         track_len = tracksA32[i].shape[0]
 *         if track_len > longest_track_lenA:
 */
    __pyx_t_13 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_13, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    __pyx_t_13 = __Pyx_GetItemInt(__pyx_v_tracksA, __pyx_v_i, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 0); if (!__pyx_t_13) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_13);
    __Pyx_GIVEREF(__pyx_t_13);
    __pyx_t_13 = 0;
    __pyx_t_13 = PyDict_New(); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_13));
    if (PyDict_SetItem(__pyx_t_13, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_1 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_6), ((PyObject *)__pyx_t_13)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 505; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_13)); __pyx_t_13 = 0;
    __pyx_t_17 = __pyx_v_i;
    __pyx_t_18 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_tracksA32.diminfo[0].strides);
    __Pyx_GOTREF(*__pyx_t_18);
    __Pyx_INCREF(__pyx_t_1); __Pyx_DECREF(*__pyx_t_18);
    *__pyx_t_18 = __pyx_t_1;
    __Pyx_GIVEREF(*__pyx_t_18);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

    /* "dipy/tracking/distances.pyx":506
 *     for i in range(lentA):
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)
 *         track_len = tracksA32[i].shape[0]             # <<<<<<<<<<<<<<
 *         if track_len > longest_track_lenA:
 *             longest_track_lenA = track_len
 */
    __pyx_t_19 = __pyx_v_i;
    __pyx_t_1 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.buf, __pyx_t_19, __pyx_pybuffernd_tracksA32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_1);
    __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__shape); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_13, 0, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    __pyx_t_20 = __Pyx_PyInt_AsSize_t(__pyx_t_1); if (unlikely((__pyx_t_20 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 506; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_v_track_len = __pyx_t_20;

    /* "dipy/tracking/distances.pyx":507
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)
 *         track_len = tracksA32[i].shape[0]
 *         if track_len > longest_track_lenA:             # <<<<<<<<<<<<<<
 *             longest_track_lenA = track_len
 *     for i in range(lentB):
 */
    __pyx_t_2 = ((__pyx_v_track_len > __pyx_v_longest_track_lenA) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":508
 *         track_len = tracksA32[i].shape[0]
 *         if track_len > longest_track_lenA:
 *             longest_track_lenA = track_len             # <<<<<<<<<<<<<<
 *     for i in range(lentB):
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)
 */
      __pyx_v_longest_track_lenA = __pyx_v_track_len;
      goto __pyx_L6;
    }
    __pyx_L6:;
  }

  /* "dipy/tracking/distances.pyx":509
 *         if track_len > longest_track_lenA:
 *             longest_track_lenA = track_len
 *     for i in range(lentB):             # <<<<<<<<<<<<<<
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)
 *         track_len = tracksB32[i].shape[0]
 */
  __pyx_t_15 = __pyx_v_lentB;
  for (__pyx_t_16 = 0; __pyx_t_16 < __pyx_t_15; __pyx_t_16+=1) {
    __pyx_v_i = __pyx_t_16;

    /* "dipy/tracking/distances.pyx":510
 *             longest_track_lenA = track_len
 *     for i in range(lentB):
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         track_len = tracksB32[i].shape[0]
 *         if track_len > longest_track_lenB:
 */
    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_tracksB, __pyx_v_i, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 0); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_1);
    __Pyx_GIVEREF(__pyx_t_1);
    __pyx_t_1 = 0;
    __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_1));
    if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_5 = PyObject_Call(__pyx_t_13, ((PyObject *)__pyx_t_6), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 510; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
    __pyx_t_20 = __pyx_v_i;
    __pyx_t_18 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.buf, __pyx_t_20, __pyx_pybuffernd_tracksB32.diminfo[0].strides);
    __Pyx_GOTREF(*__pyx_t_18);
    __Pyx_INCREF(__pyx_t_5); __Pyx_DECREF(*__pyx_t_18);
    *__pyx_t_18 = __pyx_t_5;
    __Pyx_GIVEREF(*__pyx_t_18);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

    /* "dipy/tracking/distances.pyx":511
 *     for i in range(lentB):
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)
 *         track_len = tracksB32[i].shape[0]             # <<<<<<<<<<<<<<
 *         if track_len > longest_track_lenB:
 *             longest_track_lenB = track_len
 */
    __pyx_t_21 = __pyx_v_i;
    __pyx_t_5 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.buf, __pyx_t_21, __pyx_pybuffernd_tracksB32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_5);
    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__shape); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = __Pyx_GetItemInt(__pyx_t_1, 0, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_22 = __Pyx_PyInt_AsSize_t(__pyx_t_5); if (unlikely((__pyx_t_22 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_v_track_len = __pyx_t_22;

    /* "dipy/tracking/distances.pyx":512
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)
 *         track_len = tracksB32[i].shape[0]
 *         if track_len > longest_track_lenB:             # <<<<<<<<<<<<<<
 *             longest_track_lenB = track_len
 *     if longest_track_lenB > longest_track_lenA:
 */
    __pyx_t_2 = ((__pyx_v_track_len > __pyx_v_longest_track_lenB) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":513
 *         track_len = tracksB32[i].shape[0]
 *         if track_len > longest_track_lenB:
 *             longest_track_lenB = track_len             # <<<<<<<<<<<<<<
 *     if longest_track_lenB > longest_track_lenA:
 *         longest_track_lenA = longest_track_lenB
 */
      __pyx_v_longest_track_lenB = __pyx_v_track_len;
      goto __pyx_L9;
    }
    __pyx_L9:;
  }

  /* "dipy/tracking/distances.pyx":514
 *         if track_len > longest_track_lenB:
 *             longest_track_lenB = track_len
 *     if longest_track_lenB > longest_track_lenA:             # <<<<<<<<<<<<<<
 *         longest_track_lenA = longest_track_lenB
 *     # preallocate buffer array for track distance calculations
 */
  __pyx_t_2 = ((__pyx_v_longest_track_lenB > __pyx_v_longest_track_lenA) != 0);
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":515
 *             longest_track_lenB = track_len
 *     if longest_track_lenB > longest_track_lenA:
 *         longest_track_lenA = longest_track_lenB             # <<<<<<<<<<<<<<
 *     # preallocate buffer array for track distance calculations
 *     cdef:
 */
    __pyx_v_longest_track_lenA = __pyx_v_longest_track_lenB;
    goto __pyx_L10;
  }
  __pyx_L10:;

  /* "dipy/tracking/distances.pyx":520
 *         cnp.ndarray [cnp.float32_t, ndim=1] distances_buffer
 *         cnp.float32_t *t1_ptr, *t2_ptr, *min_buffer
 *     distances_buffer = np.zeros((longest_track_lenA*2,), dtype=np.float32)             # <<<<<<<<<<<<<<
 *     min_buffer = <cnp.float32_t *> distances_buffer.data
 *     # cycle over tracks
 */
  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = __Pyx_PyInt_FromSize_t((__pyx_v_longest_track_lenA * 2)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __pyx_t_5 = 0;
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_6));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_6));
  __pyx_t_6 = 0;
  __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_6));
  __pyx_t_13 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_13, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  if (PyDict_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__dtype), __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_23 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_t_23, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_v_distances_buffer, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_distances_buffer.diminfo[0].strides = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_distances_buffer.diminfo[0].shape = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 520; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_23 = 0;
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":521
 *         cnp.float32_t *t1_ptr, *t2_ptr, *min_buffer
 *     distances_buffer = np.zeros((longest_track_lenA*2,), dtype=np.float32)
 *     min_buffer = <cnp.float32_t *> distances_buffer.data             # <<<<<<<<<<<<<<
 *     # cycle over tracks
 *     cdef:
 */
  __pyx_v_min_buffer = ((__pyx_t_5numpy_float32_t *)__pyx_v_distances_buffer->data);

  /* "dipy/tracking/distances.pyx":526
 *         cnp.ndarray [cnp.float32_t, ndim=2] t1, t2
 *         size_t t1_len, t2_len
 *     for i from 0 <= i < lentA:             # <<<<<<<<<<<<<<
 *         t1 = tracksA32[i]
 *         t1_len = t1.shape[0]
 */
  __pyx_t_15 = __pyx_v_lentA;
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_15; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":527
 *         size_t t1_len, t2_len
 *     for i from 0 <= i < lentA:
 *         t1 = tracksA32[i]             # <<<<<<<<<<<<<<
 *         t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data
 */
    __pyx_t_16 = __pyx_v_i;
    __pyx_t_4 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.buf, __pyx_t_16, __pyx_pybuffernd_tracksA32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_4);
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_24 = ((PyArrayObject *)__pyx_t_4);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_t_24, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_8 < 0)) {
        PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_v_t1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
        }
      }
      __pyx_pybuffernd_t1.diminfo[0].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t1.diminfo[0].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t1.diminfo[1].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t1.diminfo[1].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 527; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_24 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_t1));
    __pyx_v_t1 = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":528
 *     for i from 0 <= i < lentA:
 *         t1 = tracksA32[i]
 *         t1_len = t1.shape[0]             # <<<<<<<<<<<<<<
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from 0 <= j < lentB:
 */
    __pyx_v_t1_len = (__pyx_v_t1->dimensions[0]);

    /* "dipy/tracking/distances.pyx":529
 *         t1 = tracksA32[i]
 *         t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data             # <<<<<<<<<<<<<<
 *         for j from 0 <= j < lentB:
 *             t2 = tracksB32[j]
 */
    __pyx_v_t1_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t1->data);

    /* "dipy/tracking/distances.pyx":530
 *         t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from 0 <= j < lentB:             # <<<<<<<<<<<<<<
 *             t2 = tracksB32[j]
 *             t2_len = t2.shape[0]
 */
    __pyx_t_22 = __pyx_v_lentB;
    for (__pyx_v_j = 0; __pyx_v_j < __pyx_t_22; __pyx_v_j++) {

      /* "dipy/tracking/distances.pyx":531
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from 0 <= j < lentB:
 *             t2 = tracksB32[j]             # <<<<<<<<<<<<<<
 *             t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data
 */
      __pyx_t_25 = __pyx_v_j;
      __pyx_t_4 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.buf, __pyx_t_25, __pyx_pybuffernd_tracksB32.diminfo[0].strides);
      __Pyx_INCREF((PyObject*)__pyx_t_4);
      if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_24 = ((PyArrayObject *)__pyx_t_4);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
        __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_t_24, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_8 < 0)) {
          PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_v_t2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
          }
        }
        __pyx_pybuffernd_t2.diminfo[0].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t2.diminfo[0].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t2.diminfo[1].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t2.diminfo[1].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_24 = 0;
      __Pyx_XDECREF(((PyObject *)__pyx_v_t2));
      __pyx_v_t2 = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":532
 *         for j from 0 <= j < lentB:
 *             t2 = tracksB32[j]
 *             t2_len = t2.shape[0]             # <<<<<<<<<<<<<<
 *             t2_ptr = <cnp.float32_t *>t2.data
 *             DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 */
      __pyx_v_t2_len = (__pyx_v_t2->dimensions[0]);

      /* "dipy/tracking/distances.pyx":533
 *             t2 = tracksB32[j]
 *             t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data             # <<<<<<<<<<<<<<
 *             DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 * 
 */
      __pyx_v_t2_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t2->data);

      /* "dipy/tracking/distances.pyx":534
 *             t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data
 *             DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)             # <<<<<<<<<<<<<<
 * 
 *     return DM
 */
      __pyx_t_26 = __pyx_v_i;
      __pyx_t_27 = __pyx_v_j;
      *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_DM.rcbuffer->pybuffer.buf, __pyx_t_26, __pyx_pybuffernd_DM.diminfo[0].strides, __pyx_t_27, __pyx_pybuffernd_DM.diminfo[1].strides) = __pyx_f_4dipy_8tracking_9distances_czhang(__pyx_v_t1_len, __pyx_v_t1_ptr, __pyx_v_t2_len, __pyx_v_t2_ptr, __pyx_v_min_buffer, __pyx_v_metric_type);
    }
  }

  /* "dipy/tracking/distances.pyx":536
 *             DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 * 
 *     return DM             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_DM));
  __pyx_r = ((PyObject *)__pyx_v_DM);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __Pyx_XDECREF(__pyx_t_13);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_DM.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.bundles_distances_mam", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_DM.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_tracksA32);
  __Pyx_XDECREF((PyObject *)__pyx_v_tracksB32);
  __Pyx_XDECREF((PyObject *)__pyx_v_DM);
  __Pyx_XDECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_XDECREF((PyObject *)__pyx_v_t1);
  __Pyx_XDECREF((PyObject *)__pyx_v_t2);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_21bundles_distances_mdf(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_20bundles_distances_mdf[] = " Calculate distances between list of tracks A and list of tracks B\n\n    All tracks need to have the same number of points\n\n    Parameters\n    ----------\n    tracksA : sequence\n       of tracks as arrays, [(N,3) .. (N,3)]\n    tracksB : sequence\n       of tracks as arrays, [(N,3) .. (N,3)]\n\n    Returns\n    -------\n    DM : array, shape (len(tracksA), len(tracksB))\n        distances between tracksA and tracksB according to metric\n\n    See Also\n    ---------\n    dipy.metrics.downsample\n\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_21bundles_distances_mdf = {__Pyx_NAMESTR("bundles_distances_mdf"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_21bundles_distances_mdf, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_20bundles_distances_mdf)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_21bundles_distances_mdf(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracksA = 0;
  PyObject *__pyx_v_tracksB = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("bundles_distances_mdf (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracksA,&__pyx_n_s__tracksB,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracksA)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracksB)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("bundles_distances_mdf", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "bundles_distances_mdf") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_tracksA = values[0];
    __pyx_v_tracksB = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("bundles_distances_mdf", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.bundles_distances_mdf", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_20bundles_distances_mdf(__pyx_self, __pyx_v_tracksA, __pyx_v_tracksB);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":541
 * @cython.boundscheck(False)
 * @cython.wraparound(False)
 * def bundles_distances_mdf(tracksA, tracksB):             # <<<<<<<<<<<<<<
 *     ''' Calculate distances between list of tracks A and list of tracks B
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_20bundles_distances_mdf(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracksA, PyObject *__pyx_v_tracksB) {
  size_t __pyx_v_i;
  size_t __pyx_v_j;
  size_t __pyx_v_lentA;
  size_t __pyx_v_lentB;
  CYTHON_UNUSED size_t __pyx_v_longest_track_len;
  PyArrayObject *__pyx_v_tracksA32 = 0;
  PyArrayObject *__pyx_v_tracksB32 = 0;
  PyArrayObject *__pyx_v_DM = 0;
  __pyx_t_5numpy_float32_t *__pyx_v_t1_ptr;
  __pyx_t_5numpy_float32_t *__pyx_v_t2_ptr;
  PyArrayObject *__pyx_v_t1 = 0;
  PyArrayObject *__pyx_v_t2 = 0;
  float __pyx_v_d[2];
  PyObject *__pyx_v_t_len = NULL;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_DM;
  __Pyx_Buffer __pyx_pybuffer_DM;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_t1;
  __Pyx_Buffer __pyx_pybuffer_t1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_t2;
  __Pyx_Buffer __pyx_pybuffer_t2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_tracksA32;
  __Pyx_Buffer __pyx_pybuffer_tracksA32;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_tracksB32;
  __Pyx_Buffer __pyx_pybuffer_tracksB32;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  Py_ssize_t __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyArrayObject *__pyx_t_6 = NULL;
  int __pyx_t_7;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyArrayObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  PyArrayObject *__pyx_t_13 = NULL;
  size_t __pyx_t_14;
  size_t __pyx_t_15;
  size_t __pyx_t_16;
  PyObject **__pyx_t_17;
  size_t __pyx_t_18;
  long __pyx_t_19;
  PyArrayObject *__pyx_t_20 = NULL;
  size_t __pyx_t_21;
  size_t __pyx_t_22;
  long __pyx_t_23;
  int __pyx_t_24;
  size_t __pyx_t_25;
  size_t __pyx_t_26;
  size_t __pyx_t_27;
  size_t __pyx_t_28;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("bundles_distances_mdf", 0);
  __pyx_pybuffer_tracksA32.pybuffer.buf = NULL;
  __pyx_pybuffer_tracksA32.refcount = 0;
  __pyx_pybuffernd_tracksA32.data = NULL;
  __pyx_pybuffernd_tracksA32.rcbuffer = &__pyx_pybuffer_tracksA32;
  __pyx_pybuffer_tracksB32.pybuffer.buf = NULL;
  __pyx_pybuffer_tracksB32.refcount = 0;
  __pyx_pybuffernd_tracksB32.data = NULL;
  __pyx_pybuffernd_tracksB32.rcbuffer = &__pyx_pybuffer_tracksB32;
  __pyx_pybuffer_DM.pybuffer.buf = NULL;
  __pyx_pybuffer_DM.refcount = 0;
  __pyx_pybuffernd_DM.data = NULL;
  __pyx_pybuffernd_DM.rcbuffer = &__pyx_pybuffer_DM;
  __pyx_pybuffer_t1.pybuffer.buf = NULL;
  __pyx_pybuffer_t1.refcount = 0;
  __pyx_pybuffernd_t1.data = NULL;
  __pyx_pybuffernd_t1.rcbuffer = &__pyx_pybuffer_t1;
  __pyx_pybuffer_t2.pybuffer.buf = NULL;
  __pyx_pybuffer_t2.refcount = 0;
  __pyx_pybuffernd_t2.data = NULL;
  __pyx_pybuffernd_t2.rcbuffer = &__pyx_pybuffer_t2;

  /* "dipy/tracking/distances.pyx":567
 *     # preprocess tracks
 *     cdef:
 *         size_t longest_track_len = 0, track_len             # <<<<<<<<<<<<<<
 *         longest_track_lenA, longest_track_lenB
 *         cnp.ndarray[object, ndim=1] tracksA32
 */
  __pyx_v_longest_track_len = 0;

  /* "dipy/tracking/distances.pyx":572
 *         cnp.ndarray[object, ndim=1] tracksB32
 *         cnp.ndarray[cnp.double_t, ndim=2] DM
 *     lentA = len(tracksA)             # <<<<<<<<<<<<<<
 *     lentB = len(tracksB)
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 */
  __pyx_t_1 = PyObject_Length(__pyx_v_tracksA); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 572; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lentA = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":573
 *         cnp.ndarray[cnp.double_t, ndim=2] DM
 *     lentA = len(tracksA)
 *     lentB = len(tracksB)             # <<<<<<<<<<<<<<
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 *     tracksB32 = np.zeros((lentB,), dtype=object)
 */
  __pyx_t_1 = PyObject_Length(__pyx_v_tracksB); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 573; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lentB = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":574
 *     lentA = len(tracksA)
 *     lentB = len(tracksB)
 *     tracksA32 = np.zeros((lentA,), dtype=object)             # <<<<<<<<<<<<<<
 *     tracksB32 = np.zeros((lentB,), dtype=object)
 *     DM = np.zeros((lentA,lentB), dtype=np.double)
 */
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = __Pyx_PyInt_FromSize_t(__pyx_v_lentA); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
  __Pyx_GIVEREF(__pyx_t_2);
  __pyx_t_2 = 0;
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_4));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_4));
  __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_builtin_object) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer);
    __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_7 < 0)) {
      PyErr_Fetch(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer, (PyObject*)__pyx_v_tracksA32, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_8, __pyx_t_9, __pyx_t_10);
      }
    }
    __pyx_pybuffernd_tracksA32.diminfo[0].strides = __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_tracksA32.diminfo[0].shape = __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 574; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_6 = 0;
  __pyx_v_tracksA32 = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "dipy/tracking/distances.pyx":575
 *     lentB = len(tracksB)
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 *     tracksB32 = np.zeros((lentB,), dtype=object)             # <<<<<<<<<<<<<<
 *     DM = np.zeros((lentA,lentB), dtype=np.double)
 *     # process tracks to predictable memory layout
 */
  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = __Pyx_PyInt_FromSize_t(__pyx_v_lentB); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __pyx_t_5 = 0;
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_2));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_2));
  __pyx_t_2 = 0;
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), __pyx_builtin_object) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_3 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_11 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer);
    __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer, (PyObject*)__pyx_t_11, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_7 < 0)) {
      PyErr_Fetch(&__pyx_t_10, &__pyx_t_9, &__pyx_t_8);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer, (PyObject*)__pyx_v_tracksB32, &__Pyx_TypeInfo_object, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_10, __pyx_t_9, __pyx_t_8);
      }
    }
    __pyx_pybuffernd_tracksB32.diminfo[0].strides = __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_tracksB32.diminfo[0].shape = __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 575; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_11 = 0;
  __pyx_v_tracksB32 = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":576
 *     tracksA32 = np.zeros((lentA,), dtype=object)
 *     tracksB32 = np.zeros((lentB,), dtype=object)
 *     DM = np.zeros((lentA,lentB), dtype=np.double)             # <<<<<<<<<<<<<<
 *     # process tracks to predictable memory layout
 *     for i in range(lentA):
 */
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = __Pyx_PyInt_FromSize_t(__pyx_v_lentA); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_5 = __Pyx_PyInt_FromSize_t(__pyx_v_lentB); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __pyx_t_3 = 0;
  __pyx_t_5 = 0;
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_4));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_4));
  __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__double); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_12);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_12) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
  __pyx_t_12 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_12);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_12) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_12, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_13 = ((PyArrayObject *)__pyx_t_12);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_DM.rcbuffer->pybuffer);
    __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_DM.rcbuffer->pybuffer, (PyObject*)__pyx_t_13, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_7 < 0)) {
      PyErr_Fetch(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_DM.rcbuffer->pybuffer, (PyObject*)__pyx_v_DM, &__Pyx_TypeInfo_nn___pyx_t_5numpy_double_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_8, __pyx_t_9, __pyx_t_10);
      }
    }
    __pyx_pybuffernd_DM.diminfo[0].strides = __pyx_pybuffernd_DM.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_DM.diminfo[0].shape = __pyx_pybuffernd_DM.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_DM.diminfo[1].strides = __pyx_pybuffernd_DM.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_DM.diminfo[1].shape = __pyx_pybuffernd_DM.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 576; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_13 = 0;
  __pyx_v_DM = ((PyArrayObject *)__pyx_t_12);
  __pyx_t_12 = 0;

  /* "dipy/tracking/distances.pyx":578
 *     DM = np.zeros((lentA,lentB), dtype=np.double)
 *     # process tracks to predictable memory layout
 *     for i in range(lentA):             # <<<<<<<<<<<<<<
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)
 *     for i in range(lentB):
 */
  __pyx_t_14 = __pyx_v_lentA;
  for (__pyx_t_15 = 0; __pyx_t_15 < __pyx_t_14; __pyx_t_15+=1) {
    __pyx_v_i = __pyx_t_15;

    /* "dipy/tracking/distances.pyx":579
 *     # process tracks to predictable memory layout
 *     for i in range(lentA):
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     for i in range(lentB):
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)
 */
    __pyx_t_12 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_12);
    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_12, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
    __pyx_t_12 = __Pyx_GetItemInt(__pyx_v_tracksA, __pyx_v_i, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 0); if (!__pyx_t_12) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_12);
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_12);
    __Pyx_GIVEREF(__pyx_t_12);
    __pyx_t_12 = 0;
    __pyx_t_12 = PyDict_New(); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_12));
    if (PyDict_SetItem(__pyx_t_12, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_2 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_12)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 579; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_12)); __pyx_t_12 = 0;
    __pyx_t_16 = __pyx_v_i;
    __pyx_t_17 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.buf, __pyx_t_16, __pyx_pybuffernd_tracksA32.diminfo[0].strides);
    __Pyx_GOTREF(*__pyx_t_17);
    __Pyx_INCREF(__pyx_t_2); __Pyx_DECREF(*__pyx_t_17);
    *__pyx_t_17 = __pyx_t_2;
    __Pyx_GIVEREF(*__pyx_t_17);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  }

  /* "dipy/tracking/distances.pyx":580
 *     for i in range(lentA):
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)
 *     for i in range(lentB):             # <<<<<<<<<<<<<<
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)
 *     # preallocate buffer array for track distance calculations
 */
  __pyx_t_14 = __pyx_v_lentB;
  for (__pyx_t_15 = 0; __pyx_t_15 < __pyx_t_14; __pyx_t_15+=1) {
    __pyx_v_i = __pyx_t_15;

    /* "dipy/tracking/distances.pyx":581
 *         tracksA32[i] = np.ascontiguousarray(tracksA[i], dtype=f32_dt)
 *     for i in range(lentB):
 *         tracksB32[i] = np.ascontiguousarray(tracksB[i], dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     # preallocate buffer array for track distance calculations
 *     cdef:
 */
    __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_12);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_tracksB, __pyx_v_i, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t, 0, 0, 0); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_2);
    __Pyx_GIVEREF(__pyx_t_2);
    __pyx_t_2 = 0;
    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_2));
    if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_4 = PyObject_Call(__pyx_t_12, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 581; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
    __pyx_t_18 = __pyx_v_i;
    __pyx_t_17 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.buf, __pyx_t_18, __pyx_pybuffernd_tracksB32.diminfo[0].strides);
    __Pyx_GOTREF(*__pyx_t_17);
    __Pyx_INCREF(__pyx_t_4); __Pyx_DECREF(*__pyx_t_17);
    *__pyx_t_17 = __pyx_t_4;
    __Pyx_GIVEREF(*__pyx_t_17);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  }

  /* "dipy/tracking/distances.pyx":590
 *         size_t t1_len, t2_len
 *         float d[2]
 *     t_len = tracksA32[0].shape[0]             # <<<<<<<<<<<<<<
 * 
 *     for i from 0 <= i < lentA:
 */
  __pyx_t_19 = 0;
  __pyx_t_4 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.buf, __pyx_t_19, __pyx_pybuffernd_tracksA32.diminfo[0].strides);
  __Pyx_INCREF((PyObject*)__pyx_t_4);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__shape); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = __Pyx_GetItemInt(__pyx_t_2, 0, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 590; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_v_t_len = __pyx_t_4;
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":592
 *     t_len = tracksA32[0].shape[0]
 * 
 *     for i from 0 <= i < lentA:             # <<<<<<<<<<<<<<
 *         t1 = tracksA32[i]
 *         #t1_len = t1.shape[0]
 */
  __pyx_t_14 = __pyx_v_lentA;
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_14; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":593
 * 
 *     for i from 0 <= i < lentA:
 *         t1 = tracksA32[i]             # <<<<<<<<<<<<<<
 *         #t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data
 */
    __pyx_t_15 = __pyx_v_i;
    __pyx_t_4 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksA32.rcbuffer->pybuffer.buf, __pyx_t_15, __pyx_pybuffernd_tracksA32.diminfo[0].strides);
    __Pyx_INCREF((PyObject*)__pyx_t_4);
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_20 = ((PyArrayObject *)__pyx_t_4);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
      __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_t_20, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_7 < 0)) {
        PyErr_Fetch(&__pyx_t_10, &__pyx_t_9, &__pyx_t_8);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t1.rcbuffer->pybuffer, (PyObject*)__pyx_v_t1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_10, __pyx_t_9, __pyx_t_8);
        }
      }
      __pyx_pybuffernd_t1.diminfo[0].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t1.diminfo[0].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t1.diminfo[1].strides = __pyx_pybuffernd_t1.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t1.diminfo[1].shape = __pyx_pybuffernd_t1.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 593; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_20 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_t1));
    __pyx_v_t1 = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":595
 *         t1 = tracksA32[i]
 *         #t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data             # <<<<<<<<<<<<<<
 *         for j from 0 <= j < lentB:
 *             t2 = tracksB32[j]
 */
    __pyx_v_t1_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t1->data);

    /* "dipy/tracking/distances.pyx":596
 *         #t1_len = t1.shape[0]
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from 0 <= j < lentB:             # <<<<<<<<<<<<<<
 *             t2 = tracksB32[j]
 *             #t2_len = t2.shape[0]
 */
    __pyx_t_21 = __pyx_v_lentB;
    for (__pyx_v_j = 0; __pyx_v_j < __pyx_t_21; __pyx_v_j++) {

      /* "dipy/tracking/distances.pyx":597
 *         t1_ptr = <cnp.float32_t *>t1.data
 *         for j from 0 <= j < lentB:
 *             t2 = tracksB32[j]             # <<<<<<<<<<<<<<
 *             #t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data
 */
      __pyx_t_22 = __pyx_v_j;
      __pyx_t_4 = (PyObject *) *__Pyx_BufPtrStrided1d(PyObject **, __pyx_pybuffernd_tracksB32.rcbuffer->pybuffer.buf, __pyx_t_22, __pyx_pybuffernd_tracksB32.diminfo[0].strides);
      __Pyx_INCREF((PyObject*)__pyx_t_4);
      if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_20 = ((PyArrayObject *)__pyx_t_4);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
        __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_t_20, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_7 < 0)) {
          PyErr_Fetch(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_t2.rcbuffer->pybuffer, (PyObject*)__pyx_v_t2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_8, __pyx_t_9, __pyx_t_10);
          }
        }
        __pyx_pybuffernd_t2.diminfo[0].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_t2.diminfo[0].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_t2.diminfo[1].strides = __pyx_pybuffernd_t2.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_t2.diminfo[1].shape = __pyx_pybuffernd_t2.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 597; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_20 = 0;
      __Pyx_XDECREF(((PyObject *)__pyx_v_t2));
      __pyx_v_t2 = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":599
 *             t2 = tracksB32[j]
 *             #t2_len = t2.shape[0]
 *             t2_ptr = <cnp.float32_t *>t2.data             # <<<<<<<<<<<<<<
 *             #DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *             track_direct_flip_dist(t1_ptr, t2_ptr,t_len,<float *>d)
 */
      __pyx_v_t2_ptr = ((__pyx_t_5numpy_float32_t *)__pyx_v_t2->data);

      /* "dipy/tracking/distances.pyx":601
 *             t2_ptr = <cnp.float32_t *>t2.data
 *             #DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *             track_direct_flip_dist(t1_ptr, t2_ptr,t_len,<float *>d)             # <<<<<<<<<<<<<<
 *             if d[0]<d[1]:
 *                 DM[i,j]=d[0]
 */
      __pyx_t_23 = __Pyx_PyInt_AsLong(__pyx_v_t_len); if (unlikely((__pyx_t_23 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 601; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_f_4dipy_8tracking_9distances_track_direct_flip_dist(__pyx_v_t1_ptr, __pyx_v_t2_ptr, __pyx_t_23, ((float *)__pyx_v_d));

      /* "dipy/tracking/distances.pyx":602
 *             #DM[i,j] = czhang(t1_len, t1_ptr, t2_len, t2_ptr, min_buffer, metric_type)
 *             track_direct_flip_dist(t1_ptr, t2_ptr,t_len,<float *>d)
 *             if d[0]<d[1]:             # <<<<<<<<<<<<<<
 *                 DM[i,j]=d[0]
 *             else:
 */
      __pyx_t_24 = (((__pyx_v_d[0]) < (__pyx_v_d[1])) != 0);
      if (__pyx_t_24) {

        /* "dipy/tracking/distances.pyx":603
 *             track_direct_flip_dist(t1_ptr, t2_ptr,t_len,<float *>d)
 *             if d[0]<d[1]:
 *                 DM[i,j]=d[0]             # <<<<<<<<<<<<<<
 *             else:
 *                 DM[i,j]=d[1]
 */
        __pyx_t_25 = __pyx_v_i;
        __pyx_t_26 = __pyx_v_j;
        *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_DM.rcbuffer->pybuffer.buf, __pyx_t_25, __pyx_pybuffernd_DM.diminfo[0].strides, __pyx_t_26, __pyx_pybuffernd_DM.diminfo[1].strides) = (__pyx_v_d[0]);
        goto __pyx_L11;
      }
      /*else*/ {

        /* "dipy/tracking/distances.pyx":605
 *                 DM[i,j]=d[0]
 *             else:
 *                 DM[i,j]=d[1]             # <<<<<<<<<<<<<<
 *     return DM
 * 
 */
        __pyx_t_27 = __pyx_v_i;
        __pyx_t_28 = __pyx_v_j;
        *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_DM.rcbuffer->pybuffer.buf, __pyx_t_27, __pyx_pybuffernd_DM.diminfo[0].strides, __pyx_t_28, __pyx_pybuffernd_DM.diminfo[1].strides) = (__pyx_v_d[1]);
      }
      __pyx_L11:;
    }
  }

  /* "dipy/tracking/distances.pyx":606
 *             else:
 *                 DM[i,j]=d[1]
 *     return DM             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_DM));
  __pyx_r = ((PyObject *)__pyx_v_DM);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_12);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_DM.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.bundles_distances_mdf", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_DM.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_t2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksA32.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_tracksB32.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_tracksA32);
  __Pyx_XDECREF((PyObject *)__pyx_v_tracksB32);
  __Pyx_XDECREF((PyObject *)__pyx_v_DM);
  __Pyx_XDECREF((PyObject *)__pyx_v_t1);
  __Pyx_XDECREF((PyObject *)__pyx_v_t2);
  __Pyx_XDECREF(__pyx_v_t_len);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":614
 * 
 * @cython.cdivision(True)
 * cdef inline cnp.float32_t czhang(size_t t1_len,             # <<<<<<<<<<<<<<
 *                                  cnp.float32_t *track1_ptr,
 *                                  size_t t2_len,
 */

static CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_4dipy_8tracking_9distances_czhang(size_t __pyx_v_t1_len, __pyx_t_5numpy_float32_t *__pyx_v_track1_ptr, size_t __pyx_v_t2_len, __pyx_t_5numpy_float32_t *__pyx_v_track2_ptr, __pyx_t_5numpy_float32_t *__pyx_v_min_buffer, int __pyx_v_metric_type) {
  __pyx_t_5numpy_float32_t *__pyx_v_min_t2t1;
  __pyx_t_5numpy_float32_t *__pyx_v_min_t1t2;
  size_t __pyx_v_t1_pi;
  size_t __pyx_v_t2_pi;
  __pyx_t_5numpy_float32_t __pyx_v_mean_t2t1;
  __pyx_t_5numpy_float32_t __pyx_v_mean_t1t2;
  __pyx_t_5numpy_float32_t __pyx_v_dist_val;
  __pyx_t_5numpy_float32_t __pyx_r;
  size_t __pyx_t_1;
  int __pyx_t_2;

  /* "dipy/tracking/distances.pyx":623
 *     cdef:
 *         cnp.float32_t *min_t2t1, *min_t1t2
 *     min_t2t1 = min_buffer             # <<<<<<<<<<<<<<
 *     min_t1t2 = min_buffer + t2_len
 *     min_distances(t1_len, track1_ptr,
 */
  __pyx_v_min_t2t1 = __pyx_v_min_buffer;

  /* "dipy/tracking/distances.pyx":624
 *         cnp.float32_t *min_t2t1, *min_t1t2
 *     min_t2t1 = min_buffer
 *     min_t1t2 = min_buffer + t2_len             # <<<<<<<<<<<<<<
 *     min_distances(t1_len, track1_ptr,
 *                   t2_len, track2_ptr,
 */
  __pyx_v_min_t1t2 = (__pyx_v_min_buffer + __pyx_v_t2_len);

  /* "dipy/tracking/distances.pyx":628
 *                   t2_len, track2_ptr,
 *                   min_t2t1,
 *                   min_t1t2)             # <<<<<<<<<<<<<<
 *     cdef:
 *         size_t t1_pi, t2_pi
 */
  __pyx_f_4dipy_8tracking_9distances_min_distances(__pyx_v_t1_len, __pyx_v_track1_ptr, __pyx_v_t2_len, __pyx_v_track2_ptr, __pyx_v_min_t2t1, __pyx_v_min_t1t2);

  /* "dipy/tracking/distances.pyx":631
 *     cdef:
 *         size_t t1_pi, t2_pi
 *         cnp.float32_t mean_t2t1 = 0, mean_t1t2 = 0, dist_val             # <<<<<<<<<<<<<<
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         mean_t1t2+=min_t1t2[t1_pi]
 */
  __pyx_v_mean_t2t1 = 0.0;
  __pyx_v_mean_t1t2 = 0.0;

  /* "dipy/tracking/distances.pyx":632
 *         size_t t1_pi, t2_pi
 *         cnp.float32_t mean_t2t1 = 0, mean_t1t2 = 0, dist_val
 *     for t1_pi from 0<= t1_pi < t1_len:             # <<<<<<<<<<<<<<
 *         mean_t1t2+=min_t1t2[t1_pi]
 *     mean_t1t2=mean_t1t2/t1_len
 */
  __pyx_t_1 = __pyx_v_t1_len;
  for (__pyx_v_t1_pi = 0; __pyx_v_t1_pi < __pyx_t_1; __pyx_v_t1_pi++) {

    /* "dipy/tracking/distances.pyx":633
 *         cnp.float32_t mean_t2t1 = 0, mean_t1t2 = 0, dist_val
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         mean_t1t2+=min_t1t2[t1_pi]             # <<<<<<<<<<<<<<
 *     mean_t1t2=mean_t1t2/t1_len
 *     for t2_pi from 0<= t2_pi < t2_len:
 */
    __pyx_v_mean_t1t2 = (__pyx_v_mean_t1t2 + (__pyx_v_min_t1t2[__pyx_v_t1_pi]));
  }

  /* "dipy/tracking/distances.pyx":634
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         mean_t1t2+=min_t1t2[t1_pi]
 *     mean_t1t2=mean_t1t2/t1_len             # <<<<<<<<<<<<<<
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         mean_t2t1+=min_t2t1[t2_pi]
 */
  __pyx_v_mean_t1t2 = (__pyx_v_mean_t1t2 / __pyx_v_t1_len);

  /* "dipy/tracking/distances.pyx":635
 *         mean_t1t2+=min_t1t2[t1_pi]
 *     mean_t1t2=mean_t1t2/t1_len
 *     for t2_pi from 0<= t2_pi < t2_len:             # <<<<<<<<<<<<<<
 *         mean_t2t1+=min_t2t1[t2_pi]
 *     mean_t2t1=mean_t2t1/t2_len
 */
  __pyx_t_1 = __pyx_v_t2_len;
  for (__pyx_v_t2_pi = 0; __pyx_v_t2_pi < __pyx_t_1; __pyx_v_t2_pi++) {

    /* "dipy/tracking/distances.pyx":636
 *     mean_t1t2=mean_t1t2/t1_len
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         mean_t2t1+=min_t2t1[t2_pi]             # <<<<<<<<<<<<<<
 *     mean_t2t1=mean_t2t1/t2_len
 *     if metric_type == 0:
 */
    __pyx_v_mean_t2t1 = (__pyx_v_mean_t2t1 + (__pyx_v_min_t2t1[__pyx_v_t2_pi]));
  }

  /* "dipy/tracking/distances.pyx":637
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         mean_t2t1+=min_t2t1[t2_pi]
 *     mean_t2t1=mean_t2t1/t2_len             # <<<<<<<<<<<<<<
 *     if metric_type == 0:
 *         dist_val=(mean_t2t1+mean_t1t2)/2.0
 */
  __pyx_v_mean_t2t1 = (__pyx_v_mean_t2t1 / __pyx_v_t2_len);

  /* "dipy/tracking/distances.pyx":645
 *         else:
 *             dist_val=mean_t1t2
 *     elif metric_type == 2:             # <<<<<<<<<<<<<<
 *         if mean_t2t1 > mean_t1t2:
 *             dist_val=mean_t2t1
 */
  switch (__pyx_v_metric_type) {

    /* "dipy/tracking/distances.pyx":638
 *         mean_t2t1+=min_t2t1[t2_pi]
 *     mean_t2t1=mean_t2t1/t2_len
 *     if metric_type == 0:             # <<<<<<<<<<<<<<
 *         dist_val=(mean_t2t1+mean_t1t2)/2.0
 *     elif metric_type == 1:
 */
    case 0:

    /* "dipy/tracking/distances.pyx":639
 *     mean_t2t1=mean_t2t1/t2_len
 *     if metric_type == 0:
 *         dist_val=(mean_t2t1+mean_t1t2)/2.0             # <<<<<<<<<<<<<<
 *     elif metric_type == 1:
 *         if mean_t2t1 < mean_t1t2:
 */
    __pyx_v_dist_val = ((__pyx_v_mean_t2t1 + __pyx_v_mean_t1t2) / 2.0);
    break;

    /* "dipy/tracking/distances.pyx":640
 *     if metric_type == 0:
 *         dist_val=(mean_t2t1+mean_t1t2)/2.0
 *     elif metric_type == 1:             # <<<<<<<<<<<<<<
 *         if mean_t2t1 < mean_t1t2:
 *             dist_val=mean_t2t1
 */
    case 1:

    /* "dipy/tracking/distances.pyx":641
 *         dist_val=(mean_t2t1+mean_t1t2)/2.0
 *     elif metric_type == 1:
 *         if mean_t2t1 < mean_t1t2:             # <<<<<<<<<<<<<<
 *             dist_val=mean_t2t1
 *         else:
 */
    __pyx_t_2 = ((__pyx_v_mean_t2t1 < __pyx_v_mean_t1t2) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":642
 *     elif metric_type == 1:
 *         if mean_t2t1 < mean_t1t2:
 *             dist_val=mean_t2t1             # <<<<<<<<<<<<<<
 *         else:
 *             dist_val=mean_t1t2
 */
      __pyx_v_dist_val = __pyx_v_mean_t2t1;
      goto __pyx_L7;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":644
 *             dist_val=mean_t2t1
 *         else:
 *             dist_val=mean_t1t2             # <<<<<<<<<<<<<<
 *     elif metric_type == 2:
 *         if mean_t2t1 > mean_t1t2:
 */
      __pyx_v_dist_val = __pyx_v_mean_t1t2;
    }
    __pyx_L7:;
    break;

    /* "dipy/tracking/distances.pyx":645
 *         else:
 *             dist_val=mean_t1t2
 *     elif metric_type == 2:             # <<<<<<<<<<<<<<
 *         if mean_t2t1 > mean_t1t2:
 *             dist_val=mean_t2t1
 */
    case 2:

    /* "dipy/tracking/distances.pyx":646
 *             dist_val=mean_t1t2
 *     elif metric_type == 2:
 *         if mean_t2t1 > mean_t1t2:             # <<<<<<<<<<<<<<
 *             dist_val=mean_t2t1
 *         else:
 */
    __pyx_t_2 = ((__pyx_v_mean_t2t1 > __pyx_v_mean_t1t2) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":647
 *     elif metric_type == 2:
 *         if mean_t2t1 > mean_t1t2:
 *             dist_val=mean_t2t1             # <<<<<<<<<<<<<<
 *         else:
 *             dist_val=mean_t1t2
 */
      __pyx_v_dist_val = __pyx_v_mean_t2t1;
      goto __pyx_L8;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":649
 *             dist_val=mean_t2t1
 *         else:
 *             dist_val=mean_t1t2             # <<<<<<<<<<<<<<
 *     return dist_val
 * 
 */
      __pyx_v_dist_val = __pyx_v_mean_t1t2;
    }
    __pyx_L8:;
    break;
  }

  /* "dipy/tracking/distances.pyx":650
 *         else:
 *             dist_val=mean_t1t2
 *     return dist_val             # <<<<<<<<<<<<<<
 * 
 * @cython.cdivision(True)
 */
  __pyx_r = __pyx_v_dist_val;
  goto __pyx_L0;

  __pyx_r = 0;
  __pyx_L0:;
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":653
 * 
 * @cython.cdivision(True)
 * cdef inline void min_distances(size_t t1_len,             # <<<<<<<<<<<<<<
 *                                cnp.float32_t *track1_ptr,
 *                                size_t t2_len,
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_min_distances(size_t __pyx_v_t1_len, __pyx_t_5numpy_float32_t *__pyx_v_track1_ptr, size_t __pyx_v_t2_len, __pyx_t_5numpy_float32_t *__pyx_v_track2_ptr, __pyx_t_5numpy_float32_t *__pyx_v_min_t2t1, __pyx_t_5numpy_float32_t *__pyx_v_min_t1t2) {
  __pyx_t_5numpy_float32_t *__pyx_v_t1_pt;
  __pyx_t_5numpy_float32_t *__pyx_v_t2_pt;
  __pyx_t_5numpy_float32_t __pyx_v_d0;
  __pyx_t_5numpy_float32_t __pyx_v_d1;
  __pyx_t_5numpy_float32_t __pyx_v_d2;
  __pyx_t_5numpy_float32_t __pyx_v_delta2;
  int __pyx_v_t1_pi;
  int __pyx_v_t2_pi;
  size_t __pyx_t_1;
  size_t __pyx_t_2;
  int __pyx_t_3;

  /* "dipy/tracking/distances.pyx":663
 *         cnp.float32_t delta2
 *         int t1_pi, t2_pi
 *     for t2_pi from 0<= t2_pi < t2_len:             # <<<<<<<<<<<<<<
 *         min_t2t1[t2_pi] = inf
 *     for t1_pi from 0<= t1_pi < t1_len:
 */
  __pyx_t_1 = __pyx_v_t2_len;
  for (__pyx_v_t2_pi = 0; __pyx_v_t2_pi < __pyx_t_1; __pyx_v_t2_pi++) {

    /* "dipy/tracking/distances.pyx":664
 *         int t1_pi, t2_pi
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         min_t2t1[t2_pi] = inf             # <<<<<<<<<<<<<<
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         min_t1t2[t1_pi] = inf
 */
    (__pyx_v_min_t2t1[__pyx_v_t2_pi]) = __pyx_v_4dipy_8tracking_9distances_inf;
  }

  /* "dipy/tracking/distances.pyx":665
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         min_t2t1[t2_pi] = inf
 *     for t1_pi from 0<= t1_pi < t1_len:             # <<<<<<<<<<<<<<
 *         min_t1t2[t1_pi] = inf
 *     # pointer to current point in track 1
 */
  __pyx_t_1 = __pyx_v_t1_len;
  for (__pyx_v_t1_pi = 0; __pyx_v_t1_pi < __pyx_t_1; __pyx_v_t1_pi++) {

    /* "dipy/tracking/distances.pyx":666
 *         min_t2t1[t2_pi] = inf
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         min_t1t2[t1_pi] = inf             # <<<<<<<<<<<<<<
 *     # pointer to current point in track 1
 *     t1_pt = track1_ptr
 */
    (__pyx_v_min_t1t2[__pyx_v_t1_pi]) = __pyx_v_4dipy_8tracking_9distances_inf;
  }

  /* "dipy/tracking/distances.pyx":668
 *         min_t1t2[t1_pi] = inf
 *     # pointer to current point in track 1
 *     t1_pt = track1_ptr             # <<<<<<<<<<<<<<
 *     # calculate min squared distance between each point in the two
 *     # lines.  Squared distance to delay doing the sqrt until after this
 */
  __pyx_v_t1_pt = __pyx_v_track1_ptr;

  /* "dipy/tracking/distances.pyx":672
 *     # lines.  Squared distance to delay doing the sqrt until after this
 *     # speed-critical loop
 *     for t1_pi from 0<= t1_pi < t1_len:             # <<<<<<<<<<<<<<
 *         # pointer to current point in track 2
 *         t2_pt = track2_ptr
 */
  __pyx_t_1 = __pyx_v_t1_len;
  for (__pyx_v_t1_pi = 0; __pyx_v_t1_pi < __pyx_t_1; __pyx_v_t1_pi++) {

    /* "dipy/tracking/distances.pyx":674
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         # pointer to current point in track 2
 *         t2_pt = track2_ptr             # <<<<<<<<<<<<<<
 *         for t2_pi from 0<= t2_pi < t2_len:
 *             d0 = t1_pt[0] - t2_pt[0]
 */
    __pyx_v_t2_pt = __pyx_v_track2_ptr;

    /* "dipy/tracking/distances.pyx":675
 *         # pointer to current point in track 2
 *         t2_pt = track2_ptr
 *         for t2_pi from 0<= t2_pi < t2_len:             # <<<<<<<<<<<<<<
 *             d0 = t1_pt[0] - t2_pt[0]
 *             d1 = t1_pt[1] - t2_pt[1]
 */
    __pyx_t_2 = __pyx_v_t2_len;
    for (__pyx_v_t2_pi = 0; __pyx_v_t2_pi < __pyx_t_2; __pyx_v_t2_pi++) {

      /* "dipy/tracking/distances.pyx":676
 *         t2_pt = track2_ptr
 *         for t2_pi from 0<= t2_pi < t2_len:
 *             d0 = t1_pt[0] - t2_pt[0]             # <<<<<<<<<<<<<<
 *             d1 = t1_pt[1] - t2_pt[1]
 *             d2 = t1_pt[2] - t2_pt[2]
 */
      __pyx_v_d0 = ((__pyx_v_t1_pt[0]) - (__pyx_v_t2_pt[0]));

      /* "dipy/tracking/distances.pyx":677
 *         for t2_pi from 0<= t2_pi < t2_len:
 *             d0 = t1_pt[0] - t2_pt[0]
 *             d1 = t1_pt[1] - t2_pt[1]             # <<<<<<<<<<<<<<
 *             d2 = t1_pt[2] - t2_pt[2]
 *             delta2 = d0*d0 + d1*d1 + d2*d2
 */
      __pyx_v_d1 = ((__pyx_v_t1_pt[1]) - (__pyx_v_t2_pt[1]));

      /* "dipy/tracking/distances.pyx":678
 *             d0 = t1_pt[0] - t2_pt[0]
 *             d1 = t1_pt[1] - t2_pt[1]
 *             d2 = t1_pt[2] - t2_pt[2]             # <<<<<<<<<<<<<<
 *             delta2 = d0*d0 + d1*d1 + d2*d2
 *             if delta2 < min_t2t1[t2_pi]:
 */
      __pyx_v_d2 = ((__pyx_v_t1_pt[2]) - (__pyx_v_t2_pt[2]));

      /* "dipy/tracking/distances.pyx":679
 *             d1 = t1_pt[1] - t2_pt[1]
 *             d2 = t1_pt[2] - t2_pt[2]
 *             delta2 = d0*d0 + d1*d1 + d2*d2             # <<<<<<<<<<<<<<
 *             if delta2 < min_t2t1[t2_pi]:
 *                 min_t2t1[t2_pi]=delta2
 */
      __pyx_v_delta2 = (((__pyx_v_d0 * __pyx_v_d0) + (__pyx_v_d1 * __pyx_v_d1)) + (__pyx_v_d2 * __pyx_v_d2));

      /* "dipy/tracking/distances.pyx":680
 *             d2 = t1_pt[2] - t2_pt[2]
 *             delta2 = d0*d0 + d1*d1 + d2*d2
 *             if delta2 < min_t2t1[t2_pi]:             # <<<<<<<<<<<<<<
 *                 min_t2t1[t2_pi]=delta2
 *             if delta2 < min_t1t2[t1_pi]:
 */
      __pyx_t_3 = ((__pyx_v_delta2 < (__pyx_v_min_t2t1[__pyx_v_t2_pi])) != 0);
      if (__pyx_t_3) {

        /* "dipy/tracking/distances.pyx":681
 *             delta2 = d0*d0 + d1*d1 + d2*d2
 *             if delta2 < min_t2t1[t2_pi]:
 *                 min_t2t1[t2_pi]=delta2             # <<<<<<<<<<<<<<
 *             if delta2 < min_t1t2[t1_pi]:
 *                 min_t1t2[t1_pi]=delta2
 */
        (__pyx_v_min_t2t1[__pyx_v_t2_pi]) = __pyx_v_delta2;
        goto __pyx_L11;
      }
      __pyx_L11:;

      /* "dipy/tracking/distances.pyx":682
 *             if delta2 < min_t2t1[t2_pi]:
 *                 min_t2t1[t2_pi]=delta2
 *             if delta2 < min_t1t2[t1_pi]:             # <<<<<<<<<<<<<<
 *                 min_t1t2[t1_pi]=delta2
 *             t2_pt += 3 # to next point in track 2
 */
      __pyx_t_3 = ((__pyx_v_delta2 < (__pyx_v_min_t1t2[__pyx_v_t1_pi])) != 0);
      if (__pyx_t_3) {

        /* "dipy/tracking/distances.pyx":683
 *                 min_t2t1[t2_pi]=delta2
 *             if delta2 < min_t1t2[t1_pi]:
 *                 min_t1t2[t1_pi]=delta2             # <<<<<<<<<<<<<<
 *             t2_pt += 3 # to next point in track 2
 *         t1_pt += 3 # to next point in track 1
 */
        (__pyx_v_min_t1t2[__pyx_v_t1_pi]) = __pyx_v_delta2;
        goto __pyx_L12;
      }
      __pyx_L12:;

      /* "dipy/tracking/distances.pyx":684
 *             if delta2 < min_t1t2[t1_pi]:
 *                 min_t1t2[t1_pi]=delta2
 *             t2_pt += 3 # to next point in track 2             # <<<<<<<<<<<<<<
 *         t1_pt += 3 # to next point in track 1
 *     # sqrt to get Euclidean distance from squared distance
 */
      __pyx_v_t2_pt = (__pyx_v_t2_pt + 3);
    }

    /* "dipy/tracking/distances.pyx":685
 *                 min_t1t2[t1_pi]=delta2
 *             t2_pt += 3 # to next point in track 2
 *         t1_pt += 3 # to next point in track 1             # <<<<<<<<<<<<<<
 *     # sqrt to get Euclidean distance from squared distance
 *     for t1_pi from 0<= t1_pi < t1_len:
 */
    __pyx_v_t1_pt = (__pyx_v_t1_pt + 3);
  }

  /* "dipy/tracking/distances.pyx":687
 *         t1_pt += 3 # to next point in track 1
 *     # sqrt to get Euclidean distance from squared distance
 *     for t1_pi from 0<= t1_pi < t1_len:             # <<<<<<<<<<<<<<
 *         min_t1t2[t1_pi]=sqrt(min_t1t2[t1_pi])
 *     for t2_pi from 0<= t2_pi < t2_len:
 */
  __pyx_t_1 = __pyx_v_t1_len;
  for (__pyx_v_t1_pi = 0; __pyx_v_t1_pi < __pyx_t_1; __pyx_v_t1_pi++) {

    /* "dipy/tracking/distances.pyx":688
 *     # sqrt to get Euclidean distance from squared distance
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         min_t1t2[t1_pi]=sqrt(min_t1t2[t1_pi])             # <<<<<<<<<<<<<<
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         min_t2t1[t2_pi]=sqrt(min_t2t1[t2_pi])
 */
    (__pyx_v_min_t1t2[__pyx_v_t1_pi]) = sqrt((__pyx_v_min_t1t2[__pyx_v_t1_pi]));
  }

  /* "dipy/tracking/distances.pyx":689
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         min_t1t2[t1_pi]=sqrt(min_t1t2[t1_pi])
 *     for t2_pi from 0<= t2_pi < t2_len:             # <<<<<<<<<<<<<<
 *         min_t2t1[t2_pi]=sqrt(min_t2t1[t2_pi])
 * 
 */
  __pyx_t_1 = __pyx_v_t2_len;
  for (__pyx_v_t2_pi = 0; __pyx_v_t2_pi < __pyx_t_1; __pyx_v_t2_pi++) {

    /* "dipy/tracking/distances.pyx":690
 *         min_t1t2[t1_pi]=sqrt(min_t1t2[t1_pi])
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         min_t2t1[t2_pi]=sqrt(min_t2t1[t2_pi])             # <<<<<<<<<<<<<<
 * 
 * 
 */
    (__pyx_v_min_t2t1[__pyx_v_t2_pi]) = sqrt((__pyx_v_min_t2t1[__pyx_v_t2_pi]));
  }

}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_23mam_distances(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_22mam_distances[] = " Min/Max/Mean Average Minimum Distance between tracks xyz1 and xyz2\n\n    Based on the metrics in Zhang, Correia, Laidlaw 2008\n    http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4479455\n    which in turn are based on those of Corouge et al. 2004\n\n    Parameters\n    ----------\n    xyz1 : array, shape (N1,3), dtype float32\n    xyz2 : array, shape (N2,3), dtype float32\n       arrays representing x,y,z of the N1 and N2 points of two tracks\n    metrics : {'avg','min','max','all'}\n       Metric to calculate.  {'avg','min','max'} return a scalar. 'all'\n       returns a tuple\n\n    Returns\n    -------\n    avg_mcd : float\n       average_mean_closest_distance\n    min_mcd : float\n       minimum_mean_closest_distance\n    max_mcd : float\n       maximum_mean_closest_distance\n\n    Notes\n    -----\n    Algorithmic description\n\n    Lets say we have curves A and B.\n\n    For every point in A calculate the minimum distance from every point\n    in B stored in minAB\n\n    For every point in B calculate the minimum distance from every point\n    in A stored in minBA\n\n    find average of minAB stored as avg_minAB\n    find average of minBA stored as avg_minBA\n\n    if metric is 'avg' then return (avg_minAB + avg_minBA)/2.0\n    if metric is 'min' then return min(avg_minAB,avg_minBA)\n    if metric is 'max' then return max(avg_minAB,avg_minBA)\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_23mam_distances = {__Pyx_NAMESTR("mam_distances"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_23mam_distances, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_22mam_distances)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_23mam_distances(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz1 = 0;
  PyObject *__pyx_v_xyz2 = 0;
  PyObject *__pyx_v_metric = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("mam_distances (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz1,&__pyx_n_s__xyz2,&__pyx_n_s__metric,0};
    PyObject* values[3] = {0,0,0};
    values[2] = ((PyObject *)__pyx_n_s__all);
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("mam_distances", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__metric);
          if (value) { values[2] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "mam_distances") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_xyz1 = values[0];
    __pyx_v_xyz2 = values[1];
    __pyx_v_metric = values[2];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("mam_distances", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.mam_distances", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_22mam_distances(__pyx_self, __pyx_v_xyz1, __pyx_v_xyz2, __pyx_v_metric);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":694
 * 
 * 
 * def mam_distances(xyz1,xyz2,metric='all'):             # <<<<<<<<<<<<<<
 *     ''' Min/Max/Mean Average Minimum Distance between tracks xyz1 and xyz2
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_22mam_distances(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz1, PyObject *__pyx_v_xyz2, PyObject *__pyx_v_metric) {
  PyArrayObject *__pyx_v_track1 = 0;
  PyArrayObject *__pyx_v_track2 = 0;
  size_t __pyx_v_t1_len;
  size_t __pyx_v_t2_len;
  __pyx_t_5numpy_float32_t *__pyx_v_min_t2t1;
  __pyx_t_5numpy_float32_t *__pyx_v_min_t1t2;
  PyArrayObject *__pyx_v_distances_buffer = 0;
  size_t __pyx_v_t1_pi;
  size_t __pyx_v_t2_pi;
  __pyx_t_5numpy_float32_t __pyx_v_mean_t2t1;
  __pyx_t_5numpy_float32_t __pyx_v_mean_t1t2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_distances_buffer;
  __Pyx_Buffer __pyx_pybuffer_distances_buffer;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track1;
  __Pyx_Buffer __pyx_pybuffer_track1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track2;
  __Pyx_Buffer __pyx_pybuffer_track2;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyArrayObject *__pyx_t_5 = NULL;
  int __pyx_t_6;
  PyObject *__pyx_t_7 = NULL;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  PyArrayObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyArrayObject *__pyx_t_12 = NULL;
  size_t __pyx_t_13;
  int __pyx_t_14;
  PyObject *__pyx_t_15 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("mam_distances", 0);
  __pyx_pybuffer_track1.pybuffer.buf = NULL;
  __pyx_pybuffer_track1.refcount = 0;
  __pyx_pybuffernd_track1.data = NULL;
  __pyx_pybuffernd_track1.rcbuffer = &__pyx_pybuffer_track1;
  __pyx_pybuffer_track2.pybuffer.buf = NULL;
  __pyx_pybuffer_track2.refcount = 0;
  __pyx_pybuffernd_track2.data = NULL;
  __pyx_pybuffernd_track2.rcbuffer = &__pyx_pybuffer_track2;
  __pyx_pybuffer_distances_buffer.pybuffer.buf = NULL;
  __pyx_pybuffer_distances_buffer.refcount = 0;
  __pyx_pybuffernd_distances_buffer.data = NULL;
  __pyx_pybuffernd_distances_buffer.rcbuffer = &__pyx_pybuffer_distances_buffer;

  /* "dipy/tracking/distances.pyx":742
 *         cnp.ndarray[cnp.float32_t, ndim=2] track2
 *         size_t t1_len, t2_len
 *     track1 = np.ascontiguousarray(xyz1, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t1_len = track1.shape[0]
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_v_xyz1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_xyz1);
  __Pyx_GIVEREF(__pyx_v_xyz1);
  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track1.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track1.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track1.rcbuffer->pybuffer, (PyObject*)__pyx_v_track1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_track1.diminfo[0].strides = __pyx_pybuffernd_track1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track1.diminfo[0].shape = __pyx_pybuffernd_track1.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track1.diminfo[1].strides = __pyx_pybuffernd_track1.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track1.diminfo[1].shape = __pyx_pybuffernd_track1.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 742; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __pyx_v_track1 = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":743
 *         size_t t1_len, t2_len
 *     track1 = np.ascontiguousarray(xyz1, dtype=f32_dt)
 *     t1_len = track1.shape[0]             # <<<<<<<<<<<<<<
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)
 *     t2_len = track2.shape[0]
 */
  __pyx_v_t1_len = (__pyx_v_track1->dimensions[0]);

  /* "dipy/tracking/distances.pyx":744
 *     track1 = np.ascontiguousarray(xyz1, dtype=f32_dt)
 *     t1_len = track1.shape[0]
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t2_len = track2.shape[0]
 *     # preallocate buffer array for track distance calculations
 */
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(__pyx_v_xyz2);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_xyz2);
  __Pyx_GIVEREF(__pyx_v_xyz2);
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_4), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_10 = ((PyArrayObject *)__pyx_t_2);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track2.rcbuffer->pybuffer, (PyObject*)__pyx_t_10, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track2.rcbuffer->pybuffer, (PyObject*)__pyx_v_track2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_8, __pyx_t_7);
      }
    }
    __pyx_pybuffernd_track2.diminfo[0].strides = __pyx_pybuffernd_track2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track2.diminfo[0].shape = __pyx_pybuffernd_track2.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track2.diminfo[1].strides = __pyx_pybuffernd_track2.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track2.diminfo[1].shape = __pyx_pybuffernd_track2.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_10 = 0;
  __pyx_v_track2 = ((PyArrayObject *)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "dipy/tracking/distances.pyx":745
 *     t1_len = track1.shape[0]
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)
 *     t2_len = track2.shape[0]             # <<<<<<<<<<<<<<
 *     # preallocate buffer array for track distance calculations
 *     cdef:
 */
  __pyx_v_t2_len = (__pyx_v_track2->dimensions[0]);

  /* "dipy/tracking/distances.pyx":750
 *         cnp.float32_t *min_t2t1, *min_t1t2
 *         cnp.ndarray [cnp.float32_t, ndim=1] distances_buffer
 *     distances_buffer = np.zeros((t1_len + t2_len,), dtype=np.float32)             # <<<<<<<<<<<<<<
 *     min_t2t1 = <cnp.float32_t *> distances_buffer.data
 *     min_t1t2 = min_t2t1 + t2_len
 */
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = __Pyx_PyInt_FromSize_t((__pyx_v_t1_len + __pyx_v_t2_len)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
  __Pyx_GIVEREF(__pyx_t_2);
  __pyx_t_2 = 0;
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_4));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_4));
  __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_11) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  __pyx_t_11 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_11);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_t_12, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_v_distances_buffer, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_distances_buffer.diminfo[0].strides = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_distances_buffer.diminfo[0].shape = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "dipy/tracking/distances.pyx":751
 *         cnp.ndarray [cnp.float32_t, ndim=1] distances_buffer
 *     distances_buffer = np.zeros((t1_len + t2_len,), dtype=np.float32)
 *     min_t2t1 = <cnp.float32_t *> distances_buffer.data             # <<<<<<<<<<<<<<
 *     min_t1t2 = min_t2t1 + t2_len
 *     min_distances(t1_len, <cnp.float32_t *>track1.data,
 */
  __pyx_v_min_t2t1 = ((__pyx_t_5numpy_float32_t *)__pyx_v_distances_buffer->data);

  /* "dipy/tracking/distances.pyx":752
 *     distances_buffer = np.zeros((t1_len + t2_len,), dtype=np.float32)
 *     min_t2t1 = <cnp.float32_t *> distances_buffer.data
 *     min_t1t2 = min_t2t1 + t2_len             # <<<<<<<<<<<<<<
 *     min_distances(t1_len, <cnp.float32_t *>track1.data,
 *                   t2_len, <cnp.float32_t *>track2.data,
 */
  __pyx_v_min_t1t2 = (__pyx_v_min_t2t1 + __pyx_v_t2_len);

  /* "dipy/tracking/distances.pyx":756
 *                   t2_len, <cnp.float32_t *>track2.data,
 *                   min_t2t1,
 *                   min_t1t2)             # <<<<<<<<<<<<<<
 *     cdef:
 *         size_t t1_pi, t2_pi
 */
  __pyx_f_4dipy_8tracking_9distances_min_distances(__pyx_v_t1_len, ((__pyx_t_5numpy_float32_t *)__pyx_v_track1->data), __pyx_v_t2_len, ((__pyx_t_5numpy_float32_t *)__pyx_v_track2->data), __pyx_v_min_t2t1, __pyx_v_min_t1t2);

  /* "dipy/tracking/distances.pyx":759
 *     cdef:
 *         size_t t1_pi, t2_pi
 *         cnp.float32_t mean_t2t1 = 0, mean_t1t2 = 0             # <<<<<<<<<<<<<<
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         mean_t1t2+=min_t1t2[t1_pi]
 */
  __pyx_v_mean_t2t1 = 0.0;
  __pyx_v_mean_t1t2 = 0.0;

  /* "dipy/tracking/distances.pyx":760
 *         size_t t1_pi, t2_pi
 *         cnp.float32_t mean_t2t1 = 0, mean_t1t2 = 0
 *     for t1_pi from 0<= t1_pi < t1_len:             # <<<<<<<<<<<<<<
 *         mean_t1t2+=min_t1t2[t1_pi]
 *     mean_t1t2=mean_t1t2/t1_len
 */
  __pyx_t_13 = __pyx_v_t1_len;
  for (__pyx_v_t1_pi = 0; __pyx_v_t1_pi < __pyx_t_13; __pyx_v_t1_pi++) {

    /* "dipy/tracking/distances.pyx":761
 *         cnp.float32_t mean_t2t1 = 0, mean_t1t2 = 0
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         mean_t1t2+=min_t1t2[t1_pi]             # <<<<<<<<<<<<<<
 *     mean_t1t2=mean_t1t2/t1_len
 *     for t2_pi from 0<= t2_pi < t2_len:
 */
    __pyx_v_mean_t1t2 = (__pyx_v_mean_t1t2 + (__pyx_v_min_t1t2[__pyx_v_t1_pi]));
  }

  /* "dipy/tracking/distances.pyx":762
 *     for t1_pi from 0<= t1_pi < t1_len:
 *         mean_t1t2+=min_t1t2[t1_pi]
 *     mean_t1t2=mean_t1t2/t1_len             # <<<<<<<<<<<<<<
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         mean_t2t1+=min_t2t1[t2_pi]
 */
  if (unlikely(__pyx_v_t1_len == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 762; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_mean_t1t2 = (__pyx_v_mean_t1t2 / __pyx_v_t1_len);

  /* "dipy/tracking/distances.pyx":763
 *         mean_t1t2+=min_t1t2[t1_pi]
 *     mean_t1t2=mean_t1t2/t1_len
 *     for t2_pi from 0<= t2_pi < t2_len:             # <<<<<<<<<<<<<<
 *         mean_t2t1+=min_t2t1[t2_pi]
 *     mean_t2t1=mean_t2t1/t2_len
 */
  __pyx_t_13 = __pyx_v_t2_len;
  for (__pyx_v_t2_pi = 0; __pyx_v_t2_pi < __pyx_t_13; __pyx_v_t2_pi++) {

    /* "dipy/tracking/distances.pyx":764
 *     mean_t1t2=mean_t1t2/t1_len
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         mean_t2t1+=min_t2t1[t2_pi]             # <<<<<<<<<<<<<<
 *     mean_t2t1=mean_t2t1/t2_len
 *     if metric=='all':
 */
    __pyx_v_mean_t2t1 = (__pyx_v_mean_t2t1 + (__pyx_v_min_t2t1[__pyx_v_t2_pi]));
  }

  /* "dipy/tracking/distances.pyx":765
 *     for t2_pi from 0<= t2_pi < t2_len:
 *         mean_t2t1+=min_t2t1[t2_pi]
 *     mean_t2t1=mean_t2t1/t2_len             # <<<<<<<<<<<<<<
 *     if metric=='all':
 *         return ((mean_t2t1+mean_t1t2)/2.0,
 */
  if (unlikely(__pyx_v_t2_len == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 765; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_mean_t2t1 = (__pyx_v_mean_t2t1 / __pyx_v_t2_len);

  /* "dipy/tracking/distances.pyx":766
 *         mean_t2t1+=min_t2t1[t2_pi]
 *     mean_t2t1=mean_t2t1/t2_len
 *     if metric=='all':             # <<<<<<<<<<<<<<
 *         return ((mean_t2t1+mean_t1t2)/2.0,
 *                 np.min((mean_t2t1,mean_t1t2)),
 */
  __pyx_t_11 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__all), Py_EQ); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 766; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 766; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  if (__pyx_t_14) {

    /* "dipy/tracking/distances.pyx":767
 *     mean_t2t1=mean_t2t1/t2_len
 *     if metric=='all':
 *         return ((mean_t2t1+mean_t1t2)/2.0,             # <<<<<<<<<<<<<<
 *                 np.min((mean_t2t1,mean_t1t2)),
 *                 np.max((mean_t2t1,mean_t1t2)))
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_11 = PyFloat_FromDouble(((__pyx_v_mean_t2t1 + __pyx_v_mean_t1t2) / 2.0)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 767; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);

    /* "dipy/tracking/distances.pyx":768
 *     if metric=='all':
 *         return ((mean_t2t1+mean_t1t2)/2.0,
 *                 np.min((mean_t2t1,mean_t1t2)),             # <<<<<<<<<<<<<<
 *                 np.max((mean_t2t1,mean_t1t2)))
 *     elif metric=='avg':
 */
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__min); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyFloat_FromDouble(__pyx_v_mean_t2t1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_1 = PyFloat_FromDouble(__pyx_v_mean_t1t2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_3 = PyTuple_New(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_1);
    __Pyx_GIVEREF(__pyx_t_1);
    __pyx_t_4 = 0;
    __pyx_t_1 = 0;
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_3));
    __Pyx_GIVEREF(((PyObject *)__pyx_t_3));
    __pyx_t_3 = 0;
    __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 768; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;

    /* "dipy/tracking/distances.pyx":769
 *         return ((mean_t2t1+mean_t1t2)/2.0,
 *                 np.min((mean_t2t1,mean_t1t2)),
 *                 np.max((mean_t2t1,mean_t1t2)))             # <<<<<<<<<<<<<<
 *     elif metric=='avg':
 *         return (mean_t2t1+mean_t1t2)/2.0
 */
    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__max); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = PyFloat_FromDouble(__pyx_v_mean_t2t1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_4 = PyFloat_FromDouble(__pyx_v_mean_t1t2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_15 = PyTuple_New(2); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    PyTuple_SET_ITEM(__pyx_t_15, 0, __pyx_t_1);
    __Pyx_GIVEREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_15, 1, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    __pyx_t_1 = 0;
    __pyx_t_4 = 0;
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_t_15));
    __Pyx_GIVEREF(((PyObject *)__pyx_t_15));
    __pyx_t_15 = 0;
    __pyx_t_15 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 767; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_11);
    __Pyx_GIVEREF(__pyx_t_11);
    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_4, 2, __pyx_t_15);
    __Pyx_GIVEREF(__pyx_t_15);
    __pyx_t_11 = 0;
    __pyx_t_3 = 0;
    __pyx_t_15 = 0;
    __pyx_r = ((PyObject *)__pyx_t_4);
    __pyx_t_4 = 0;
    goto __pyx_L0;
    goto __pyx_L7;
  }

  /* "dipy/tracking/distances.pyx":770
 *                 np.min((mean_t2t1,mean_t1t2)),
 *                 np.max((mean_t2t1,mean_t1t2)))
 *     elif metric=='avg':             # <<<<<<<<<<<<<<
 *         return (mean_t2t1+mean_t1t2)/2.0
 *     elif metric=='min':
 */
  __pyx_t_4 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__avg), Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (__pyx_t_14) {

    /* "dipy/tracking/distances.pyx":771
 *                 np.max((mean_t2t1,mean_t1t2)))
 *     elif metric=='avg':
 *         return (mean_t2t1+mean_t1t2)/2.0             # <<<<<<<<<<<<<<
 *     elif metric=='min':
 *         return np.min((mean_t2t1,mean_t1t2))
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_4 = PyFloat_FromDouble(((__pyx_v_mean_t2t1 + __pyx_v_mean_t1t2) / 2.0)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_r = __pyx_t_4;
    __pyx_t_4 = 0;
    goto __pyx_L0;
    goto __pyx_L7;
  }

  /* "dipy/tracking/distances.pyx":772
 *     elif metric=='avg':
 *         return (mean_t2t1+mean_t1t2)/2.0
 *     elif metric=='min':             # <<<<<<<<<<<<<<
 *         return np.min((mean_t2t1,mean_t1t2))
 *     elif metric =='max':
 */
  __pyx_t_4 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__min), Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (__pyx_t_14) {

    /* "dipy/tracking/distances.pyx":773
 *         return (mean_t2t1+mean_t1t2)/2.0
 *     elif metric=='min':
 *         return np.min((mean_t2t1,mean_t1t2))             # <<<<<<<<<<<<<<
 *     elif metric =='max':
 *         return np.max((mean_t2t1,mean_t1t2))
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_15 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__min); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyFloat_FromDouble(__pyx_v_mean_t2t1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = PyFloat_FromDouble(__pyx_v_mean_t1t2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_11 = PyTuple_New(2); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_11, 1, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    __pyx_t_4 = 0;
    __pyx_t_3 = 0;
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_11));
    __Pyx_GIVEREF(((PyObject *)__pyx_t_11));
    __pyx_t_11 = 0;
    __pyx_t_11 = PyObject_Call(__pyx_t_15, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    __pyx_r = __pyx_t_11;
    __pyx_t_11 = 0;
    goto __pyx_L0;
    goto __pyx_L7;
  }

  /* "dipy/tracking/distances.pyx":774
 *     elif metric=='min':
 *         return np.min((mean_t2t1,mean_t1t2))
 *     elif metric =='max':             # <<<<<<<<<<<<<<
 *         return np.max((mean_t2t1,mean_t1t2))
 *     else :
 */
  __pyx_t_11 = PyObject_RichCompare(__pyx_v_metric, ((PyObject *)__pyx_n_s__max), Py_EQ); __Pyx_XGOTREF(__pyx_t_11); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 774; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  if (__pyx_t_14) {

    /* "dipy/tracking/distances.pyx":775
 *         return np.min((mean_t2t1,mean_t1t2))
 *     elif metric =='max':
 *         return np.max((mean_t2t1,mean_t1t2))             # <<<<<<<<<<<<<<
 *     else :
 *         ValueError('Wrong argument for metric')
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s__max); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
    __pyx_t_11 = PyFloat_FromDouble(__pyx_v_mean_t2t1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_t_15 = PyFloat_FromDouble(__pyx_v_mean_t1t2); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_11);
    __Pyx_GIVEREF(__pyx_t_11);
    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_15);
    __Pyx_GIVEREF(__pyx_t_15);
    __pyx_t_11 = 0;
    __pyx_t_15 = 0;
    __pyx_t_15 = PyTuple_New(1); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    PyTuple_SET_ITEM(__pyx_t_15, 0, ((PyObject *)__pyx_t_4));
    __Pyx_GIVEREF(((PyObject *)__pyx_t_4));
    __pyx_t_4 = 0;
    __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_15), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_15)); __pyx_t_15 = 0;
    __pyx_r = __pyx_t_4;
    __pyx_t_4 = 0;
    goto __pyx_L0;
    goto __pyx_L7;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":777
 *         return np.max((mean_t2t1,mean_t1t2))
 *     else :
 *         ValueError('Wrong argument for metric')             # <<<<<<<<<<<<<<
 * 
 * 
 */
    __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_13), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  }
  __pyx_L7:;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_11);
  __Pyx_XDECREF(__pyx_t_15);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.mam_distances", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_track1);
  __Pyx_XDECREF((PyObject *)__pyx_v_track2);
  __Pyx_XDECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_25minimum_closest_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_24minimum_closest_distance[] = " Find the minimum distance between two curves xyz1, xyz2\n\n    Parameters\n    ----------\n    xyz1 : array, shape (N1,3), dtype float32\n    xyz2 : array, shape (N2,3), dtype float32\n        arrays representing x,y,z of the N1 and N2 points  of two tracks\n\n    Returns\n    -------\n    md : minimum distance\n\n    Notes\n    -----\n    Algorithmic description\n\n    Lets say we have curves A and B\n\n    for every point in A calculate the minimum distance from every point in B stored in minAB\n    for every point in B calculate the minimum distance from every point in A stored in minBA\n    find min of minAB stored in min_minAB\n    find min of minBA stored in min_minBA\n\n    Then return (min_minAB + min_minBA)/2.0\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_25minimum_closest_distance = {__Pyx_NAMESTR("minimum_closest_distance"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_25minimum_closest_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_24minimum_closest_distance)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_25minimum_closest_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz1 = 0;
  PyObject *__pyx_v_xyz2 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("minimum_closest_distance (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz1,&__pyx_n_s__xyz2,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("minimum_closest_distance", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "minimum_closest_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_xyz1 = values[0];
    __pyx_v_xyz2 = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("minimum_closest_distance", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.minimum_closest_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_24minimum_closest_distance(__pyx_self, __pyx_v_xyz1, __pyx_v_xyz2);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":780
 * 
 * 
 * def minimum_closest_distance(xyz1,xyz2):             # <<<<<<<<<<<<<<
 *     ''' Find the minimum distance between two curves xyz1, xyz2
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_24minimum_closest_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz1, PyObject *__pyx_v_xyz2) {
  PyArrayObject *__pyx_v_track1 = 0;
  PyArrayObject *__pyx_v_track2 = 0;
  size_t __pyx_v_t1_len;
  size_t __pyx_v_t2_len;
  __pyx_t_5numpy_float32_t *__pyx_v_min_t2t1;
  __pyx_t_5numpy_float32_t *__pyx_v_min_t1t2;
  PyArrayObject *__pyx_v_distances_buffer = 0;
  size_t __pyx_v_t1_pi;
  size_t __pyx_v_t2_pi;
  double __pyx_v_min_min_t2t1;
  double __pyx_v_min_min_t1t2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_distances_buffer;
  __Pyx_Buffer __pyx_pybuffer_distances_buffer;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track1;
  __Pyx_Buffer __pyx_pybuffer_track1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track2;
  __Pyx_Buffer __pyx_pybuffer_track2;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyArrayObject *__pyx_t_5 = NULL;
  int __pyx_t_6;
  PyObject *__pyx_t_7 = NULL;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  PyArrayObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyArrayObject *__pyx_t_12 = NULL;
  size_t __pyx_t_13;
  size_t __pyx_t_14;
  int __pyx_t_15;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("minimum_closest_distance", 0);
  __pyx_pybuffer_track1.pybuffer.buf = NULL;
  __pyx_pybuffer_track1.refcount = 0;
  __pyx_pybuffernd_track1.data = NULL;
  __pyx_pybuffernd_track1.rcbuffer = &__pyx_pybuffer_track1;
  __pyx_pybuffer_track2.pybuffer.buf = NULL;
  __pyx_pybuffer_track2.refcount = 0;
  __pyx_pybuffernd_track2.data = NULL;
  __pyx_pybuffernd_track2.rcbuffer = &__pyx_pybuffer_track2;
  __pyx_pybuffer_distances_buffer.pybuffer.buf = NULL;
  __pyx_pybuffer_distances_buffer.refcount = 0;
  __pyx_pybuffernd_distances_buffer.data = NULL;
  __pyx_pybuffernd_distances_buffer.rcbuffer = &__pyx_pybuffer_distances_buffer;

  /* "dipy/tracking/distances.pyx":810
 *         cnp.ndarray[cnp.float32_t, ndim=2] track2
 *         size_t t1_len, t2_len
 *     track1 = np.ascontiguousarray(xyz1, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t1_len = track1.shape[0]
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_v_xyz1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_xyz1);
  __Pyx_GIVEREF(__pyx_v_xyz1);
  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track1.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track1.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track1.rcbuffer->pybuffer, (PyObject*)__pyx_v_track1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_track1.diminfo[0].strides = __pyx_pybuffernd_track1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track1.diminfo[0].shape = __pyx_pybuffernd_track1.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track1.diminfo[1].strides = __pyx_pybuffernd_track1.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track1.diminfo[1].shape = __pyx_pybuffernd_track1.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 810; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __pyx_v_track1 = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":811
 *         size_t t1_len, t2_len
 *     track1 = np.ascontiguousarray(xyz1, dtype=f32_dt)
 *     t1_len = track1.shape[0]             # <<<<<<<<<<<<<<
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)
 *     t2_len = track2.shape[0]
 */
  __pyx_v_t1_len = (__pyx_v_track1->dimensions[0]);

  /* "dipy/tracking/distances.pyx":812
 *     track1 = np.ascontiguousarray(xyz1, dtype=f32_dt)
 *     t1_len = track1.shape[0]
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t2_len = track2.shape[0]
 *     # preallocate buffer array for track distance calculations
 */
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(__pyx_v_xyz2);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_xyz2);
  __Pyx_GIVEREF(__pyx_v_xyz2);
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_4), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_10 = ((PyArrayObject *)__pyx_t_2);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track2.rcbuffer->pybuffer, (PyObject*)__pyx_t_10, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track2.rcbuffer->pybuffer, (PyObject*)__pyx_v_track2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_8, __pyx_t_7);
      }
    }
    __pyx_pybuffernd_track2.diminfo[0].strides = __pyx_pybuffernd_track2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track2.diminfo[0].shape = __pyx_pybuffernd_track2.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track2.diminfo[1].strides = __pyx_pybuffernd_track2.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track2.diminfo[1].shape = __pyx_pybuffernd_track2.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_10 = 0;
  __pyx_v_track2 = ((PyArrayObject *)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "dipy/tracking/distances.pyx":813
 *     t1_len = track1.shape[0]
 *     track2 = np.ascontiguousarray(xyz2, dtype=f32_dt)
 *     t2_len = track2.shape[0]             # <<<<<<<<<<<<<<
 *     # preallocate buffer array for track distance calculations
 *     cdef:
 */
  __pyx_v_t2_len = (__pyx_v_track2->dimensions[0]);

  /* "dipy/tracking/distances.pyx":818
 *         cnp.float32_t *min_t2t1, *min_t1t2
 *         cnp.ndarray [cnp.float32_t, ndim=1] distances_buffer
 *     distances_buffer = np.zeros((t1_len + t2_len,), dtype=np.float32)             # <<<<<<<<<<<<<<
 *     min_t2t1 = <cnp.float32_t *> distances_buffer.data
 *     min_t1t2 = min_t2t1 + t2_len
 */
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = __Pyx_PyInt_FromSize_t((__pyx_v_t1_len + __pyx_v_t2_len)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
  __Pyx_GIVEREF(__pyx_t_2);
  __pyx_t_2 = 0;
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_t_4));
  __Pyx_GIVEREF(((PyObject *)__pyx_t_4));
  __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_11) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  __pyx_t_11 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_11);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_t_12, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer, (PyObject*)__pyx_v_distances_buffer, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_distances_buffer.diminfo[0].strides = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_distances_buffer.diminfo[0].shape = __pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "dipy/tracking/distances.pyx":819
 *         cnp.ndarray [cnp.float32_t, ndim=1] distances_buffer
 *     distances_buffer = np.zeros((t1_len + t2_len,), dtype=np.float32)
 *     min_t2t1 = <cnp.float32_t *> distances_buffer.data             # <<<<<<<<<<<<<<
 *     min_t1t2 = min_t2t1 + t2_len
 *     min_distances(t1_len, <cnp.float32_t *>track1.data,
 */
  __pyx_v_min_t2t1 = ((__pyx_t_5numpy_float32_t *)__pyx_v_distances_buffer->data);

  /* "dipy/tracking/distances.pyx":820
 *     distances_buffer = np.zeros((t1_len + t2_len,), dtype=np.float32)
 *     min_t2t1 = <cnp.float32_t *> distances_buffer.data
 *     min_t1t2 = min_t2t1 + t2_len             # <<<<<<<<<<<<<<
 *     min_distances(t1_len, <cnp.float32_t *>track1.data,
 *                   t2_len, <cnp.float32_t *>track2.data,
 */
  __pyx_v_min_t1t2 = (__pyx_v_min_t2t1 + __pyx_v_t2_len);

  /* "dipy/tracking/distances.pyx":824
 *                   t2_len, <cnp.float32_t *>track2.data,
 *                   min_t2t1,
 *                   min_t1t2)             # <<<<<<<<<<<<<<
 *     cdef:
 *         size_t t1_pi, t2_pi
 */
  __pyx_f_4dipy_8tracking_9distances_min_distances(__pyx_v_t1_len, ((__pyx_t_5numpy_float32_t *)__pyx_v_track1->data), __pyx_v_t2_len, ((__pyx_t_5numpy_float32_t *)__pyx_v_track2->data), __pyx_v_min_t2t1, __pyx_v_min_t1t2);

  /* "dipy/tracking/distances.pyx":827
 *     cdef:
 *         size_t t1_pi, t2_pi
 *         double min_min_t2t1 = inf             # <<<<<<<<<<<<<<
 *         double min_min_t1t2 = inf
 *     for t1_pi in range(t1_len):
 */
  __pyx_v_min_min_t2t1 = __pyx_v_4dipy_8tracking_9distances_inf;

  /* "dipy/tracking/distances.pyx":828
 *         size_t t1_pi, t2_pi
 *         double min_min_t2t1 = inf
 *         double min_min_t1t2 = inf             # <<<<<<<<<<<<<<
 *     for t1_pi in range(t1_len):
 *         if min_min_t1t2 > min_t1t2[t1_pi]:
 */
  __pyx_v_min_min_t1t2 = __pyx_v_4dipy_8tracking_9distances_inf;

  /* "dipy/tracking/distances.pyx":829
 *         double min_min_t2t1 = inf
 *         double min_min_t1t2 = inf
 *     for t1_pi in range(t1_len):             # <<<<<<<<<<<<<<
 *         if min_min_t1t2 > min_t1t2[t1_pi]:
 *             min_min_t1t2 = min_t1t2[t1_pi]
 */
  __pyx_t_13 = __pyx_v_t1_len;
  for (__pyx_t_14 = 0; __pyx_t_14 < __pyx_t_13; __pyx_t_14+=1) {
    __pyx_v_t1_pi = __pyx_t_14;

    /* "dipy/tracking/distances.pyx":830
 *         double min_min_t1t2 = inf
 *     for t1_pi in range(t1_len):
 *         if min_min_t1t2 > min_t1t2[t1_pi]:             # <<<<<<<<<<<<<<
 *             min_min_t1t2 = min_t1t2[t1_pi]
 *     for t2_pi in range(t2_len):
 */
    __pyx_t_15 = ((__pyx_v_min_min_t1t2 > (__pyx_v_min_t1t2[__pyx_v_t1_pi])) != 0);
    if (__pyx_t_15) {

      /* "dipy/tracking/distances.pyx":831
 *     for t1_pi in range(t1_len):
 *         if min_min_t1t2 > min_t1t2[t1_pi]:
 *             min_min_t1t2 = min_t1t2[t1_pi]             # <<<<<<<<<<<<<<
 *     for t2_pi in range(t2_len):
 *         if min_min_t2t1 > min_t2t1[t2_pi]:
 */
      __pyx_v_min_min_t1t2 = (__pyx_v_min_t1t2[__pyx_v_t1_pi]);
      goto __pyx_L5;
    }
    __pyx_L5:;
  }

  /* "dipy/tracking/distances.pyx":832
 *         if min_min_t1t2 > min_t1t2[t1_pi]:
 *             min_min_t1t2 = min_t1t2[t1_pi]
 *     for t2_pi in range(t2_len):             # <<<<<<<<<<<<<<
 *         if min_min_t2t1 > min_t2t1[t2_pi]:
 *             min_min_t2t1 = min_t2t1[t2_pi]
 */
  __pyx_t_13 = __pyx_v_t2_len;
  for (__pyx_t_14 = 0; __pyx_t_14 < __pyx_t_13; __pyx_t_14+=1) {
    __pyx_v_t2_pi = __pyx_t_14;

    /* "dipy/tracking/distances.pyx":833
 *             min_min_t1t2 = min_t1t2[t1_pi]
 *     for t2_pi in range(t2_len):
 *         if min_min_t2t1 > min_t2t1[t2_pi]:             # <<<<<<<<<<<<<<
 *             min_min_t2t1 = min_t2t1[t2_pi]
 *     return (min_min_t1t2+min_min_t2t1)/2.0
 */
    __pyx_t_15 = ((__pyx_v_min_min_t2t1 > (__pyx_v_min_t2t1[__pyx_v_t2_pi])) != 0);
    if (__pyx_t_15) {

      /* "dipy/tracking/distances.pyx":834
 *     for t2_pi in range(t2_len):
 *         if min_min_t2t1 > min_t2t1[t2_pi]:
 *             min_min_t2t1 = min_t2t1[t2_pi]             # <<<<<<<<<<<<<<
 *     return (min_min_t1t2+min_min_t2t1)/2.0
 * 
 */
      __pyx_v_min_min_t2t1 = (__pyx_v_min_t2t1[__pyx_v_t2_pi]);
      goto __pyx_L8;
    }
    __pyx_L8:;
  }

  /* "dipy/tracking/distances.pyx":835
 *         if min_min_t2t1 > min_t2t1[t2_pi]:
 *             min_min_t2t1 = min_t2t1[t2_pi]
 *     return (min_min_t1t2+min_min_t2t1)/2.0             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_11 = PyFloat_FromDouble(((__pyx_v_min_min_t1t2 + __pyx_v_min_min_t2t1) / 2.0)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __pyx_r = __pyx_t_11;
  __pyx_t_11 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_11);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.minimum_closest_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_distances_buffer.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track2.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_track1);
  __Pyx_XDECREF((PyObject *)__pyx_v_track2);
  __Pyx_XDECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_27lee_perpendicular_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_26lee_perpendicular_distance[] = " Calculates perpendicular distance metric for the distance between two line segments\n\n    Based on Lee , Han & Whang SIGMOD07.\n\n    This function assumes that norm(end0-start0)>norm(end1-start1) i.e. that the\n    first segment will be bigger than the second one.\n\n    Parameters\n    ----------\n    start0 : float array(3,)\n    end0 : float array(3,)\n    start1 : float array(3,)\n    end1 : float array(3,)\n\n    Returns\n    -------\n    perpendicular_distance: float\n\n    Notes\n    -----\n    l0 = np.inner(end0-start0,end0-start0)\n    l1 = np.inner(end1-start1,end1-start1)\n\n    k0=end0-start0\n\n    u1 = np.inner(start1-start0,k0)/l0\n    u2 = np.inner(end1-start0,k0)/l0\n\n    ps = start0+u1*k0\n    pe = start0+u2*k0\n\n    lperp1 = np.sqrt(np.inner(ps-start1,ps-start1))\n    lperp2 = np.sqrt(np.inner(pe-end1,pe-end1))\n\n    if lperp1+lperp2 > 0.:\n        return (lperp1**2+lperp2**2)/(lperp1+lperp2)\n    else:\n        return 0.\n\n    Examples\n    --------\n    >>> d = lee_perpendicular_distance([0,0,0],[1,0,0],[3,4,5],[5,4,3])\n    >>> print('%.6f' % d)\n    5.787888\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_27lee_perpendicular_distance = {__Pyx_NAMESTR("lee_perpendicular_distance"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_27lee_perpendicular_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_26lee_perpendicular_distance)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_27lee_perpendicular_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_start0 = 0;
  PyObject *__pyx_v_end0 = 0;
  PyObject *__pyx_v_start1 = 0;
  PyObject *__pyx_v_end1 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("lee_perpendicular_distance (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__start0,&__pyx_n_s__end0,&__pyx_n_s__start1,&__pyx_n_s__end1,0};
    PyObject* values[4] = {0,0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start0)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end0)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start1)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  3:
        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end1)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "lee_perpendicular_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
      values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
    }
    __pyx_v_start0 = values[0];
    __pyx_v_end0 = values[1];
    __pyx_v_start1 = values[2];
    __pyx_v_end1 = values[3];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.lee_perpendicular_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_26lee_perpendicular_distance(__pyx_self, __pyx_v_start0, __pyx_v_end0, __pyx_v_start1, __pyx_v_end1);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":838
 * 
 * 
 * def lee_perpendicular_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Calculates perpendicular distance metric for the distance between two line segments
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_26lee_perpendicular_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_start0, PyObject *__pyx_v_end0, PyObject *__pyx_v_start1, PyObject *__pyx_v_end1) {
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_fvec3 = 0;
  PyArrayObject *__pyx_v_fvec4 = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec3;
  __Pyx_Buffer __pyx_pybuffer_fvec3;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec4;
  __Pyx_Buffer __pyx_pybuffer_fvec4;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("lee_perpendicular_distance", 0);
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;
  __pyx_pybuffer_fvec3.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec3.refcount = 0;
  __pyx_pybuffernd_fvec3.data = NULL;
  __pyx_pybuffernd_fvec3.rcbuffer = &__pyx_pybuffer_fvec3;
  __pyx_pybuffer_fvec4.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec4.refcount = 0;
  __pyx_pybuffernd_fvec4.data = NULL;
  __pyx_pybuffernd_fvec4.rcbuffer = &__pyx_pybuffer_fvec4;

  /* "dipy/tracking/distances.pyx":887
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1,fvec2,fvec3,fvec4
 * 
 *     fvec1 = as_float_3vec(start0)             # <<<<<<<<<<<<<<
 *     fvec2 = as_float_3vec(end0)
 *     fvec3 = as_float_3vec(start1)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_start0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_3, &__pyx_t_4, &__pyx_t_5);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_3); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_5);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_3, __pyx_t_4, __pyx_t_5);
      }
    }
    __pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 887; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":888
 * 
 *     fvec1 = as_float_3vec(start0)
 *     fvec2 = as_float_3vec(end0)             # <<<<<<<<<<<<<<
 *     fvec3 = as_float_3vec(start1)
 *     fvec4 = as_float_3vec(end1)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_end0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_5, &__pyx_t_4, &__pyx_t_3);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_5); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_3);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_5, __pyx_t_4, __pyx_t_3);
      }
    }
    __pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 888; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":889
 *     fvec1 = as_float_3vec(start0)
 *     fvec2 = as_float_3vec(end0)
 *     fvec3 = as_float_3vec(start1)             # <<<<<<<<<<<<<<
 *     fvec4 = as_float_3vec(end1)
 * 
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_start1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_3, &__pyx_t_4, &__pyx_t_5);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec3, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_3); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_5);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_3, __pyx_t_4, __pyx_t_5);
      }
    }
    __pyx_pybuffernd_fvec3.diminfo[0].strides = __pyx_pybuffernd_fvec3.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec3.diminfo[0].shape = __pyx_pybuffernd_fvec3.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec3 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":890
 *     fvec2 = as_float_3vec(end0)
 *     fvec3 = as_float_3vec(start1)
 *     fvec4 = as_float_3vec(end1)             # <<<<<<<<<<<<<<
 * 
 *     return clee_perpendicular_distance(<float *>fvec1.data,<float *>fvec2.data,<float *>fvec3.data,<float *>fvec4.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_end1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_5, &__pyx_t_4, &__pyx_t_3);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_5); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_3);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_5, __pyx_t_4, __pyx_t_3);
      }
    }
    __pyx_pybuffernd_fvec4.diminfo[0].strides = __pyx_pybuffernd_fvec4.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec4.diminfo[0].shape = __pyx_pybuffernd_fvec4.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 890; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec4 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":892
 *     fvec4 = as_float_3vec(end1)
 * 
 *     return clee_perpendicular_distance(<float *>fvec1.data,<float *>fvec2.data,<float *>fvec3.data,<float *>fvec4.data)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyFloat_FromDouble(__pyx_f_4dipy_8tracking_9distances_clee_perpendicular_distance(((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data), ((float *)__pyx_v_fvec3->data), ((float *)__pyx_v_fvec4->data))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 892; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.lee_perpendicular_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec3);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec4);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":895
 * 
 * 
 * cdef float clee_perpendicular_distance(float *start0, float *end0,float *start1, float *end1):             # <<<<<<<<<<<<<<
 *     ''' This function assumes that norm(end0-start0)>norm(end1-start1)
 *     '''
 */

static float __pyx_f_4dipy_8tracking_9distances_clee_perpendicular_distance(float *__pyx_v_start0, float *__pyx_v_end0, float *__pyx_v_start1, float *__pyx_v_end1) {
  float __pyx_v_l0;
  CYTHON_UNUSED float __pyx_v_l1;
  float __pyx_v_u1;
  float __pyx_v_u2;
  float __pyx_v_lperp1;
  float __pyx_v_lperp2;
  float __pyx_v_k0[3];
  float __pyx_v_ps[3];
  float __pyx_v_pe[3];
  float __pyx_v_ps1[3];
  float __pyx_v_pe1[3];
  float __pyx_v_tmp[3];
  float __pyx_r;
  __Pyx_RefNannyDeclarations
  float __pyx_t_1;
  int __pyx_t_2;
  float __pyx_t_3;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("clee_perpendicular_distance", 0);

  /* "dipy/tracking/distances.pyx":903
 *         float *s_tmp,*e_tmp,k0[3],ps[3],pe[3],ps1[3],pe1[3],tmp[3]
 * 
 *     csub_3vecs(end0,start0,k0)             # <<<<<<<<<<<<<<
 *     l0 = cinner_3vecs(k0,k0)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_end0, __pyx_v_start0, __pyx_v_k0);

  /* "dipy/tracking/distances.pyx":904
 * 
 *     csub_3vecs(end0,start0,k0)
 *     l0 = cinner_3vecs(k0,k0)             # <<<<<<<<<<<<<<
 * 
 *     csub_3vecs(end1,start1,tmp)
 */
  __pyx_v_l0 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_k0, __pyx_v_k0);

  /* "dipy/tracking/distances.pyx":906
 *     l0 = cinner_3vecs(k0,k0)
 * 
 *     csub_3vecs(end1,start1,tmp)             # <<<<<<<<<<<<<<
 *     l1 = cinner_3vecs(tmp, tmp)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_end1, __pyx_v_start1, __pyx_v_tmp);

  /* "dipy/tracking/distances.pyx":907
 * 
 *     csub_3vecs(end1,start1,tmp)
 *     l1 = cinner_3vecs(tmp, tmp)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_v_l1 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_tmp, __pyx_v_tmp);

  /* "dipy/tracking/distances.pyx":914
 *     #u1 = np.inner(start1-start0,k0)/l0
 *     #u2 = np.inner(end1-start0,k0)/l0
 *     csub_3vecs(start1,start0,tmp)             # <<<<<<<<<<<<<<
 *     u1 = cinner_3vecs(tmp,k0)/l0
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_start1, __pyx_v_start0, __pyx_v_tmp);

  /* "dipy/tracking/distances.pyx":915
 *     #u2 = np.inner(end1-start0,k0)/l0
 *     csub_3vecs(start1,start0,tmp)
 *     u1 = cinner_3vecs(tmp,k0)/l0             # <<<<<<<<<<<<<<
 * 
 *     csub_3vecs(end1,start0,tmp)
 */
  __pyx_t_1 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_tmp, __pyx_v_k0);
  if (unlikely(__pyx_v_l0 == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_u1 = (__pyx_t_1 / __pyx_v_l0);

  /* "dipy/tracking/distances.pyx":917
 *     u1 = cinner_3vecs(tmp,k0)/l0
 * 
 *     csub_3vecs(end1,start0,tmp)             # <<<<<<<<<<<<<<
 *     u2 = cinner_3vecs(tmp,k0)/l0
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_end1, __pyx_v_start0, __pyx_v_tmp);

  /* "dipy/tracking/distances.pyx":918
 * 
 *     csub_3vecs(end1,start0,tmp)
 *     u2 = cinner_3vecs(tmp,k0)/l0             # <<<<<<<<<<<<<<
 * 
 *     cmul_3vec(u1,k0,tmp)
 */
  __pyx_t_1 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_tmp, __pyx_v_k0);
  if (unlikely(__pyx_v_l0 == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_u2 = (__pyx_t_1 / __pyx_v_l0);

  /* "dipy/tracking/distances.pyx":920
 *     u2 = cinner_3vecs(tmp,k0)/l0
 * 
 *     cmul_3vec(u1,k0,tmp)             # <<<<<<<<<<<<<<
 *     cadd_3vecs(start0,tmp,ps)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_cmul_3vec(__pyx_v_u1, __pyx_v_k0, __pyx_v_tmp);

  /* "dipy/tracking/distances.pyx":921
 * 
 *     cmul_3vec(u1,k0,tmp)
 *     cadd_3vecs(start0,tmp,ps)             # <<<<<<<<<<<<<<
 * 
 *     cmul_3vec(u2,k0,tmp)
 */
  __pyx_f_4dipy_8tracking_9distances_cadd_3vecs(__pyx_v_start0, __pyx_v_tmp, __pyx_v_ps);

  /* "dipy/tracking/distances.pyx":923
 *     cadd_3vecs(start0,tmp,ps)
 * 
 *     cmul_3vec(u2,k0,tmp)             # <<<<<<<<<<<<<<
 *     cadd_3vecs(start0,tmp,pe)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_cmul_3vec(__pyx_v_u2, __pyx_v_k0, __pyx_v_tmp);

  /* "dipy/tracking/distances.pyx":924
 * 
 *     cmul_3vec(u2,k0,tmp)
 *     cadd_3vecs(start0,tmp,pe)             # <<<<<<<<<<<<<<
 * 
 *     #lperp1 = np.sqrt(np.inner(ps-start1,ps-start1))
 */
  __pyx_f_4dipy_8tracking_9distances_cadd_3vecs(__pyx_v_start0, __pyx_v_tmp, __pyx_v_pe);

  /* "dipy/tracking/distances.pyx":929
 *     #lperp2 = np.sqrt(np.inner(pe-end1,pe-end1))
 * 
 *     csub_3vecs(ps,start1,ps1)             # <<<<<<<<<<<<<<
 *     csub_3vecs(pe,end1,pe1)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_ps, __pyx_v_start1, __pyx_v_ps1);

  /* "dipy/tracking/distances.pyx":930
 * 
 *     csub_3vecs(ps,start1,ps1)
 *     csub_3vecs(pe,end1,pe1)             # <<<<<<<<<<<<<<
 * 
 *     lperp1 = sqrt(cinner_3vecs(ps1,ps1))
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_pe, __pyx_v_end1, __pyx_v_pe1);

  /* "dipy/tracking/distances.pyx":932
 *     csub_3vecs(pe,end1,pe1)
 * 
 *     lperp1 = sqrt(cinner_3vecs(ps1,ps1))             # <<<<<<<<<<<<<<
 *     lperp2 = sqrt(cinner_3vecs(pe1,pe1))
 * 
 */
  __pyx_v_lperp1 = sqrt(__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_ps1, __pyx_v_ps1));

  /* "dipy/tracking/distances.pyx":933
 * 
 *     lperp1 = sqrt(cinner_3vecs(ps1,ps1))
 *     lperp2 = sqrt(cinner_3vecs(pe1,pe1))             # <<<<<<<<<<<<<<
 * 
 *     if lperp1+lperp2 > 0.:
 */
  __pyx_v_lperp2 = sqrt(__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_pe1, __pyx_v_pe1));

  /* "dipy/tracking/distances.pyx":935
 *     lperp2 = sqrt(cinner_3vecs(pe1,pe1))
 * 
 *     if lperp1+lperp2 > 0.:             # <<<<<<<<<<<<<<
 *         return (lperp1*lperp1+lperp2*lperp2)/(lperp1+lperp2)
 *     else:
 */
  __pyx_t_2 = (((__pyx_v_lperp1 + __pyx_v_lperp2) > 0.) != 0);
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":936
 * 
 *     if lperp1+lperp2 > 0.:
 *         return (lperp1*lperp1+lperp2*lperp2)/(lperp1+lperp2)             # <<<<<<<<<<<<<<
 *     else:
 *         return 0.
 */
    __pyx_t_1 = ((__pyx_v_lperp1 * __pyx_v_lperp1) + (__pyx_v_lperp2 * __pyx_v_lperp2));
    __pyx_t_3 = (__pyx_v_lperp1 + __pyx_v_lperp2);
    if (unlikely(__pyx_t_3 == 0)) {
      #ifdef WITH_THREAD
      PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
      #endif
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      #ifdef WITH_THREAD
      PyGILState_Release(__pyx_gilstate_save);
      #endif
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 936; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_r = (__pyx_t_1 / __pyx_t_3);
    goto __pyx_L0;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":938
 *         return (lperp1*lperp1+lperp2*lperp2)/(lperp1+lperp2)
 *     else:
 *         return 0.             # <<<<<<<<<<<<<<
 * 
 * 
 */
    __pyx_r = 0.;
    goto __pyx_L0;
  }
  __pyx_L3:;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_WriteUnraisable("dipy.tracking.distances.clee_perpendicular_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_29lee_angle_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_28lee_angle_distance[] = " Calculates angle distance metric for the distance between two line segments\n\n    Based on Lee , Han & Whang SIGMOD07.\n\n    This function assumes that norm(end0-start0)>norm(end1-start1) i.e. that the\n    first segment will be bigger than the second one.\n\n    Parameters\n    ----------\n    start0 : float array(3,)\n    end0 : float array(3,)\n    start1 : float array(3,)\n    end1 : float array(3,)\n\n    Returns\n    -------\n    angle_distance : float\n\n    Notes\n    -----\n    l_0 = np.inner(end0-start0,end0-start0)\n    l_1 = np.inner(end1-start1,end1-start1)\n\n    cos_theta_squared = np.inner(end0-start0,end1-start1)**2/ (l_0*l_1)\n    return np.sqrt((1-cos_theta_squared)*l_1)\n\n    Examples\n    --------\n    >>> lee_angle_distance([0,0,0],[1,0,0],[3,4,5],[5,4,3])\n    2.0\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_29lee_angle_distance = {__Pyx_NAMESTR("lee_angle_distance"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_29lee_angle_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_28lee_angle_distance)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_29lee_angle_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_start0 = 0;
  PyObject *__pyx_v_end0 = 0;
  PyObject *__pyx_v_start1 = 0;
  PyObject *__pyx_v_end1 = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("lee_angle_distance (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__start0,&__pyx_n_s__end0,&__pyx_n_s__start1,&__pyx_n_s__end1,0};
    PyObject* values[4] = {0,0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start0)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end0)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start1)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  3:
        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end1)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "lee_angle_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
      values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
    }
    __pyx_v_start0 = values[0];
    __pyx_v_end0 = values[1];
    __pyx_v_start1 = values[2];
    __pyx_v_end1 = values[3];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.lee_angle_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_28lee_angle_distance(__pyx_self, __pyx_v_start0, __pyx_v_end0, __pyx_v_start1, __pyx_v_end1);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":941
 * 
 * 
 * def lee_angle_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Calculates angle distance metric for the distance between two line segments
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_28lee_angle_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_start0, PyObject *__pyx_v_end0, PyObject *__pyx_v_start1, PyObject *__pyx_v_end1) {
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_fvec3 = 0;
  PyArrayObject *__pyx_v_fvec4 = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec3;
  __Pyx_Buffer __pyx_pybuffer_fvec3;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec4;
  __Pyx_Buffer __pyx_pybuffer_fvec4;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("lee_angle_distance", 0);
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;
  __pyx_pybuffer_fvec3.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec3.refcount = 0;
  __pyx_pybuffernd_fvec3.data = NULL;
  __pyx_pybuffernd_fvec3.rcbuffer = &__pyx_pybuffer_fvec3;
  __pyx_pybuffer_fvec4.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec4.refcount = 0;
  __pyx_pybuffernd_fvec4.data = NULL;
  __pyx_pybuffernd_fvec4.rcbuffer = &__pyx_pybuffer_fvec4;

  /* "dipy/tracking/distances.pyx":976
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1,fvec2,fvec3,fvec4
 * 
 *     fvec1 = as_float_3vec(start0)             # <<<<<<<<<<<<<<
 *     fvec2 = as_float_3vec(end0)
 *     fvec3 = as_float_3vec(start1)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_start0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_3, &__pyx_t_4, &__pyx_t_5);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_3); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_5);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_3, __pyx_t_4, __pyx_t_5);
      }
    }
    __pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 976; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":977
 * 
 *     fvec1 = as_float_3vec(start0)
 *     fvec2 = as_float_3vec(end0)             # <<<<<<<<<<<<<<
 *     fvec3 = as_float_3vec(start1)
 *     fvec4 = as_float_3vec(end1)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_end0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_5, &__pyx_t_4, &__pyx_t_3);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_5); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_3);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_5, __pyx_t_4, __pyx_t_3);
      }
    }
    __pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 977; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":978
 *     fvec1 = as_float_3vec(start0)
 *     fvec2 = as_float_3vec(end0)
 *     fvec3 = as_float_3vec(start1)             # <<<<<<<<<<<<<<
 *     fvec4 = as_float_3vec(end1)
 * 
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_start1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_3, &__pyx_t_4, &__pyx_t_5);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec3, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_3); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_5);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_3, __pyx_t_4, __pyx_t_5);
      }
    }
    __pyx_pybuffernd_fvec3.diminfo[0].strides = __pyx_pybuffernd_fvec3.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec3.diminfo[0].shape = __pyx_pybuffernd_fvec3.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 978; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec3 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":979
 *     fvec2 = as_float_3vec(end0)
 *     fvec3 = as_float_3vec(start1)
 *     fvec4 = as_float_3vec(end1)             # <<<<<<<<<<<<<<
 * 
 *     return clee_angle_distance(<float *>fvec1.data,<float *>fvec2.data,<float *>fvec3.data,<float *>fvec4.data)
 */
  __pyx_t_1 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_v_end1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_1), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
    if (unlikely(__pyx_t_2 < 0)) {
      PyErr_Fetch(&__pyx_t_5, &__pyx_t_4, &__pyx_t_3);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_5); Py_XDECREF(__pyx_t_4); Py_XDECREF(__pyx_t_3);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_5, __pyx_t_4, __pyx_t_3);
      }
    }
    __pyx_pybuffernd_fvec4.diminfo[0].strides = __pyx_pybuffernd_fvec4.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec4.diminfo[0].shape = __pyx_pybuffernd_fvec4.rcbuffer->pybuffer.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 979; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_fvec4 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":981
 *     fvec4 = as_float_3vec(end1)
 * 
 *     return clee_angle_distance(<float *>fvec1.data,<float *>fvec2.data,<float *>fvec3.data,<float *>fvec4.data)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyFloat_FromDouble(__pyx_f_4dipy_8tracking_9distances_clee_angle_distance(((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data), ((float *)__pyx_v_fvec3->data), ((float *)__pyx_v_fvec4->data))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 981; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.lee_angle_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec3);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec4);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":984
 * 
 * 
 * cdef float clee_angle_distance(float *start0, float *end0,float *start1, float *end1):             # <<<<<<<<<<<<<<
 *     ''' This function assumes that norm(end0-start0)>norm(end1-start1)
 *     '''
 */

static float __pyx_f_4dipy_8tracking_9distances_clee_angle_distance(float *__pyx_v_start0, float *__pyx_v_end0, float *__pyx_v_start1, float *__pyx_v_end1) {
  float __pyx_v_l0;
  float __pyx_v_l1;
  float __pyx_v_ltmp;
  float __pyx_v_cos_theta_squared;
  float __pyx_v_k0[3];
  float __pyx_v_k1[3];
  float __pyx_r;
  __Pyx_RefNannyDeclarations
  float __pyx_t_1;
  float __pyx_t_2;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("clee_angle_distance", 0);

  /* "dipy/tracking/distances.pyx":992
 *         float *s_tmp,*e_tmp,k0[3],k1[3],tmp[3]
 * 
 *     csub_3vecs(end0,start0,k0)             # <<<<<<<<<<<<<<
 *     l0 = cinner_3vecs(k0,k0)
 *     #print l0
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_end0, __pyx_v_start0, __pyx_v_k0);

  /* "dipy/tracking/distances.pyx":993
 * 
 *     csub_3vecs(end0,start0,k0)
 *     l0 = cinner_3vecs(k0,k0)             # <<<<<<<<<<<<<<
 *     #print l0
 * 
 */
  __pyx_v_l0 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_k0, __pyx_v_k0);

  /* "dipy/tracking/distances.pyx":996
 *     #print l0
 * 
 *     csub_3vecs(end1,start1,k1)             # <<<<<<<<<<<<<<
 *     l1 = cinner_3vecs(k1, k1)
 *     #print l1
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_end1, __pyx_v_start1, __pyx_v_k1);

  /* "dipy/tracking/distances.pyx":997
 * 
 *     csub_3vecs(end1,start1,k1)
 *     l1 = cinner_3vecs(k1, k1)             # <<<<<<<<<<<<<<
 *     #print l1
 * 
 */
  __pyx_v_l1 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_k1, __pyx_v_k1);

  /* "dipy/tracking/distances.pyx":1000
 *     #print l1
 * 
 *     ltmp=cinner_3vecs(k0,k1)             # <<<<<<<<<<<<<<
 * 
 *     cos_theta_squared = (ltmp*ltmp)/ (l0*l1)
 */
  __pyx_v_ltmp = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_k0, __pyx_v_k1);

  /* "dipy/tracking/distances.pyx":1002
 *     ltmp=cinner_3vecs(k0,k1)
 * 
 *     cos_theta_squared = (ltmp*ltmp)/ (l0*l1)             # <<<<<<<<<<<<<<
 *     #print cos_theta_squared
 *     return sqrt((1-cos_theta_squared)*l1)
 */
  __pyx_t_1 = (__pyx_v_ltmp * __pyx_v_ltmp);
  __pyx_t_2 = (__pyx_v_l0 * __pyx_v_l1);
  if (unlikely(__pyx_t_2 == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1002; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_cos_theta_squared = (__pyx_t_1 / __pyx_t_2);

  /* "dipy/tracking/distances.pyx":1004
 *     cos_theta_squared = (ltmp*ltmp)/ (l0*l1)
 *     #print cos_theta_squared
 *     return sqrt((1-cos_theta_squared)*l1)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = sqrt(((1.0 - __pyx_v_cos_theta_squared) * __pyx_v_l1));
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_WriteUnraisable("dipy.tracking.distances.clee_angle_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_31approx_polygon_track(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_30approx_polygon_track[] = " Fast and simple trajectory approximation algorithm by Eleftherios and Ian\n\n    It will reduce the number of points of the track by keeping\n    intact the start and endpoints of the track and trying to remove\n    as many points as possible without distorting much the shape of\n    the track\n\n    Parameters\n    ----------\n    xyz : array(N,3)\n        initial trajectory\n    alpha : float\n        smoothing parameter (<0.392 smoother, >0.392 rougher) if the\n        trajectory was a smooth circle then with alpha =0.393\n        ~=pi/8. the circle would be approximated with an decahexagon if\n        alpha = 0.7853 ~=pi/4. with an octagon.\n\n    Returns\n    -------\n    characteristic_points: list of M array(3,) points\n\n    Examples\n    --------\n\n    Approximating a helix:\n\n    >>> t=np.linspace(0,1.75*2*np.pi,100)\n    >>> x = np.sin(t)\n    >>> y = np.cos(t)\n    >>> z = t\n    >>> xyz=np.vstack((x,y,z)).T\n    >>> xyza = approx_polygon_track(xyz)\n    >>> len(xyza) < len(xyz)\n    True\n\n    Notes\n    -----\n    Assuming that a good approximation for a circle is an octagon then\n    that means that the points of the octagon will have angle alpha =\n    2*pi/8 = pi/4 . We calculate the angle between every two neighbour\n    segments of a trajectory and if the angle is higher than pi/4 we\n    choose that point as a characteristic point otherwise we move at the\n    next point.\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_31approx_polygon_track = {__Pyx_NAMESTR("approx_polygon_track"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_31approx_polygon_track, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_30approx_polygon_track)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_31approx_polygon_track(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz = 0;
  PyObject *__pyx_v_alpha = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("approx_polygon_track (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz,&__pyx_n_s__alpha,0};
    PyObject* values[2] = {0,0};
    values[1] = __pyx_k_14;
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__alpha);
          if (value) { values[1] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "approx_polygon_track") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_xyz = values[0];
    __pyx_v_alpha = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("approx_polygon_track", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.approx_polygon_track", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_30approx_polygon_track(__pyx_self, __pyx_v_xyz, __pyx_v_alpha);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1007
 * 
 * 
 * def approx_polygon_track(xyz,alpha=0.392):             # <<<<<<<<<<<<<<
 *     ''' Fast and simple trajectory approximation algorithm by Eleftherios and Ian
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_30approx_polygon_track(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz, PyObject *__pyx_v_alpha) {
  int __pyx_v_mid_index;
  PyArrayObject *__pyx_v_track = 0;
  float *__pyx_v_fvec0;
  float *__pyx_v_fvec1;
  float *__pyx_v_fvec2;
  PyObject *__pyx_v_characteristic_points = 0;
  size_t __pyx_v_t_len;
  double __pyx_v_angle;
  double __pyx_v_tmp;
  float __pyx_v_vec0[3];
  float __pyx_v_vec1[3];
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  double __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyArrayObject *__pyx_t_6 = NULL;
  int __pyx_t_7;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  Py_ssize_t __pyx_t_11;
  int __pyx_t_12;
  long __pyx_t_13;
  float __pyx_t_14;
  float __pyx_t_15;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("approx_polygon_track", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;

  /* "dipy/tracking/distances.pyx":1061
 *         float vec0[3],vec1[3]
 * 
 *     angle=alpha             # <<<<<<<<<<<<<<
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)
 *     t_len=len(track)
 */
  __pyx_t_1 = __pyx_PyFloat_AsDouble(__pyx_v_alpha); if (unlikely((__pyx_t_1 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1061; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_angle = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1062
 * 
 *     angle=alpha
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t_len=len(track)
 *     characteristic_points=[track[0]]
 */
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_INCREF(__pyx_v_xyz);
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_xyz);
  __Pyx_GIVEREF(__pyx_v_xyz);
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
    __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_7 < 0)) {
      PyErr_Fetch(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_8, __pyx_t_9, __pyx_t_10);
      }
    }
    __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1062; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_6 = 0;
  __pyx_v_track = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "dipy/tracking/distances.pyx":1063
 *     angle=alpha
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)
 *     t_len=len(track)             # <<<<<<<<<<<<<<
 *     characteristic_points=[track[0]]
 *     mid_index = 1
 */
  __pyx_t_11 = PyObject_Length(((PyObject *)__pyx_v_track)); if (unlikely(__pyx_t_11 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1063; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_t_len = __pyx_t_11;

  /* "dipy/tracking/distances.pyx":1064
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)
 *     t_len=len(track)
 *     characteristic_points=[track[0]]             # <<<<<<<<<<<<<<
 *     mid_index = 1
 *     angle=0
 */
  __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_4 = PyList_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1064; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  PyList_SET_ITEM(__pyx_t_4, 0, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __pyx_t_5 = 0;
  __pyx_v_characteristic_points = ((PyObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":1065
 *     t_len=len(track)
 *     characteristic_points=[track[0]]
 *     mid_index = 1             # <<<<<<<<<<<<<<
 *     angle=0
 * 
 */
  __pyx_v_mid_index = 1;

  /* "dipy/tracking/distances.pyx":1066
 *     characteristic_points=[track[0]]
 *     mid_index = 1
 *     angle=0             # <<<<<<<<<<<<<<
 * 
 *     while mid_index < t_len-1:
 */
  __pyx_v_angle = 0.0;

  /* "dipy/tracking/distances.pyx":1068
 *     angle=0
 * 
 *     while mid_index < t_len-1:             # <<<<<<<<<<<<<<
 *         #fvec0 = as_float_3vec(track[mid_index-1])
 *         #<float *>track[0].data
 */
  while (1) {
    __pyx_t_12 = ((__pyx_v_mid_index < (__pyx_v_t_len - 1)) != 0);
    if (!__pyx_t_12) break;

    /* "dipy/tracking/distances.pyx":1071
 *         #fvec0 = as_float_3vec(track[mid_index-1])
 *         #<float *>track[0].data
 *         fvec0 = asfp(track[mid_index-1])             # <<<<<<<<<<<<<<
 *         fvec1 = asfp(track[mid_index])
 *         fvec2 = asfp(track[mid_index+1])
 */
    __pyx_t_13 = (__pyx_v_mid_index - 1);
    __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_t_13, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1071; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_fvec0 = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4));
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1072
 *         #<float *>track[0].data
 *         fvec0 = asfp(track[mid_index-1])
 *         fvec1 = asfp(track[mid_index])             # <<<<<<<<<<<<<<
 *         fvec2 = asfp(track[mid_index+1])
 *         #csub_3vecs(<float *>fvec1.data,<float *>fvec0.data,vec0)
 */
    __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_v_mid_index, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1072; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1072; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_fvec1 = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4));
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1073
 *         fvec0 = asfp(track[mid_index-1])
 *         fvec1 = asfp(track[mid_index])
 *         fvec2 = asfp(track[mid_index+1])             # <<<<<<<<<<<<<<
 *         #csub_3vecs(<float *>fvec1.data,<float *>fvec0.data,vec0)
 *         csub_3vecs(fvec1,fvec0,vec0)
 */
    __pyx_t_13 = (__pyx_v_mid_index + 1);
    __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_t_13, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1073; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_fvec2 = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4));
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1075
 *         fvec2 = asfp(track[mid_index+1])
 *         #csub_3vecs(<float *>fvec1.data,<float *>fvec0.data,vec0)
 *         csub_3vecs(fvec1,fvec0,vec0)             # <<<<<<<<<<<<<<
 *         csub_3vecs(fvec2,fvec1,vec1)
 *         tmp=<double>fabs(acos(cinner_3vecs(vec0,vec1)/(cnorm_3vec(vec0)*cnorm_3vec(vec1))))
 */
    __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_fvec1, __pyx_v_fvec0, __pyx_v_vec0);

    /* "dipy/tracking/distances.pyx":1076
 *         #csub_3vecs(<float *>fvec1.data,<float *>fvec0.data,vec0)
 *         csub_3vecs(fvec1,fvec0,vec0)
 *         csub_3vecs(fvec2,fvec1,vec1)             # <<<<<<<<<<<<<<
 *         tmp=<double>fabs(acos(cinner_3vecs(vec0,vec1)/(cnorm_3vec(vec0)*cnorm_3vec(vec1))))
 *         if dpy_isnan(tmp) :
 */
    __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_fvec2, __pyx_v_fvec1, __pyx_v_vec1);

    /* "dipy/tracking/distances.pyx":1077
 *         csub_3vecs(fvec1,fvec0,vec0)
 *         csub_3vecs(fvec2,fvec1,vec1)
 *         tmp=<double>fabs(acos(cinner_3vecs(vec0,vec1)/(cnorm_3vec(vec0)*cnorm_3vec(vec1))))             # <<<<<<<<<<<<<<
 *         if dpy_isnan(tmp) :
 *             angle+=0.
 */
    __pyx_t_14 = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_vec0, __pyx_v_vec1);
    __pyx_t_15 = (__pyx_f_4dipy_8tracking_9distances_cnorm_3vec(__pyx_v_vec0) * __pyx_f_4dipy_8tracking_9distances_cnorm_3vec(__pyx_v_vec1));
    if (unlikely(__pyx_t_15 == 0)) {
      #ifdef WITH_THREAD
      PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
      #endif
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      #ifdef WITH_THREAD
      PyGILState_Release(__pyx_gilstate_save);
      #endif
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1077; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_v_tmp = ((double)fabs(acos((__pyx_t_14 / __pyx_t_15))));

    /* "dipy/tracking/distances.pyx":1078
 *         csub_3vecs(fvec2,fvec1,vec1)
 *         tmp=<double>fabs(acos(cinner_3vecs(vec0,vec1)/(cnorm_3vec(vec0)*cnorm_3vec(vec1))))
 *         if dpy_isnan(tmp) :             # <<<<<<<<<<<<<<
 *             angle+=0.
 *         else:
 */
    __pyx_t_12 = (dpy_isnan(__pyx_v_tmp) != 0);
    if (__pyx_t_12) {

      /* "dipy/tracking/distances.pyx":1079
 *         tmp=<double>fabs(acos(cinner_3vecs(vec0,vec1)/(cnorm_3vec(vec0)*cnorm_3vec(vec1))))
 *         if dpy_isnan(tmp) :
 *             angle+=0.             # <<<<<<<<<<<<<<
 *         else:
 *             angle+=tmp
 */
      __pyx_v_angle = (__pyx_v_angle + 0.);
      goto __pyx_L5;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":1081
 *             angle+=0.
 *         else:
 *             angle+=tmp             # <<<<<<<<<<<<<<
 *         if  angle > alpha:
 *             characteristic_points.append(track[mid_index])
 */
      __pyx_v_angle = (__pyx_v_angle + __pyx_v_tmp);
    }
    __pyx_L5:;

    /* "dipy/tracking/distances.pyx":1082
 *         else:
 *             angle+=tmp
 *         if  angle > alpha:             # <<<<<<<<<<<<<<
 *             characteristic_points.append(track[mid_index])
 *             angle=0
 */
    __pyx_t_4 = PyFloat_FromDouble(__pyx_v_angle); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = PyObject_RichCompare(__pyx_t_4, __pyx_v_alpha, Py_GT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_12 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_12 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (__pyx_t_12) {

      /* "dipy/tracking/distances.pyx":1083
 *             angle+=tmp
 *         if  angle > alpha:
 *             characteristic_points.append(track[mid_index])             # <<<<<<<<<<<<<<
 *             angle=0
 *         mid_index+=1
 */
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_v_mid_index, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1083; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_4 = __Pyx_PyObject_Append(__pyx_v_characteristic_points, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1083; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":1084
 *         if  angle > alpha:
 *             characteristic_points.append(track[mid_index])
 *             angle=0             # <<<<<<<<<<<<<<
 *         mid_index+=1
 * 
 */
      __pyx_v_angle = 0.0;
      goto __pyx_L6;
    }
    __pyx_L6:;

    /* "dipy/tracking/distances.pyx":1085
 *             characteristic_points.append(track[mid_index])
 *             angle=0
 *         mid_index+=1             # <<<<<<<<<<<<<<
 * 
 *     characteristic_points.append(track[-1])
 */
    __pyx_v_mid_index = (__pyx_v_mid_index + 1);
  }

  /* "dipy/tracking/distances.pyx":1087
 *         mid_index+=1
 * 
 *     characteristic_points.append(track[-1])             # <<<<<<<<<<<<<<
 *     return np.array(characteristic_points)
 * 
 */
  __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), -1, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_5 = __Pyx_PyObject_Append(__pyx_v_characteristic_points, __pyx_t_4); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1087; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

  /* "dipy/tracking/distances.pyx":1088
 * 
 *     characteristic_points.append(track[-1])
 *     return np.array(characteristic_points)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1088; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__array); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1088; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1088; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_INCREF(__pyx_v_characteristic_points);
  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_characteristic_points);
  __Pyx_GIVEREF(__pyx_v_characteristic_points);
  __pyx_t_2 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1088; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  __pyx_r = __pyx_t_2;
  __pyx_t_2 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.approx_polygon_track", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_track);
  __Pyx_XDECREF(__pyx_v_characteristic_points);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_33approximate_mdl_trajectory(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_32approximate_mdl_trajectory[] = " Implementation of Lee et al Approximate Trajectory\n    Partitioning Algorithm\n\n    This is base on the minimum description length principle\n\n    Parameters\n    ----------\n    xyz : array(N,3)\n        initial trajectory\n    alpha : float\n        smoothing parameter (>1 smoother, <1  rougher)\n\n    Returns\n    -------\n    characteristic_points : list of M array(3,) points\n\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_33approximate_mdl_trajectory = {__Pyx_NAMESTR("approximate_mdl_trajectory"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_33approximate_mdl_trajectory, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_32approximate_mdl_trajectory)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_33approximate_mdl_trajectory(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz = 0;
  PyObject *__pyx_v_alpha = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("approximate_mdl_trajectory (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz,&__pyx_n_s__alpha,0};
    PyObject* values[2] = {0,0};
    values[1] = __pyx_k_15;
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__alpha);
          if (value) { values[1] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "approximate_mdl_trajectory") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_xyz = values[0];
    __pyx_v_alpha = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("approximate_mdl_trajectory", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.approximate_mdl_trajectory", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_32approximate_mdl_trajectory(__pyx_self, __pyx_v_xyz, __pyx_v_alpha);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1091
 * 
 * 
 * def approximate_mdl_trajectory(xyz, alpha=1.):             # <<<<<<<<<<<<<<
 *     ''' Implementation of Lee et al Approximate Trajectory
 *     Partitioning Algorithm
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_32approximate_mdl_trajectory(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_xyz, PyObject *__pyx_v_alpha) {
  int __pyx_v_start_index;
  int __pyx_v_length;
  int __pyx_v_current_index;
  int __pyx_v_i;
  double __pyx_v_cost_par;
  double __pyx_v_cost_nopar;
  double __pyx_v_alphac;
  PyObject *__pyx_v_characteristic_points = 0;
  size_t __pyx_v_t_len;
  PyArrayObject *__pyx_v_track = 0;
  float __pyx_v_tmp[3];
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_fvec3 = 0;
  PyArrayObject *__pyx_v_fvec4 = 0;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec1;
  __Pyx_Buffer __pyx_pybuffer_fvec1;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec2;
  __Pyx_Buffer __pyx_pybuffer_fvec2;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec3;
  __Pyx_Buffer __pyx_pybuffer_fvec3;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_fvec4;
  __Pyx_Buffer __pyx_pybuffer_fvec4;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyArrayObject *__pyx_t_5 = NULL;
  int __pyx_t_6;
  PyObject *__pyx_t_7 = NULL;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  Py_ssize_t __pyx_t_10;
  double __pyx_t_11;
  int __pyx_t_12;
  long __pyx_t_13;
  int __pyx_t_14;
  long __pyx_t_15;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("approximate_mdl_trajectory", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;
  __pyx_pybuffer_fvec1.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec1.refcount = 0;
  __pyx_pybuffernd_fvec1.data = NULL;
  __pyx_pybuffernd_fvec1.rcbuffer = &__pyx_pybuffer_fvec1;
  __pyx_pybuffer_fvec2.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec2.refcount = 0;
  __pyx_pybuffernd_fvec2.data = NULL;
  __pyx_pybuffernd_fvec2.rcbuffer = &__pyx_pybuffer_fvec2;
  __pyx_pybuffer_fvec3.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec3.refcount = 0;
  __pyx_pybuffernd_fvec3.data = NULL;
  __pyx_pybuffernd_fvec3.rcbuffer = &__pyx_pybuffer_fvec3;
  __pyx_pybuffer_fvec4.pybuffer.buf = NULL;
  __pyx_pybuffer_fvec4.refcount = 0;
  __pyx_pybuffernd_fvec4.data = NULL;
  __pyx_pybuffernd_fvec4.rcbuffer = &__pyx_pybuffer_fvec4;

  /* "dipy/tracking/distances.pyx":1118
 *         cnp.ndarray[cnp.float32_t, ndim=1] fvec1,fvec2,fvec3,fvec4
 * 
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t_len=len(track)
 * 
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_v_xyz);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_xyz);
  __Pyx_GIVEREF(__pyx_v_xyz);
  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __pyx_v_track = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":1119
 * 
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)
 *     t_len=len(track)             # <<<<<<<<<<<<<<
 * 
 *     alphac=alpha
 */
  __pyx_t_10 = PyObject_Length(((PyObject *)__pyx_v_track)); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1119; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_t_len = __pyx_t_10;

  /* "dipy/tracking/distances.pyx":1121
 *     t_len=len(track)
 * 
 *     alphac=alpha             # <<<<<<<<<<<<<<
 *     characteristic_points=[xyz[0]]
 *     start_index = 0
 */
  __pyx_t_11 = __pyx_PyFloat_AsDouble(__pyx_v_alpha); if (unlikely((__pyx_t_11 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_alphac = __pyx_t_11;

  /* "dipy/tracking/distances.pyx":1122
 * 
 *     alphac=alpha
 *     characteristic_points=[xyz[0]]             # <<<<<<<<<<<<<<
 *     start_index = 0
 *     length = 2
 */
  __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_xyz, 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = PyList_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyList_SET_ITEM(__pyx_t_3, 0, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_4 = 0;
  __pyx_v_characteristic_points = ((PyObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":1123
 *     alphac=alpha
 *     characteristic_points=[xyz[0]]
 *     start_index = 0             # <<<<<<<<<<<<<<
 *     length = 2
 *     #print t_len
 */
  __pyx_v_start_index = 0;

  /* "dipy/tracking/distances.pyx":1124
 *     characteristic_points=[xyz[0]]
 *     start_index = 0
 *     length = 2             # <<<<<<<<<<<<<<
 *     #print t_len
 * 
 */
  __pyx_v_length = 2;

  /* "dipy/tracking/distances.pyx":1127
 *     #print t_len
 * 
 *     while start_index+length < <int>t_len-1:             # <<<<<<<<<<<<<<
 *         current_index = start_index+length
 *         fvec1 = as_float_3vec(track[start_index])
 */
  while (1) {
    __pyx_t_12 = (((__pyx_v_start_index + __pyx_v_length) < (((int)__pyx_v_t_len) - 1)) != 0);
    if (!__pyx_t_12) break;

    /* "dipy/tracking/distances.pyx":1128
 * 
 *     while start_index+length < <int>t_len-1:
 *         current_index = start_index+length             # <<<<<<<<<<<<<<
 *         fvec1 = as_float_3vec(track[start_index])
 *         fvec2 = as_float_3vec(track[current_index])
 */
    __pyx_v_current_index = (__pyx_v_start_index + __pyx_v_length);

    /* "dipy/tracking/distances.pyx":1129
 *     while start_index+length < <int>t_len-1:
 *         current_index = start_index+length
 *         fvec1 = as_float_3vec(track[start_index])             # <<<<<<<<<<<<<<
 *         fvec2 = as_float_3vec(track[current_index])
 *         # L(H)
 */
    __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_v_start_index, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_4 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
      __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_4), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
      if (unlikely(__pyx_t_6 < 0)) {
        PyErr_Fetch(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec1, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_9, __pyx_t_8, __pyx_t_7);
        }
      }
      __pyx_pybuffernd_fvec1.diminfo[0].strides = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec1.diminfo[0].shape = __pyx_pybuffernd_fvec1.rcbuffer->pybuffer.shape[0];
      if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1129; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __Pyx_XDECREF(((PyObject *)__pyx_v_fvec1));
    __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1130
 *         current_index = start_index+length
 *         fvec1 = as_float_3vec(track[start_index])
 *         fvec2 = as_float_3vec(track[current_index])             # <<<<<<<<<<<<<<
 *         # L(H)
 *         csub_3vecs(<float *>fvec2.data,<float *>fvec1.data,tmp)
 */
    __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_v_current_index, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_t_4)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
      __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_3), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
      if (unlikely(__pyx_t_6 < 0)) {
        PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
        }
      }
      __pyx_pybuffernd_fvec2.diminfo[0].strides = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec2.diminfo[0].shape = __pyx_pybuffernd_fvec2.rcbuffer->pybuffer.shape[0];
      if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1130; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __Pyx_XDECREF(((PyObject *)__pyx_v_fvec2));
    __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "dipy/tracking/distances.pyx":1132
 *         fvec2 = as_float_3vec(track[current_index])
 *         # L(H)
 *         csub_3vecs(<float *>fvec2.data,<float *>fvec1.data,tmp)             # <<<<<<<<<<<<<<
 *         cost_par=dpy_log2(sqrt(cinner_3vecs(tmp,tmp)))
 *         cost_nopar=0
 */
    __pyx_f_4dipy_8tracking_9distances_csub_3vecs(((float *)__pyx_v_fvec2->data), ((float *)__pyx_v_fvec1->data), __pyx_v_tmp);

    /* "dipy/tracking/distances.pyx":1133
 *         # L(H)
 *         csub_3vecs(<float *>fvec2.data,<float *>fvec1.data,tmp)
 *         cost_par=dpy_log2(sqrt(cinner_3vecs(tmp,tmp)))             # <<<<<<<<<<<<<<
 *         cost_nopar=0
 *         #print start_index,current_index
 */
    __pyx_v_cost_par = dpy_log2(sqrt(__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_tmp, __pyx_v_tmp)));

    /* "dipy/tracking/distances.pyx":1134
 *         csub_3vecs(<float *>fvec2.data,<float *>fvec1.data,tmp)
 *         cost_par=dpy_log2(sqrt(cinner_3vecs(tmp,tmp)))
 *         cost_nopar=0             # <<<<<<<<<<<<<<
 *         #print start_index,current_index
 *         # L(D|H)
 */
    __pyx_v_cost_nopar = 0.0;

    /* "dipy/tracking/distances.pyx":1138
 *         # L(D|H)
 *         #for i in range(start_index+1,current_index):#+1):
 *         for i in range(start_index,current_index+1):             # <<<<<<<<<<<<<<
 *             #print i
 *             fvec3 = as_float_3vec(track[i])
 */
    __pyx_t_13 = (__pyx_v_current_index + 1);
    for (__pyx_t_6 = __pyx_v_start_index; __pyx_t_6 < __pyx_t_13; __pyx_t_6+=1) {
      __pyx_v_i = __pyx_t_6;

      /* "dipy/tracking/distances.pyx":1140
 *         for i in range(start_index,current_index+1):
 *             #print i
 *             fvec3 = as_float_3vec(track[i])             # <<<<<<<<<<<<<<
 *             fvec4 = as_float_3vec(track[i+1])
 *             cost_par += dpy_log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 */
      __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_v_i, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_4 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
        __pyx_t_14 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_4), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
        if (unlikely(__pyx_t_14 < 0)) {
          PyErr_Fetch(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec3, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_9, __pyx_t_8, __pyx_t_7);
          }
        }
        __pyx_pybuffernd_fvec3.diminfo[0].strides = __pyx_pybuffernd_fvec3.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec3.diminfo[0].shape = __pyx_pybuffernd_fvec3.rcbuffer->pybuffer.shape[0];
        if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __Pyx_XDECREF(((PyObject *)__pyx_v_fvec3));
      __pyx_v_fvec3 = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":1141
 *             #print i
 *             fvec3 = as_float_3vec(track[i])
 *             fvec4 = as_float_3vec(track[i+1])             # <<<<<<<<<<<<<<
 *             cost_par += dpy_log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             cost_par += dpy_log2(clee_angle_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 */
      __pyx_t_15 = (__pyx_v_i + 1);
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_t_15, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_3 = ((PyObject *)__pyx_f_4dipy_8tracking_9distances_as_float_3vec(__pyx_t_4)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
        __pyx_t_14 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer, (PyObject*)((PyArrayObject *)__pyx_t_3), &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);
        if (unlikely(__pyx_t_14 < 0)) {
          PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer, (PyObject*)__pyx_v_fvec4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
          }
        }
        __pyx_pybuffernd_fvec4.diminfo[0].strides = __pyx_pybuffernd_fvec4.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_fvec4.diminfo[0].shape = __pyx_pybuffernd_fvec4.rcbuffer->pybuffer.shape[0];
        if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __Pyx_XDECREF(((PyObject *)__pyx_v_fvec4));
      __pyx_v_fvec4 = ((PyArrayObject *)__pyx_t_3);
      __pyx_t_3 = 0;

      /* "dipy/tracking/distances.pyx":1142
 *             fvec3 = as_float_3vec(track[i])
 *             fvec4 = as_float_3vec(track[i+1])
 *             cost_par += dpy_log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))             # <<<<<<<<<<<<<<
 *             cost_par += dpy_log2(clee_angle_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             csub_3vecs(<float *>fvec4.data,<float *>fvec3.data,tmp)
 */
      __pyx_v_cost_par = (__pyx_v_cost_par + dpy_log2(__pyx_f_4dipy_8tracking_9distances_clee_perpendicular_distance(((float *)__pyx_v_fvec3->data), ((float *)__pyx_v_fvec4->data), ((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data))));

      /* "dipy/tracking/distances.pyx":1143
 *             fvec4 = as_float_3vec(track[i+1])
 *             cost_par += dpy_log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             cost_par += dpy_log2(clee_angle_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))             # <<<<<<<<<<<<<<
 *             csub_3vecs(<float *>fvec4.data,<float *>fvec3.data,tmp)
 *             cost_nopar += dpy_log2(cinner_3vecs(tmp,tmp))
 */
      __pyx_v_cost_par = (__pyx_v_cost_par + dpy_log2(__pyx_f_4dipy_8tracking_9distances_clee_angle_distance(((float *)__pyx_v_fvec3->data), ((float *)__pyx_v_fvec4->data), ((float *)__pyx_v_fvec1->data), ((float *)__pyx_v_fvec2->data))));

      /* "dipy/tracking/distances.pyx":1144
 *             cost_par += dpy_log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             cost_par += dpy_log2(clee_angle_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             csub_3vecs(<float *>fvec4.data,<float *>fvec3.data,tmp)             # <<<<<<<<<<<<<<
 *             cost_nopar += dpy_log2(cinner_3vecs(tmp,tmp))
 *         cost_nopar /= 2
 */
      __pyx_f_4dipy_8tracking_9distances_csub_3vecs(((float *)__pyx_v_fvec4->data), ((float *)__pyx_v_fvec3->data), __pyx_v_tmp);

      /* "dipy/tracking/distances.pyx":1145
 *             cost_par += dpy_log2(clee_angle_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             csub_3vecs(<float *>fvec4.data,<float *>fvec3.data,tmp)
 *             cost_nopar += dpy_log2(cinner_3vecs(tmp,tmp))             # <<<<<<<<<<<<<<
 *         cost_nopar /= 2
 *         #print cost_par, cost_nopar, start_index,length
 */
      __pyx_v_cost_nopar = (__pyx_v_cost_nopar + dpy_log2(__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_tmp, __pyx_v_tmp)));
    }

    /* "dipy/tracking/distances.pyx":1146
 *             csub_3vecs(<float *>fvec4.data,<float *>fvec3.data,tmp)
 *             cost_nopar += dpy_log2(cinner_3vecs(tmp,tmp))
 *         cost_nopar /= 2             # <<<<<<<<<<<<<<
 *         #print cost_par, cost_nopar, start_index,length
 *         if alphac*cost_par>cost_nopar:
 */
    __pyx_v_cost_nopar = (__pyx_v_cost_nopar / 2.0);

    /* "dipy/tracking/distances.pyx":1148
 *         cost_nopar /= 2
 *         #print cost_par, cost_nopar, start_index,length
 *         if alphac*cost_par>cost_nopar:             # <<<<<<<<<<<<<<
 *             characteristic_points.append(track[current_index-1])
 *             start_index = current_index-1
 */
    __pyx_t_12 = (((__pyx_v_alphac * __pyx_v_cost_par) > __pyx_v_cost_nopar) != 0);
    if (__pyx_t_12) {

      /* "dipy/tracking/distances.pyx":1149
 *         #print cost_par, cost_nopar, start_index,length
 *         if alphac*cost_par>cost_nopar:
 *             characteristic_points.append(track[current_index-1])             # <<<<<<<<<<<<<<
 *             start_index = current_index-1
 *             length = 2
 */
      __pyx_t_13 = (__pyx_v_current_index - 1);
      __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), __pyx_t_13, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_4 = __Pyx_PyObject_Append(__pyx_v_characteristic_points, __pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1149; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":1150
 *         if alphac*cost_par>cost_nopar:
 *             characteristic_points.append(track[current_index-1])
 *             start_index = current_index-1             # <<<<<<<<<<<<<<
 *             length = 2
 *         else:
 */
      __pyx_v_start_index = (__pyx_v_current_index - 1);

      /* "dipy/tracking/distances.pyx":1151
 *             characteristic_points.append(track[current_index-1])
 *             start_index = current_index-1
 *             length = 2             # <<<<<<<<<<<<<<
 *         else:
 *             length+=1
 */
      __pyx_v_length = 2;
      goto __pyx_L7;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":1153
 *             length = 2
 *         else:
 *             length+=1             # <<<<<<<<<<<<<<
 *     characteristic_points.append(track[-1])
 *     return np.array(characteristic_points)
 */
      __pyx_v_length = (__pyx_v_length + 1);
    }
    __pyx_L7:;
  }

  /* "dipy/tracking/distances.pyx":1154
 *         else:
 *             length+=1
 *     characteristic_points.append(track[-1])             # <<<<<<<<<<<<<<
 *     return np.array(characteristic_points)
 * 
 */
  __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), -1, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = __Pyx_PyObject_Append(__pyx_v_characteristic_points, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":1155
 *             length+=1
 *     characteristic_points.append(track[-1])
 *     return np.array(characteristic_points)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__array); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_INCREF(__pyx_v_characteristic_points);
  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_characteristic_points);
  __Pyx_GIVEREF(__pyx_v_characteristic_points);
  __pyx_t_1 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.approximate_mdl_trajectory", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec1.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec2.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec3.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_fvec4.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF(__pyx_v_characteristic_points);
  __Pyx_XDECREF((PyObject *)__pyx_v_track);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec3);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec4);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_35intersect_segment_cylinder(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_34intersect_segment_cylinder[] = " Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r\n\n    See p.197 from Real Time Collision Detection by C. Ericson\n\n    Examples\n    --------\n    Define cylinder using a segment defined by\n\n    >>> p=np.array([0,0,0],dtype=np.float32)\n    >>> q=np.array([1,0,0],dtype=np.float32)\n    >>> r=0.5\n\n    Define segment\n\n    >>> sa=np.array([0.5,1 ,0],dtype=np.float32)\n    >>> sb=np.array([0.5,-1,0],dtype=np.float32)\n\n    Intersection\n\n    >>> intersect_segment_cylinder(sa, sb, p, q, r)\n    (1.0, 0.25, 0.75)\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_35intersect_segment_cylinder = {__Pyx_NAMESTR("intersect_segment_cylinder"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_35intersect_segment_cylinder, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_34intersect_segment_cylinder)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_35intersect_segment_cylinder(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_sa = 0;
  PyObject *__pyx_v_sb = 0;
  PyObject *__pyx_v_p = 0;
  PyObject *__pyx_v_q = 0;
  PyObject *__pyx_v_r = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("intersect_segment_cylinder (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__sa,&__pyx_n_s__sb,&__pyx_n_s__p,&__pyx_n_s__q,&__pyx_n_s__r,0};
    PyObject* values[5] = {0,0,0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sa)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sb)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__p)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  3:
        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__q)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  4:
        if (likely((values[4] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__r)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "intersect_segment_cylinder") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 5) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
      values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
      values[4] = PyTuple_GET_ITEM(__pyx_args, 4);
    }
    __pyx_v_sa = values[0];
    __pyx_v_sb = values[1];
    __pyx_v_p = values[2];
    __pyx_v_q = values[3];
    __pyx_v_r = values[4];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.intersect_segment_cylinder", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_34intersect_segment_cylinder(__pyx_self, __pyx_v_sa, __pyx_v_sb, __pyx_v_p, __pyx_v_q, __pyx_v_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1158
 * 
 * 
 * def intersect_segment_cylinder(sa,sb,p,q,r):             # <<<<<<<<<<<<<<
 *     ''' Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_34intersect_segment_cylinder(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_sa, PyObject *__pyx_v_sb, PyObject *__pyx_v_p, PyObject *__pyx_v_q, PyObject *__pyx_v_r) {
  float *__pyx_v_csa;
  float *__pyx_v_csb;
  float *__pyx_v_cp;
  float *__pyx_v_cq;
  float __pyx_v_cr;
  float __pyx_v_ct[2];
  PyObject *__pyx_v_tmp = NULL;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  float __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("intersect_segment_cylinder", 0);

  /* "dipy/tracking/distances.pyx":1186
 *         float ct[2]
 * 
 *     csa = asfp(sa)             # <<<<<<<<<<<<<<
 *     csb = asfp(sb)
 *     cp = asfp(p)
 */
  if (!(likely(((__pyx_v_sa) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_sa, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_csa = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_sa));

  /* "dipy/tracking/distances.pyx":1187
 * 
 *     csa = asfp(sa)
 *     csb = asfp(sb)             # <<<<<<<<<<<<<<
 *     cp = asfp(p)
 *     cq = asfp(q)
 */
  if (!(likely(((__pyx_v_sb) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_sb, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1187; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_csb = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_sb));

  /* "dipy/tracking/distances.pyx":1188
 *     csa = asfp(sa)
 *     csb = asfp(sb)
 *     cp = asfp(p)             # <<<<<<<<<<<<<<
 *     cq = asfp(q)
 *     cr=r
 */
  if (!(likely(((__pyx_v_p) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_p, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cp = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_p));

  /* "dipy/tracking/distances.pyx":1189
 *     csb = asfp(sb)
 *     cp = asfp(p)
 *     cq = asfp(q)             # <<<<<<<<<<<<<<
 *     cr=r
 *     ct[0]=-100
 */
  if (!(likely(((__pyx_v_q) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_q, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1189; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cq = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_q));

  /* "dipy/tracking/distances.pyx":1190
 *     cp = asfp(p)
 *     cq = asfp(q)
 *     cr=r             # <<<<<<<<<<<<<<
 *     ct[0]=-100
 *     ct[1]=-100
 */
  __pyx_t_1 = __pyx_PyFloat_AsFloat(__pyx_v_r); if (unlikely((__pyx_t_1 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cr = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1191
 *     cq = asfp(q)
 *     cr=r
 *     ct[0]=-100             # <<<<<<<<<<<<<<
 *     ct[1]=-100
 * 
 */
  (__pyx_v_ct[0]) = -100.0;

  /* "dipy/tracking/distances.pyx":1192
 *     cr=r
 *     ct[0]=-100
 *     ct[1]=-100             # <<<<<<<<<<<<<<
 * 
 *     tmp = cintersect_segment_cylinder(csa,csb,cp, cq, cr, ct)
 */
  (__pyx_v_ct[1]) = -100.0;

  /* "dipy/tracking/distances.pyx":1194
 *     ct[1]=-100
 * 
 *     tmp = cintersect_segment_cylinder(csa,csb,cp, cq, cr, ct)             # <<<<<<<<<<<<<<
 * 
 *     return tmp, ct[0], ct[1]
 */
  __pyx_t_2 = PyFloat_FromDouble(__pyx_f_4dipy_8tracking_9distances_cintersect_segment_cylinder(__pyx_v_csa, __pyx_v_csb, __pyx_v_cp, __pyx_v_cq, __pyx_v_cr, __pyx_v_ct)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1194; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_v_tmp = __pyx_t_2;
  __pyx_t_2 = 0;

  /* "dipy/tracking/distances.pyx":1196
 *     tmp = cintersect_segment_cylinder(csa,csb,cp, cq, cr, ct)
 * 
 *     return tmp, ct[0], ct[1]             # <<<<<<<<<<<<<<
 * 
 * cdef float cintersect_segment_cylinder(float *sa,float *sb,float *p, float *q, float r, float *t):
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_2 = PyFloat_FromDouble((__pyx_v_ct[0])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = PyFloat_FromDouble((__pyx_v_ct[1])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1196; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(__pyx_v_tmp);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_tmp);
  __Pyx_GIVEREF(__pyx_v_tmp);
  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_2);
  __Pyx_GIVEREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_4, 2, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_2 = 0;
  __pyx_t_3 = 0;
  __pyx_r = ((PyObject *)__pyx_t_4);
  __pyx_t_4 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_AddTraceback("dipy.tracking.distances.intersect_segment_cylinder", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XDECREF(__pyx_v_tmp);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1198
 *     return tmp, ct[0], ct[1]
 * 
 * cdef float cintersect_segment_cylinder(float *sa,float *sb,float *p, float *q, float r, float *t):             # <<<<<<<<<<<<<<
 *     ''' Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r
 * 
 */

static float __pyx_f_4dipy_8tracking_9distances_cintersect_segment_cylinder(float *__pyx_v_sa, float *__pyx_v_sb, float *__pyx_v_p, float *__pyx_v_q, float __pyx_v_r, float *__pyx_v_t) {
  float __pyx_v_d[3];
  float __pyx_v_m[3];
  float __pyx_v_n[3];
  float __pyx_v_md;
  float __pyx_v_nd;
  float __pyx_v_dd;
  float __pyx_v_nn;
  float __pyx_v_mn;
  float __pyx_v_a;
  float __pyx_v_k;
  float __pyx_v_c;
  float __pyx_v_b;
  float __pyx_v_discr;
  float __pyx_v_epsilon_float;
  float __pyx_r;
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  int __pyx_t_2;
  int __pyx_t_3;
  float __pyx_t_4;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("cintersect_segment_cylinder", 0);

  /* "dipy/tracking/distances.pyx":1213
 *         float md,nd,dd, nn, mn, a, k, c,b, discr
 * 
 *         float epsilon_float=5.96e-08             # <<<<<<<<<<<<<<
 * 
 *     csub_3vecs(q,p,d)
 */
  __pyx_v_epsilon_float = 5.96e-08;

  /* "dipy/tracking/distances.pyx":1215
 *         float epsilon_float=5.96e-08
 * 
 *     csub_3vecs(q,p,d)             # <<<<<<<<<<<<<<
 *     csub_3vecs(sa,p,m)
 *     csub_3vecs(sb,sa,n)
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_q, __pyx_v_p, __pyx_v_d);

  /* "dipy/tracking/distances.pyx":1216
 * 
 *     csub_3vecs(q,p,d)
 *     csub_3vecs(sa,p,m)             # <<<<<<<<<<<<<<
 *     csub_3vecs(sb,sa,n)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_sa, __pyx_v_p, __pyx_v_m);

  /* "dipy/tracking/distances.pyx":1217
 *     csub_3vecs(q,p,d)
 *     csub_3vecs(sa,p,m)
 *     csub_3vecs(sb,sa,n)             # <<<<<<<<<<<<<<
 * 
 *     md=cinner_3vecs(m,d)
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_sb, __pyx_v_sa, __pyx_v_n);

  /* "dipy/tracking/distances.pyx":1219
 *     csub_3vecs(sb,sa,n)
 * 
 *     md=cinner_3vecs(m,d)             # <<<<<<<<<<<<<<
 *     nd=cinner_3vecs(n,d)
 *     dd=cinner_3vecs(d,d)
 */
  __pyx_v_md = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_m, __pyx_v_d);

  /* "dipy/tracking/distances.pyx":1220
 * 
 *     md=cinner_3vecs(m,d)
 *     nd=cinner_3vecs(n,d)             # <<<<<<<<<<<<<<
 *     dd=cinner_3vecs(d,d)
 * 
 */
  __pyx_v_nd = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_n, __pyx_v_d);

  /* "dipy/tracking/distances.pyx":1221
 *     md=cinner_3vecs(m,d)
 *     nd=cinner_3vecs(n,d)
 *     dd=cinner_3vecs(d,d)             # <<<<<<<<<<<<<<
 * 
 *     #test if segment fully outside either endcap of cylinder
 */
  __pyx_v_dd = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_d, __pyx_v_d);

  /* "dipy/tracking/distances.pyx":1224
 * 
 *     #test if segment fully outside either endcap of cylinder
 *     if md < 0. and md + nd < 0.:  return 0 #segment outside p side             # <<<<<<<<<<<<<<
 *     if md > dd and md + nd > dd:  return 0 #segment outside q side
 * 
 */
  __pyx_t_1 = ((__pyx_v_md < 0.) != 0);
  if (__pyx_t_1) {
    __pyx_t_2 = (((__pyx_v_md + __pyx_v_nd) < 0.) != 0);
    __pyx_t_3 = __pyx_t_2;
  } else {
    __pyx_t_3 = __pyx_t_1;
  }
  if (__pyx_t_3) {
    __pyx_r = 0.0;
    goto __pyx_L0;
    goto __pyx_L3;
  }
  __pyx_L3:;

  /* "dipy/tracking/distances.pyx":1225
 *     #test if segment fully outside either endcap of cylinder
 *     if md < 0. and md + nd < 0.:  return 0 #segment outside p side
 *     if md > dd and md + nd > dd:  return 0 #segment outside q side             # <<<<<<<<<<<<<<
 * 
 *     nn=cinner_3vecs(n,n)
 */
  __pyx_t_3 = ((__pyx_v_md > __pyx_v_dd) != 0);
  if (__pyx_t_3) {
    __pyx_t_1 = (((__pyx_v_md + __pyx_v_nd) > __pyx_v_dd) != 0);
    __pyx_t_2 = __pyx_t_1;
  } else {
    __pyx_t_2 = __pyx_t_3;
  }
  if (__pyx_t_2) {
    __pyx_r = 0.0;
    goto __pyx_L0;
    goto __pyx_L4;
  }
  __pyx_L4:;

  /* "dipy/tracking/distances.pyx":1227
 *     if md > dd and md + nd > dd:  return 0 #segment outside q side
 * 
 *     nn=cinner_3vecs(n,n)             # <<<<<<<<<<<<<<
 *     mn=cinner_3vecs(m,n)
 *     a=dd*nn-nd*nd
 */
  __pyx_v_nn = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_n, __pyx_v_n);

  /* "dipy/tracking/distances.pyx":1228
 * 
 *     nn=cinner_3vecs(n,n)
 *     mn=cinner_3vecs(m,n)             # <<<<<<<<<<<<<<
 *     a=dd*nn-nd*nd
 *     k=cinner_3vecs(m,m) -r*r
 */
  __pyx_v_mn = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_m, __pyx_v_n);

  /* "dipy/tracking/distances.pyx":1229
 *     nn=cinner_3vecs(n,n)
 *     mn=cinner_3vecs(m,n)
 *     a=dd*nn-nd*nd             # <<<<<<<<<<<<<<
 *     k=cinner_3vecs(m,m) -r*r
 *     c=dd*k-md*md
 */
  __pyx_v_a = ((__pyx_v_dd * __pyx_v_nn) - (__pyx_v_nd * __pyx_v_nd));

  /* "dipy/tracking/distances.pyx":1230
 *     mn=cinner_3vecs(m,n)
 *     a=dd*nn-nd*nd
 *     k=cinner_3vecs(m,m) -r*r             # <<<<<<<<<<<<<<
 *     c=dd*k-md*md
 * 
 */
  __pyx_v_k = (__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_m, __pyx_v_m) - (__pyx_v_r * __pyx_v_r));

  /* "dipy/tracking/distances.pyx":1231
 *     a=dd*nn-nd*nd
 *     k=cinner_3vecs(m,m) -r*r
 *     c=dd*k-md*md             # <<<<<<<<<<<<<<
 * 
 *     if fabs(a) < epsilon_float:
 */
  __pyx_v_c = ((__pyx_v_dd * __pyx_v_k) - (__pyx_v_md * __pyx_v_md));

  /* "dipy/tracking/distances.pyx":1233
 *     c=dd*k-md*md
 * 
 *     if fabs(a) < epsilon_float:             # <<<<<<<<<<<<<<
 *         #segment runs parallel to cylinder axis
 *         if c>0.:  return 0. # segment lies outside cylinder
 */
  __pyx_t_2 = ((fabs(__pyx_v_a) < __pyx_v_epsilon_float) != 0);
  if (__pyx_t_2) {

    /* "dipy/tracking/distances.pyx":1235
 *     if fabs(a) < epsilon_float:
 *         #segment runs parallel to cylinder axis
 *         if c>0.:  return 0. # segment lies outside cylinder             # <<<<<<<<<<<<<<
 *         if md < 0.:
 *             t[0]=-mn/nn # intersect against p endcap
 */
    __pyx_t_2 = ((__pyx_v_c > 0.) != 0);
    if (__pyx_t_2) {
      __pyx_r = 0.;
      goto __pyx_L0;
      goto __pyx_L6;
    }
    __pyx_L6:;

    /* "dipy/tracking/distances.pyx":1236
 *         #segment runs parallel to cylinder axis
 *         if c>0.:  return 0. # segment lies outside cylinder
 *         if md < 0.:             # <<<<<<<<<<<<<<
 *             t[0]=-mn/nn # intersect against p endcap
 *         elif md > dd :
 */
    __pyx_t_2 = ((__pyx_v_md < 0.) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":1237
 *         if c>0.:  return 0. # segment lies outside cylinder
 *         if md < 0.:
 *             t[0]=-mn/nn # intersect against p endcap             # <<<<<<<<<<<<<<
 *         elif md > dd :
 *             t[0]=(nd-mn)/nn # intersect against q endcap
 */
      __pyx_t_4 = (-__pyx_v_mn);
      if (unlikely(__pyx_v_nn == 0)) {
        #ifdef WITH_THREAD
        PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
        #endif
        PyErr_Format(PyExc_ZeroDivisionError, "float division");
        #ifdef WITH_THREAD
        PyGILState_Release(__pyx_gilstate_save);
        #endif
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1237; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nn);
      goto __pyx_L7;
    }

    /* "dipy/tracking/distances.pyx":1238
 *         if md < 0.:
 *             t[0]=-mn/nn # intersect against p endcap
 *         elif md > dd :             # <<<<<<<<<<<<<<
 *             t[0]=(nd-mn)/nn # intersect against q endcap
 *         else:
 */
    __pyx_t_2 = ((__pyx_v_md > __pyx_v_dd) != 0);
    if (__pyx_t_2) {

      /* "dipy/tracking/distances.pyx":1239
 *             t[0]=-mn/nn # intersect against p endcap
 *         elif md > dd :
 *             t[0]=(nd-mn)/nn # intersect against q endcap             # <<<<<<<<<<<<<<
 *         else:
 *             t[0]=0. # lies inside cylinder
 */
      __pyx_t_4 = (__pyx_v_nd - __pyx_v_mn);
      if (unlikely(__pyx_v_nn == 0)) {
        #ifdef WITH_THREAD
        PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
        #endif
        PyErr_Format(PyExc_ZeroDivisionError, "float division");
        #ifdef WITH_THREAD
        PyGILState_Release(__pyx_gilstate_save);
        #endif
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1239; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nn);
      goto __pyx_L7;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":1241
 *             t[0]=(nd-mn)/nn # intersect against q endcap
 *         else:
 *             t[0]=0. # lies inside cylinder             # <<<<<<<<<<<<<<
 *         return 1
 * 
 */
      (__pyx_v_t[0]) = 0.;
    }
    __pyx_L7:;

    /* "dipy/tracking/distances.pyx":1242
 *         else:
 *             t[0]=0. # lies inside cylinder
 *         return 1             # <<<<<<<<<<<<<<
 * 
 *     b=dd*mn -nd*md
 */
    __pyx_r = 1.0;
    goto __pyx_L0;
    goto __pyx_L5;
  }
  __pyx_L5:;

  /* "dipy/tracking/distances.pyx":1244
 *         return 1
 * 
 *     b=dd*mn -nd*md             # <<<<<<<<<<<<<<
 *     discr=b*b-a*c
 *     if discr < 0.: return 0. # no real roots ; no intersection
 */
  __pyx_v_b = ((__pyx_v_dd * __pyx_v_mn) - (__pyx_v_nd * __pyx_v_md));

  /* "dipy/tracking/distances.pyx":1245
 * 
 *     b=dd*mn -nd*md
 *     discr=b*b-a*c             # <<<<<<<<<<<<<<
 *     if discr < 0.: return 0. # no real roots ; no intersection
 * 
 */
  __pyx_v_discr = ((__pyx_v_b * __pyx_v_b) - (__pyx_v_a * __pyx_v_c));

  /* "dipy/tracking/distances.pyx":1246
 *     b=dd*mn -nd*md
 *     discr=b*b-a*c
 *     if discr < 0.: return 0. # no real roots ; no intersection             # <<<<<<<<<<<<<<
 * 
 *     t[0]=(-b-sqrt(discr))/a
 */
  __pyx_t_2 = ((__pyx_v_discr < 0.) != 0);
  if (__pyx_t_2) {
    __pyx_r = 0.;
    goto __pyx_L0;
    goto __pyx_L8;
  }
  __pyx_L8:;

  /* "dipy/tracking/distances.pyx":1248
 *     if discr < 0.: return 0. # no real roots ; no intersection
 * 
 *     t[0]=(-b-sqrt(discr))/a             # <<<<<<<<<<<<<<
 *     t[1]=(-b+sqrt(discr))/a
 *     if t[0]<0. or t[0] > 1. : return 0. # intersection lies outside segment
 */
  __pyx_t_4 = ((-__pyx_v_b) - sqrt(__pyx_v_discr));
  if (unlikely(__pyx_v_a == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_a);

  /* "dipy/tracking/distances.pyx":1249
 * 
 *     t[0]=(-b-sqrt(discr))/a
 *     t[1]=(-b+sqrt(discr))/a             # <<<<<<<<<<<<<<
 *     if t[0]<0. or t[0] > 1. : return 0. # intersection lies outside segment
 * 
 */
  __pyx_t_4 = ((-__pyx_v_b) + sqrt(__pyx_v_discr));
  if (unlikely(__pyx_v_a == 0)) {
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
    #endif
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save);
    #endif
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1249; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  (__pyx_v_t[1]) = (__pyx_t_4 / __pyx_v_a);

  /* "dipy/tracking/distances.pyx":1250
 *     t[0]=(-b-sqrt(discr))/a
 *     t[1]=(-b+sqrt(discr))/a
 *     if t[0]<0. or t[0] > 1. : return 0. # intersection lies outside segment             # <<<<<<<<<<<<<<
 * 
 *     if md + t[0]* nd < 0.:
 */
  __pyx_t_2 = (((__pyx_v_t[0]) < 0.) != 0);
  if (!__pyx_t_2) {
    __pyx_t_3 = (((__pyx_v_t[0]) > 1.) != 0);
    __pyx_t_1 = __pyx_t_3;
  } else {
    __pyx_t_1 = __pyx_t_2;
  }
  if (__pyx_t_1) {
    __pyx_r = 0.;
    goto __pyx_L0;
    goto __pyx_L9;
  }
  __pyx_L9:;

  /* "dipy/tracking/distances.pyx":1252
 *     if t[0]<0. or t[0] > 1. : return 0. # intersection lies outside segment
 * 
 *     if md + t[0]* nd < 0.:             # <<<<<<<<<<<<<<
 *         #intersection outside cylinder on 'p' side
 *         if nd <= 0. : return 0. # segment pointing away from endcap
 */
  __pyx_t_1 = (((__pyx_v_md + ((__pyx_v_t[0]) * __pyx_v_nd)) < 0.) != 0);
  if (__pyx_t_1) {

    /* "dipy/tracking/distances.pyx":1254
 *     if md + t[0]* nd < 0.:
 *         #intersection outside cylinder on 'p' side
 *         if nd <= 0. : return 0. # segment pointing away from endcap             # <<<<<<<<<<<<<<
 *         t[0]=-md/nd
 *         #keep intersection if Dot(S(t)-p,S(t)-p) <= r^2
 */
    __pyx_t_1 = ((__pyx_v_nd <= 0.) != 0);
    if (__pyx_t_1) {
      __pyx_r = 0.;
      goto __pyx_L0;
      goto __pyx_L11;
    }
    __pyx_L11:;

    /* "dipy/tracking/distances.pyx":1255
 *         #intersection outside cylinder on 'p' side
 *         if nd <= 0. : return 0. # segment pointing away from endcap
 *         t[0]=-md/nd             # <<<<<<<<<<<<<<
 *         #keep intersection if Dot(S(t)-p,S(t)-p) <= r^2
 *         if k+2*t[0]*(mn+t[0]*nn) <=0.:
 */
    __pyx_t_4 = (-__pyx_v_md);
    if (unlikely(__pyx_v_nd == 0)) {
      #ifdef WITH_THREAD
      PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
      #endif
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      #ifdef WITH_THREAD
      PyGILState_Release(__pyx_gilstate_save);
      #endif
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1255; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nd);

    /* "dipy/tracking/distances.pyx":1257
 *         t[0]=-md/nd
 *         #keep intersection if Dot(S(t)-p,S(t)-p) <= r^2
 *         if k+2*t[0]*(mn+t[0]*nn) <=0.:             # <<<<<<<<<<<<<<
 *             return 1.
 * 
 */
    __pyx_t_1 = (((__pyx_v_k + ((2.0 * (__pyx_v_t[0])) * (__pyx_v_mn + ((__pyx_v_t[0]) * __pyx_v_nn)))) <= 0.) != 0);
    if (__pyx_t_1) {

      /* "dipy/tracking/distances.pyx":1258
 *         #keep intersection if Dot(S(t)-p,S(t)-p) <= r^2
 *         if k+2*t[0]*(mn+t[0]*nn) <=0.:
 *             return 1.             # <<<<<<<<<<<<<<
 * 
 *     elif md+t[0]*nd > dd :
 */
      __pyx_r = 1.;
      goto __pyx_L0;
      goto __pyx_L12;
    }
    __pyx_L12:;
    goto __pyx_L10;
  }

  /* "dipy/tracking/distances.pyx":1260
 *             return 1.
 * 
 *     elif md+t[0]*nd > dd :             # <<<<<<<<<<<<<<
 *         #intersection outside cylinder on 'q' side
 *         if nd >= 0.: return 0. # segment pointing away from endcap
 */
  __pyx_t_1 = (((__pyx_v_md + ((__pyx_v_t[0]) * __pyx_v_nd)) > __pyx_v_dd) != 0);
  if (__pyx_t_1) {

    /* "dipy/tracking/distances.pyx":1262
 *     elif md+t[0]*nd > dd :
 *         #intersection outside cylinder on 'q' side
 *         if nd >= 0.: return 0. # segment pointing away from endcap             # <<<<<<<<<<<<<<
 *         t[0]= (dd-md)/nd
 *         #keep intersection if Dot(S(t)-q,S(t)-q) <= r^2
 */
    __pyx_t_1 = ((__pyx_v_nd >= 0.) != 0);
    if (__pyx_t_1) {
      __pyx_r = 0.;
      goto __pyx_L0;
      goto __pyx_L13;
    }
    __pyx_L13:;

    /* "dipy/tracking/distances.pyx":1263
 *         #intersection outside cylinder on 'q' side
 *         if nd >= 0.: return 0. # segment pointing away from endcap
 *         t[0]= (dd-md)/nd             # <<<<<<<<<<<<<<
 *         #keep intersection if Dot(S(t)-q,S(t)-q) <= r^2
 *         if k+dd-2*md+t[0]*(2*(mn-nd)+t[0]*nn) <= 0.:
 */
    __pyx_t_4 = (__pyx_v_dd - __pyx_v_md);
    if (unlikely(__pyx_v_nd == 0)) {
      #ifdef WITH_THREAD
      PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
      #endif
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      #ifdef WITH_THREAD
      PyGILState_Release(__pyx_gilstate_save);
      #endif
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1263; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nd);

    /* "dipy/tracking/distances.pyx":1265
 *         t[0]= (dd-md)/nd
 *         #keep intersection if Dot(S(t)-q,S(t)-q) <= r^2
 *         if k+dd-2*md+t[0]*(2*(mn-nd)+t[0]*nn) <= 0.:             # <<<<<<<<<<<<<<
 *             return 1.
 *     # segment intersects cylinder between the endcaps; t is correct
 */
    __pyx_t_1 = (((((__pyx_v_k + __pyx_v_dd) - (2.0 * __pyx_v_md)) + ((__pyx_v_t[0]) * ((2.0 * (__pyx_v_mn - __pyx_v_nd)) + ((__pyx_v_t[0]) * __pyx_v_nn)))) <= 0.) != 0);
    if (__pyx_t_1) {

      /* "dipy/tracking/distances.pyx":1266
 *         #keep intersection if Dot(S(t)-q,S(t)-q) <= r^2
 *         if k+dd-2*md+t[0]*(2*(mn-nd)+t[0]*nn) <= 0.:
 *             return 1.             # <<<<<<<<<<<<<<
 *     # segment intersects cylinder between the endcaps; t is correct
 *     return 1.
 */
      __pyx_r = 1.;
      goto __pyx_L0;
      goto __pyx_L14;
    }
    __pyx_L14:;
    goto __pyx_L10;
  }
  __pyx_L10:;

  /* "dipy/tracking/distances.pyx":1268
 *             return 1.
 *     # segment intersects cylinder between the endcaps; t is correct
 *     return 1.             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = 1.;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_WriteUnraisable("dipy.tracking.distances.cintersect_segment_cylinder", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_37point_segment_sq_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_36point_segment_sq_distance[] = " Calculate the squared distance from a point c to a finite line segment ab.\n\n    Examples\n    --------\n    >>> a=np.array([0,0,0], dtype=np.float32)\n    >>> b=np.array([1,0,0], dtype=np.float32)\n    >>> c=np.array([0,1,0], dtype=np.float32)\n    >>> point_segment_sq_distance(a, b, c)\n    1.0\n    >>> c = np.array([0,3,0], dtype=np.float32)\n    >>> point_segment_sq_distance(a,b,c)\n    9.0\n    >>> c = np.array([-1,1,0], dtype=np.float32)\n    >>> point_segment_sq_distance(a, b, c)\n    2.0\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_37point_segment_sq_distance = {__Pyx_NAMESTR("point_segment_sq_distance"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_37point_segment_sq_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_36point_segment_sq_distance)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_37point_segment_sq_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_a = 0;
  PyObject *__pyx_v_b = 0;
  PyObject *__pyx_v_c = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("point_segment_sq_distance (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__a,&__pyx_n_s__b,&__pyx_n_s__c,0};
    PyObject* values[3] = {0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__a)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__b)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("point_segment_sq_distance", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__c)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("point_segment_sq_distance", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "point_segment_sq_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
    }
    __pyx_v_a = values[0];
    __pyx_v_b = values[1];
    __pyx_v_c = values[2];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("point_segment_sq_distance", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.point_segment_sq_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_36point_segment_sq_distance(__pyx_self, __pyx_v_a, __pyx_v_b, __pyx_v_c);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1271
 * 
 * 
 * def point_segment_sq_distance(a, b, c):             # <<<<<<<<<<<<<<
 *     ''' Calculate the squared distance from a point c to a finite line segment ab.
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_36point_segment_sq_distance(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c) {
  float *__pyx_v_ca;
  float *__pyx_v_cb;
  float *__pyx_v_cc;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("point_segment_sq_distance", 0);

  /* "dipy/tracking/distances.pyx":1293
 *         float ct[2]
 * 
 *     ca = asfp(a)             # <<<<<<<<<<<<<<
 *     cb = asfp(b)
 *     cc = asfp(c)
 */
  if (!(likely(((__pyx_v_a) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_a, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1293; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_ca = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_a));

  /* "dipy/tracking/distances.pyx":1294
 * 
 *     ca = asfp(a)
 *     cb = asfp(b)             # <<<<<<<<<<<<<<
 *     cc = asfp(c)
 * 
 */
  if (!(likely(((__pyx_v_b) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_b, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1294; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cb = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_b));

  /* "dipy/tracking/distances.pyx":1295
 *     ca = asfp(a)
 *     cb = asfp(b)
 *     cc = asfp(c)             # <<<<<<<<<<<<<<
 * 
 *     return cpoint_segment_sq_dist(ca, cb, cc)
 */
  if (!(likely(((__pyx_v_c) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_c, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1295; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cc = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_c));

  /* "dipy/tracking/distances.pyx":1297
 *     cc = asfp(c)
 * 
 *     return cpoint_segment_sq_dist(ca, cb, cc)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyFloat_FromDouble(__pyx_f_4dipy_8tracking_9distances_cpoint_segment_sq_dist(__pyx_v_ca, __pyx_v_cb, __pyx_v_cc)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1297; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("dipy.tracking.distances.point_segment_sq_distance", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1301
 * 
 * @cython.cdivision(True)
 * cdef inline float cpoint_segment_sq_dist(float * a, float * b, float * c) nogil:             # <<<<<<<<<<<<<<
 *     ''' Calculate the squared distance from a point c to a line segment ab.
 * 
 */

static CYTHON_INLINE float __pyx_f_4dipy_8tracking_9distances_cpoint_segment_sq_dist(float *__pyx_v_a, float *__pyx_v_b, float *__pyx_v_c) {
  float __pyx_v_ab[3];
  float __pyx_v_ac[3];
  float __pyx_v_bc[3];
  float __pyx_v_e;
  float __pyx_v_f;
  float __pyx_r;
  int __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1309
 *         float e,f
 * 
 *     csub_3vecs(b,a,ab)             # <<<<<<<<<<<<<<
 *     csub_3vecs(c,a,ac)
 *     csub_3vecs(c,b,bc)
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_b, __pyx_v_a, __pyx_v_ab);

  /* "dipy/tracking/distances.pyx":1310
 * 
 *     csub_3vecs(b,a,ab)
 *     csub_3vecs(c,a,ac)             # <<<<<<<<<<<<<<
 *     csub_3vecs(c,b,bc)
 * 
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_c, __pyx_v_a, __pyx_v_ac);

  /* "dipy/tracking/distances.pyx":1311
 *     csub_3vecs(b,a,ab)
 *     csub_3vecs(c,a,ac)
 *     csub_3vecs(c,b,bc)             # <<<<<<<<<<<<<<
 * 
 *     e = cinner_3vecs(ac, ab)
 */
  __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_c, __pyx_v_b, __pyx_v_bc);

  /* "dipy/tracking/distances.pyx":1313
 *     csub_3vecs(c,b,bc)
 * 
 *     e = cinner_3vecs(ac, ab)             # <<<<<<<<<<<<<<
 *     #Handle cases where c projects outside ab
 *     if e <= 0.:
 */
  __pyx_v_e = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_ac, __pyx_v_ab);

  /* "dipy/tracking/distances.pyx":1315
 *     e = cinner_3vecs(ac, ab)
 *     #Handle cases where c projects outside ab
 *     if e <= 0.:             # <<<<<<<<<<<<<<
 *         return cinner_3vecs(ac, ac)
 *     f = cinner_3vecs(ab, ab)
 */
  __pyx_t_1 = ((__pyx_v_e <= 0.) != 0);
  if (__pyx_t_1) {

    /* "dipy/tracking/distances.pyx":1316
 *     #Handle cases where c projects outside ab
 *     if e <= 0.:
 *         return cinner_3vecs(ac, ac)             # <<<<<<<<<<<<<<
 *     f = cinner_3vecs(ab, ab)
 *     if e >= f :
 */
    __pyx_r = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_ac, __pyx_v_ac);
    goto __pyx_L0;
    goto __pyx_L3;
  }
  __pyx_L3:;

  /* "dipy/tracking/distances.pyx":1317
 *     if e <= 0.:
 *         return cinner_3vecs(ac, ac)
 *     f = cinner_3vecs(ab, ab)             # <<<<<<<<<<<<<<
 *     if e >= f :
 *         return cinner_3vecs(bc, bc)
 */
  __pyx_v_f = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_ab, __pyx_v_ab);

  /* "dipy/tracking/distances.pyx":1318
 *         return cinner_3vecs(ac, ac)
 *     f = cinner_3vecs(ab, ab)
 *     if e >= f :             # <<<<<<<<<<<<<<
 *         return cinner_3vecs(bc, bc)
 *     #Handle case where c projects onto ab
 */
  __pyx_t_1 = ((__pyx_v_e >= __pyx_v_f) != 0);
  if (__pyx_t_1) {

    /* "dipy/tracking/distances.pyx":1319
 *     f = cinner_3vecs(ab, ab)
 *     if e >= f :
 *         return cinner_3vecs(bc, bc)             # <<<<<<<<<<<<<<
 *     #Handle case where c projects onto ab
 *     return cinner_3vecs(ac, ac) - e * e / f
 */
    __pyx_r = __pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_bc, __pyx_v_bc);
    goto __pyx_L0;
    goto __pyx_L4;
  }
  __pyx_L4:;

  /* "dipy/tracking/distances.pyx":1321
 *         return cinner_3vecs(bc, bc)
 *     #Handle case where c projects onto ab
 *     return cinner_3vecs(ac, ac) - e * e / f             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = (__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_ac, __pyx_v_ac) - ((__pyx_v_e * __pyx_v_e) / __pyx_v_f));
  goto __pyx_L0;

  __pyx_r = 0;
  __pyx_L0:;
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_39track_dist_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_38track_dist_3pts[] = " Calculate the euclidean distance between two 3pt tracks\n\n    Both direct and flip distances are calculated but only the smallest is returned\n\n    Parameters\n    ----------\n    a : array, shape (3,3)\n    a three point track\n    b : array, shape (3,3)\n    a three point track\n\n    Returns\n    -------\n    dist :float\n\n    Examples\n    --------\n    >>> a = np.array([[0,0,0],[1,0,0,],[2,0,0]])\n    >>> b = np.array([[3,0,0],[3.5,1,0],[4,2,0]])\n    >>> c = track_dist_3pts(a, b)\n    >>> print('%.6f' % c)\n    2.721573\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_39track_dist_3pts = {__Pyx_NAMESTR("track_dist_3pts"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_39track_dist_3pts, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_38track_dist_3pts)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_39track_dist_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracka = 0;
  PyObject *__pyx_v_trackb = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("track_dist_3pts (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracka,&__pyx_n_s__trackb,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracka)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__trackb)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("track_dist_3pts", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "track_dist_3pts") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_tracka = values[0];
    __pyx_v_trackb = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("track_dist_3pts", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.track_dist_3pts", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_38track_dist_3pts(__pyx_self, __pyx_v_tracka, __pyx_v_trackb);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1324
 * 
 * 
 * def track_dist_3pts(tracka,trackb):             # <<<<<<<<<<<<<<
 *     ''' Calculate the euclidean distance between two 3pt tracks
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_38track_dist_3pts(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracka, PyObject *__pyx_v_trackb) {
  PyArrayObject *__pyx_v_a = 0;
  PyArrayObject *__pyx_v_b = 0;
  float __pyx_v_d[2];
  __Pyx_LocalBuf_ND __pyx_pybuffernd_a;
  __Pyx_Buffer __pyx_pybuffer_a;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_b;
  __Pyx_Buffer __pyx_pybuffer_b;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyArrayObject *__pyx_t_5 = NULL;
  int __pyx_t_6;
  PyObject *__pyx_t_7 = NULL;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  int __pyx_t_12;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("track_dist_3pts", 0);
  __pyx_pybuffer_a.pybuffer.buf = NULL;
  __pyx_pybuffer_a.refcount = 0;
  __pyx_pybuffernd_a.data = NULL;
  __pyx_pybuffernd_a.rcbuffer = &__pyx_pybuffer_a;
  __pyx_pybuffer_b.pybuffer.buf = NULL;
  __pyx_pybuffer_b.refcount = 0;
  __pyx_pybuffernd_b.data = NULL;
  __pyx_pybuffernd_b.rcbuffer = &__pyx_pybuffer_b;

  /* "dipy/tracking/distances.pyx":1352
 *     cdef float d[2]
 * 
 *     a=np.ascontiguousarray(tracka,dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     b=np.ascontiguousarray(trackb,dtype=f32_dt)
 * 
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_v_tracka);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_tracka);
  __Pyx_GIVEREF(__pyx_v_tracka);
  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_a.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_a.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_7, &__pyx_t_8, &__pyx_t_9);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_a.rcbuffer->pybuffer, (PyObject*)__pyx_v_a, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_7); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_9);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_7, __pyx_t_8, __pyx_t_9);
      }
    }
    __pyx_pybuffernd_a.diminfo[0].strides = __pyx_pybuffernd_a.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_a.diminfo[0].shape = __pyx_pybuffernd_a.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_a.diminfo[1].strides = __pyx_pybuffernd_a.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_a.diminfo[1].shape = __pyx_pybuffernd_a.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __pyx_v_a = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":1353
 * 
 *     a=np.ascontiguousarray(tracka,dtype=f32_dt)
 *     b=np.ascontiguousarray(trackb,dtype=f32_dt)             # <<<<<<<<<<<<<<
 * 
 *     track_direct_flip_3dist(asfp(a[0]),asfp(a[1]),asfp(a[2]),
 */
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(__pyx_v_trackb);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_trackb);
  __Pyx_GIVEREF(__pyx_v_trackb);
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_4), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_2);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_b.rcbuffer->pybuffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_b.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_6 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_8, &__pyx_t_7);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_b.rcbuffer->pybuffer, (PyObject*)__pyx_v_b, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_8); Py_XDECREF(__pyx_t_7);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_8, __pyx_t_7);
      }
    }
    __pyx_pybuffernd_b.diminfo[0].strides = __pyx_pybuffernd_b.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_b.diminfo[0].shape = __pyx_pybuffernd_b.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_b.diminfo[1].strides = __pyx_pybuffernd_b.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_b.diminfo[1].shape = __pyx_pybuffernd_b.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1353; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __pyx_v_b = ((PyArrayObject *)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "dipy/tracking/distances.pyx":1355
 *     b=np.ascontiguousarray(trackb,dtype=f32_dt)
 * 
 *     track_direct_flip_3dist(asfp(a[0]),asfp(a[1]),asfp(a[2]),             # <<<<<<<<<<<<<<
 *                             asfp(b[0]),asfp(b[1]),asfp(b[2]),d)
 * 
 */
  __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_a), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_a), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_a), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1355; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1356
 * 
 *     track_direct_flip_3dist(asfp(a[0]),asfp(a[1]),asfp(a[2]),
 *                             asfp(b[0]),asfp(b[1]),asfp(b[2]),d)             # <<<<<<<<<<<<<<
 * 
 *     if d[0]<d[1]:
 */
  __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_b), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_10 = __Pyx_GetItemInt(((PyObject *)__pyx_v_b), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_10);
  if (!(likely(((__pyx_t_10) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_10, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_11 = __Pyx_GetItemInt(((PyObject *)__pyx_v_b), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  if (!(likely(((__pyx_t_11) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_11, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1356; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3dist(__pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_2)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_1)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_3)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_10)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_11)), __pyx_v_d);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0;
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;

  /* "dipy/tracking/distances.pyx":1358
 *                             asfp(b[0]),asfp(b[1]),asfp(b[2]),d)
 * 
 *     if d[0]<d[1]:             # <<<<<<<<<<<<<<
 *         return d[0]
 *     else:
 */
  __pyx_t_12 = (((__pyx_v_d[0]) < (__pyx_v_d[1])) != 0);
  if (__pyx_t_12) {

    /* "dipy/tracking/distances.pyx":1359
 * 
 *     if d[0]<d[1]:
 *         return d[0]             # <<<<<<<<<<<<<<
 *     else:
 *         return d[1]
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_11 = PyFloat_FromDouble((__pyx_v_d[0])); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1359; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_r = __pyx_t_11;
    __pyx_t_11 = 0;
    goto __pyx_L0;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":1361
 *         return d[0]
 *     else:
 *         return d[1]             # <<<<<<<<<<<<<<
 * 
 * 
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_11 = PyFloat_FromDouble((__pyx_v_d[1])); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1361; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_r = __pyx_t_11;
    __pyx_t_11 = 0;
    goto __pyx_L0;
  }
  __pyx_L3:;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_10);
  __Pyx_XDECREF(__pyx_t_11);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_a.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_b.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.track_dist_3pts", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_a.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_b.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_a);
  __Pyx_XDECREF((PyObject *)__pyx_v_b);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1367
 * @cython.wraparound(False)
 * @cython.cdivision(True)
 * cdef void track_direct_flip_dist(float *a,float *b,long rows,float *out) nogil:             # <<<<<<<<<<<<<<
 *     r''' Direct and flip average distance between two tracks
 * 
 */

static void __pyx_f_4dipy_8tracking_9distances_track_direct_flip_dist(float *__pyx_v_a, float *__pyx_v_b, long __pyx_v_rows, float *__pyx_v_out) {
  long __pyx_v_i;
  long __pyx_v_j;
  float __pyx_v_sub;
  float __pyx_v_subf;
  float __pyx_v_distf;
  float __pyx_v_dist;
  float __pyx_v_tmprow;
  float __pyx_v_tmprowf;
  long __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1405
 *     '''
 *     cdef:
 *         long i=0,j=0             # <<<<<<<<<<<<<<
 *         float sub=0,subf=0,distf=0,dist=0,tmprow=0, tmprowf=0
 * 
 */
  __pyx_v_i = 0;
  __pyx_v_j = 0;

  /* "dipy/tracking/distances.pyx":1406
 *     cdef:
 *         long i=0,j=0
 *         float sub=0,subf=0,distf=0,dist=0,tmprow=0, tmprowf=0             # <<<<<<<<<<<<<<
 * 
 *     for i from 0<=i<rows:
 */
  __pyx_v_sub = 0.0;
  __pyx_v_subf = 0.0;
  __pyx_v_distf = 0.0;
  __pyx_v_dist = 0.0;
  __pyx_v_tmprow = 0.0;
  __pyx_v_tmprowf = 0.0;

  /* "dipy/tracking/distances.pyx":1408
 *         float sub=0,subf=0,distf=0,dist=0,tmprow=0, tmprowf=0
 * 
 *     for i from 0<=i<rows:             # <<<<<<<<<<<<<<
 *         tmprow=0
 *         tmprowf=0
 */
  __pyx_t_1 = __pyx_v_rows;
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_1; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":1409
 * 
 *     for i from 0<=i<rows:
 *         tmprow=0             # <<<<<<<<<<<<<<
 *         tmprowf=0
 *         for j from 0<=j<3:
 */
    __pyx_v_tmprow = 0.0;

    /* "dipy/tracking/distances.pyx":1410
 *     for i from 0<=i<rows:
 *         tmprow=0
 *         tmprowf=0             # <<<<<<<<<<<<<<
 *         for j from 0<=j<3:
 *             sub=a[i*3+j]-b[i*3+j]
 */
    __pyx_v_tmprowf = 0.0;

    /* "dipy/tracking/distances.pyx":1411
 *         tmprow=0
 *         tmprowf=0
 *         for j from 0<=j<3:             # <<<<<<<<<<<<<<
 *             sub=a[i*3+j]-b[i*3+j]
 *             subf=a[i*3+j]-b[(rows-1-i)*3+j]
 */
    for (__pyx_v_j = 0; __pyx_v_j < 3; __pyx_v_j++) {

      /* "dipy/tracking/distances.pyx":1412
 *         tmprowf=0
 *         for j from 0<=j<3:
 *             sub=a[i*3+j]-b[i*3+j]             # <<<<<<<<<<<<<<
 *             subf=a[i*3+j]-b[(rows-1-i)*3+j]
 *             tmprow+=sub*sub
 */
      __pyx_v_sub = ((__pyx_v_a[((__pyx_v_i * 3) + __pyx_v_j)]) - (__pyx_v_b[((__pyx_v_i * 3) + __pyx_v_j)]));

      /* "dipy/tracking/distances.pyx":1413
 *         for j from 0<=j<3:
 *             sub=a[i*3+j]-b[i*3+j]
 *             subf=a[i*3+j]-b[(rows-1-i)*3+j]             # <<<<<<<<<<<<<<
 *             tmprow+=sub*sub
 *             tmprowf+=subf*subf
 */
      __pyx_v_subf = ((__pyx_v_a[((__pyx_v_i * 3) + __pyx_v_j)]) - (__pyx_v_b[((((__pyx_v_rows - 1) - __pyx_v_i) * 3) + __pyx_v_j)]));

      /* "dipy/tracking/distances.pyx":1414
 *             sub=a[i*3+j]-b[i*3+j]
 *             subf=a[i*3+j]-b[(rows-1-i)*3+j]
 *             tmprow+=sub*sub             # <<<<<<<<<<<<<<
 *             tmprowf+=subf*subf
 *         dist+=sqrt(tmprow)
 */
      __pyx_v_tmprow = (__pyx_v_tmprow + (__pyx_v_sub * __pyx_v_sub));

      /* "dipy/tracking/distances.pyx":1415
 *             subf=a[i*3+j]-b[(rows-1-i)*3+j]
 *             tmprow+=sub*sub
 *             tmprowf+=subf*subf             # <<<<<<<<<<<<<<
 *         dist+=sqrt(tmprow)
 *         distf+=sqrt(tmprowf)
 */
      __pyx_v_tmprowf = (__pyx_v_tmprowf + (__pyx_v_subf * __pyx_v_subf));
    }

    /* "dipy/tracking/distances.pyx":1416
 *             tmprow+=sub*sub
 *             tmprowf+=subf*subf
 *         dist+=sqrt(tmprow)             # <<<<<<<<<<<<<<
 *         distf+=sqrt(tmprowf)
 * 
 */
    __pyx_v_dist = (__pyx_v_dist + sqrt(__pyx_v_tmprow));

    /* "dipy/tracking/distances.pyx":1417
 *             tmprowf+=subf*subf
 *         dist+=sqrt(tmprow)
 *         distf+=sqrt(tmprowf)             # <<<<<<<<<<<<<<
 * 
 *     out[0]=dist/<float>rows
 */
    __pyx_v_distf = (__pyx_v_distf + sqrt(__pyx_v_tmprowf));
  }

  /* "dipy/tracking/distances.pyx":1419
 *         distf+=sqrt(tmprowf)
 * 
 *     out[0]=dist/<float>rows             # <<<<<<<<<<<<<<
 *     out[1]=distf/<float>rows
 * 
 */
  (__pyx_v_out[0]) = (__pyx_v_dist / ((float)__pyx_v_rows));

  /* "dipy/tracking/distances.pyx":1420
 * 
 *     out[0]=dist/<float>rows
 *     out[1]=distf/<float>rows             # <<<<<<<<<<<<<<
 * 
 * 
 */
  (__pyx_v_out[1]) = (__pyx_v_distf / ((float)__pyx_v_rows));

}

/* "dipy/tracking/distances.pyx":1424
 * 
 * @cython.cdivision(True)
 * cdef inline void track_direct_flip_3dist(float *a1, float *b1,float  *c1,float *a2, float *b2, float *c2, float *out) nogil:             # <<<<<<<<<<<<<<
 *     ''' Calculate the euclidean distance between two 3pt tracks
 *     both direct and flip are given as output
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3dist(float *__pyx_v_a1, float *__pyx_v_b1, float *__pyx_v_c1, float *__pyx_v_a2, float *__pyx_v_b2, float *__pyx_v_c2, float *__pyx_v_out) {
  int __pyx_v_i;
  float __pyx_v_tmp1;
  float __pyx_v_tmp2;
  float __pyx_v_tmp3;
  float __pyx_v_tmp1f;
  float __pyx_v_tmp3f;

  /* "dipy/tracking/distances.pyx":1441
 *     cdef:
 *         int i
 *         float tmp1=0,tmp2=0,tmp3=0,tmp1f=0,tmp3f=0             # <<<<<<<<<<<<<<
 * 
 *     #for i in range(3):
 */
  __pyx_v_tmp1 = 0.0;
  __pyx_v_tmp2 = 0.0;
  __pyx_v_tmp3 = 0.0;
  __pyx_v_tmp1f = 0.0;
  __pyx_v_tmp3f = 0.0;

  /* "dipy/tracking/distances.pyx":1444
 * 
 *     #for i in range(3):
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":1445
 *     #for i in range(3):
 *     for i from 0<=i<3:
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])             # <<<<<<<<<<<<<<
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 */
    __pyx_v_tmp1 = (__pyx_v_tmp1 + (((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i])) * ((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1446
 *     for i from 0<=i<3:
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])             # <<<<<<<<<<<<<<
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])
 */
    __pyx_v_tmp2 = (__pyx_v_tmp2 + (((__pyx_v_b1[__pyx_v_i]) - (__pyx_v_b2[__pyx_v_i])) * ((__pyx_v_b1[__pyx_v_i]) - (__pyx_v_b2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1447
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])             # <<<<<<<<<<<<<<
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])
 */
    __pyx_v_tmp3 = (__pyx_v_tmp3 + (((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i])) * ((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1448
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])             # <<<<<<<<<<<<<<
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])
 * 
 */
    __pyx_v_tmp1f = (__pyx_v_tmp1f + (((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i])) * ((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1449
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])             # <<<<<<<<<<<<<<
 * 
 *     out[0]=(sqrt(tmp1)+sqrt(tmp2)+sqrt(tmp3))/3.0
 */
    __pyx_v_tmp3f = (__pyx_v_tmp3f + (((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i])) * ((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i]))));
  }

  /* "dipy/tracking/distances.pyx":1451
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])
 * 
 *     out[0]=(sqrt(tmp1)+sqrt(tmp2)+sqrt(tmp3))/3.0             # <<<<<<<<<<<<<<
 *     out[1]=(sqrt(tmp1f)+sqrt(tmp2)+sqrt(tmp3f))/3.0
 * 
 */
  (__pyx_v_out[0]) = (((sqrt(__pyx_v_tmp1) + sqrt(__pyx_v_tmp2)) + sqrt(__pyx_v_tmp3)) / 3.0);

  /* "dipy/tracking/distances.pyx":1452
 * 
 *     out[0]=(sqrt(tmp1)+sqrt(tmp2)+sqrt(tmp3))/3.0
 *     out[1]=(sqrt(tmp1f)+sqrt(tmp2)+sqrt(tmp3f))/3.0             # <<<<<<<<<<<<<<
 * 
 *     #out[0]=(tmp1+tmp2+tmp3)/3.0
 */
  (__pyx_v_out[1]) = (((sqrt(__pyx_v_tmp1f) + sqrt(__pyx_v_tmp2)) + sqrt(__pyx_v_tmp3f)) / 3.0);

}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_41local_skeleton_clustering(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_40local_skeleton_clustering[] = "Efficient tractography clustering\n\n    Every track can needs to have the same number of points.\n    Use `dipy.tracking.metrics.downsample` to restrict the number of points\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape (N,3) .. (N,3) where N=points\n    d_thr : float\n        average euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n        Clusters.\n\n    Examples\n    --------\n    >>> tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]]),\n    ...         np.array([[3,0,0],[3.5,1,0],[4,2,0]]),\n    ...         np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]]),\n    ...         np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]]),\n    ...         np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]]),\n    ...         np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]]),\n    ...         np.array([[0,0,0],[0,1,0],[0,2,0]])]\n    >>> C = local_skeleton_clustering(tracks, d_thr=0.5)\n\n    Notes\n    -----\n    The distance calculated between two tracks::\n\n        t_1       t_2\n\n        0*   a    *0\n          \\       |\n           \\      |\n           1*     |\n            |  b  *1\n            |      \\\n            2*      \\\n                c    *2\n\n    is equal to $(a+b+c)/3$ where $a$ the euclidean distance between ``t_1[0]``\n    and ``t_2[0]``, $b$ between ``t_1[1]`` and ``t_2[1]`` and $c$ between\n    ``t_1[2]`` and ``t_2[2]``. Also the same with t2 flipped (so ``t_1[0]``\n    compared to ``t_2[2]`` etc).\n\n    Visualization:\n\n    It is possible to visualize the clustering C from the example\n    above using the fvtk module::\n\n        from dipy.viz import fvtk\n        r=fvtk.ren()\n        for c in C:\n            color=np.random.rand(3)\n            for i in C[c]['indices']:\n                fvtk.add(r,fvtk.line(tracks[i],color))\n        fvtk.show(r)\n\n    See Also\n    --------\n    dipy.tracking.metrics.downsample\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_41local_skeleton_clustering = {__Pyx_NAMESTR("local_skeleton_clustering"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_41local_skeleton_clustering, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_40local_skeleton_clustering)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_41local_skeleton_clustering(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_d_thr = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("local_skeleton_clustering (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__d_thr,0};
    PyObject* values[2] = {0,0};
    values[1] = ((PyObject *)__pyx_int_10);
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__d_thr);
          if (value) { values[1] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "local_skeleton_clustering") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1471; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_tracks = values[0];
    __pyx_v_d_thr = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("local_skeleton_clustering", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1471; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_40local_skeleton_clustering(__pyx_self, __pyx_v_tracks, __pyx_v_d_thr);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1471
 * @cython.wraparound(False)
 * @cython.cdivision(True)
 * def local_skeleton_clustering(tracks, d_thr=10):             # <<<<<<<<<<<<<<
 *     r"""Efficient tractography clustering
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_40local_skeleton_clustering(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_d_thr) {
  PyArrayObject *__pyx_v_track = 0;
  __pyx_t_4dipy_8tracking_9distances_LSC_Cluster *__pyx_v_cluster;
  long __pyx_v_lent;
  long __pyx_v_lenC;
  long __pyx_v_dim;
  long __pyx_v_points;
  long __pyx_v_i;
  long __pyx_v_j;
  CYTHON_UNUSED long __pyx_v_c;
  long __pyx_v_i_k;
  long __pyx_v_rows;
  long __pyx_v_cit;
  float *__pyx_v_ptr;
  float *__pyx_v_hid;
  float *__pyx_v_alld;
  float __pyx_v_d[2];
  float __pyx_v_m_d;
  float __pyx_v_cd_thr;
  long *__pyx_v_flip;
  long __pyx_v_it;
  long __pyx_v_k;
  PyObject *__pyx_v_C = NULL;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  Py_ssize_t __pyx_t_2;
  float __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyObject *__pyx_t_6 = NULL;
  PyArrayObject *__pyx_t_7 = NULL;
  int __pyx_t_8;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  long __pyx_t_12;
  long __pyx_t_13;
  long __pyx_t_14;
  long __pyx_t_15;
  int __pyx_t_16;
  long __pyx_t_17;
  PyObject *__pyx_t_18 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("local_skeleton_clustering", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;

  /* "dipy/tracking/distances.pyx":1540
 *         cnp.ndarray[cnp.float32_t, ndim=2] track
 *         LSC_Cluster *cluster
 *         long lent = 0,lenC = 0, dim = 0, points=0             # <<<<<<<<<<<<<<
 *         long i=0, j=0, c=0, i_k=0, rows=0 ,cit=0
 *         float *ptr, *hid, *alld
 */
  __pyx_v_lent = 0;
  __pyx_v_lenC = 0;
  __pyx_v_dim = 0;
  __pyx_v_points = 0;

  /* "dipy/tracking/distances.pyx":1541
 *         LSC_Cluster *cluster
 *         long lent = 0,lenC = 0, dim = 0, points=0
 *         long i=0, j=0, c=0, i_k=0, rows=0 ,cit=0             # <<<<<<<<<<<<<<
 *         float *ptr, *hid, *alld
 *         float d[2],m_d,cd_thr
 */
  __pyx_v_i = 0;
  __pyx_v_j = 0;
  __pyx_v_c = 0;
  __pyx_v_i_k = 0;
  __pyx_v_rows = 0;
  __pyx_v_cit = 0;

  /* "dipy/tracking/distances.pyx":1546
 *         long *flip
 * 
 *     points=len(tracks[0])             # <<<<<<<<<<<<<<
 *     dim = points*3
 *     rows = points
 */
  __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_tracks, 0, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1546; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_v_points = __pyx_t_2;

  /* "dipy/tracking/distances.pyx":1547
 * 
 *     points=len(tracks[0])
 *     dim = points*3             # <<<<<<<<<<<<<<
 *     rows = points
 *     cd_thr = d_thr
 */
  __pyx_v_dim = (__pyx_v_points * 3);

  /* "dipy/tracking/distances.pyx":1548
 *     points=len(tracks[0])
 *     dim = points*3
 *     rows = points             # <<<<<<<<<<<<<<
 *     cd_thr = d_thr
 * 
 */
  __pyx_v_rows = __pyx_v_points;

  /* "dipy/tracking/distances.pyx":1549
 *     dim = points*3
 *     rows = points
 *     cd_thr = d_thr             # <<<<<<<<<<<<<<
 * 
 *     #Allocate and copy memory for first cluster
 */
  __pyx_t_3 = __pyx_PyFloat_AsFloat(__pyx_v_d_thr); if (unlikely((__pyx_t_3 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1549; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cd_thr = __pyx_t_3;

  /* "dipy/tracking/distances.pyx":1552
 * 
 *     #Allocate and copy memory for first cluster
 *     cluster=<LSC_Cluster *>realloc(NULL,sizeof(LSC_Cluster))             # <<<<<<<<<<<<<<
 *     cluster[0].indices=<long *>realloc(NULL,sizeof(long))
 *     cluster[0].hidden=<float *>realloc(NULL,dim*sizeof(float))
 */
  __pyx_v_cluster = ((__pyx_t_4dipy_8tracking_9distances_LSC_Cluster *)realloc(NULL, (sizeof(__pyx_t_4dipy_8tracking_9distances_LSC_Cluster))));

  /* "dipy/tracking/distances.pyx":1553
 *     #Allocate and copy memory for first cluster
 *     cluster=<LSC_Cluster *>realloc(NULL,sizeof(LSC_Cluster))
 *     cluster[0].indices=<long *>realloc(NULL,sizeof(long))             # <<<<<<<<<<<<<<
 *     cluster[0].hidden=<float *>realloc(NULL,dim*sizeof(float))
 *     cluster[0].indices[0]=0
 */
  (__pyx_v_cluster[0]).indices = ((long *)realloc(NULL, (sizeof(long))));

  /* "dipy/tracking/distances.pyx":1554
 *     cluster=<LSC_Cluster *>realloc(NULL,sizeof(LSC_Cluster))
 *     cluster[0].indices=<long *>realloc(NULL,sizeof(long))
 *     cluster[0].hidden=<float *>realloc(NULL,dim*sizeof(float))             # <<<<<<<<<<<<<<
 *     cluster[0].indices[0]=0
 *     track=np.ascontiguousarray(tracks[0],dtype=f32_dt)
 */
  (__pyx_v_cluster[0]).hidden = ((float *)realloc(NULL, (__pyx_v_dim * (sizeof(float)))));

  /* "dipy/tracking/distances.pyx":1555
 *     cluster[0].indices=<long *>realloc(NULL,sizeof(long))
 *     cluster[0].hidden=<float *>realloc(NULL,dim*sizeof(float))
 *     cluster[0].indices[0]=0             # <<<<<<<<<<<<<<
 *     track=np.ascontiguousarray(tracks[0],dtype=f32_dt)
 *     ptr=<float *>track.data
 */
  ((__pyx_v_cluster[0]).indices[0]) = 0;

  /* "dipy/tracking/distances.pyx":1556
 *     cluster[0].hidden=<float *>realloc(NULL,dim*sizeof(float))
 *     cluster[0].indices[0]=0
 *     track=np.ascontiguousarray(tracks[0],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     ptr=<float *>track.data
 *     for i from 0<=i<dim:
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_tracks, 0, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_6) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_6, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_6);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_7, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
    if (unlikely(__pyx_t_8 < 0)) {
      PyErr_Fetch(&__pyx_t_9, &__pyx_t_10, &__pyx_t_11);
      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
        Py_XDECREF(__pyx_t_9); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11);
        __Pyx_RaiseBufferFallbackError();
      } else {
        PyErr_Restore(__pyx_t_9, __pyx_t_10, __pyx_t_11);
      }
    }
    __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_7 = 0;
  __pyx_v_track = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;

  /* "dipy/tracking/distances.pyx":1557
 *     cluster[0].indices[0]=0
 *     track=np.ascontiguousarray(tracks[0],dtype=f32_dt)
 *     ptr=<float *>track.data             # <<<<<<<<<<<<<<
 *     for i from 0<=i<dim:
 *         cluster[0].hidden[i]=ptr[i]
 */
  __pyx_v_ptr = ((float *)__pyx_v_track->data);

  /* "dipy/tracking/distances.pyx":1558
 *     track=np.ascontiguousarray(tracks[0],dtype=f32_dt)
 *     ptr=<float *>track.data
 *     for i from 0<=i<dim:             # <<<<<<<<<<<<<<
 *         cluster[0].hidden[i]=ptr[i]
 *     cluster[0].N=1
 */
  __pyx_t_12 = __pyx_v_dim;
  for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_12; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":1559
 *     ptr=<float *>track.data
 *     for i from 0<=i<dim:
 *         cluster[0].hidden[i]=ptr[i]             # <<<<<<<<<<<<<<
 *     cluster[0].N=1
 * 
 */
    ((__pyx_v_cluster[0]).hidden[__pyx_v_i]) = (__pyx_v_ptr[__pyx_v_i]);
  }

  /* "dipy/tracking/distances.pyx":1560
 *     for i from 0<=i<dim:
 *         cluster[0].hidden[i]=ptr[i]
 *     cluster[0].N=1             # <<<<<<<<<<<<<<
 * 
 *     #holds number of clusters
 */
  (__pyx_v_cluster[0]).N = 1;

  /* "dipy/tracking/distances.pyx":1563
 * 
 *     #holds number of clusters
 *     lenC = 1             # <<<<<<<<<<<<<<
 * 
 *     #store memmory for the hid variable
 */
  __pyx_v_lenC = 1;

  /* "dipy/tracking/distances.pyx":1566
 * 
 *     #store memmory for the hid variable
 *     hid=<float *>realloc(NULL,dim*sizeof(float))             # <<<<<<<<<<<<<<
 * 
 *     #Work with the rest of the tracks
 */
  __pyx_v_hid = ((float *)realloc(NULL, (__pyx_v_dim * (sizeof(float)))));

  /* "dipy/tracking/distances.pyx":1569
 * 
 *     #Work with the rest of the tracks
 *     lent=len(tracks)             # <<<<<<<<<<<<<<
 *     for it in range(1,lent):
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 */
  __pyx_t_2 = PyObject_Length(__pyx_v_tracks); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1569; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_2;

  /* "dipy/tracking/distances.pyx":1570
 *     #Work with the rest of the tracks
 *     lent=len(tracks)
 *     for it in range(1,lent):             # <<<<<<<<<<<<<<
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         ptr=<float *>track.data
 */
  __pyx_t_12 = __pyx_v_lent;
  for (__pyx_t_13 = 1; __pyx_t_13 < __pyx_t_12; __pyx_t_13+=1) {
    __pyx_v_it = __pyx_t_13;

    /* "dipy/tracking/distances.pyx":1571
 *     lent=len(tracks)
 *     for it in range(1,lent):
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         ptr=<float *>track.data
 *         cit=it
 */
    __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_tracks, __pyx_v_it, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_6);
    __Pyx_GIVEREF(__pyx_t_6);
    __pyx_t_6 = 0;
    __pyx_t_6 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_6));
    if (PyDict_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_7 = ((PyArrayObject *)__pyx_t_4);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_7, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_8 < 0)) {
        PyErr_Fetch(&__pyx_t_11, &__pyx_t_10, &__pyx_t_9);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_9);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_11, __pyx_t_10, __pyx_t_9);
        }
      }
      __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1571; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_7 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1572
 *     for it in range(1,lent):
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         ptr=<float *>track.data             # <<<<<<<<<<<<<<
 *         cit=it
 * 
 */
    __pyx_v_ptr = ((float *)__pyx_v_track->data);

    /* "dipy/tracking/distances.pyx":1573
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         ptr=<float *>track.data
 *         cit=it             # <<<<<<<<<<<<<<
 * 
 *         with nogil:
 */
    __pyx_v_cit = __pyx_v_it;

    /* "dipy/tracking/distances.pyx":1575
 *         cit=it
 * 
 *         with nogil:             # <<<<<<<<<<<<<<
 * 
 *             alld=<float *>calloc(lenC,sizeof(float))
 */
    {
        #ifdef WITH_THREAD
        PyThreadState *_save;
        Py_UNBLOCK_THREADS
        #endif
        /*try:*/ {

          /* "dipy/tracking/distances.pyx":1577
 *         with nogil:
 * 
 *             alld=<float *>calloc(lenC,sizeof(float))             # <<<<<<<<<<<<<<
 *             flip=<long *>calloc(lenC,sizeof(long))
 *             for k from 0<=k<lenC:
 */
          __pyx_v_alld = ((float *)calloc(__pyx_v_lenC, (sizeof(float))));

          /* "dipy/tracking/distances.pyx":1578
 * 
 *             alld=<float *>calloc(lenC,sizeof(float))
 *             flip=<long *>calloc(lenC,sizeof(long))             # <<<<<<<<<<<<<<
 *             for k from 0<=k<lenC:
 *                 for i from 0<=i<dim:
 */
          __pyx_v_flip = ((long *)calloc(__pyx_v_lenC, (sizeof(long))));

          /* "dipy/tracking/distances.pyx":1579
 *             alld=<float *>calloc(lenC,sizeof(float))
 *             flip=<long *>calloc(lenC,sizeof(long))
 *             for k from 0<=k<lenC:             # <<<<<<<<<<<<<<
 *                 for i from 0<=i<dim:
 *                     hid[i]=cluster[k].hidden[i]/<float>cluster[k].N
 */
          __pyx_t_14 = __pyx_v_lenC;
          for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_14; __pyx_v_k++) {

            /* "dipy/tracking/distances.pyx":1580
 *             flip=<long *>calloc(lenC,sizeof(long))
 *             for k from 0<=k<lenC:
 *                 for i from 0<=i<dim:             # <<<<<<<<<<<<<<
 *                     hid[i]=cluster[k].hidden[i]/<float>cluster[k].N
 * 
 */
            __pyx_t_15 = __pyx_v_dim;
            for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_15; __pyx_v_i++) {

              /* "dipy/tracking/distances.pyx":1581
 *             for k from 0<=k<lenC:
 *                 for i from 0<=i<dim:
 *                     hid[i]=cluster[k].hidden[i]/<float>cluster[k].N             # <<<<<<<<<<<<<<
 * 
 *                 #track_direct_flip_3dist(&ptr[0],&ptr[3],&ptr[6],&hid[0],&hid[3],&hid[6],d)
 */
              (__pyx_v_hid[__pyx_v_i]) = (((__pyx_v_cluster[__pyx_v_k]).hidden[__pyx_v_i]) / ((float)(__pyx_v_cluster[__pyx_v_k]).N));
            }

            /* "dipy/tracking/distances.pyx":1585
 *                 #track_direct_flip_3dist(&ptr[0],&ptr[3],&ptr[6],&hid[0],&hid[3],&hid[6],d)
 *                 #track_direct_flip_3dist(ptr,ptr+3,ptr+6,hid,hid+3,hid+6,<float *>d)
 *                 track_direct_flip_dist(ptr, hid,rows,<float *>d)             # <<<<<<<<<<<<<<
 * 
 *                 if d[1]<d[0]:
 */
            __pyx_f_4dipy_8tracking_9distances_track_direct_flip_dist(__pyx_v_ptr, __pyx_v_hid, __pyx_v_rows, ((float *)__pyx_v_d));

            /* "dipy/tracking/distances.pyx":1587
 *                 track_direct_flip_dist(ptr, hid,rows,<float *>d)
 * 
 *                 if d[1]<d[0]:             # <<<<<<<<<<<<<<
 *                     d[0]=d[1]
 *                     flip[k]=1
 */
            __pyx_t_16 = (((__pyx_v_d[1]) < (__pyx_v_d[0])) != 0);
            if (__pyx_t_16) {

              /* "dipy/tracking/distances.pyx":1588
 * 
 *                 if d[1]<d[0]:
 *                     d[0]=d[1]             # <<<<<<<<<<<<<<
 *                     flip[k]=1
 *                 alld[k]=d[0]
 */
              (__pyx_v_d[0]) = (__pyx_v_d[1]);

              /* "dipy/tracking/distances.pyx":1589
 *                 if d[1]<d[0]:
 *                     d[0]=d[1]
 *                     flip[k]=1             # <<<<<<<<<<<<<<
 *                 alld[k]=d[0]
 * 
 */
              (__pyx_v_flip[__pyx_v_k]) = 1;
              goto __pyx_L16;
            }
            __pyx_L16:;

            /* "dipy/tracking/distances.pyx":1590
 *                     d[0]=d[1]
 *                     flip[k]=1
 *                 alld[k]=d[0]             # <<<<<<<<<<<<<<
 * 
 *             m_d = biggest_float
 */
            (__pyx_v_alld[__pyx_v_k]) = (__pyx_v_d[0]);
          }

          /* "dipy/tracking/distances.pyx":1592
 *                 alld[k]=d[0]
 * 
 *             m_d = biggest_float             # <<<<<<<<<<<<<<
 *             #find minimum distance and index
 *             for k from 0<=k<lenC:
 */
          __pyx_v_m_d = 3.4028235e+38;

          /* "dipy/tracking/distances.pyx":1594
 *             m_d = biggest_float
 *             #find minimum distance and index
 *             for k from 0<=k<lenC:             # <<<<<<<<<<<<<<
 *                 if alld[k] < m_d:
 *                     m_d=alld[k]
 */
          __pyx_t_14 = __pyx_v_lenC;
          for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_14; __pyx_v_k++) {

            /* "dipy/tracking/distances.pyx":1595
 *             #find minimum distance and index
 *             for k from 0<=k<lenC:
 *                 if alld[k] < m_d:             # <<<<<<<<<<<<<<
 *                     m_d=alld[k]
 *                     i_k=k
 */
            __pyx_t_16 = (((__pyx_v_alld[__pyx_v_k]) < __pyx_v_m_d) != 0);
            if (__pyx_t_16) {

              /* "dipy/tracking/distances.pyx":1596
 *             for k from 0<=k<lenC:
 *                 if alld[k] < m_d:
 *                     m_d=alld[k]             # <<<<<<<<<<<<<<
 *                     i_k=k
 * 
 */
              __pyx_v_m_d = (__pyx_v_alld[__pyx_v_k]);

              /* "dipy/tracking/distances.pyx":1597
 *                 if alld[k] < m_d:
 *                     m_d=alld[k]
 *                     i_k=k             # <<<<<<<<<<<<<<
 * 
 *             if m_d < cd_thr:
 */
              __pyx_v_i_k = __pyx_v_k;
              goto __pyx_L19;
            }
            __pyx_L19:;
          }

          /* "dipy/tracking/distances.pyx":1599
 *                     i_k=k
 * 
 *             if m_d < cd_thr:             # <<<<<<<<<<<<<<
 *                 if flip[i_k]==1:#correct if flipping is needed
 *                     for i from 0<=i<rows:
 */
          __pyx_t_16 = ((__pyx_v_m_d < __pyx_v_cd_thr) != 0);
          if (__pyx_t_16) {

            /* "dipy/tracking/distances.pyx":1600
 * 
 *             if m_d < cd_thr:
 *                 if flip[i_k]==1:#correct if flipping is needed             # <<<<<<<<<<<<<<
 *                     for i from 0<=i<rows:
 *                         for j from 0<=j<3:
 */
            __pyx_t_16 = (((__pyx_v_flip[__pyx_v_i_k]) == 1) != 0);
            if (__pyx_t_16) {

              /* "dipy/tracking/distances.pyx":1601
 *             if m_d < cd_thr:
 *                 if flip[i_k]==1:#correct if flipping is needed
 *                     for i from 0<=i<rows:             # <<<<<<<<<<<<<<
 *                         for j from 0<=j<3:
 *                             cluster[i_k].hidden[i*3+j]+=ptr[(rows-1-i)*3+j]
 */
              __pyx_t_14 = __pyx_v_rows;
              for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_14; __pyx_v_i++) {

                /* "dipy/tracking/distances.pyx":1602
 *                 if flip[i_k]==1:#correct if flipping is needed
 *                     for i from 0<=i<rows:
 *                         for j from 0<=j<3:             # <<<<<<<<<<<<<<
 *                             cluster[i_k].hidden[i*3+j]+=ptr[(rows-1-i)*3+j]
 *                 else:
 */
                for (__pyx_v_j = 0; __pyx_v_j < 3; __pyx_v_j++) {

                  /* "dipy/tracking/distances.pyx":1603
 *                     for i from 0<=i<rows:
 *                         for j from 0<=j<3:
 *                             cluster[i_k].hidden[i*3+j]+=ptr[(rows-1-i)*3+j]             # <<<<<<<<<<<<<<
 *                 else:
 *                      for i from 0<=i<rows:
 */
                  __pyx_t_15 = __pyx_v_i_k;
                  __pyx_t_17 = ((__pyx_v_i * 3) + __pyx_v_j);
                  ((__pyx_v_cluster[__pyx_t_15]).hidden[__pyx_t_17]) = (((__pyx_v_cluster[__pyx_t_15]).hidden[__pyx_t_17]) + (__pyx_v_ptr[((((__pyx_v_rows - 1) - __pyx_v_i) * 3) + __pyx_v_j)]));
                }
              }
              goto __pyx_L21;
            }
            /*else*/ {

              /* "dipy/tracking/distances.pyx":1605
 *                             cluster[i_k].hidden[i*3+j]+=ptr[(rows-1-i)*3+j]
 *                 else:
 *                      for i from 0<=i<rows:             # <<<<<<<<<<<<<<
 *                         for j from 0<=j<3:
 *                             cluster[i_k].hidden[i*3+j]+=ptr[i*3+j]
 */
              __pyx_t_14 = __pyx_v_rows;
              for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_14; __pyx_v_i++) {

                /* "dipy/tracking/distances.pyx":1606
 *                 else:
 *                      for i from 0<=i<rows:
 *                         for j from 0<=j<3:             # <<<<<<<<<<<<<<
 *                             cluster[i_k].hidden[i*3+j]+=ptr[i*3+j]
 *                 cluster[i_k].N+=1
 */
                for (__pyx_v_j = 0; __pyx_v_j < 3; __pyx_v_j++) {

                  /* "dipy/tracking/distances.pyx":1607
 *                      for i from 0<=i<rows:
 *                         for j from 0<=j<3:
 *                             cluster[i_k].hidden[i*3+j]+=ptr[i*3+j]             # <<<<<<<<<<<<<<
 *                 cluster[i_k].N+=1
 *                 cluster[i_k].indices=<long *>realloc(cluster[i_k].indices,cluster[i_k].N*sizeof(long))
 */
                  __pyx_t_15 = __pyx_v_i_k;
                  __pyx_t_17 = ((__pyx_v_i * 3) + __pyx_v_j);
                  ((__pyx_v_cluster[__pyx_t_15]).hidden[__pyx_t_17]) = (((__pyx_v_cluster[__pyx_t_15]).hidden[__pyx_t_17]) + (__pyx_v_ptr[((__pyx_v_i * 3) + __pyx_v_j)]));
                }
              }
            }
            __pyx_L21:;

            /* "dipy/tracking/distances.pyx":1608
 *                         for j from 0<=j<3:
 *                             cluster[i_k].hidden[i*3+j]+=ptr[i*3+j]
 *                 cluster[i_k].N+=1             # <<<<<<<<<<<<<<
 *                 cluster[i_k].indices=<long *>realloc(cluster[i_k].indices,cluster[i_k].N*sizeof(long))
 *                 cluster[i_k].indices[cluster[i_k].N-1]=cit
 */
            __pyx_t_14 = __pyx_v_i_k;
            (__pyx_v_cluster[__pyx_t_14]).N = ((__pyx_v_cluster[__pyx_t_14]).N + 1);

            /* "dipy/tracking/distances.pyx":1609
 *                             cluster[i_k].hidden[i*3+j]+=ptr[i*3+j]
 *                 cluster[i_k].N+=1
 *                 cluster[i_k].indices=<long *>realloc(cluster[i_k].indices,cluster[i_k].N*sizeof(long))             # <<<<<<<<<<<<<<
 *                 cluster[i_k].indices[cluster[i_k].N-1]=cit
 * 
 */
            (__pyx_v_cluster[__pyx_v_i_k]).indices = ((long *)realloc((__pyx_v_cluster[__pyx_v_i_k]).indices, ((__pyx_v_cluster[__pyx_v_i_k]).N * (sizeof(long)))));

            /* "dipy/tracking/distances.pyx":1610
 *                 cluster[i_k].N+=1
 *                 cluster[i_k].indices=<long *>realloc(cluster[i_k].indices,cluster[i_k].N*sizeof(long))
 *                 cluster[i_k].indices[cluster[i_k].N-1]=cit             # <<<<<<<<<<<<<<
 * 
 *             else:#New cluster added
 */
            ((__pyx_v_cluster[__pyx_v_i_k]).indices[((__pyx_v_cluster[__pyx_v_i_k]).N - 1)]) = __pyx_v_cit;
            goto __pyx_L20;
          }
          /*else*/ {

            /* "dipy/tracking/distances.pyx":1613
 * 
 *             else:#New cluster added
 *                 lenC+=1             # <<<<<<<<<<<<<<
 *                 cluster=<LSC_Cluster *>realloc(cluster,lenC*sizeof(LSC_Cluster))
 *                 cluster[lenC-1].indices=<long *>realloc(NULL,sizeof(long))
 */
            __pyx_v_lenC = (__pyx_v_lenC + 1);

            /* "dipy/tracking/distances.pyx":1614
 *             else:#New cluster added
 *                 lenC+=1
 *                 cluster=<LSC_Cluster *>realloc(cluster,lenC*sizeof(LSC_Cluster))             # <<<<<<<<<<<<<<
 *                 cluster[lenC-1].indices=<long *>realloc(NULL,sizeof(long))
 *                 cluster[lenC-1].hidden=<float *>realloc(NULL,dim*sizeof(float))
 */
            __pyx_v_cluster = ((__pyx_t_4dipy_8tracking_9distances_LSC_Cluster *)realloc(__pyx_v_cluster, (__pyx_v_lenC * (sizeof(__pyx_t_4dipy_8tracking_9distances_LSC_Cluster)))));

            /* "dipy/tracking/distances.pyx":1615
 *                 lenC+=1
 *                 cluster=<LSC_Cluster *>realloc(cluster,lenC*sizeof(LSC_Cluster))
 *                 cluster[lenC-1].indices=<long *>realloc(NULL,sizeof(long))             # <<<<<<<<<<<<<<
 *                 cluster[lenC-1].hidden=<float *>realloc(NULL,dim*sizeof(float))
 *                 cluster[lenC-1].indices[0]=cit
 */
            (__pyx_v_cluster[(__pyx_v_lenC - 1)]).indices = ((long *)realloc(NULL, (sizeof(long))));

            /* "dipy/tracking/distances.pyx":1616
 *                 cluster=<LSC_Cluster *>realloc(cluster,lenC*sizeof(LSC_Cluster))
 *                 cluster[lenC-1].indices=<long *>realloc(NULL,sizeof(long))
 *                 cluster[lenC-1].hidden=<float *>realloc(NULL,dim*sizeof(float))             # <<<<<<<<<<<<<<
 *                 cluster[lenC-1].indices[0]=cit
 *                 for i from 0<=i<dim:
 */
            (__pyx_v_cluster[(__pyx_v_lenC - 1)]).hidden = ((float *)realloc(NULL, (__pyx_v_dim * (sizeof(float)))));

            /* "dipy/tracking/distances.pyx":1617
 *                 cluster[lenC-1].indices=<long *>realloc(NULL,sizeof(long))
 *                 cluster[lenC-1].hidden=<float *>realloc(NULL,dim*sizeof(float))
 *                 cluster[lenC-1].indices[0]=cit             # <<<<<<<<<<<<<<
 *                 for i from 0<=i<dim:
 *                     cluster[lenC-1].hidden[i]=ptr[i]
 */
            ((__pyx_v_cluster[(__pyx_v_lenC - 1)]).indices[0]) = __pyx_v_cit;

            /* "dipy/tracking/distances.pyx":1618
 *                 cluster[lenC-1].hidden=<float *>realloc(NULL,dim*sizeof(float))
 *                 cluster[lenC-1].indices[0]=cit
 *                 for i from 0<=i<dim:             # <<<<<<<<<<<<<<
 *                     cluster[lenC-1].hidden[i]=ptr[i]
 *                 cluster[lenC-1].N=1
 */
            __pyx_t_14 = __pyx_v_dim;
            for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_14; __pyx_v_i++) {

              /* "dipy/tracking/distances.pyx":1619
 *                 cluster[lenC-1].indices[0]=cit
 *                 for i from 0<=i<dim:
 *                     cluster[lenC-1].hidden[i]=ptr[i]             # <<<<<<<<<<<<<<
 *                 cluster[lenC-1].N=1
 * 
 */
              ((__pyx_v_cluster[(__pyx_v_lenC - 1)]).hidden[__pyx_v_i]) = (__pyx_v_ptr[__pyx_v_i]);
            }

            /* "dipy/tracking/distances.pyx":1620
 *                 for i from 0<=i<dim:
 *                     cluster[lenC-1].hidden[i]=ptr[i]
 *                 cluster[lenC-1].N=1             # <<<<<<<<<<<<<<
 * 
 *             free(alld)
 */
            (__pyx_v_cluster[(__pyx_v_lenC - 1)]).N = 1;
          }
          __pyx_L20:;

          /* "dipy/tracking/distances.pyx":1622
 *                 cluster[lenC-1].N=1
 * 
 *             free(alld)             # <<<<<<<<<<<<<<
 *             free(flip)
 * 
 */
          free(__pyx_v_alld);

          /* "dipy/tracking/distances.pyx":1623
 * 
 *             free(alld)
 *             free(flip)             # <<<<<<<<<<<<<<
 * 
 * 
 */
          free(__pyx_v_flip);
        }

        /* "dipy/tracking/distances.pyx":1575
 *         cit=it
 * 
 *         with nogil:             # <<<<<<<<<<<<<<
 * 
 *             alld=<float *>calloc(lenC,sizeof(float))
 */
        /*finally:*/ {
          #ifdef WITH_THREAD
          Py_BLOCK_THREADS
          #endif
        }
    }
  }

  /* "dipy/tracking/distances.pyx":1628
 *     #Copy results to a dictionary
 * 
 *     C={}             # <<<<<<<<<<<<<<
 *     for k in range(lenC):
 * 
 */
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1628; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_v_C = ((PyObject*)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":1629
 * 
 *     C={}
 *     for k in range(lenC):             # <<<<<<<<<<<<<<
 * 
 *         C[k]={}
 */
  __pyx_t_12 = __pyx_v_lenC;
  for (__pyx_t_13 = 0; __pyx_t_13 < __pyx_t_12; __pyx_t_13+=1) {
    __pyx_v_k = __pyx_t_13;

    /* "dipy/tracking/distances.pyx":1631
 *     for k in range(lenC):
 * 
 *         C[k]={}             # <<<<<<<<<<<<<<
 *         C[k]['hidden']=np.zeros(points*3,dtype=np.float32)
 * 
 */
    __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    if (__Pyx_SetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, ((PyObject *)__pyx_t_4), sizeof(long), PyInt_FromLong, 0, 0, 0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1631; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1632
 * 
 *         C[k]={}
 *         C[k]['hidden']=np.zeros(points*3,dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *         for j in range(points*3):
 */
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyInt_FromLong((__pyx_v_points * 3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    __pyx_t_4 = 0;
    __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_18 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_18) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
    __pyx_t_18 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    if (PyObject_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__hidden), __pyx_t_18) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1632; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;

    /* "dipy/tracking/distances.pyx":1634
 *         C[k]['hidden']=np.zeros(points*3,dtype=np.float32)
 * 
 *         for j in range(points*3):             # <<<<<<<<<<<<<<
 *             C[k]['hidden'][j]=cluster[k].hidden[j]
 *         C[k]['hidden'].shape=(points,3)
 */
    __pyx_t_14 = (__pyx_v_points * 3);
    for (__pyx_t_15 = 0; __pyx_t_15 < __pyx_t_14; __pyx_t_15+=1) {
      __pyx_v_j = __pyx_t_15;

      /* "dipy/tracking/distances.pyx":1635
 * 
 *         for j in range(points*3):
 *             C[k]['hidden'][j]=cluster[k].hidden[j]             # <<<<<<<<<<<<<<
 *         C[k]['hidden'].shape=(points,3)
 * 
 */
      __pyx_t_18 = PyFloat_FromDouble(((__pyx_v_cluster[__pyx_v_k]).hidden[__pyx_v_j])); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_5 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      if (__Pyx_SetItemInt(__pyx_t_5, __pyx_v_j, __pyx_t_18, sizeof(long), PyInt_FromLong, 0, 0, 0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
    }

    /* "dipy/tracking/distances.pyx":1636
 *         for j in range(points*3):
 *             C[k]['hidden'][j]=cluster[k].hidden[j]
 *         C[k]['hidden'].shape=(points,3)             # <<<<<<<<<<<<<<
 * 
 *         C[k]['N']=cluster[k].N
 */
    __pyx_t_18 = PyInt_FromLong(__pyx_v_points); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_18);
    __Pyx_GIVEREF(__pyx_t_18);
    __Pyx_INCREF(__pyx_int_3);
    PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_3);
    __Pyx_GIVEREF(__pyx_int_3);
    __pyx_t_18 = 0;
    __pyx_t_18 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __pyx_t_4 = PyObject_GetItem(__pyx_t_18, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
    if (__Pyx_PyObject_SetAttrStr(__pyx_t_4, __pyx_n_s__shape, ((PyObject *)__pyx_t_5)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1636; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1638
 *         C[k]['hidden'].shape=(points,3)
 * 
 *         C[k]['N']=cluster[k].N             # <<<<<<<<<<<<<<
 *         C[k]['indices']=np.zeros(cluster[k].N,dtype=np.int64)
 * 
 */
    __pyx_t_4 = PyInt_FromLong((__pyx_v_cluster[__pyx_v_k]).N); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__N), __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1639
 * 
 *         C[k]['N']=cluster[k].N
 *         C[k]['indices']=np.zeros(cluster[k].N,dtype=np.int64)             # <<<<<<<<<<<<<<
 * 
 *         for i in range(cluster[k].N):
 */
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyInt_FromLong((__pyx_v_cluster[__pyx_v_k]).N); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_18 = PyTuple_New(1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    PyTuple_SET_ITEM(__pyx_t_18, 0, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    __pyx_t_4 = 0;
    __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    __pyx_t_6 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_6, __pyx_n_s__int64); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_18), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_18)); __pyx_t_18 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    if (PyObject_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__indices), __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

    /* "dipy/tracking/distances.pyx":1641
 *         C[k]['indices']=np.zeros(cluster[k].N,dtype=np.int64)
 * 
 *         for i in range(cluster[k].N):             # <<<<<<<<<<<<<<
 *             C[k]['indices'][i]=cluster[k].indices[i]
 * 
 */
    __pyx_t_14 = (__pyx_v_cluster[__pyx_v_k]).N;
    for (__pyx_t_15 = 0; __pyx_t_15 < __pyx_t_14; __pyx_t_15+=1) {
      __pyx_v_i = __pyx_t_15;

      /* "dipy/tracking/distances.pyx":1642
 * 
 *         for i in range(cluster[k].N):
 *             C[k]['indices'][i]=cluster[k].indices[i]             # <<<<<<<<<<<<<<
 * 
 *         C[k]['indices']=list(C[k]['indices'])
 */
      __pyx_t_1 = PyInt_FromLong(((__pyx_v_cluster[__pyx_v_k]).indices[__pyx_v_i])); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_18 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      if (__Pyx_SetItemInt(__pyx_t_18, __pyx_v_i, __pyx_t_1, sizeof(long), PyInt_FromLong, 0, 0, 0) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1642; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    }

    /* "dipy/tracking/distances.pyx":1644
 *             C[k]['indices'][i]=cluster[k].indices[i]
 * 
 *         C[k]['indices']=list(C[k]['indices'])             # <<<<<<<<<<<<<<
 * 
 *     #Free memory
 */
    __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_18 = PyObject_GetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_18);
    __Pyx_GIVEREF(__pyx_t_18);
    __pyx_t_18 = 0;
    __pyx_t_18 = PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
    __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong, 0, 0, 0); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    if (PyObject_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__indices), __pyx_t_18) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1644; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
  }

  /* "dipy/tracking/distances.pyx":1647
 * 
 *     #Free memory
 *     with nogil:             # <<<<<<<<<<<<<<
 * 
 *         for k from 0<=k<lenC:
 */
  {
      #ifdef WITH_THREAD
      PyThreadState *_save;
      Py_UNBLOCK_THREADS
      #endif
      /*try:*/ {

        /* "dipy/tracking/distances.pyx":1649
 *     with nogil:
 * 
 *         for k from 0<=k<lenC:             # <<<<<<<<<<<<<<
 *             free(cluster[k].indices)
 *             free(cluster[k].hidden)
 */
        __pyx_t_12 = __pyx_v_lenC;
        for (__pyx_v_k = 0; __pyx_v_k < __pyx_t_12; __pyx_v_k++) {

          /* "dipy/tracking/distances.pyx":1650
 * 
 *         for k from 0<=k<lenC:
 *             free(cluster[k].indices)             # <<<<<<<<<<<<<<
 *             free(cluster[k].hidden)
 *         free(cluster)
 */
          free((__pyx_v_cluster[__pyx_v_k]).indices);

          /* "dipy/tracking/distances.pyx":1651
 *         for k from 0<=k<lenC:
 *             free(cluster[k].indices)
 *             free(cluster[k].hidden)             # <<<<<<<<<<<<<<
 *         free(cluster)
 * 
 */
          free((__pyx_v_cluster[__pyx_v_k]).hidden);
        }

        /* "dipy/tracking/distances.pyx":1652
 *             free(cluster[k].indices)
 *             free(cluster[k].hidden)
 *         free(cluster)             # <<<<<<<<<<<<<<
 * 
 *     return C
 */
        free(__pyx_v_cluster);
      }

      /* "dipy/tracking/distances.pyx":1647
 * 
 *     #Free memory
 *     with nogil:             # <<<<<<<<<<<<<<
 * 
 *         for k from 0<=k<lenC:
 */
      /*finally:*/ {
        #ifdef WITH_THREAD
        Py_BLOCK_THREADS
        #endif
      }
  }

  /* "dipy/tracking/distances.pyx":1654
 *         free(cluster)
 * 
 *     return C             # <<<<<<<<<<<<<<
 * 
 * def local_skeleton_clustering_3pts(tracks, d_thr=10):
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_C));
  __pyx_r = ((PyObject *)__pyx_v_C);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __Pyx_XDECREF(__pyx_t_18);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_track);
  __Pyx_XDECREF(__pyx_v_C);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_43local_skeleton_clustering_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_42local_skeleton_clustering_3pts[] = " Does a first pass clustering\n\n    Every track can only have 3 pts neither less or more.\n    Use `dipy.tracking.metrics.downsample` to restrict the number of points\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape (N,3) .. (N,3) where N=3\n    d_thr : float\n        Average euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n       Clusters.\n\n    Examples\n    --------\n    >>> tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]]),\n    ...         np.array([[3,0,0],[3.5,1,0],[4,2,0]]),\n    ...         np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]]),\n    ...         np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]]),\n    ...         np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]]),\n    ...         np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]]),\n    ...         np.array([[0,0,0],[0,1,0],[0,2,0]])]\n    >>> C=local_skeleton_clustering_3pts(tracks,d_thr=0.5)\n\n    Notes\n    -----\n    It is possible to visualize the clustering C from the example\n    above using the fvtk module::\n\n        r=fvtk.ren()\n        for c in C:\n            color=np.random.rand(3)\n            for i in C[c]['indices']:\n                fvtk.add(r,fos.line(tracks[i],color))\n        fvtk.show(r)\n\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_43local_skeleton_clustering_3pts = {__Pyx_NAMESTR("local_skeleton_clustering_3pts"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_43local_skeleton_clustering_3pts, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_42local_skeleton_clustering_3pts)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_43local_skeleton_clustering_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_d_thr = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("local_skeleton_clustering_3pts (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__d_thr,0};
    PyObject* values[2] = {0,0};
    values[1] = ((PyObject *)__pyx_int_10);
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__d_thr);
          if (value) { values[1] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "local_skeleton_clustering_3pts") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_tracks = values[0];
    __pyx_v_d_thr = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("local_skeleton_clustering_3pts", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering_3pts", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_42local_skeleton_clustering_3pts(__pyx_self, __pyx_v_tracks, __pyx_v_d_thr);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1656
 *     return C
 * 
 * def local_skeleton_clustering_3pts(tracks, d_thr=10):             # <<<<<<<<<<<<<<
 *     ''' Does a first pass clustering
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_42local_skeleton_clustering_3pts(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_d_thr) {
  PyArrayObject *__pyx_v_track = 0;
  PyArrayObject *__pyx_v_h = 0;
  int __pyx_v_lent;
  int __pyx_v_k;
  int __pyx_v_it;
  float __pyx_v_d[2];
  PyObject *__pyx_v_C = NULL;
  PyObject *__pyx_v_ts = NULL;
  PyObject *__pyx_v_lenC = NULL;
  PyObject *__pyx_v_alld = NULL;
  PyObject *__pyx_v_flip = NULL;
  PyObject *__pyx_v_m_k = NULL;
  PyObject *__pyx_v_i_k = NULL;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_h;
  __Pyx_Buffer __pyx_pybuffer_h;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  Py_ssize_t __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  int __pyx_t_6;
  int __pyx_t_7;
  PyArrayObject *__pyx_t_8 = NULL;
  int __pyx_t_9;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  long __pyx_t_13;
  PyArrayObject *__pyx_t_14 = NULL;
  int __pyx_t_15;
  PyObject *__pyx_t_16 = NULL;
  PyObject *__pyx_t_17 = NULL;
  int __pyx_t_18;
  PyObject *__pyx_t_19 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("local_skeleton_clustering_3pts", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;
  __pyx_pybuffer_h.pybuffer.buf = NULL;
  __pyx_pybuffer_h.refcount = 0;
  __pyx_pybuffernd_h.data = NULL;
  __pyx_pybuffernd_h.rcbuffer = &__pyx_pybuffer_h;

  /* "dipy/tracking/distances.pyx":1705
 *         #float d_sq=d_thr**2
 * 
 *     lent=len(tracks)             # <<<<<<<<<<<<<<
 * 
 *     #Network C
 */
  __pyx_t_1 = PyObject_Length(__pyx_v_tracks); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1708
 * 
 *     #Network C
 *     C={0:{'indices':[0],'hidden':tracks[0].copy(),'N':1}}             # <<<<<<<<<<<<<<
 *     ts=np.zeros((3,3),dtype=np.float32)
 * 
 */
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  __pyx_t_4 = PyList_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(__pyx_int_0);
  PyList_SET_ITEM(__pyx_t_4, 0, __pyx_int_0);
  __Pyx_GIVEREF(__pyx_int_0);
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_4)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_tracks, 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__copy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__hidden), __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_2, __pyx_int_0, ((PyObject *)__pyx_t_3)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1708; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  __pyx_v_C = ((PyObject*)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "dipy/tracking/distances.pyx":1709
 *     #Network C
 *     C={0:{'indices':[0],'hidden':tracks[0].copy(),'N':1}}
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *     #for (it,t) in enumerate(tracks[1:]):
 */
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__float32); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_k_tuple_17), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __pyx_v_ts = __pyx_t_5;
  __pyx_t_5 = 0;

  /* "dipy/tracking/distances.pyx":1712
 * 
 *     #for (it,t) in enumerate(tracks[1:]):
 *     for it in range(1,lent):             # <<<<<<<<<<<<<<
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         lenC=len(C.keys())
 */
  __pyx_t_6 = __pyx_v_lent;
  for (__pyx_t_7 = 1; __pyx_t_7 < __pyx_t_6; __pyx_t_7+=1) {
    __pyx_v_it = __pyx_t_7;

    /* "dipy/tracking/distances.pyx":1713
 *     #for (it,t) in enumerate(tracks[1:]):
 *     for it in range(1,lent):
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         lenC=len(C.keys())
 *         #if it%1000==0:
 */
    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_tracks, __pyx_v_it, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_5);
    __Pyx_GIVEREF(__pyx_t_5);
    __pyx_t_5 = 0;
    __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_5));
    if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_3), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_8 = ((PyArrayObject *)__pyx_t_4);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
      __pyx_t_9 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_8, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_9 < 0)) {
        PyErr_Fetch(&__pyx_t_10, &__pyx_t_11, &__pyx_t_12);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_10); Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_12);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_10, __pyx_t_11, __pyx_t_12);
        }
      }
      __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_9 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1713; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_8 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1714
 *     for it in range(1,lent):
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         lenC=len(C.keys())             # <<<<<<<<<<<<<<
 *         #if it%1000==0:
 *         #    print it,lenC
 */
    __pyx_t_4 = __Pyx_PyDict_Keys(__pyx_v_C); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_1 = PyObject_Length(__pyx_t_4); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyInt_FromSsize_t(__pyx_t_1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1714; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_XDECREF(__pyx_v_lenC);
    __pyx_v_lenC = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1717
 *         #if it%1000==0:
 *         #    print it,lenC
 *         alld=np.zeros(lenC)             # <<<<<<<<<<<<<<
 *         flip=np.zeros(lenC)
 *         for k in range(lenC):
 */
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_INCREF(__pyx_v_lenC);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_lenC);
    __Pyx_GIVEREF(__pyx_v_lenC);
    __pyx_t_3 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1717; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __Pyx_XDECREF(__pyx_v_alld);
    __pyx_v_alld = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "dipy/tracking/distances.pyx":1718
 *         #    print it,lenC
 *         alld=np.zeros(lenC)
 *         flip=np.zeros(lenC)             # <<<<<<<<<<<<<<
 *         for k in range(lenC):
 *             h=np.ascontiguousarray(C[k]['hidden']/C[k]['N'],dtype=f32_dt)
 */
    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_INCREF(__pyx_v_lenC);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_lenC);
    __Pyx_GIVEREF(__pyx_v_lenC);
    __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1718; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    __Pyx_XDECREF(__pyx_v_flip);
    __pyx_v_flip = __pyx_t_5;
    __pyx_t_5 = 0;

    /* "dipy/tracking/distances.pyx":1719
 *         alld=np.zeros(lenC)
 *         flip=np.zeros(lenC)
 *         for k in range(lenC):             # <<<<<<<<<<<<<<
 *             h=np.ascontiguousarray(C[k]['hidden']/C[k]['N'],dtype=f32_dt)
 *             #print track
 */
    __pyx_t_13 = __Pyx_PyInt_AsLong(__pyx_v_lenC); if (unlikely((__pyx_t_13 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1719; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    for (__pyx_t_9 = 0; __pyx_t_9 < __pyx_t_13; __pyx_t_9+=1) {
      __pyx_v_k = __pyx_t_9;

      /* "dipy/tracking/distances.pyx":1720
 *         flip=np.zeros(lenC)
 *         for k in range(lenC):
 *             h=np.ascontiguousarray(C[k]['hidden']/C[k]['N'],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *             #print track
 *             #print h
 */
      __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_4 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_2 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = __Pyx_PyNumber_Divide(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_5);
      __Pyx_GIVEREF(__pyx_t_5);
      __pyx_t_5 = 0;
      __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_5));
      if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
      if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_14 = ((PyArrayObject *)__pyx_t_4);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
        __pyx_t_15 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_t_14, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_15 < 0)) {
          PyErr_Fetch(&__pyx_t_12, &__pyx_t_11, &__pyx_t_10);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_v_h, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_12); Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_10);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_12, __pyx_t_11, __pyx_t_10);
          }
        }
        __pyx_pybuffernd_h.diminfo[0].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_h.diminfo[0].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_h.diminfo[1].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_h.diminfo[1].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1720; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_14 = 0;
      __Pyx_XDECREF(((PyObject *)__pyx_v_h));
      __pyx_v_h = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":1724
 *             #print h
 *             track_direct_flip_3dist(
 *                 asfp(track[0]),asfp(track[1]),asfp(track[2]),             # <<<<<<<<<<<<<<
 *                 asfp(h[0]), asfp(h[1]),asfp(h[2]),<float *>d)
 *             #d=np.sum(np.sqrt(np.sum((t-h)**2,axis=1)))/3.0
 */
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1724; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

      /* "dipy/tracking/distances.pyx":1725
 *             track_direct_flip_3dist(
 *                 asfp(track[0]),asfp(track[1]),asfp(track[2]),
 *                 asfp(h[0]), asfp(h[1]),asfp(h[2]),<float *>d)             # <<<<<<<<<<<<<<
 *             #d=np.sum(np.sqrt(np.sum((t-h)**2,axis=1)))/3.0
 *             #ts[0]=t[-1];ts[1]=t[1];ts[-1]=t[0]
 */
      __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1725; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1725; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_16 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1725; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      if (!(likely(((__pyx_t_16) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_16, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1725; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_17 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1725; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      if (!(likely(((__pyx_t_17) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_17, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1725; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3dist(__pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_5)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_2)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_3)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_16)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_17)), ((float *)__pyx_v_d));
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;

      /* "dipy/tracking/distances.pyx":1730
 *             #ds=np.sum(np.sqrt(np.sum((ts-h)**2,axis=1)))/3.0
 *             #print d[0],d[1]
 *             if d[1]<d[0]:             # <<<<<<<<<<<<<<
 *                 d[0]=d[1]
 *                 flip[k]=1
 */
      __pyx_t_18 = (((__pyx_v_d[1]) < (__pyx_v_d[0])) != 0);
      if (__pyx_t_18) {

        /* "dipy/tracking/distances.pyx":1731
 *             #print d[0],d[1]
 *             if d[1]<d[0]:
 *                 d[0]=d[1]             # <<<<<<<<<<<<<<
 *                 flip[k]=1
 *             alld[k]=d[0]
 */
        (__pyx_v_d[0]) = (__pyx_v_d[1]);

        /* "dipy/tracking/distances.pyx":1732
 *             if d[1]<d[0]:
 *                 d[0]=d[1]
 *                 flip[k]=1             # <<<<<<<<<<<<<<
 *             alld[k]=d[0]
 *         m_k=np.min(alld)
 */
        if (__Pyx_SetItemInt(__pyx_v_flip, __pyx_v_k, __pyx_int_1, sizeof(int), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1732; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L7;
      }
      __pyx_L7:;

      /* "dipy/tracking/distances.pyx":1733
 *                 d[0]=d[1]
 *                 flip[k]=1
 *             alld[k]=d[0]             # <<<<<<<<<<<<<<
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)
 */
      __pyx_t_17 = PyFloat_FromDouble((__pyx_v_d[0])); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      if (__Pyx_SetItemInt(__pyx_v_alld, __pyx_v_k, __pyx_t_17, sizeof(int), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1733; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    }

    /* "dipy/tracking/distances.pyx":1734
 *                 flip[k]=1
 *             alld[k]=d[0]
 *         m_k=np.min(alld)             # <<<<<<<<<<<<<<
 *         i_k=np.argmin(alld)
 *         if m_k<d_thr:
 */
    __pyx_t_17 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_t_17, __pyx_n_s__min); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __pyx_t_17 = PyTuple_New(1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_17, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_3 = PyObject_Call(__pyx_t_16, ((PyObject *)__pyx_t_17), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1734; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_17)); __pyx_t_17 = 0;
    __Pyx_XDECREF(__pyx_v_m_k);
    __pyx_v_m_k = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "dipy/tracking/distances.pyx":1735
 *             alld[k]=d[0]
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)             # <<<<<<<<<<<<<<
 *         if m_k<d_thr:
 *             if flip[i_k]==1:
 */
    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_17 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__argmin); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_16 = PyObject_Call(__pyx_t_17, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    __Pyx_XDECREF(__pyx_v_i_k);
    __pyx_v_i_k = __pyx_t_16;
    __pyx_t_16 = 0;

    /* "dipy/tracking/distances.pyx":1736
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)
 *         if m_k<d_thr:             # <<<<<<<<<<<<<<
 *             if flip[i_k]==1:
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]
 */
    __pyx_t_16 = PyObject_RichCompare(__pyx_v_m_k, __pyx_v_d_thr, Py_LT); __Pyx_XGOTREF(__pyx_t_16); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_18 = __Pyx_PyObject_IsTrue(__pyx_t_16); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1736; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    if (__pyx_t_18) {

      /* "dipy/tracking/distances.pyx":1737
 *         i_k=np.argmin(alld)
 *         if m_k<d_thr:
 *             if flip[i_k]==1:             # <<<<<<<<<<<<<<
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]
 *                 C[i_k]['hidden']+=ts
 */
      __pyx_t_16 = PyObject_GetItem(__pyx_v_flip, __pyx_v_i_k); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_3 = PyObject_RichCompare(__pyx_t_16, __pyx_int_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __pyx_t_18 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_18) {

        /* "dipy/tracking/distances.pyx":1738
 *         if m_k<d_thr:
 *             if flip[i_k]==1:
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]             # <<<<<<<<<<<<<<
 *                 C[i_k]['hidden']+=ts
 *             else:
 */
        __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), -1, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        if (__Pyx_SetItemInt(__pyx_v_ts, 0, __pyx_t_3, sizeof(long), PyInt_FromLong, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
        __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        if (__Pyx_SetItemInt(__pyx_v_ts, 1, __pyx_t_3, sizeof(long), PyInt_FromLong, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
        __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        if (__Pyx_SetItemInt(__pyx_v_ts, -1, __pyx_t_3, sizeof(long), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1738; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

        /* "dipy/tracking/distances.pyx":1739
 *             if flip[i_k]==1:
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]
 *                 C[i_k]['hidden']+=ts             # <<<<<<<<<<<<<<
 *             else:
 *                 #print(track.shape)
 */
        __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        __Pyx_INCREF(((PyObject *)__pyx_n_s__hidden));
        __pyx_t_19 = __pyx_n_s__hidden;
        __pyx_t_16 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_t_19)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __pyx_t_17 = PyNumber_InPlaceAdd(__pyx_t_16, __pyx_v_ts); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        if (PyObject_SetItem(__pyx_t_3, ((PyObject *)__pyx_t_19), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1739; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        __Pyx_DECREF(((PyObject *)__pyx_t_19)); __pyx_t_19 = 0;
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
        goto __pyx_L9;
      }
      /*else*/ {

        /* "dipy/tracking/distances.pyx":1743
 *                 #print(track.shape)
 *                 #print(track.dtype)
 *                 C[i_k]['hidden']+=track             # <<<<<<<<<<<<<<
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)
 */
        __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        __Pyx_INCREF(((PyObject *)__pyx_n_s__hidden));
        __pyx_t_19 = __pyx_n_s__hidden;
        __pyx_t_17 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_t_19)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __pyx_t_16 = PyNumber_InPlaceAdd(__pyx_t_17, ((PyObject *)__pyx_v_track)); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        if (PyObject_SetItem(__pyx_t_3, ((PyObject *)__pyx_t_19), __pyx_t_16) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        __Pyx_DECREF(((PyObject *)__pyx_t_19)); __pyx_t_19 = 0;
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      }
      __pyx_L9:;

      /* "dipy/tracking/distances.pyx":1744
 *                 #print(track.dtype)
 *                 C[i_k]['hidden']+=track
 *             C[i_k]['N']+=1             # <<<<<<<<<<<<<<
 *             C[i_k]['indices'].append(it)
 *         else:
 */
      __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_INCREF(((PyObject *)__pyx_n_s__N));
      __pyx_t_19 = __pyx_n_s__N;
      __pyx_t_16 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_t_19)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_17 = PyNumber_InPlaceAdd(__pyx_t_16, __pyx_int_1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      if (PyObject_SetItem(__pyx_t_3, ((PyObject *)__pyx_t_19), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1744; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_19)); __pyx_t_19 = 0;
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

      /* "dipy/tracking/distances.pyx":1745
 *                 C[i_k]['hidden']+=track
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)             # <<<<<<<<<<<<<<
 *         else:
 *             C[lenC]={}
 */
      __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_17 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_3 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_16 = __Pyx_PyObject_Append(__pyx_t_17, __pyx_t_3); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1745; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      goto __pyx_L8;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":1747
 *             C[i_k]['indices'].append(it)
 *         else:
 *             C[lenC]={}             # <<<<<<<<<<<<<<
 *             C[lenC]['hidden']=track.copy()
 *             C[lenC]['N']=1
 */
      __pyx_t_16 = PyDict_New(); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_16));
      if (PyDict_SetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC, ((PyObject *)__pyx_t_16)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1747; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(((PyObject *)__pyx_t_16)); __pyx_t_16 = 0;

      /* "dipy/tracking/distances.pyx":1748
 *         else:
 *             C[lenC]={}
 *             C[lenC]['hidden']=track.copy()             # <<<<<<<<<<<<<<
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]
 */
      __pyx_t_16 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_track), __pyx_n_s__copy); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_3 = PyObject_Call(__pyx_t_16, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __pyx_t_16 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      if (PyObject_SetItem(__pyx_t_16, ((PyObject *)__pyx_n_s__hidden), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1748; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

      /* "dipy/tracking/distances.pyx":1749
 *             C[lenC]={}
 *             C[lenC]['hidden']=track.copy()
 *             C[lenC]['N']=1             # <<<<<<<<<<<<<<
 *             C[lenC]['indices']=[it]
 *     return C
 */
      __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1749; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      if (PyObject_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1749; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

      /* "dipy/tracking/distances.pyx":1750
 *             C[lenC]['hidden']=track.copy()
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]             # <<<<<<<<<<<<<<
 *     return C
 * 
 */
      __pyx_t_3 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_16 = PyList_New(1); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      PyList_SET_ITEM(__pyx_t_16, 0, __pyx_t_3);
      __Pyx_GIVEREF(__pyx_t_3);
      __pyx_t_3 = 0;
      __pyx_t_3 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      if (PyObject_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_16)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1750; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_16)); __pyx_t_16 = 0;
    }
    __pyx_L8:;
  }

  /* "dipy/tracking/distances.pyx":1751
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]
 *     return C             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_C));
  __pyx_r = ((PyObject *)__pyx_v_C);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_16);
  __Pyx_XDECREF(__pyx_t_17);
  __Pyx_XDECREF(((PyObject *)__pyx_t_19));
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering_3pts", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_track);
  __Pyx_XDECREF((PyObject *)__pyx_v_h);
  __Pyx_XDECREF(__pyx_v_C);
  __Pyx_XDECREF(__pyx_v_ts);
  __Pyx_XDECREF(__pyx_v_lenC);
  __Pyx_XDECREF(__pyx_v_alld);
  __Pyx_XDECREF(__pyx_v_flip);
  __Pyx_XDECREF(__pyx_v_m_k);
  __Pyx_XDECREF(__pyx_v_i_k);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1754
 * 
 * 
 * cdef inline void track_direct_flip_3sq_dist(float *a1, float *b1,float  *c1,float *a2, float *b2, float *c2, float *out):             # <<<<<<<<<<<<<<
 *     ''' Calculate the average squared euclidean distance between two 3pt tracks
 *     both direct and flip are given as output
 */

static CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3sq_dist(float *__pyx_v_a1, float *__pyx_v_b1, float *__pyx_v_c1, float *__pyx_v_a2, float *__pyx_v_b2, float *__pyx_v_c2, float *__pyx_v_out) {
  int __pyx_v_i;
  float __pyx_v_tmp1;
  float __pyx_v_tmp2;
  float __pyx_v_tmp3;
  float __pyx_v_tmp1f;
  float __pyx_v_tmp3f;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("track_direct_flip_3sq_dist", 0);

  /* "dipy/tracking/distances.pyx":1773
 *     cdef:
 *         int i
 *         float tmp1=0,tmp2=0,tmp3=0,tmp1f=0,tmp3f=0             # <<<<<<<<<<<<<<
 *     #for i in range(3):
 *     for i from 0<=i<3:
 */
  __pyx_v_tmp1 = 0.0;
  __pyx_v_tmp2 = 0.0;
  __pyx_v_tmp3 = 0.0;
  __pyx_v_tmp1f = 0.0;
  __pyx_v_tmp3f = 0.0;

  /* "dipy/tracking/distances.pyx":1775
 *         float tmp1=0,tmp2=0,tmp3=0,tmp1f=0,tmp3f=0
 *     #for i in range(3):
 *     for i from 0<=i<3:             # <<<<<<<<<<<<<<
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 */
  for (__pyx_v_i = 0; __pyx_v_i < 3; __pyx_v_i++) {

    /* "dipy/tracking/distances.pyx":1776
 *     #for i in range(3):
 *     for i from 0<=i<3:
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])             # <<<<<<<<<<<<<<
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 */
    __pyx_v_tmp1 = (__pyx_v_tmp1 + (((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i])) * ((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1777
 *     for i from 0<=i<3:
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])             # <<<<<<<<<<<<<<
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])
 */
    __pyx_v_tmp2 = (__pyx_v_tmp2 + (((__pyx_v_b1[__pyx_v_i]) - (__pyx_v_b2[__pyx_v_i])) * ((__pyx_v_b1[__pyx_v_i]) - (__pyx_v_b2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1778
 *         tmp1=tmp1+(a1[i]-a2[i])*(a1[i]-a2[i])
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])             # <<<<<<<<<<<<<<
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])
 */
    __pyx_v_tmp3 = (__pyx_v_tmp3 + (((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i])) * ((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1779
 *         tmp2=tmp2+(b1[i]-b2[i])*(b1[i]-b2[i])
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])             # <<<<<<<<<<<<<<
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])
 * 
 */
    __pyx_v_tmp1f = (__pyx_v_tmp1f + (((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i])) * ((__pyx_v_a1[__pyx_v_i]) - (__pyx_v_c2[__pyx_v_i]))));

    /* "dipy/tracking/distances.pyx":1780
 *         tmp3=tmp3+(c1[i]-c2[i])*(c1[i]-c2[i])
 *         tmp1f=tmp1f+(a1[i]-c2[i])*(a1[i]-c2[i])
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])             # <<<<<<<<<<<<<<
 * 
 *     out[0]=(tmp1+tmp2+tmp3)/3.0
 */
    __pyx_v_tmp3f = (__pyx_v_tmp3f + (((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i])) * ((__pyx_v_c1[__pyx_v_i]) - (__pyx_v_a2[__pyx_v_i]))));
  }

  /* "dipy/tracking/distances.pyx":1782
 *         tmp3f=tmp3f+(c1[i]-a2[i])*(c1[i]-a2[i])
 * 
 *     out[0]=(tmp1+tmp2+tmp3)/3.0             # <<<<<<<<<<<<<<
 *     out[1]=(tmp1f+tmp2+tmp3f)/3.0
 * 
 */
  (__pyx_v_out[0]) = (((__pyx_v_tmp1 + __pyx_v_tmp2) + __pyx_v_tmp3) / 3.0);

  /* "dipy/tracking/distances.pyx":1783
 * 
 *     out[0]=(tmp1+tmp2+tmp3)/3.0
 *     out[1]=(tmp1f+tmp2+tmp3f)/3.0             # <<<<<<<<<<<<<<
 * 
 * 
 */
  (__pyx_v_out[1]) = (((__pyx_v_tmp1f + __pyx_v_tmp2) + __pyx_v_tmp3f) / 3.0);

  __Pyx_RefNannyFinishContext();
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_45larch_3split(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_44larch_3split[] = "Generate a first pass clustering using 3 points on the tracks only.\n\n    Parameters\n    ----------\n    tracks : sequence\n        of tracks as arrays, shape ``(N1,3) .. (Nm,3)``, where 3 points are\n        (first, mid and last)\n    indices : None or sequence, optional\n        Sequence of integer indices of tracks\n    trh : float, optional\n        squared euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n        A tree graph containing the clusters.\n\n    Notes\n    -----\n    If a 3 point track (3track) is far away from all clusters then add a new\n    cluster and assign this 3track as the rep(representative) track for the new\n    cluster. Otherwise the rep 3track of each cluster is the average track of\n    the cluster.\n\n    Examples\n    --------\n    >>> tracks=[np.array([[0,0,0],[1,0,0,],[2,0,0]],dtype=np.float32),\n    ...         np.array([[3,0,0],[3.5,1,0],[4,2,0]],dtype=np.float32),\n    ...         np.array([[3.2,0,0],[3.7,1,0],[4.4,2,0]],dtype=np.float32),\n    ...         np.array([[3.4,0,0],[3.9,1,0],[4.6,2,0]],dtype=np.float32),\n    ...         np.array([[0,0.2,0],[1,0.2,0],[2,0.2,0]],dtype=np.float32),\n    ...         np.array([[2,0.2,0],[1,0.2,0],[0,0.2,0]],dtype=np.float32),\n    ...         np.array([[0,0,0],[0,1,0],[0,2,0]],dtype=np.float32),\n    ...         np.array([[0.2,0,0],[0.2,1,0],[0.2,2,0]],dtype=np.float32),\n    ...         np.array([[-0.2,0,0],[-0.2,1,0],[-0.2,2,0]],dtype=np.float32)]\n    >>> C = larch_3split(tracks, None, 0.5)\n\n    Here is an example of how to visualize the clustering above::\n\n        from dipy.viz import fvtk\n        r=fvtk.ren()\n        fvtk.add(r,fvtk.line(tracks,fvtk.red))\n        fvtk.show(r)\n        for c in C:\n            color=np.random.rand(3)\n            for i in C[c]['indices']:\n                fos.add(r,fvtk.line(tracks[i],color))\n        fvtk.show(r)\n        for c in C:\n            fvtk.add(r,fos.line(C[c]['rep3']/C[c]['N'],fos.white))\n        fvtk.show(""r)\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_45larch_3split = {__Pyx_NAMESTR("larch_3split"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_45larch_3split, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_44larch_3split)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_45larch_3split(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_indices = 0;
  PyObject *__pyx_v_thr = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("larch_3split (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__indices,&__pyx_n_s__thr,0};
    PyObject* values[3] = {0,0,0};

    /* "dipy/tracking/distances.pyx":1786
 * 
 * 
 * def larch_3split(tracks, indices=None, thr=10.):             # <<<<<<<<<<<<<<
 *     '''Generate a first pass clustering using 3 points on the tracks only.
 * 
 */
    values[1] = ((PyObject *)Py_None);
    values[2] = __pyx_k_18;
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__indices);
          if (value) { values[1] = value; kw_args--; }
        }
        case  2:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__thr);
          if (value) { values[2] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "larch_3split") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_tracks = values[0];
    __pyx_v_indices = values[1];
    __pyx_v_thr = values[2];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("larch_3split", 0, 1, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3split", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_44larch_3split(__pyx_self, __pyx_v_tracks, __pyx_v_indices, __pyx_v_thr);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_4dipy_8tracking_9distances_44larch_3split(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_tracks, PyObject *__pyx_v_indices, PyObject *__pyx_v_thr) {
  PyArrayObject *__pyx_v_track = 0;
  PyArrayObject *__pyx_v_h = 0;
  int __pyx_v_lent;
  int __pyx_v_k;
  int __pyx_v_it;
  float __pyx_v_d[2];
  PyObject *__pyx_v_C = NULL;
  PyObject *__pyx_v_itrange = NULL;
  PyObject *__pyx_v_ts = NULL;
  PyObject *__pyx_v_lenC = NULL;
  PyObject *__pyx_v_alld = NULL;
  PyObject *__pyx_v_flip = NULL;
  PyObject *__pyx_v_m_k = NULL;
  PyObject *__pyx_v_i_k = NULL;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_h;
  __Pyx_Buffer __pyx_pybuffer_h;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  Py_ssize_t __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  int __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyObject *__pyx_t_6 = NULL;
  PyObject *(*__pyx_t_7)(PyObject *);
  int __pyx_t_8;
  PyObject *__pyx_t_9 = NULL;
  PyArrayObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  PyObject *__pyx_t_13 = NULL;
  Py_ssize_t __pyx_t_14;
  long __pyx_t_15;
  PyArrayObject *__pyx_t_16 = NULL;
  int __pyx_t_17;
  PyObject *__pyx_t_18 = NULL;
  PyObject *__pyx_t_19 = NULL;
  PyObject *__pyx_t_20 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("larch_3split", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;
  __pyx_pybuffer_h.pybuffer.buf = NULL;
  __pyx_pybuffer_h.refcount = 0;
  __pyx_pybuffernd_h.data = NULL;
  __pyx_pybuffernd_h.rcbuffer = &__pyx_pybuffer_h;

  /* "dipy/tracking/distances.pyx":1846
 *         float d[2]
 * 
 *     lent=len(tracks)             # <<<<<<<<<<<<<<
 *     if indices==None:
 *         C={0:{'indices':[0],'rep3':tracks[0].copy(),'N':1}}
 */
  __pyx_t_1 = PyObject_Length(__pyx_v_tracks); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1846; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1847
 * 
 *     lent=len(tracks)
 *     if indices==None:             # <<<<<<<<<<<<<<
 *         C={0:{'indices':[0],'rep3':tracks[0].copy(),'N':1}}
 *         itrange=range(1,lent)
 */
  __pyx_t_2 = PyObject_RichCompare(__pyx_v_indices, Py_None, Py_EQ); __Pyx_XGOTREF(__pyx_t_2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  if (__pyx_t_3) {

    /* "dipy/tracking/distances.pyx":1848
 *     lent=len(tracks)
 *     if indices==None:
 *         C={0:{'indices':[0],'rep3':tracks[0].copy(),'N':1}}             # <<<<<<<<<<<<<<
 *         itrange=range(1,lent)
 *     else:
 */
    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_2));
    __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    __pyx_t_5 = PyList_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_INCREF(__pyx_int_0);
    PyList_SET_ITEM(__pyx_t_5, 0, __pyx_int_0);
    __Pyx_GIVEREF(__pyx_int_0);
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_5)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_tracks, 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__copy); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__rep3), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    if (PyDict_SetItem(__pyx_t_2, __pyx_int_0, ((PyObject *)__pyx_t_4)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __pyx_v_C = ((PyObject*)__pyx_t_2);
    __pyx_t_2 = 0;

    /* "dipy/tracking/distances.pyx":1849
 *     if indices==None:
 *         C={0:{'indices':[0],'rep3':tracks[0].copy(),'N':1}}
 *         itrange=range(1,lent)             # <<<<<<<<<<<<<<
 *     else:
 *         C={0:{'indices':[indices[0]],'rep3':tracks[indices[0]].copy(),'N':1}}
 */
    __pyx_t_2 = PyInt_FromLong(__pyx_v_lent); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_INCREF(__pyx_int_1);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_int_1);
    __Pyx_GIVEREF(__pyx_int_1);
    PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_2);
    __Pyx_GIVEREF(__pyx_t_2);
    __pyx_t_2 = 0;
    __pyx_t_2 = PyObject_Call(__pyx_builtin_range, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __pyx_v_itrange = __pyx_t_2;
    __pyx_t_2 = 0;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":1851
 *         itrange=range(1,lent)
 *     else:
 *         C={0:{'indices':[indices[0]],'rep3':tracks[indices[0]].copy(),'N':1}}             # <<<<<<<<<<<<<<
 *         itrange=indices[1:]
 * 
 */
    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_2));
    __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    __pyx_t_5 = __Pyx_GetItemInt(__pyx_v_indices, 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_6 = PyList_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    PyList_SET_ITEM(__pyx_t_6, 0, __pyx_t_5);
    __Pyx_GIVEREF(__pyx_t_5);
    __pyx_t_5 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_6)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;
    __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_indices, 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_5 = PyObject_GetItem(__pyx_v_tracks, __pyx_t_6); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__copy); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = PyObject_Call(__pyx_t_6, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__rep3), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    if (PyDict_SetItem(__pyx_t_2, __pyx_int_0, ((PyObject *)__pyx_t_4)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1851; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __pyx_v_C = ((PyObject*)__pyx_t_2);
    __pyx_t_2 = 0;

    /* "dipy/tracking/distances.pyx":1852
 *     else:
 *         C={0:{'indices':[indices[0]],'rep3':tracks[indices[0]].copy(),'N':1}}
 *         itrange=indices[1:]             # <<<<<<<<<<<<<<
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)
 */
    __pyx_t_2 = __Pyx_PyObject_GetSlice(__pyx_v_indices, 1, 0, NULL, NULL, &__pyx_k_slice_19, 1, 0, 1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1852; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_v_itrange = __pyx_t_2;
    __pyx_t_2 = 0;
  }
  __pyx_L3:;

  /* "dipy/tracking/distances.pyx":1854
 *         itrange=indices[1:]
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 *     for it in itrange:
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 */
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__float32); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __pyx_t_6 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_21), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __pyx_v_ts = __pyx_t_6;
  __pyx_t_6 = 0;

  /* "dipy/tracking/distances.pyx":1855
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)
 *     for it in itrange:             # <<<<<<<<<<<<<<
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         lenC=len(C.keys())
 */
  if (PyList_CheckExact(__pyx_v_itrange) || PyTuple_CheckExact(__pyx_v_itrange)) {
    __pyx_t_6 = __pyx_v_itrange; __Pyx_INCREF(__pyx_t_6); __pyx_t_1 = 0;
    __pyx_t_7 = NULL;
  } else {
    __pyx_t_1 = -1; __pyx_t_6 = PyObject_GetIter(__pyx_v_itrange); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_7 = Py_TYPE(__pyx_t_6)->tp_iternext;
  }
  for (;;) {
    if (!__pyx_t_7 && PyList_CheckExact(__pyx_t_6)) {
      if (__pyx_t_1 >= PyList_GET_SIZE(__pyx_t_6)) break;
      #if CYTHON_COMPILING_IN_CPYTHON
      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_6, __pyx_t_1); __Pyx_INCREF(__pyx_t_2); __pyx_t_1++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      #else
      __pyx_t_2 = PySequence_ITEM(__pyx_t_6, __pyx_t_1); __pyx_t_1++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      #endif
    } else if (!__pyx_t_7 && PyTuple_CheckExact(__pyx_t_6)) {
      if (__pyx_t_1 >= PyTuple_GET_SIZE(__pyx_t_6)) break;
      #if CYTHON_COMPILING_IN_CPYTHON
      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_6, __pyx_t_1); __Pyx_INCREF(__pyx_t_2); __pyx_t_1++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      #else
      __pyx_t_2 = PySequence_ITEM(__pyx_t_6, __pyx_t_1); __pyx_t_1++; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      #endif
    } else {
      __pyx_t_2 = __pyx_t_7(__pyx_t_6);
      if (unlikely(!__pyx_t_2)) {
        if (PyErr_Occurred()) {
          if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) PyErr_Clear();
          else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        }
        break;
      }
      __Pyx_GOTREF(__pyx_t_2);
    }
    __pyx_t_8 = __Pyx_PyInt_AsInt(__pyx_t_2); if (unlikely((__pyx_t_8 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __pyx_v_it = __pyx_t_8;

    /* "dipy/tracking/distances.pyx":1856
 *     ts=np.zeros((3,3),dtype=np.float32)
 *     for it in itrange:
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         lenC=len(C.keys())
 *         alld=np.zeros(lenC)
 */
    __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_tracks, __pyx_v_it, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_2);
    __Pyx_GIVEREF(__pyx_t_2);
    __pyx_t_2 = 0;
    __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_2));
    if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_9 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_5), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_9);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
    if (!(likely(((__pyx_t_9) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_9, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_10 = ((PyArrayObject *)__pyx_t_9);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_t_10, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_8 < 0)) {
        PyErr_Fetch(&__pyx_t_11, &__pyx_t_12, &__pyx_t_13);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_12); Py_XDECREF(__pyx_t_13);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_11, __pyx_t_12, __pyx_t_13);
        }
      }
      __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_10 = 0;
    __Pyx_XDECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_9);
    __pyx_t_9 = 0;

    /* "dipy/tracking/distances.pyx":1857
 *     for it in itrange:
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         lenC=len(C.keys())             # <<<<<<<<<<<<<<
 *         alld=np.zeros(lenC)
 *         flip=np.zeros(lenC)
 */
    __pyx_t_9 = __Pyx_PyDict_Keys(__pyx_v_C); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_9);
    __pyx_t_14 = PyObject_Length(__pyx_t_9); if (unlikely(__pyx_t_14 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
    __pyx_t_9 = PyInt_FromSsize_t(__pyx_t_14); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_9);
    __Pyx_XDECREF(__pyx_v_lenC);
    __pyx_v_lenC = __pyx_t_9;
    __pyx_t_9 = 0;

    /* "dipy/tracking/distances.pyx":1858
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         lenC=len(C.keys())
 *         alld=np.zeros(lenC)             # <<<<<<<<<<<<<<
 *         flip=np.zeros(lenC)
 * 
 */
    __pyx_t_9 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_9);
    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_9, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
    __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_9);
    __Pyx_INCREF(__pyx_v_lenC);
    PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_v_lenC);
    __Pyx_GIVEREF(__pyx_v_lenC);
    __pyx_t_5 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_9), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_9)); __pyx_t_9 = 0;
    __Pyx_XDECREF(__pyx_v_alld);
    __pyx_v_alld = __pyx_t_5;
    __pyx_t_5 = 0;

    /* "dipy/tracking/distances.pyx":1859
 *         lenC=len(C.keys())
 *         alld=np.zeros(lenC)
 *         flip=np.zeros(lenC)             # <<<<<<<<<<<<<<
 * 
 *         for k in range(lenC):
 */
    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__zeros); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_9);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_INCREF(__pyx_v_lenC);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_lenC);
    __Pyx_GIVEREF(__pyx_v_lenC);
    __pyx_t_2 = PyObject_Call(__pyx_t_9, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_XDECREF(__pyx_v_flip);
    __pyx_v_flip = __pyx_t_2;
    __pyx_t_2 = 0;

    /* "dipy/tracking/distances.pyx":1861
 *         flip=np.zeros(lenC)
 * 
 *         for k in range(lenC):             # <<<<<<<<<<<<<<
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)
 *             track_direct_flip_3dist(asfp(track[0]),asfp(track[1]),asfp(track[2]),
 */
    __pyx_t_15 = __Pyx_PyInt_AsLong(__pyx_v_lenC); if (unlikely((__pyx_t_15 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1861; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_15; __pyx_t_8+=1) {
      __pyx_v_k = __pyx_t_8;

      /* "dipy/tracking/distances.pyx":1862
 * 
 *         for k in range(lenC):
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *             track_direct_flip_3dist(asfp(track[0]),asfp(track[1]),asfp(track[2]),
 *                                     asfp(h[0]), asfp(h[1]), asfp(h[2]),d)
 */
      __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_9 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_9);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_4 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = __Pyx_PyNumber_Divide(__pyx_t_9, __pyx_t_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2);
      __Pyx_GIVEREF(__pyx_t_2);
      __pyx_t_2 = 0;
      __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_2));
      if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_9 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_t_4), ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_9);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
      if (!(likely(((__pyx_t_9) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_9, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_16 = ((PyArrayObject *)__pyx_t_9);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
        __pyx_t_17 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_t_16, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_17 < 0)) {
          PyErr_Fetch(&__pyx_t_13, &__pyx_t_12, &__pyx_t_11);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_v_h, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_13); Py_XDECREF(__pyx_t_12); Py_XDECREF(__pyx_t_11);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_13, __pyx_t_12, __pyx_t_11);
          }
        }
        __pyx_pybuffernd_h.diminfo[0].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_h.diminfo[0].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_h.diminfo[1].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_h.diminfo[1].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_17 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_16 = 0;
      __Pyx_XDECREF(((PyObject *)__pyx_v_h));
      __pyx_v_h = ((PyArrayObject *)__pyx_t_9);
      __pyx_t_9 = 0;

      /* "dipy/tracking/distances.pyx":1863
 *         for k in range(lenC):
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)
 *             track_direct_flip_3dist(asfp(track[0]),asfp(track[1]),asfp(track[2]),             # <<<<<<<<<<<<<<
 *                                     asfp(h[0]), asfp(h[1]), asfp(h[2]),d)
 *             if d[1]<d[0]:
 */
      __pyx_t_9 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_9);
      if (!(likely(((__pyx_t_9) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_9, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

      /* "dipy/tracking/distances.pyx":1864
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)
 *             track_direct_flip_3dist(asfp(track[0]),asfp(track[1]),asfp(track[2]),
 *                                     asfp(h[0]), asfp(h[1]), asfp(h[2]),d)             # <<<<<<<<<<<<<<
 *             if d[1]<d[0]:
 *                 d[0]=d[1];flip[k]=1
 */
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_18 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      if (!(likely(((__pyx_t_18) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_18, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_19 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_19) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_19);
      if (!(likely(((__pyx_t_19) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_19, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3dist(__pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_9)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_2)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_5)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_18)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_19)), __pyx_v_d);
      __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;

      /* "dipy/tracking/distances.pyx":1865
 *             track_direct_flip_3dist(asfp(track[0]),asfp(track[1]),asfp(track[2]),
 *                                     asfp(h[0]), asfp(h[1]), asfp(h[2]),d)
 *             if d[1]<d[0]:             # <<<<<<<<<<<<<<
 *                 d[0]=d[1];flip[k]=1
 *             alld[k]=d[0]
 */
      __pyx_t_3 = (((__pyx_v_d[1]) < (__pyx_v_d[0])) != 0);
      if (__pyx_t_3) {

        /* "dipy/tracking/distances.pyx":1866
 *                                     asfp(h[0]), asfp(h[1]), asfp(h[2]),d)
 *             if d[1]<d[0]:
 *                 d[0]=d[1];flip[k]=1             # <<<<<<<<<<<<<<
 *             alld[k]=d[0]
 *         m_k=np.min(alld)
 */
        (__pyx_v_d[0]) = (__pyx_v_d[1]);
        if (__Pyx_SetItemInt(__pyx_v_flip, __pyx_v_k, __pyx_int_1, sizeof(int), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1866; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L8;
      }
      __pyx_L8:;

      /* "dipy/tracking/distances.pyx":1867
 *             if d[1]<d[0]:
 *                 d[0]=d[1];flip[k]=1
 *             alld[k]=d[0]             # <<<<<<<<<<<<<<
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)
 */
      __pyx_t_19 = PyFloat_FromDouble((__pyx_v_d[0])); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_19);
      if (__Pyx_SetItemInt(__pyx_v_alld, __pyx_v_k, __pyx_t_19, sizeof(int), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1867; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
    }

    /* "dipy/tracking/distances.pyx":1868
 *                 d[0]=d[1];flip[k]=1
 *             alld[k]=d[0]
 *         m_k=np.min(alld)             # <<<<<<<<<<<<<<
 *         i_k=np.argmin(alld)
 *         if m_k<thr:
 */
    __pyx_t_19 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_19);
    __pyx_t_18 = __Pyx_PyObject_GetAttrStr(__pyx_t_19, __pyx_n_s__min); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
    __pyx_t_19 = PyTuple_New(1); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_19);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_19, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_5 = PyObject_Call(__pyx_t_18, ((PyObject *)__pyx_t_19), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1868; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_19)); __pyx_t_19 = 0;
    __Pyx_XDECREF(__pyx_v_m_k);
    __pyx_v_m_k = __pyx_t_5;
    __pyx_t_5 = 0;

    /* "dipy/tracking/distances.pyx":1869
 *             alld[k]=d[0]
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)             # <<<<<<<<<<<<<<
 *         if m_k<thr:
 *             if flip[i_k]==1:
 */
    __pyx_t_5 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_19 = __Pyx_PyObject_GetAttrStr(__pyx_t_5, __pyx_n_s__argmin); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_19);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_18 = PyObject_Call(__pyx_t_19, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1869; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_18);
    __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    __Pyx_XDECREF(__pyx_v_i_k);
    __pyx_v_i_k = __pyx_t_18;
    __pyx_t_18 = 0;

    /* "dipy/tracking/distances.pyx":1870
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)
 *         if m_k<thr:             # <<<<<<<<<<<<<<
 *             if flip[i_k]==1:
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]
 */
    __pyx_t_18 = PyObject_RichCompare(__pyx_v_m_k, __pyx_v_thr, Py_LT); __Pyx_XGOTREF(__pyx_t_18); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_18); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1870; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
    if (__pyx_t_3) {

      /* "dipy/tracking/distances.pyx":1871
 *         i_k=np.argmin(alld)
 *         if m_k<thr:
 *             if flip[i_k]==1:             # <<<<<<<<<<<<<<
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]
 *                 C[i_k]['rep3']+=ts
 */
      __pyx_t_18 = PyObject_GetItem(__pyx_v_flip, __pyx_v_i_k); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      __pyx_t_5 = PyObject_RichCompare(__pyx_t_18, __pyx_int_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_3) {

        /* "dipy/tracking/distances.pyx":1872
 *         if m_k<thr:
 *             if flip[i_k]==1:
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]             # <<<<<<<<<<<<<<
 *                 C[i_k]['rep3']+=ts
 *             else:
 */
        __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), -1, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        if (__Pyx_SetItemInt(__pyx_v_ts, 0, __pyx_t_5, sizeof(long), PyInt_FromLong, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        if (__Pyx_SetItemInt(__pyx_v_ts, 1, __pyx_t_5, sizeof(long), PyInt_FromLong, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        if (__Pyx_SetItemInt(__pyx_v_ts, -1, __pyx_t_5, sizeof(long), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1872; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

        /* "dipy/tracking/distances.pyx":1873
 *             if flip[i_k]==1:
 *                 ts[0]=track[-1];ts[1]=track[1];ts[-1]=track[0]
 *                 C[i_k]['rep3']+=ts             # <<<<<<<<<<<<<<
 *             else:
 *                 C[i_k]['rep3']+=track
 */
        __pyx_t_5 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1873; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __Pyx_INCREF(((PyObject *)__pyx_n_s__rep3));
        __pyx_t_20 = __pyx_n_s__rep3;
        __pyx_t_18 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_t_20)); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1873; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_18);
        __pyx_t_19 = PyNumber_InPlaceAdd(__pyx_t_18, __pyx_v_ts); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1873; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_19);
        __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
        if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_t_20), __pyx_t_19) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1873; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
        __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        goto __pyx_L10;
      }
      /*else*/ {

        /* "dipy/tracking/distances.pyx":1875
 *                 C[i_k]['rep3']+=ts
 *             else:
 *                 C[i_k]['rep3']+=track             # <<<<<<<<<<<<<<
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)
 */
        __pyx_t_5 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __Pyx_INCREF(((PyObject *)__pyx_n_s__rep3));
        __pyx_t_20 = __pyx_n_s__rep3;
        __pyx_t_19 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_t_20)); if (!__pyx_t_19) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_19);
        __pyx_t_18 = PyNumber_InPlaceAdd(__pyx_t_19, ((PyObject *)__pyx_v_track)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_18);
        __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
        if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_t_20), __pyx_t_18) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1875; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
        __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      }
      __pyx_L10:;

      /* "dipy/tracking/distances.pyx":1876
 *             else:
 *                 C[i_k]['rep3']+=track
 *             C[i_k]['N']+=1             # <<<<<<<<<<<<<<
 *             C[i_k]['indices'].append(it)
 * 
 */
      __pyx_t_5 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_INCREF(((PyObject *)__pyx_n_s__N));
      __pyx_t_20 = __pyx_n_s__N;
      __pyx_t_18 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_t_20)); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      __pyx_t_19 = PyNumber_InPlaceAdd(__pyx_t_18, __pyx_int_1); if (unlikely(!__pyx_t_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_19);
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_t_20), __pyx_t_19) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1876; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

      /* "dipy/tracking/distances.pyx":1877
 *                 C[i_k]['rep3']+=track
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)             # <<<<<<<<<<<<<<
 * 
 *         else:
 */
      __pyx_t_5 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1877; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_19 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_19) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1877; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_19);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1877; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_18 = __Pyx_PyObject_Append(__pyx_t_19, __pyx_t_5); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1877; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      __Pyx_DECREF(__pyx_t_19); __pyx_t_19 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      goto __pyx_L9;
    }
    /*else*/ {

      /* "dipy/tracking/distances.pyx":1880
 * 
 *         else:
 *             C[lenC]={}             # <<<<<<<<<<<<<<
 *             C[lenC]['rep3']=track.copy()
 *             C[lenC]['N']=1
 */
      __pyx_t_18 = PyDict_New(); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_18));
      if (PyDict_SetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC, ((PyObject *)__pyx_t_18)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1880; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(((PyObject *)__pyx_t_18)); __pyx_t_18 = 0;

      /* "dipy/tracking/distances.pyx":1881
 *         else:
 *             C[lenC]={}
 *             C[lenC]['rep3']=track.copy()             # <<<<<<<<<<<<<<
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]
 */
      __pyx_t_18 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_track), __pyx_n_s__copy); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      __pyx_t_5 = PyObject_Call(__pyx_t_18, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      __pyx_t_18 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC); if (!__pyx_t_18) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      if (PyObject_SetItem(__pyx_t_18, ((PyObject *)__pyx_n_s__rep3), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1881; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

      /* "dipy/tracking/distances.pyx":1882
 *             C[lenC]={}
 *             C[lenC]['rep3']=track.copy()
 *             C[lenC]['N']=1             # <<<<<<<<<<<<<<
 *             C[lenC]['indices']=[it]
 * 
 */
      __pyx_t_5 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1882; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1882; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

      /* "dipy/tracking/distances.pyx":1883
 *             C[lenC]['rep3']=track.copy()
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]             # <<<<<<<<<<<<<<
 * 
 * 
 */
      __pyx_t_5 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_18 = PyList_New(1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_18);
      PyList_SET_ITEM(__pyx_t_18, 0, __pyx_t_5);
      __Pyx_GIVEREF(__pyx_t_5);
      __pyx_t_5 = 0;
      __pyx_t_5 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_lenC); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_18)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1883; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_18)); __pyx_t_18 = 0;
    }
    __pyx_L9:;
  }
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;

  /* "dipy/tracking/distances.pyx":1886
 * 
 * 
 *     return C             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(((PyObject *)__pyx_v_C));
  __pyx_r = ((PyObject *)__pyx_v_C);
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __Pyx_XDECREF(__pyx_t_9);
  __Pyx_XDECREF(__pyx_t_18);
  __Pyx_XDECREF(__pyx_t_19);
  __Pyx_XDECREF(((PyObject *)__pyx_t_20));
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3split", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_track);
  __Pyx_XDECREF((PyObject *)__pyx_v_h);
  __Pyx_XDECREF(__pyx_v_C);
  __Pyx_XDECREF(__pyx_v_itrange);
  __Pyx_XDECREF(__pyx_v_ts);
  __Pyx_XDECREF(__pyx_v_lenC);
  __Pyx_XDECREF(__pyx_v_alld);
  __Pyx_XDECREF(__pyx_v_flip);
  __Pyx_XDECREF(__pyx_v_m_k);
  __Pyx_XDECREF(__pyx_v_i_k);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_47larch_3merge(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_46larch_3merge[] = "\n    Reassign tracks to existing clusters by merging clusters that their\n    representative tracks are not very distant i.e. less than sqd_thr. Using\n    tracks consisting of 3 points (first, mid and last). This is necessary\n    after running larch_fast_split after multiple split in different levels\n    (squared thresholds) as some of them have created independent clusters.\n\n    Parameters\n    ----------\n    C : graph with clusters\n        of indices 3tracks (tracks consisting of 3 points only)\n    sqd_trh: float\n        squared euclidean distance threshold\n\n    Returns\n    -------\n    C : dict\n       a tree graph containing the clusters\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_47larch_3merge = {__Pyx_NAMESTR("larch_3merge"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_47larch_3merge, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_46larch_3merge)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_47larch_3merge(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_C = 0;
  PyObject *__pyx_v_thr = 0;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("larch_3merge (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__C,&__pyx_n_s__thr,0};
    PyObject* values[2] = {0,0};
    values[1] = __pyx_k_22;
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__C)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (kw_args > 0) {
          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__thr);
          if (value) { values[1] = value; kw_args--; }
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "larch_3merge") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else {
      switch (PyTuple_GET_SIZE(__pyx_args)) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        break;
        default: goto __pyx_L5_argtuple_error;
      }
    }
    __pyx_v_C = values[0];
    __pyx_v_thr = values[1];
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("larch_3merge", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3merge", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_46larch_3merge(__pyx_self, __pyx_v_C, __pyx_v_thr);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1889
 * 
 * 
 * def larch_3merge(C,thr=10.):             # <<<<<<<<<<<<<<
 *     '''
 *     Reassign tracks to existing clusters by merging clusters that their
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_46larch_3merge(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_C, PyObject *__pyx_v_thr) {
  PyArrayObject *__pyx_v_h = 0;
  PyArrayObject *__pyx_v_ch = 0;
  int __pyx_v_lenC;
  int __pyx_v_k;
  int __pyx_v_c;
  float __pyx_v_d[2];
  PyObject *__pyx_v_ts = NULL;
  PyObject *__pyx_v_C2 = NULL;
  PyObject *__pyx_v_krange = NULL;
  PyObject *__pyx_v_klen = NULL;
  PyObject *__pyx_v_alld = NULL;
  PyObject *__pyx_v_flip = NULL;
  PyObject *__pyx_v_m_k = NULL;
  PyObject *__pyx_v_i_k = NULL;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_ch;
  __Pyx_Buffer __pyx_pybuffer_ch;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_h;
  __Pyx_Buffer __pyx_pybuffer_h;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyArrayObject *__pyx_t_5 = NULL;
  PyArrayObject *__pyx_t_6 = NULL;
  Py_ssize_t __pyx_t_7;
  long __pyx_t_8;
  int __pyx_t_9;
  int __pyx_t_10;
  PyObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  PyObject *__pyx_t_13 = NULL;
  int __pyx_t_14;
  int __pyx_t_15;
  PyObject *__pyx_t_16 = NULL;
  PyObject *__pyx_t_17 = NULL;
  int __pyx_t_18;
  long __pyx_t_19;
  PyObject *__pyx_t_20 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("larch_3merge", 0);
  __pyx_pybuffer_h.pybuffer.buf = NULL;
  __pyx_pybuffer_h.refcount = 0;
  __pyx_pybuffernd_h.data = NULL;
  __pyx_pybuffernd_h.rcbuffer = &__pyx_pybuffer_h;
  __pyx_pybuffer_ch.pybuffer.buf = NULL;
  __pyx_pybuffer_ch.refcount = 0;
  __pyx_pybuffernd_ch.data = NULL;
  __pyx_pybuffernd_ch.rcbuffer = &__pyx_pybuffer_ch;

  /* "dipy/tracking/distances.pyx":1910
 *     '''
 * 
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] h=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] ch=np.zeros((3,3),dtype=np.float32)
 *     cdef int lenC,k,c
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_k_tuple_24), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
      __pyx_v_h = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_h.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_h.diminfo[0].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_h.diminfo[0].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_h.diminfo[1].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_h.diminfo[1].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[1];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_h = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "dipy/tracking/distances.pyx":1911
 * 
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] h=np.zeros((3,3),dtype=np.float32)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] ch=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 *     cdef int lenC,k,c
 *     cdef float d[2]
 */
  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_26), ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_ch.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
      __pyx_v_ch = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_ch.rcbuffer->pybuffer.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_pybuffernd_ch.diminfo[0].strides = __pyx_pybuffernd_ch.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_ch.diminfo[0].shape = __pyx_pybuffernd_ch.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_ch.diminfo[1].strides = __pyx_pybuffernd_ch.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_ch.diminfo[1].shape = __pyx_pybuffernd_ch.rcbuffer->pybuffer.shape[1];
    }
  }
  __pyx_t_6 = 0;
  __pyx_v_ch = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":1915
 *     cdef float d[2]
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *     lenC=len(C)
 */
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_28), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  __pyx_v_ts = __pyx_t_2;
  __pyx_t_2 = 0;

  /* "dipy/tracking/distances.pyx":1917
 *     ts=np.zeros((3,3),dtype=np.float32)
 * 
 *     lenC=len(C)             # <<<<<<<<<<<<<<
 *     C2=C.copy()
 * 
 */
  __pyx_t_7 = PyObject_Length(__pyx_v_C); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1917; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lenC = __pyx_t_7;

  /* "dipy/tracking/distances.pyx":1918
 * 
 *     lenC=len(C)
 *     C2=C.copy()             # <<<<<<<<<<<<<<
 * 
 *     for c in range(0,lenC-1):
 */
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_C, __pyx_n_s__copy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1918; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_v_C2 = __pyx_t_3;
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":1920
 *     C2=C.copy()
 * 
 *     for c in range(0,lenC-1):             # <<<<<<<<<<<<<<
 *         ch=np.ascontiguousarray(C[c]['rep3']/C[c]['N'],dtype=f32_dt)
 *         krange=range(c+1,lenC)
 */
  __pyx_t_8 = (__pyx_v_lenC - 1);
  for (__pyx_t_9 = 0; __pyx_t_9 < __pyx_t_8; __pyx_t_9+=1) {
    __pyx_v_c = __pyx_t_9;

    /* "dipy/tracking/distances.pyx":1921
 * 
 *     for c in range(0,lenC-1):
 *         ch=np.ascontiguousarray(C[c]['rep3']/C[c]['N'],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *         krange=range(c+1,lenC)
 *         klen=len(krange)
 */
    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_c, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_4 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_c, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_1 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = __Pyx_PyNumber_Divide(__pyx_t_4, __pyx_t_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    __pyx_t_3 = 0;
    __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_3));
    if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_6 = ((PyArrayObject *)__pyx_t_4);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_ch.rcbuffer->pybuffer);
      __pyx_t_10 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_ch.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_10 < 0)) {
        PyErr_Fetch(&__pyx_t_11, &__pyx_t_12, &__pyx_t_13);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_ch.rcbuffer->pybuffer, (PyObject*)__pyx_v_ch, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
          Py_XDECREF(__pyx_t_11); Py_XDECREF(__pyx_t_12); Py_XDECREF(__pyx_t_13);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_11, __pyx_t_12, __pyx_t_13);
        }
      }
      __pyx_pybuffernd_ch.diminfo[0].strides = __pyx_pybuffernd_ch.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_ch.diminfo[0].shape = __pyx_pybuffernd_ch.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_ch.diminfo[1].strides = __pyx_pybuffernd_ch.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_ch.diminfo[1].shape = __pyx_pybuffernd_ch.rcbuffer->pybuffer.shape[1];
      if (unlikely(__pyx_t_10 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1921; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_6 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_ch));
    __pyx_v_ch = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1922
 *     for c in range(0,lenC-1):
 *         ch=np.ascontiguousarray(C[c]['rep3']/C[c]['N'],dtype=f32_dt)
 *         krange=range(c+1,lenC)             # <<<<<<<<<<<<<<
 *         klen=len(krange)
 *         alld=np.zeros(klen)
 */
    __pyx_t_4 = PyInt_FromLong((__pyx_v_c + 1)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = PyInt_FromLong(__pyx_v_lenC); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    __pyx_t_4 = 0;
    __pyx_t_3 = 0;
    __pyx_t_3 = PyObject_Call(__pyx_builtin_range, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1922; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
    __Pyx_XDECREF(__pyx_v_krange);
    __pyx_v_krange = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "dipy/tracking/distances.pyx":1923
 *         ch=np.ascontiguousarray(C[c]['rep3']/C[c]['N'],dtype=f32_dt)
 *         krange=range(c+1,lenC)
 *         klen=len(krange)             # <<<<<<<<<<<<<<
 *         alld=np.zeros(klen)
 *         flip=np.zeros(klen)
 */
    __pyx_t_7 = PyObject_Length(__pyx_v_krange); if (unlikely(__pyx_t_7 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_3 = PyInt_FromSsize_t(__pyx_t_7); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1923; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_XDECREF(__pyx_v_klen);
    __pyx_v_klen = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "dipy/tracking/distances.pyx":1924
 *         krange=range(c+1,lenC)
 *         klen=len(krange)
 *         alld=np.zeros(klen)             # <<<<<<<<<<<<<<
 *         flip=np.zeros(klen)
 *         for k in range(c+1,lenC):
 */
    __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_INCREF(__pyx_v_klen);
    PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_v_klen);
    __Pyx_GIVEREF(__pyx_v_klen);
    __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1924; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
    __Pyx_XDECREF(__pyx_v_alld);
    __pyx_v_alld = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1925
 *         klen=len(krange)
 *         alld=np.zeros(klen)
 *         flip=np.zeros(klen)             # <<<<<<<<<<<<<<
 *         for k in range(c+1,lenC):
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)
 */
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_INCREF(__pyx_v_klen);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_klen);
    __Pyx_GIVEREF(__pyx_v_klen);
    __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1925; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __Pyx_XDECREF(__pyx_v_flip);
    __pyx_v_flip = __pyx_t_1;
    __pyx_t_1 = 0;

    /* "dipy/tracking/distances.pyx":1926
 *         alld=np.zeros(klen)
 *         flip=np.zeros(klen)
 *         for k in range(c+1,lenC):             # <<<<<<<<<<<<<<
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)
 *             track_direct_flip_3dist(
 */
    __pyx_t_10 = __pyx_v_lenC;
    for (__pyx_t_14 = (__pyx_v_c + 1); __pyx_t_14 < __pyx_t_10; __pyx_t_14+=1) {
      __pyx_v_k = __pyx_t_14;

      /* "dipy/tracking/distances.pyx":1927
 *         flip=np.zeros(klen)
 *         for k in range(c+1,lenC):
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)             # <<<<<<<<<<<<<<
 *             track_direct_flip_3dist(
 *                 asfp(ch[0]),asfp(ch[1]),asfp(ch[2]),
 */
      __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __pyx_t_4 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_k, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __pyx_t_3 = PyObject_GetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_k, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __pyx_t_2 = PyObject_GetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __pyx_t_1 = __Pyx_PyNumber_Divide(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_1);
      __Pyx_GIVEREF(__pyx_t_1);
      __pyx_t_1 = 0;
      __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_1));
      if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__dtype), ((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_3 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_2), ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
      if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_5 = ((PyArrayObject *)__pyx_t_3);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
        __pyx_t_15 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_15 < 0)) {
          PyErr_Fetch(&__pyx_t_13, &__pyx_t_12, &__pyx_t_11);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_h.rcbuffer->pybuffer, (PyObject*)__pyx_v_h, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {
            Py_XDECREF(__pyx_t_13); Py_XDECREF(__pyx_t_12); Py_XDECREF(__pyx_t_11);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_13, __pyx_t_12, __pyx_t_11);
          }
        }
        __pyx_pybuffernd_h.diminfo[0].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_h.diminfo[0].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_h.diminfo[1].strides = __pyx_pybuffernd_h.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_h.diminfo[1].shape = __pyx_pybuffernd_h.rcbuffer->pybuffer.shape[1];
        if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1927; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_5 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_h));
      __pyx_v_h = ((PyArrayObject *)__pyx_t_3);
      __pyx_t_3 = 0;

      /* "dipy/tracking/distances.pyx":1929
 *             h=np.ascontiguousarray(C[k]['rep3']/C[k]['N'],dtype=f32_dt)
 *             track_direct_flip_3dist(
 *                 asfp(ch[0]),asfp(ch[1]),asfp(ch[2]),             # <<<<<<<<<<<<<<
 *                 asfp(h[0]), asfp(h[1]), asfp(h[2]),d)
 * 
 */
      __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      if (!(likely(((__pyx_t_2) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_2, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1929; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

      /* "dipy/tracking/distances.pyx":1930
 *             track_direct_flip_3dist(
 *                 asfp(ch[0]),asfp(ch[1]),asfp(ch[2]),
 *                 asfp(h[0]), asfp(h[1]), asfp(h[2]),d)             # <<<<<<<<<<<<<<
 * 
 *             if d[1]<d[0]:
 */
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      if (!(likely(((__pyx_t_4) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_4, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_16 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      if (!(likely(((__pyx_t_16) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_16, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_17 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 2, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      if (!(likely(((__pyx_t_17) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_17, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3dist(__pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_3)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_1)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_2)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_4)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_16)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_17)), __pyx_v_d);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;

      /* "dipy/tracking/distances.pyx":1932
 *                 asfp(h[0]), asfp(h[1]), asfp(h[2]),d)
 * 
 *             if d[1]<d[0]:             # <<<<<<<<<<<<<<
 *                 d[0]=d[1]
 *                 flip[k-c-1]=1
 */
      __pyx_t_18 = (((__pyx_v_d[1]) < (__pyx_v_d[0])) != 0);
      if (__pyx_t_18) {

        /* "dipy/tracking/distances.pyx":1933
 * 
 *             if d[1]<d[0]:
 *                 d[0]=d[1]             # <<<<<<<<<<<<<<
 *                 flip[k-c-1]=1
 *             alld[k-c-1]=d[0]
 */
        (__pyx_v_d[0]) = (__pyx_v_d[1]);

        /* "dipy/tracking/distances.pyx":1934
 *             if d[1]<d[0]:
 *                 d[0]=d[1]
 *                 flip[k-c-1]=1             # <<<<<<<<<<<<<<
 *             alld[k-c-1]=d[0]
 * 
 */
        __pyx_t_19 = ((__pyx_v_k - __pyx_v_c) - 1);
        if (__Pyx_SetItemInt(__pyx_v_flip, __pyx_t_19, __pyx_int_1, sizeof(long), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1934; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L7;
      }
      __pyx_L7:;

      /* "dipy/tracking/distances.pyx":1935
 *                 d[0]=d[1]
 *                 flip[k-c-1]=1
 *             alld[k-c-1]=d[0]             # <<<<<<<<<<<<<<
 * 
 *         m_k=np.min(alld)
 */
      __pyx_t_17 = PyFloat_FromDouble((__pyx_v_d[0])); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1935; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __pyx_t_19 = ((__pyx_v_k - __pyx_v_c) - 1);
      if (__Pyx_SetItemInt(__pyx_v_alld, __pyx_t_19, __pyx_t_17, sizeof(long), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1935; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    }

    /* "dipy/tracking/distances.pyx":1937
 *             alld[k-c-1]=d[0]
 * 
 *         m_k=np.min(alld)             # <<<<<<<<<<<<<<
 *         i_k=np.argmin(alld)
 *         if m_k<thr:
 */
    __pyx_t_17 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __pyx_t_16 = __Pyx_PyObject_GetAttrStr(__pyx_t_17, __pyx_n_s__min); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __pyx_t_17 = PyTuple_New(1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_17, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_4 = PyObject_Call(__pyx_t_16, ((PyObject *)__pyx_t_17), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1937; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_17)); __pyx_t_17 = 0;
    __Pyx_XDECREF(__pyx_v_m_k);
    __pyx_v_m_k = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "dipy/tracking/distances.pyx":1938
 * 
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)             # <<<<<<<<<<<<<<
 *         if m_k<thr:
 *             if flip[i_k]==1:
 */
    __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_17 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s__argmin); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_16 = PyObject_Call(__pyx_t_17, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1938; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __Pyx_XDECREF(__pyx_v_i_k);
    __pyx_v_i_k = __pyx_t_16;
    __pyx_t_16 = 0;

    /* "dipy/tracking/distances.pyx":1939
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)
 *         if m_k<thr:             # <<<<<<<<<<<<<<
 *             if flip[i_k]==1:
 *                 ts[0]=ch[-1];ts[1]=ch[1];ts[-1]=ch[0]
 */
    __pyx_t_16 = PyObject_RichCompare(__pyx_v_m_k, __pyx_v_thr, Py_LT); __Pyx_XGOTREF(__pyx_t_16); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_18 = __Pyx_PyObject_IsTrue(__pyx_t_16); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1939; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    if (__pyx_t_18) {

      /* "dipy/tracking/distances.pyx":1940
 *         i_k=np.argmin(alld)
 *         if m_k<thr:
 *             if flip[i_k]==1:             # <<<<<<<<<<<<<<
 *                 ts[0]=ch[-1];ts[1]=ch[1];ts[-1]=ch[0]
 *                 C2[i_k+c]['rep3']+=ts
 */
      __pyx_t_16 = PyObject_GetItem(__pyx_v_flip, __pyx_v_i_k); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_4 = PyObject_RichCompare(__pyx_t_16, __pyx_int_1, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __pyx_t_18 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1940; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      if (__pyx_t_18) {

        /* "dipy/tracking/distances.pyx":1941
 *         if m_k<thr:
 *             if flip[i_k]==1:
 *                 ts[0]=ch[-1];ts[1]=ch[1];ts[-1]=ch[0]             # <<<<<<<<<<<<<<
 *                 C2[i_k+c]['rep3']+=ts
 *             else:
 */
        __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), -1, sizeof(long), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        if (__Pyx_SetItemInt(__pyx_v_ts, 0, __pyx_t_4, sizeof(long), PyInt_FromLong, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
        __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 1, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        if (__Pyx_SetItemInt(__pyx_v_ts, 1, __pyx_t_4, sizeof(long), PyInt_FromLong, 0, 0, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
        __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 0, sizeof(long), PyInt_FromLong, 0, 0, 1); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        if (__Pyx_SetItemInt(__pyx_v_ts, -1, __pyx_t_4, sizeof(long), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

        /* "dipy/tracking/distances.pyx":1942
 *             if flip[i_k]==1:
 *                 ts[0]=ch[-1];ts[1]=ch[1];ts[-1]=ch[0]
 *                 C2[i_k+c]['rep3']+=ts             # <<<<<<<<<<<<<<
 *             else:
 *                 C2[i_k+c]['rep3']+=ch
 */
        __pyx_t_4 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        __pyx_t_16 = PyNumber_Add(__pyx_v_i_k, __pyx_t_4); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
        __pyx_t_4 = PyObject_GetItem(__pyx_v_C2, __pyx_t_16); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        __Pyx_INCREF(((PyObject *)__pyx_n_s__rep3));
        __pyx_t_20 = __pyx_n_s__rep3;
        __pyx_t_16 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_t_20)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __pyx_t_17 = PyNumber_InPlaceAdd(__pyx_t_16, __pyx_v_ts); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        if (PyObject_SetItem(__pyx_t_4, ((PyObject *)__pyx_t_20), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1942; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
        goto __pyx_L9;
      }
      /*else*/ {

        /* "dipy/tracking/distances.pyx":1944
 *                 C2[i_k+c]['rep3']+=ts
 *             else:
 *                 C2[i_k+c]['rep3']+=ch             # <<<<<<<<<<<<<<
 *             C2[i_k+c]['N']+=C2[c]['N']
 *             C2[i_k+c]['indices']+=C2[c]['indices']
 */
        __pyx_t_4 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        __pyx_t_17 = PyNumber_Add(__pyx_v_i_k, __pyx_t_4); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
        __pyx_t_4 = PyObject_GetItem(__pyx_v_C2, __pyx_t_17); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_4);
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        __Pyx_INCREF(((PyObject *)__pyx_n_s__rep3));
        __pyx_t_20 = __pyx_n_s__rep3;
        __pyx_t_17 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_t_20)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __pyx_t_16 = PyNumber_InPlaceAdd(__pyx_t_17, ((PyObject *)__pyx_v_ch)); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        if (PyObject_SetItem(__pyx_t_4, ((PyObject *)__pyx_t_20), __pyx_t_16) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1944; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      }
      __pyx_L9:;

      /* "dipy/tracking/distances.pyx":1945
 *             else:
 *                 C2[i_k+c]['rep3']+=ch
 *             C2[i_k+c]['N']+=C2[c]['N']             # <<<<<<<<<<<<<<
 *             C2[i_k+c]['indices']+=C2[c]['indices']
 *             del C2[c]
 */
      __pyx_t_4 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_16 = PyNumber_Add(__pyx_v_i_k, __pyx_t_4); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __pyx_t_4 = PyObject_GetItem(__pyx_v_C2, __pyx_t_16); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_INCREF(((PyObject *)__pyx_n_s__N));
      __pyx_t_20 = __pyx_n_s__N;
      __pyx_t_16 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_t_20)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_17 = __Pyx_GetItemInt(__pyx_v_C2, __pyx_v_c, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __pyx_t_2 = PyObject_GetItem(__pyx_t_17, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __pyx_t_17 = PyNumber_InPlaceAdd(__pyx_t_16, __pyx_t_2); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      if (PyObject_SetItem(__pyx_t_4, ((PyObject *)__pyx_t_20), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1945; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":1946
 *                 C2[i_k+c]['rep3']+=ch
 *             C2[i_k+c]['N']+=C2[c]['N']
 *             C2[i_k+c]['indices']+=C2[c]['indices']             # <<<<<<<<<<<<<<
 *             del C2[c]
 * 
 */
      __pyx_t_4 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_17 = PyNumber_Add(__pyx_v_i_k, __pyx_t_4); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __pyx_t_4 = PyObject_GetItem(__pyx_v_C2, __pyx_t_17); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_INCREF(((PyObject *)__pyx_n_s__indices));
      __pyx_t_20 = __pyx_n_s__indices;
      __pyx_t_17 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_t_20)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __pyx_t_2 = __Pyx_GetItemInt(__pyx_v_C2, __pyx_v_c, sizeof(int), PyInt_FromLong, 0, 1, 1); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_16 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_t_17, __pyx_t_16); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      if (PyObject_SetItem(__pyx_t_4, ((PyObject *)__pyx_t_20), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1946; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_20)); __pyx_t_20 = 0;
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

      /* "dipy/tracking/distances.pyx":1947
 *             C2[i_k+c]['N']+=C2[c]['N']
 *             C2[i_k+c]['indices']+=C2[c]['indices']
 *             del C2[c]             # <<<<<<<<<<<<<<
 * 
 *     return C2
 */
      if (unlikely(!__pyx_v_C2)) { __Pyx_RaiseUnboundLocalError("C2"); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1947; __pyx_clineno = __LINE__; goto __pyx_L1_error;} }
      if (__Pyx_DelItemInt(__pyx_v_C2, __pyx_v_c, sizeof(int), PyInt_FromLong, 0, 1, 1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1947; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L8;
    }
    __pyx_L8:;
  }

  /* "dipy/tracking/distances.pyx":1949
 *             del C2[c]
 * 
 *     return C2             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(__pyx_v_C2);
  __pyx_r = __pyx_v_C2;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_16);
  __Pyx_XDECREF(__pyx_t_17);
  __Pyx_XDECREF(((PyObject *)__pyx_t_20));
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_ch.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3merge", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_ch.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_h.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_h);
  __Pyx_XDECREF((PyObject *)__pyx_v_ch);
  __Pyx_XDECREF(__pyx_v_ts);
  __Pyx_XDECREF(__pyx_v_C2);
  __Pyx_XDECREF(__pyx_v_krange);
  __Pyx_XDECREF(__pyx_v_klen);
  __Pyx_XDECREF(__pyx_v_alld);
  __Pyx_XDECREF(__pyx_v_flip);
  __Pyx_XDECREF(__pyx_v_m_k);
  __Pyx_XDECREF(__pyx_v_i_k);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_49point_track_sq_distance_check(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_48point_track_sq_distance_check[] = " Check if square distance of track from point is smaller than threshold\n\n    Parameters\n    ----------\n    track: array,float32, shape (N,3)\n    point: array,float32, shape (3,)\n    sq_dist_thr: double, threshold\n\n    Returns\n    -------\n    bool: True, if sq_distance <= sq_dist_thr, otherwise False.\n\n    Examples\n    --------\n    >>> t=np.random.rand(10,3).astype(np.float32)\n    >>> p=np.array([0.5,0.5,0.5],dtype=np.float32)\n    >>> point_track_sq_distance_check(t,p,2**2)\n    True\n    >>> t=np.array([[0,0,0],[1,1,1],[2,2,2]],dtype='f4')\n    >>> p=np.array([-1,-1.,-1],dtype='f4')\n    >>> point_track_sq_distance_check(t,p,.2**2)\n    False\n    >>> point_track_sq_distance_check(t,p,2**2)\n    True\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_49point_track_sq_distance_check = {__Pyx_NAMESTR("point_track_sq_distance_check"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_49point_track_sq_distance_check, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_48point_track_sq_distance_check)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_49point_track_sq_distance_check(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyArrayObject *__pyx_v_track = 0;
  PyArrayObject *__pyx_v_point = 0;
  double __pyx_v_sq_dist_thr;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("point_track_sq_distance_check (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__track,&__pyx_n_s__point,&__pyx_n_s__sq_dist_thr,0};
    PyObject* values[3] = {0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__track)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__point)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("point_track_sq_distance_check", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sq_dist_thr)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("point_track_sq_distance_check", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "point_track_sq_distance_check") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
    }
    __pyx_v_track = ((PyArrayObject *)values[0]);
    __pyx_v_point = ((PyArrayObject *)values[1]);
    __pyx_v_sq_dist_thr = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_sq_dist_thr == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1955; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("point_track_sq_distance_check", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.point_track_sq_distance_check", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_track), __pyx_ptype_5numpy_ndarray, 1, "track", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_point), __pyx_ptype_5numpy_ndarray, 1, "point", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1954; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_48point_track_sq_distance_check(__pyx_self, __pyx_v_track, __pyx_v_point, __pyx_v_sq_dist_thr);
  goto __pyx_L0;
  __pyx_L1_error:;
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":1953
 * 
 * 
 * def point_track_sq_distance_check(cnp.ndarray[float,ndim=2] track,             # <<<<<<<<<<<<<<
 *                                   cnp.ndarray[float,ndim=1] point,
 *                                   double sq_dist_thr):
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_48point_track_sq_distance_check(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_track, PyArrayObject *__pyx_v_point, double __pyx_v_sq_dist_thr) {
  float *__pyx_v_t;
  float *__pyx_v_p;
  float __pyx_v_a[3];
  float __pyx_v_b[3];
  int __pyx_v_tlen;
  int __pyx_v_curr;
  float __pyx_v_dist;
  int __pyx_v_i;
  int __pyx_v_intersects;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_point;
  __Pyx_Buffer __pyx_pybuffer_point;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  Py_ssize_t __pyx_t_1;
  long __pyx_t_2;
  int __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("point_track_sq_distance_check", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;
  __pyx_pybuffer_point.pybuffer.buf = NULL;
  __pyx_pybuffer_point.refcount = 0;
  __pyx_pybuffernd_point.data = NULL;
  __pyx_pybuffernd_point.rcbuffer = &__pyx_pybuffer_point;
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_point.rcbuffer->pybuffer, (PyObject*)__pyx_v_point, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_pybuffernd_point.diminfo[0].strides = __pyx_pybuffernd_point.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_point.diminfo[0].shape = __pyx_pybuffernd_point.rcbuffer->pybuffer.shape[0];

  /* "dipy/tracking/distances.pyx":1983
 * 
 *     cdef:
 *         float *t=<float *>track.data             # <<<<<<<<<<<<<<
 *         float *p=<float *>point.data
 *         float a[3],b[3]
 */
  __pyx_v_t = ((float *)__pyx_v_track->data);

  /* "dipy/tracking/distances.pyx":1984
 *     cdef:
 *         float *t=<float *>track.data
 *         float *p=<float *>point.data             # <<<<<<<<<<<<<<
 *         float a[3],b[3]
 *         int tlen = len(track)
 */
  __pyx_v_p = ((float *)__pyx_v_point->data);

  /* "dipy/tracking/distances.pyx":1986
 *         float *p=<float *>point.data
 *         float a[3],b[3]
 *         int tlen = len(track)             # <<<<<<<<<<<<<<
 *         int curr = 0
 *         float dist = 0
 */
  __pyx_t_1 = PyObject_Length(((PyObject *)__pyx_v_track)); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1986; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_tlen = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":1987
 *         float a[3],b[3]
 *         int tlen = len(track)
 *         int curr = 0             # <<<<<<<<<<<<<<
 *         float dist = 0
 *         int i
 */
  __pyx_v_curr = 0;

  /* "dipy/tracking/distances.pyx":1988
 *         int tlen = len(track)
 *         int curr = 0
 *         float dist = 0             # <<<<<<<<<<<<<<
 *         int i
 *         int intersects = 0
 */
  __pyx_v_dist = 0.0;

  /* "dipy/tracking/distances.pyx":1990
 *         float dist = 0
 *         int i
 *         int intersects = 0             # <<<<<<<<<<<<<<
 * 
 *     with nogil:
 */
  __pyx_v_intersects = 0;

  /* "dipy/tracking/distances.pyx":1992
 *         int intersects = 0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
  {
      #ifdef WITH_THREAD
      PyThreadState *_save;
      Py_UNBLOCK_THREADS
      #endif
      /*try:*/ {

        /* "dipy/tracking/distances.pyx":1993
 * 
 *     with nogil:
 *         for i from 0<=i<tlen-1:             # <<<<<<<<<<<<<<
 *             curr=i*3
 *             a[0]=t[curr]
 */
        __pyx_t_2 = (__pyx_v_tlen - 1);
        for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_2; __pyx_v_i++) {

          /* "dipy/tracking/distances.pyx":1994
 *     with nogil:
 *         for i from 0<=i<tlen-1:
 *             curr=i*3             # <<<<<<<<<<<<<<
 *             a[0]=t[curr]
 *             a[1]=t[curr+1]
 */
          __pyx_v_curr = (__pyx_v_i * 3);

          /* "dipy/tracking/distances.pyx":1995
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 *             a[0]=t[curr]             # <<<<<<<<<<<<<<
 *             a[1]=t[curr+1]
 *             a[2]=t[curr+2]
 */
          (__pyx_v_a[0]) = (__pyx_v_t[__pyx_v_curr]);

          /* "dipy/tracking/distances.pyx":1996
 *             curr=i*3
 *             a[0]=t[curr]
 *             a[1]=t[curr+1]             # <<<<<<<<<<<<<<
 *             a[2]=t[curr+2]
 *             b[0]=t[curr+3]
 */
          (__pyx_v_a[1]) = (__pyx_v_t[(__pyx_v_curr + 1)]);

          /* "dipy/tracking/distances.pyx":1997
 *             a[0]=t[curr]
 *             a[1]=t[curr+1]
 *             a[2]=t[curr+2]             # <<<<<<<<<<<<<<
 *             b[0]=t[curr+3]
 *             b[1]=t[curr+4]
 */
          (__pyx_v_a[2]) = (__pyx_v_t[(__pyx_v_curr + 2)]);

          /* "dipy/tracking/distances.pyx":1998
 *             a[1]=t[curr+1]
 *             a[2]=t[curr+2]
 *             b[0]=t[curr+3]             # <<<<<<<<<<<<<<
 *             b[1]=t[curr+4]
 *             b[2]=t[curr+5]
 */
          (__pyx_v_b[0]) = (__pyx_v_t[(__pyx_v_curr + 3)]);

          /* "dipy/tracking/distances.pyx":1999
 *             a[2]=t[curr+2]
 *             b[0]=t[curr+3]
 *             b[1]=t[curr+4]             # <<<<<<<<<<<<<<
 *             b[2]=t[curr+5]
 *             dist=cpoint_segment_sq_dist(<float *>a,<float *>b,p)
 */
          (__pyx_v_b[1]) = (__pyx_v_t[(__pyx_v_curr + 4)]);

          /* "dipy/tracking/distances.pyx":2000
 *             b[0]=t[curr+3]
 *             b[1]=t[curr+4]
 *             b[2]=t[curr+5]             # <<<<<<<<<<<<<<
 *             dist=cpoint_segment_sq_dist(<float *>a,<float *>b,p)
 *             if dist<=sq_dist_thr:
 */
          (__pyx_v_b[2]) = (__pyx_v_t[(__pyx_v_curr + 5)]);

          /* "dipy/tracking/distances.pyx":2001
 *             b[1]=t[curr+4]
 *             b[2]=t[curr+5]
 *             dist=cpoint_segment_sq_dist(<float *>a,<float *>b,p)             # <<<<<<<<<<<<<<
 *             if dist<=sq_dist_thr:
 *                 intersects=1
 */
          __pyx_v_dist = __pyx_f_4dipy_8tracking_9distances_cpoint_segment_sq_dist(((float *)__pyx_v_a), ((float *)__pyx_v_b), __pyx_v_p);

          /* "dipy/tracking/distances.pyx":2002
 *             b[2]=t[curr+5]
 *             dist=cpoint_segment_sq_dist(<float *>a,<float *>b,p)
 *             if dist<=sq_dist_thr:             # <<<<<<<<<<<<<<
 *                 intersects=1
 *                 break
 */
          __pyx_t_3 = ((__pyx_v_dist <= __pyx_v_sq_dist_thr) != 0);
          if (__pyx_t_3) {

            /* "dipy/tracking/distances.pyx":2003
 *             dist=cpoint_segment_sq_dist(<float *>a,<float *>b,p)
 *             if dist<=sq_dist_thr:
 *                 intersects=1             # <<<<<<<<<<<<<<
 *                 break
 * 
 */
            __pyx_v_intersects = 1;

            /* "dipy/tracking/distances.pyx":2004
 *             if dist<=sq_dist_thr:
 *                 intersects=1
 *                 break             # <<<<<<<<<<<<<<
 * 
 *     if intersects==1:
 */
            goto __pyx_L7_break;
            goto __pyx_L8;
          }
          __pyx_L8:;
        }
        __pyx_L7_break:;
      }

      /* "dipy/tracking/distances.pyx":1992
 *         int intersects = 0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
      /*finally:*/ {
        #ifdef WITH_THREAD
        Py_BLOCK_THREADS
        #endif
      }
  }

  /* "dipy/tracking/distances.pyx":2006
 *                 break
 * 
 *     if intersects==1:             # <<<<<<<<<<<<<<
 *         return True
 *     else:
 */
  __pyx_t_3 = ((__pyx_v_intersects == 1) != 0);
  if (__pyx_t_3) {

    /* "dipy/tracking/distances.pyx":2007
 * 
 *     if intersects==1:
 *         return True             # <<<<<<<<<<<<<<
 *     else:
 *         return False
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_4 = __Pyx_PyBool_FromLong(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_r = __pyx_t_4;
    __pyx_t_4 = 0;
    goto __pyx_L0;
    goto __pyx_L9;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":2009
 *         return True
 *     else:
 *         return False             # <<<<<<<<<<<<<<
 * 
 * def track_roi_intersection_check(cnp.ndarray[float,ndim=2] track, cnp.ndarray[float,ndim=2] roi, double sq_dist_thr):
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_4 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_r = __pyx_t_4;
    __pyx_t_4 = 0;
    goto __pyx_L0;
  }
  __pyx_L9:;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_point.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.point_track_sq_distance_check", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_point.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_4dipy_8tracking_9distances_51track_roi_intersection_check(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_50track_roi_intersection_check[] = " Check if a track is intersecting a region of interest\n\n    Parameters\n    ----------\n    track: array,float32, shape (N,3)\n    roi: array,float32, shape (M,3)\n    sq_dist_thr: double, threshold, check squared euclidean distance from every roi point\n\n    Returns\n    -------\n    bool: True, if sq_distance <= sq_dist_thr, otherwise False.\n\n    Examples\n    --------\n    >>> roi=np.array([[0,0,0],[1,0,0],[2,0,0]],dtype='f4')\n    >>> t=np.array([[0,0,0],[1,1,1],[2,2,2]],dtype='f4')\n    >>> track_roi_intersection_check(t,roi,1)\n    True\n    >>> track_roi_intersection_check(t,np.array([[10,0,0]],dtype='f4'),1)\n    False\n    ";
static PyMethodDef __pyx_mdef_4dipy_8tracking_9distances_51track_roi_intersection_check = {__Pyx_NAMESTR("track_roi_intersection_check"), (PyCFunction)__pyx_pw_4dipy_8tracking_9distances_51track_roi_intersection_check, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_50track_roi_intersection_check)};
static PyObject *__pyx_pw_4dipy_8tracking_9distances_51track_roi_intersection_check(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyArrayObject *__pyx_v_track = 0;
  PyArrayObject *__pyx_v_roi = 0;
  double __pyx_v_sq_dist_thr;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("track_roi_intersection_check (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__track,&__pyx_n_s__roi,&__pyx_n_s__sq_dist_thr,0};
    PyObject* values[3] = {0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__track)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__roi)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("track_roi_intersection_check", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sq_dist_thr)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("track_roi_intersection_check", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "track_roi_intersection_check") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
    }
    __pyx_v_track = ((PyArrayObject *)values[0]);
    __pyx_v_roi = ((PyArrayObject *)values[1]);
    __pyx_v_sq_dist_thr = __pyx_PyFloat_AsDouble(values[2]); if (unlikely((__pyx_v_sq_dist_thr == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("track_roi_intersection_check", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.track_roi_intersection_check", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_track), __pyx_ptype_5numpy_ndarray, 1, "track", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_roi), __pyx_ptype_5numpy_ndarray, 1, "roi", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_r = __pyx_pf_4dipy_8tracking_9distances_50track_roi_intersection_check(__pyx_self, __pyx_v_track, __pyx_v_roi, __pyx_v_sq_dist_thr);
  goto __pyx_L0;
  __pyx_L1_error:;
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "dipy/tracking/distances.pyx":2011
 *         return False
 * 
 * def track_roi_intersection_check(cnp.ndarray[float,ndim=2] track, cnp.ndarray[float,ndim=2] roi, double sq_dist_thr):             # <<<<<<<<<<<<<<
 *     ''' Check if a track is intersecting a region of interest
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_50track_roi_intersection_check(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_track, PyArrayObject *__pyx_v_roi, double __pyx_v_sq_dist_thr) {
  float *__pyx_v_t;
  float *__pyx_v_r;
  float __pyx_v_a[3];
  float __pyx_v_b[3];
  float __pyx_v_p[3];
  int __pyx_v_tlen;
  int __pyx_v_rlen;
  int __pyx_v_curr;
  int __pyx_v_currp;
  float __pyx_v_dist;
  int __pyx_v_i;
  int __pyx_v_j;
  int __pyx_v_intersects;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_roi;
  __Pyx_Buffer __pyx_pybuffer_roi;
  __Pyx_LocalBuf_ND __pyx_pybuffernd_track;
  __Pyx_Buffer __pyx_pybuffer_track;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  Py_ssize_t __pyx_t_1;
  long __pyx_t_2;
  int __pyx_t_3;
  int __pyx_t_4;
  PyObject *__pyx_t_5 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("track_roi_intersection_check", 0);
  __pyx_pybuffer_track.pybuffer.buf = NULL;
  __pyx_pybuffer_track.refcount = 0;
  __pyx_pybuffernd_track.data = NULL;
  __pyx_pybuffernd_track.rcbuffer = &__pyx_pybuffer_track;
  __pyx_pybuffer_roi.pybuffer.buf = NULL;
  __pyx_pybuffer_roi.refcount = 0;
  __pyx_pybuffernd_roi.data = NULL;
  __pyx_pybuffernd_roi.rcbuffer = &__pyx_pybuffer_roi;
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_track.rcbuffer->pybuffer, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_pybuffernd_track.diminfo[0].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_track.diminfo[0].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_track.diminfo[1].strides = __pyx_pybuffernd_track.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_track.diminfo[1].shape = __pyx_pybuffernd_track.rcbuffer->pybuffer.shape[1];
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_roi.rcbuffer->pybuffer, (PyObject*)__pyx_v_roi, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_pybuffernd_roi.diminfo[0].strides = __pyx_pybuffernd_roi.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_roi.diminfo[0].shape = __pyx_pybuffernd_roi.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_roi.diminfo[1].strides = __pyx_pybuffernd_roi.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_roi.diminfo[1].shape = __pyx_pybuffernd_roi.rcbuffer->pybuffer.shape[1];

  /* "dipy/tracking/distances.pyx":2035
 * 
 *     cdef:
 *         float *t=<float *>track.data             # <<<<<<<<<<<<<<
 *         float *r=<float *>roi.data
 *         float a[3],b[3],p[3]
 */
  __pyx_v_t = ((float *)__pyx_v_track->data);

  /* "dipy/tracking/distances.pyx":2036
 *     cdef:
 *         float *t=<float *>track.data
 *         float *r=<float *>roi.data             # <<<<<<<<<<<<<<
 *         float a[3],b[3],p[3]
 *         int tlen = len(track)
 */
  __pyx_v_r = ((float *)__pyx_v_roi->data);

  /* "dipy/tracking/distances.pyx":2038
 *         float *r=<float *>roi.data
 *         float a[3],b[3],p[3]
 *         int tlen = len(track)             # <<<<<<<<<<<<<<
 *         int rlen = len(roi)
 *         int curr = 0
 */
  __pyx_t_1 = PyObject_Length(((PyObject *)__pyx_v_track)); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2038; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_tlen = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":2039
 *         float a[3],b[3],p[3]
 *         int tlen = len(track)
 *         int rlen = len(roi)             # <<<<<<<<<<<<<<
 *         int curr = 0
 *         int currp = 0
 */
  __pyx_t_1 = PyObject_Length(((PyObject *)__pyx_v_roi)); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2039; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_rlen = __pyx_t_1;

  /* "dipy/tracking/distances.pyx":2040
 *         int tlen = len(track)
 *         int rlen = len(roi)
 *         int curr = 0             # <<<<<<<<<<<<<<
 *         int currp = 0
 *         float dist = 0
 */
  __pyx_v_curr = 0;

  /* "dipy/tracking/distances.pyx":2041
 *         int rlen = len(roi)
 *         int curr = 0
 *         int currp = 0             # <<<<<<<<<<<<<<
 *         float dist = 0
 *         int i,j
 */
  __pyx_v_currp = 0;

  /* "dipy/tracking/distances.pyx":2042
 *         int curr = 0
 *         int currp = 0
 *         float dist = 0             # <<<<<<<<<<<<<<
 *         int i,j
 *         int intersects=0
 */
  __pyx_v_dist = 0.0;

  /* "dipy/tracking/distances.pyx":2044
 *         float dist = 0
 *         int i,j
 *         int intersects=0             # <<<<<<<<<<<<<<
 * 
 *     with nogil:
 */
  __pyx_v_intersects = 0;

  /* "dipy/tracking/distances.pyx":2046
 *         int intersects=0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
  {
      #ifdef WITH_THREAD
      PyThreadState *_save;
      Py_UNBLOCK_THREADS
      #endif
      /*try:*/ {

        /* "dipy/tracking/distances.pyx":2047
 * 
 *     with nogil:
 *         for i from 0<=i<tlen-1:             # <<<<<<<<<<<<<<
 *             curr=i*3
 *             a[0]=t[curr]
 */
        __pyx_t_2 = (__pyx_v_tlen - 1);
        for (__pyx_v_i = 0; __pyx_v_i < __pyx_t_2; __pyx_v_i++) {

          /* "dipy/tracking/distances.pyx":2048
 *     with nogil:
 *         for i from 0<=i<tlen-1:
 *             curr=i*3             # <<<<<<<<<<<<<<
 *             a[0]=t[curr]
 *             a[1]=t[curr+1]
 */
          __pyx_v_curr = (__pyx_v_i * 3);

          /* "dipy/tracking/distances.pyx":2049
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 *             a[0]=t[curr]             # <<<<<<<<<<<<<<
 *             a[1]=t[curr+1]
 *             a[2]=t[curr+2]
 */
          (__pyx_v_a[0]) = (__pyx_v_t[__pyx_v_curr]);

          /* "dipy/tracking/distances.pyx":2050
 *             curr=i*3
 *             a[0]=t[curr]
 *             a[1]=t[curr+1]             # <<<<<<<<<<<<<<
 *             a[2]=t[curr+2]
 *             b[0]=t[curr+3]
 */
          (__pyx_v_a[1]) = (__pyx_v_t[(__pyx_v_curr + 1)]);

          /* "dipy/tracking/distances.pyx":2051
 *             a[0]=t[curr]
 *             a[1]=t[curr+1]
 *             a[2]=t[curr+2]             # <<<<<<<<<<<<<<
 *             b[0]=t[curr+3]
 *             b[1]=t[curr+4]
 */
          (__pyx_v_a[2]) = (__pyx_v_t[(__pyx_v_curr + 2)]);

          /* "dipy/tracking/distances.pyx":2052
 *             a[1]=t[curr+1]
 *             a[2]=t[curr+2]
 *             b[0]=t[curr+3]             # <<<<<<<<<<<<<<
 *             b[1]=t[curr+4]
 *             b[2]=t[curr+5]
 */
          (__pyx_v_b[0]) = (__pyx_v_t[(__pyx_v_curr + 3)]);

          /* "dipy/tracking/distances.pyx":2053
 *             a[2]=t[curr+2]
 *             b[0]=t[curr+3]
 *             b[1]=t[curr+4]             # <<<<<<<<<<<<<<
 *             b[2]=t[curr+5]
 *             for j from 0<=j<rlen:
 */
          (__pyx_v_b[1]) = (__pyx_v_t[(__pyx_v_curr + 4)]);

          /* "dipy/tracking/distances.pyx":2054
 *             b[0]=t[curr+3]
 *             b[1]=t[curr+4]
 *             b[2]=t[curr+5]             # <<<<<<<<<<<<<<
 *             for j from 0<=j<rlen:
 *                 currp=j*3
 */
          (__pyx_v_b[2]) = (__pyx_v_t[(__pyx_v_curr + 5)]);

          /* "dipy/tracking/distances.pyx":2055
 *             b[1]=t[curr+4]
 *             b[2]=t[curr+5]
 *             for j from 0<=j<rlen:             # <<<<<<<<<<<<<<
 *                 currp=j*3
 *                 p[0]=r[currp]
 */
          __pyx_t_3 = __pyx_v_rlen;
          for (__pyx_v_j = 0; __pyx_v_j < __pyx_t_3; __pyx_v_j++) {

            /* "dipy/tracking/distances.pyx":2056
 *             b[2]=t[curr+5]
 *             for j from 0<=j<rlen:
 *                 currp=j*3             # <<<<<<<<<<<<<<
 *                 p[0]=r[currp]
 *                 p[1]=r[currp+1]
 */
            __pyx_v_currp = (__pyx_v_j * 3);

            /* "dipy/tracking/distances.pyx":2057
 *             for j from 0<=j<rlen:
 *                 currp=j*3
 *                 p[0]=r[currp]             # <<<<<<<<<<<<<<
 *                 p[1]=r[currp+1]
 *                 p[2]=r[currp+2]
 */
            (__pyx_v_p[0]) = (__pyx_v_r[__pyx_v_currp]);

            /* "dipy/tracking/distances.pyx":2058
 *                 currp=j*3
 *                 p[0]=r[currp]
 *                 p[1]=r[currp+1]             # <<<<<<<<<<<<<<
 *                 p[2]=r[currp+2]
 *                 dist=cpoint_segment_sq_dist(<float *>a,<float *>b,<float *>p)
 */
            (__pyx_v_p[1]) = (__pyx_v_r[(__pyx_v_currp + 1)]);

            /* "dipy/tracking/distances.pyx":2059
 *                 p[0]=r[currp]
 *                 p[1]=r[currp+1]
 *                 p[2]=r[currp+2]             # <<<<<<<<<<<<<<
 *                 dist=cpoint_segment_sq_dist(<float *>a,<float *>b,<float *>p)
 *                 if dist<=sq_dist_thr:
 */
            (__pyx_v_p[2]) = (__pyx_v_r[(__pyx_v_currp + 2)]);

            /* "dipy/tracking/distances.pyx":2060
 *                 p[1]=r[currp+1]
 *                 p[2]=r[currp+2]
 *                 dist=cpoint_segment_sq_dist(<float *>a,<float *>b,<float *>p)             # <<<<<<<<<<<<<<
 *                 if dist<=sq_dist_thr:
 *                     intersects=1
 */
            __pyx_v_dist = __pyx_f_4dipy_8tracking_9distances_cpoint_segment_sq_dist(((float *)__pyx_v_a), ((float *)__pyx_v_b), ((float *)__pyx_v_p));

            /* "dipy/tracking/distances.pyx":2061
 *                 p[2]=r[currp+2]
 *                 dist=cpoint_segment_sq_dist(<float *>a,<float *>b,<float *>p)
 *                 if dist<=sq_dist_thr:             # <<<<<<<<<<<<<<
 *                     intersects=1
 *                     break
 */
            __pyx_t_4 = ((__pyx_v_dist <= __pyx_v_sq_dist_thr) != 0);
            if (__pyx_t_4) {

              /* "dipy/tracking/distances.pyx":2062
 *                 dist=cpoint_segment_sq_dist(<float *>a,<float *>b,<float *>p)
 *                 if dist<=sq_dist_thr:
 *                     intersects=1             # <<<<<<<<<<<<<<
 *                     break
 *             if intersects==1:
 */
              __pyx_v_intersects = 1;

              /* "dipy/tracking/distances.pyx":2063
 *                 if dist<=sq_dist_thr:
 *                     intersects=1
 *                     break             # <<<<<<<<<<<<<<
 *             if intersects==1:
 *                 break
 */
              goto __pyx_L9_break;
              goto __pyx_L10;
            }
            __pyx_L10:;
          }
          __pyx_L9_break:;

          /* "dipy/tracking/distances.pyx":2064
 *                     intersects=1
 *                     break
 *             if intersects==1:             # <<<<<<<<<<<<<<
 *                 break
 *     if intersects==1:
 */
          __pyx_t_4 = ((__pyx_v_intersects == 1) != 0);
          if (__pyx_t_4) {

            /* "dipy/tracking/distances.pyx":2065
 *                     break
 *             if intersects==1:
 *                 break             # <<<<<<<<<<<<<<
 *     if intersects==1:
 *         return True
 */
            goto __pyx_L7_break;
            goto __pyx_L11;
          }
          __pyx_L11:;
        }
        __pyx_L7_break:;
      }

      /* "dipy/tracking/distances.pyx":2046
 *         int intersects=0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
      /*finally:*/ {
        #ifdef WITH_THREAD
        Py_BLOCK_THREADS
        #endif
      }
  }

  /* "dipy/tracking/distances.pyx":2066
 *             if intersects==1:
 *                 break
 *     if intersects==1:             # <<<<<<<<<<<<<<
 *         return True
 *     else:
 */
  __pyx_t_4 = ((__pyx_v_intersects == 1) != 0);
  if (__pyx_t_4) {

    /* "dipy/tracking/distances.pyx":2067
 *                 break
 *     if intersects==1:
 *         return True             # <<<<<<<<<<<<<<
 *     else:
 *         return False
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_5 = __Pyx_PyBool_FromLong(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2067; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_r = __pyx_t_5;
    __pyx_t_5 = 0;
    goto __pyx_L0;
    goto __pyx_L12;
  }
  /*else*/ {

    /* "dipy/tracking/distances.pyx":2069
 *         return True
 *     else:
 *         return False             # <<<<<<<<<<<<<<
 */
    __Pyx_XDECREF(__pyx_r);
    __pyx_t_5 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2069; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_r = __pyx_t_5;
    __pyx_t_5 = 0;
    goto __pyx_L0;
  }
  __pyx_L12:;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_5);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_roi.rcbuffer->pybuffer);
    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.track_roi_intersection_check", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_roi.rcbuffer->pybuffer);
  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_track.rcbuffer->pybuffer);
  __pyx_L2:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static CYTHON_UNUSED int __pyx_pw_5numpy_7ndarray_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /*proto*/
static CYTHON_UNUSED int __pyx_pw_5numpy_7ndarray_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {
  int __pyx_r;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("__getbuffer__ (wrapper)", 0);
  __pyx_r = __pyx_pf_5numpy_7ndarray___getbuffer__(((PyArrayObject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info), ((int)__pyx_v_flags));
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":194
 *         # experimental exception made for __getbuffer__ and __releasebuffer__
 *         # -- the details of this may change.
 *         def __getbuffer__(ndarray self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<
 *             # This implementation of getbuffer is geared towards Cython
 *             # requirements, and does not yet fullfill the PEP.
 */

static int __pyx_pf_5numpy_7ndarray___getbuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {
  int __pyx_v_copy_shape;
  int __pyx_v_i;
  int __pyx_v_ndim;
  int __pyx_v_endian_detector;
  int __pyx_v_little_endian;
  int __pyx_v_t;
  char *__pyx_v_f;
  PyArray_Descr *__pyx_v_descr = 0;
  int __pyx_v_offset;
  int __pyx_v_hasfields;
  int __pyx_r;
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  int __pyx_t_2;
  int __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  int __pyx_t_5;
  int __pyx_t_6;
  int __pyx_t_7;
  PyObject *__pyx_t_8 = NULL;
  char *__pyx_t_9;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("__getbuffer__", 0);
  if (__pyx_v_info != NULL) {
    __pyx_v_info->obj = Py_None; __Pyx_INCREF(Py_None);
    __Pyx_GIVEREF(__pyx_v_info->obj);
  }

  /* "numpy.pxd":200
 *             # of flags
 * 
 *             if info == NULL: return             # <<<<<<<<<<<<<<
 * 
 *             cdef int copy_shape, i, ndim
 */
  __pyx_t_1 = ((__pyx_v_info == NULL) != 0);
  if (__pyx_t_1) {
    __pyx_r = 0;
    goto __pyx_L0;
    goto __pyx_L3;
  }
  __pyx_L3:;

  /* "numpy.pxd":203
 * 
 *             cdef int copy_shape, i, ndim
 *             cdef int endian_detector = 1             # <<<<<<<<<<<<<<
 *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
 * 
 */
  __pyx_v_endian_detector = 1;

  /* "numpy.pxd":204
 *             cdef int copy_shape, i, ndim
 *             cdef int endian_detector = 1
 *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)             # <<<<<<<<<<<<<<
 * 
 *             ndim = PyArray_NDIM(self)
 */
  __pyx_v_little_endian = ((((char *)(&__pyx_v_endian_detector))[0]) != 0);

  /* "numpy.pxd":206
 *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
 * 
 *             ndim = PyArray_NDIM(self)             # <<<<<<<<<<<<<<
 * 
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):
 */
  __pyx_v_ndim = PyArray_NDIM(__pyx_v_self);

  /* "numpy.pxd":208
 *             ndim = PyArray_NDIM(self)
 * 
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):             # <<<<<<<<<<<<<<
 *                 copy_shape = 1
 *             else:
 */
  __pyx_t_1 = (((sizeof(npy_intp)) != (sizeof(Py_ssize_t))) != 0);
  if (__pyx_t_1) {

    /* "numpy.pxd":209
 * 
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):
 *                 copy_shape = 1             # <<<<<<<<<<<<<<
 *             else:
 *                 copy_shape = 0
 */
    __pyx_v_copy_shape = 1;
    goto __pyx_L4;
  }
  /*else*/ {

    /* "numpy.pxd":211
 *                 copy_shape = 1
 *             else:
 *                 copy_shape = 0             # <<<<<<<<<<<<<<
 * 
 *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
 */
    __pyx_v_copy_shape = 0;
  }
  __pyx_L4:;

  /* "numpy.pxd":213
 *                 copy_shape = 0
 * 
 *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)             # <<<<<<<<<<<<<<
 *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
 *                 raise ValueError(u"ndarray is not C contiguous")
 */
  __pyx_t_1 = (((__pyx_v_flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) != 0);
  if (__pyx_t_1) {

    /* "numpy.pxd":214
 * 
 *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
 *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):             # <<<<<<<<<<<<<<
 *                 raise ValueError(u"ndarray is not C contiguous")
 * 
 */
    __pyx_t_2 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_C_CONTIGUOUS) != 0)) != 0);
    __pyx_t_3 = __pyx_t_2;
  } else {
    __pyx_t_3 = __pyx_t_1;
  }
  if (__pyx_t_3) {

    /* "numpy.pxd":215
 *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
 *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
 *                 raise ValueError(u"ndarray is not C contiguous")             # <<<<<<<<<<<<<<
 * 
 *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
 */
    __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_30), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    goto __pyx_L5;
  }
  __pyx_L5:;

  /* "numpy.pxd":217
 *                 raise ValueError(u"ndarray is not C contiguous")
 * 
 *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)             # <<<<<<<<<<<<<<
 *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
 *                 raise ValueError(u"ndarray is not Fortran contiguous")
 */
  __pyx_t_3 = (((__pyx_v_flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) != 0);
  if (__pyx_t_3) {

    /* "numpy.pxd":218
 * 
 *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
 *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):             # <<<<<<<<<<<<<<
 *                 raise ValueError(u"ndarray is not Fortran contiguous")
 * 
 */
    __pyx_t_1 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_F_CONTIGUOUS) != 0)) != 0);
    __pyx_t_2 = __pyx_t_1;
  } else {
    __pyx_t_2 = __pyx_t_3;
  }
  if (__pyx_t_2) {

    /* "numpy.pxd":219
 *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
 *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
 *                 raise ValueError(u"ndarray is not Fortran contiguous")             # <<<<<<<<<<<<<<
 * 
 *             info.buf = PyArray_DATA(self)
 */
    __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_32), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_Raise(__pyx_t_4, 0, 0, 0);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    goto __pyx_L6;
  }
  __pyx_L6:;

  /* "numpy.pxd":221
 *                 raise ValueError(u"ndarray is not Fortran contiguous")
 * 
 *             info.buf = PyArray_DATA(self)             # <<<<<<<<<<<<<<
 *             info.ndim = ndim
 *             if copy_shape:
 */
  __pyx_v_info->buf = PyArray_DATA(__pyx_v_self);

  /* "numpy.pxd":222
 * 
 *             info.buf = PyArray_DATA(self)
 *             info.ndim = ndim             # <<<<<<<<<<<<<<
 *             if copy_shape:
 *                 # Allocate new buffer for strides and shape info.
 */
  __pyx_v_info->ndim = __pyx_v_ndim;

  /* "numpy.pxd":223
 *             info.buf = PyArray_DATA(self)
 *             info.ndim = ndim
 *             if copy_shape:             # <<<<<<<<<<<<<<
 *                 # Allocate new buffer for strides and shape info.
 *                 # This is allocated as one block, strides first.
 */
  __pyx_t_2 = (__pyx_v_copy_shape != 0);
  if (__pyx_t_2) {

    /* "numpy.pxd":226
 *                 # Allocate new buffer for strides and shape info.
 *                 # This is allocated as one block, strides first.
 *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)             # <<<<<<<<<<<<<<
 *                 info.shape = info.strides + ndim
 *                 for i in range(ndim):
 */
    __pyx_v_info->strides = ((Py_ssize_t *)malloc((((sizeof(Py_ssize_t)) * ((size_t)__pyx_v_ndim)) * 2)));

    /* "numpy.pxd":227
 *                 # This is allocated as one block, strides first.
 *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)
 *                 info.shape = info.strides + ndim             # <<<<<<<<<<<<<<
 *                 for i in range(ndim):
 *                     info.strides[i] = PyArray_STRIDES(self)[i]
 */
    __pyx_v_info->shape = (__pyx_v_info->strides + __pyx_v_ndim);

    /* "numpy.pxd":228
 *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)
 *                 info.shape = info.strides + ndim
 *                 for i in range(ndim):             # <<<<<<<<<<<<<<
 *                     info.strides[i] = PyArray_STRIDES(self)[i]
 *                     info.shape[i] = PyArray_DIMS(self)[i]
 */
    __pyx_t_5 = __pyx_v_ndim;
    for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_5; __pyx_t_6+=1) {
      __pyx_v_i = __pyx_t_6;

      /* "numpy.pxd":229
 *                 info.shape = info.strides + ndim
 *                 for i in range(ndim):
 *                     info.strides[i] = PyArray_STRIDES(self)[i]             # <<<<<<<<<<<<<<
 *                     info.shape[i] = PyArray_DIMS(self)[i]
 *             else:
 */
      (__pyx_v_info->strides[__pyx_v_i]) = (PyArray_STRIDES(__pyx_v_self)[__pyx_v_i]);

      /* "numpy.pxd":230
 *                 for i in range(ndim):
 *                     info.strides[i] = PyArray_STRIDES(self)[i]
 *                     info.shape[i] = PyArray_DIMS(self)[i]             # <<<<<<<<<<<<<<
 *             else:
 *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
 */
      (__pyx_v_info->shape[__pyx_v_i]) = (PyArray_DIMS(__pyx_v_self)[__pyx_v_i]);
    }
    goto __pyx_L7;
  }
  /*else*/ {

    /* "numpy.pxd":232
 *                     info.shape[i] = PyArray_DIMS(self)[i]
 *             else:
 *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)             # <<<<<<<<<<<<<<
 *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)
 *             info.suboffsets = NULL
 */
    __pyx_v_info->strides = ((Py_ssize_t *)PyArray_STRIDES(__pyx_v_self));

    /* "numpy.pxd":233
 *             else:
 *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
 *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)             # <<<<<<<<<<<<<<
 *             info.suboffsets = NULL
 *             info.itemsize = PyArray_ITEMSIZE(self)
 */
    __pyx_v_info->shape = ((Py_ssize_t *)PyArray_DIMS(__pyx_v_self));
  }
  __pyx_L7:;

  /* "numpy.pxd":234
 *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
 *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)
 *             info.suboffsets = NULL             # <<<<<<<<<<<<<<
 *             info.itemsize = PyArray_ITEMSIZE(self)
 *             info.readonly = not PyArray_ISWRITEABLE(self)
 */
  __pyx_v_info->suboffsets = NULL;

  /* "numpy.pxd":235
 *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)
 *             info.suboffsets = NULL
 *             info.itemsize = PyArray_ITEMSIZE(self)             # <<<<<<<<<<<<<<
 *             info.readonly = not PyArray_ISWRITEABLE(self)
 * 
 */
  __pyx_v_info->itemsize = PyArray_ITEMSIZE(__pyx_v_self);

  /* "numpy.pxd":236
 *             info.suboffsets = NULL
 *             info.itemsize = PyArray_ITEMSIZE(self)
 *             info.readonly = not PyArray_ISWRITEABLE(self)             # <<<<<<<<<<<<<<
 * 
 *             cdef int t
 */
  __pyx_v_info->readonly = (!(PyArray_ISWRITEABLE(__pyx_v_self) != 0));

  /* "numpy.pxd":239
 * 
 *             cdef int t
 *             cdef char* f = NULL             # <<<<<<<<<<<<<<
 *             cdef dtype descr = self.descr
 *             cdef list stack
 */
  __pyx_v_f = NULL;

  /* "numpy.pxd":240
 *             cdef int t
 *             cdef char* f = NULL
 *             cdef dtype descr = self.descr             # <<<<<<<<<<<<<<
 *             cdef list stack
 *             cdef int offset
 */
  __pyx_t_4 = ((PyObject *)__pyx_v_self->descr);
  __Pyx_INCREF(__pyx_t_4);
  __pyx_v_descr = ((PyArray_Descr *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "numpy.pxd":244
 *             cdef int offset
 * 
 *             cdef bint hasfields = PyDataType_HASFIELDS(descr)             # <<<<<<<<<<<<<<
 * 
 *             if not hasfields and not copy_shape:
 */
  __pyx_v_hasfields = PyDataType_HASFIELDS(__pyx_v_descr);

  /* "numpy.pxd":246
 *             cdef bint hasfields = PyDataType_HASFIELDS(descr)
 * 
 *             if not hasfields and not copy_shape:             # <<<<<<<<<<<<<<
 *                 # do not call releasebuffer
 *                 info.obj = None
 */
  __pyx_t_2 = ((!(__pyx_v_hasfields != 0)) != 0);
  if (__pyx_t_2) {
    __pyx_t_3 = ((!(__pyx_v_copy_shape != 0)) != 0);
    __pyx_t_1 = __pyx_t_3;
  } else {
    __pyx_t_1 = __pyx_t_2;
  }
  if (__pyx_t_1) {

    /* "numpy.pxd":248
 *             if not hasfields and not copy_shape:
 *                 # do not call releasebuffer
 *                 info.obj = None             # <<<<<<<<<<<<<<
 *             else:
 *                 # need to call releasebuffer
 */
    __Pyx_INCREF(Py_None);
    __Pyx_GIVEREF(Py_None);
    __Pyx_GOTREF(__pyx_v_info->obj);
    __Pyx_DECREF(__pyx_v_info->obj);
    __pyx_v_info->obj = Py_None;
    goto __pyx_L10;
  }
  /*else*/ {

    /* "numpy.pxd":251
 *             else:
 *                 # need to call releasebuffer
 *                 info.obj = self             # <<<<<<<<<<<<<<
 * 
 *             if not hasfields:
 */
    __Pyx_INCREF(((PyObject *)__pyx_v_self));
    __Pyx_GIVEREF(((PyObject *)__pyx_v_self));
    __Pyx_GOTREF(__pyx_v_info->obj);
    __Pyx_DECREF(__pyx_v_info->obj);
    __pyx_v_info->obj = ((PyObject *)__pyx_v_self);
  }
  __pyx_L10:;

  /* "numpy.pxd":253
 *                 info.obj = self
 * 
 *             if not hasfields:             # <<<<<<<<<<<<<<
 *                 t = descr.type_num
 *                 if ((descr.byteorder == c'>' and little_endian) or
 */
  __pyx_t_1 = ((!(__pyx_v_hasfields != 0)) != 0);
  if (__pyx_t_1) {

    /* "numpy.pxd":254
 * 
 *             if not hasfields:
 *                 t = descr.type_num             # <<<<<<<<<<<<<<
 *                 if ((descr.byteorder == c'>' and little_endian) or
 *                     (descr.byteorder == c'<' and not little_endian)):
 */
    __pyx_t_5 = __pyx_v_descr->type_num;
    __pyx_v_t = __pyx_t_5;

    /* "numpy.pxd":255
 *             if not hasfields:
 *                 t = descr.type_num
 *                 if ((descr.byteorder == c'>' and little_endian) or             # <<<<<<<<<<<<<<
 *                     (descr.byteorder == c'<' and not little_endian)):
 *                     raise ValueError(u"Non-native byte order not supported")
 */
    __pyx_t_1 = ((__pyx_v_descr->byteorder == '>') != 0);
    if (__pyx_t_1) {
      __pyx_t_2 = (__pyx_v_little_endian != 0);
    } else {
      __pyx_t_2 = __pyx_t_1;
    }
    if (!__pyx_t_2) {

      /* "numpy.pxd":256
 *                 t = descr.type_num
 *                 if ((descr.byteorder == c'>' and little_endian) or
 *                     (descr.byteorder == c'<' and not little_endian)):             # <<<<<<<<<<<<<<
 *                     raise ValueError(u"Non-native byte order not supported")
 *                 if   t == NPY_BYTE:        f = "b"
 */
      __pyx_t_1 = ((__pyx_v_descr->byteorder == '<') != 0);
      if (__pyx_t_1) {
        __pyx_t_3 = ((!(__pyx_v_little_endian != 0)) != 0);
        __pyx_t_7 = __pyx_t_3;
      } else {
        __pyx_t_7 = __pyx_t_1;
      }
      __pyx_t_1 = __pyx_t_7;
    } else {
      __pyx_t_1 = __pyx_t_2;
    }
    if (__pyx_t_1) {

      /* "numpy.pxd":257
 *                 if ((descr.byteorder == c'>' and little_endian) or
 *                     (descr.byteorder == c'<' and not little_endian)):
 *                     raise ValueError(u"Non-native byte order not supported")             # <<<<<<<<<<<<<<
 *                 if   t == NPY_BYTE:        f = "b"
 *                 elif t == NPY_UBYTE:       f = "B"
 */
      __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_34), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_Raise(__pyx_t_4, 0, 0, 0);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L12;
    }
    __pyx_L12:;

    /* "numpy.pxd":274
 *                 elif t == NPY_CDOUBLE:     f = "Zd"
 *                 elif t == NPY_CLONGDOUBLE: f = "Zg"
 *                 elif t == NPY_OBJECT:      f = "O"             # <<<<<<<<<<<<<<
 *                 else:
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 */
    switch (__pyx_v_t) {

      /* "numpy.pxd":258
 *                     (descr.byteorder == c'<' and not little_endian)):
 *                     raise ValueError(u"Non-native byte order not supported")
 *                 if   t == NPY_BYTE:        f = "b"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_UBYTE:       f = "B"
 *                 elif t == NPY_SHORT:       f = "h"
 */
      case NPY_BYTE:
      __pyx_v_f = __pyx_k__b;
      break;

      /* "numpy.pxd":259
 *                     raise ValueError(u"Non-native byte order not supported")
 *                 if   t == NPY_BYTE:        f = "b"
 *                 elif t == NPY_UBYTE:       f = "B"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_SHORT:       f = "h"
 *                 elif t == NPY_USHORT:      f = "H"
 */
      case NPY_UBYTE:
      __pyx_v_f = __pyx_k__B;
      break;

      /* "numpy.pxd":260
 *                 if   t == NPY_BYTE:        f = "b"
 *                 elif t == NPY_UBYTE:       f = "B"
 *                 elif t == NPY_SHORT:       f = "h"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_USHORT:      f = "H"
 *                 elif t == NPY_INT:         f = "i"
 */
      case NPY_SHORT:
      __pyx_v_f = __pyx_k__h;
      break;

      /* "numpy.pxd":261
 *                 elif t == NPY_UBYTE:       f = "B"
 *                 elif t == NPY_SHORT:       f = "h"
 *                 elif t == NPY_USHORT:      f = "H"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_INT:         f = "i"
 *                 elif t == NPY_UINT:        f = "I"
 */
      case NPY_USHORT:
      __pyx_v_f = __pyx_k__H;
      break;

      /* "numpy.pxd":262
 *                 elif t == NPY_SHORT:       f = "h"
 *                 elif t == NPY_USHORT:      f = "H"
 *                 elif t == NPY_INT:         f = "i"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_UINT:        f = "I"
 *                 elif t == NPY_LONG:        f = "l"
 */
      case NPY_INT:
      __pyx_v_f = __pyx_k__i;
      break;

      /* "numpy.pxd":263
 *                 elif t == NPY_USHORT:      f = "H"
 *                 elif t == NPY_INT:         f = "i"
 *                 elif t == NPY_UINT:        f = "I"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_LONG:        f = "l"
 *                 elif t == NPY_ULONG:       f = "L"
 */
      case NPY_UINT:
      __pyx_v_f = __pyx_k__I;
      break;

      /* "numpy.pxd":264
 *                 elif t == NPY_INT:         f = "i"
 *                 elif t == NPY_UINT:        f = "I"
 *                 elif t == NPY_LONG:        f = "l"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_ULONG:       f = "L"
 *                 elif t == NPY_LONGLONG:    f = "q"
 */
      case NPY_LONG:
      __pyx_v_f = __pyx_k__l;
      break;

      /* "numpy.pxd":265
 *                 elif t == NPY_UINT:        f = "I"
 *                 elif t == NPY_LONG:        f = "l"
 *                 elif t == NPY_ULONG:       f = "L"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_LONGLONG:    f = "q"
 *                 elif t == NPY_ULONGLONG:   f = "Q"
 */
      case NPY_ULONG:
      __pyx_v_f = __pyx_k__L;
      break;

      /* "numpy.pxd":266
 *                 elif t == NPY_LONG:        f = "l"
 *                 elif t == NPY_ULONG:       f = "L"
 *                 elif t == NPY_LONGLONG:    f = "q"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_ULONGLONG:   f = "Q"
 *                 elif t == NPY_FLOAT:       f = "f"
 */
      case NPY_LONGLONG:
      __pyx_v_f = __pyx_k__q;
      break;

      /* "numpy.pxd":267
 *                 elif t == NPY_ULONG:       f = "L"
 *                 elif t == NPY_LONGLONG:    f = "q"
 *                 elif t == NPY_ULONGLONG:   f = "Q"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_FLOAT:       f = "f"
 *                 elif t == NPY_DOUBLE:      f = "d"
 */
      case NPY_ULONGLONG:
      __pyx_v_f = __pyx_k__Q;
      break;

      /* "numpy.pxd":268
 *                 elif t == NPY_LONGLONG:    f = "q"
 *                 elif t == NPY_ULONGLONG:   f = "Q"
 *                 elif t == NPY_FLOAT:       f = "f"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_DOUBLE:      f = "d"
 *                 elif t == NPY_LONGDOUBLE:  f = "g"
 */
      case NPY_FLOAT:
      __pyx_v_f = __pyx_k__f;
      break;

      /* "numpy.pxd":269
 *                 elif t == NPY_ULONGLONG:   f = "Q"
 *                 elif t == NPY_FLOAT:       f = "f"
 *                 elif t == NPY_DOUBLE:      f = "d"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_LONGDOUBLE:  f = "g"
 *                 elif t == NPY_CFLOAT:      f = "Zf"
 */
      case NPY_DOUBLE:
      __pyx_v_f = __pyx_k__d;
      break;

      /* "numpy.pxd":270
 *                 elif t == NPY_FLOAT:       f = "f"
 *                 elif t == NPY_DOUBLE:      f = "d"
 *                 elif t == NPY_LONGDOUBLE:  f = "g"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_CFLOAT:      f = "Zf"
 *                 elif t == NPY_CDOUBLE:     f = "Zd"
 */
      case NPY_LONGDOUBLE:
      __pyx_v_f = __pyx_k__g;
      break;

      /* "numpy.pxd":271
 *                 elif t == NPY_DOUBLE:      f = "d"
 *                 elif t == NPY_LONGDOUBLE:  f = "g"
 *                 elif t == NPY_CFLOAT:      f = "Zf"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_CDOUBLE:     f = "Zd"
 *                 elif t == NPY_CLONGDOUBLE: f = "Zg"
 */
      case NPY_CFLOAT:
      __pyx_v_f = __pyx_k__Zf;
      break;

      /* "numpy.pxd":272
 *                 elif t == NPY_LONGDOUBLE:  f = "g"
 *                 elif t == NPY_CFLOAT:      f = "Zf"
 *                 elif t == NPY_CDOUBLE:     f = "Zd"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_CLONGDOUBLE: f = "Zg"
 *                 elif t == NPY_OBJECT:      f = "O"
 */
      case NPY_CDOUBLE:
      __pyx_v_f = __pyx_k__Zd;
      break;

      /* "numpy.pxd":273
 *                 elif t == NPY_CFLOAT:      f = "Zf"
 *                 elif t == NPY_CDOUBLE:     f = "Zd"
 *                 elif t == NPY_CLONGDOUBLE: f = "Zg"             # <<<<<<<<<<<<<<
 *                 elif t == NPY_OBJECT:      f = "O"
 *                 else:
 */
      case NPY_CLONGDOUBLE:
      __pyx_v_f = __pyx_k__Zg;
      break;

      /* "numpy.pxd":274
 *                 elif t == NPY_CDOUBLE:     f = "Zd"
 *                 elif t == NPY_CLONGDOUBLE: f = "Zg"
 *                 elif t == NPY_OBJECT:      f = "O"             # <<<<<<<<<<<<<<
 *                 else:
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 */
      case NPY_OBJECT:
      __pyx_v_f = __pyx_k__O;
      break;
      default:

      /* "numpy.pxd":276
 *                 elif t == NPY_OBJECT:      f = "O"
 *                 else:
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)             # <<<<<<<<<<<<<<
 *                 info.format = f
 *                 return
 */
      __pyx_t_4 = PyInt_FromLong(__pyx_v_t); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_8 = PyNumber_Remainder(((PyObject *)__pyx_kp_u_35), __pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_8));
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_t_8));
      __Pyx_GIVEREF(((PyObject *)__pyx_t_8));
      __pyx_t_8 = 0;
      __pyx_t_8 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_8);
      __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
      __Pyx_Raise(__pyx_t_8, 0, 0, 0);
      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      break;
    }

    /* "numpy.pxd":277
 *                 else:
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 *                 info.format = f             # <<<<<<<<<<<<<<
 *                 return
 *             else:
 */
    __pyx_v_info->format = __pyx_v_f;

    /* "numpy.pxd":278
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 *                 info.format = f
 *                 return             # <<<<<<<<<<<<<<
 *             else:
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
 */
    __pyx_r = 0;
    goto __pyx_L0;
    goto __pyx_L11;
  }
  /*else*/ {

    /* "numpy.pxd":280
 *                 return
 *             else:
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)             # <<<<<<<<<<<<<<
 *                 info.format[0] = c'^' # Native data types, manual alignment
 *                 offset = 0
 */
    __pyx_v_info->format = ((char *)malloc(255));

    /* "numpy.pxd":281
 *             else:
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
 *                 info.format[0] = c'^' # Native data types, manual alignment             # <<<<<<<<<<<<<<
 *                 offset = 0
 *                 f = _util_dtypestring(descr, info.format + 1,
 */
    (__pyx_v_info->format[0]) = '^';

    /* "numpy.pxd":282
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
 *                 info.format[0] = c'^' # Native data types, manual alignment
 *                 offset = 0             # <<<<<<<<<<<<<<
 *                 f = _util_dtypestring(descr, info.format + 1,
 *                                       info.format + _buffer_format_string_len,
 */
    __pyx_v_offset = 0;

    /* "numpy.pxd":285
 *                 f = _util_dtypestring(descr, info.format + 1,
 *                                       info.format + _buffer_format_string_len,
 *                                       &offset)             # <<<<<<<<<<<<<<
 *                 f[0] = c'\0' # Terminate format string
 * 
 */
    __pyx_t_9 = __pyx_f_5numpy__util_dtypestring(__pyx_v_descr, (__pyx_v_info->format + 1), (__pyx_v_info->format + 255), (&__pyx_v_offset)); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_f = __pyx_t_9;

    /* "numpy.pxd":286
 *                                       info.format + _buffer_format_string_len,
 *                                       &offset)
 *                 f[0] = c'\0' # Terminate format string             # <<<<<<<<<<<<<<
 * 
 *         def __releasebuffer__(ndarray self, Py_buffer* info):
 */
    (__pyx_v_f[0]) = '\x00';
  }
  __pyx_L11:;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_8);
  __Pyx_AddTraceback("numpy.ndarray.__getbuffer__", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = -1;
  if (__pyx_v_info != NULL && __pyx_v_info->obj != NULL) {
    __Pyx_GOTREF(__pyx_v_info->obj);
    __Pyx_DECREF(__pyx_v_info->obj); __pyx_v_info->obj = NULL;
  }
  goto __pyx_L2;
  __pyx_L0:;
  if (__pyx_v_info != NULL && __pyx_v_info->obj == Py_None) {
    __Pyx_GOTREF(Py_None);
    __Pyx_DECREF(Py_None); __pyx_v_info->obj = NULL;
  }
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_descr);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static CYTHON_UNUSED void __pyx_pw_5numpy_7ndarray_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info); /*proto*/
static CYTHON_UNUSED void __pyx_pw_5numpy_7ndarray_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info) {
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("__releasebuffer__ (wrapper)", 0);
  __pyx_pf_5numpy_7ndarray_2__releasebuffer__(((PyArrayObject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info));
  __Pyx_RefNannyFinishContext();
}

/* "numpy.pxd":288
 *                 f[0] = c'\0' # Terminate format string
 * 
 *         def __releasebuffer__(ndarray self, Py_buffer* info):             # <<<<<<<<<<<<<<
 *             if PyArray_HASFIELDS(self):
 *                 stdlib.free(info.format)
 */

static void __pyx_pf_5numpy_7ndarray_2__releasebuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info) {
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  __Pyx_RefNannySetupContext("__releasebuffer__", 0);

  /* "numpy.pxd":289
 * 
 *         def __releasebuffer__(ndarray self, Py_buffer* info):
 *             if PyArray_HASFIELDS(self):             # <<<<<<<<<<<<<<
 *                 stdlib.free(info.format)
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):
 */
  __pyx_t_1 = (PyArray_HASFIELDS(__pyx_v_self) != 0);
  if (__pyx_t_1) {

    /* "numpy.pxd":290
 *         def __releasebuffer__(ndarray self, Py_buffer* info):
 *             if PyArray_HASFIELDS(self):
 *                 stdlib.free(info.format)             # <<<<<<<<<<<<<<
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):
 *                 stdlib.free(info.strides)
 */
    free(__pyx_v_info->format);
    goto __pyx_L3;
  }
  __pyx_L3:;

  /* "numpy.pxd":291
 *             if PyArray_HASFIELDS(self):
 *                 stdlib.free(info.format)
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):             # <<<<<<<<<<<<<<
 *                 stdlib.free(info.strides)
 *                 # info.shape was stored after info.strides in the same block
 */
  __pyx_t_1 = (((sizeof(npy_intp)) != (sizeof(Py_ssize_t))) != 0);
  if (__pyx_t_1) {

    /* "numpy.pxd":292
 *                 stdlib.free(info.format)
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):
 *                 stdlib.free(info.strides)             # <<<<<<<<<<<<<<
 *                 # info.shape was stored after info.strides in the same block
 * 
 */
    free(__pyx_v_info->strides);
    goto __pyx_L4;
  }
  __pyx_L4:;

  __Pyx_RefNannyFinishContext();
}

/* "numpy.pxd":768
 * ctypedef npy_cdouble     complex_t
 * 
 * cdef inline object PyArray_MultiIterNew1(a):             # <<<<<<<<<<<<<<
 *     return PyArray_MultiIterNew(1, <void*>a)
 * 
 */

static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew1(PyObject *__pyx_v_a) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew1", 0);

  /* "numpy.pxd":769
 * 
 * cdef inline object PyArray_MultiIterNew1(a):
 *     return PyArray_MultiIterNew(1, <void*>a)             # <<<<<<<<<<<<<<
 * 
 * cdef inline object PyArray_MultiIterNew2(a, b):
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyArray_MultiIterNew(1, ((void *)__pyx_v_a)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("numpy.PyArray_MultiIterNew1", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":771
 *     return PyArray_MultiIterNew(1, <void*>a)
 * 
 * cdef inline object PyArray_MultiIterNew2(a, b):             # <<<<<<<<<<<<<<
 *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)
 * 
 */

static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew2(PyObject *__pyx_v_a, PyObject *__pyx_v_b) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew2", 0);

  /* "numpy.pxd":772
 * 
 * cdef inline object PyArray_MultiIterNew2(a, b):
 *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)             # <<<<<<<<<<<<<<
 * 
 * cdef inline object PyArray_MultiIterNew3(a, b, c):
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyArray_MultiIterNew(2, ((void *)__pyx_v_a), ((void *)__pyx_v_b)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("numpy.PyArray_MultiIterNew2", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":774
 *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)
 * 
 * cdef inline object PyArray_MultiIterNew3(a, b, c):             # <<<<<<<<<<<<<<
 *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
 * 
 */

static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew3(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew3", 0);

  /* "numpy.pxd":775
 * 
 * cdef inline object PyArray_MultiIterNew3(a, b, c):
 *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)             # <<<<<<<<<<<<<<
 * 
 * cdef inline object PyArray_MultiIterNew4(a, b, c, d):
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyArray_MultiIterNew(3, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("numpy.PyArray_MultiIterNew3", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":777
 *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
 * 
 * cdef inline object PyArray_MultiIterNew4(a, b, c, d):             # <<<<<<<<<<<<<<
 *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
 * 
 */

static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew4(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c, PyObject *__pyx_v_d) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew4", 0);

  /* "numpy.pxd":778
 * 
 * cdef inline object PyArray_MultiIterNew4(a, b, c, d):
 *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)             # <<<<<<<<<<<<<<
 * 
 * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyArray_MultiIterNew(4, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c), ((void *)__pyx_v_d)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("numpy.PyArray_MultiIterNew4", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":780
 *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
 * 
 * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):             # <<<<<<<<<<<<<<
 *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
 * 
 */

static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew5(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c, PyObject *__pyx_v_d, PyObject *__pyx_v_e) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew5", 0);

  /* "numpy.pxd":781
 * 
 * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
 *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)             # <<<<<<<<<<<<<<
 * 
 * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyArray_MultiIterNew(5, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c), ((void *)__pyx_v_d), ((void *)__pyx_v_e)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("numpy.PyArray_MultiIterNew5", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":783
 *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
 * 
 * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:             # <<<<<<<<<<<<<<
 *     # Recursive utility function used in __getbuffer__ to get format
 *     # string. The new location in the format string is returned.
 */

static CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *__pyx_v_descr, char *__pyx_v_f, char *__pyx_v_end, int *__pyx_v_offset) {
  PyArray_Descr *__pyx_v_child = 0;
  int __pyx_v_endian_detector;
  int __pyx_v_little_endian;
  PyObject *__pyx_v_fields = 0;
  PyObject *__pyx_v_childname = NULL;
  PyObject *__pyx_v_new_offset = NULL;
  PyObject *__pyx_v_t = NULL;
  char *__pyx_r;
  __Pyx_RefNannyDeclarations
  PyObject *__pyx_t_1 = NULL;
  Py_ssize_t __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  PyObject *(*__pyx_t_6)(PyObject *);
  int __pyx_t_7;
  int __pyx_t_8;
  int __pyx_t_9;
  int __pyx_t_10;
  long __pyx_t_11;
  char *__pyx_t_12;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("_util_dtypestring", 0);

  /* "numpy.pxd":790
 *     cdef int delta_offset
 *     cdef tuple i
 *     cdef int endian_detector = 1             # <<<<<<<<<<<<<<
 *     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
 *     cdef tuple fields
 */
  __pyx_v_endian_detector = 1;

  /* "numpy.pxd":791
 *     cdef tuple i
 *     cdef int endian_detector = 1
 *     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)             # <<<<<<<<<<<<<<
 *     cdef tuple fields
 * 
 */
  __pyx_v_little_endian = ((((char *)(&__pyx_v_endian_detector))[0]) != 0);

  /* "numpy.pxd":794
 *     cdef tuple fields
 * 
 *     for childname in descr.names:             # <<<<<<<<<<<<<<
 *         fields = descr.fields[childname]
 *         child, new_offset = fields
 */
  if (unlikely(((PyObject *)__pyx_v_descr->names) == Py_None)) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_1 = ((PyObject *)__pyx_v_descr->names); __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;
  for (;;) {
    if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
    #if CYTHON_COMPILING_IN_CPYTHON
    __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_3); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    #else
    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    #endif
    __Pyx_XDECREF(__pyx_v_childname);
    __pyx_v_childname = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "numpy.pxd":795
 * 
 *     for childname in descr.names:
 *         fields = descr.fields[childname]             # <<<<<<<<<<<<<<
 *         child, new_offset = fields
 * 
 */
    __pyx_t_3 = PyObject_GetItem(__pyx_v_descr->fields, __pyx_v_childname); if (!__pyx_t_3) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    if (!(likely(PyTuple_CheckExact(__pyx_t_3))||((__pyx_t_3) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected tuple, got %.200s", Py_TYPE(__pyx_t_3)->tp_name), 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_XDECREF(((PyObject *)__pyx_v_fields));
    __pyx_v_fields = ((PyObject*)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "numpy.pxd":796
 *     for childname in descr.names:
 *         fields = descr.fields[childname]
 *         child, new_offset = fields             # <<<<<<<<<<<<<<
 * 
 *         if (end - f) - (new_offset - offset[0]) < 15:
 */
    if (likely(PyTuple_CheckExact(((PyObject *)__pyx_v_fields)))) {
      PyObject* sequence = ((PyObject *)__pyx_v_fields);
      #if CYTHON_COMPILING_IN_CPYTHON
      Py_ssize_t size = Py_SIZE(sequence);
      #else
      Py_ssize_t size = PySequence_Size(sequence);
      #endif
      if (unlikely(size != 2)) {
        if (size > 2) __Pyx_RaiseTooManyValuesError(2);
        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      #if CYTHON_COMPILING_IN_CPYTHON
      __pyx_t_3 = PyTuple_GET_ITEM(sequence, 0); 
      __pyx_t_4 = PyTuple_GET_ITEM(sequence, 1); 
      __Pyx_INCREF(__pyx_t_3);
      __Pyx_INCREF(__pyx_t_4);
      #else
      __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_4 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      #endif
    } else if (1) {
      __Pyx_RaiseNoneNotIterableError(); {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else
    {
      Py_ssize_t index = -1;
      __pyx_t_5 = PyObject_GetIter(((PyObject *)__pyx_v_fields)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_6 = Py_TYPE(__pyx_t_5)->tp_iternext;
      index = 0; __pyx_t_3 = __pyx_t_6(__pyx_t_5); if (unlikely(!__pyx_t_3)) goto __pyx_L5_unpacking_failed;
      __Pyx_GOTREF(__pyx_t_3);
      index = 1; __pyx_t_4 = __pyx_t_6(__pyx_t_5); if (unlikely(!__pyx_t_4)) goto __pyx_L5_unpacking_failed;
      __Pyx_GOTREF(__pyx_t_4);
      if (__Pyx_IternextUnpackEndCheck(__pyx_t_6(__pyx_t_5), 2) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_6 = NULL;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      goto __pyx_L6_unpacking_done;
      __pyx_L5_unpacking_failed:;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = NULL;
      if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_L6_unpacking_done:;
    }
    if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_dtype))))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_XDECREF(((PyObject *)__pyx_v_child));
    __pyx_v_child = ((PyArray_Descr *)__pyx_t_3);
    __pyx_t_3 = 0;
    __Pyx_XDECREF(__pyx_v_new_offset);
    __pyx_v_new_offset = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "numpy.pxd":798
 *         child, new_offset = fields
 * 
 *         if (end - f) - (new_offset - offset[0]) < 15:             # <<<<<<<<<<<<<<
 *             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")
 * 
 */
    __pyx_t_4 = PyInt_FromLong((__pyx_v_end - __pyx_v_f)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = PyInt_FromLong((__pyx_v_offset[0])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_5 = PyNumber_Subtract(__pyx_v_new_offset, __pyx_t_3); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = PyNumber_Subtract(__pyx_t_4, __pyx_t_5); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = PyObject_RichCompare(__pyx_t_3, __pyx_int_15, Py_LT); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (__pyx_t_7) {

      /* "numpy.pxd":799
 * 
 *         if (end - f) - (new_offset - offset[0]) < 15:
 *             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")             # <<<<<<<<<<<<<<
 * 
 *         if ((child.byteorder == c'>' and little_endian) or
 */
      __pyx_t_5 = PyObject_Call(__pyx_builtin_RuntimeError, ((PyObject *)__pyx_k_tuple_37), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_Raise(__pyx_t_5, 0, 0, 0);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L7;
    }
    __pyx_L7:;

    /* "numpy.pxd":801
 *             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")
 * 
 *         if ((child.byteorder == c'>' and little_endian) or             # <<<<<<<<<<<<<<
 *             (child.byteorder == c'<' and not little_endian)):
 *             raise ValueError(u"Non-native byte order not supported")
 */
    __pyx_t_7 = ((__pyx_v_child->byteorder == '>') != 0);
    if (__pyx_t_7) {
      __pyx_t_8 = (__pyx_v_little_endian != 0);
    } else {
      __pyx_t_8 = __pyx_t_7;
    }
    if (!__pyx_t_8) {

      /* "numpy.pxd":802
 * 
 *         if ((child.byteorder == c'>' and little_endian) or
 *             (child.byteorder == c'<' and not little_endian)):             # <<<<<<<<<<<<<<
 *             raise ValueError(u"Non-native byte order not supported")
 *             # One could encode it in the format string and have Cython
 */
      __pyx_t_7 = ((__pyx_v_child->byteorder == '<') != 0);
      if (__pyx_t_7) {
        __pyx_t_9 = ((!(__pyx_v_little_endian != 0)) != 0);
        __pyx_t_10 = __pyx_t_9;
      } else {
        __pyx_t_10 = __pyx_t_7;
      }
      __pyx_t_7 = __pyx_t_10;
    } else {
      __pyx_t_7 = __pyx_t_8;
    }
    if (__pyx_t_7) {

      /* "numpy.pxd":803
 *         if ((child.byteorder == c'>' and little_endian) or
 *             (child.byteorder == c'<' and not little_endian)):
 *             raise ValueError(u"Non-native byte order not supported")             # <<<<<<<<<<<<<<
 *             # One could encode it in the format string and have Cython
 *             # complain instead, BUT: < and > in format strings also imply
 */
      __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_k_tuple_38), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_Raise(__pyx_t_5, 0, 0, 0);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L8;
    }
    __pyx_L8:;

    /* "numpy.pxd":813
 * 
 *         # Output padding bytes
 *         while offset[0] < new_offset:             # <<<<<<<<<<<<<<
 *             f[0] = 120 # "x"; pad byte
 *             f += 1
 */
    while (1) {
      __pyx_t_5 = PyInt_FromLong((__pyx_v_offset[0])); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_t_5, __pyx_v_new_offset, Py_LT); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (!__pyx_t_7) break;

      /* "numpy.pxd":814
 *         # Output padding bytes
 *         while offset[0] < new_offset:
 *             f[0] = 120 # "x"; pad byte             # <<<<<<<<<<<<<<
 *             f += 1
 *             offset[0] += 1
 */
      (__pyx_v_f[0]) = 120;

      /* "numpy.pxd":815
 *         while offset[0] < new_offset:
 *             f[0] = 120 # "x"; pad byte
 *             f += 1             # <<<<<<<<<<<<<<
 *             offset[0] += 1
 * 
 */
      __pyx_v_f = (__pyx_v_f + 1);

      /* "numpy.pxd":816
 *             f[0] = 120 # "x"; pad byte
 *             f += 1
 *             offset[0] += 1             # <<<<<<<<<<<<<<
 * 
 *         offset[0] += child.itemsize
 */
      __pyx_t_11 = 0;
      (__pyx_v_offset[__pyx_t_11]) = ((__pyx_v_offset[__pyx_t_11]) + 1);
    }

    /* "numpy.pxd":818
 *             offset[0] += 1
 * 
 *         offset[0] += child.itemsize             # <<<<<<<<<<<<<<
 * 
 *         if not PyDataType_HASFIELDS(child):
 */
    __pyx_t_11 = 0;
    (__pyx_v_offset[__pyx_t_11]) = ((__pyx_v_offset[__pyx_t_11]) + __pyx_v_child->elsize);

    /* "numpy.pxd":820
 *         offset[0] += child.itemsize
 * 
 *         if not PyDataType_HASFIELDS(child):             # <<<<<<<<<<<<<<
 *             t = child.type_num
 *             if end - f < 5:
 */
    __pyx_t_7 = ((!(PyDataType_HASFIELDS(__pyx_v_child) != 0)) != 0);
    if (__pyx_t_7) {

      /* "numpy.pxd":821
 * 
 *         if not PyDataType_HASFIELDS(child):
 *             t = child.type_num             # <<<<<<<<<<<<<<
 *             if end - f < 5:
 *                 raise RuntimeError(u"Format string allocated too short.")
 */
      __pyx_t_3 = PyInt_FromLong(__pyx_v_child->type_num); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_XDECREF(__pyx_v_t);
      __pyx_v_t = __pyx_t_3;
      __pyx_t_3 = 0;

      /* "numpy.pxd":822
 *         if not PyDataType_HASFIELDS(child):
 *             t = child.type_num
 *             if end - f < 5:             # <<<<<<<<<<<<<<
 *                 raise RuntimeError(u"Format string allocated too short.")
 * 
 */
      __pyx_t_7 = (((__pyx_v_end - __pyx_v_f) < 5) != 0);
      if (__pyx_t_7) {

        /* "numpy.pxd":823
 *             t = child.type_num
 *             if end - f < 5:
 *                 raise RuntimeError(u"Format string allocated too short.")             # <<<<<<<<<<<<<<
 * 
 *             # Until ticket #99 is fixed, use integers to avoid warnings
 */
        __pyx_t_3 = PyObject_Call(__pyx_builtin_RuntimeError, ((PyObject *)__pyx_k_tuple_40), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        __Pyx_Raise(__pyx_t_3, 0, 0, 0);
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L12;
      }
      __pyx_L12:;

      /* "numpy.pxd":826
 * 
 *             # Until ticket #99 is fixed, use integers to avoid warnings
 *             if   t == NPY_BYTE:        f[0] =  98 #"b"             # <<<<<<<<<<<<<<
 *             elif t == NPY_UBYTE:       f[0] =  66 #"B"
 *             elif t == NPY_SHORT:       f[0] = 104 #"h"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_BYTE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 98;
        goto __pyx_L13;
      }

      /* "numpy.pxd":827
 *             # Until ticket #99 is fixed, use integers to avoid warnings
 *             if   t == NPY_BYTE:        f[0] =  98 #"b"
 *             elif t == NPY_UBYTE:       f[0] =  66 #"B"             # <<<<<<<<<<<<<<
 *             elif t == NPY_SHORT:       f[0] = 104 #"h"
 *             elif t == NPY_USHORT:      f[0] =  72 #"H"
 */
      __pyx_t_5 = PyInt_FromLong(NPY_UBYTE); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 66;
        goto __pyx_L13;
      }

      /* "numpy.pxd":828
 *             if   t == NPY_BYTE:        f[0] =  98 #"b"
 *             elif t == NPY_UBYTE:       f[0] =  66 #"B"
 *             elif t == NPY_SHORT:       f[0] = 104 #"h"             # <<<<<<<<<<<<<<
 *             elif t == NPY_USHORT:      f[0] =  72 #"H"
 *             elif t == NPY_INT:         f[0] = 105 #"i"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_SHORT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 104;
        goto __pyx_L13;
      }

      /* "numpy.pxd":829
 *             elif t == NPY_UBYTE:       f[0] =  66 #"B"
 *             elif t == NPY_SHORT:       f[0] = 104 #"h"
 *             elif t == NPY_USHORT:      f[0] =  72 #"H"             # <<<<<<<<<<<<<<
 *             elif t == NPY_INT:         f[0] = 105 #"i"
 *             elif t == NPY_UINT:        f[0] =  73 #"I"
 */
      __pyx_t_5 = PyInt_FromLong(NPY_USHORT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 72;
        goto __pyx_L13;
      }

      /* "numpy.pxd":830
 *             elif t == NPY_SHORT:       f[0] = 104 #"h"
 *             elif t == NPY_USHORT:      f[0] =  72 #"H"
 *             elif t == NPY_INT:         f[0] = 105 #"i"             # <<<<<<<<<<<<<<
 *             elif t == NPY_UINT:        f[0] =  73 #"I"
 *             elif t == NPY_LONG:        f[0] = 108 #"l"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_INT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 105;
        goto __pyx_L13;
      }

      /* "numpy.pxd":831
 *             elif t == NPY_USHORT:      f[0] =  72 #"H"
 *             elif t == NPY_INT:         f[0] = 105 #"i"
 *             elif t == NPY_UINT:        f[0] =  73 #"I"             # <<<<<<<<<<<<<<
 *             elif t == NPY_LONG:        f[0] = 108 #"l"
 *             elif t == NPY_ULONG:       f[0] = 76  #"L"
 */
      __pyx_t_5 = PyInt_FromLong(NPY_UINT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 73;
        goto __pyx_L13;
      }

      /* "numpy.pxd":832
 *             elif t == NPY_INT:         f[0] = 105 #"i"
 *             elif t == NPY_UINT:        f[0] =  73 #"I"
 *             elif t == NPY_LONG:        f[0] = 108 #"l"             # <<<<<<<<<<<<<<
 *             elif t == NPY_ULONG:       f[0] = 76  #"L"
 *             elif t == NPY_LONGLONG:    f[0] = 113 #"q"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_LONG); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 108;
        goto __pyx_L13;
      }

      /* "numpy.pxd":833
 *             elif t == NPY_UINT:        f[0] =  73 #"I"
 *             elif t == NPY_LONG:        f[0] = 108 #"l"
 *             elif t == NPY_ULONG:       f[0] = 76  #"L"             # <<<<<<<<<<<<<<
 *             elif t == NPY_LONGLONG:    f[0] = 113 #"q"
 *             elif t == NPY_ULONGLONG:   f[0] = 81  #"Q"
 */
      __pyx_t_5 = PyInt_FromLong(NPY_ULONG); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 76;
        goto __pyx_L13;
      }

      /* "numpy.pxd":834
 *             elif t == NPY_LONG:        f[0] = 108 #"l"
 *             elif t == NPY_ULONG:       f[0] = 76  #"L"
 *             elif t == NPY_LONGLONG:    f[0] = 113 #"q"             # <<<<<<<<<<<<<<
 *             elif t == NPY_ULONGLONG:   f[0] = 81  #"Q"
 *             elif t == NPY_FLOAT:       f[0] = 102 #"f"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_LONGLONG); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 113;
        goto __pyx_L13;
      }

      /* "numpy.pxd":835
 *             elif t == NPY_ULONG:       f[0] = 76  #"L"
 *             elif t == NPY_LONGLONG:    f[0] = 113 #"q"
 *             elif t == NPY_ULONGLONG:   f[0] = 81  #"Q"             # <<<<<<<<<<<<<<
 *             elif t == NPY_FLOAT:       f[0] = 102 #"f"
 *             elif t == NPY_DOUBLE:      f[0] = 100 #"d"
 */
      __pyx_t_5 = PyInt_FromLong(NPY_ULONGLONG); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 81;
        goto __pyx_L13;
      }

      /* "numpy.pxd":836
 *             elif t == NPY_LONGLONG:    f[0] = 113 #"q"
 *             elif t == NPY_ULONGLONG:   f[0] = 81  #"Q"
 *             elif t == NPY_FLOAT:       f[0] = 102 #"f"             # <<<<<<<<<<<<<<
 *             elif t == NPY_DOUBLE:      f[0] = 100 #"d"
 *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #"g"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_FLOAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 102;
        goto __pyx_L13;
      }

      /* "numpy.pxd":837
 *             elif t == NPY_ULONGLONG:   f[0] = 81  #"Q"
 *             elif t == NPY_FLOAT:       f[0] = 102 #"f"
 *             elif t == NPY_DOUBLE:      f[0] = 100 #"d"             # <<<<<<<<<<<<<<
 *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #"g"
 *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf
 */
      __pyx_t_5 = PyInt_FromLong(NPY_DOUBLE); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 100;
        goto __pyx_L13;
      }

      /* "numpy.pxd":838
 *             elif t == NPY_FLOAT:       f[0] = 102 #"f"
 *             elif t == NPY_DOUBLE:      f[0] = 100 #"d"
 *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #"g"             # <<<<<<<<<<<<<<
 *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf
 *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd
 */
      __pyx_t_3 = PyInt_FromLong(NPY_LONGDOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 103;
        goto __pyx_L13;
      }

      /* "numpy.pxd":839
 *             elif t == NPY_DOUBLE:      f[0] = 100 #"d"
 *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #"g"
 *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf             # <<<<<<<<<<<<<<
 *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd
 *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg
 */
      __pyx_t_5 = PyInt_FromLong(NPY_CFLOAT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 90;
        (__pyx_v_f[1]) = 102;
        __pyx_v_f = (__pyx_v_f + 1);
        goto __pyx_L13;
      }

      /* "numpy.pxd":840
 *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #"g"
 *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf
 *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd             # <<<<<<<<<<<<<<
 *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg
 *             elif t == NPY_OBJECT:      f[0] = 79 #"O"
 */
      __pyx_t_3 = PyInt_FromLong(NPY_CDOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 90;
        (__pyx_v_f[1]) = 100;
        __pyx_v_f = (__pyx_v_f + 1);
        goto __pyx_L13;
      }

      /* "numpy.pxd":841
 *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf
 *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd
 *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg             # <<<<<<<<<<<<<<
 *             elif t == NPY_OBJECT:      f[0] = 79 #"O"
 *             else:
 */
      __pyx_t_5 = PyInt_FromLong(NPY_CLONGDOUBLE); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_5, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 90;
        (__pyx_v_f[1]) = 103;
        __pyx_v_f = (__pyx_v_f + 1);
        goto __pyx_L13;
      }

      /* "numpy.pxd":842
 *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd
 *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg
 *             elif t == NPY_OBJECT:      f[0] = 79 #"O"             # <<<<<<<<<<<<<<
 *             else:
 *                 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 */
      __pyx_t_3 = PyInt_FromLong(NPY_OBJECT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_5); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_7 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_7) {
        (__pyx_v_f[0]) = 79;
        goto __pyx_L13;
      }
      /*else*/ {

        /* "numpy.pxd":844
 *             elif t == NPY_OBJECT:      f[0] = 79 #"O"
 *             else:
 *                 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)             # <<<<<<<<<<<<<<
 *             f += 1
 *         else:
 */
        __pyx_t_5 = PyNumber_Remainder(((PyObject *)__pyx_kp_u_35), __pyx_v_t); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(((PyObject *)__pyx_t_5));
        __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_5));
        __Pyx_GIVEREF(((PyObject *)__pyx_t_5));
        __pyx_t_5 = 0;
        __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
        __Pyx_Raise(__pyx_t_5, 0, 0, 0);
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_L13:;

      /* "numpy.pxd":845
 *             else:
 *                 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 *             f += 1             # <<<<<<<<<<<<<<
 *         else:
 *             # Cython ignores struct boundary information ("T{...}"),
 */
      __pyx_v_f = (__pyx_v_f + 1);
      goto __pyx_L11;
    }
    /*else*/ {

      /* "numpy.pxd":849
 *             # Cython ignores struct boundary information ("T{...}"),
 *             # so don't output it
 *             f = _util_dtypestring(child, f, end, offset)             # <<<<<<<<<<<<<<
 *     return f
 * 
 */
      __pyx_t_12 = __pyx_f_5numpy__util_dtypestring(__pyx_v_child, __pyx_v_f, __pyx_v_end, __pyx_v_offset); if (unlikely(__pyx_t_12 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_v_f = __pyx_t_12;
    }
    __pyx_L11:;
  }
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "numpy.pxd":850
 *             # so don't output it
 *             f = _util_dtypestring(child, f, end, offset)
 *     return f             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_r = __pyx_v_f;
  goto __pyx_L0;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_AddTraceback("numpy._util_dtypestring", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XDECREF((PyObject *)__pyx_v_child);
  __Pyx_XDECREF(__pyx_v_fields);
  __Pyx_XDECREF(__pyx_v_childname);
  __Pyx_XDECREF(__pyx_v_new_offset);
  __Pyx_XDECREF(__pyx_v_t);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "numpy.pxd":965
 * 
 * 
 * cdef inline void set_array_base(ndarray arr, object base):             # <<<<<<<<<<<<<<
 *      cdef PyObject* baseptr
 *      if base is None:
 */

static CYTHON_INLINE void __pyx_f_5numpy_set_array_base(PyArrayObject *__pyx_v_arr, PyObject *__pyx_v_base) {
  PyObject *__pyx_v_baseptr;
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  int __pyx_t_2;
  __Pyx_RefNannySetupContext("set_array_base", 0);

  /* "numpy.pxd":967
 * cdef inline void set_array_base(ndarray arr, object base):
 *      cdef PyObject* baseptr
 *      if base is None:             # <<<<<<<<<<<<<<
 *          baseptr = NULL
 *      else:
 */
  __pyx_t_1 = (__pyx_v_base == Py_None);
  __pyx_t_2 = (__pyx_t_1 != 0);
  if (__pyx_t_2) {

    /* "numpy.pxd":968
 *      cdef PyObject* baseptr
 *      if base is None:
 *          baseptr = NULL             # <<<<<<<<<<<<<<
 *      else:
 *          Py_INCREF(base) # important to do this before decref below!
 */
    __pyx_v_baseptr = NULL;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "numpy.pxd":970
 *          baseptr = NULL
 *      else:
 *          Py_INCREF(base) # important to do this before decref below!             # <<<<<<<<<<<<<<
 *          baseptr = <PyObject*>base
 *      Py_XDECREF(arr.base)
 */
    Py_INCREF(__pyx_v_base);

    /* "numpy.pxd":971
 *      else:
 *          Py_INCREF(base) # important to do this before decref below!
 *          baseptr = <PyObject*>base             # <<<<<<<<<<<<<<
 *      Py_XDECREF(arr.base)
 *      arr.base = baseptr
 */
    __pyx_v_baseptr = ((PyObject *)__pyx_v_base);
  }
  __pyx_L3:;

  /* "numpy.pxd":972
 *          Py_INCREF(base) # important to do this before decref below!
 *          baseptr = <PyObject*>base
 *      Py_XDECREF(arr.base)             # <<<<<<<<<<<<<<
 *      arr.base = baseptr
 * 
 */
  Py_XDECREF(__pyx_v_arr->base);

  /* "numpy.pxd":973
 *          baseptr = <PyObject*>base
 *      Py_XDECREF(arr.base)
 *      arr.base = baseptr             # <<<<<<<<<<<<<<
 * 
 * cdef inline object get_array_base(ndarray arr):
 */
  __pyx_v_arr->base = __pyx_v_baseptr;

  __Pyx_RefNannyFinishContext();
}

/* "numpy.pxd":975
 *      arr.base = baseptr
 * 
 * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<
 *     if arr.base is NULL:
 *         return None
 */

static CYTHON_INLINE PyObject *__pyx_f_5numpy_get_array_base(PyArrayObject *__pyx_v_arr) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  __Pyx_RefNannySetupContext("get_array_base", 0);

  /* "numpy.pxd":976
 * 
 * cdef inline object get_array_base(ndarray arr):
 *     if arr.base is NULL:             # <<<<<<<<<<<<<<
 *         return None
 *     else:
 */
  __pyx_t_1 = ((__pyx_v_arr->base == NULL) != 0);
  if (__pyx_t_1) {

    /* "numpy.pxd":977
 * cdef inline object get_array_base(ndarray arr):
 *     if arr.base is NULL:
 *         return None             # <<<<<<<<<<<<<<
 *     else:
 *         return <object>arr.base
 */
    __Pyx_XDECREF(__pyx_r);
    __Pyx_INCREF(Py_None);
    __pyx_r = Py_None;
    goto __pyx_L0;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "numpy.pxd":979
 *         return None
 *     else:
 *         return <object>arr.base             # <<<<<<<<<<<<<<
 */
    __Pyx_XDECREF(__pyx_r);
    __Pyx_INCREF(((PyObject *)__pyx_v_arr->base));
    __pyx_r = ((PyObject *)__pyx_v_arr->base);
    goto __pyx_L0;
  }
  __pyx_L3:;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyMethodDef __pyx_methods[] = {
  {0, 0, 0, 0}
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef __pyx_moduledef = {
  #if PY_VERSION_HEX < 0x03020000
    { PyObject_HEAD_INIT(NULL) NULL, 0, NULL },
  #else
    PyModuleDef_HEAD_INIT,
  #endif
    __Pyx_NAMESTR("distances"),
    __Pyx_DOCSTR(__pyx_k_41), /* m_doc */
    -1, /* m_size */
    __pyx_methods /* m_methods */,
    NULL, /* m_reload */
    NULL, /* m_traverse */
    NULL, /* m_clear */
    NULL /* m_free */
};
#endif

static __Pyx_StringTabEntry __pyx_string_tab[] = {
  {&__pyx_n_s_106, __pyx_k_106, sizeof(__pyx_k_106), 0, 0, 1, 1},
  {&__pyx_n_s_109, __pyx_k_109, sizeof(__pyx_k_109), 0, 0, 1, 1},
  {&__pyx_kp_u_110, __pyx_k_110, sizeof(__pyx_k_110), 0, 1, 0, 0},
  {&__pyx_kp_u_111, __pyx_k_111, sizeof(__pyx_k_111), 0, 1, 0, 0},
  {&__pyx_kp_u_112, __pyx_k_112, sizeof(__pyx_k_112), 0, 1, 0, 0},
  {&__pyx_kp_u_113, __pyx_k_113, sizeof(__pyx_k_113), 0, 1, 0, 0},
  {&__pyx_kp_u_114, __pyx_k_114, sizeof(__pyx_k_114), 0, 1, 0, 0},
  {&__pyx_kp_u_115, __pyx_k_115, sizeof(__pyx_k_115), 0, 1, 0, 0},
  {&__pyx_kp_u_116, __pyx_k_116, sizeof(__pyx_k_116), 0, 1, 0, 0},
  {&__pyx_kp_u_117, __pyx_k_117, sizeof(__pyx_k_117), 0, 1, 0, 0},
  {&__pyx_kp_u_118, __pyx_k_118, sizeof(__pyx_k_118), 0, 1, 0, 0},
  {&__pyx_kp_u_119, __pyx_k_119, sizeof(__pyx_k_119), 0, 1, 0, 0},
  {&__pyx_kp_s_12, __pyx_k_12, sizeof(__pyx_k_12), 0, 0, 1, 0},
  {&__pyx_kp_u_120, __pyx_k_120, sizeof(__pyx_k_120), 0, 1, 0, 0},
  {&__pyx_kp_u_121, __pyx_k_121, sizeof(__pyx_k_121), 0, 1, 0, 0},
  {&__pyx_kp_u_122, __pyx_k_122, sizeof(__pyx_k_122), 0, 1, 0, 0},
  {&__pyx_kp_u_123, __pyx_k_123, sizeof(__pyx_k_123), 0, 1, 0, 0},
  {&__pyx_kp_u_124, __pyx_k_124, sizeof(__pyx_k_124), 0, 1, 0, 0},
  {&__pyx_kp_u_125, __pyx_k_125, sizeof(__pyx_k_125), 0, 1, 0, 0},
  {&__pyx_kp_u_126, __pyx_k_126, sizeof(__pyx_k_126), 0, 1, 0, 0},
  {&__pyx_kp_u_127, __pyx_k_127, sizeof(__pyx_k_127), 0, 1, 0, 0},
  {&__pyx_kp_u_128, __pyx_k_128, sizeof(__pyx_k_128), 0, 1, 0, 0},
  {&__pyx_kp_u_129, __pyx_k_129, sizeof(__pyx_k_129), 0, 1, 0, 0},
  {&__pyx_kp_u_130, __pyx_k_130, sizeof(__pyx_k_130), 0, 1, 0, 0},
  {&__pyx_kp_u_131, __pyx_k_131, sizeof(__pyx_k_131), 0, 1, 0, 0},
  {&__pyx_kp_u_132, __pyx_k_132, sizeof(__pyx_k_132), 0, 1, 0, 0},
  {&__pyx_kp_u_133, __pyx_k_133, sizeof(__pyx_k_133), 0, 1, 0, 0},
  {&__pyx_kp_u_29, __pyx_k_29, sizeof(__pyx_k_29), 0, 1, 0, 0},
  {&__pyx_kp_u_31, __pyx_k_31, sizeof(__pyx_k_31), 0, 1, 0, 0},
  {&__pyx_kp_u_33, __pyx_k_33, sizeof(__pyx_k_33), 0, 1, 0, 0},
  {&__pyx_kp_u_35, __pyx_k_35, sizeof(__pyx_k_35), 0, 1, 0, 0},
  {&__pyx_kp_u_36, __pyx_k_36, sizeof(__pyx_k_36), 0, 1, 0, 0},
  {&__pyx_kp_u_39, __pyx_k_39, sizeof(__pyx_k_39), 0, 1, 0, 0},
  {&__pyx_kp_s_44, __pyx_k_44, sizeof(__pyx_k_44), 0, 0, 1, 0},
  {&__pyx_n_s_45, __pyx_k_45, sizeof(__pyx_k_45), 0, 0, 1, 1},
  {&__pyx_n_s_62, __pyx_k_62, sizeof(__pyx_k_62), 0, 0, 1, 1},
  {&__pyx_n_s_65, __pyx_k_65, sizeof(__pyx_k_65), 0, 0, 1, 1},
  {&__pyx_n_s_68, __pyx_k_68, sizeof(__pyx_k_68), 0, 0, 1, 1},
  {&__pyx_n_s_73, __pyx_k_73, sizeof(__pyx_k_73), 0, 0, 1, 1},
  {&__pyx_n_s_76, __pyx_k_76, sizeof(__pyx_k_76), 0, 0, 1, 1},
  {&__pyx_n_s_79, __pyx_k_79, sizeof(__pyx_k_79), 0, 0, 1, 1},
  {&__pyx_n_s_82, __pyx_k_82, sizeof(__pyx_k_82), 0, 0, 1, 1},
  {&__pyx_n_s_85, __pyx_k_85, sizeof(__pyx_k_85), 0, 0, 1, 1},
  {&__pyx_n_s_88, __pyx_k_88, sizeof(__pyx_k_88), 0, 0, 1, 1},
  {&__pyx_kp_s_9, __pyx_k_9, sizeof(__pyx_k_9), 0, 0, 1, 0},
  {&__pyx_n_s_91, __pyx_k_91, sizeof(__pyx_k_91), 0, 0, 1, 1},
  {&__pyx_n_s_96, __pyx_k_96, sizeof(__pyx_k_96), 0, 0, 1, 1},
  {&__pyx_n_s_99, __pyx_k_99, sizeof(__pyx_k_99), 0, 0, 1, 1},
  {&__pyx_n_s__C, __pyx_k__C, sizeof(__pyx_k__C), 0, 0, 1, 1},
  {&__pyx_n_s__C2, __pyx_k__C2, sizeof(__pyx_k__C2), 0, 0, 1, 1},
  {&__pyx_n_s__DM, __pyx_k__DM, sizeof(__pyx_k__DM), 0, 0, 1, 1},
  {&__pyx_n_s__Hit, __pyx_k__Hit, sizeof(__pyx_k__Hit), 0, 0, 1, 1},
  {&__pyx_n_s__N, __pyx_k__N, sizeof(__pyx_k__N), 0, 0, 1, 1},
  {&__pyx_n_s__N_ref, __pyx_k__N_ref, sizeof(__pyx_k__N_ref), 0, 0, 1, 1},
  {&__pyx_n_s__N_track, __pyx_k__N_track, sizeof(__pyx_k__N_track), 0, 0, 1, 1},
  {&__pyx_n_s__N_tracks, __pyx_k__N_tracks, sizeof(__pyx_k__N_tracks), 0, 0, 1, 1},
  {&__pyx_n_s__RuntimeError, __pyx_k__RuntimeError, sizeof(__pyx_k__RuntimeError), 0, 0, 1, 1},
  {&__pyx_n_s__ValueError, __pyx_k__ValueError, sizeof(__pyx_k__ValueError), 0, 0, 1, 1},
  {&__pyx_n_s____import__, __pyx_k____import__, sizeof(__pyx_k____import__), 0, 0, 1, 1},
  {&__pyx_n_s____main__, __pyx_k____main__, sizeof(__pyx_k____main__), 0, 0, 1, 1},
  {&__pyx_n_s____pyx_getbuffer, __pyx_k____pyx_getbuffer, sizeof(__pyx_k____pyx_getbuffer), 0, 0, 1, 1},
  {&__pyx_n_s____pyx_releasebuffer, __pyx_k____pyx_releasebuffer, sizeof(__pyx_k____pyx_releasebuffer), 0, 0, 1, 1},
  {&__pyx_n_s____test__, __pyx_k____test__, sizeof(__pyx_k____test__), 0, 0, 1, 1},
  {&__pyx_n_s__a, __pyx_k__a, sizeof(__pyx_k__a), 0, 0, 1, 1},
  {&__pyx_n_s__add_3vecs, __pyx_k__add_3vecs, sizeof(__pyx_k__add_3vecs), 0, 0, 1, 1},
  {&__pyx_n_s__all, __pyx_k__all, sizeof(__pyx_k__all), 0, 0, 1, 1},
  {&__pyx_n_s__alld, __pyx_k__alld, sizeof(__pyx_k__alld), 0, 0, 1, 1},
  {&__pyx_n_s__along, __pyx_k__along, sizeof(__pyx_k__along), 0, 0, 1, 1},
  {&__pyx_n_s__alpha, __pyx_k__alpha, sizeof(__pyx_k__alpha), 0, 0, 1, 1},
  {&__pyx_n_s__alphac, __pyx_k__alphac, sizeof(__pyx_k__alphac), 0, 0, 1, 1},
  {&__pyx_n_s__angle, __pyx_k__angle, sizeof(__pyx_k__angle), 0, 0, 1, 1},
  {&__pyx_n_s__append, __pyx_k__append, sizeof(__pyx_k__append), 0, 0, 1, 1},
  {&__pyx_n_s__argmin, __pyx_k__argmin, sizeof(__pyx_k__argmin), 0, 0, 1, 1},
  {&__pyx_n_s__array, __pyx_k__array, sizeof(__pyx_k__array), 0, 0, 1, 1},
  {&__pyx_n_s__asarray, __pyx_k__asarray, sizeof(__pyx_k__asarray), 0, 0, 1, 1},
  {&__pyx_n_s__ascontiguousarray, __pyx_k__ascontiguousarray, sizeof(__pyx_k__ascontiguousarray), 0, 0, 1, 1},
  {&__pyx_n_s__avg, __pyx_k__avg, sizeof(__pyx_k__avg), 0, 0, 1, 1},
  {&__pyx_n_s__b, __pyx_k__b, sizeof(__pyx_k__b), 0, 0, 1, 1},
  {&__pyx_n_s__beta, __pyx_k__beta, sizeof(__pyx_k__beta), 0, 0, 1, 1},
  {&__pyx_n_s__c, __pyx_k__c, sizeof(__pyx_k__c), 0, 0, 1, 1},
  {&__pyx_n_s__ca, __pyx_k__ca, sizeof(__pyx_k__ca), 0, 0, 1, 1},
  {&__pyx_n_s__cb, __pyx_k__cb, sizeof(__pyx_k__cb), 0, 0, 1, 1},
  {&__pyx_n_s__cc, __pyx_k__cc, sizeof(__pyx_k__cc), 0, 0, 1, 1},
  {&__pyx_n_s__cd_thr, __pyx_k__cd_thr, sizeof(__pyx_k__cd_thr), 0, 0, 1, 1},
  {&__pyx_n_s__ch, __pyx_k__ch, sizeof(__pyx_k__ch), 0, 0, 1, 1},
  {&__pyx_n_s__cit, __pyx_k__cit, sizeof(__pyx_k__cit), 0, 0, 1, 1},
  {&__pyx_n_s__cluster, __pyx_k__cluster, sizeof(__pyx_k__cluster), 0, 0, 1, 1},
  {&__pyx_n_s__copy, __pyx_k__copy, sizeof(__pyx_k__copy), 0, 0, 1, 1},
  {&__pyx_n_s__cost_nopar, __pyx_k__cost_nopar, sizeof(__pyx_k__cost_nopar), 0, 0, 1, 1},
  {&__pyx_n_s__cost_par, __pyx_k__cost_par, sizeof(__pyx_k__cost_par), 0, 0, 1, 1},
  {&__pyx_n_s__cp, __pyx_k__cp, sizeof(__pyx_k__cp), 0, 0, 1, 1},
  {&__pyx_n_s__cq, __pyx_k__cq, sizeof(__pyx_k__cq), 0, 0, 1, 1},
  {&__pyx_n_s__cr, __pyx_k__cr, sizeof(__pyx_k__cr), 0, 0, 1, 1},
  {&__pyx_n_s__csa, __pyx_k__csa, sizeof(__pyx_k__csa), 0, 0, 1, 1},
  {&__pyx_n_s__csb, __pyx_k__csb, sizeof(__pyx_k__csb), 0, 0, 1, 1},
  {&__pyx_n_s__ct, __pyx_k__ct, sizeof(__pyx_k__ct), 0, 0, 1, 1},
  {&__pyx_n_s__curr, __pyx_k__curr, sizeof(__pyx_k__curr), 0, 0, 1, 1},
  {&__pyx_n_s__current_index, __pyx_k__current_index, sizeof(__pyx_k__current_index), 0, 0, 1, 1},
  {&__pyx_n_s__currp, __pyx_k__currp, sizeof(__pyx_k__currp), 0, 0, 1, 1},
  {&__pyx_n_s__cut_plane, __pyx_k__cut_plane, sizeof(__pyx_k__cut_plane), 0, 0, 1, 1},
  {&__pyx_n_s__d, __pyx_k__d, sizeof(__pyx_k__d), 0, 0, 1, 1},
  {&__pyx_n_s__d_thr, __pyx_k__d_thr, sizeof(__pyx_k__d_thr), 0, 0, 1, 1},
  {&__pyx_n_s__delta, __pyx_k__delta, sizeof(__pyx_k__delta), 0, 0, 1, 1},
  {&__pyx_n_s__dim, __pyx_k__dim, sizeof(__pyx_k__dim), 0, 0, 1, 1},
  {&__pyx_n_s__dist, __pyx_k__dist, sizeof(__pyx_k__dist), 0, 0, 1, 1},
  {&__pyx_n_s__distance, __pyx_k__distance, sizeof(__pyx_k__distance), 0, 0, 1, 1},
  {&__pyx_n_s__distances_buffer, __pyx_k__distances_buffer, sizeof(__pyx_k__distances_buffer), 0, 0, 1, 1},
  {&__pyx_n_s__double, __pyx_k__double, sizeof(__pyx_k__double), 0, 0, 1, 1},
  {&__pyx_n_s__dtype, __pyx_k__dtype, sizeof(__pyx_k__dtype), 0, 0, 1, 1},
  {&__pyx_n_s__empty, __pyx_k__empty, sizeof(__pyx_k__empty), 0, 0, 1, 1},
  {&__pyx_n_s__end0, __pyx_k__end0, sizeof(__pyx_k__end0), 0, 0, 1, 1},
  {&__pyx_n_s__end1, __pyx_k__end1, sizeof(__pyx_k__end1), 0, 0, 1, 1},
  {&__pyx_n_s__flip, __pyx_k__flip, sizeof(__pyx_k__flip), 0, 0, 1, 1},
  {&__pyx_n_s__float32, __pyx_k__float32, sizeof(__pyx_k__float32), 0, 0, 1, 1},
  {&__pyx_n_s__fvec, __pyx_k__fvec, sizeof(__pyx_k__fvec), 0, 0, 1, 1},
  {&__pyx_n_s__fvec0, __pyx_k__fvec0, sizeof(__pyx_k__fvec0), 0, 0, 1, 1},
  {&__pyx_n_s__fvec1, __pyx_k__fvec1, sizeof(__pyx_k__fvec1), 0, 0, 1, 1},
  {&__pyx_n_s__fvec2, __pyx_k__fvec2, sizeof(__pyx_k__fvec2), 0, 0, 1, 1},
  {&__pyx_n_s__fvec3, __pyx_k__fvec3, sizeof(__pyx_k__fvec3), 0, 0, 1, 1},
  {&__pyx_n_s__fvec4, __pyx_k__fvec4, sizeof(__pyx_k__fvec4), 0, 0, 1, 1},
  {&__pyx_n_s__h, __pyx_k__h, sizeof(__pyx_k__h), 0, 0, 1, 1},
  {&__pyx_n_s__hid, __pyx_k__hid, sizeof(__pyx_k__hid), 0, 0, 1, 1},
  {&__pyx_n_s__hidden, __pyx_k__hidden, sizeof(__pyx_k__hidden), 0, 0, 1, 1},
  {&__pyx_n_s__hit, __pyx_k__hit, sizeof(__pyx_k__hit), 0, 0, 1, 1},
  {&__pyx_n_s__hitMp, __pyx_k__hitMp, sizeof(__pyx_k__hitMp), 0, 0, 1, 1},
  {&__pyx_n_s__hit_arr, __pyx_k__hit_arr, sizeof(__pyx_k__hit_arr), 0, 0, 1, 1},
  {&__pyx_n_s__hit_no, __pyx_k__hit_no, sizeof(__pyx_k__hit_no), 0, 0, 1, 1},
  {&__pyx_n_s__hit_ptr, __pyx_k__hit_ptr, sizeof(__pyx_k__hit_ptr), 0, 0, 1, 1},
  {&__pyx_n_s__hits, __pyx_k__hits, sizeof(__pyx_k__hits), 0, 0, 1, 1},
  {&__pyx_n_s__i, __pyx_k__i, sizeof(__pyx_k__i), 0, 0, 1, 1},
  {&__pyx_n_s__i_k, __pyx_k__i_k, sizeof(__pyx_k__i_k), 0, 0, 1, 1},
  {&__pyx_n_s__indices, __pyx_k__indices, sizeof(__pyx_k__indices), 0, 0, 1, 1},
  {&__pyx_n_s__inf, __pyx_k__inf, sizeof(__pyx_k__inf), 0, 0, 1, 1},
  {&__pyx_n_s__inner_3vecs, __pyx_k__inner_3vecs, sizeof(__pyx_k__inner_3vecs), 0, 0, 1, 1},
  {&__pyx_n_s__int64, __pyx_k__int64, sizeof(__pyx_k__int64), 0, 0, 1, 1},
  {&__pyx_n_s__intersects, __pyx_k__intersects, sizeof(__pyx_k__intersects), 0, 0, 1, 1},
  {&__pyx_n_s__it, __pyx_k__it, sizeof(__pyx_k__it), 0, 0, 1, 1},
  {&__pyx_n_s__itrange, __pyx_k__itrange, sizeof(__pyx_k__itrange), 0, 0, 1, 1},
  {&__pyx_n_s__j, __pyx_k__j, sizeof(__pyx_k__j), 0, 0, 1, 1},
  {&__pyx_n_s__k, __pyx_k__k, sizeof(__pyx_k__k), 0, 0, 1, 1},
  {&__pyx_n_s__keys, __pyx_k__keys, sizeof(__pyx_k__keys), 0, 0, 1, 1},
  {&__pyx_n_s__klen, __pyx_k__klen, sizeof(__pyx_k__klen), 0, 0, 1, 1},
  {&__pyx_n_s__krange, __pyx_k__krange, sizeof(__pyx_k__krange), 0, 0, 1, 1},
  {&__pyx_n_s__larch_3merge, __pyx_k__larch_3merge, sizeof(__pyx_k__larch_3merge), 0, 0, 1, 1},
  {&__pyx_n_s__larch_3split, __pyx_k__larch_3split, sizeof(__pyx_k__larch_3split), 0, 0, 1, 1},
  {&__pyx_n_s__ld, __pyx_k__ld, sizeof(__pyx_k__ld), 0, 0, 1, 1},
  {&__pyx_n_s__lee_angle_distance, __pyx_k__lee_angle_distance, sizeof(__pyx_k__lee_angle_distance), 0, 0, 1, 1},
  {&__pyx_n_s__lenC, __pyx_k__lenC, sizeof(__pyx_k__lenC), 0, 0, 1, 1},
  {&__pyx_n_s__length, __pyx_k__length, sizeof(__pyx_k__length), 0, 0, 1, 1},
  {&__pyx_n_s__lent, __pyx_k__lent, sizeof(__pyx_k__lent), 0, 0, 1, 1},
  {&__pyx_n_s__lentA, __pyx_k__lentA, sizeof(__pyx_k__lentA), 0, 0, 1, 1},
  {&__pyx_n_s__lentB, __pyx_k__lentB, sizeof(__pyx_k__lentB), 0, 0, 1, 1},
  {&__pyx_n_s__lhp, __pyx_k__lhp, sizeof(__pyx_k__lhp), 0, 0, 1, 1},
  {&__pyx_n_s__longest_track_len, __pyx_k__longest_track_len, sizeof(__pyx_k__longest_track_len), 0, 0, 1, 1},
  {&__pyx_n_s__longest_track_lenA, __pyx_k__longest_track_lenA, sizeof(__pyx_k__longest_track_lenA), 0, 0, 1, 1},
  {&__pyx_n_s__longest_track_lenB, __pyx_k__longest_track_lenB, sizeof(__pyx_k__longest_track_lenB), 0, 0, 1, 1},
  {&__pyx_n_s__lrq, __pyx_k__lrq, sizeof(__pyx_k__lrq), 0, 0, 1, 1},
  {&__pyx_n_s__m_d, __pyx_k__m_d, sizeof(__pyx_k__m_d), 0, 0, 1, 1},
  {&__pyx_n_s__m_k, __pyx_k__m_k, sizeof(__pyx_k__m_k), 0, 0, 1, 1},
  {&__pyx_n_s__mam_distances, __pyx_k__mam_distances, sizeof(__pyx_k__mam_distances), 0, 0, 1, 1},
  {&__pyx_n_s__max, __pyx_k__max, sizeof(__pyx_k__max), 0, 0, 1, 1},
  {&__pyx_n_s__max_hit_len, __pyx_k__max_hit_len, sizeof(__pyx_k__max_hit_len), 0, 0, 1, 1},
  {&__pyx_n_s__mean_t1t2, __pyx_k__mean_t1t2, sizeof(__pyx_k__mean_t1t2), 0, 0, 1, 1},
  {&__pyx_n_s__mean_t2t1, __pyx_k__mean_t2t1, sizeof(__pyx_k__mean_t2t1), 0, 0, 1, 1},
  {&__pyx_n_s__metric, __pyx_k__metric, sizeof(__pyx_k__metric), 0, 0, 1, 1},
  {&__pyx_n_s__metric_type, __pyx_k__metric_type, sizeof(__pyx_k__metric_type), 0, 0, 1, 1},
  {&__pyx_n_s__mid_index, __pyx_k__mid_index, sizeof(__pyx_k__mid_index), 0, 0, 1, 1},
  {&__pyx_n_s__min, __pyx_k__min, sizeof(__pyx_k__min), 0, 0, 1, 1},
  {&__pyx_n_s__min_buffer, __pyx_k__min_buffer, sizeof(__pyx_k__min_buffer), 0, 0, 1, 1},
  {&__pyx_n_s__min_min_t1t2, __pyx_k__min_min_t1t2, sizeof(__pyx_k__min_min_t1t2), 0, 0, 1, 1},
  {&__pyx_n_s__min_min_t2t1, __pyx_k__min_min_t2t1, sizeof(__pyx_k__min_min_t2t1), 0, 0, 1, 1},
  {&__pyx_n_s__min_t1t2, __pyx_k__min_t1t2, sizeof(__pyx_k__min_t1t2), 0, 0, 1, 1},
  {&__pyx_n_s__min_t2t1, __pyx_k__min_t2t1, sizeof(__pyx_k__min_t2t1), 0, 0, 1, 1},
  {&__pyx_n_s__mn, __pyx_k__mn, sizeof(__pyx_k__mn), 0, 0, 1, 1},
  {&__pyx_n_s__mul_3vec, __pyx_k__mul_3vec, sizeof(__pyx_k__mul_3vec), 0, 0, 1, 1},
  {&__pyx_n_s__mul_3vecs, __pyx_k__mul_3vecs, sizeof(__pyx_k__mul_3vecs), 0, 0, 1, 1},
  {&__pyx_n_s__n_hits, __pyx_k__n_hits, sizeof(__pyx_k__n_hits), 0, 0, 1, 1},
  {&__pyx_n_s__next_ref_p, __pyx_k__next_ref_p, sizeof(__pyx_k__next_ref_p), 0, 0, 1, 1},
  {&__pyx_n_s__next_trk_p, __pyx_k__next_trk_p, sizeof(__pyx_k__next_trk_p), 0, 0, 1, 1},
  {&__pyx_n_s__norm_3vec, __pyx_k__norm_3vec, sizeof(__pyx_k__norm_3vec), 0, 0, 1, 1},
  {&__pyx_n_s__normal, __pyx_k__normal, sizeof(__pyx_k__normal), 0, 0, 1, 1},
  {&__pyx_n_s__normalized_3vec, __pyx_k__normalized_3vec, sizeof(__pyx_k__normalized_3vec), 0, 0, 1, 1},
  {&__pyx_n_s__np, __pyx_k__np, sizeof(__pyx_k__np), 0, 0, 1, 1},
  {&__pyx_n_s__numpy, __pyx_k__numpy, sizeof(__pyx_k__numpy), 0, 0, 1, 1},
  {&__pyx_n_s__object, __pyx_k__object, sizeof(__pyx_k__object), 0, 0, 1, 1},
  {&__pyx_n_s__one_hit, __pyx_k__one_hit, sizeof(__pyx_k__one_hit), 0, 0, 1, 1},
  {&__pyx_n_s__p, __pyx_k__p, sizeof(__pyx_k__p), 0, 0, 1, 1},
  {&__pyx_n_s__pMq, __pyx_k__pMq, sizeof(__pyx_k__pMq), 0, 0, 1, 1},
  {&__pyx_n_s__p_no, __pyx_k__p_no, sizeof(__pyx_k__p_no), 0, 0, 1, 1},
  {&__pyx_n_s__point, __pyx_k__point, sizeof(__pyx_k__point), 0, 0, 1, 1},
  {&__pyx_n_s__points, __pyx_k__points, sizeof(__pyx_k__points), 0, 0, 1, 1},
  {&__pyx_n_s__ptr, __pyx_k__ptr, sizeof(__pyx_k__ptr), 0, 0, 1, 1},
  {&__pyx_n_s__q, __pyx_k__q, sizeof(__pyx_k__q), 0, 0, 1, 1},
  {&__pyx_n_s__qMp, __pyx_k__qMp, sizeof(__pyx_k__qMp), 0, 0, 1, 1},
  {&__pyx_n_s__q_no, __pyx_k__q_no, sizeof(__pyx_k__q_no), 0, 0, 1, 1},
  {&__pyx_n_s__r, __pyx_k__r, sizeof(__pyx_k__r), 0, 0, 1, 1},
  {&__pyx_n_s__rMp, __pyx_k__rMp, sizeof(__pyx_k__rMp), 0, 0, 1, 1},
  {&__pyx_n_s__rMq, __pyx_k__rMq, sizeof(__pyx_k__rMq), 0, 0, 1, 1},
  {&__pyx_n_s__range, __pyx_k__range, sizeof(__pyx_k__range), 0, 0, 1, 1},
  {&__pyx_n_s__rcd, __pyx_k__rcd, sizeof(__pyx_k__rcd), 0, 0, 1, 1},
  {&__pyx_n_s__ref, __pyx_k__ref, sizeof(__pyx_k__ref), 0, 0, 1, 1},
  {&__pyx_n_s__ref32, __pyx_k__ref32, sizeof(__pyx_k__ref32), 0, 0, 1, 1},
  {&__pyx_n_s__rep3, __pyx_k__rep3, sizeof(__pyx_k__rep3), 0, 0, 1, 1},
  {&__pyx_n_s__rlen, __pyx_k__rlen, sizeof(__pyx_k__rlen), 0, 0, 1, 1},
  {&__pyx_n_s__roi, __pyx_k__roi, sizeof(__pyx_k__roi), 0, 0, 1, 1},
  {&__pyx_n_s__rows, __pyx_k__rows, sizeof(__pyx_k__rows), 0, 0, 1, 1},
  {&__pyx_n_s__sa, __pyx_k__sa, sizeof(__pyx_k__sa), 0, 0, 1, 1},
  {&__pyx_n_s__sb, __pyx_k__sb, sizeof(__pyx_k__sb), 0, 0, 1, 1},
  {&__pyx_n_s__shape, __pyx_k__shape, sizeof(__pyx_k__shape), 0, 0, 1, 1},
  {&__pyx_n_s__si, __pyx_k__si, sizeof(__pyx_k__si), 0, 0, 1, 1},
  {&__pyx_n_s__sq_dist_thr, __pyx_k__sq_dist_thr, sizeof(__pyx_k__sq_dist_thr), 0, 0, 1, 1},
  {&__pyx_n_s__squeeze, __pyx_k__squeeze, sizeof(__pyx_k__squeeze), 0, 0, 1, 1},
  {&__pyx_n_s__start0, __pyx_k__start0, sizeof(__pyx_k__start0), 0, 0, 1, 1},
  {&__pyx_n_s__start1, __pyx_k__start1, sizeof(__pyx_k__start1), 0, 0, 1, 1},
  {&__pyx_n_s__start_index, __pyx_k__start_index, sizeof(__pyx_k__start_index), 0, 0, 1, 1},
  {&__pyx_n_s__sub_3vecs, __pyx_k__sub_3vecs, sizeof(__pyx_k__sub_3vecs), 0, 0, 1, 1},
  {&__pyx_n_s__sum_track2others, __pyx_k__sum_track2others, sizeof(__pyx_k__sum_track2others), 0, 0, 1, 1},
  {&__pyx_n_s__t, __pyx_k__t, sizeof(__pyx_k__t), 0, 0, 1, 1},
  {&__pyx_n_s__t1, __pyx_k__t1, sizeof(__pyx_k__t1), 0, 0, 1, 1},
  {&__pyx_n_s__t1_len, __pyx_k__t1_len, sizeof(__pyx_k__t1_len), 0, 0, 1, 1},
  {&__pyx_n_s__t1_pi, __pyx_k__t1_pi, sizeof(__pyx_k__t1_pi), 0, 0, 1, 1},
  {&__pyx_n_s__t1_ptr, __pyx_k__t1_ptr, sizeof(__pyx_k__t1_ptr), 0, 0, 1, 1},
  {&__pyx_n_s__t2, __pyx_k__t2, sizeof(__pyx_k__t2), 0, 0, 1, 1},
  {&__pyx_n_s__t2_len, __pyx_k__t2_len, sizeof(__pyx_k__t2_len), 0, 0, 1, 1},
  {&__pyx_n_s__t2_pi, __pyx_k__t2_pi, sizeof(__pyx_k__t2_pi), 0, 0, 1, 1},
  {&__pyx_n_s__t2_ptr, __pyx_k__t2_ptr, sizeof(__pyx_k__t2_ptr), 0, 0, 1, 1},
  {&__pyx_n_s__t_len, __pyx_k__t_len, sizeof(__pyx_k__t_len), 0, 0, 1, 1},
  {&__pyx_n_s__t_no, __pyx_k__t_no, sizeof(__pyx_k__t_no), 0, 0, 1, 1},
  {&__pyx_n_s__this_ref_p, __pyx_k__this_ref_p, sizeof(__pyx_k__this_ref_p), 0, 0, 1, 1},
  {&__pyx_n_s__this_trk_p, __pyx_k__this_trk_p, sizeof(__pyx_k__this_trk_p), 0, 0, 1, 1},
  {&__pyx_n_s__thr, __pyx_k__thr, sizeof(__pyx_k__thr), 0, 0, 1, 1},
  {&__pyx_n_s__time, __pyx_k__time, sizeof(__pyx_k__time), 0, 0, 1, 1},
  {&__pyx_n_s__tlen, __pyx_k__tlen, sizeof(__pyx_k__tlen), 0, 0, 1, 1},
  {&__pyx_n_s__tmp, __pyx_k__tmp, sizeof(__pyx_k__tmp), 0, 0, 1, 1},
  {&__pyx_n_s__track, __pyx_k__track, sizeof(__pyx_k__track), 0, 0, 1, 1},
  {&__pyx_n_s__track1, __pyx_k__track1, sizeof(__pyx_k__track1), 0, 0, 1, 1},
  {&__pyx_n_s__track2, __pyx_k__track2, sizeof(__pyx_k__track2), 0, 0, 1, 1},
  {&__pyx_n_s__track2others, __pyx_k__track2others, sizeof(__pyx_k__track2others), 0, 0, 1, 1},
  {&__pyx_n_s__track_dist_3pts, __pyx_k__track_dist_3pts, sizeof(__pyx_k__track_dist_3pts), 0, 0, 1, 1},
  {&__pyx_n_s__track_len, __pyx_k__track_len, sizeof(__pyx_k__track_len), 0, 0, 1, 1},
  {&__pyx_n_s__track_lengths, __pyx_k__track_lengths, sizeof(__pyx_k__track_lengths), 0, 0, 1, 1},
  {&__pyx_n_s__tracka, __pyx_k__tracka, sizeof(__pyx_k__tracka), 0, 0, 1, 1},
  {&__pyx_n_s__trackb, __pyx_k__trackb, sizeof(__pyx_k__trackb), 0, 0, 1, 1},
  {&__pyx_n_s__tracks, __pyx_k__tracks, sizeof(__pyx_k__tracks), 0, 0, 1, 1},
  {&__pyx_n_s__tracks32, __pyx_k__tracks32, sizeof(__pyx_k__tracks32), 0, 0, 1, 1},
  {&__pyx_n_s__tracksA, __pyx_k__tracksA, sizeof(__pyx_k__tracksA), 0, 0, 1, 1},
  {&__pyx_n_s__tracksA32, __pyx_k__tracksA32, sizeof(__pyx_k__tracksA32), 0, 0, 1, 1},
  {&__pyx_n_s__tracksB, __pyx_k__tracksB, sizeof(__pyx_k__tracksB), 0, 0, 1, 1},
  {&__pyx_n_s__tracksB32, __pyx_k__tracksB32, sizeof(__pyx_k__tracksB32), 0, 0, 1, 1},
  {&__pyx_n_s__ts, __pyx_k__ts, sizeof(__pyx_k__ts), 0, 0, 1, 1},
  {&__pyx_n_s__uint64, __pyx_k__uint64, sizeof(__pyx_k__uint64), 0, 0, 1, 1},
  {&__pyx_n_s__vec, __pyx_k__vec, sizeof(__pyx_k__vec), 0, 0, 1, 1},
  {&__pyx_n_s__vec0, __pyx_k__vec0, sizeof(__pyx_k__vec0), 0, 0, 1, 1},
  {&__pyx_n_s__vec1, __pyx_k__vec1, sizeof(__pyx_k__vec1), 0, 0, 1, 1},
  {&__pyx_n_s__vec2, __pyx_k__vec2, sizeof(__pyx_k__vec2), 0, 0, 1, 1},
  {&__pyx_n_s__vec_in, __pyx_k__vec_in, sizeof(__pyx_k__vec_in), 0, 0, 1, 1},
  {&__pyx_n_s__vec_out, __pyx_k__vec_out, sizeof(__pyx_k__vec_out), 0, 0, 1, 1},
  {&__pyx_n_s__xyz, __pyx_k__xyz, sizeof(__pyx_k__xyz), 0, 0, 1, 1},
  {&__pyx_n_s__xyz1, __pyx_k__xyz1, sizeof(__pyx_k__xyz1), 0, 0, 1, 1},
  {&__pyx_n_s__xyz2, __pyx_k__xyz2, sizeof(__pyx_k__xyz2), 0, 0, 1, 1},
  {&__pyx_n_s__zeros, __pyx_k__zeros, sizeof(__pyx_k__zeros), 0, 0, 1, 1},
  {0, 0, 0, 0, 0, 0, 0}
};
static int __Pyx_InitCachedBuiltins(void) {
  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s__range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 118; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s__ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_builtin_object = __Pyx_GetBuiltinName(__pyx_n_s__object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 406; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_builtin_RuntimeError = __Pyx_GetBuiltinName(__pyx_n_s__RuntimeError); if (!__pyx_builtin_RuntimeError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  return 0;
  __pyx_L1_error:;
  return -1;
}

static int __Pyx_InitCachedConstants(void) {
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0);

  /* "dipy/tracking/distances.pyx":64
 *     '''
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_in = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cnormalized_3vec(<float *>vec_in.data, <float*>vec_out.data)
 *     return vec_out
 */
  __pyx_k_tuple_1 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_k_tuple_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_1);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_1));

  /* "dipy/tracking/distances.pyx":139
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     csub_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_k_tuple_2 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_k_tuple_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_2);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_2));

  /* "dipy/tracking/distances.pyx":153
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cadd_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_k_tuple_3 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_k_tuple_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_3);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_3));

  /* "dipy/tracking/distances.pyx":166
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cmul_3vecs(<float *>fvec1.data, <float*>fvec2.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_k_tuple_4 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_k_tuple_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_4);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_4));

  /* "dipy/tracking/distances.pyx":177
 * def mul_3vec(a, vec):
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)             # <<<<<<<<<<<<<<
 *     cmul_3vec(a,<float *>fvec.data, <float *>vec_out.data)
 *     return vec_out
 */
  __pyx_k_tuple_5 = PyTuple_Pack(1, __pyx_int_3); if (unlikely(!__pyx_k_tuple_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_5);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_5));

  /* "dipy/tracking/distances.pyx":326
 *                             # hit data into array
 *                             if hit_no >= max_hit_len:
 *                                 one_hit = np.empty((5,), dtype=f32_dt)             # <<<<<<<<<<<<<<
 *                                 hits.append(one_hit)
 *                             else:
 */
  __pyx_k_tuple_6 = PyTuple_Pack(1, __pyx_int_5); if (unlikely(!__pyx_k_tuple_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_6);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_6));
  __pyx_k_tuple_7 = PyTuple_Pack(1, ((PyObject *)__pyx_k_tuple_6)); if (unlikely(!__pyx_k_tuple_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 326; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_7);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_7));

  /* "dipy/tracking/distances.pyx":346
 *         Hit.append(hit_arr)
 *         #Div.append(divs[1:])
 *     return Hit[1:]             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_k_slice_8 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_k_slice_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 346; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_slice_8);
  __Pyx_GIVEREF(__pyx_k_slice_8);

  /* "dipy/tracking/distances.pyx":400
 *         metric_type = 2
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')             # <<<<<<<<<<<<<<
 *     # preprocess tracks
 *     cdef:
 */
  __pyx_k_tuple_10 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_s_9)); if (unlikely(!__pyx_k_tuple_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 400; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_10);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_10));

  /* "dipy/tracking/distances.pyx":490
 *         metric_type = 2
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')             # <<<<<<<<<<<<<<
 *     # preprocess tracks
 *     cdef:
 */
  __pyx_k_tuple_11 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_s_9)); if (unlikely(!__pyx_k_tuple_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_11);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_11));

  /* "dipy/tracking/distances.pyx":777
 *         return np.max((mean_t2t1,mean_t1t2))
 *     else :
 *         ValueError('Wrong argument for metric')             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_k_tuple_13 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_s_12)); if (unlikely(!__pyx_k_tuple_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 777; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_13);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_13));

  /* "dipy/tracking/distances.pyx":1709
 *     #Network C
 *     C={0:{'indices':[0],'hidden':tracks[0].copy(),'N':1}}
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *     #for (it,t) in enumerate(tracks[1:]):
 */
  __pyx_k_tuple_16 = PyTuple_Pack(2, __pyx_int_3, __pyx_int_3); if (unlikely(!__pyx_k_tuple_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_16);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_16));
  __pyx_k_tuple_17 = PyTuple_Pack(1, ((PyObject *)__pyx_k_tuple_16)); if (unlikely(!__pyx_k_tuple_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1709; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_17);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_17));

  /* "dipy/tracking/distances.pyx":1852
 *     else:
 *         C={0:{'indices':[indices[0]],'rep3':tracks[indices[0]].copy(),'N':1}}
 *         itrange=indices[1:]             # <<<<<<<<<<<<<<
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)
 */
  __pyx_k_slice_19 = PySlice_New(__pyx_int_1, Py_None, Py_None); if (unlikely(!__pyx_k_slice_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1852; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_slice_19);
  __Pyx_GIVEREF(__pyx_k_slice_19);

  /* "dipy/tracking/distances.pyx":1854
 *         itrange=indices[1:]
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 *     for it in itrange:
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 */
  __pyx_k_tuple_20 = PyTuple_Pack(2, __pyx_int_3, __pyx_int_3); if (unlikely(!__pyx_k_tuple_20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_20);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_20));
  __pyx_k_tuple_21 = PyTuple_Pack(1, ((PyObject *)__pyx_k_tuple_20)); if (unlikely(!__pyx_k_tuple_21)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1854; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_21);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_21));

  /* "dipy/tracking/distances.pyx":1910
 *     '''
 * 
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] h=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] ch=np.zeros((3,3),dtype=np.float32)
 *     cdef int lenC,k,c
 */
  __pyx_k_tuple_23 = PyTuple_Pack(2, __pyx_int_3, __pyx_int_3); if (unlikely(!__pyx_k_tuple_23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_23);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_23));
  __pyx_k_tuple_24 = PyTuple_Pack(1, ((PyObject *)__pyx_k_tuple_23)); if (unlikely(!__pyx_k_tuple_24)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1910; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_24);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_24));

  /* "dipy/tracking/distances.pyx":1911
 * 
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] h=np.zeros((3,3),dtype=np.float32)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=2] ch=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 *     cdef int lenC,k,c
 *     cdef float d[2]
 */
  __pyx_k_tuple_25 = PyTuple_Pack(2, __pyx_int_3, __pyx_int_3); if (unlikely(!__pyx_k_tuple_25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_25);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_25));
  __pyx_k_tuple_26 = PyTuple_Pack(1, ((PyObject *)__pyx_k_tuple_25)); if (unlikely(!__pyx_k_tuple_26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1911; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_26);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_26));

  /* "dipy/tracking/distances.pyx":1915
 *     cdef float d[2]
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *     lenC=len(C)
 */
  __pyx_k_tuple_27 = PyTuple_Pack(2, __pyx_int_3, __pyx_int_3); if (unlikely(!__pyx_k_tuple_27)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_27);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_27));
  __pyx_k_tuple_28 = PyTuple_Pack(1, ((PyObject *)__pyx_k_tuple_27)); if (unlikely(!__pyx_k_tuple_28)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1915; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_28);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_28));

  /* "numpy.pxd":215
 *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
 *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
 *                 raise ValueError(u"ndarray is not C contiguous")             # <<<<<<<<<<<<<<
 * 
 *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
 */
  __pyx_k_tuple_30 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_u_29)); if (unlikely(!__pyx_k_tuple_30)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_30);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_30));

  /* "numpy.pxd":219
 *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
 *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
 *                 raise ValueError(u"ndarray is not Fortran contiguous")             # <<<<<<<<<<<<<<
 * 
 *             info.buf = PyArray_DATA(self)
 */
  __pyx_k_tuple_32 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_u_31)); if (unlikely(!__pyx_k_tuple_32)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_32);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_32));

  /* "numpy.pxd":257
 *                 if ((descr.byteorder == c'>' and little_endian) or
 *                     (descr.byteorder == c'<' and not little_endian)):
 *                     raise ValueError(u"Non-native byte order not supported")             # <<<<<<<<<<<<<<
 *                 if   t == NPY_BYTE:        f = "b"
 *                 elif t == NPY_UBYTE:       f = "B"
 */
  __pyx_k_tuple_34 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_u_33)); if (unlikely(!__pyx_k_tuple_34)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_34);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_34));

  /* "numpy.pxd":799
 * 
 *         if (end - f) - (new_offset - offset[0]) < 15:
 *             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")             # <<<<<<<<<<<<<<
 * 
 *         if ((child.byteorder == c'>' and little_endian) or
 */
  __pyx_k_tuple_37 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_u_36)); if (unlikely(!__pyx_k_tuple_37)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_37);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_37));

  /* "numpy.pxd":803
 *         if ((child.byteorder == c'>' and little_endian) or
 *             (child.byteorder == c'<' and not little_endian)):
 *             raise ValueError(u"Non-native byte order not supported")             # <<<<<<<<<<<<<<
 *             # One could encode it in the format string and have Cython
 *             # complain instead, BUT: < and > in format strings also imply
 */
  __pyx_k_tuple_38 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_u_33)); if (unlikely(!__pyx_k_tuple_38)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_38);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_38));

  /* "numpy.pxd":823
 *             t = child.type_num
 *             if end - f < 5:
 *                 raise RuntimeError(u"Format string allocated too short.")             # <<<<<<<<<<<<<<
 * 
 *             # Until ticket #99 is fixed, use integers to avoid warnings
 */
  __pyx_k_tuple_40 = PyTuple_Pack(1, ((PyObject *)__pyx_kp_u_39)); if (unlikely(!__pyx_k_tuple_40)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_40);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_40));

  /* "dipy/tracking/distances.pyx":50
 * 
 * 
 * def normalized_3vec(vec):             # <<<<<<<<<<<<<<
 *     ''' Return normalized 3D vector
 * 
 */
  __pyx_k_tuple_42 = PyTuple_Pack(3, ((PyObject *)__pyx_n_s__vec), ((PyObject *)__pyx_n_s__vec_in), ((PyObject *)__pyx_n_s__vec_out)); if (unlikely(!__pyx_k_tuple_42)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_42);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_42));
  __pyx_k_codeobj_43 = (PyObject*)__Pyx_PyCode_New(1, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_42, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__normalized_3vec, 50, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_43)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":69
 * 
 * 
 * def norm_3vec(vec):             # <<<<<<<<<<<<<<
 *     ''' Euclidean (L2) norm of length 3 vector
 * 
 */
  __pyx_k_tuple_46 = PyTuple_Pack(2, ((PyObject *)__pyx_n_s__vec), ((PyObject *)__pyx_n_s__vec_in)); if (unlikely(!__pyx_k_tuple_46)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_46);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_46));
  __pyx_k_codeobj_47 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_46, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__norm_3vec, 69, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_47)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":122
 * 
 * 
 * def inner_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_k_tuple_48 = PyTuple_Pack(4, ((PyObject *)__pyx_n_s__vec1), ((PyObject *)__pyx_n_s__vec2), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2)); if (unlikely(!__pyx_k_tuple_48)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_48);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_48));
  __pyx_k_codeobj_49 = (PyObject*)__Pyx_PyCode_New(2, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_48, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__inner_3vecs, 122, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_49)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":136
 * 
 * 
 * def sub_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_k_tuple_50 = PyTuple_Pack(5, ((PyObject *)__pyx_n_s__vec1), ((PyObject *)__pyx_n_s__vec2), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s__vec_out)); if (unlikely(!__pyx_k_tuple_50)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_50);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_50));
  __pyx_k_codeobj_51 = (PyObject*)__Pyx_PyCode_New(2, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_50, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__sub_3vecs, 136, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_51)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":150
 * 
 * 
 * def add_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_k_tuple_52 = PyTuple_Pack(5, ((PyObject *)__pyx_n_s__vec1), ((PyObject *)__pyx_n_s__vec2), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s__vec_out)); if (unlikely(!__pyx_k_tuple_52)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_52);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_52));
  __pyx_k_codeobj_53 = (PyObject*)__Pyx_PyCode_New(2, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_52, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__add_3vecs, 150, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_53)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":163
 *         vec_out[i] = vec1[i]+vec2[i]
 * 
 * def mul_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_k_tuple_54 = PyTuple_Pack(5, ((PyObject *)__pyx_n_s__vec1), ((PyObject *)__pyx_n_s__vec2), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s__vec_out)); if (unlikely(!__pyx_k_tuple_54)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_54);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_54));
  __pyx_k_codeobj_55 = (PyObject*)__Pyx_PyCode_New(2, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_54, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__mul_3vecs, 163, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_55)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":175
 *         vec_out[i] = vec1[i]*vec2[i]
 * 
 * def mul_3vec(a, vec):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 */
  __pyx_k_tuple_56 = PyTuple_Pack(4, ((PyObject *)__pyx_n_s__a), ((PyObject *)__pyx_n_s__vec), ((PyObject *)__pyx_n_s__fvec), ((PyObject *)__pyx_n_s__vec_out)); if (unlikely(!__pyx_k_tuple_56)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_56);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_56));
  __pyx_k_codeobj_57 = (PyObject*)__Pyx_PyCode_New(2, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_56, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__mul_3vec, 175, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_57)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":191
 * 
 * 
 * def cut_plane(tracks,ref):             # <<<<<<<<<<<<<<
 *     ''' Extract divergence vectors and points of intersection
 *     between planes normal to the reference fiber and other tracks
 */
  __pyx_k_tuple_58 = PyTuple_Pack(39, ((PyObject *)__pyx_n_s__tracks), ((PyObject *)__pyx_n_s__ref), ((PyObject *)__pyx_n_s__n_hits), ((PyObject *)__pyx_n_s__hit_no), ((PyObject *)__pyx_n_s__max_hit_len), ((PyObject *)__pyx_n_s__alpha), ((PyObject *)__pyx_n_s__beta), ((PyObject *)__pyx_n_s__lrq), ((PyObject *)__pyx_n_s__rcd), ((PyObject *)__pyx_n_s__lhp), ((PyObject *)__pyx_n_s__ld), ((PyObject *)__pyx_n_s__ref32), ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__hits), ((PyObject *)__pyx_n_s__one_hit), ((PyObject *)__pyx_n_s__hit_ptr), ((PyObject *)__pyx_n_s__hit_arr), ((PyObject *)__pyx_n_s__Hit), ((PyObject *)__pyx_n_s__N_tracks), ((PyObject *)__pyx_n_s__track_lengths), ((PyObject *)__pyx_n_s__t_no), ((PyObject *)__pyx_n_s__N_track), ((PyObject *)__pyx_n_s__tracks32), ((PyObject *)__pyx_n_s__N_ref), ((PyObject *)__pyx_n_s__p_no), ((PyObject *)__pyx_n_s__q_no), ((PyObject *)__pyx_n_s__this_ref_p), ((PyObject *)__pyx_n_s__next_ref_p), ((PyObject *)__pyx_n_s__this_trk_p), ((PyObject *)__pyx_n_s__next_trk_p), ((PyObject *)__pyx_n_s__along), ((PyObject *)__pyx_n_s__normal), ((PyObject *)__pyx_n_s__qMp), ((PyObject *)__pyx_n_s__rMp), ((PyObject *)__pyx_n_s__rMq), ((PyObject *)__pyx_n_s__pMq), ((PyObject *)__pyx_n_s__hit), ((PyObject *)__pyx_n_s__hitMp), ((PyObject *)__pyx_n_s__delta)); if (unlikely(!__pyx_k_tuple_58)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_58);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_58));
  __pyx_k_codeobj_59 = (PyObject*)__Pyx_PyCode_New(2, 0, 39, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_58, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__cut_plane, 191, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_59)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":352
 * 
 * 
 * def most_similar_track_mam(tracks,metric='avg'):             # <<<<<<<<<<<<<<
 *     ''' Find the most similar track in a bundle
 *     using distances calculated from Zhang et. al 2008.
 */
  __pyx_k_tuple_60 = PyTuple_Pack(22, ((PyObject *)__pyx_n_s__tracks), ((PyObject *)__pyx_n_s__metric), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__j), ((PyObject *)__pyx_n_s__lent), ((PyObject *)__pyx_n_s__metric_type), ((PyObject *)__pyx_n_s__longest_track_len), ((PyObject *)__pyx_n_s__track_len), ((PyObject *)__pyx_n_s__tracks32), ((PyObject *)__pyx_n_s__track2others), ((PyObject *)__pyx_n_s__sum_track2others), ((PyObject *)__pyx_n_s__distances_buffer), ((PyObject *)__pyx_n_s__t1_ptr), ((PyObject *)__pyx_n_s__t2_ptr), ((PyObject *)__pyx_n_s__min_buffer), ((PyObject *)__pyx_n_s__distance), ((PyObject *)__pyx_n_s__t1), ((PyObject *)__pyx_n_s__t2), ((PyObject *)__pyx_n_s__t1_len), ((PyObject *)__pyx_n_s__t2_len), ((PyObject *)__pyx_n_s__mn), ((PyObject *)__pyx_n_s__si)); if (unlikely(!__pyx_k_tuple_60)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_60);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_60));
  __pyx_k_codeobj_61 = (PyObject*)__Pyx_PyCode_New(2, 0, 22, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_60, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_62, 352, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_61)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":462
 * @cython.boundscheck(False)
 * @cython.wraparound(False)
 * def bundles_distances_mam(tracksA, tracksB, metric='avg'):             # <<<<<<<<<<<<<<
 *     ''' Calculate distances between list of tracks A and list of tracks B
 * 
 */
  __pyx_k_tuple_63 = PyTuple_Pack(23, ((PyObject *)__pyx_n_s__tracksA), ((PyObject *)__pyx_n_s__tracksB), ((PyObject *)__pyx_n_s__metric), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__j), ((PyObject *)__pyx_n_s__lentA), ((PyObject *)__pyx_n_s__lentB), ((PyObject *)__pyx_n_s__metric_type), ((PyObject *)__pyx_n_s__longest_track_len), ((PyObject *)__pyx_n_s__track_len), ((PyObject *)__pyx_n_s__longest_track_lenA), ((PyObject *)__pyx_n_s__longest_track_lenB), ((PyObject *)__pyx_n_s__tracksA32), ((PyObject *)__pyx_n_s__tracksB32), ((PyObject *)__pyx_n_s__DM), ((PyObject *)__pyx_n_s__distances_buffer), ((PyObject *)__pyx_n_s__t1_ptr), ((PyObject *)__pyx_n_s__t2_ptr), ((PyObject *)__pyx_n_s__min_buffer), ((PyObject *)__pyx_n_s__t1), ((PyObject *)__pyx_n_s__t2), ((PyObject *)__pyx_n_s__t1_len), ((PyObject *)__pyx_n_s__t2_len)); if (unlikely(!__pyx_k_tuple_63)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_63);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_63));
  __pyx_k_codeobj_64 = (PyObject*)__Pyx_PyCode_New(3, 0, 23, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_63, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_65, 462, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_64)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":541
 * @cython.boundscheck(False)
 * @cython.wraparound(False)
 * def bundles_distances_mdf(tracksA, tracksB):             # <<<<<<<<<<<<<<
 *     ''' Calculate distances between list of tracks A and list of tracks B
 * 
 */
  __pyx_k_tuple_66 = PyTuple_Pack(22, ((PyObject *)__pyx_n_s__tracksA), ((PyObject *)__pyx_n_s__tracksB), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__j), ((PyObject *)__pyx_n_s__lentA), ((PyObject *)__pyx_n_s__lentB), ((PyObject *)__pyx_n_s__longest_track_len), ((PyObject *)__pyx_n_s__track_len), ((PyObject *)__pyx_n_s__longest_track_lenA), ((PyObject *)__pyx_n_s__longest_track_lenB), ((PyObject *)__pyx_n_s__tracksA32), ((PyObject *)__pyx_n_s__tracksB32), ((PyObject *)__pyx_n_s__DM), ((PyObject *)__pyx_n_s__t1_ptr), ((PyObject *)__pyx_n_s__t2_ptr), ((PyObject *)__pyx_n_s__min_buffer), ((PyObject *)__pyx_n_s__t1), ((PyObject *)__pyx_n_s__t2), ((PyObject *)__pyx_n_s__t1_len), ((PyObject *)__pyx_n_s__t2_len), ((PyObject *)__pyx_n_s__d), ((PyObject *)__pyx_n_s__t_len)); if (unlikely(!__pyx_k_tuple_66)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_66);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_66));
  __pyx_k_codeobj_67 = (PyObject*)__Pyx_PyCode_New(2, 0, 22, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_66, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_68, 541, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_67)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":694
 * 
 * 
 * def mam_distances(xyz1,xyz2,metric='all'):             # <<<<<<<<<<<<<<
 *     ''' Min/Max/Mean Average Minimum Distance between tracks xyz1 and xyz2
 * 
 */
  __pyx_k_tuple_69 = PyTuple_Pack(14, ((PyObject *)__pyx_n_s__xyz1), ((PyObject *)__pyx_n_s__xyz2), ((PyObject *)__pyx_n_s__metric), ((PyObject *)__pyx_n_s__track1), ((PyObject *)__pyx_n_s__track2), ((PyObject *)__pyx_n_s__t1_len), ((PyObject *)__pyx_n_s__t2_len), ((PyObject *)__pyx_n_s__min_t2t1), ((PyObject *)__pyx_n_s__min_t1t2), ((PyObject *)__pyx_n_s__distances_buffer), ((PyObject *)__pyx_n_s__t1_pi), ((PyObject *)__pyx_n_s__t2_pi), ((PyObject *)__pyx_n_s__mean_t2t1), ((PyObject *)__pyx_n_s__mean_t1t2)); if (unlikely(!__pyx_k_tuple_69)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_69);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_69));
  __pyx_k_codeobj_70 = (PyObject*)__Pyx_PyCode_New(3, 0, 14, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_69, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__mam_distances, 694, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_70)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":780
 * 
 * 
 * def minimum_closest_distance(xyz1,xyz2):             # <<<<<<<<<<<<<<
 *     ''' Find the minimum distance between two curves xyz1, xyz2
 * 
 */
  __pyx_k_tuple_71 = PyTuple_Pack(13, ((PyObject *)__pyx_n_s__xyz1), ((PyObject *)__pyx_n_s__xyz2), ((PyObject *)__pyx_n_s__track1), ((PyObject *)__pyx_n_s__track2), ((PyObject *)__pyx_n_s__t1_len), ((PyObject *)__pyx_n_s__t2_len), ((PyObject *)__pyx_n_s__min_t2t1), ((PyObject *)__pyx_n_s__min_t1t2), ((PyObject *)__pyx_n_s__distances_buffer), ((PyObject *)__pyx_n_s__t1_pi), ((PyObject *)__pyx_n_s__t2_pi), ((PyObject *)__pyx_n_s__min_min_t2t1), ((PyObject *)__pyx_n_s__min_min_t1t2)); if (unlikely(!__pyx_k_tuple_71)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_71);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_71));
  __pyx_k_codeobj_72 = (PyObject*)__Pyx_PyCode_New(2, 0, 13, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_71, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_73, 780, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_72)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":838
 * 
 * 
 * def lee_perpendicular_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Calculates perpendicular distance metric for the distance between two line segments
 * 
 */
  __pyx_k_tuple_74 = PyTuple_Pack(8, ((PyObject *)__pyx_n_s__start0), ((PyObject *)__pyx_n_s__end0), ((PyObject *)__pyx_n_s__start1), ((PyObject *)__pyx_n_s__end1), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s__fvec3), ((PyObject *)__pyx_n_s__fvec4)); if (unlikely(!__pyx_k_tuple_74)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_74);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_74));
  __pyx_k_codeobj_75 = (PyObject*)__Pyx_PyCode_New(4, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_74, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_76, 838, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_75)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":941
 * 
 * 
 * def lee_angle_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Calculates angle distance metric for the distance between two line segments
 * 
 */
  __pyx_k_tuple_77 = PyTuple_Pack(8, ((PyObject *)__pyx_n_s__start0), ((PyObject *)__pyx_n_s__end0), ((PyObject *)__pyx_n_s__start1), ((PyObject *)__pyx_n_s__end1), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s__fvec3), ((PyObject *)__pyx_n_s__fvec4)); if (unlikely(!__pyx_k_tuple_77)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_77);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_77));
  __pyx_k_codeobj_78 = (PyObject*)__Pyx_PyCode_New(4, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_77, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__lee_angle_distance, 941, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_78)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1007
 * 
 * 
 * def approx_polygon_track(xyz,alpha=0.392):             # <<<<<<<<<<<<<<
 *     ''' Fast and simple trajectory approximation algorithm by Eleftherios and Ian
 * 
 */
  __pyx_k_tuple_80 = PyTuple_Pack(13, ((PyObject *)__pyx_n_s__xyz), ((PyObject *)__pyx_n_s__alpha), ((PyObject *)__pyx_n_s__mid_index), ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__fvec0), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s_79), ((PyObject *)__pyx_n_s__t_len), ((PyObject *)__pyx_n_s__angle), ((PyObject *)__pyx_n_s__tmp), ((PyObject *)__pyx_n_s__vec0), ((PyObject *)__pyx_n_s__vec1)); if (unlikely(!__pyx_k_tuple_80)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_80);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_80));
  __pyx_k_codeobj_81 = (PyObject*)__Pyx_PyCode_New(2, 0, 13, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_80, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_82, 1007, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_81)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1091
 * 
 * 
 * def approximate_mdl_trajectory(xyz, alpha=1.):             # <<<<<<<<<<<<<<
 *     ''' Implementation of Lee et al Approximate Trajectory
 *     Partitioning Algorithm
 */
  __pyx_k_tuple_83 = PyTuple_Pack(17, ((PyObject *)__pyx_n_s__xyz), ((PyObject *)__pyx_n_s__alpha), ((PyObject *)__pyx_n_s__start_index), ((PyObject *)__pyx_n_s__length), ((PyObject *)__pyx_n_s__current_index), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__cost_par), ((PyObject *)__pyx_n_s__cost_nopar), ((PyObject *)__pyx_n_s__alphac), ((PyObject *)__pyx_n_s_79), ((PyObject *)__pyx_n_s__t_len), ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__tmp), ((PyObject *)__pyx_n_s__fvec1), ((PyObject *)__pyx_n_s__fvec2), ((PyObject *)__pyx_n_s__fvec3), ((PyObject *)__pyx_n_s__fvec4)); if (unlikely(!__pyx_k_tuple_83)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_83);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_83));
  __pyx_k_codeobj_84 = (PyObject*)__Pyx_PyCode_New(2, 0, 17, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_83, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_85, 1091, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_84)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1158
 * 
 * 
 * def intersect_segment_cylinder(sa,sb,p,q,r):             # <<<<<<<<<<<<<<
 *     ''' Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r
 * 
 */
  __pyx_k_tuple_86 = PyTuple_Pack(12, ((PyObject *)__pyx_n_s__sa), ((PyObject *)__pyx_n_s__sb), ((PyObject *)__pyx_n_s__p), ((PyObject *)__pyx_n_s__q), ((PyObject *)__pyx_n_s__r), ((PyObject *)__pyx_n_s__csa), ((PyObject *)__pyx_n_s__csb), ((PyObject *)__pyx_n_s__cp), ((PyObject *)__pyx_n_s__cq), ((PyObject *)__pyx_n_s__cr), ((PyObject *)__pyx_n_s__ct), ((PyObject *)__pyx_n_s__tmp)); if (unlikely(!__pyx_k_tuple_86)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_86);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_86));
  __pyx_k_codeobj_87 = (PyObject*)__Pyx_PyCode_New(5, 0, 12, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_86, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_88, 1158, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_87)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1271
 * 
 * 
 * def point_segment_sq_distance(a, b, c):             # <<<<<<<<<<<<<<
 *     ''' Calculate the squared distance from a point c to a finite line segment ab.
 * 
 */
  __pyx_k_tuple_89 = PyTuple_Pack(8, ((PyObject *)__pyx_n_s__a), ((PyObject *)__pyx_n_s__b), ((PyObject *)__pyx_n_s__c), ((PyObject *)__pyx_n_s__ca), ((PyObject *)__pyx_n_s__cb), ((PyObject *)__pyx_n_s__cc), ((PyObject *)__pyx_n_s__cr), ((PyObject *)__pyx_n_s__ct)); if (unlikely(!__pyx_k_tuple_89)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_89);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_89));
  __pyx_k_codeobj_90 = (PyObject*)__Pyx_PyCode_New(3, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_89, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_91, 1271, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_90)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1324
 * 
 * 
 * def track_dist_3pts(tracka,trackb):             # <<<<<<<<<<<<<<
 *     ''' Calculate the euclidean distance between two 3pt tracks
 * 
 */
  __pyx_k_tuple_92 = PyTuple_Pack(5, ((PyObject *)__pyx_n_s__tracka), ((PyObject *)__pyx_n_s__trackb), ((PyObject *)__pyx_n_s__a), ((PyObject *)__pyx_n_s__b), ((PyObject *)__pyx_n_s__d)); if (unlikely(!__pyx_k_tuple_92)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_92);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_92));
  __pyx_k_codeobj_93 = (PyObject*)__Pyx_PyCode_New(2, 0, 5, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_92, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__track_dist_3pts, 1324, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_93)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1471
 * @cython.wraparound(False)
 * @cython.cdivision(True)
 * def local_skeleton_clustering(tracks, d_thr=10):             # <<<<<<<<<<<<<<
 *     r"""Efficient tractography clustering
 * 
 */
  __pyx_k_tuple_94 = PyTuple_Pack(24, ((PyObject *)__pyx_n_s__tracks), ((PyObject *)__pyx_n_s__d_thr), ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__cluster), ((PyObject *)__pyx_n_s__lent), ((PyObject *)__pyx_n_s__lenC), ((PyObject *)__pyx_n_s__dim), ((PyObject *)__pyx_n_s__points), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__j), ((PyObject *)__pyx_n_s__c), ((PyObject *)__pyx_n_s__i_k), ((PyObject *)__pyx_n_s__rows), ((PyObject *)__pyx_n_s__cit), ((PyObject *)__pyx_n_s__ptr), ((PyObject *)__pyx_n_s__hid), ((PyObject *)__pyx_n_s__alld), ((PyObject *)__pyx_n_s__d), ((PyObject *)__pyx_n_s__m_d), ((PyObject *)__pyx_n_s__cd_thr), ((PyObject *)__pyx_n_s__flip), ((PyObject *)__pyx_n_s__it), ((PyObject *)__pyx_n_s__k), ((PyObject *)__pyx_n_s__C)); if (unlikely(!__pyx_k_tuple_94)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_94);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_94));
  __pyx_k_codeobj_95 = (PyObject*)__Pyx_PyCode_New(2, 0, 24, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_94, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_96, 1471, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_95)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1656
 *     return C
 * 
 * def local_skeleton_clustering_3pts(tracks, d_thr=10):             # <<<<<<<<<<<<<<
 *     ''' Does a first pass clustering
 * 
 */
  __pyx_k_tuple_97 = PyTuple_Pack(15, ((PyObject *)__pyx_n_s__tracks), ((PyObject *)__pyx_n_s__d_thr), ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__h), ((PyObject *)__pyx_n_s__lent), ((PyObject *)__pyx_n_s__k), ((PyObject *)__pyx_n_s__it), ((PyObject *)__pyx_n_s__d), ((PyObject *)__pyx_n_s__C), ((PyObject *)__pyx_n_s__ts), ((PyObject *)__pyx_n_s__lenC), ((PyObject *)__pyx_n_s__alld), ((PyObject *)__pyx_n_s__flip), ((PyObject *)__pyx_n_s__m_k), ((PyObject *)__pyx_n_s__i_k)); if (unlikely(!__pyx_k_tuple_97)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_97);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_97));
  __pyx_k_codeobj_98 = (PyObject*)__Pyx_PyCode_New(2, 0, 15, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_97, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_99, 1656, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_98)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1786
 * 
 * 
 * def larch_3split(tracks, indices=None, thr=10.):             # <<<<<<<<<<<<<<
 *     '''Generate a first pass clustering using 3 points on the tracks only.
 * 
 */
  __pyx_k_tuple_100 = PyTuple_Pack(17, ((PyObject *)__pyx_n_s__tracks), ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_n_s__thr), ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__h), ((PyObject *)__pyx_n_s__lent), ((PyObject *)__pyx_n_s__k), ((PyObject *)__pyx_n_s__it), ((PyObject *)__pyx_n_s__d), ((PyObject *)__pyx_n_s__C), ((PyObject *)__pyx_n_s__itrange), ((PyObject *)__pyx_n_s__ts), ((PyObject *)__pyx_n_s__lenC), ((PyObject *)__pyx_n_s__alld), ((PyObject *)__pyx_n_s__flip), ((PyObject *)__pyx_n_s__m_k), ((PyObject *)__pyx_n_s__i_k)); if (unlikely(!__pyx_k_tuple_100)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_100);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_100));
  __pyx_k_codeobj_101 = (PyObject*)__Pyx_PyCode_New(3, 0, 17, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_100, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__larch_3split, 1786, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_101)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1889
 * 
 * 
 * def larch_3merge(C,thr=10.):             # <<<<<<<<<<<<<<
 *     '''
 *     Reassign tracks to existing clusters by merging clusters that their
 */
  __pyx_k_tuple_102 = PyTuple_Pack(16, ((PyObject *)__pyx_n_s__C), ((PyObject *)__pyx_n_s__thr), ((PyObject *)__pyx_n_s__h), ((PyObject *)__pyx_n_s__ch), ((PyObject *)__pyx_n_s__lenC), ((PyObject *)__pyx_n_s__k), ((PyObject *)__pyx_n_s__c), ((PyObject *)__pyx_n_s__d), ((PyObject *)__pyx_n_s__ts), ((PyObject *)__pyx_n_s__C2), ((PyObject *)__pyx_n_s__krange), ((PyObject *)__pyx_n_s__klen), ((PyObject *)__pyx_n_s__alld), ((PyObject *)__pyx_n_s__flip), ((PyObject *)__pyx_n_s__m_k), ((PyObject *)__pyx_n_s__i_k)); if (unlikely(!__pyx_k_tuple_102)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_102);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_102));
  __pyx_k_codeobj_103 = (PyObject*)__Pyx_PyCode_New(2, 0, 16, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_102, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s__larch_3merge, 1889, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_103)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":1953
 * 
 * 
 * def point_track_sq_distance_check(cnp.ndarray[float,ndim=2] track,             # <<<<<<<<<<<<<<
 *                                   cnp.ndarray[float,ndim=1] point,
 *                                   double sq_dist_thr):
 */
  __pyx_k_tuple_104 = PyTuple_Pack(12, ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__point), ((PyObject *)__pyx_n_s__sq_dist_thr), ((PyObject *)__pyx_n_s__t), ((PyObject *)__pyx_n_s__p), ((PyObject *)__pyx_n_s__a), ((PyObject *)__pyx_n_s__b), ((PyObject *)__pyx_n_s__tlen), ((PyObject *)__pyx_n_s__curr), ((PyObject *)__pyx_n_s__dist), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__intersects)); if (unlikely(!__pyx_k_tuple_104)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_104);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_104));
  __pyx_k_codeobj_105 = (PyObject*)__Pyx_PyCode_New(3, 0, 12, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_104, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_106, 1953, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_105)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "dipy/tracking/distances.pyx":2011
 *         return False
 * 
 * def track_roi_intersection_check(cnp.ndarray[float,ndim=2] track, cnp.ndarray[float,ndim=2] roi, double sq_dist_thr):             # <<<<<<<<<<<<<<
 *     ''' Check if a track is intersecting a region of interest
 * 
 */
  __pyx_k_tuple_107 = PyTuple_Pack(16, ((PyObject *)__pyx_n_s__track), ((PyObject *)__pyx_n_s__roi), ((PyObject *)__pyx_n_s__sq_dist_thr), ((PyObject *)__pyx_n_s__t), ((PyObject *)__pyx_n_s__r), ((PyObject *)__pyx_n_s__a), ((PyObject *)__pyx_n_s__b), ((PyObject *)__pyx_n_s__p), ((PyObject *)__pyx_n_s__tlen), ((PyObject *)__pyx_n_s__rlen), ((PyObject *)__pyx_n_s__curr), ((PyObject *)__pyx_n_s__currp), ((PyObject *)__pyx_n_s__dist), ((PyObject *)__pyx_n_s__i), ((PyObject *)__pyx_n_s__j), ((PyObject *)__pyx_n_s__intersects)); if (unlikely(!__pyx_k_tuple_107)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_k_tuple_107);
  __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_107));
  __pyx_k_codeobj_108 = (PyObject*)__Pyx_PyCode_New(3, 0, 16, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_107, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_44, __pyx_n_s_109, 2011, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_108)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_RefNannyFinishContext();
  return 0;
  __pyx_L1_error:;
  __Pyx_RefNannyFinishContext();
  return -1;
}

static int __Pyx_InitGlobals(void) {
  if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  __pyx_int_0 = PyInt_FromLong(0); if (unlikely(!__pyx_int_0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  __pyx_int_1 = PyInt_FromLong(1); if (unlikely(!__pyx_int_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  __pyx_int_3 = PyInt_FromLong(3); if (unlikely(!__pyx_int_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  __pyx_int_5 = PyInt_FromLong(5); if (unlikely(!__pyx_int_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  __pyx_int_10 = PyInt_FromLong(10); if (unlikely(!__pyx_int_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  __pyx_int_15 = PyInt_FromLong(15); if (unlikely(!__pyx_int_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  return 0;
  __pyx_L1_error:;
  return -1;
}

#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initdistances(void); /*proto*/
PyMODINIT_FUNC initdistances(void)
#else
PyMODINIT_FUNC PyInit_distances(void); /*proto*/
PyMODINIT_FUNC PyInit_distances(void)
#endif
{
  PyObject *__pyx_t_1 = NULL;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  __pyx_t_5numpy_float32_t __pyx_t_4;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannyDeclarations
  #if CYTHON_REFNANNY
  __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny");
  if (!__Pyx_RefNanny) {
      PyErr_Clear();
      __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny");
      if (!__Pyx_RefNanny)
          Py_FatalError("failed to import 'refnanny' module");
  }
  #endif
  __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit_distances(void)", 0);
  if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #ifdef __Pyx_CyFunction_USED
  if (__Pyx_CyFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #endif
  #ifdef __Pyx_FusedFunction_USED
  if (__pyx_FusedFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #endif
  #ifdef __Pyx_Generator_USED
  if (__pyx_Generator_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #endif
  /*--- Library function declarations ---*/
  /*--- Threads initialization code ---*/
  #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS
  #ifdef WITH_THREAD /* Python build with threading support? */
  PyEval_InitThreads();
  #endif
  #endif
  /*--- Module creation code ---*/
  #if PY_MAJOR_VERSION < 3
  __pyx_m = Py_InitModule4(__Pyx_NAMESTR("distances"), __pyx_methods, __Pyx_DOCSTR(__pyx_k_41), 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);
  #else
  __pyx_m = PyModule_Create(&__pyx_moduledef);
  #endif
  if (unlikely(!__pyx_m)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  Py_INCREF(__pyx_d);
  #if PY_MAJOR_VERSION >= 3
  {
    PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    if (!PyDict_GetItemString(modules, "dipy.tracking.distances")) {
      if (unlikely(PyDict_SetItemString(modules, "dipy.tracking.distances", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
  }
  #endif
  __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); if (unlikely(!__pyx_b)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #if CYTHON_COMPILING_IN_PYPY
  Py_INCREF(__pyx_b);
  #endif
  if (__Pyx_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  /*--- Initialize various global constants etc. ---*/
  if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)
  if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  #endif
  if (__pyx_module_is_main_dipy__tracking__distances) {
    if (__Pyx_SetAttrString(__pyx_m, "__name__", __pyx_n_s____main__) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  }
  /*--- Builtin init code ---*/
  if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  /*--- Constants init code ---*/
  if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  /*--- Global init code ---*/
  __pyx_v_4dipy_8tracking_9distances_f32_dt = ((PyArray_Descr *)Py_None); Py_INCREF(Py_None);
  /*--- Variable export code ---*/
  /*--- Function export code ---*/
  /*--- Type init code ---*/
  /*--- Type import code ---*/
  __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 
  #if CYTHON_COMPILING_IN_PYPY
  sizeof(PyTypeObject),
  #else
  sizeof(PyHeapTypeObject),
  #endif
  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_ptype_5numpy_dtype = __Pyx_ImportType("numpy", "dtype", sizeof(PyArray_Descr), 0); if (unlikely(!__pyx_ptype_5numpy_dtype)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_ptype_5numpy_flatiter = __Pyx_ImportType("numpy", "flatiter", sizeof(PyArrayIterObject), 0); if (unlikely(!__pyx_ptype_5numpy_flatiter)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_ptype_5numpy_broadcast = __Pyx_ImportType("numpy", "broadcast", sizeof(PyArrayMultiIterObject), 0); if (unlikely(!__pyx_ptype_5numpy_broadcast)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_ptype_5numpy_ndarray = __Pyx_ImportType("numpy", "ndarray", sizeof(PyArrayObject), 0); if (unlikely(!__pyx_ptype_5numpy_ndarray)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_ptype_5numpy_ufunc = __Pyx_ImportType("numpy", "ufunc", sizeof(PyUFuncObject), 0); if (unlikely(!__pyx_ptype_5numpy_ufunc)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 861; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  /*--- Variable import code ---*/
  /*--- Function import code ---*/
  /*--- Execution code ---*/

  /* "dipy/tracking/distances.pyx":11
 * cimport cython
 * 
 * import time             # <<<<<<<<<<<<<<
 * import numpy as np
 * cimport numpy as cnp
 */
  __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__time), 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__time, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":12
 * 
 * import time
 * import numpy as np             # <<<<<<<<<<<<<<
 * cimport numpy as cnp
 * 
 */
  __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__numpy), 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":50
 * 
 * 
 * def normalized_3vec(vec):             # <<<<<<<<<<<<<<
 *     ''' Return normalized 3D vector
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_1normalized_3vec, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__normalized_3vec, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":69
 * 
 * 
 * def norm_3vec(vec):             # <<<<<<<<<<<<<<
 *     ''' Euclidean (L2) norm of length 3 vector
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_3norm_3vec, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__norm_3vec, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":122
 * 
 * 
 * def inner_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_5inner_3vecs, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__inner_3vecs, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":136
 * 
 * 
 * def sub_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_7sub_3vecs, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__sub_3vecs, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 136; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":150
 * 
 * 
 * def add_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_9add_3vecs, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__add_3vecs, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 150; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":163
 *         vec_out[i] = vec1[i]+vec2[i]
 * 
 * def mul_3vecs(vec1, vec2):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec1 = as_float_3vec(vec1)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec2 = as_float_3vec(vec2)
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_11mul_3vecs, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__mul_3vecs, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":175
 *         vec_out[i] = vec1[i]*vec2[i]
 * 
 * def mul_3vec(a, vec):             # <<<<<<<<<<<<<<
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] fvec = as_float_3vec(vec)
 *     cdef cnp.ndarray[cnp.float32_t, ndim=1] vec_out = np.zeros((3,), np.float32)
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_13mul_3vec, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__mul_3vec, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 175; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":188
 * 
 * # float 32 dtype for casting
 * cdef cnp.dtype f32_dt = np.dtype(np.float32)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__dtype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_3);
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;
  if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_dtype))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 188; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_XGOTREF(((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
  __Pyx_DECREF(((PyObject *)__pyx_v_4dipy_8tracking_9distances_f32_dt));
  __Pyx_GIVEREF(__pyx_t_3);
  __pyx_v_4dipy_8tracking_9distances_f32_dt = ((PyArray_Descr *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":191
 * 
 * 
 * def cut_plane(tracks,ref):             # <<<<<<<<<<<<<<
 *     ''' Extract divergence vectors and points of intersection
 *     between planes normal to the reference fiber and other tracks
 */
  __pyx_t_3 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_15cut_plane, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__cut_plane, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":352
 * 
 * 
 * def most_similar_track_mam(tracks,metric='avg'):             # <<<<<<<<<<<<<<
 *     ''' Find the most similar track in a bundle
 *     using distances calculated from Zhang et. al 2008.
 */
  __pyx_t_3 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_17most_similar_track_mam, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_62, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 352; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":462
 * @cython.boundscheck(False)
 * @cython.wraparound(False)
 * def bundles_distances_mam(tracksA, tracksB, metric='avg'):             # <<<<<<<<<<<<<<
 *     ''' Calculate distances between list of tracks A and list of tracks B
 * 
 */
  __pyx_t_3 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_19bundles_distances_mam, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_65, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 462; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":541
 * @cython.boundscheck(False)
 * @cython.wraparound(False)
 * def bundles_distances_mdf(tracksA, tracksB):             # <<<<<<<<<<<<<<
 *     ''' Calculate distances between list of tracks A and list of tracks B
 * 
 */
  __pyx_t_3 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_21bundles_distances_mdf, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_68, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 541; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

  /* "dipy/tracking/distances.pyx":611
 * 
 * 
 * cdef cnp.float32_t inf = np.inf             # <<<<<<<<<<<<<<
 * 
 * @cython.cdivision(True)
 */
  __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s__inf); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_4 = __pyx_PyFloat_AsFloat(__pyx_t_1); if (unlikely((__pyx_t_4 == (npy_float32)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 611; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_v_4dipy_8tracking_9distances_inf = __pyx_t_4;

  /* "dipy/tracking/distances.pyx":694
 * 
 * 
 * def mam_distances(xyz1,xyz2,metric='all'):             # <<<<<<<<<<<<<<
 *     ''' Min/Max/Mean Average Minimum Distance between tracks xyz1 and xyz2
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_23mam_distances, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__mam_distances, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":780
 * 
 * 
 * def minimum_closest_distance(xyz1,xyz2):             # <<<<<<<<<<<<<<
 *     ''' Find the minimum distance between two curves xyz1, xyz2
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_25minimum_closest_distance, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_73, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":838
 * 
 * 
 * def lee_perpendicular_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Calculates perpendicular distance metric for the distance between two line segments
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_27lee_perpendicular_distance, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_76, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":941
 * 
 * 
 * def lee_angle_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Calculates angle distance metric for the distance between two line segments
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_29lee_angle_distance, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__lee_angle_distance, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 941; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1007
 * 
 * 
 * def approx_polygon_track(xyz,alpha=0.392):             # <<<<<<<<<<<<<<
 *     ''' Fast and simple trajectory approximation algorithm by Eleftherios and Ian
 * 
 */
  __pyx_t_1 = PyFloat_FromDouble(0.392); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_14 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_31approx_polygon_track, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_82, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1007; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1091
 * 
 * 
 * def approximate_mdl_trajectory(xyz, alpha=1.):             # <<<<<<<<<<<<<<
 *     ''' Implementation of Lee et al Approximate Trajectory
 *     Partitioning Algorithm
 */
  __pyx_t_1 = PyFloat_FromDouble(1.); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_15 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_33approximate_mdl_trajectory, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_85, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1091; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1158
 * 
 * 
 * def intersect_segment_cylinder(sa,sb,p,q,r):             # <<<<<<<<<<<<<<
 *     ''' Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_35intersect_segment_cylinder, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_88, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1158; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1271
 * 
 * 
 * def point_segment_sq_distance(a, b, c):             # <<<<<<<<<<<<<<
 *     ''' Calculate the squared distance from a point c to a finite line segment ab.
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_37point_segment_sq_distance, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_91, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1271; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1324
 * 
 * 
 * def track_dist_3pts(tracka,trackb):             # <<<<<<<<<<<<<<
 *     ''' Calculate the euclidean distance between two 3pt tracks
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_39track_dist_3pts, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__track_dist_3pts, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1471
 * @cython.wraparound(False)
 * @cython.cdivision(True)
 * def local_skeleton_clustering(tracks, d_thr=10):             # <<<<<<<<<<<<<<
 *     r"""Efficient tractography clustering
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_41local_skeleton_clustering, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_96, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1471; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1656
 *     return C
 * 
 * def local_skeleton_clustering_3pts(tracks, d_thr=10):             # <<<<<<<<<<<<<<
 *     ''' Does a first pass clustering
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_43local_skeleton_clustering_3pts, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_99, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1786
 * 
 * 
 * def larch_3split(tracks, indices=None, thr=10.):             # <<<<<<<<<<<<<<
 *     '''Generate a first pass clustering using 3 points on the tracks only.
 * 
 */
  __pyx_t_1 = PyFloat_FromDouble(10.); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_18 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_45larch_3split, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__larch_3split, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1889
 * 
 * 
 * def larch_3merge(C,thr=10.):             # <<<<<<<<<<<<<<
 *     '''
 *     Reassign tracks to existing clusters by merging clusters that their
 */
  __pyx_t_1 = PyFloat_FromDouble(10.); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_22 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_47larch_3merge, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s__larch_3merge, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1889; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":1953
 * 
 * 
 * def point_track_sq_distance_check(cnp.ndarray[float,ndim=2] track,             # <<<<<<<<<<<<<<
 *                                   cnp.ndarray[float,ndim=1] point,
 *                                   double sq_dist_thr):
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_49point_track_sq_distance_check, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_106, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1953; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":2011
 *         return False
 * 
 * def track_roi_intersection_check(cnp.ndarray[float,ndim=2] track, cnp.ndarray[float,ndim=2] roi, double sq_dist_thr):             # <<<<<<<<<<<<<<
 *     ''' Check if a track is intersecting a region of interest
 * 
 */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4dipy_8tracking_9distances_51track_roi_intersection_check, NULL, __pyx_n_s_45); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_109, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2011; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "dipy/tracking/distances.pyx":2
 *  # A type of -*- python -*- file
 * """ Optimized track distances, similarities and clustering algorithms using             # <<<<<<<<<<<<<<
 * track distances
 * """
 */
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_110), ((PyObject *)__pyx_kp_u_111)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_112), ((PyObject *)__pyx_kp_u_113)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_114), ((PyObject *)__pyx_kp_u_115)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_116), ((PyObject *)__pyx_kp_u_117)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_118), ((PyObject *)__pyx_kp_u_119)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_120), ((PyObject *)__pyx_kp_u_121)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_122), ((PyObject *)__pyx_kp_u_123)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_124), ((PyObject *)__pyx_kp_u_125)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_126), ((PyObject *)__pyx_kp_u_127)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_128), ((PyObject *)__pyx_kp_u_129)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_130), ((PyObject *)__pyx_kp_u_131)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_132), ((PyObject *)__pyx_kp_u_133)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (PyDict_SetItem(__pyx_d, __pyx_n_s____test__, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0;

  /* "numpy.pxd":975
 *      arr.base = baseptr
 * 
 * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<
 *     if arr.base is NULL:
 *         return None
 */
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  if (__pyx_m) {
    __Pyx_AddTraceback("init dipy.tracking.distances", __pyx_clineno, __pyx_lineno, __pyx_filename);
    Py_DECREF(__pyx_m); __pyx_m = 0;
  } else if (!PyErr_Occurred()) {
    PyErr_SetString(PyExc_ImportError, "init dipy.tracking.distances");
  }
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  #if PY_MAJOR_VERSION < 3
  return;
  #else
  return __pyx_m;
  #endif
}

/* Runtime support code */
#if CYTHON_REFNANNY
static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {
    PyObject *m = NULL, *p = NULL;
    void *r = NULL;
    m = PyImport_ImportModule((char *)modname);
    if (!m) goto end;
    p = PyObject_GetAttrString(m, (char *)"RefNannyAPI");
    if (!p) goto end;
    r = PyLong_AsVoidPtr(p);
end:
    Py_XDECREF(p);
    Py_XDECREF(m);
    return (__Pyx_RefNannyAPIStruct *)r;
}
#endif /* CYTHON_REFNANNY */

static PyObject *__Pyx_GetBuiltinName(PyObject *name) {
    PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name);
    if (unlikely(!result)) {
        PyErr_Format(PyExc_NameError,
#if PY_MAJOR_VERSION >= 3
            "name '%U' is not defined", name);
#else
            "name '%s' is not defined", PyString_AS_STRING(name));
#endif
    }
    return result;
}

static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
    PyObject *result;
#if CYTHON_COMPILING_IN_CPYTHON
    result = PyDict_GetItem(__pyx_d, name);
    if (result) {
        Py_INCREF(result);
    } else {
#else
    result = PyObject_GetItem(__pyx_d, name);
    if (!result) {
        PyErr_Clear();
#endif
        result = __Pyx_GetBuiltinName(name);
    }
    return result;
}

static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
    if (unlikely(!type)) {
        PyErr_Format(PyExc_SystemError, "Missing type object");
        return 0;
    }
    if (likely(PyObject_TypeCheck(obj, type)))
        return 1;
    PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
                 Py_TYPE(obj)->tp_name, type->tp_name);
    return 0;
}

static CYTHON_INLINE int __Pyx_IsLittleEndian(void) {
  unsigned int n = 1;
  return *(unsigned char*)(&n) != 0;
}
static void __Pyx_BufFmt_Init(__Pyx_BufFmt_Context* ctx,
                              __Pyx_BufFmt_StackElem* stack,
                              __Pyx_TypeInfo* type) {
  stack[0].field = &ctx->root;
  stack[0].parent_offset = 0;
  ctx->root.type = type;
  ctx->root.name = "buffer dtype";
  ctx->root.offset = 0;
  ctx->head = stack;
  ctx->head->field = &ctx->root;
  ctx->fmt_offset = 0;
  ctx->head->parent_offset = 0;
  ctx->new_packmode = '@';
  ctx->enc_packmode = '@';
  ctx->new_count = 1;
  ctx->enc_count = 0;
  ctx->enc_type = 0;
  ctx->is_complex = 0;
  ctx->is_valid_array = 0;
  ctx->struct_alignment = 0;
  while (type->typegroup == 'S') {
    ++ctx->head;
    ctx->head->field = type->fields;
    ctx->head->parent_offset = 0;
    type = type->fields->type;
  }
}
static int __Pyx_BufFmt_ParseNumber(const char** ts) {
    int count;
    const char* t = *ts;
    if (*t < '0' || *t > '9') {
      return -1;
    } else {
        count = *t++ - '0';
        while (*t >= '0' && *t < '9') {
            count *= 10;
            count += *t++ - '0';
        }
    }
    *ts = t;
    return count;
}
static int __Pyx_BufFmt_ExpectNumber(const char **ts) {
    int number = __Pyx_BufFmt_ParseNumber(ts);
    if (number == -1) /* First char was not a digit */
        PyErr_Format(PyExc_ValueError,\
                     "Does not understand character buffer dtype format string ('%c')", **ts);
    return number;
}
static void __Pyx_BufFmt_RaiseUnexpectedChar(char ch) {
  PyErr_Format(PyExc_ValueError,
               "Unexpected format string character: '%c'", ch);
}
static const char* __Pyx_BufFmt_DescribeTypeChar(char ch, int is_complex) {
  switch (ch) {
    case 'c': return "'char'";
    case 'b': return "'signed char'";
    case 'B': return "'unsigned char'";
    case 'h': return "'short'";
    case 'H': return "'unsigned short'";
    case 'i': return "'int'";
    case 'I': return "'unsigned int'";
    case 'l': return "'long'";
    case 'L': return "'unsigned long'";
    case 'q': return "'long long'";
    case 'Q': return "'unsigned long long'";
    case 'f': return (is_complex ? "'complex float'" : "'float'");
    case 'd': return (is_complex ? "'complex double'" : "'double'");
    case 'g': return (is_complex ? "'complex long double'" : "'long double'");
    case 'T': return "a struct";
    case 'O': return "Python object";
    case 'P': return "a pointer";
    case 's': case 'p': return "a string";
    case 0: return "end";
    default: return "unparseable format string";
  }
}
static size_t __Pyx_BufFmt_TypeCharToStandardSize(char ch, int is_complex) {
  switch (ch) {
    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;
    case 'h': case 'H': return 2;
    case 'i': case 'I': case 'l': case 'L': return 4;
    case 'q': case 'Q': return 8;
    case 'f': return (is_complex ? 8 : 4);
    case 'd': return (is_complex ? 16 : 8);
    case 'g': {
      PyErr_SetString(PyExc_ValueError, "Python does not define a standard format string size for long double ('g')..");
      return 0;
    }
    case 'O': case 'P': return sizeof(void*);
    default:
      __Pyx_BufFmt_RaiseUnexpectedChar(ch);
      return 0;
    }
}
static size_t __Pyx_BufFmt_TypeCharToNativeSize(char ch, int is_complex) {
  switch (ch) {
    case 'c': case 'b': case 'B': case 's': case 'p': return 1;
    case 'h': case 'H': return sizeof(short);
    case 'i': case 'I': return sizeof(int);
    case 'l': case 'L': return sizeof(long);
    #ifdef HAVE_LONG_LONG
    case 'q': case 'Q': return sizeof(PY_LONG_LONG);
    #endif
    case 'f': return sizeof(float) * (is_complex ? 2 : 1);
    case 'd': return sizeof(double) * (is_complex ? 2 : 1);
    case 'g': return sizeof(long double) * (is_complex ? 2 : 1);
    case 'O': case 'P': return sizeof(void*);
    default: {
      __Pyx_BufFmt_RaiseUnexpectedChar(ch);
      return 0;
    }
  }
}
typedef struct { char c; short x; } __Pyx_st_short;
typedef struct { char c; int x; } __Pyx_st_int;
typedef struct { char c; long x; } __Pyx_st_long;
typedef struct { char c; float x; } __Pyx_st_float;
typedef struct { char c; double x; } __Pyx_st_double;
typedef struct { char c; long double x; } __Pyx_st_longdouble;
typedef struct { char c; void *x; } __Pyx_st_void_p;
#ifdef HAVE_LONG_LONG
typedef struct { char c; PY_LONG_LONG x; } __Pyx_st_longlong;
#endif
static size_t __Pyx_BufFmt_TypeCharToAlignment(char ch, CYTHON_UNUSED int is_complex) {
  switch (ch) {
    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;
    case 'h': case 'H': return sizeof(__Pyx_st_short) - sizeof(short);
    case 'i': case 'I': return sizeof(__Pyx_st_int) - sizeof(int);
    case 'l': case 'L': return sizeof(__Pyx_st_long) - sizeof(long);
#ifdef HAVE_LONG_LONG
    case 'q': case 'Q': return sizeof(__Pyx_st_longlong) - sizeof(PY_LONG_LONG);
#endif
    case 'f': return sizeof(__Pyx_st_float) - sizeof(float);
    case 'd': return sizeof(__Pyx_st_double) - sizeof(double);
    case 'g': return sizeof(__Pyx_st_longdouble) - sizeof(long double);
    case 'P': case 'O': return sizeof(__Pyx_st_void_p) - sizeof(void*);
    default:
      __Pyx_BufFmt_RaiseUnexpectedChar(ch);
      return 0;
    }
}
/* These are for computing the padding at the end of the struct to align
   on the first member of the struct. This will probably the same as above,
   but we don't have any guarantees.
 */
typedef struct { short x; char c; } __Pyx_pad_short;
typedef struct { int x; char c; } __Pyx_pad_int;
typedef struct { long x; char c; } __Pyx_pad_long;
typedef struct { float x; char c; } __Pyx_pad_float;
typedef struct { double x; char c; } __Pyx_pad_double;
typedef struct { long double x; char c; } __Pyx_pad_longdouble;
typedef struct { void *x; char c; } __Pyx_pad_void_p;
#ifdef HAVE_LONG_LONG
typedef struct { PY_LONG_LONG x; char c; } __Pyx_pad_longlong;
#endif
static size_t __Pyx_BufFmt_TypeCharToPadding(char ch, CYTHON_UNUSED int is_complex) {
  switch (ch) {
    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;
    case 'h': case 'H': return sizeof(__Pyx_pad_short) - sizeof(short);
    case 'i': case 'I': return sizeof(__Pyx_pad_int) - sizeof(int);
    case 'l': case 'L': return sizeof(__Pyx_pad_long) - sizeof(long);
#ifdef HAVE_LONG_LONG
    case 'q': case 'Q': return sizeof(__Pyx_pad_longlong) - sizeof(PY_LONG_LONG);
#endif
    case 'f': return sizeof(__Pyx_pad_float) - sizeof(float);
    case 'd': return sizeof(__Pyx_pad_double) - sizeof(double);
    case 'g': return sizeof(__Pyx_pad_longdouble) - sizeof(long double);
    case 'P': case 'O': return sizeof(__Pyx_pad_void_p) - sizeof(void*);
    default:
      __Pyx_BufFmt_RaiseUnexpectedChar(ch);
      return 0;
    }
}
static char __Pyx_BufFmt_TypeCharToGroup(char ch, int is_complex) {
  switch (ch) {
    case 'c':
        return 'H';
    case 'b': case 'h': case 'i':
    case 'l': case 'q': case 's': case 'p':
        return 'I';
    case 'B': case 'H': case 'I': case 'L': case 'Q':
        return 'U';
    case 'f': case 'd': case 'g':
        return (is_complex ? 'C' : 'R');
    case 'O':
        return 'O';
    case 'P':
        return 'P';
    default: {
      __Pyx_BufFmt_RaiseUnexpectedChar(ch);
      return 0;
    }
  }
}
static void __Pyx_BufFmt_RaiseExpected(__Pyx_BufFmt_Context* ctx) {
  if (ctx->head == NULL || ctx->head->field == &ctx->root) {
    const char* expected;
    const char* quote;
    if (ctx->head == NULL) {
      expected = "end";
      quote = "";
    } else {
      expected = ctx->head->field->type->name;
      quote = "'";
    }
    PyErr_Format(PyExc_ValueError,
                 "Buffer dtype mismatch, expected %s%s%s but got %s",
                 quote, expected, quote,
                 __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex));
  } else {
    __Pyx_StructField* field = ctx->head->field;
    __Pyx_StructField* parent = (ctx->head - 1)->field;
    PyErr_Format(PyExc_ValueError,
                 "Buffer dtype mismatch, expected '%s' but got %s in '%s.%s'",
                 field->type->name, __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex),
                 parent->type->name, field->name);
  }
}
static int __Pyx_BufFmt_ProcessTypeChunk(__Pyx_BufFmt_Context* ctx) {
  char group;
  size_t size, offset, arraysize = 1;
  if (ctx->enc_type == 0) return 0;
  if (ctx->head->field->type->arraysize[0]) {
    int i, ndim = 0;
    if (ctx->enc_type == 's' || ctx->enc_type == 'p') {
        ctx->is_valid_array = ctx->head->field->type->ndim == 1;
        ndim = 1;
        if (ctx->enc_count != ctx->head->field->type->arraysize[0]) {
            PyErr_Format(PyExc_ValueError,
                         "Expected a dimension of size %zu, got %zu",
                         ctx->head->field->type->arraysize[0], ctx->enc_count);
            return -1;
        }
    }
    if (!ctx->is_valid_array) {
      PyErr_Format(PyExc_ValueError, "Expected %d dimensions, got %d",
                   ctx->head->field->type->ndim, ndim);
      return -1;
    }
    for (i = 0; i < ctx->head->field->type->ndim; i++) {
      arraysize *= ctx->head->field->type->arraysize[i];
    }
    ctx->is_valid_array = 0;
    ctx->enc_count = 1;
  }
  group = __Pyx_BufFmt_TypeCharToGroup(ctx->enc_type, ctx->is_complex);
  do {
    __Pyx_StructField* field = ctx->head->field;
    __Pyx_TypeInfo* type = field->type;
    if (ctx->enc_packmode == '@' || ctx->enc_packmode == '^') {
      size = __Pyx_BufFmt_TypeCharToNativeSize(ctx->enc_type, ctx->is_complex);
    } else {
      size = __Pyx_BufFmt_TypeCharToStandardSize(ctx->enc_type, ctx->is_complex);
    }
    if (ctx->enc_packmode == '@') {
      size_t align_at = __Pyx_BufFmt_TypeCharToAlignment(ctx->enc_type, ctx->is_complex);
      size_t align_mod_offset;
      if (align_at == 0) return -1;
      align_mod_offset = ctx->fmt_offset % align_at;
      if (align_mod_offset > 0) ctx->fmt_offset += align_at - align_mod_offset;
      if (ctx->struct_alignment == 0)
          ctx->struct_alignment = __Pyx_BufFmt_TypeCharToPadding(ctx->enc_type,
                                                                 ctx->is_complex);
    }
    if (type->size != size || type->typegroup != group) {
      if (type->typegroup == 'C' && type->fields != NULL) {
        size_t parent_offset = ctx->head->parent_offset + field->offset;
        ++ctx->head;
        ctx->head->field = type->fields;
        ctx->head->parent_offset = parent_offset;
        continue;
      }
      if ((type->typegroup == 'H' || group == 'H') && type->size == size) {
      } else {
          __Pyx_BufFmt_RaiseExpected(ctx);
          return -1;
      }
    }
    offset = ctx->head->parent_offset + field->offset;
    if (ctx->fmt_offset != offset) {
      PyErr_Format(PyExc_ValueError,
                   "Buffer dtype mismatch; next field is at offset %" CYTHON_FORMAT_SSIZE_T "d but %" CYTHON_FORMAT_SSIZE_T "d expected",
                   (Py_ssize_t)ctx->fmt_offset, (Py_ssize_t)offset);
      return -1;
    }
    ctx->fmt_offset += size;
    if (arraysize)
      ctx->fmt_offset += (arraysize - 1) * size;
    --ctx->enc_count; /* Consume from buffer string */
    while (1) {
      if (field == &ctx->root) {
        ctx->head = NULL;
        if (ctx->enc_count != 0) {
          __Pyx_BufFmt_RaiseExpected(ctx);
          return -1;
        }
        break; /* breaks both loops as ctx->enc_count == 0 */
      }
      ctx->head->field = ++field;
      if (field->type == NULL) {
        --ctx->head;
        field = ctx->head->field;
        continue;
      } else if (field->type->typegroup == 'S') {
        size_t parent_offset = ctx->head->parent_offset + field->offset;
        if (field->type->fields->type == NULL) continue; /* empty struct */
        field = field->type->fields;
        ++ctx->head;
        ctx->head->field = field;
        ctx->head->parent_offset = parent_offset;
        break;
      } else {
        break;
      }
    }
  } while (ctx->enc_count);
  ctx->enc_type = 0;
  ctx->is_complex = 0;
  return 0;
}
static CYTHON_INLINE PyObject *
__pyx_buffmt_parse_array(__Pyx_BufFmt_Context* ctx, const char** tsp)
{
    const char *ts = *tsp;
    int i = 0, number;
    int ndim = ctx->head->field->type->ndim;
;
    ++ts;
    if (ctx->new_count != 1) {
        PyErr_SetString(PyExc_ValueError,
                        "Cannot handle repeated arrays in format string");
        return NULL;
    }
    if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
    while (*ts && *ts != ')') {
        if (isspace(*ts))
            continue;
        number = __Pyx_BufFmt_ExpectNumber(&ts);
        if (number == -1) return NULL;
        if (i < ndim && (size_t) number != ctx->head->field->type->arraysize[i])
            return PyErr_Format(PyExc_ValueError,
                        "Expected a dimension of size %zu, got %d",
                        ctx->head->field->type->arraysize[i], number);
        if (*ts != ',' && *ts != ')')
            return PyErr_Format(PyExc_ValueError,
                                "Expected a comma in format string, got '%c'", *ts);
        if (*ts == ',') ts++;
        i++;
    }
    if (i != ndim)
        return PyErr_Format(PyExc_ValueError, "Expected %d dimension(s), got %d",
                            ctx->head->field->type->ndim, i);
    if (!*ts) {
        PyErr_SetString(PyExc_ValueError,
                        "Unexpected end of format string, expected ')'");
        return NULL;
    }
    ctx->is_valid_array = 1;
    ctx->new_count = 1;
    *tsp = ++ts;
    return Py_None;
}
static const char* __Pyx_BufFmt_CheckString(__Pyx_BufFmt_Context* ctx, const char* ts) {
  int got_Z = 0;
  while (1) {
    switch(*ts) {
      case 0:
        if (ctx->enc_type != 0 && ctx->head == NULL) {
          __Pyx_BufFmt_RaiseExpected(ctx);
          return NULL;
        }
        if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
        if (ctx->head != NULL) {
          __Pyx_BufFmt_RaiseExpected(ctx);
          return NULL;
        }
                return ts;
      case ' ':
      case 10:
      case 13:
        ++ts;
        break;
      case '<':
        if (!__Pyx_IsLittleEndian()) {
          PyErr_SetString(PyExc_ValueError, "Little-endian buffer not supported on big-endian compiler");
          return NULL;
        }
        ctx->new_packmode = '=';
        ++ts;
        break;
      case '>':
      case '!':
        if (__Pyx_IsLittleEndian()) {
          PyErr_SetString(PyExc_ValueError, "Big-endian buffer not supported on little-endian compiler");
          return NULL;
        }
        ctx->new_packmode = '=';
        ++ts;
        break;
      case '=':
      case '@':
      case '^':
        ctx->new_packmode = *ts++;
        break;
      case 'T': /* substruct */
        {
          const char* ts_after_sub;
          size_t i, struct_count = ctx->new_count;
          size_t struct_alignment = ctx->struct_alignment;
          ctx->new_count = 1;
          ++ts;
          if (*ts != '{') {
            PyErr_SetString(PyExc_ValueError, "Buffer acquisition: Expected '{' after 'T'");
            return NULL;
          }
          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
          ctx->enc_type = 0; /* Erase processed last struct element */
          ctx->enc_count = 0;
          ctx->struct_alignment = 0;
          ++ts;
          ts_after_sub = ts;
          for (i = 0; i != struct_count; ++i) {
            ts_after_sub = __Pyx_BufFmt_CheckString(ctx, ts);
            if (!ts_after_sub) return NULL;
          }
          ts = ts_after_sub;
          if (struct_alignment) ctx->struct_alignment = struct_alignment;
        }
        break;
      case '}': /* end of substruct; either repeat or move on */
        {
          size_t alignment = ctx->struct_alignment;
          ++ts;
          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
          ctx->enc_type = 0; /* Erase processed last struct element */
          if (alignment && ctx->fmt_offset % alignment) {
            ctx->fmt_offset += alignment - (ctx->fmt_offset % alignment);
          }
        }
        return ts;
      case 'x':
        if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
        ctx->fmt_offset += ctx->new_count;
        ctx->new_count = 1;
        ctx->enc_count = 0;
        ctx->enc_type = 0;
        ctx->enc_packmode = ctx->new_packmode;
        ++ts;
        break;
      case 'Z':
        got_Z = 1;
        ++ts;
        if (*ts != 'f' && *ts != 'd' && *ts != 'g') {
          __Pyx_BufFmt_RaiseUnexpectedChar('Z');
          return NULL;
        }        /* fall through */
      case 'c': case 'b': case 'B': case 'h': case 'H': case 'i': case 'I':
      case 'l': case 'L': case 'q': case 'Q':
      case 'f': case 'd': case 'g':
      case 'O': case 's': case 'p':
        if (ctx->enc_type == *ts && got_Z == ctx->is_complex &&
            ctx->enc_packmode == ctx->new_packmode) {
          ctx->enc_count += ctx->new_count;
        } else {
          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
          ctx->enc_count = ctx->new_count;
          ctx->enc_packmode = ctx->new_packmode;
          ctx->enc_type = *ts;
          ctx->is_complex = got_Z;
        }
        ++ts;
        ctx->new_count = 1;
        got_Z = 0;
        break;
      case ':':
        ++ts;
        while(*ts != ':') ++ts;
        ++ts;
        break;
      case '(':
        if (!__pyx_buffmt_parse_array(ctx, &ts)) return NULL;
        break;
      default:
        {
          int number = __Pyx_BufFmt_ExpectNumber(&ts);
          if (number == -1) return NULL;
          ctx->new_count = (size_t)number;
        }
    }
  }
}
static CYTHON_INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) {
  buf->buf = NULL;
  buf->obj = NULL;
  buf->strides = __Pyx_zeros;
  buf->shape = __Pyx_zeros;
  buf->suboffsets = __Pyx_minusones;
}
static CYTHON_INLINE int __Pyx_GetBufferAndValidate(
        Py_buffer* buf, PyObject* obj,  __Pyx_TypeInfo* dtype, int flags,
        int nd, int cast, __Pyx_BufFmt_StackElem* stack)
{
  if (obj == Py_None || obj == NULL) {
    __Pyx_ZeroBuffer(buf);
    return 0;
  }
  buf->buf = NULL;
  if (__Pyx_GetBuffer(obj, buf, flags) == -1) goto fail;
  if (buf->ndim != nd) {
    PyErr_Format(PyExc_ValueError,
                 "Buffer has wrong number of dimensions (expected %d, got %d)",
                 nd, buf->ndim);
    goto fail;
  }
  if (!cast) {
    __Pyx_BufFmt_Context ctx;
    __Pyx_BufFmt_Init(&ctx, stack, dtype);
    if (!__Pyx_BufFmt_CheckString(&ctx, buf->format)) goto fail;
  }
  if ((unsigned)buf->itemsize != dtype->size) {
    PyErr_Format(PyExc_ValueError,
      "Item size of buffer (%" CYTHON_FORMAT_SSIZE_T "d byte%s) does not match size of '%s' (%" CYTHON_FORMAT_SSIZE_T "d byte%s)",
      buf->itemsize, (buf->itemsize > 1) ? "s" : "",
      dtype->name, (Py_ssize_t)dtype->size, (dtype->size > 1) ? "s" : "");
    goto fail;
  }
  if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones;
  return 0;
fail:;
  __Pyx_ZeroBuffer(buf);
  return -1;
}
static CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info) {
  if (info->buf == NULL) return;
  if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL;
  __Pyx_ReleaseBuffer(info);
}

static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {
#if CYTHON_COMPILING_IN_CPYTHON
    PyObject *tmp_type, *tmp_value, *tmp_tb;
    PyThreadState *tstate = PyThreadState_GET();
    tmp_type = tstate->curexc_type;
    tmp_value = tstate->curexc_value;
    tmp_tb = tstate->curexc_traceback;
    tstate->curexc_type = type;
    tstate->curexc_value = value;
    tstate->curexc_traceback = tb;
    Py_XDECREF(tmp_type);
    Py_XDECREF(tmp_value);
    Py_XDECREF(tmp_tb);
#else
    PyErr_Restore(type, value, tb);
#endif
}
static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {
#if CYTHON_COMPILING_IN_CPYTHON
    PyThreadState *tstate = PyThreadState_GET();
    *type = tstate->curexc_type;
    *value = tstate->curexc_value;
    *tb = tstate->curexc_traceback;
    tstate->curexc_type = 0;
    tstate->curexc_value = 0;
    tstate->curexc_traceback = 0;
#else
    PyErr_Fetch(type, value, tb);
#endif
}

static void __Pyx_WriteUnraisable(const char *name, CYTHON_UNUSED int clineno,
                                  CYTHON_UNUSED int lineno, CYTHON_UNUSED const char *filename) {
    PyObject *old_exc, *old_val, *old_tb;
    PyObject *ctx;
    __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
    #if PY_MAJOR_VERSION < 3
    ctx = PyString_FromString(name);
    #else
    ctx = PyUnicode_FromString(name);
    #endif
    __Pyx_ErrRestore(old_exc, old_val, old_tb);
    if (!ctx) {
        PyErr_WriteUnraisable(Py_None);
    } else {
        PyErr_WriteUnraisable(ctx);
        Py_DECREF(ctx);
    }
}

static void __Pyx_RaiseArgtupleInvalid(
    const char* func_name,
    int exact,
    Py_ssize_t num_min,
    Py_ssize_t num_max,
    Py_ssize_t num_found)
{
    Py_ssize_t num_expected;
    const char *more_or_less;
    if (num_found < num_min) {
        num_expected = num_min;
        more_or_less = "at least";
    } else {
        num_expected = num_max;
        more_or_less = "at most";
    }
    if (exact) {
        more_or_less = "exactly";
    }
    PyErr_Format(PyExc_TypeError,
                 "%s() takes %s %" CYTHON_FORMAT_SSIZE_T "d positional argument%s (%" CYTHON_FORMAT_SSIZE_T "d given)",
                 func_name, more_or_less, num_expected,
                 (num_expected == 1) ? "" : "s", num_found);
}

static void __Pyx_RaiseDoubleKeywordsError(
    const char* func_name,
    PyObject* kw_name)
{
    PyErr_Format(PyExc_TypeError,
        #if PY_MAJOR_VERSION >= 3
        "%s() got multiple values for keyword argument '%U'", func_name, kw_name);
        #else
        "%s() got multiple values for keyword argument '%s'", func_name,
        PyString_AsString(kw_name));
        #endif
}

static int __Pyx_ParseOptionalKeywords(
    PyObject *kwds,
    PyObject **argnames[],
    PyObject *kwds2,
    PyObject *values[],
    Py_ssize_t num_pos_args,
    const char* function_name)
{
    PyObject *key = 0, *value = 0;
    Py_ssize_t pos = 0;
    PyObject*** name;
    PyObject*** first_kw_arg = argnames + num_pos_args;
    while (PyDict_Next(kwds, &pos, &key, &value)) {
        name = first_kw_arg;
        while (*name && (**name != key)) name++;
        if (*name) {
            values[name-argnames] = value;
            continue;
        }
        name = first_kw_arg;
        #if PY_MAJOR_VERSION < 3
        if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) {
            while (*name) {
                if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key))
                        && _PyString_Eq(**name, key)) {
                    values[name-argnames] = value;
                    break;
                }
                name++;
            }
            if (*name) continue;
            else {
                PyObject*** argname = argnames;
                while (argname != first_kw_arg) {
                    if ((**argname == key) || (
                            (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key))
                             && _PyString_Eq(**argname, key))) {
                        goto arg_passed_twice;
                    }
                    argname++;
                }
            }
        } else
        #endif
        if (likely(PyUnicode_Check(key))) {
            while (*name) {
                int cmp = (**name == key) ? 0 :
                #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
                    (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :
                #endif
                    PyUnicode_Compare(**name, key);
                if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
                if (cmp == 0) {
                    values[name-argnames] = value;
                    break;
                }
                name++;
            }
            if (*name) continue;
            else {
                PyObject*** argname = argnames;
                while (argname != first_kw_arg) {
                    int cmp = (**argname == key) ? 0 :
                    #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3
                        (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :
                    #endif
                        PyUnicode_Compare(**argname, key);
                    if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;
                    if (cmp == 0) goto arg_passed_twice;
                    argname++;
                }
            }
        } else
            goto invalid_keyword_type;
        if (kwds2) {
            if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
        } else {
            goto invalid_keyword;
        }
    }
    return 0;
arg_passed_twice:
    __Pyx_RaiseDoubleKeywordsError(function_name, key);
    goto bad;
invalid_keyword_type:
    PyErr_Format(PyExc_TypeError,
        "%s() keywords must be strings", function_name);
    goto bad;
invalid_keyword:
    PyErr_Format(PyExc_TypeError,
    #if PY_MAJOR_VERSION < 3
        "%s() got an unexpected keyword argument '%s'",
        function_name, PyString_AsString(key));
    #else
        "%s() got an unexpected keyword argument '%U'",
        function_name, key);
    #endif
bad:
    return -1;
}

static void __Pyx_RaiseBufferFallbackError(void) {
  PyErr_Format(PyExc_ValueError,
     "Buffer acquisition failed on assignment; and then reacquiring the old buffer failed too!");
}

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
    PyObject *r;
    if (!j) return NULL;
    r = PyObject_GetItem(o, j);
    Py_DECREF(j);
    return r;
}
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
                                                              int wraparound, int boundscheck) {
#if CYTHON_COMPILING_IN_CPYTHON
    if (wraparound & unlikely(i < 0)) i += PyList_GET_SIZE(o);
    if ((!boundscheck) || likely((0 <= i) & (i < PyList_GET_SIZE(o)))) {
        PyObject *r = PyList_GET_ITEM(o, i);
        Py_INCREF(r);
        return r;
    }
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
#else
    return PySequence_GetItem(o, i);
#endif
}
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
                                                              int wraparound, int boundscheck) {
#if CYTHON_COMPILING_IN_CPYTHON
    if (wraparound & unlikely(i < 0)) i += PyTuple_GET_SIZE(o);
    if ((!boundscheck) || likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
        PyObject *r = PyTuple_GET_ITEM(o, i);
        Py_INCREF(r);
        return r;
    }
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
#else
    return PySequence_GetItem(o, i);
#endif
}
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
                                                     int is_list, int wraparound, int boundscheck) {
#if CYTHON_COMPILING_IN_CPYTHON
    if (is_list || PyList_CheckExact(o)) {
        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyList_GET_SIZE(o);
        if ((!boundscheck) || (likely((n >= 0) & (n < PyList_GET_SIZE(o))))) {
            PyObject *r = PyList_GET_ITEM(o, n);
            Py_INCREF(r);
            return r;
        }
    }
    else if (PyTuple_CheckExact(o)) {
        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o);
        if ((!boundscheck) || likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) {
            PyObject *r = PyTuple_GET_ITEM(o, n);
            Py_INCREF(r);
            return r;
        }
    } else {
        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
        if (likely(m && m->sq_item)) {
            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
                Py_ssize_t l = m->sq_length(o);
                if (likely(l >= 0)) {
                    i += l;
                } else {
                    if (PyErr_ExceptionMatches(PyExc_OverflowError))
                        PyErr_Clear();
                    else
                        return NULL;
                }
            }
            return m->sq_item(o, i);
        }
    }
#else
    if (is_list || PySequence_Check(o)) {
        return PySequence_GetItem(o, i);
    }
#endif
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
}

static void __Pyx_RaiseBufferIndexError(int axis) {
  PyErr_Format(PyExc_IndexError,
     "Out of bounds on buffer access (axis %d)", axis);
}

static CYTHON_INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
    if (likely(PyList_CheckExact(L))) {
        if (unlikely(__Pyx_PyList_Append(L, x) < 0)) return NULL;
        Py_INCREF(Py_None);
        return Py_None; /* this is just to have an accurate signature */
    } else {
        return __Pyx_PyObject_CallMethod1(L, __pyx_n_s__append, x);
    }
}

static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
    int r;
    if (!j) return -1;
    r = PyObject_SetItem(o, j, v);
    Py_DECREF(j);
    return r;
}
static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v,
                                               int is_list, int wraparound, int boundscheck) {
#if CYTHON_COMPILING_IN_CPYTHON
    if (is_list || PyList_CheckExact(o)) {
        Py_ssize_t n = (!wraparound) ? i : ((likely(i >= 0)) ? i : i + PyList_GET_SIZE(o));
        if ((!boundscheck) || likely((n >= 0) & (n < PyList_GET_SIZE(o)))) {
            PyObject* old = PyList_GET_ITEM(o, n);
            Py_INCREF(v);
            PyList_SET_ITEM(o, n, v);
            Py_DECREF(old);
            return 1;
        }
    } else {
        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
        if (likely(m && m->sq_ass_item)) {
            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
                Py_ssize_t l = m->sq_length(o);
                if (likely(l >= 0)) {
                    i += l;
                } else {
                    if (PyErr_ExceptionMatches(PyExc_OverflowError))
                        PyErr_Clear();
                    else
                        return -1;
                }
            }
            return m->sq_ass_item(o, i, v);
        }
    }
#else
#if CYTHON_COMPILING_IN_PYPY
    if (is_list || (PySequence_Check(o) && !PyDict_Check(o))) {
#else
    if (is_list || PySequence_Check(o)) {
#endif
        return PySequence_SetItem(o, i, v);
    }
#endif
    return __Pyx_SetItemInt_Generic(o, PyInt_FromSsize_t(i), v);
}

static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(
        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,
        PyObject** _py_start, PyObject** _py_stop, PyObject** _py_slice,
        int has_cstart, int has_cstop, CYTHON_UNUSED int wraparound) {
#if CYTHON_COMPILING_IN_CPYTHON
    PyMappingMethods* mp;
#if PY_MAJOR_VERSION < 3
    PySequenceMethods* ms = Py_TYPE(obj)->tp_as_sequence;
    if (likely(ms && ms->sq_slice)) {
        if (!has_cstart) {
            if (_py_start && (*_py_start != Py_None)) {
                cstart = __Pyx_PyIndex_AsSsize_t(*_py_start);
                if ((cstart == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
            } else
                cstart = 0;
        }
        if (!has_cstop) {
            if (_py_stop && (*_py_stop != Py_None)) {
                cstop = __Pyx_PyIndex_AsSsize_t(*_py_stop);
                if ((cstop == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
            } else
                cstop = PY_SSIZE_T_MAX;
        }
        if (wraparound && unlikely((cstart < 0) | (cstop < 0)) && likely(ms->sq_length)) {
            Py_ssize_t l = ms->sq_length(obj);
            if (likely(l >= 0)) {
                if (cstop < 0) {
                    cstop += l;
                    if (cstop < 0) cstop = 0;
                }
                if (cstart < 0) {
                    cstart += l;
                    if (cstart < 0) cstart = 0;
                }
            } else {
                if (PyErr_ExceptionMatches(PyExc_OverflowError))
                    PyErr_Clear();
                else
                    goto bad;
            }
        }
        return ms->sq_slice(obj, cstart, cstop);
    }
#endif
    mp = Py_TYPE(obj)->tp_as_mapping;
    if (likely(mp && mp->mp_subscript))
#endif
    {
        PyObject* result;
        PyObject *py_slice, *py_start, *py_stop;
        if (_py_slice) {
            py_slice = *_py_slice;
        } else {
            PyObject* owned_start = NULL;
            PyObject* owned_stop = NULL;
            if (_py_start) {
                py_start = *_py_start;
            } else {
                if (has_cstart) {
                    owned_start = py_start = PyInt_FromSsize_t(cstart);
                    if (unlikely(!py_start)) goto bad;
                } else
                    py_start = Py_None;
            }
            if (_py_stop) {
                py_stop = *_py_stop;
            } else {
                if (has_cstop) {
                    owned_stop = py_stop = PyInt_FromSsize_t(cstop);
                    if (unlikely(!py_stop)) {
                        Py_XDECREF(owned_start);
                        goto bad;
                    }
                } else
                    py_stop = Py_None;
            }
            py_slice = PySlice_New(py_start, py_stop, Py_None);
            Py_XDECREF(owned_start);
            Py_XDECREF(owned_stop);
            if (unlikely(!py_slice)) goto bad;
        }
#if CYTHON_COMPILING_IN_CPYTHON
        result = mp->mp_subscript(obj, py_slice);
#else
        result = PyObject_GetItem(obj, py_slice);
#endif
        if (!_py_slice) {
            Py_DECREF(py_slice);
        }
        return result;
    }
    PyErr_Format(PyExc_TypeError,
        "'%.200s' object is unsliceable", Py_TYPE(obj)->tp_name);
bad:
    return NULL;
}

#if PY_MAJOR_VERSION < 3
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,
                        CYTHON_UNUSED PyObject *cause) {
    Py_XINCREF(type);
    if (!value || value == Py_None)
        value = NULL;
    else
        Py_INCREF(value);
    if (!tb || tb == Py_None)
        tb = NULL;
    else {
        Py_INCREF(tb);
        if (!PyTraceBack_Check(tb)) {
            PyErr_SetString(PyExc_TypeError,
                "raise: arg 3 must be a traceback or None");
            goto raise_error;
        }
    }
    #if PY_VERSION_HEX < 0x02050000
    if (PyClass_Check(type)) {
    #else
    if (PyType_Check(type)) {
    #endif
#if CYTHON_COMPILING_IN_PYPY
        if (!value) {
            Py_INCREF(Py_None);
            value = Py_None;
        }
#endif
        PyErr_NormalizeException(&type, &value, &tb);
    } else {
        if (value) {
            PyErr_SetString(PyExc_TypeError,
                "instance exception may not have a separate value");
            goto raise_error;
        }
        value = type;
        #if PY_VERSION_HEX < 0x02050000
        if (PyInstance_Check(type)) {
            type = (PyObject*) ((PyInstanceObject*)type)->in_class;
            Py_INCREF(type);
        } else {
            type = 0;
            PyErr_SetString(PyExc_TypeError,
                "raise: exception must be an old-style class or instance");
            goto raise_error;
        }
        #else
        type = (PyObject*) Py_TYPE(type);
        Py_INCREF(type);
        if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
            PyErr_SetString(PyExc_TypeError,
                "raise: exception class must be a subclass of BaseException");
            goto raise_error;
        }
        #endif
    }
    __Pyx_ErrRestore(type, value, tb);
    return;
raise_error:
    Py_XDECREF(value);
    Py_XDECREF(type);
    Py_XDECREF(tb);
    return;
}
#else /* Python 3+ */
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) {
    PyObject* owned_instance = NULL;
    if (tb == Py_None) {
        tb = 0;
    } else if (tb && !PyTraceBack_Check(tb)) {
        PyErr_SetString(PyExc_TypeError,
            "raise: arg 3 must be a traceback or None");
        goto bad;
    }
    if (value == Py_None)
        value = 0;
    if (PyExceptionInstance_Check(type)) {
        if (value) {
            PyErr_SetString(PyExc_TypeError,
                "instance exception may not have a separate value");
            goto bad;
        }
        value = type;
        type = (PyObject*) Py_TYPE(value);
    } else if (PyExceptionClass_Check(type)) {
        PyObject *args;
        if (!value)
            args = PyTuple_New(0);
        else if (PyTuple_Check(value)) {
            Py_INCREF(value);
            args = value;
        } else
            args = PyTuple_Pack(1, value);
        if (!args)
            goto bad;
        owned_instance = PyEval_CallObject(type, args);
        Py_DECREF(args);
        if (!owned_instance)
            goto bad;
        value = owned_instance;
        if (!PyExceptionInstance_Check(value)) {
            PyErr_Format(PyExc_TypeError,
                         "calling %R should have returned an instance of "
                         "BaseException, not %R",
                         type, Py_TYPE(value));
            goto bad;
        }
    } else {
        PyErr_SetString(PyExc_TypeError,
            "raise: exception class must be a subclass of BaseException");
        goto bad;
    }
#if PY_VERSION_HEX >= 0x03030000
    if (cause) {
#else
    if (cause && cause != Py_None) {
#endif
        PyObject *fixed_cause;
        if (cause == Py_None) {
            fixed_cause = NULL;
        } else if (PyExceptionClass_Check(cause)) {
            fixed_cause = PyObject_CallObject(cause, NULL);
            if (fixed_cause == NULL)
                goto bad;
        } else if (PyExceptionInstance_Check(cause)) {
            fixed_cause = cause;
            Py_INCREF(fixed_cause);
        } else {
            PyErr_SetString(PyExc_TypeError,
                            "exception causes must derive from "
                            "BaseException");
            goto bad;
        }
        PyException_SetCause(value, fixed_cause);
    }
    PyErr_SetObject(type, value);
    if (tb) {
        PyThreadState *tstate = PyThreadState_GET();
        PyObject* tmp_tb = tstate->curexc_traceback;
        if (tb != tmp_tb) {
            Py_INCREF(tb);
            tstate->curexc_traceback = tb;
            Py_XDECREF(tmp_tb);
        }
    }
bad:
    Py_XDECREF(owned_instance);
    return;
}
#endif

#if PY_MAJOR_VERSION >= 3
static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d) {
    return PyObject_CallMethodObjArgs(d, __pyx_n_s__keys, NULL);
}
#endif

static CYTHON_INLINE void __Pyx_RaiseUnboundLocalError(const char *varname) {
    PyErr_Format(PyExc_UnboundLocalError, "local variable '%s' referenced before assignment", varname);
}

static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
    int r;
    if (!j) return -1;
    r = PyObject_DelItem(o, j);
    Py_DECREF(j);
    return r;
}
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
                                               CYTHON_UNUSED int is_list, int wraparound) {
#if CYTHON_COMPILING_IN_PYPY
    if (is_list || PySequence_Check(o)) {
        return PySequence_DelItem(o, i);
    }
#else
    PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
    if (likely(m && m->sq_ass_item)) {
        if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
            Py_ssize_t l = m->sq_length(o);
            if (likely(l >= 0)) {
                i += l;
            } else {
                if (PyErr_ExceptionMatches(PyExc_OverflowError))
                    PyErr_Clear();
                else
                    return -1;
            }
        }
        return m->sq_ass_item(o, i, (PyObject *)NULL);
    }
#endif
    return __Pyx_DelItem_Generic(o, PyInt_FromSsize_t(i));
}

static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
    const char *name, int exact)
{
    if (!type) {
        PyErr_Format(PyExc_SystemError, "Missing type object");
        return 0;
    }
    if (none_allowed && obj == Py_None) return 1;
    else if (exact) {
        if (Py_TYPE(obj) == type) return 1;
    }
    else {
        if (PyObject_TypeCheck(obj, type)) return 1;
    }
    PyErr_Format(PyExc_TypeError,
        "Argument '%s' has incorrect type (expected %s, got %s)",
        name, type->tp_name, Py_TYPE(obj)->tp_name);
    return 0;
}

static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
    PyErr_Format(PyExc_ValueError,
                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
}

static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
    PyErr_Format(PyExc_ValueError,
                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%s to unpack",
                 index, (index == 1) ? "" : "s");
}

static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
}

static CYTHON_INLINE int __Pyx_IterFinish(void) {
#if CYTHON_COMPILING_IN_CPYTHON
    PyThreadState *tstate = PyThreadState_GET();
    PyObject* exc_type = tstate->curexc_type;
    if (unlikely(exc_type)) {
        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
            PyObject *exc_value, *exc_tb;
            exc_value = tstate->curexc_value;
            exc_tb = tstate->curexc_traceback;
            tstate->curexc_type = 0;
            tstate->curexc_value = 0;
            tstate->curexc_traceback = 0;
            Py_DECREF(exc_type);
            Py_XDECREF(exc_value);
            Py_XDECREF(exc_tb);
            return 0;
        } else {
            return -1;
        }
    }
    return 0;
#else
    if (unlikely(PyErr_Occurred())) {
        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
            PyErr_Clear();
            return 0;
        } else {
            return -1;
        }
    }
    return 0;
#endif
}

static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
    if (unlikely(retval)) {
        Py_DECREF(retval);
        __Pyx_RaiseTooManyValuesError(expected);
        return -1;
    } else {
        return __Pyx_IterFinish();
    }
    return 0;
}

#if PY_MAJOR_VERSION < 3
static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {
  #if PY_VERSION_HEX >= 0x02060000
    if (PyObject_CheckBuffer(obj)) return PyObject_GetBuffer(obj, view, flags);
  #endif
        if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) return __pyx_pw_5numpy_7ndarray_1__getbuffer__(obj, view, flags);
  #if PY_VERSION_HEX < 0x02060000
    if (obj->ob_type->tp_dict) {
        PyObject *getbuffer_cobj = PyObject_GetItem(
            obj->ob_type->tp_dict, __pyx_n_s____pyx_getbuffer);
        if (getbuffer_cobj) {
            getbufferproc func = (getbufferproc) PyCObject_AsVoidPtr(getbuffer_cobj);
            Py_DECREF(getbuffer_cobj);
            if (!func)
                goto fail;
            return func(obj, view, flags);
        } else {
            PyErr_Clear();
        }
    }
  #endif
    PyErr_Format(PyExc_TypeError, "'%100s' does not have the buffer interface", Py_TYPE(obj)->tp_name);
#if PY_VERSION_HEX < 0x02060000
fail:
#endif
    return -1;
}
static void __Pyx_ReleaseBuffer(Py_buffer *view) {
    PyObject *obj = view->obj;
    if (!obj) return;
  #if PY_VERSION_HEX >= 0x02060000
    if (PyObject_CheckBuffer(obj)) {
        PyBuffer_Release(view);
        return;
    }
  #endif
        if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) { __pyx_pw_5numpy_7ndarray_3__releasebuffer__(obj, view); return; }
  #if PY_VERSION_HEX < 0x02060000
    if (obj->ob_type->tp_dict) {
        PyObject *releasebuffer_cobj = PyObject_GetItem(
            obj->ob_type->tp_dict, __pyx_n_s____pyx_releasebuffer);
        if (releasebuffer_cobj) {
            releasebufferproc func = (releasebufferproc) PyCObject_AsVoidPtr(releasebuffer_cobj);
            Py_DECREF(releasebuffer_cobj);
            if (!func)
                goto fail;
            func(obj, view);
            return;
        } else {
            PyErr_Clear();
        }
    }
  #endif
    goto nofail;
#if PY_VERSION_HEX < 0x02060000
fail:
#endif
    PyErr_WriteUnraisable(obj);
nofail:
    Py_DECREF(obj);
    view->obj = NULL;
}
#endif /*  PY_MAJOR_VERSION < 3 */


          static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
    PyObject *empty_list = 0;
    PyObject *module = 0;
    PyObject *global_dict = 0;
    PyObject *empty_dict = 0;
    PyObject *list;
    #if PY_VERSION_HEX < 0x03030000
    PyObject *py_import;
    py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s____import__);
    if (!py_import)
        goto bad;
    #endif
    if (from_list)
        list = from_list;
    else {
        empty_list = PyList_New(0);
        if (!empty_list)
            goto bad;
        list = empty_list;
    }
    global_dict = PyModule_GetDict(__pyx_m);
    if (!global_dict)
        goto bad;
    empty_dict = PyDict_New();
    if (!empty_dict)
        goto bad;
    #if PY_VERSION_HEX >= 0x02050000
    {
        #if PY_MAJOR_VERSION >= 3
        if (level == -1) {
            if (strchr(__Pyx_MODULE_NAME, '.')) {
                #if PY_VERSION_HEX < 0x03030000
                PyObject *py_level = PyInt_FromLong(1);
                if (!py_level)
                    goto bad;
                module = PyObject_CallFunctionObjArgs(py_import,
                    name, global_dict, empty_dict, list, py_level, NULL);
                Py_DECREF(py_level);
                #else
                module = PyImport_ImportModuleLevelObject(
                    name, global_dict, empty_dict, list, 1);
                #endif
                if (!module) {
                    if (!PyErr_ExceptionMatches(PyExc_ImportError))
                        goto bad;
                    PyErr_Clear();
                }
            }
            level = 0; /* try absolute import on failure */
        }
        #endif
        if (!module) {
            #if PY_VERSION_HEX < 0x03030000
            PyObject *py_level = PyInt_FromLong(level);
            if (!py_level)
                goto bad;
            module = PyObject_CallFunctionObjArgs(py_import,
                name, global_dict, empty_dict, list, py_level, NULL);
            Py_DECREF(py_level);
            #else
            module = PyImport_ImportModuleLevelObject(
                name, global_dict, empty_dict, list, level);
            #endif
        }
    }
    #else
    if (level>0) {
        PyErr_SetString(PyExc_RuntimeError, "Relative import is not supported for Python <=2.4.");
        goto bad;
    }
    module = PyObject_CallFunctionObjArgs(py_import,
        name, global_dict, empty_dict, list, NULL);
    #endif
bad:
    #if PY_VERSION_HEX < 0x03030000
    Py_XDECREF(py_import);
    #endif
    Py_XDECREF(empty_list);
    Py_XDECREF(empty_dict);
    return module;
}

#if CYTHON_CCOMPLEX
  #ifdef __cplusplus
    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {
      return ::std::complex< float >(x, y);
    }
  #else
    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {
      return x + y*(__pyx_t_float_complex)_Complex_I;
    }
  #endif
#else
    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {
      __pyx_t_float_complex z;
      z.real = x;
      z.imag = y;
      return z;
    }
#endif

#if CYTHON_CCOMPLEX
#else
    static CYTHON_INLINE int __Pyx_c_eqf(__pyx_t_float_complex a, __pyx_t_float_complex b) {
       return (a.real == b.real) && (a.imag == b.imag);
    }
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sumf(__pyx_t_float_complex a, __pyx_t_float_complex b) {
        __pyx_t_float_complex z;
        z.real = a.real + b.real;
        z.imag = a.imag + b.imag;
        return z;
    }
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_difff(__pyx_t_float_complex a, __pyx_t_float_complex b) {
        __pyx_t_float_complex z;
        z.real = a.real - b.real;
        z.imag = a.imag - b.imag;
        return z;
    }
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prodf(__pyx_t_float_complex a, __pyx_t_float_complex b) {
        __pyx_t_float_complex z;
        z.real = a.real * b.real - a.imag * b.imag;
        z.imag = a.real * b.imag + a.imag * b.real;
        return z;
    }
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quotf(__pyx_t_float_complex a, __pyx_t_float_complex b) {
        __pyx_t_float_complex z;
        float denom = b.real * b.real + b.imag * b.imag;
        z.real = (a.real * b.real + a.imag * b.imag) / denom;
        z.imag = (a.imag * b.real - a.real * b.imag) / denom;
        return z;
    }
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_negf(__pyx_t_float_complex a) {
        __pyx_t_float_complex z;
        z.real = -a.real;
        z.imag = -a.imag;
        return z;
    }
    static CYTHON_INLINE int __Pyx_c_is_zerof(__pyx_t_float_complex a) {
       return (a.real == 0) && (a.imag == 0);
    }
    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conjf(__pyx_t_float_complex a) {
        __pyx_t_float_complex z;
        z.real =  a.real;
        z.imag = -a.imag;
        return z;
    }
    #if 1
        static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex z) {
          #if !defined(HAVE_HYPOT) || defined(_MSC_VER)
            return sqrtf(z.real*z.real + z.imag*z.imag);
          #else
            return hypotf(z.real, z.imag);
          #endif
        }
        static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_powf(__pyx_t_float_complex a, __pyx_t_float_complex b) {
            __pyx_t_float_complex z;
            float r, lnr, theta, z_r, z_theta;
            if (b.imag == 0 && b.real == (int)b.real) {
                if (b.real < 0) {
                    float denom = a.real * a.real + a.imag * a.imag;
                    a.real = a.real / denom;
                    a.imag = -a.imag / denom;
                    b.real = -b.real;
                }
                switch ((int)b.real) {
                    case 0:
                        z.real = 1;
                        z.imag = 0;
                        return z;
                    case 1:
                        return a;
                    case 2:
                        z = __Pyx_c_prodf(a, a);
                        return __Pyx_c_prodf(a, a);
                    case 3:
                        z = __Pyx_c_prodf(a, a);
                        return __Pyx_c_prodf(z, a);
                    case 4:
                        z = __Pyx_c_prodf(a, a);
                        return __Pyx_c_prodf(z, z);
                }
            }
            if (a.imag == 0) {
                if (a.real == 0) {
                    return a;
                }
                r = a.real;
                theta = 0;
            } else {
                r = __Pyx_c_absf(a);
                theta = atan2f(a.imag, a.real);
            }
            lnr = logf(r);
            z_r = expf(lnr * b.real - theta * b.imag);
            z_theta = theta * b.real + lnr * b.imag;
            z.real = z_r * cosf(z_theta);
            z.imag = z_r * sinf(z_theta);
            return z;
        }
    #endif
#endif

#if CYTHON_CCOMPLEX
  #ifdef __cplusplus
    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {
      return ::std::complex< double >(x, y);
    }
  #else
    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {
      return x + y*(__pyx_t_double_complex)_Complex_I;
    }
  #endif
#else
    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {
      __pyx_t_double_complex z;
      z.real = x;
      z.imag = y;
      return z;
    }
#endif

#if CYTHON_CCOMPLEX
#else
    static CYTHON_INLINE int __Pyx_c_eq(__pyx_t_double_complex a, __pyx_t_double_complex b) {
       return (a.real == b.real) && (a.imag == b.imag);
    }
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum(__pyx_t_double_complex a, __pyx_t_double_complex b) {
        __pyx_t_double_complex z;
        z.real = a.real + b.real;
        z.imag = a.imag + b.imag;
        return z;
    }
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff(__pyx_t_double_complex a, __pyx_t_double_complex b) {
        __pyx_t_double_complex z;
        z.real = a.real - b.real;
        z.imag = a.imag - b.imag;
        return z;
    }
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod(__pyx_t_double_complex a, __pyx_t_double_complex b) {
        __pyx_t_double_complex z;
        z.real = a.real * b.real - a.imag * b.imag;
        z.imag = a.real * b.imag + a.imag * b.real;
        return z;
    }
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot(__pyx_t_double_complex a, __pyx_t_double_complex b) {
        __pyx_t_double_complex z;
        double denom = b.real * b.real + b.imag * b.imag;
        z.real = (a.real * b.real + a.imag * b.imag) / denom;
        z.imag = (a.imag * b.real - a.real * b.imag) / denom;
        return z;
    }
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg(__pyx_t_double_complex a) {
        __pyx_t_double_complex z;
        z.real = -a.real;
        z.imag = -a.imag;
        return z;
    }
    static CYTHON_INLINE int __Pyx_c_is_zero(__pyx_t_double_complex a) {
       return (a.real == 0) && (a.imag == 0);
    }
    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj(__pyx_t_double_complex a) {
        __pyx_t_double_complex z;
        z.real =  a.real;
        z.imag = -a.imag;
        return z;
    }
    #if 1
        static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex z) {
          #if !defined(HAVE_HYPOT) || defined(_MSC_VER)
            return sqrt(z.real*z.real + z.imag*z.imag);
          #else
            return hypot(z.real, z.imag);
          #endif
        }
        static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow(__pyx_t_double_complex a, __pyx_t_double_complex b) {
            __pyx_t_double_complex z;
            double r, lnr, theta, z_r, z_theta;
            if (b.imag == 0 && b.real == (int)b.real) {
                if (b.real < 0) {
                    double denom = a.real * a.real + a.imag * a.imag;
                    a.real = a.real / denom;
                    a.imag = -a.imag / denom;
                    b.real = -b.real;
                }
                switch ((int)b.real) {
                    case 0:
                        z.real = 1;
                        z.imag = 0;
                        return z;
                    case 1:
                        return a;
                    case 2:
                        z = __Pyx_c_prod(a, a);
                        return __Pyx_c_prod(a, a);
                    case 3:
                        z = __Pyx_c_prod(a, a);
                        return __Pyx_c_prod(z, a);
                    case 4:
                        z = __Pyx_c_prod(a, a);
                        return __Pyx_c_prod(z, z);
                }
            }
            if (a.imag == 0) {
                if (a.real == 0) {
                    return a;
                }
                r = a.real;
                theta = 0;
            } else {
                r = __Pyx_c_abs(a);
                theta = atan2(a.imag, a.real);
            }
            lnr = log(r);
            z_r = exp(lnr * b.real - theta * b.imag);
            z_theta = theta * b.real + lnr * b.imag;
            z.real = z_r * cos(z_theta);
            z.imag = z_r * sin(z_theta);
            return z;
        }
    #endif
#endif

static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject* x) {
    const unsigned char neg_one = (unsigned char)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(unsigned char) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(unsigned char)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to unsigned char" :
                    "value too large to convert to unsigned char");
            }
            return (unsigned char)-1;
        }
        return (unsigned char)val;
    }
    return (unsigned char)__Pyx_PyInt_AsUnsignedLong(x);
}

static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject* x) {
    const unsigned short neg_one = (unsigned short)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(unsigned short) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(unsigned short)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to unsigned short" :
                    "value too large to convert to unsigned short");
            }
            return (unsigned short)-1;
        }
        return (unsigned short)val;
    }
    return (unsigned short)__Pyx_PyInt_AsUnsignedLong(x);
}

static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject* x) {
    const unsigned int neg_one = (unsigned int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(unsigned int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(unsigned int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to unsigned int" :
                    "value too large to convert to unsigned int");
            }
            return (unsigned int)-1;
        }
        return (unsigned int)val;
    }
    return (unsigned int)__Pyx_PyInt_AsUnsignedLong(x);
}

static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject* x) {
    const char neg_one = (char)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(char) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(char)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to char" :
                    "value too large to convert to char");
            }
            return (char)-1;
        }
        return (char)val;
    }
    return (char)__Pyx_PyInt_AsLong(x);
}

static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject* x) {
    const short neg_one = (short)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(short) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(short)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to short" :
                    "value too large to convert to short");
            }
            return (short)-1;
        }
        return (short)val;
    }
    return (short)__Pyx_PyInt_AsLong(x);
}

static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject* x) {
    const int neg_one = (int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to int" :
                    "value too large to convert to int");
            }
            return (int)-1;
        }
        return (int)val;
    }
    return (int)__Pyx_PyInt_AsLong(x);
}

static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject* x) {
    const signed char neg_one = (signed char)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(signed char) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(signed char)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to signed char" :
                    "value too large to convert to signed char");
            }
            return (signed char)-1;
        }
        return (signed char)val;
    }
    return (signed char)__Pyx_PyInt_AsSignedLong(x);
}

static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject* x) {
    const signed short neg_one = (signed short)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(signed short) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(signed short)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to signed short" :
                    "value too large to convert to signed short");
            }
            return (signed short)-1;
        }
        return (signed short)val;
    }
    return (signed short)__Pyx_PyInt_AsSignedLong(x);
}

static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject* x) {
    const signed int neg_one = (signed int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(signed int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(signed int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to signed int" :
                    "value too large to convert to signed int");
            }
            return (signed int)-1;
        }
        return (signed int)val;
    }
    return (signed int)__Pyx_PyInt_AsSignedLong(x);
}

static CYTHON_INLINE int __Pyx_PyInt_AsLongDouble(PyObject* x) {
    const int neg_one = (int)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (sizeof(int) < sizeof(long)) {
        long val = __Pyx_PyInt_AsLong(x);
        if (unlikely(val != (long)(int)val)) {
            if (!unlikely(val == -1 && PyErr_Occurred())) {
                PyErr_SetString(PyExc_OverflowError,
                    (is_unsigned && unlikely(val < 0)) ?
                    "can't convert negative value to int" :
                    "value too large to convert to int");
            }
            return (int)-1;
        }
        return (int)val;
    }
    return (int)__Pyx_PyInt_AsLong(x);
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject* x) {
    const unsigned long neg_one = (unsigned long)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to unsigned long");
            return (unsigned long)-1;
        }
        return (unsigned long)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(unsigned long)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return (unsigned long) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to unsigned long");
                return (unsigned long)-1;
            }
            return (unsigned long)PyLong_AsUnsignedLong(x);
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(unsigned long)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +(unsigned long) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -(unsigned long) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            return (unsigned long)PyLong_AsLong(x);
        }
    } else {
        unsigned long val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (unsigned long)-1;
        val = __Pyx_PyInt_AsUnsignedLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject* x) {
    const unsigned PY_LONG_LONG neg_one = (unsigned PY_LONG_LONG)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to unsigned PY_LONG_LONG");
            return (unsigned PY_LONG_LONG)-1;
        }
        return (unsigned PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(unsigned PY_LONG_LONG)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return (unsigned PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to unsigned PY_LONG_LONG");
                return (unsigned PY_LONG_LONG)-1;
            }
            return (unsigned PY_LONG_LONG)PyLong_AsUnsignedLongLong(x);
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(unsigned PY_LONG_LONG)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +(unsigned PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -(unsigned PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            return (unsigned PY_LONG_LONG)PyLong_AsLongLong(x);
        }
    } else {
        unsigned PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (unsigned PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsUnsignedLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject* x) {
    const long neg_one = (long)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to long");
            return (long)-1;
        }
        return (long)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(long)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return (long) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to long");
                return (long)-1;
            }
            return (long)PyLong_AsUnsignedLong(x);
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(long)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +(long) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -(long) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            return (long)PyLong_AsLong(x);
        }
    } else {
        long val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (long)-1;
        val = __Pyx_PyInt_AsLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject* x) {
    const PY_LONG_LONG neg_one = (PY_LONG_LONG)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to PY_LONG_LONG");
            return (PY_LONG_LONG)-1;
        }
        return (PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(PY_LONG_LONG)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return (PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to PY_LONG_LONG");
                return (PY_LONG_LONG)-1;
            }
            return (PY_LONG_LONG)PyLong_AsUnsignedLongLong(x);
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(PY_LONG_LONG)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +(PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -(PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            return (PY_LONG_LONG)PyLong_AsLongLong(x);
        }
    } else {
        PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject* x) {
    const signed long neg_one = (signed long)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to signed long");
            return (signed long)-1;
        }
        return (signed long)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(signed long)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return (signed long) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to signed long");
                return (signed long)-1;
            }
            return (signed long)PyLong_AsUnsignedLong(x);
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(signed long)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +(signed long) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -(signed long) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            return (signed long)PyLong_AsLong(x);
        }
    } else {
        signed long val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (signed long)-1;
        val = __Pyx_PyInt_AsSignedLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject* x) {
    const signed PY_LONG_LONG neg_one = (signed PY_LONG_LONG)-1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (is_unsigned && unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to signed PY_LONG_LONG");
            return (signed PY_LONG_LONG)-1;
        }
        return (signed PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(signed PY_LONG_LONG)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return (signed PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to signed PY_LONG_LONG");
                return (signed PY_LONG_LONG)-1;
            }
            return (signed PY_LONG_LONG)PyLong_AsUnsignedLongLong(x);
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
            if (sizeof(digit) <= sizeof(signed PY_LONG_LONG)) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +(signed PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -(signed PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0];
                }
            }
#endif
#endif
            return (signed PY_LONG_LONG)PyLong_AsLongLong(x);
        }
    } else {
        signed PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (signed PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsSignedLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}

static int __Pyx_check_binary_version(void) {
    char ctversion[4], rtversion[4];
    PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION);
    PyOS_snprintf(rtversion, 4, "%s", Py_GetVersion());
    if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) {
        char message[200];
        PyOS_snprintf(message, sizeof(message),
                      "compiletime version %s of module '%.100s' "
                      "does not match runtime version %s",
                      ctversion, __Pyx_MODULE_NAME, rtversion);
        #if PY_VERSION_HEX < 0x02050000
        return PyErr_Warn(NULL, message);
        #else
        return PyErr_WarnEx(NULL, message, 1);
        #endif
    }
    return 0;
}

#ifndef __PYX_HAVE_RT_ImportModule
#define __PYX_HAVE_RT_ImportModule
static PyObject *__Pyx_ImportModule(const char *name) {
    PyObject *py_name = 0;
    PyObject *py_module = 0;
    py_name = __Pyx_PyIdentifier_FromString(name);
    if (!py_name)
        goto bad;
    py_module = PyImport_Import(py_name);
    Py_DECREF(py_name);
    return py_module;
bad:
    Py_XDECREF(py_name);
    return 0;
}
#endif

#ifndef __PYX_HAVE_RT_ImportType
#define __PYX_HAVE_RT_ImportType
static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
    size_t size, int strict)
{
    PyObject *py_module = 0;
    PyObject *result = 0;
    PyObject *py_name = 0;
    char warning[200];
    Py_ssize_t basicsize;
#ifdef Py_LIMITED_API
    PyObject *py_basicsize;
#endif
    py_module = __Pyx_ImportModule(module_name);
    if (!py_module)
        goto bad;
    py_name = __Pyx_PyIdentifier_FromString(class_name);
    if (!py_name)
        goto bad;
    result = PyObject_GetAttr(py_module, py_name);
    Py_DECREF(py_name);
    py_name = 0;
    Py_DECREF(py_module);
    py_module = 0;
    if (!result)
        goto bad;
    if (!PyType_Check(result)) {
        PyErr_Format(PyExc_TypeError,
            "%s.%s is not a type object",
            module_name, class_name);
        goto bad;
    }
#ifndef Py_LIMITED_API
    basicsize = ((PyTypeObject *)result)->tp_basicsize;
#else
    py_basicsize = PyObject_GetAttrString(result, "__basicsize__");
    if (!py_basicsize)
        goto bad;
    basicsize = PyLong_AsSsize_t(py_basicsize);
    Py_DECREF(py_basicsize);
    py_basicsize = 0;
    if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred())
        goto bad;
#endif
    if (!strict && (size_t)basicsize > size) {
        PyOS_snprintf(warning, sizeof(warning),
            "%s.%s size changed, may indicate binary incompatibility",
            module_name, class_name);
        #if PY_VERSION_HEX < 0x02050000
        if (PyErr_Warn(NULL, warning) < 0) goto bad;
        #else
        if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad;
        #endif
    }
    else if ((size_t)basicsize != size) {
        PyErr_Format(PyExc_ValueError,
            "%s.%s has the wrong size, try recompiling",
            module_name, class_name);
        goto bad;
    }
    return (PyTypeObject *)result;
bad:
    Py_XDECREF(py_module);
    Py_XDECREF(result);
    return NULL;
}
#endif

static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) {
    int start = 0, mid = 0, end = count - 1;
    if (end >= 0 && code_line > entries[end].code_line) {
        return count;
    }
    while (start < end) {
        mid = (start + end) / 2;
        if (code_line < entries[mid].code_line) {
            end = mid;
        } else if (code_line > entries[mid].code_line) {
             start = mid + 1;
        } else {
            return mid;
        }
    }
    if (code_line <= entries[mid].code_line) {
        return mid;
    } else {
        return mid + 1;
    }
}
static PyCodeObject *__pyx_find_code_object(int code_line) {
    PyCodeObject* code_object;
    int pos;
    if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) {
        return NULL;
    }
    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);
    if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) {
        return NULL;
    }
    code_object = __pyx_code_cache.entries[pos].code_object;
    Py_INCREF(code_object);
    return code_object;
}
static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) {
    int pos, i;
    __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;
    if (unlikely(!code_line)) {
        return;
    }
    if (unlikely(!entries)) {
        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry));
        if (likely(entries)) {
            __pyx_code_cache.entries = entries;
            __pyx_code_cache.max_count = 64;
            __pyx_code_cache.count = 1;
            entries[0].code_line = code_line;
            entries[0].code_object = code_object;
            Py_INCREF(code_object);
        }
        return;
    }
    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);
    if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) {
        PyCodeObject* tmp = entries[pos].code_object;
        entries[pos].code_object = code_object;
        Py_DECREF(tmp);
        return;
    }
    if (__pyx_code_cache.count == __pyx_code_cache.max_count) {
        int new_max = __pyx_code_cache.max_count + 64;
        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc(
            __pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry));
        if (unlikely(!entries)) {
            return;
        }
        __pyx_code_cache.entries = entries;
        __pyx_code_cache.max_count = new_max;
    }
    for (i=__pyx_code_cache.count; i>pos; i--) {
        entries[i] = entries[i-1];
    }
    entries[pos].code_line = code_line;
    entries[pos].code_object = code_object;
    __pyx_code_cache.count++;
    Py_INCREF(code_object);
}

#include "compile.h"
#include "frameobject.h"
#include "traceback.h"
static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
            const char *funcname, int c_line,
            int py_line, const char *filename) {
    PyCodeObject *py_code = 0;
    PyObject *py_srcfile = 0;
    PyObject *py_funcname = 0;
    #if PY_MAJOR_VERSION < 3
    py_srcfile = PyString_FromString(filename);
    #else
    py_srcfile = PyUnicode_FromString(filename);
    #endif
    if (!py_srcfile) goto bad;
    if (c_line) {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line);
        #else
        py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line);
        #endif
    }
    else {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromString(funcname);
        #else
        py_funcname = PyUnicode_FromString(funcname);
        #endif
    }
    if (!py_funcname) goto bad;
    py_code = __Pyx_PyCode_New(
        0,            /*int argcount,*/
        0,            /*int kwonlyargcount,*/
        0,            /*int nlocals,*/
        0,            /*int stacksize,*/
        0,            /*int flags,*/
        __pyx_empty_bytes, /*PyObject *code,*/
        __pyx_empty_tuple, /*PyObject *consts,*/
        __pyx_empty_tuple, /*PyObject *names,*/
        __pyx_empty_tuple, /*PyObject *varnames,*/
        __pyx_empty_tuple, /*PyObject *freevars,*/
        __pyx_empty_tuple, /*PyObject *cellvars,*/
        py_srcfile,   /*PyObject *filename,*/
        py_funcname,  /*PyObject *name,*/
        py_line,      /*int firstlineno,*/
        __pyx_empty_bytes  /*PyObject *lnotab*/
    );
    Py_DECREF(py_srcfile);
    Py_DECREF(py_funcname);
    return py_code;
bad:
    Py_XDECREF(py_srcfile);
    Py_XDECREF(py_funcname);
    return NULL;
}
static void __Pyx_AddTraceback(const char *funcname, int c_line,
                               int py_line, const char *filename) {
    PyCodeObject *py_code = 0;
    PyObject *py_globals = 0;
    PyFrameObject *py_frame = 0;
    py_code = __pyx_find_code_object(c_line ? c_line : py_line);
    if (!py_code) {
        py_code = __Pyx_CreateCodeObjectForTraceback(
            funcname, c_line, py_line, filename);
        if (!py_code) goto bad;
        __pyx_insert_code_object(c_line ? c_line : py_line, py_code);
    }
    py_globals = PyModule_GetDict(__pyx_m);
    if (!py_globals) goto bad;
    py_frame = PyFrame_New(
        PyThreadState_GET(), /*PyThreadState *tstate,*/
        py_code,             /*PyCodeObject *code,*/
        py_globals,          /*PyObject *globals,*/
        0                    /*PyObject *locals*/
    );
    if (!py_frame) goto bad;
    py_frame->f_lineno = py_line;
    PyTraceBack_Here(py_frame);
bad:
    Py_XDECREF(py_code);
    Py_XDECREF(py_frame);
}

static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
    while (t->p) {
        #if PY_MAJOR_VERSION < 3
        if (t->is_unicode) {
            *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);
        } else if (t->intern) {
            *t->p = PyString_InternFromString(t->s);
        } else {
            *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
        }
        #else  /* Python 3+ has unicode identifiers */
        if (t->is_unicode | t->is_str) {
            if (t->intern) {
                *t->p = PyUnicode_InternFromString(t->s);
            } else if (t->encoding) {
                *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL);
            } else {
                *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);
            }
        } else {
            *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);
        }
        #endif
        if (!*t->p)
            return -1;
        ++t;
    }
    return 0;
}

static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) {
    return __Pyx_PyUnicode_FromStringAndSize(c_str, strlen(c_str));
}
static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {
    Py_ssize_t ignore;
    return __Pyx_PyObject_AsStringAndSize(o, &ignore);
}
static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
    if (
#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
            __Pyx_sys_getdefaultencoding_not_ascii &&
#endif
            PyUnicode_Check(o)) {
#if PY_VERSION_HEX < 0x03030000
        char* defenc_c;
        PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);
        if (!defenc) return NULL;
        defenc_c = PyBytes_AS_STRING(defenc);
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
        {
            char* end = defenc_c + PyBytes_GET_SIZE(defenc);
            char* c;
            for (c = defenc_c; c < end; c++) {
                if ((unsigned char) (*c) >= 128) {
                    PyUnicode_AsASCIIString(o);
                    return NULL;
                }
            }
        }
#endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/
        *length = PyBytes_GET_SIZE(defenc);
        return defenc_c;
#else /* PY_VERSION_HEX < 0x03030000 */
        if (PyUnicode_READY(o) == -1) return NULL;
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
        if (PyUnicode_IS_ASCII(o)) {
            *length = PyUnicode_GET_DATA_SIZE(o);
            return PyUnicode_AsUTF8(o);
        } else {
            PyUnicode_AsASCIIString(o);
            return NULL;
        }
#else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
        return PyUnicode_AsUTF8AndSize(o, length);
#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
#endif /* PY_VERSION_HEX < 0x03030000 */
    } else
#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII  || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */
    {
        char* result;
        int r = PyBytes_AsStringAndSize(o, &result, length);
        if (r < 0) {
            return NULL;
        } else {
            return result;
        }
    }
}
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
   int is_true = x == Py_True;
   if (is_true | (x == Py_False) | (x == Py_None)) return is_true;
   else return PyObject_IsTrue(x);
}
static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {
  PyNumberMethods *m;
  const char *name = NULL;
  PyObject *res = NULL;
#if PY_MAJOR_VERSION < 3
  if (PyInt_Check(x) || PyLong_Check(x))
#else
  if (PyLong_Check(x))
#endif
    return Py_INCREF(x), x;
  m = Py_TYPE(x)->tp_as_number;
#if PY_MAJOR_VERSION < 3
  if (m && m->nb_int) {
    name = "int";
    res = PyNumber_Int(x);
  }
  else if (m && m->nb_long) {
    name = "long";
    res = PyNumber_Long(x);
  }
#else
  if (m && m->nb_int) {
    name = "int";
    res = PyNumber_Long(x);
  }
#endif
  if (res) {
#if PY_MAJOR_VERSION < 3
    if (!PyInt_Check(res) && !PyLong_Check(res)) {
#else
    if (!PyLong_Check(res)) {
#endif
      PyErr_Format(PyExc_TypeError,
                   "__%s__ returned non-%s (type %.200s)",
                   name, name, Py_TYPE(res)->tp_name);
      Py_DECREF(res);
      return NULL;
    }
  }
  else if (!PyErr_Occurred()) {
    PyErr_SetString(PyExc_TypeError,
                    "an integer is required");
  }
  return res;
}
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
  Py_ssize_t ival;
  PyObject* x = PyNumber_Index(b);
  if (!x) return -1;
  ival = PyInt_AsSsize_t(x);
  Py_DECREF(x);
  return ival;
}
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
#if PY_VERSION_HEX < 0x02050000
   if (ival <= LONG_MAX)
       return PyInt_FromLong((long)ival);
   else {
       unsigned char *bytes = (unsigned char *) &ival;
       int one = 1; int little = (int)*(unsigned char*)&one;
       return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0);
   }
#else
   return PyInt_FromSize_t(ival);
#endif
}
static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) {
   unsigned PY_LONG_LONG val = __Pyx_PyInt_AsUnsignedLongLong(x);
   if (unlikely(val != (unsigned PY_LONG_LONG)(size_t)val)) {
       if ((val != (unsigned PY_LONG_LONG)-1) || !PyErr_Occurred())
           PyErr_SetString(PyExc_OverflowError,
                           "value too large to convert to size_t");
       return (size_t)-1;
   }
   return (size_t)val;
}


#endif /* Py_PYTHON_H */
