/* Generated by Cython 0.13 on Sun Feb 27 00:36:15 2011 */

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifndef Py_PYTHON_H
    #error Python headers needed to compile C extensions, please install development version of Python.
#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

#if PY_VERSION_HEX < 0x02040000
  #define METH_COEXIST 0
  #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
  #define PyDict_Contains(d,o)   PySequence_Contains(d,o)
#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 PyInt_FromSsize_t(z) PyInt_FromLong(z)
  #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
  #define PyNumber_Index(o)    PyNumber_Int(o)
  #define PyIndex_Check(o)     PyNumber_Check(o)
  #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
#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)

#endif

#if PY_MAJOR_VERSION < 3
  #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
#else
  #define __Pyx_BUILTIN_MODULE_NAME "builtins"
#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_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
  #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

#if PY_MAJOR_VERSION >= 3
  #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_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

#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

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

#if defined(WIN32) || defined(MS_WINDOWS)
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#define __PYX_HAVE_API__dipy__tracking__distances
#include "stdio.h"
#include "stdlib.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "math.h"

/* inline attribute */
#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

/* unused attribute */
#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)
#   define CYTHON_UNUSED __attribute__ ((__unused__)) 
# else
#   define CYTHON_UNUSED 
# endif
#endif

typedef struct {PyObject **p; char *s; const long n; const char* encoding; const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/


/* Type Conversion Predeclarations */

#define __Pyx_PyBytes_FromUString(s) PyBytes_FromString((char*)s)
#define __Pyx_PyBytes_AsUString(s)   ((unsigned char*) PyBytes_AsString(s))

#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*);

#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))


#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_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",
};

typedef npy_int8 __pyx_t_5numpy_int8_t;

typedef npy_int16 __pyx_t_5numpy_int16_t;

typedef npy_int32 __pyx_t_5numpy_int32_t;

typedef npy_int64 __pyx_t_5numpy_int64_t;

typedef npy_uint8 __pyx_t_5numpy_uint8_t;

typedef npy_uint16 __pyx_t_5numpy_uint16_t;

typedef npy_uint32 __pyx_t_5numpy_uint32_t;

typedef npy_uint64 __pyx_t_5numpy_uint64_t;

typedef npy_float32 __pyx_t_5numpy_float32_t;

typedef npy_float64 __pyx_t_5numpy_float64_t;

typedef npy_long __pyx_t_5numpy_int_t;

typedef npy_longlong __pyx_t_5numpy_long_t;

typedef npy_intp __pyx_t_5numpy_intp_t;

typedef npy_uintp __pyx_t_5numpy_uintp_t;

typedef npy_ulong __pyx_t_5numpy_uint_t;

typedef npy_ulonglong __pyx_t_5numpy_ulong_t;

typedef npy_double __pyx_t_5numpy_float_t;

typedef npy_double __pyx_t_5numpy_double_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 */

typedef npy_cfloat __pyx_t_5numpy_cfloat_t;

typedef npy_cdouble __pyx_t_5numpy_cdouble_t;

typedef npy_clongdouble __pyx_t_5numpy_clongdouble_t;

typedef npy_cdouble __pyx_t_5numpy_complex_t;

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1381
 * 
 * 
 * ctypedef struct LSC_Cluster:             # <<<<<<<<<<<<<<
 *     long *indices
 *     float *hidden
 */

typedef struct {
  long *indices;
  float *hidden;
  long N;
} __pyx_t_4dipy_8tracking_9distances_LSC_Cluster;

#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) {
    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;
  }
  #define __Pyx_RefNannySetupContext(name)           void *__pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
  #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_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r);} } while(0)
#else
  #define __Pyx_RefNannySetupContext(name)
  #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_XDECREF(r) Py_XDECREF(r)
#endif /* CYTHON_REFNANNY */
#define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);} } while(0)
#define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r);} } while(0)

static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/

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

/* Run-time type information about structs used with buffers */
struct __Pyx_StructField_;

typedef struct {
  const char* name; /* for error messages only */
  struct __Pyx_StructField_* fields;
  size_t size;     /* sizeof(type) */
  char typegroup; /* _R_eal, _C_omplex, Signed _I_nt, _U_nsigned int, _S_truct, _P_ointer, _O_bject */
} __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;


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*/

static void __Pyx_RaiseDoubleKeywordsError(
    const char* func_name, PyObject* kw_name); /*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 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*/


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;
}


#define __Pyx_GetItemInt_List(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \
                                                    __Pyx_GetItemInt_List_Fast(o, i) : \
                                                    __Pyx_GetItemInt_Generic(o, to_py_func(i)))

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i) {
    if (likely(o != Py_None)) {
        if (likely((0 <= i) & (i < PyList_GET_SIZE(o)))) {
            PyObject *r = PyList_GET_ITEM(o, i);
            Py_INCREF(r);
            return r;
        }
        else if ((-PyList_GET_SIZE(o) <= i) & (i < 0)) {
            PyObject *r = PyList_GET_ITEM(o, PyList_GET_SIZE(o) + i);
            Py_INCREF(r);
            return r;
        }
    }
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
}

#define __Pyx_GetItemInt_Tuple(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \
                                                    __Pyx_GetItemInt_Tuple_Fast(o, i) : \
                                                    __Pyx_GetItemInt_Generic(o, to_py_func(i)))

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i) {
    if (likely(o != Py_None)) {
        if (likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
            PyObject *r = PyTuple_GET_ITEM(o, i);
            Py_INCREF(r);
            return r;
        }
        else if ((-PyTuple_GET_SIZE(o) <= i) & (i < 0)) {
            PyObject *r = PyTuple_GET_ITEM(o, PyTuple_GET_SIZE(o) + i);
            Py_INCREF(r);
            return r;
        }
    }
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
}


#define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \
                                                    __Pyx_GetItemInt_Fast(o, i) : \
                                                    __Pyx_GetItemInt_Generic(o, to_py_func(i)))

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) {
    PyObject *r;
    if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
        r = PyList_GET_ITEM(o, i);
        Py_INCREF(r);
    }
    else if (PyTuple_CheckExact(o) && ((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
        r = PyTuple_GET_ITEM(o, i);
        Py_INCREF(r);
    }
    else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0))) {
        r = PySequence_GetItem(o, i);
    }
    else {
        r = __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
    }
    return r;
}
static void __Pyx_RaiseBufferIndexError(int axis); /*proto*/
#define __Pyx_BufPtrStrided1d(type, buf, i0, s0) (type)((char*)buf + i0 * s0)

static CYTHON_INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
    if (likely(PyList_CheckExact(L))) {
        if (PyList_Append(L, x) < 0) return NULL;
        Py_INCREF(Py_None);
        return Py_None; /* this is just to have an accurate signature */
    }
    else {
        PyObject *r, *m;
        m = __Pyx_GetAttrString(L, "append");
        if (!m) return NULL;
        r = PyObject_CallFunctionObjArgs(m, x, NULL);
        Py_DECREF(m);
        return r;
    }
}

#define __Pyx_SetItemInt(o, i, v, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \
                                                    __Pyx_SetItemInt_Fast(o, i, v) : \
                                                    __Pyx_SetItemInt_Generic(o, to_py_func(i), v))

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) {
    if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
        Py_INCREF(v);
        Py_DECREF(PyList_GET_ITEM(o, i));
        PyList_SET_ITEM(o, i, v);
        return 1;
    }
    else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0)))
        return PySequence_SetItem(o, i, v);
    else {
        PyObject *j = PyInt_FromSsize_t(i);
        return __Pyx_SetItemInt_Generic(o, j, v);
    }
}
#define __Pyx_BufPtrStrided2d(type, buf, i0, s0, i1, s1) (type)((char*)buf + i0 * s0 + i1 * s1)

static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void);


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

#define __Pyx_DelItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \
                                                    __Pyx_DelItemInt_Fast(o, i) : \
                                                    __Pyx_DelItem_Generic(o, to_py_func(i)))

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) {
    if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && likely(i >= 0))
        return PySequence_DelItem(o, i);
    else {
        PyObject *j = PyInt_FromSsize_t(i);
        return __Pyx_DelItem_Generic(o, j);
    }
}

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

static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);

static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);

static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);

static void __Pyx_UnpackTupleError(PyObject *, Py_ssize_t index); /*proto*/
#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

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

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

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

#ifndef __PYX_FORCE_INIT_THREADS
  #if PY_VERSION_HEX < 0x02040200
    #define __PYX_FORCE_INIT_THREADS 1
  #else
    #define __PYX_FORCE_INIT_THREADS 0
  #endif
#endif

#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))
    /*#define __Pyx_c_absf(z)     (::std::abs(z))*/
  #else
    #define __Pyx_c_is_zerof(z) ((z)==0)
    #define __Pyx_c_conjf(z)    (conjf(z))
    /*#define __Pyx_c_absf(z)     (cabsf(z))*/
 #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);
    /*static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex);*/
#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))
    /*#define __Pyx_c_abs(z)     (::std::abs(z))*/
  #else
    #define __Pyx_c_is_zero(z) ((z)==0)
    #define __Pyx_c_conj(z)    (conj(z))
    /*#define __Pyx_c_abs(z)     (cabs(z))*/
 #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);
    /*static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex);*/
#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 void __Pyx_WriteUnraisable(const char *name); /*proto*/

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

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

static void __Pyx_AddTraceback(const char *funcname); /*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.stdio */

/* Module declarations from cpython.object */

/* 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 PyObject *__pyx_f_5numpy_PyArray_MultiIterNew1(PyObject *); /*proto*/
static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew2(PyObject *, PyObject *); /*proto*/
static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew3(PyObject *, PyObject *, PyObject *); /*proto*/
static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew4(PyObject *, PyObject *, PyObject *, PyObject *); /*proto*/
static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew5(PyObject *, PyObject *, PyObject *, PyObject *, PyObject *); /*proto*/
static CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *, char *, char *, int *); /*proto*/
static CYTHON_INLINE void __pyx_f_5numpy_set_array_base(PyArrayObject *, PyObject *); /*proto*/
static CYTHON_INLINE PyObject *__pyx_f_5numpy_get_array_base(PyArrayObject *); /*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 CYTHON_INLINE void __pyx_f_4dipy_8tracking_9distances_track_direct_flip_3sq_dist(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), 'R' };
static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_uint64_t = { "uint64_t", NULL, sizeof(__pyx_t_5numpy_uint64_t), 'U' };
static __Pyx_TypeInfo __Pyx_TypeInfo_object = { "Python object", NULL, sizeof(PyObject *), 'O' };
static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_double_t = { "double_t", NULL, sizeof(__pyx_t_5numpy_double_t), 'R' };
static __Pyx_TypeInfo __Pyx_TypeInfo_float = { "float", NULL, sizeof(float), 'R' };
#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 char __pyx_k_1[] = "Metric should be one of avg, min, max";
static char __pyx_k_2[] = "Wrong argument for metric";
static char __pyx_k_7[] = "ndarray is not C contiguous";
static char __pyx_k_8[] = "ndarray is not Fortran contiguous";
static char __pyx_k_9[] = "Non-native byte order not supported";
static char __pyx_k_10[] = "unknown dtype code in numpy.pxd (%d)";
static char __pyx_k_11[] = "Format string allocated too short, see comment in numpy.pxd";
static char __pyx_k_12[] = "Format string allocated too short.";
static char __pyx_k_13[] = " Optimized track distances, similarities and clustering algorithms using track distances \n";
static char __pyx_k_14[] = "normalized_3vec (line 49)";
static char __pyx_k_15[] = "norm_3vec (line 68)";
static char __pyx_k_16[] = "cut_plane (line 193)";
static char __pyx_k_17[] = "most_similar_track_mam (line 350)";
static char __pyx_k_18[] = "most_similar_track_mam";
static char __pyx_k_19[] = "bundles_distances_mam (line 459)";
static char __pyx_k_20[] = "bundles_distances_mam";
static char __pyx_k_21[] = "mam_distances (line 619)";
static char __pyx_k_22[] = "minimum_closest_distance (line 705)";
static char __pyx_k_23[] = "minimum_closest_distance";
static char __pyx_k_24[] = "lee_perpendicular_distance (line 763)";
static char __pyx_k_25[] = "lee_perpendicular_distance";
static char __pyx_k_26[] = "lee_angle_distance (line 866)";
static char __pyx_k_27[] = "approx_polygon_track (line 935)";
static char __pyx_k_28[] = "approx_polygon_track";
static char __pyx_k_29[] = "approximate_mdl_trajectory (line 1018)";
static char __pyx_k_30[] = "approximate_mdl_trajectory";
static char __pyx_k_31[] = "intersect_segment_cylinder (line 1085)";
static char __pyx_k_32[] = "intersect_segment_cylinder";
static char __pyx_k_33[] = "point_segment_sq_distance (line 1188)";
static char __pyx_k_34[] = "point_segment_sq_distance";
static char __pyx_k_35[] = "track_dist_3pts (line 1244)";
static char __pyx_k_36[] = "local_skeleton_clustering (line 1392)";
static char __pyx_k_37[] = "local_skeleton_clustering";
static char __pyx_k_38[] = "local_skeleton_clustering_3pts (line 1577)";
static char __pyx_k_39[] = "local_skeleton_clustering_3pts";
static char __pyx_k_40[] = "larch_3split (line 1705)";
static char __pyx_k_41[] = "larch_3merge (line 1813)";
static char __pyx_k_42[] = "point_track_sq_distance_check (line 1871)";
static char __pyx_k_43[] = "point_track_sq_distance_check";
static char __pyx_k_44[] = "track_roi_intersection_check (line 1928)";
static char __pyx_k_45[] = "track_roi_intersection_check";
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__l[] = "l";
static char __pyx_k__p[] = "p";
static char __pyx_k__q[] = "q";
static char __pyx_k__r[] = "r";
static char __pyx_k__Zd[] = "Zd";
static char __pyx_k__Zf[] = "Zf";
static char __pyx_k__Zg[] = "Zg";
static char __pyx_k__np[] = "np";
static char __pyx_k__sa[] = "sa";
static char __pyx_k__sb[] = "sb";
static char __pyx_k__all[] = "all";
static char __pyx_k__avg[] = "avg";
static char __pyx_k__buf[] = "buf";
static char __pyx_k__inf[] = "inf";
static char __pyx_k__max[] = "max";
static char __pyx_k__min[] = "min";
static char __pyx_k__obj[] = "obj";
static char __pyx_k__ref[] = "ref";
static char __pyx_k__roi[] = "roi";
static char __pyx_k__thr[] = "thr";
static char __pyx_k__vec[] = "vec";
static char __pyx_k__xyz[] = "xyz";
static char __pyx_k__base[] = "base";
static char __pyx_k__copy[] = "copy";
static char __pyx_k__data[] = "data";
static char __pyx_k__end0[] = "end0";
static char __pyx_k__end1[] = "end1";
static char __pyx_k__keys[] = "keys";
static char __pyx_k__ndim[] = "ndim";
static char __pyx_k__rep3[] = "rep3";
static char __pyx_k__time[] = "time";
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__alpha[] = "alpha";
static char __pyx_k__array[] = "array";
static char __pyx_k__d_thr[] = "d_thr";
static char __pyx_k__descr[] = "descr";
static char __pyx_k__dtype[] = "dtype";
static char __pyx_k__empty[] = "empty";
static char __pyx_k__int64[] = "int64";
static char __pyx_k__names[] = "names";
static char __pyx_k__numpy[] = "numpy";
static char __pyx_k__point[] = "point";
static char __pyx_k__range[] = "range";
static char __pyx_k__shape[] = "shape";
static char __pyx_k__track[] = "track";
static char __pyx_k__zeros[] = "zeros";
static char __pyx_k__argmin[] = "argmin";
static char __pyx_k__double[] = "double";
static char __pyx_k__fields[] = "fields";
static char __pyx_k__format[] = "format";
static char __pyx_k__hidden[] = "hidden";
static char __pyx_k__metric[] = "metric";
static char __pyx_k__object[] = "object";
static char __pyx_k__start0[] = "start0";
static char __pyx_k__start1[] = "start1";
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__asarray[] = "asarray";
static char __pyx_k__float32[] = "float32";
static char __pyx_k__indices[] = "indices";
static char __pyx_k__squeeze[] = "squeeze";
static char __pyx_k__strides[] = "strides";
static char __pyx_k__tracksA[] = "tracksA";
static char __pyx_k__tracksB[] = "tracksB";
static char __pyx_k____main__[] = "__main__";
static char __pyx_k____test__[] = "__test__";
static char __pyx_k__itemsize[] = "itemsize";
static char __pyx_k__readonly[] = "readonly";
static char __pyx_k__type_num[] = "type_num";
static char __pyx_k__byteorder[] = "byteorder";
static char __pyx_k__cut_plane[] = "cut_plane";
static char __pyx_k__norm_3vec[] = "norm_3vec";
static char __pyx_k__ValueError[] = "ValueError";
static char __pyx_k__suboffsets[] = "suboffsets";
static char __pyx_k__sq_dist_thr[] = "sq_dist_thr";
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__mam_distances[] = "mam_distances";
static char __pyx_k__normalized_3vec[] = "normalized_3vec";
static char __pyx_k__track_dist_3pts[] = "track_dist_3pts";
static char __pyx_k__ascontiguousarray[] = "ascontiguousarray";
static char __pyx_k__lee_angle_distance[] = "lee_angle_distance";
static PyObject *__pyx_kp_s_1;
static PyObject *__pyx_kp_u_10;
static PyObject *__pyx_kp_u_11;
static PyObject *__pyx_kp_u_12;
static PyObject *__pyx_kp_u_14;
static PyObject *__pyx_kp_u_15;
static PyObject *__pyx_kp_u_16;
static PyObject *__pyx_kp_u_17;
static PyObject *__pyx_n_s_18;
static PyObject *__pyx_kp_u_19;
static PyObject *__pyx_kp_s_2;
static PyObject *__pyx_n_s_20;
static PyObject *__pyx_kp_u_21;
static PyObject *__pyx_kp_u_22;
static PyObject *__pyx_n_s_23;
static PyObject *__pyx_kp_u_24;
static PyObject *__pyx_n_s_25;
static PyObject *__pyx_kp_u_26;
static PyObject *__pyx_kp_u_27;
static PyObject *__pyx_n_s_28;
static PyObject *__pyx_kp_u_29;
static PyObject *__pyx_n_s_30;
static PyObject *__pyx_kp_u_31;
static PyObject *__pyx_n_s_32;
static PyObject *__pyx_kp_u_33;
static PyObject *__pyx_n_s_34;
static PyObject *__pyx_kp_u_35;
static PyObject *__pyx_kp_u_36;
static PyObject *__pyx_n_s_37;
static PyObject *__pyx_kp_u_38;
static PyObject *__pyx_n_s_39;
static PyObject *__pyx_kp_u_40;
static PyObject *__pyx_kp_u_41;
static PyObject *__pyx_kp_u_42;
static PyObject *__pyx_n_s_43;
static PyObject *__pyx_kp_u_44;
static PyObject *__pyx_n_s_45;
static PyObject *__pyx_kp_u_7;
static PyObject *__pyx_kp_u_8;
static PyObject *__pyx_kp_u_9;
static PyObject *__pyx_n_s__C;
static PyObject *__pyx_n_s__N;
static PyObject *__pyx_n_s__RuntimeError;
static PyObject *__pyx_n_s__ValueError;
static PyObject *__pyx_n_s____main__;
static PyObject *__pyx_n_s____test__;
static PyObject *__pyx_n_s__a;
static PyObject *__pyx_n_s__all;
static PyObject *__pyx_n_s__alpha;
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__base;
static PyObject *__pyx_n_s__buf;
static PyObject *__pyx_n_s__byteorder;
static PyObject *__pyx_n_s__c;
static PyObject *__pyx_n_s__copy;
static PyObject *__pyx_n_s__cut_plane;
static PyObject *__pyx_n_s__d_thr;
static PyObject *__pyx_n_s__data;
static PyObject *__pyx_n_s__descr;
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__fields;
static PyObject *__pyx_n_s__float32;
static PyObject *__pyx_n_s__format;
static PyObject *__pyx_n_s__hidden;
static PyObject *__pyx_n_s__indices;
static PyObject *__pyx_n_s__inf;
static PyObject *__pyx_n_s__int64;
static PyObject *__pyx_n_s__itemsize;
static PyObject *__pyx_n_s__keys;
static PyObject *__pyx_n_s__larch_3merge;
static PyObject *__pyx_n_s__larch_3split;
static PyObject *__pyx_n_s__lee_angle_distance;
static PyObject *__pyx_n_s__mam_distances;
static PyObject *__pyx_n_s__max;
static PyObject *__pyx_n_s__metric;
static PyObject *__pyx_n_s__min;
static PyObject *__pyx_n_s__names;
static PyObject *__pyx_n_s__ndim;
static PyObject *__pyx_n_s__norm_3vec;
static PyObject *__pyx_n_s__normalized_3vec;
static PyObject *__pyx_n_s__np;
static PyObject *__pyx_n_s__numpy;
static PyObject *__pyx_n_s__obj;
static PyObject *__pyx_n_s__object;
static PyObject *__pyx_n_s__p;
static PyObject *__pyx_n_s__point;
static PyObject *__pyx_n_s__q;
static PyObject *__pyx_n_s__r;
static PyObject *__pyx_n_s__range;
static PyObject *__pyx_n_s__readonly;
static PyObject *__pyx_n_s__ref;
static PyObject *__pyx_n_s__rep3;
static PyObject *__pyx_n_s__roi;
static PyObject *__pyx_n_s__sa;
static PyObject *__pyx_n_s__sb;
static PyObject *__pyx_n_s__shape;
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__strides;
static PyObject *__pyx_n_s__suboffsets;
static PyObject *__pyx_n_s__thr;
static PyObject *__pyx_n_s__time;
static PyObject *__pyx_n_s__track;
static PyObject *__pyx_n_s__track_dist_3pts;
static PyObject *__pyx_n_s__tracka;
static PyObject *__pyx_n_s__trackb;
static PyObject *__pyx_n_s__tracks;
static PyObject *__pyx_n_s__tracksA;
static PyObject *__pyx_n_s__tracksB;
static PyObject *__pyx_n_s__type_num;
static PyObject *__pyx_n_s__uint64;
static PyObject *__pyx_n_s__vec;
static PyObject *__pyx_n_s__vec1;
static PyObject *__pyx_n_s__vec2;
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_2;
static PyObject *__pyx_int_3;
static PyObject *__pyx_int_5;
static PyObject *__pyx_int_10;
static PyObject *__pyx_int_15;
static PyObject *__pyx_k_3;
static PyObject *__pyx_k_4;
static PyObject *__pyx_k_5;
static PyObject *__pyx_k_6;

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":40
 * 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;
  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;
  __Pyx_RefNannySetupContext("as_float_3vec");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":42
 * 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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__squeeze); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__asarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __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 = 42; __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 = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_5 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_6 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__float32); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __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 = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __pyx_t_6 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_1, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__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 = 42; __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, __pyx_t_4, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 42; __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_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF((PyObject *)__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":45
 * 
 * 
 * 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_RefNannySetupContext("asfp");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":46
 * 
 * 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;
}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_normalized_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_normalized_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec) {
  PyArrayObject *__pyx_v_vec_in = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  Py_buffer __pyx_bstruct_vec_in;
  Py_ssize_t __pyx_bstride_0_vec_in = 0;
  Py_ssize_t __pyx_bshape_0_vec_in = 0;
  Py_buffer __pyx_bstruct_vec_out;
  Py_ssize_t __pyx_bstride_0_vec_out = 0;
  Py_ssize_t __pyx_bshape_0_vec_out = 0;
  PyObject *__pyx_r = NULL;
  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;
  __Pyx_RefNannySetupContext("normalized_3vec");
  __pyx_self = __pyx_self;
  __pyx_bstruct_vec_in.buf = NULL;
  __pyx_bstruct_vec_out.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":62
 *     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 = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_vec_in, (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_bstruct_vec_in.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_in = __pyx_bstruct_vec_in.strides[0];
      __pyx_bshape_0_vec_in = __pyx_bstruct_vec_in.shape[0];
    }
  }
  __pyx_v_vec_in = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":63
 *     '''
 *     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_GetName(__pyx_m, __pyx_n_s__np); 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_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __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 = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __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(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_1 = 0;
  __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 63; __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_bstruct_vec_out, (PyObject*)__pyx_t_5, &__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_bstruct_vec_out.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_out = __pyx_bstruct_vec_out.strides[0];
      __pyx_bshape_0_vec_out = __pyx_bstruct_vec_out.shape[0];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/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_f_4dipy_8tracking_9distances_cnormalized_3vec(((float *)__pyx_v_vec_in->data), ((float *)__pyx_v_vec_out->data));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":65
 *     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);
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_in);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.normalized_3vec");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_in);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
  __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;
}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_norm_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_norm_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_norm_3vec(PyObject *__pyx_self, PyObject *__pyx_v_vec) {
  PyArrayObject *__pyx_v_vec_in = 0;
  Py_buffer __pyx_bstruct_vec_in;
  Py_ssize_t __pyx_bstride_0_vec_in = 0;
  Py_ssize_t __pyx_bshape_0_vec_in = 0;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  __Pyx_RefNannySetupContext("norm_3vec");
  __pyx_self = __pyx_self;
  __pyx_bstruct_vec_in.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":80
 *        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 = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_vec_in, (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_bstruct_vec_in.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 80; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_in = __pyx_bstruct_vec_in.strides[0];
      __pyx_bshape_0_vec_in = __pyx_bstruct_vec_in.shape[0];
    }
  }
  __pyx_v_vec_in = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":81
 *     '''
 *     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 = 81; __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_bstruct_vec_in);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.norm_3vec");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_in);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_in);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":84
 * 
 * 
 * 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_RefNannySetupContext("cnorm_3vec");

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":99
 *     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]);

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":101
 *     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;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":104
 * 
 * 
 * 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;
  int __pyx_t_1;
  float __pyx_t_2;
  __Pyx_RefNannySetupContext("cnormalized_3vec");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":118
 *     void
 *     '''
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":120
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":121
 *     cdef int i
 *     for i in range(3):
 *         vec_out[i] = vec_in[i] / norm             # <<<<<<<<<<<<<<
 * 
 * 
 */
    __pyx_t_2 = (__pyx_v_vec_in[__pyx_v_i]);
    if (unlikely(__pyx_v_norm == 0)) {
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 121; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    (__pyx_v_vec_out[__pyx_v_i]) = (__pyx_t_2 / __pyx_v_norm);
  }

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

/* "/Users/mb312/dev_trees/dipy/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)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_inner_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pf_4dipy_8tracking_9distances_inner_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
  __Pyx_RefNannySetupContext("inner_3vecs");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("inner_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 124; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "inner_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 124; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_vec1 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_vec2 = PyTuple_GET_ITEM(__pyx_args, 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 = 124; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.inner_3vecs");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":125
 * 
 * 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 = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 125; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
      __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":126
 * 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 = 126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 126; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
      __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":127
 *     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 = 127; __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_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.inner_3vecs");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":130
 * 
 * 
 * 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":132
 * 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":133
 *     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++) {

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":135
 *     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;
}

/* "/Users/mb312/dev_trees/dipy/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)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_sub_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pf_4dipy_8tracking_9distances_sub_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  Py_buffer __pyx_bstruct_vec_out;
  Py_ssize_t __pyx_bstride_0_vec_out = 0;
  Py_ssize_t __pyx_bshape_0_vec_out = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
  __Pyx_RefNannySetupContext("sub_3vecs");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("sub_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 138; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "sub_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 138; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_vec1 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_vec2 = PyTuple_GET_ITEM(__pyx_args, 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 = 138; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.sub_3vecs");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;
  __pyx_bstruct_vec_out.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":139
 * 
 * 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 = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
      __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":140
 * 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 = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 140; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
      __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":141
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __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 = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __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(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_1 = 0;
  __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 141; __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_bstruct_vec_out, (PyObject*)__pyx_t_5, &__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_bstruct_vec_out.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 141; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_out = __pyx_bstruct_vec_out.strides[0];
      __pyx_bshape_0_vec_out = __pyx_bstruct_vec_out.shape[0];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":142
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":143
 *     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);
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.sub_3vecs");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __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;
}

/* "/Users/mb312/dev_trees/dipy/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:
 */

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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":148
 * 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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":149
 *     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]));
  }

}

/* "/Users/mb312/dev_trees/dipy/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)
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_add_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pf_4dipy_8tracking_9distances_add_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  Py_buffer __pyx_bstruct_vec_out;
  Py_ssize_t __pyx_bstride_0_vec_out = 0;
  Py_ssize_t __pyx_bshape_0_vec_out = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
  __Pyx_RefNannySetupContext("add_3vecs");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("add_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "add_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_vec1 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_vec2 = PyTuple_GET_ITEM(__pyx_args, 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 = 152; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.add_3vecs");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;
  __pyx_bstruct_vec_out.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":153
 * 
 * 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 = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
      __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":154
 * 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 = 154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
      __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":155
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __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 = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __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(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_1 = 0;
  __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 155; __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_bstruct_vec_out, (PyObject*)__pyx_t_5, &__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_bstruct_vec_out.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_out = __pyx_bstruct_vec_out.strides[0];
      __pyx_bshape_0_vec_out = __pyx_bstruct_vec_out.shape[0];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":156
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":157
 *     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);
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.add_3vecs");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __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;
}

/* "/Users/mb312/dev_trees/dipy/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:
 */

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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":162
 * 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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":163
 *     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]));
  }

}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":165
 *         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_mul_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pf_4dipy_8tracking_9distances_mul_3vecs(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_vec1 = 0;
  PyObject *__pyx_v_vec2 = 0;
  PyArrayObject *__pyx_v_fvec1 = 0;
  PyArrayObject *__pyx_v_fvec2 = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  Py_buffer __pyx_bstruct_vec_out;
  Py_ssize_t __pyx_bstride_0_vec_out = 0;
  Py_ssize_t __pyx_bshape_0_vec_out = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__vec1,&__pyx_n_s__vec2,0};
  __Pyx_RefNannySetupContext("mul_3vecs");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("mul_3vecs", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "mul_3vecs") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_vec1 = values[0];
    __pyx_v_vec2 = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_vec1 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_vec2 = PyTuple_GET_ITEM(__pyx_args, 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 = 165; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vecs");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;
  __pyx_bstruct_vec_out.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":166
 * 
 * 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 = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
      __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
    }
  }
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":167
 * 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 = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 167; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
      __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
    }
  }
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":168
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __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 = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __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(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_1 = 0;
  __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 168; __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_bstruct_vec_out, (PyObject*)__pyx_t_5, &__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_bstruct_vec_out.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 168; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_out = __pyx_bstruct_vec_out.strides[0];
      __pyx_bshape_0_vec_out = __pyx_bstruct_vec_out.shape[0];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":169
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":170
 *     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);
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vecs");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __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;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":172
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":174
 * 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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":175
 *     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]));
  }

}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":177
 *         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_mul_3vec(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pf_4dipy_8tracking_9distances_mul_3vec(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_a = 0;
  PyObject *__pyx_v_vec = 0;
  PyArrayObject *__pyx_v_fvec = 0;
  PyArrayObject *__pyx_v_vec_out = 0;
  Py_buffer __pyx_bstruct_vec_out;
  Py_ssize_t __pyx_bstride_0_vec_out = 0;
  Py_ssize_t __pyx_bshape_0_vec_out = 0;
  Py_buffer __pyx_bstruct_fvec;
  Py_ssize_t __pyx_bstride_0_fvec = 0;
  Py_ssize_t __pyx_bshape_0_fvec = 0;
  PyObject *__pyx_r = NULL;
  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;
  float __pyx_t_6;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__a,&__pyx_n_s__vec,0};
  __Pyx_RefNannySetupContext("mul_3vec");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__a);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__vec);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("mul_3vec", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "mul_3vec") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_a = values[0];
    __pyx_v_vec = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_a = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_vec = PyTuple_GET_ITEM(__pyx_args, 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 = 177; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vec");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_fvec.buf = NULL;
  __pyx_bstruct_vec_out.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":178
 * 
 * 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 = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec, (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_bstruct_fvec.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_fvec = __pyx_bstruct_fvec.strides[0];
      __pyx_bshape_0_fvec = __pyx_bstruct_fvec.shape[0];
    }
  }
  __pyx_v_fvec = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":179
 * 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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __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 = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __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(2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);
  __Pyx_GIVEREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_1 = 0;
  __pyx_t_4 = 0;
  __pyx_t_4 = PyObject_Call(__pyx_t_2, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 179; __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_bstruct_vec_out, (PyObject*)__pyx_t_5, &__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_bstruct_vec_out.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 179; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_vec_out = __pyx_bstruct_vec_out.strides[0];
      __pyx_bshape_0_vec_out = __pyx_bstruct_vec_out.shape[0];
    }
  }
  __pyx_t_5 = 0;
  __pyx_v_vec_out = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":180
 *     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_6 = __pyx_PyFloat_AsDouble(__pyx_v_a); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_f_4dipy_8tracking_9distances_cmul_3vec(__pyx_t_6, ((float *)__pyx_v_fvec->data), ((float *)__pyx_v_vec_out->data));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":181
 *     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);
  __Pyx_XDECREF(__pyx_t_4);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.mul_3vec");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_vec_out);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_fvec);
  __Pyx_XDECREF((PyObject *)__pyx_v_vec_out);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":183
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":185
 * 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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":186
 *     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]));
  }

}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":193
 * 
 * 
 * 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_cut_plane(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_cut_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    Examples\n    ----------\n    >>> from dipy.tracking.distances import cut_plane\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    >>> cut_plane(bundlex,refx)\n    [array([[ 1.        ,  1.5       ,  0.        ,  0.70710683, 0,]], dtype=float32),\n     array([[ 2.        ,  2.5       ,  0.        ,  0.70710677, 0.]], dtype=float32)]\n        \n    The orthogonality relationship\n    np.inner(hits[p][q][0:3]-ref[p+1],ref[p+2]-ref[r][p+1])\n    will hold throughout for every point q in the hits plane\n    at point (p+1) on the reference track.\n    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_cut_plane(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_ref = 0;
  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;
  PyArrayObject *__pyx_v_track;
  PyObject *__pyx_v_hits;
  PyArrayObject *__pyx_v_one_hit;
  float *__pyx_v_hit_ptr;
  PyArrayObject *__pyx_v_hit_arr;
  PyObject *__pyx_v_Hit = 0;
  size_t __pyx_v_N_tracks;
  PyArrayObject *__pyx_v_track_lengths;
  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;
  Py_buffer __pyx_bstruct_one_hit;
  Py_ssize_t __pyx_bstride_0_one_hit = 0;
  Py_ssize_t __pyx_bshape_0_one_hit = 0;
  Py_buffer __pyx_bstruct_hit_arr;
  Py_ssize_t __pyx_bstride_0_hit_arr = 0;
  Py_ssize_t __pyx_bstride_1_hit_arr = 0;
  Py_ssize_t __pyx_bshape_0_hit_arr = 0;
  Py_ssize_t __pyx_bshape_1_hit_arr = 0;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  Py_buffer __pyx_bstruct_ref32;
  Py_ssize_t __pyx_bstride_0_ref32 = 0;
  Py_ssize_t __pyx_bstride_1_ref32 = 0;
  Py_ssize_t __pyx_bshape_0_ref32 = 0;
  Py_ssize_t __pyx_bshape_1_ref32 = 0;
  Py_buffer __pyx_bstruct_track_lengths;
  Py_ssize_t __pyx_bstride_0_track_lengths = 0;
  Py_ssize_t __pyx_bshape_0_track_lengths = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__ref,0};
  __Pyx_RefNannySetupContext("cut_plane");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__ref);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("cut_plane", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 193; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "cut_plane") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 193; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracks = values[0];
    __pyx_v_ref = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_tracks = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_ref = PyTuple_GET_ITEM(__pyx_args, 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 = 193; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.cut_plane");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_ref32 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_track = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_hits = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_one_hit = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_hit_arr = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_track_lengths = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_ref32.buf = NULL;
  __pyx_bstruct_track.buf = NULL;
  __pyx_bstruct_one_hit.buf = NULL;
  __pyx_bstruct_hit_arr.buf = NULL;
  __pyx_bstruct_track_lengths.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":232
 *         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 = 232; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  __pyx_v_Hit = ((PyObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":234
 *         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_GetName(__pyx_m, __pyx_n_s__np); 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_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __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 = 234; __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, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = ((PyArrayObject *)__pyx_t_3);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_ref32);
    __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_ref32, (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_bstruct_ref32, (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_bstride_0_ref32 = __pyx_bstruct_ref32.strides[0]; __pyx_bstride_1_ref32 = __pyx_bstruct_ref32.strides[1];
    __pyx_bshape_0_ref32 = __pyx_bstruct_ref32.shape[0]; __pyx_bshape_1_ref32 = __pyx_bstruct_ref32.shape[1];
    if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 234; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_ref32));
  __pyx_v_ref32 = ((PyArrayObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":238
 *     # 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 = 238; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_N_tracks = __pyx_t_9;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":241
 *         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 = 241; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  __pyx_v_tracks32 = ((PyObject *)__pyx_t_3);
  __pyx_t_3 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":242
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__empty); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __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 = 242; __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 = 242; __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 = 242; __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_t_2 = 0;
  __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_10 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_10);
  __pyx_t_11 = PyObject_GetAttr(__pyx_t_10, __pyx_n_s__uint64); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __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 = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  __pyx_t_11 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_3, ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__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 = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_11);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track_lengths);
    __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track_lengths, (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_bstruct_track_lengths, (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_bstride_0_track_lengths = __pyx_bstruct_track_lengths.strides[0];
    __pyx_bshape_0_track_lengths = __pyx_bstruct_track_lengths.shape[0];
    if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 242; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track_lengths));
  __pyx_v_track_lengths = ((PyArrayObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":243
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":244
 *     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_GetName(__pyx_m, __pyx_n_s__np); 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_t_2 = PyObject_GetAttr(__pyx_t_11, __pyx_n_s__ascontiguousarray); 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);
    __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); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __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 = 244; __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, __pyx_t_3, NULL); 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_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__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 = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_15 = ((PyArrayObject *)__pyx_t_11);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
      __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
      __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
      if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 244; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_15 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_11);
    __pyx_t_11 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":245
 *     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_bshape_0_track_lengths)) __pyx_t_5 = 0;
    if (unlikely(__pyx_t_5 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_5);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 245; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    *__Pyx_BufPtrStrided1d(__pyx_t_5numpy_uint64_t *, __pyx_bstruct_track_lengths.buf, __pyx_t_16, __pyx_bstride_0_track_lengths) = (__pyx_v_track->dimensions[0]);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":246
 *         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 = 246; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":249
 *     # 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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":258
 *     # 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":259
 *     # 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 = 259; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_11));
  __Pyx_DECREF(__pyx_v_hits);
  __pyx_v_hits = ((PyObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":261
 *     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); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 261; __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 = 261; __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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":262
 *     # 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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":264
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":265
 *         # 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); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 265; __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 = 265; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":266
 *         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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":268
 *         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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":270
 *         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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":272
 *         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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":273
 *         # 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); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 273; __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 = 273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_15 = ((PyArrayObject *)__pyx_t_11);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
        __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
        __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
        if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 273; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_15 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_track));
      __pyx_v_track = ((PyArrayObject *)__pyx_t_11);
      __pyx_t_11 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":274
 *         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_bshape_0_track_lengths)) __pyx_t_5 = 0;
      if (unlikely(__pyx_t_5 != -1)) {
        __Pyx_RaiseBufferIndexError(__pyx_t_5);
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_v_N_track = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_uint64_t *, __pyx_bstruct_track_lengths.buf, __pyx_t_19, __pyx_bstride_0_track_lengths));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":276
 *             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); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 276; __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 = 276; __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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":277
 *             # 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;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":282
 *                 # 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;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":283
 *                 # 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); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 283; __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 = 283; __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;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":285
 *                 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);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":286
 *                 #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);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":287
 *                 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);
        if (__pyx_t_23) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":289
 *                 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);

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":290
 *                     #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);

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":291
 *                     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);
          if (__pyx_t_23) {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":293
 *                     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);

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":294
 *                         #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);

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":295
 *                         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)) {
              PyErr_Format(PyExc_ZeroDivisionError, "float division");
              {__pyx_filename = __pyx_f[0]; __pyx_lineno = 294; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
            }
            __pyx_v_alpha = (__pyx_t_24 / __pyx_t_25);

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":296
 *                         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);
            if (__pyx_t_23) {

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":298
 *                         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])));

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":299
 *                             # 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])));

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":300
 *                             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])));

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":302
 *                             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);

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":304
 *                             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);

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

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":307
 *                             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);

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":317
 *                             # 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);
              if (__pyx_t_23) {

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":318
 *                             # 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);

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":319
 *                             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)) {
                  PyErr_Format(PyExc_ZeroDivisionError, "float division");
                  {__pyx_filename = __pyx_f[0]; __pyx_lineno = 319; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                }
                __pyx_v_rcd = fabs((__pyx_t_25 / __pyx_t_24));
                goto __pyx_L17;
              }
              /*else*/ {

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

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":323
 *                                 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);
              if (__pyx_t_23) {

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":324
 *                             # 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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_11);
                __pyx_t_3 = PyObject_GetAttr(__pyx_t_11, __pyx_n_s__empty); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_3);
                __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
                __pyx_t_11 = PyTuple_New(1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_11);
                __Pyx_INCREF(__pyx_int_5);
                PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_int_5);
                __Pyx_GIVEREF(__pyx_int_5);
                __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_2);
                PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_11);
                __Pyx_GIVEREF(__pyx_t_11);
                __pyx_t_11 = 0;
                __pyx_t_11 = PyDict_New(); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __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 = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __pyx_t_1 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_2, ((PyObject *)__pyx_t_11)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __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_DECREF(((PyObject *)__pyx_t_11)); __pyx_t_11 = 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 = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __pyx_t_26 = ((PyArrayObject *)__pyx_t_1);
                {
                  __Pyx_BufFmt_StackElem __pyx_stack[1];
                  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_one_hit);
                  __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_one_hit, (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_bstruct_one_hit, (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_bstride_0_one_hit = __pyx_bstruct_one_hit.strides[0];
                  __pyx_bshape_0_one_hit = __pyx_bstruct_one_hit.shape[0];
                  if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 324; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                }
                __pyx_t_26 = 0;
                __Pyx_DECREF(((PyObject *)__pyx_v_one_hit));
                __pyx_v_one_hit = ((PyArrayObject *)__pyx_t_1);
                __pyx_t_1 = 0;

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":325
 *                             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_1 = __Pyx_PyObject_Append(__pyx_v_hits, ((PyObject *)__pyx_v_one_hit)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 325; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __Pyx_GOTREF(__pyx_t_1);
                __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
                goto __pyx_L18;
              }
              /*else*/ {

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":327
 *                                 hits.append(one_hit)
 *                             else:
 *                                 one_hit = hits[hit_no]             # <<<<<<<<<<<<<<
 *                             hit_ptr = <float *>one_hit.data
 *                             hit_ptr[0] = hit[0]
 */
                __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_hits, __pyx_v_hit_no, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __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 = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                __pyx_t_26 = ((PyArrayObject *)__pyx_t_1);
                {
                  __Pyx_BufFmt_StackElem __pyx_stack[1];
                  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_one_hit);
                  __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_one_hit, (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_bstruct_one_hit, (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_bstride_0_one_hit = __pyx_bstruct_one_hit.strides[0];
                  __pyx_bshape_0_one_hit = __pyx_bstruct_one_hit.shape[0];
                  if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 327; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
                }
                __pyx_t_26 = 0;
                __Pyx_DECREF(((PyObject *)__pyx_v_one_hit));
                __pyx_v_one_hit = ((PyArrayObject *)__pyx_t_1);
                __pyx_t_1 = 0;
              }
              __pyx_L18:;

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":328
 *                             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);

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":329
 *                                 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]);

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":330
 *                             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]);

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":331
 *                             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]);

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":332
 *                             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;

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":333
 *                             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;

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":334
 *                             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 += 1;
              goto __pyx_L16;
            }
            __pyx_L16:;
            goto __pyx_L15;
          }
          __pyx_L15:;
          goto __pyx_L14;
        }
        __pyx_L14:;
      }
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":336
 *                             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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":337
 *         # 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);
    if (__pyx_t_23) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":338
 *         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_L19;
    }
    __pyx_L19:;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":339
 *         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_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_11 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__empty); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = __Pyx_PyInt_FromSize_t(__pyx_v_n_hits); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __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_INCREF(__pyx_int_5);
    PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_5);
    __Pyx_GIVEREF(__pyx_int_5);
    __pyx_t_1 = 0;
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 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 = 339; __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 = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_11, __pyx_t_1, ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 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 = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_27 = ((PyArrayObject *)__pyx_t_3);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_hit_arr);
      __pyx_t_5 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_hit_arr, (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_bstruct_hit_arr, (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_bstride_0_hit_arr = __pyx_bstruct_hit_arr.strides[0]; __pyx_bstride_1_hit_arr = __pyx_bstruct_hit_arr.strides[1];
      __pyx_bshape_0_hit_arr = __pyx_bstruct_hit_arr.shape[0]; __pyx_bshape_1_hit_arr = __pyx_bstruct_hit_arr.shape[1];
      if (unlikely(__pyx_t_5 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 339; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_27 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_hit_arr));
    __pyx_v_hit_arr = ((PyArrayObject *)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":340
 *             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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":341
 *         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_3 = __Pyx_GetItemInt(__pyx_v_hits, __pyx_v_hit_no, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      if (__Pyx_SetItemInt(((PyObject *)__pyx_v_hit_arr), __pyx_v_hit_no, __pyx_t_3, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 341; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":342
 *         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_3 = __Pyx_PyObject_Append(__pyx_v_Hit, ((PyObject *)__pyx_v_hit_arr)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 342; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":344
 *         Hit.append(hit_arr)
 *         #Div.append(divs[1:])
 *     return Hit[1:]             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_3 = PySequence_GetSlice(__pyx_v_Hit, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 344; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_r = __pyx_t_3;
  __pyx_t_3 = 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_bstruct_one_hit);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_hit_arr);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_ref32);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track_lengths);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.cut_plane");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_one_hit);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_hit_arr);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_ref32);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track_lengths);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_ref32);
  __Pyx_DECREF((PyObject *)__pyx_v_track);
  __Pyx_DECREF(__pyx_v_hits);
  __Pyx_DECREF((PyObject *)__pyx_v_one_hit);
  __Pyx_DECREF((PyObject *)__pyx_v_hit_arr);
  __Pyx_XDECREF(__pyx_v_Hit);
  __Pyx_DECREF((PyObject *)__pyx_v_track_lengths);
  __Pyx_XDECREF(__pyx_v_tracks32);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":350
 * 
 * 
 * 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_most_similar_track_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_most_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_most_similar_track_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_metric = 0;
  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;
  PyArrayObject *__pyx_v_track2others;
  __pyx_t_5numpy_double_t *__pyx_v_sum_track2others;
  PyArrayObject *__pyx_v_distances_buffer;
  __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;
  PyArrayObject *__pyx_v_t2;
  size_t __pyx_v_t1_len;
  size_t __pyx_v_t2_len;
  double __pyx_v_mn;
  size_t __pyx_v_si;
  Py_buffer __pyx_bstruct_tracks32;
  Py_ssize_t __pyx_bstride_0_tracks32 = 0;
  Py_ssize_t __pyx_bshape_0_tracks32 = 0;
  Py_buffer __pyx_bstruct_distances_buffer;
  Py_ssize_t __pyx_bstride_0_distances_buffer = 0;
  Py_ssize_t __pyx_bshape_0_distances_buffer = 0;
  Py_buffer __pyx_bstruct_track2others;
  Py_ssize_t __pyx_bstride_0_track2others = 0;
  Py_ssize_t __pyx_bshape_0_track2others = 0;
  Py_buffer __pyx_bstruct_t2;
  Py_ssize_t __pyx_bstride_0_t2 = 0;
  Py_ssize_t __pyx_bstride_1_t2 = 0;
  Py_ssize_t __pyx_bshape_0_t2 = 0;
  Py_ssize_t __pyx_bshape_1_t2 = 0;
  Py_buffer __pyx_bstruct_t1;
  Py_ssize_t __pyx_bstride_0_t1 = 0;
  Py_ssize_t __pyx_bstride_1_t1 = 0;
  Py_ssize_t __pyx_bshape_0_t1 = 0;
  Py_ssize_t __pyx_bshape_1_t1 = 0;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  Py_ssize_t __pyx_t_4;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__metric,0};
  __Pyx_RefNannySetupContext("most_similar_track_mam");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    values[1] = ((PyObject *)__pyx_n_s__avg);
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "most_similar_track_mam") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 350; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracks = values[0];
    __pyx_v_metric = values[1];
  } else {
    __pyx_v_metric = ((PyObject *)__pyx_n_s__avg);
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: __pyx_v_metric = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_tracks = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 350; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.most_similar_track_mam");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_tracks32 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_track2others = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_distances_buffer = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_t1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_t2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_tracks32.buf = NULL;
  __pyx_bstruct_track2others.buf = NULL;
  __pyx_bstruct_distances_buffer.buf = NULL;
  __pyx_bstruct_t1.buf = NULL;
  __pyx_bstruct_t2.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":391
 *         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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 391; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":392
 *         int metric_type
 *     if metric=='avg':
 *         metric_type = 0             # <<<<<<<<<<<<<<
 *     elif metric == 'min':
 *         metric_type = 1
 */
    __pyx_v_metric_type = 0;
    goto __pyx_L6;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":393
 *     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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 393; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __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) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":394
 *         metric_type = 0
 *     elif metric == 'min':
 *         metric_type = 1             # <<<<<<<<<<<<<<
 *     elif metric == 'max':
 *         metric_type = 2
 */
    __pyx_v_metric_type = 1;
    goto __pyx_L6;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":395
 *     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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 395; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __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) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":396
 *         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_L6;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":398
 *         metric_type = 2
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')             # <<<<<<<<<<<<<<
 *     # preprocess tracks
 *     cdef:
 */
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 398; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_INCREF(((PyObject *)__pyx_kp_s_1));
    PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_kp_s_1));
    __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_1));
    __pyx_t_3 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 398; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_Raise(__pyx_t_3, 0, 0);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 398; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_L6:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":401
 *     # 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":403
 *         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_4 = PyObject_Length(__pyx_v_tracks); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 403; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_4;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":404
 *         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_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __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_lent); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 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 = 404; __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 = 404; __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 = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_3, ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 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 = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_6);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracks32);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_tracks32, (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_bstruct_tracks32, (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_bstride_0_tracks32 = __pyx_bstruct_tracks32.strides[0];
    __pyx_bshape_0_tracks32 = __pyx_bstruct_tracks32.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_7 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_tracks32));
  __pyx_v_tracks32 = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":406
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":407
 *     # 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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_5 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __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); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_3, 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 = 407; __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 = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_1 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_3, ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 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_bshape_0_tracks32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 407; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_15 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracks32.buf, __pyx_t_14, __pyx_bstride_0_tracks32);
    __Pyx_GOTREF(*__pyx_t_15);
    __Pyx_DECREF(*__pyx_t_15); __Pyx_INCREF(__pyx_t_1);
    *__pyx_t_15 = __pyx_t_1;
    __Pyx_GIVEREF(*__pyx_t_15);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":408
 *     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_bshape_0_tracks32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_1 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracks32.buf, __pyx_t_16, __pyx_bstride_0_tracks32);
    __Pyx_INCREF((PyObject*)__pyx_t_1);
    __pyx_t_6 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__shape); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = __Pyx_GetItemInt(__pyx_t_6, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_17 = __Pyx_PyInt_AsSize_t(__pyx_t_1); if (unlikely((__pyx_t_17 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 408; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_v_track_len = __pyx_t_17;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":409
 *         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);
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":410
 *         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_L9;
    }
    __pyx_L9:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":414
 *     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_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_6 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __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 = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 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 = 414; __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 = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  __pyx_t_5 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_18 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__double); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __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_3, ((PyObject *)__pyx_n_s__dtype), __pyx_t_18) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
  __pyx_t_18 = PyEval_CallObjectWithKeywords(__pyx_t_6, __pyx_t_1, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 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 = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_19 = ((PyArrayObject *)__pyx_t_18);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2others);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track2others, (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_bstruct_track2others, (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_bstride_0_track2others = __pyx_bstruct_track2others.strides[0];
    __pyx_bshape_0_track2others = __pyx_bstruct_track2others.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 414; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_19 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track2others));
  __pyx_v_track2others = ((PyArrayObject *)__pyx_t_18);
  __pyx_t_18 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":417
 *     # 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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":422
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_18, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __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 = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __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 = PyTuple_New(1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_18);
  PyTuple_SET_ITEM(__pyx_t_18, 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 = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_5 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__float32); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __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_1, ((PyObject *)__pyx_n_s__dtype), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_18, ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 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 = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_20 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_distances_buffer, (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_bstruct_distances_buffer, (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_bstride_0_distances_buffer = __pyx_bstruct_distances_buffer.strides[0];
    __pyx_bshape_0_distances_buffer = __pyx_bstruct_distances_buffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 422; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_20 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_distances_buffer));
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":423
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":428
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":429
 *         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_bshape_0_tracks32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_5 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracks32.buf, __pyx_t_13, __pyx_bstride_0_tracks32);
    __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 = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_t1, (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_bstruct_t1, (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_bstride_0_t1 = __pyx_bstruct_t1.strides[0]; __pyx_bstride_1_t1 = __pyx_bstruct_t1.strides[1];
      __pyx_bshape_0_t1 = __pyx_bstruct_t1.shape[0]; __pyx_bshape_1_t1 = __pyx_bstruct_t1.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 429; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_21 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_t1));
    __pyx_v_t1 = ((PyArrayObject *)__pyx_t_5);
    __pyx_t_5 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":430
 *     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]);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":431
 *         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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":432
 *         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++) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":433
 *         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_bshape_0_tracks32)) __pyx_t_8 = 0;
      if (unlikely(__pyx_t_8 != -1)) {
        __Pyx_RaiseBufferIndexError(__pyx_t_8);
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_5 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracks32.buf, __pyx_t_22, __pyx_bstride_0_tracks32);
      __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 = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
        __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_t2, (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_bstruct_t2, (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_bstride_0_t2 = __pyx_bstruct_t2.strides[0]; __pyx_bstride_1_t2 = __pyx_bstruct_t2.strides[1];
        __pyx_bshape_0_t2 = __pyx_bstruct_t2.shape[0]; __pyx_bshape_1_t2 = __pyx_bstruct_t2.shape[1];
        if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 433; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_21 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_t2));
      __pyx_v_t2 = ((PyArrayObject *)__pyx_t_5);
      __pyx_t_5 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":434
 *         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]);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":435
 *             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);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":436
 *             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);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":438
 *             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_v_sum_track2others[__pyx_v_i]) += __pyx_v_distance;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":439
 *             # 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_v_sum_track2others[__pyx_v_j]) += __pyx_v_distance;
    }
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":441
 *             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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":442
 *     # 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":443
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":444
 *     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);
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":445
 *     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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":446
 *         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_L16;
    }
    __pyx_L16:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":448
 *             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_bshape_0_tracks32)) __pyx_t_8 = 0;
  if (unlikely(__pyx_t_8 != -1)) {
    __Pyx_RaiseBufferIndexError(__pyx_t_8);
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracks32.buf, __pyx_t_12, __pyx_bstride_0_tracks32);
  __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 = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_t1, (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_bstruct_t1, (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_bstride_0_t1 = __pyx_bstruct_t1.strides[0]; __pyx_bstride_1_t1 = __pyx_bstruct_t1.strides[1];
    __pyx_bshape_0_t1 = __pyx_bstruct_t1.shape[0]; __pyx_bshape_1_t1 = __pyx_bstruct_t1.shape[1];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 448; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_21 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_t1));
  __pyx_v_t1 = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":449
 *     # 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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":450
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":451
 *     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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":452
 *     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_bshape_0_tracks32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_5 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracks32.buf, __pyx_t_23, __pyx_bstride_0_tracks32);
    __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 = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_21 = ((PyArrayObject *)__pyx_t_5);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_t2, (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_bstruct_t2, (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_bstride_0_t2 = __pyx_bstruct_t2.strides[0]; __pyx_bstride_1_t2 = __pyx_bstruct_t2.strides[1];
      __pyx_bshape_0_t2 = __pyx_bstruct_t2.shape[0]; __pyx_bshape_1_t2 = __pyx_bstruct_t2.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 452; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_21 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_t2));
    __pyx_v_t2 = ((PyArrayObject *)__pyx_t_5);
    __pyx_t_5 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":453
 *     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]);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":454
 *         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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":455
 *         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_bshape_0_track2others)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 455; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    *__Pyx_BufPtrStrided1d(__pyx_t_5numpy_double_t *, __pyx_bstruct_track2others.buf, __pyx_t_24, __pyx_bstride_0_track2others) = __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);
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":456
 *         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_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 = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 456; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __Pyx_INCREF(((PyObject *)__pyx_v_track2others));
  PyTuple_SET_ITEM(__pyx_t_1, 1, ((PyObject *)__pyx_v_track2others));
  __Pyx_GIVEREF(((PyObject *)__pyx_v_track2others));
  __pyx_t_5 = 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_3);
  __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_bstruct_tracks32);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2others);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.most_similar_track_mam");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracks32);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2others);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_tracks32);
  __Pyx_DECREF((PyObject *)__pyx_v_track2others);
  __Pyx_DECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_DECREF((PyObject *)__pyx_v_t1);
  __Pyx_DECREF((PyObject *)__pyx_v_t2);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":459
 * 
 * 
 * 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_bundles_distances_mam(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_bundles_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_bundles_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;
  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;
  size_t __pyx_v_longest_track_len;
  size_t __pyx_v_track_len;
  PyObject *__pyx_v_longest_track_lenA;
  PyObject *__pyx_v_longest_track_lenB;
  PyArrayObject *__pyx_v_tracksA32;
  PyArrayObject *__pyx_v_tracksB32;
  PyArrayObject *__pyx_v_DM;
  PyArrayObject *__pyx_v_distances_buffer;
  __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;
  PyArrayObject *__pyx_v_t2;
  size_t __pyx_v_t1_len;
  size_t __pyx_v_t2_len;
  Py_buffer __pyx_bstruct_DM;
  Py_ssize_t __pyx_bstride_0_DM = 0;
  Py_ssize_t __pyx_bstride_1_DM = 0;
  Py_ssize_t __pyx_bshape_0_DM = 0;
  Py_ssize_t __pyx_bshape_1_DM = 0;
  Py_buffer __pyx_bstruct_distances_buffer;
  Py_ssize_t __pyx_bstride_0_distances_buffer = 0;
  Py_ssize_t __pyx_bshape_0_distances_buffer = 0;
  Py_buffer __pyx_bstruct_tracksB32;
  Py_ssize_t __pyx_bstride_0_tracksB32 = 0;
  Py_ssize_t __pyx_bshape_0_tracksB32 = 0;
  Py_buffer __pyx_bstruct_t2;
  Py_ssize_t __pyx_bstride_0_t2 = 0;
  Py_ssize_t __pyx_bstride_1_t2 = 0;
  Py_ssize_t __pyx_bshape_0_t2 = 0;
  Py_ssize_t __pyx_bshape_1_t2 = 0;
  Py_buffer __pyx_bstruct_t1;
  Py_ssize_t __pyx_bstride_0_t1 = 0;
  Py_ssize_t __pyx_bstride_1_t1 = 0;
  Py_ssize_t __pyx_bshape_0_t1 = 0;
  Py_ssize_t __pyx_bshape_1_t1 = 0;
  Py_buffer __pyx_bstruct_tracksA32;
  Py_ssize_t __pyx_bstride_0_tracksA32 = 0;
  Py_ssize_t __pyx_bshape_0_tracksA32 = 0;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  Py_ssize_t __pyx_t_4;
  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;
  PyObject **__pyx_t_21;
  size_t __pyx_t_22;
  size_t __pyx_t_23;
  PyArrayObject *__pyx_t_24 = NULL;
  PyArrayObject *__pyx_t_25 = NULL;
  size_t __pyx_t_26;
  size_t __pyx_t_27;
  size_t __pyx_t_28;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracksA,&__pyx_n_s__tracksB,&__pyx_n_s__metric,0};
  __Pyx_RefNannySetupContext("bundles_distances_mam");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[3] = {0,0,0};
    values[2] = ((PyObject *)__pyx_n_s__avg);
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracksA);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracksB);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("bundles_distances_mam", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __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, PyTuple_GET_SIZE(__pyx_args), "bundles_distances_mam") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 459; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracksA = values[0];
    __pyx_v_tracksB = values[1];
    __pyx_v_metric = values[2];
  } else {
    __pyx_v_metric = ((PyObject *)__pyx_n_s__avg);
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  3:
      __pyx_v_metric = PyTuple_GET_ITEM(__pyx_args, 2);
      case  2:
      __pyx_v_tracksB = PyTuple_GET_ITEM(__pyx_args, 1);
      __pyx_v_tracksA = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 459; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.bundles_distances_mam");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_longest_track_lenA = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_longest_track_lenB = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_tracksA32 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_tracksB32 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_DM = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_distances_buffer = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_t1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_t2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_tracksA32.buf = NULL;
  __pyx_bstruct_tracksB32.buf = NULL;
  __pyx_bstruct_DM.buf = NULL;
  __pyx_bstruct_distances_buffer.buf = NULL;
  __pyx_bstruct_t1.buf = NULL;
  __pyx_bstruct_t2.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":480
 *         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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 480; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":481
 *         int metric_type
 *     if metric=='avg':
 *         metric_type = 0             # <<<<<<<<<<<<<<
 *     elif metric == 'min':
 *         metric_type = 1
 */
    __pyx_v_metric_type = 0;
    goto __pyx_L6;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":482
 *     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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 482; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":483
 *         metric_type = 0
 *     elif metric == 'min':
 *         metric_type = 1             # <<<<<<<<<<<<<<
 *     elif metric == 'max':
 *         metric_type = 2
 */
    __pyx_v_metric_type = 1;
    goto __pyx_L6;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":484
 *     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); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 484; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":485
 *         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_L6;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":487
 *         metric_type = 2
 *     else:
 *         raise ValueError('Metric should be one of avg, min, max')             # <<<<<<<<<<<<<<
 *     # preprocess tracks
 *     cdef:
 */
    __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_INCREF(((PyObject *)__pyx_kp_s_1));
    PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_kp_s_1));
    __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_1));
    __pyx_t_3 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_Raise(__pyx_t_3, 0, 0);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 487; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_L6:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":490
 *     # 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":495
 *         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_4 = PyObject_Length(__pyx_v_tracksA); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 495; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lentA = __pyx_t_4;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":496
 *         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_4 = PyObject_Length(__pyx_v_tracksB); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 496; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lentB = __pyx_t_4;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":497
 *     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_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __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_lentA); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  PyTuple_SET_ITEM(__pyx_t_5, 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 = 497; __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 = 497; __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 = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_3, ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 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 = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_6);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracksA32);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_tracksA32, (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_bstruct_tracksA32, (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_bstride_0_tracksA32 = __pyx_bstruct_tracksA32.strides[0];
    __pyx_bshape_0_tracksA32 = __pyx_bstruct_tracksA32.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 497; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_7 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_tracksA32));
  __pyx_v_tracksA32 = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":498
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_5 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __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 = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 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 = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  PyTuple_SET_ITEM(__pyx_t_6, 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 = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__dtype), __pyx_builtin_object) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_1 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_6, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 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 = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracksB32);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_tracksB32, (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_bstruct_tracksB32, (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_bstride_0_tracksB32 = __pyx_bstruct_tracksB32.strides[0];
    __pyx_bshape_0_tracksB32 = __pyx_bstruct_tracksB32.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 498; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_tracksB32));
  __pyx_v_tracksB32 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":499
 *     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_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __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_PyInt_FromSize_t(__pyx_v_lentA); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_6 = __Pyx_PyInt_FromSize_t(__pyx_v_lentB); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __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 = 499; __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);
  PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_6);
  __Pyx_GIVEREF(__pyx_t_6);
  __pyx_t_1 = 0;
  __pyx_t_6 = 0;
  __pyx_t_6 = PyTuple_New(1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __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 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_5));
  __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_13 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__double); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), __pyx_t_13) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  __pyx_t_13 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_6, ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__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 = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_14 = ((PyArrayObject *)__pyx_t_13);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_DM);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_DM, (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_bstruct_DM, (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_bstride_0_DM = __pyx_bstruct_DM.strides[0]; __pyx_bstride_1_DM = __pyx_bstruct_DM.strides[1];
    __pyx_bshape_0_DM = __pyx_bstruct_DM.shape[0]; __pyx_bshape_1_DM = __pyx_bstruct_DM.shape[1];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 499; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_14 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_DM));
  __pyx_v_DM = ((PyArrayObject *)__pyx_t_13);
  __pyx_t_13 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":501
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":502
 *     # 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_GetName(__pyx_m, __pyx_n_s__np); 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_t_5 = PyObject_GetAttr(__pyx_t_13, __pyx_n_s__ascontiguousarray); 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);
    __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); if (!__pyx_t_13) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __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 = 502; __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 = 502; __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 = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_6, ((PyObject *)__pyx_t_13)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__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_8 = -1;
    if (unlikely(__pyx_t_17 >= (size_t)__pyx_bshape_0_tracksA32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 502; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_18 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracksA32.buf, __pyx_t_17, __pyx_bstride_0_tracksA32);
    __Pyx_GOTREF(*__pyx_t_18);
    __Pyx_DECREF(*__pyx_t_18); __Pyx_INCREF(__pyx_t_3);
    *__pyx_t_18 = __pyx_t_3;
    __Pyx_GIVEREF(*__pyx_t_18);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":503
 *     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_8 = -1;
    if (unlikely(__pyx_t_19 >= (size_t)__pyx_bshape_0_tracksA32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_3 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracksA32.buf, __pyx_t_19, __pyx_bstride_0_tracksA32);
    __Pyx_INCREF((PyObject*)__pyx_t_3);
    __pyx_t_13 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__shape); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = __Pyx_GetItemInt(__pyx_t_13, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    __pyx_t_20 = __Pyx_PyInt_AsSize_t(__pyx_t_3); if (unlikely((__pyx_t_20 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 503; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_v_track_len = __pyx_t_20;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":504
 *         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_3 = __Pyx_PyInt_FromSize_t(__pyx_v_track_len); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_13 = PyObject_RichCompare(__pyx_t_3, __pyx_v_longest_track_lenA, Py_GT); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_13); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 504; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":505
 *         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_t_13 = __Pyx_PyInt_FromSize_t(__pyx_v_track_len); 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_DECREF(__pyx_v_longest_track_lenA);
      __pyx_v_longest_track_lenA = __pyx_t_13;
      __pyx_t_13 = 0;
      goto __pyx_L9;
    }
    __pyx_L9:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":506
 *         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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":507
 *             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_13 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __pyx_t_3 = PyObject_GetAttr(__pyx_t_13, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    __pyx_t_13 = __Pyx_GetItemInt(__pyx_v_tracksB, __pyx_v_i, sizeof(size_t)+1, __Pyx_PyInt_FromSize_t); if (!__pyx_t_13) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __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 = 507; __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 = 507; __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 = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_5 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_6, ((PyObject *)__pyx_t_13)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_13)); __pyx_t_13 = 0;
    __pyx_t_20 = __pyx_v_i;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_20 >= (size_t)__pyx_bshape_0_tracksB32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 507; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_21 = __Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracksB32.buf, __pyx_t_20, __pyx_bstride_0_tracksB32);
    __Pyx_GOTREF(*__pyx_t_21);
    __Pyx_DECREF(*__pyx_t_21); __Pyx_INCREF(__pyx_t_5);
    *__pyx_t_21 = __pyx_t_5;
    __Pyx_GIVEREF(*__pyx_t_21);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":508
 *     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_22 = __pyx_v_i;
    __pyx_t_8 = -1;
    if (unlikely(__pyx_t_22 >= (size_t)__pyx_bshape_0_tracksB32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_5 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracksB32.buf, __pyx_t_22, __pyx_bstride_0_tracksB32);
    __Pyx_INCREF((PyObject*)__pyx_t_5);
    __pyx_t_13 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__shape); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_5 = __Pyx_GetItemInt(__pyx_t_13, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    __pyx_t_23 = __Pyx_PyInt_AsSize_t(__pyx_t_5); if (unlikely((__pyx_t_23 == (size_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 508; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_v_track_len = __pyx_t_23;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":509
 *         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_5 = __Pyx_PyInt_FromSize_t(__pyx_v_track_len); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_13 = PyObject_RichCompare(__pyx_t_5, __pyx_v_longest_track_lenB, Py_GT); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_13);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_13); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 509; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":510
 *         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_t_13 = __Pyx_PyInt_FromSize_t(__pyx_v_track_len); 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_v_longest_track_lenB);
      __pyx_v_longest_track_lenB = __pyx_t_13;
      __pyx_t_13 = 0;
      goto __pyx_L12;
    }
    __pyx_L12:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":511
 *         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_13 = PyObject_RichCompare(__pyx_v_longest_track_lenB, __pyx_v_longest_track_lenA, Py_GT); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_13); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 511; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":512
 *             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_INCREF(__pyx_v_longest_track_lenB);
    __Pyx_DECREF(__pyx_v_longest_track_lenA);
    __pyx_v_longest_track_lenA = __pyx_v_longest_track_lenB;
    goto __pyx_L13;
  }
  __pyx_L13:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":517
 *         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_13 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  __pyx_t_5 = PyObject_GetAttr(__pyx_t_13, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  __pyx_t_13 = PyNumber_Multiply(__pyx_v_longest_track_lenA, __pyx_int_2); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __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 = 517; __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 = PyTuple_New(1); if (unlikely(!__pyx_t_13)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_13);
  PyTuple_SET_ITEM(__pyx_t_13, 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 = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_6));
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__dtype), __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_t_1 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_13, ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 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 = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_24 = ((PyArrayObject *)__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_distances_buffer, (PyObject*)__pyx_t_24, &__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_bstruct_distances_buffer, (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_bstride_0_distances_buffer = __pyx_bstruct_distances_buffer.strides[0];
    __pyx_bshape_0_distances_buffer = __pyx_bstruct_distances_buffer.shape[0];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 517; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_24 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_distances_buffer));
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":518
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":523
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":524
 *         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_8 = -1;
    if (unlikely(__pyx_t_16 >= (size_t)__pyx_bshape_0_tracksA32)) __pyx_t_8 = 0;
    if (unlikely(__pyx_t_8 != -1)) {
      __Pyx_RaiseBufferIndexError(__pyx_t_8);
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_1 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracksA32.buf, __pyx_t_16, __pyx_bstride_0_tracksA32);
    __Pyx_INCREF((PyObject*)__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 = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_25 = ((PyArrayObject *)__pyx_t_1);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_t1, (PyObject*)__pyx_t_25, &__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_bstruct_t1, (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_bstride_0_t1 = __pyx_bstruct_t1.strides[0]; __pyx_bstride_1_t1 = __pyx_bstruct_t1.strides[1];
      __pyx_bshape_0_t1 = __pyx_bstruct_t1.shape[0]; __pyx_bshape_1_t1 = __pyx_bstruct_t1.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 524; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_25 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_t1));
    __pyx_v_t1 = ((PyArrayObject *)__pyx_t_1);
    __pyx_t_1 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":525
 *     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]);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":526
 *         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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":527
 *         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_23 = __pyx_v_lentB;
    for (__pyx_v_j = 0; __pyx_v_j < __pyx_t_23; __pyx_v_j++) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":528
 *         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_26 = __pyx_v_j;
      __pyx_t_8 = -1;
      if (unlikely(__pyx_t_26 >= (size_t)__pyx_bshape_0_tracksB32)) __pyx_t_8 = 0;
      if (unlikely(__pyx_t_8 != -1)) {
        __Pyx_RaiseBufferIndexError(__pyx_t_8);
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_1 = *__Pyx_BufPtrStrided1d(PyObject **, __pyx_bstruct_tracksB32.buf, __pyx_t_26, __pyx_bstride_0_tracksB32);
      __Pyx_INCREF((PyObject*)__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 = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_25 = ((PyArrayObject *)__pyx_t_1);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
        __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_t2, (PyObject*)__pyx_t_25, &__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_bstruct_t2, (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_bstride_0_t2 = __pyx_bstruct_t2.strides[0]; __pyx_bstride_1_t2 = __pyx_bstruct_t2.strides[1];
        __pyx_bshape_0_t2 = __pyx_bstruct_t2.shape[0]; __pyx_bshape_1_t2 = __pyx_bstruct_t2.shape[1];
        if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 528; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_25 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_t2));
      __pyx_v_t2 = ((PyArrayObject *)__pyx_t_1);
      __pyx_t_1 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":529
 *         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]);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":530
 *             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);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":531
 *             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_27 = __pyx_v_i;
      __pyx_t_28 = __pyx_v_j;
      __pyx_t_8 = -1;
      if (unlikely(__pyx_t_27 >= (size_t)__pyx_bshape_0_DM)) __pyx_t_8 = 0;
      if (unlikely(__pyx_t_28 >= (size_t)__pyx_bshape_1_DM)) __pyx_t_8 = 1;
      if (unlikely(__pyx_t_8 != -1)) {
        __Pyx_RaiseBufferIndexError(__pyx_t_8);
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 531; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_bstruct_DM.buf, __pyx_t_27, __pyx_bstride_0_DM, __pyx_t_28, __pyx_bstride_1_DM) = __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);
    }
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":533
 *             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_3);
  __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_bstruct_DM);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracksB32);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracksA32);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.bundles_distances_mam");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_DM);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracksB32);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t2);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_t1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_tracksA32);
  __pyx_L2:;
  __Pyx_DECREF(__pyx_v_longest_track_lenA);
  __Pyx_DECREF(__pyx_v_longest_track_lenB);
  __Pyx_DECREF((PyObject *)__pyx_v_tracksA32);
  __Pyx_DECREF((PyObject *)__pyx_v_tracksB32);
  __Pyx_DECREF((PyObject *)__pyx_v_DM);
  __Pyx_DECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_DECREF((PyObject *)__pyx_v_t1);
  __Pyx_DECREF((PyObject *)__pyx_v_t2);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":539
 * 
 * @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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":548
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":549
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":553
 *                   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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":556
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":557
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":558
 *         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_min_t1t2[__pyx_v_t1_pi]);
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":559
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":560
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":561
 *     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_min_t2t1[__pyx_v_t2_pi]);
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":562
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":563
 *         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:
 */
  switch (__pyx_v_metric_type) {
    case 0:

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":564
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":565
 *     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:

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":566
 *         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);
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":567
 *     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*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":569
 *             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;

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":571
 *             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);
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":572
 *     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*/ {

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":575
 *         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;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":578
 * 
 * @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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":588
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":589
 *         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;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":590
 *     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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":591
 *         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;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":593
 *         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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":597
 *     # 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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":599
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":600
 *         # 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++) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":601
 *         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]));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":602
 *         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]));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":603
 *             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]));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":604
 *             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));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":605
 *             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]));
      if (__pyx_t_3) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":606
 *             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:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":607
 *             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]));
      if (__pyx_t_3) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":608
 *                 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:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":609
 *             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 += 3;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":610
 *                 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 += 3;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":612
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":613
 *     # 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]));
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":614
 *     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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":615
 *         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]));
  }

}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":619
 * 
 * 
 * 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_mam_distances(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_mam_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_mam_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;
  PyArrayObject *__pyx_v_track1;
  PyArrayObject *__pyx_v_track2;
  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;
  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;
  Py_buffer __pyx_bstruct_track1;
  Py_ssize_t __pyx_bstride_0_track1 = 0;
  Py_ssize_t __pyx_bstride_1_track1 = 0;
  Py_ssize_t __pyx_bshape_0_track1 = 0;
  Py_ssize_t __pyx_bshape_1_track1 = 0;
  Py_buffer __pyx_bstruct_track2;
  Py_ssize_t __pyx_bstride_0_track2 = 0;
  Py_ssize_t __pyx_bstride_1_track2 = 0;
  Py_ssize_t __pyx_bshape_0_track2 = 0;
  Py_ssize_t __pyx_bshape_1_track2 = 0;
  Py_buffer __pyx_bstruct_distances_buffer;
  Py_ssize_t __pyx_bstride_0_distances_buffer = 0;
  Py_ssize_t __pyx_bshape_0_distances_buffer = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz1,&__pyx_n_s__xyz2,&__pyx_n_s__metric,0};
  __Pyx_RefNannySetupContext("mam_distances");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[3] = {0,0,0};
    values[2] = ((PyObject *)__pyx_n_s__all);
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("mam_distances", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __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, PyTuple_GET_SIZE(__pyx_args), "mam_distances") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_xyz1 = values[0];
    __pyx_v_xyz2 = values[1];
    __pyx_v_metric = values[2];
  } else {
    __pyx_v_metric = ((PyObject *)__pyx_n_s__all);
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  3:
      __pyx_v_metric = PyTuple_GET_ITEM(__pyx_args, 2);
      case  2:
      __pyx_v_xyz2 = PyTuple_GET_ITEM(__pyx_args, 1);
      __pyx_v_xyz1 = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 619; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.mam_distances");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_track1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_track2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_distances_buffer = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_track1.buf = NULL;
  __pyx_bstruct_track2.buf = NULL;
  __pyx_bstruct_distances_buffer.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":667
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __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 = 667; __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 = 667; __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 = 667; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_1, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 667; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track1);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track1, (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_bstruct_track1, (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_bstride_0_track1 = __pyx_bstruct_track1.strides[0]; __pyx_bstride_1_track1 = __pyx_bstruct_track1.strides[1];
    __pyx_bshape_0_track1 = __pyx_bstruct_track1.shape[0]; __pyx_bshape_1_track1 = __pyx_bstruct_track1.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 667; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track1));
  __pyx_v_track1 = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":668
 *         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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":669
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 669; __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 = 669; __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 = 669; __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 = 669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_4, ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__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 = 669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_10 = ((PyArrayObject *)__pyx_t_2);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track2, (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_bstruct_track2, (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_bstride_0_track2 = __pyx_bstruct_track2.strides[0]; __pyx_bstride_1_track2 = __pyx_bstruct_track2.strides[1];
    __pyx_bshape_0_track2 = __pyx_bstruct_track2.shape[0]; __pyx_bshape_1_track2 = __pyx_bstruct_track2.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_10 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track2));
  __pyx_v_track2 = ((PyArrayObject *)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":670
 *     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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":675
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __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 = 675; __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 = 675; __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 = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 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 = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_11 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __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 = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  __pyx_t_11 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_2, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__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 = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_11);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_distances_buffer, (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_bstruct_distances_buffer, (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_bstride_0_distances_buffer = __pyx_bstruct_distances_buffer.strides[0];
    __pyx_bshape_0_distances_buffer = __pyx_bstruct_distances_buffer.shape[0];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 675; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_distances_buffer));
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":676
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":677
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":681
 *                   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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":684
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":685
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":686
 *         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_min_t1t2[__pyx_v_t1_pi]);
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":687
 *     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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 687; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_mean_t1t2 = (__pyx_v_mean_t1t2 / __pyx_v_t1_len);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":688
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":689
 *     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_min_t2t1[__pyx_v_t2_pi]);
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":690
 *     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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 690; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_mean_t2t1 = (__pyx_v_mean_t2t1 / __pyx_v_t2_len);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":691
 *         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); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 691; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  if (__pyx_t_14) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":692
 *     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 = 692; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":693
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_2 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__min); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __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 = 693; __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 = 693; __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 = 693; __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 = 693; __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, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 693; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":694
 *         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_GetName(__pyx_m, __pyx_n_s__np); 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);
    __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__max); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __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 = 694; __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 = 694; __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 = 694; __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 = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_15);
    __Pyx_GIVEREF(__pyx_t_15);
    __pyx_t_15 = 0;
    __pyx_t_15 = PyObject_Call(__pyx_t_2, __pyx_t_4, NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 694; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__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 = 692; __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 = __pyx_t_4;
    __pyx_t_4 = 0;
    goto __pyx_L0;
    goto __pyx_L10;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":695
 *                 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); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 695; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (__pyx_t_14) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":696
 *                 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 = 696; __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_L10;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":697
 *     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); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 697; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  if (__pyx_t_14) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":698
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_15 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__min); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __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 = 698; __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 = 698; __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 = 698; __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 = 698; __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_t_11 = 0;
    __pyx_t_11 = PyObject_Call(__pyx_t_15, __pyx_t_3, NULL); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 698; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_r = __pyx_t_11;
    __pyx_t_11 = 0;
    goto __pyx_L0;
    goto __pyx_L10;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":699
 *     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); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_11); if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 699; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  if (__pyx_t_14) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":700
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_11);
    __pyx_t_3 = PyObject_GetAttr(__pyx_t_11, __pyx_n_s__max); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __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 = 700; __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 = 700; __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 = 700; __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 = 700; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    PyTuple_SET_ITEM(__pyx_t_15, 0, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    __pyx_t_4 = 0;
    __pyx_t_4 = PyObject_Call(__pyx_t_3, __pyx_t_15, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 700; __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_15); __pyx_t_15 = 0;
    __pyx_r = __pyx_t_4;
    __pyx_t_4 = 0;
    goto __pyx_L0;
    goto __pyx_L10;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":702
 *         return np.max((mean_t2t1,mean_t1t2))
 *     else :
 *         ValueError('Wrong argument for metric')             # <<<<<<<<<<<<<<
 * 
 * 
 */
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_INCREF(((PyObject *)__pyx_kp_s_2));
    PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_kp_s_2));
    __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_2));
    __pyx_t_15 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 702; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_15);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0;
  }
  __pyx_L10:;

  __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_bstruct_track1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.mam_distances");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_track1);
  __Pyx_DECREF((PyObject *)__pyx_v_track2);
  __Pyx_DECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_minimum_closest_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_minimum_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_minimum_closest_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz1 = 0;
  PyObject *__pyx_v_xyz2 = 0;
  PyArrayObject *__pyx_v_track1;
  PyArrayObject *__pyx_v_track2;
  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;
  size_t __pyx_v_t1_pi;
  size_t __pyx_v_t2_pi;
  double __pyx_v_min_min_t2t1;
  double __pyx_v_min_min_t1t2;
  Py_buffer __pyx_bstruct_track1;
  Py_ssize_t __pyx_bstride_0_track1 = 0;
  Py_ssize_t __pyx_bstride_1_track1 = 0;
  Py_ssize_t __pyx_bshape_0_track1 = 0;
  Py_ssize_t __pyx_bshape_1_track1 = 0;
  Py_buffer __pyx_bstruct_track2;
  Py_ssize_t __pyx_bstride_0_track2 = 0;
  Py_ssize_t __pyx_bstride_1_track2 = 0;
  Py_ssize_t __pyx_bshape_0_track2 = 0;
  Py_ssize_t __pyx_bshape_1_track2 = 0;
  Py_buffer __pyx_bstruct_distances_buffer;
  Py_ssize_t __pyx_bstride_0_distances_buffer = 0;
  Py_ssize_t __pyx_bshape_0_distances_buffer = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz1,&__pyx_n_s__xyz2,0};
  __Pyx_RefNannySetupContext("minimum_closest_distance");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("minimum_closest_distance", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "minimum_closest_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_xyz1 = values[0];
    __pyx_v_xyz2 = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_xyz1 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_xyz2 = PyTuple_GET_ITEM(__pyx_args, 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 = 705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.minimum_closest_distance");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_track1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_track2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_distances_buffer = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_track1.buf = NULL;
  __pyx_bstruct_track2.buf = NULL;
  __pyx_bstruct_distances_buffer.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":735
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 735; __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 = 735; __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 = 735; __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 = 735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_1, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track1);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track1, (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_bstruct_track1, (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_bstride_0_track1 = __pyx_bstruct_track1.strides[0]; __pyx_bstride_1_track1 = __pyx_bstruct_track1.strides[1];
    __pyx_bshape_0_track1 = __pyx_bstruct_track1.shape[0]; __pyx_bshape_1_track1 = __pyx_bstruct_track1.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 735; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track1));
  __pyx_v_track1 = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":736
 *         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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":737
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __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 = 737; __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 = 737; __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 = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_4, ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__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 = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_10 = ((PyArrayObject *)__pyx_t_2);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track2, (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_bstruct_track2, (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_bstride_0_track2 = __pyx_bstruct_track2.strides[0]; __pyx_bstride_1_track2 = __pyx_bstruct_track2.strides[1];
    __pyx_bshape_0_track2 = __pyx_bstruct_track2.shape[0]; __pyx_bshape_1_track2 = __pyx_bstruct_track2.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 737; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_10 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track2));
  __pyx_v_track2 = ((PyArrayObject *)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":738
 *     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]);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":743
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __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 = 743; __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 = 743; __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 = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  PyTuple_SET_ITEM(__pyx_t_2, 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 = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_11 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__float32); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __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 = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;
  __pyx_t_11 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_2, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_11);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__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 = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_12 = ((PyArrayObject *)__pyx_t_11);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_distances_buffer, (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_bstruct_distances_buffer, (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_bstride_0_distances_buffer = __pyx_bstruct_distances_buffer.strides[0];
    __pyx_bshape_0_distances_buffer = __pyx_bstruct_distances_buffer.shape[0];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 743; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_12 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_distances_buffer));
  __pyx_v_distances_buffer = ((PyArrayObject *)__pyx_t_11);
  __pyx_t_11 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":744
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":745
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":749
 *                   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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":752
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":753
 *         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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":754
 *         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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":755
 *         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]));
    if (__pyx_t_15) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":756
 *     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_L8;
    }
    __pyx_L8:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":757
 *         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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":758
 *             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]));
    if (__pyx_t_15) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":759
 *     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_L11;
    }
    __pyx_L11:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":760
 *         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 = 760; __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_bstruct_track1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.minimum_closest_distance");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track2);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_distances_buffer);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_track1);
  __Pyx_DECREF((PyObject *)__pyx_v_track2);
  __Pyx_DECREF((PyObject *)__pyx_v_distances_buffer);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":763
 * 
 * 
 * def lee_perpendicular_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Based on Lee , Han & Whang SIGMOD07.
 *         Calculates perpendicular distance metric for the distance between two line segments
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_lee_perpendicular_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_lee_perpendicular_distance[] = " Based on Lee , Han & Whang SIGMOD07.\n        Calculates perpendicular distance metric for the distance between two line segments\n        \n    This function assumes that norm(end0-start0)>norm(end1-start1)\n    i.e. that the first segment will be bigger than the second one.\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    Examples\n    ---------\n    >>> import dipy.core.performance as pf\n    >>> pf.lee_perpendicular_distance([0,0,0],[1,0,0],[3,4,5],[5,4,3])\n    5.9380966767403436  \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    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_lee_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;
  PyArrayObject *__pyx_v_fvec1;
  PyArrayObject *__pyx_v_fvec2;
  PyArrayObject *__pyx_v_fvec3;
  PyArrayObject *__pyx_v_fvec4;
  Py_buffer __pyx_bstruct_fvec4;
  Py_ssize_t __pyx_bstride_0_fvec4 = 0;
  Py_ssize_t __pyx_bshape_0_fvec4 = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec3;
  Py_ssize_t __pyx_bstride_0_fvec3 = 0;
  Py_ssize_t __pyx_bshape_0_fvec3 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__start0,&__pyx_n_s__end0,&__pyx_n_s__start1,&__pyx_n_s__end1,0};
  __Pyx_RefNannySetupContext("lee_perpendicular_distance");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[4] = {0,0,0,0};
    switch (PyTuple_GET_SIZE(__pyx_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;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start0);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end0);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  2:
      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start1);
      if (likely(values[2])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  3:
      values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end1);
      if (likely(values[3])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("lee_perpendicular_distance", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "lee_perpendicular_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 763; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_start0 = values[0];
    __pyx_v_end0 = values[1];
    __pyx_v_start1 = values[2];
    __pyx_v_end1 = values[3];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_start0 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_end0 = PyTuple_GET_ITEM(__pyx_args, 1);
    __pyx_v_start1 = PyTuple_GET_ITEM(__pyx_args, 2);
    __pyx_v_end1 = PyTuple_GET_ITEM(__pyx_args, 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 = 763; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.lee_perpendicular_distance");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec3 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec4 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;
  __pyx_bstruct_fvec3.buf = NULL;
  __pyx_bstruct_fvec4.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":811
 *     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 = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1, (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_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
    __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec1));
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":812
 * 
 *     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 = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2, (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_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
    __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 812; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec2));
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":813
 *     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 = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec3, (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_bstruct_fvec3, (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_bstride_0_fvec3 = __pyx_bstruct_fvec3.strides[0];
    __pyx_bshape_0_fvec3 = __pyx_bstruct_fvec3.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec3));
  __pyx_v_fvec3 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":814
 *     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 = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec4);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec4, (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_bstruct_fvec4, (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_bstride_0_fvec4 = __pyx_bstruct_fvec4.strides[0];
    __pyx_bshape_0_fvec4 = __pyx_bstruct_fvec4.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec4));
  __pyx_v_fvec4 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":816
 *     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 = 816; __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_bstruct_fvec4);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.lee_perpendicular_distance");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec4);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec3);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec4);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":819
 * 
 * 
 * cdef float clee_perpendicular_distance(float *start0, float *end0,float *start1, float *end1):             # <<<<<<<<<<<<<<
 *     '''
 *     This function assumes that
 */

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;
  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;
  float __pyx_t_1;
  int __pyx_t_2;
  float __pyx_t_3;
  __Pyx_RefNannySetupContext("clee_perpendicular_distance");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":829
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":830
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":832
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":833
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":840
 *     #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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":841
 *     #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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_u1 = (__pyx_t_1 / __pyx_v_l0);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":843
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":844
 * 
 *     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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_u2 = (__pyx_t_1 / __pyx_v_l0);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":846
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":847
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":849
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":850
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":855
 *     #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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":856
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":858
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":859
 * 
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":861
 *     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.);
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":862
 * 
 *     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)) {
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_r = (__pyx_t_1 / __pyx_t_3);
    goto __pyx_L0;
    goto __pyx_L3;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":864
 *         return (lperp1*lperp1+lperp2*lperp2)/(lperp1+lperp2)
 *     else:
 *         return 0.             # <<<<<<<<<<<<<<
 * 
 * def lee_angle_distance(start0, end0, start1, end1):
 */
    __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_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":866
 *         return 0.
 * 
 * def lee_angle_distance(start0, end0, start1, end1):             # <<<<<<<<<<<<<<
 *     ''' Based on Lee , Han & Whang SIGMOD07.
 *         Calculates angle distance metric for the distance between two line segments
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_lee_angle_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_lee_angle_distance[] = " Based on Lee , Han & Whang SIGMOD07.\n        Calculates angle distance metric for the distance between two line segments\n            \n    This function assumes that norm(end0-start0)>norm(end1-start1)\n    i.e. that the 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    Examples\n    ---------\n    >>> import dipy.tracking.distances as pf \n    >>> pf.lee_angle_distance([0,0,0],[1,0,0],[3,4,5],[5,4,3])\n    2.0 \n    \n    Notes\n    -------\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    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_lee_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;
  PyArrayObject *__pyx_v_fvec1;
  PyArrayObject *__pyx_v_fvec2;
  PyArrayObject *__pyx_v_fvec3;
  PyArrayObject *__pyx_v_fvec4;
  Py_buffer __pyx_bstruct_fvec4;
  Py_ssize_t __pyx_bstride_0_fvec4 = 0;
  Py_ssize_t __pyx_bshape_0_fvec4 = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec3;
  Py_ssize_t __pyx_bstride_0_fvec3 = 0;
  Py_ssize_t __pyx_bshape_0_fvec3 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  int __pyx_t_2;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__start0,&__pyx_n_s__end0,&__pyx_n_s__start1,&__pyx_n_s__end1,0};
  __Pyx_RefNannySetupContext("lee_angle_distance");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[4] = {0,0,0,0};
    switch (PyTuple_GET_SIZE(__pyx_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;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start0);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end0);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  2:
      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__start1);
      if (likely(values[2])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  3:
      values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__end1);
      if (likely(values[3])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("lee_angle_distance", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "lee_angle_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 866; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_start0 = values[0];
    __pyx_v_end0 = values[1];
    __pyx_v_start1 = values[2];
    __pyx_v_end1 = values[3];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_start0 = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_end0 = PyTuple_GET_ITEM(__pyx_args, 1);
    __pyx_v_start1 = PyTuple_GET_ITEM(__pyx_args, 2);
    __pyx_v_end1 = PyTuple_GET_ITEM(__pyx_args, 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 = 866; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.lee_angle_distance");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec3 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec4 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;
  __pyx_bstruct_fvec3.buf = NULL;
  __pyx_bstruct_fvec4.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":903
 *     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 = 903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1, (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_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
    __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec1));
  __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":904
 * 
 *     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 = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2, (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_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
    __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 904; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec2));
  __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":905
 *     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 = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec3, (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_bstruct_fvec3, (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_bstride_0_fvec3 = __pyx_bstruct_fvec3.strides[0];
    __pyx_bshape_0_fvec3 = __pyx_bstruct_fvec3.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 905; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec3));
  __pyx_v_fvec3 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":906
 *     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 = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec4);
    __pyx_t_2 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec4, (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_bstruct_fvec4, (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_bstride_0_fvec4 = __pyx_bstruct_fvec4.strides[0];
    __pyx_bshape_0_fvec4 = __pyx_bstruct_fvec4.shape[0];
    if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 906; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __Pyx_DECREF(((PyObject *)__pyx_v_fvec4));
  __pyx_v_fvec4 = ((PyArrayObject *)__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":908
 *     fvec4 = as_float_3vec(end1)
 * 
 *     return clee_angle_distance(<float *>fvec1.data,<float *>fvec2.data,<float *>fvec3.data,<float *>fvec4.data)             # <<<<<<<<<<<<<<
 * 
 * cdef float clee_angle_distance(float *start0, float *end0,float *start1, float *end1):
 */
  __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 = 908; __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_bstruct_fvec4);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.lee_angle_distance");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec4);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec3);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec4);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":910
 *     return clee_angle_distance(<float *>fvec1.data,<float *>fvec2.data,<float *>fvec3.data,<float *>fvec4.data)
 * 
 * cdef float clee_angle_distance(float *start0, float *end0,float *start1, float *end1):             # <<<<<<<<<<<<<<
 *     '''
 *     This function assumes that
 */

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;
  float __pyx_t_1;
  float __pyx_t_2;
  __Pyx_RefNannySetupContext("clee_angle_distance");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":920
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":921
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":924
 *     #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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":925
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":928
 *     #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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":930
 *     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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 930; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_v_cos_theta_squared = (__pyx_t_1 / __pyx_t_2);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":932
 *     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_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":935
 * 
 * 
 * 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_approx_polygon_track(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_approx_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    >>> from fos.tracking.distances import approx_polygon_track\n    >>> #approximating a helix\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 PyObject *__pyx_pf_4dipy_8tracking_9distances_approx_polygon_track(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz = 0;
  PyObject *__pyx_v_alpha = 0;
  int __pyx_v_mid_index;
  PyArrayObject *__pyx_v_track;
  float *__pyx_v_fvec0;
  float *__pyx_v_fvec1;
  float *__pyx_v_fvec2;
  PyObject *__pyx_v_characteristic_points;
  size_t __pyx_v_t_len;
  double __pyx_v_angle;
  double __pyx_v_tmp;
  float __pyx_v_vec0[3];
  float __pyx_v_vec1[3];
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz,&__pyx_n_s__alpha,0};
  __Pyx_RefNannySetupContext("approx_polygon_track");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    values[1] = __pyx_k_3;
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "approx_polygon_track") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 935; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_xyz = values[0];
    __pyx_v_alpha = values[1];
  } else {
    __pyx_v_alpha = __pyx_k_3;
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: __pyx_v_alpha = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_xyz = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 935; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.approx_polygon_track");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_track = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_characteristic_points = Py_None; __Pyx_INCREF(Py_None);
  __pyx_bstruct_track.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":988
 *         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 = 988; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_angle = __pyx_t_1;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":989
 * 
 *     angle=alpha
 *     track = np.ascontiguousarray(xyz, dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     t_len=len(track)
 *     characteristic_points=[track[0]]
 */
  __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __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 = 989; __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 = 989; __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 = 989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_2, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__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 = 989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __pyx_t_7 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
    __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
    if (unlikely(__pyx_t_7 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 989; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_6 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track));
  __pyx_v_track = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":990
 *     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 = 990; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_t_len = __pyx_t_11;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":991
 *     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); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 991; __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 = 991; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  PyList_SET_ITEM(__pyx_t_4, 0, __pyx_t_5);
  __Pyx_GIVEREF(__pyx_t_5);
  __pyx_t_5 = 0;
  __Pyx_DECREF(__pyx_v_characteristic_points);
  __pyx_v_characteristic_points = ((PyObject *)__pyx_t_4);
  __pyx_t_4 = 0;

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

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":995
 *     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));
    if (!__pyx_t_12) break;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":998
 *         #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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 998; __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 = 998; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":999
 *         #<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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 999; __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 = 999; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1000
 *         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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1000; __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 = 1000; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1002
 *         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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1003
 *         #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 isnan(tmp) :
 */
    __pyx_f_4dipy_8tracking_9distances_csub_3vecs(__pyx_v_fvec2, __pyx_v_fvec1, __pyx_v_vec1);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1004
 *         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 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)) {
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1004; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_v_tmp = ((double)fabs(acos((__pyx_t_14 / __pyx_t_15))));

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

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1006
 *         tmp=<double>fabs(acos(cinner_3vecs(vec0,vec1)/(cnorm_3vec(vec0)*cnorm_3vec(vec1))))
 *         if isnan(tmp) :
 *             angle+=0.             # <<<<<<<<<<<<<<
 *         else:
 *             angle+=tmp
 */
      __pyx_v_angle += 0.;
      goto __pyx_L8;
    }
    /*else*/ {

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1009
 *         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 = 1009; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __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 = 1009; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (__pyx_t_12) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1010
 *             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); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1010; __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 = 1010; __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;

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

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1014
 *         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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1014; __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 = 1014; __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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1015
 * 
 *     characteristic_points.append(track[-1])
 *     return np.array(characteristic_points)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_5 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__array); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __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 = 1015; __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, __pyx_t_5, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1015; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__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_bstruct_track);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.approx_polygon_track");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_track);
  __Pyx_DECREF(__pyx_v_characteristic_points);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_approximate_mdl_trajectory(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_approximate_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 PyObject *__pyx_pf_4dipy_8tracking_9distances_approximate_mdl_trajectory(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_xyz = 0;
  PyObject *__pyx_v_alpha = 0;
  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;
  size_t __pyx_v_t_len;
  PyArrayObject *__pyx_v_track;
  float __pyx_v_tmp[3];
  PyArrayObject *__pyx_v_fvec1;
  PyArrayObject *__pyx_v_fvec2;
  PyArrayObject *__pyx_v_fvec3;
  PyArrayObject *__pyx_v_fvec4;
  Py_buffer __pyx_bstruct_fvec3;
  Py_ssize_t __pyx_bstride_0_fvec3 = 0;
  Py_ssize_t __pyx_bshape_0_fvec3 = 0;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  Py_buffer __pyx_bstruct_fvec4;
  Py_ssize_t __pyx_bstride_0_fvec4 = 0;
  Py_ssize_t __pyx_bshape_0_fvec4 = 0;
  Py_buffer __pyx_bstruct_fvec1;
  Py_ssize_t __pyx_bstride_0_fvec1 = 0;
  Py_ssize_t __pyx_bshape_0_fvec1 = 0;
  Py_buffer __pyx_bstruct_fvec2;
  Py_ssize_t __pyx_bstride_0_fvec2 = 0;
  Py_ssize_t __pyx_bshape_0_fvec2 = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__xyz,&__pyx_n_s__alpha,0};
  __Pyx_RefNannySetupContext("approximate_mdl_trajectory");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    values[1] = __pyx_k_4;
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__xyz);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "approximate_mdl_trajectory") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1018; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_xyz = values[0];
    __pyx_v_alpha = values[1];
  } else {
    __pyx_v_alpha = __pyx_k_4;
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: __pyx_v_alpha = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_xyz = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 1018; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.approximate_mdl_trajectory");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_characteristic_points = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_track = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec3 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fvec4 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_track.buf = NULL;
  __pyx_bstruct_fvec1.buf = NULL;
  __pyx_bstruct_fvec2.buf = NULL;
  __pyx_bstruct_fvec3.buf = NULL;
  __pyx_bstruct_fvec4.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1045
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1045; __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 = 1045; __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 = 1045; __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 = 1045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_1, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 1045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
    __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1045; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track));
  __pyx_v_track = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1046
 * 
 *     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 = 1046; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_t_len = __pyx_t_10;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1048
 *     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 = 1048; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_alphac = __pyx_t_11;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1049
 * 
 *     alphac=alpha
 *     characteristic_points=[xyz[0]]             # <<<<<<<<<<<<<<
 *     start_index = 0
 *     length = 2
 */
  __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_xyz, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1049; __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 = 1049; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_3));
  PyList_SET_ITEM(__pyx_t_3, 0, __pyx_t_4);
  __Pyx_GIVEREF(__pyx_t_4);
  __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_v_characteristic_points);
  __pyx_v_characteristic_points = ((PyObject *)__pyx_t_3);
  __pyx_t_3 = 0;

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

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1054
 *     #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));
    if (!__pyx_t_12) break;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1055
 * 
 *     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);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1056
 *     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); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __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 = 1056; __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_bstruct_fvec1);
      __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec1, (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_bstruct_fvec1, (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_bstride_0_fvec1 = __pyx_bstruct_fvec1.strides[0];
      __pyx_bshape_0_fvec1 = __pyx_bstruct_fvec1.shape[0];
      if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1056; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __Pyx_DECREF(((PyObject *)__pyx_v_fvec1));
    __pyx_v_fvec1 = ((PyArrayObject *)__pyx_t_4);
    __pyx_t_4 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1057
 *         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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __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 = 1057; __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_bstruct_fvec2);
      __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec2, (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_bstruct_fvec2, (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_bstride_0_fvec2 = __pyx_bstruct_fvec2.strides[0];
      __pyx_bshape_0_fvec2 = __pyx_bstruct_fvec2.shape[0];
      if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1057; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __Pyx_DECREF(((PyObject *)__pyx_v_fvec2));
    __pyx_v_fvec2 = ((PyArrayObject *)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1059
 *         fvec2 = as_float_3vec(track[current_index])
 *         # L(H)
 *         csub_3vecs(<float *>fvec2.data,<float *>fvec1.data,tmp)             # <<<<<<<<<<<<<<
 *         cost_par=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);

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

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1065
 *         # 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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1067
 *         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 += 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); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1067; __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 = 1067; __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_bstruct_fvec3);
        __pyx_t_14 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec3, (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_bstruct_fvec3, (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_bstride_0_fvec3 = __pyx_bstruct_fvec3.strides[0];
        __pyx_bshape_0_fvec3 = __pyx_bstruct_fvec3.shape[0];
        if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1067; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __Pyx_DECREF(((PyObject *)__pyx_v_fvec3));
      __pyx_v_fvec3 = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1068
 *             #print i
 *             fvec3 = as_float_3vec(track[i])
 *             fvec4 = as_float_3vec(track[i+1])             # <<<<<<<<<<<<<<
 *             cost_par += log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             cost_par += 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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1068; __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 = 1068; __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_bstruct_fvec4);
        __pyx_t_14 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_fvec4, (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_bstruct_fvec4, (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_bstride_0_fvec4 = __pyx_bstruct_fvec4.strides[0];
        __pyx_bshape_0_fvec4 = __pyx_bstruct_fvec4.shape[0];
        if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1068; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __Pyx_DECREF(((PyObject *)__pyx_v_fvec4));
      __pyx_v_fvec4 = ((PyArrayObject *)__pyx_t_3);
      __pyx_t_3 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1069
 *             fvec3 = as_float_3vec(track[i])
 *             fvec4 = as_float_3vec(track[i+1])
 *             cost_par += log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))             # <<<<<<<<<<<<<<
 *             cost_par += 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 += 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)));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1070
 *             fvec4 = as_float_3vec(track[i+1])
 *             cost_par += log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             cost_par += 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 += log2(cinner_3vecs(tmp,tmp))
 */
      __pyx_v_cost_par += 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)));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1071
 *             cost_par += log2(clee_perpendicular_distance(<float *>fvec3.data,<float *>fvec4.data,<float *>fvec1.data,<float *>fvec2.data))
 *             cost_par += 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 += 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);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1072
 *             cost_par += 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 += log2(cinner_3vecs(tmp,tmp))             # <<<<<<<<<<<<<<
 *         cost_nopar /= 2
 *         #print cost_par, cost_nopar, start_index,length
 */
      __pyx_v_cost_nopar += log2(__pyx_f_4dipy_8tracking_9distances_cinner_3vecs(__pyx_v_tmp, __pyx_v_tmp));
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1073
 *             csub_3vecs(<float *>fvec4.data,<float *>fvec3.data,tmp)
 *             cost_nopar += 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 /= 2.0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1075
 *         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);
    if (__pyx_t_12) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1076
 *         #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); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1076; __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 = 1076; __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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1077
 *         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);

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

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1081
 *         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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1081; __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 = 1081; __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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1082
 *             length+=1
 *     characteristic_points.append(track[-1])
 *     return np.array(characteristic_points)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__array); 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_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 = 1082; __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, __pyx_t_3, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1082; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__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_bstruct_fvec3);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec4);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.approximate_mdl_trajectory");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec3);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec4);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec1);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_fvec2);
  __pyx_L2:;
  __Pyx_DECREF(__pyx_v_characteristic_points);
  __Pyx_DECREF((PyObject *)__pyx_v_track);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec1);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec2);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec3);
  __Pyx_DECREF((PyObject *)__pyx_v_fvec4);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1085
 * 
 * 
 * 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_intersect_segment_cylinder(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_intersect_segment_cylinder[] = "\n    Intersect Segment S(t) = sa +t(sb-sa), 0 <=t<= 1 against cylinder specified by p,q and r\n    Look p.197 from Real Time Collision Detection C. Ericson\n    \n    Examples\n    ----------\n    >>> from dipy.tracking.distances import intersect_segment_cylinder as isc\n    >>> # Define cylinder using a segment defined by \n    >>> p=np.array([0,0,0],dtype=float32)\n    >>> q=np.array([1,0,0],dtype=float32)\n    >>> r=0.5\n    >>> # Define segment\n    >>> sa=np.array([0.5,1 ,0],dtype=float32)\n    >>> sb=np.array([0.5,-1,0],dtype=float32)    \n    >>> isc(sa,sb,p,q,r)\n    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_intersect_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;
  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;
  PyObject *__pyx_r = NULL;
  float __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  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};
  __Pyx_RefNannySetupContext("intersect_segment_cylinder");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[5] = {0,0,0,0,0};
    switch (PyTuple_GET_SIZE(__pyx_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;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sa);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sb);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  2:
      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__p);
      if (likely(values[2])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  3:
      values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__q);
      if (likely(values[3])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  4:
      values[4] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__r);
      if (likely(values[4])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("intersect_segment_cylinder", 1, 5, 5, 4); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "intersect_segment_cylinder") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1085; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __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];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 5) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_sa = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_sb = PyTuple_GET_ITEM(__pyx_args, 1);
    __pyx_v_p = PyTuple_GET_ITEM(__pyx_args, 2);
    __pyx_v_q = PyTuple_GET_ITEM(__pyx_args, 3);
    __pyx_v_r = PyTuple_GET_ITEM(__pyx_args, 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 = 1085; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.intersect_segment_cylinder");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_tmp = Py_None; __Pyx_INCREF(Py_None);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1107
 *         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 = 1107; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_csa = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_sa));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1108
 * 
 *     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 = 1108; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_csb = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_sb));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1109
 *     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 = 1109; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cp = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_p));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1110
 *     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 = 1110; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cq = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_q));

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

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

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1115
 *     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 = 1115; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_v_tmp);
  __pyx_v_tmp = __pyx_t_2;
  __pyx_t_2 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1117
 *     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 = 1117; __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 = 1117; __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 = 1117; __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 = __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_r = NULL;
  __pyx_L0:;
  __Pyx_DECREF(__pyx_v_tmp);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1119
 *     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
 *     Look p.197 from Real Time Collision Detection C. Ericson
 */

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;
  int __pyx_t_1;
  int __pyx_t_2;
  int __pyx_t_3;
  float __pyx_t_4;
  __Pyx_RefNannySetupContext("cintersect_segment_cylinder");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1130
 *         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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1132
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1133
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1134
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1136
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1137
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1138
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1141
 * 
 *     #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.);
  if (__pyx_t_1) {
    __pyx_t_2 = ((__pyx_v_md + __pyx_v_nd) < 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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1142
 *     #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);
  if (__pyx_t_3) {
    __pyx_t_1 = ((__pyx_v_md + __pyx_v_nd) > __pyx_v_dd);
    __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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1144
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1145
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1146
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1147
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1148
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1150
 *     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);
  if (__pyx_t_2) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1152
 *     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.);
    if (__pyx_t_2) {
      __pyx_r = 0.;
      goto __pyx_L0;
      goto __pyx_L6;
    }
    __pyx_L6:;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1153
 *         #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.);
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1154
 *         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)) {
        PyErr_Format(PyExc_ZeroDivisionError, "float division");
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1154; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nn);
      goto __pyx_L7;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1155
 *         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);
    if (__pyx_t_2) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1156
 *             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)) {
        PyErr_Format(PyExc_ZeroDivisionError, "float division");
        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1156; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nn);
      goto __pyx_L7;
    }
    /*else*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1158
 *             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:;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1159
 *         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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1161
 *         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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1162
 * 
 *     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));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1163
 *     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.);
  if (__pyx_t_2) {
    __pyx_r = 0.;
    goto __pyx_L0;
    goto __pyx_L8;
  }
  __pyx_L8:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1165
 *     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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_a);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1166
 * 
 *     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)) {
    PyErr_Format(PyExc_ZeroDivisionError, "float division");
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1166; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  (__pyx_v_t[1]) = (__pyx_t_4 / __pyx_v_a);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1167
 *     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.);
  if (!__pyx_t_2) {
    __pyx_t_3 = ((__pyx_v_t[0]) > 1.);
    __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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1169
 *     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.);
  if (__pyx_t_1) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1171
 *     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.);
    if (__pyx_t_1) {
      __pyx_r = 0.;
      goto __pyx_L0;
      goto __pyx_L11;
    }
    __pyx_L11:;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1172
 *         #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)) {
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1172; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nd);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1174
 *         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.);
    if (__pyx_t_1) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1175
 *         #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;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1177
 *             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);
  if (__pyx_t_1) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1179
 *     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.);
    if (__pyx_t_1) {
      __pyx_r = 0.;
      goto __pyx_L0;
      goto __pyx_L13;
    }
    __pyx_L13:;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1180
 *         #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)) {
      PyErr_Format(PyExc_ZeroDivisionError, "float division");
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1180; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    (__pyx_v_t[0]) = (__pyx_t_4 / __pyx_v_nd);

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1182
 *         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.);
    if (__pyx_t_1) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1183
 *         #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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1185
 *             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_r = 0;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1188
 * 
 * 
 * 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_point_segment_sq_distance(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_point_segment_sq_distance[] = " Calculate the squared distance from a point c to a finite line segment ab.\n \n    Examples\n    ----------\n    >>> from dipy.tracking.distances import point_segment_sq_distance \n    >>> a=np.array([0,0,0],dtype=float32)\n    >>> b=np.array([1,0,0],dtype=float32)\n    >>> c=np.array([0,1,0],dtype=float32)    \n    >>> point_segment_sq_distance(a,b,c)\n    1.0\n    >>> c=np.array([0,3,0],dtype=float32)\n    >>> pf.point_segment_sq_distance(a,b,c)\n    9.0 \n    >>> c=np.array([-1,1,0],dtype=float32)\n    >>> pf.point_segment_sq_distance(a,b,c)\n    2.0\n    \n\n    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_point_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;
  float *__pyx_v_ca;
  float *__pyx_v_cb;
  float *__pyx_v_cc;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__a,&__pyx_n_s__b,&__pyx_n_s__c,0};
  __Pyx_RefNannySetupContext("point_segment_sq_distance");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[3] = {0,0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__a);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__b);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("point_segment_sq_distance", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1188; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  2:
      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__c);
      if (likely(values[2])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("point_segment_sq_distance", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1188; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "point_segment_sq_distance") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1188; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_a = values[0];
    __pyx_v_b = values[1];
    __pyx_v_c = values[2];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_a = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_b = PyTuple_GET_ITEM(__pyx_args, 1);
    __pyx_v_c = PyTuple_GET_ITEM(__pyx_args, 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 = 1188; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.point_segment_sq_distance");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1213
 *         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 = 1213; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_ca = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_a));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1214
 * 
 *     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 = 1214; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cb = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_b));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1215
 *     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 = 1215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_cc = __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_v_c));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1217
 *     cc = asfp(c)
 * 
 *     return cpoint_segment_sq_dist(ca, cb, cc)             # <<<<<<<<<<<<<<
 * 
 * @cython.cdivision(True)
 */
  __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 = 1217; __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_r = NULL;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1220
 * 
 * @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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1228
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1229
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1230
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1232
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1234
 *     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.);
  if (__pyx_t_1) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1235
 *     #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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1236
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1237
 *         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);
  if (__pyx_t_1) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1238
 *     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:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1240
 *         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;
}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_track_dist_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_track_dist_3pts[] = " Calculate the euclidean distance between two 3pt tracks\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    >>> import numpy as np\n    >>> from dipy.tracking.distances import track_dist_3pts\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    >>> track_dist_3pts(a,b)\n    \n\n    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_track_dist_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracka = 0;
  PyObject *__pyx_v_trackb = 0;
  PyArrayObject *__pyx_v_a;
  PyArrayObject *__pyx_v_b;
  float __pyx_v_d[2];
  Py_buffer __pyx_bstruct_a;
  Py_ssize_t __pyx_bstride_0_a = 0;
  Py_ssize_t __pyx_bstride_1_a = 0;
  Py_ssize_t __pyx_bshape_0_a = 0;
  Py_ssize_t __pyx_bshape_1_a = 0;
  Py_buffer __pyx_bstruct_b;
  Py_ssize_t __pyx_bstride_0_b = 0;
  Py_ssize_t __pyx_bstride_1_b = 0;
  Py_ssize_t __pyx_bshape_0_b = 0;
  Py_ssize_t __pyx_bshape_1_b = 0;
  PyObject *__pyx_r = NULL;
  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;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracka,&__pyx_n_s__trackb,0};
  __Pyx_RefNannySetupContext("track_dist_3pts");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracka);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__trackb);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("track_dist_3pts", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1244; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "track_dist_3pts") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1244; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracka = values[0];
    __pyx_v_trackb = values[1];
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_tracka = PyTuple_GET_ITEM(__pyx_args, 0);
    __pyx_v_trackb = PyTuple_GET_ITEM(__pyx_args, 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 = 1244; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.track_dist_3pts");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_a = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_b = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_a.buf = NULL;
  __pyx_bstruct_b.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1274
 *     cdef float d[2]
 * 
 *     a=np.ascontiguousarray(tracka,dtype=f32_dt)             # <<<<<<<<<<<<<<
 *     b=np.ascontiguousarray(trackb,dtype=f32_dt)
 * 
 */
  __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1274; __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 = 1274; __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 = 1274; __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 = 1274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_1, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 1274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_a);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_a, (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_bstruct_a, (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_bstride_0_a = __pyx_bstruct_a.strides[0]; __pyx_bstride_1_a = __pyx_bstruct_a.strides[1];
    __pyx_bshape_0_a = __pyx_bstruct_a.shape[0]; __pyx_bshape_1_a = __pyx_bstruct_a.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_a));
  __pyx_v_a = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1275
 * 
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1275; __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 = 1275; __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 = 1275; __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 = 1275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_2 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_4, ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__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 = 1275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_5 = ((PyArrayObject *)__pyx_t_2);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_b);
    __pyx_t_6 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_b, (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_bstruct_b, (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_bstride_0_b = __pyx_bstruct_b.strides[0]; __pyx_bstride_1_b = __pyx_bstruct_b.strides[1];
    __pyx_bshape_0_b = __pyx_bstruct_b.shape[0]; __pyx_bshape_1_b = __pyx_bstruct_b.shape[1];
    if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1275; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_b));
  __pyx_v_b = ((PyArrayObject *)__pyx_t_2);
  __pyx_t_2 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1277
 *     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); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1277; __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 = 1277; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_a), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1277; __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 = 1277; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_a), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1277; __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 = 1277; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1278
 * 
 *     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); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1278; __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 = 1278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_10 = __Pyx_GetItemInt(((PyObject *)__pyx_v_b), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1278; __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 = 1278; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_11 = __Pyx_GetItemInt(((PyObject *)__pyx_v_b), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_11) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1278; __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 = 1278; __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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1280
 *                             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]));
  if (__pyx_t_12) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1281
 * 
 *     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 = 1281; __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_L6;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1283
 *         return d[0]
 *     else:
 *         return d[1]             # <<<<<<<<<<<<<<
 * 
 * @cython.boundscheck(False)
 */
    __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 = 1283; __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_L6:;

  __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_bstruct_a);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_b);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.track_dist_3pts");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_a);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_b);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_a);
  __Pyx_DECREF((PyObject *)__pyx_v_b);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1288
 * @cython.wraparound(False)
 * @cython.cdivision(True)
 * cdef void track_direct_flip_dist(float *a,float *b,long rows,float *out) nogil:             # <<<<<<<<<<<<<<
 *     ''' 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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1326
 *     '''
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1327
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1329
 *         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++) {

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1331
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1332
 *         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++) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1333
 *         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)]));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1334
 *         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)]));

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1335
 *             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_sub * __pyx_v_sub);

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1336
 *             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_subf * __pyx_v_subf);
    }

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

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

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

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

}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1345
 * 
 * @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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1362
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1365
 * 
 *     #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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1366
 *     #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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1367
 *     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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1368
 *         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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1369
 *         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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1370
 *         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]))));
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1372
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1373
 * 
 *     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);

}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_local_skeleton_clustering(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_local_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           \n    Parameters\n    -----------\n    tracks : sequence\n        of tracks as arrays, shape (N,3) .. (N,3) where N=points\n    d_thr : float, average euclidean distance threshold\n\n    Returns\n    --------\n    C : dict\n        \n    Examples\n    ----------\n    >>> from dipy.tracking.distances import local_skeleton_clustering\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,3)\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*                   c    *2\n           \n    is equal to (a+b+c)/3 where a the euclidean distance between t_1[0] and t_2[0], \n    b between  t_1[1] and t_2[1] and c between t_1[2] and t_2[2]. Also the fliped\n    \n    Visualization\n    --------------\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    \n    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_local_skeleton_clustering(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_d_thr = 0;
  PyArrayObject *__pyx_v_track;
  __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;
  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;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  PyObject *__pyx_r = NULL;
  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;
  PyObject *__pyx_t_17 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__d_thr,0};
  __Pyx_RefNannySetupContext("local_skeleton_clustering");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    values[1] = ((PyObject *)__pyx_int_10);
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "local_skeleton_clustering") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1392; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracks = values[0];
    __pyx_v_d_thr = values[1];
  } else {
    __pyx_v_d_thr = ((PyObject *)__pyx_int_10);
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: __pyx_v_d_thr = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_tracks = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 1392; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_track = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_C = ((PyObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_bstruct_track.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1461
 *         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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1462
 *         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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1467
 *         long *flip
 * 
 *     points=len(tracks[0])             # <<<<<<<<<<<<<<
 *     dim = points*3
 *     rows = points
 */
  __pyx_t_1 = __Pyx_GetItemInt(__pyx_v_tracks, 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1467; __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 = 1467; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_v_points = __pyx_t_2;

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

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

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1473
 * 
 *     #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))));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1474
 *     #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))));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1475
 *     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)))));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1476
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1477
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1477; __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); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1477; __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 = 1477; __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 = 1477; __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 = 1477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = PyEval_CallObjectWithKeywords(__pyx_t_4, __pyx_t_5, ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__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 = 1477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_6);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
    __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
    if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1477; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_t_7 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_track));
  __pyx_v_track = ((PyArrayObject *)__pyx_t_6);
  __pyx_t_6 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1478
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1479
 *     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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1480
 *     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]);
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1481
 *     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;

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

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1487
 * 
 *     #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)))));

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1490
 * 
 *     #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 = 1490; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_2;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1491
 *     #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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1492
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_1 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1492; __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); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1492; __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 = 1492; __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 = 1492; __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 = 1492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_5, ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_DECREF(__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 = 1492; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_7 = ((PyArrayObject *)__pyx_t_4);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
      __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1492; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1493
 *     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);

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1496
 *         cit=it
 * 
 *         with nogil:             # <<<<<<<<<<<<<<
 * 
 *             alld=<float *>calloc(lenC,sizeof(float))
 */
    { PyThreadState *_save;
      Py_UNBLOCK_THREADS
      /*try:*/ {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1498
 *         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))));

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1499
 * 
 *             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))));

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1500
 *             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++) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1501
 *             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++) {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1502
 *             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));
          }

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1506
 *                 #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));

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1508
 *                 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]));
          if (__pyx_t_16) {

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

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1510
 *                 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_L19;
          }
          __pyx_L19:;

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1511
 *                     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]);
        }

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

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1515
 *             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++) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1516
 *             #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);
          if (__pyx_t_16) {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1517
 *             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]);

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1518
 *                 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_L22;
          }
          __pyx_L22:;
        }

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1520
 *                     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);
        if (__pyx_t_16) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1521
 * 
 *             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);
          if (__pyx_t_16) {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1522
 *             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++) {

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1523
 *                 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++) {

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1524
 *                     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_v_cluster[__pyx_v_i_k]).hidden[((__pyx_v_i * 3) + __pyx_v_j)]) += (__pyx_v_ptr[((((__pyx_v_rows - 1) - __pyx_v_i) * 3) + __pyx_v_j)]);
              }
            }
            goto __pyx_L24;
          }
          /*else*/ {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1526
 *                             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++) {

              /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1527
 *                 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++) {

                /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1528
 *                      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_v_cluster[__pyx_v_i_k]).hidden[((__pyx_v_i * 3) + __pyx_v_j)]) += (__pyx_v_ptr[((__pyx_v_i * 3) + __pyx_v_j)]);
              }
            }
          }
          __pyx_L24:;

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1529
 *                         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_v_cluster[__pyx_v_i_k]).N += 1;

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1530
 *                             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)))));

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1531
 *                 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_L23;
        }
        /*else*/ {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1534
 * 
 *             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 += 1;

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1535
 *             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)))));

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1536
 *                 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))));

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1537
 *                 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)))));

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1538
 *                 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;

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1539
 *                 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++) {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1540
 *                 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]);
          }

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1541
 *                 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_L23:;

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

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1544
 * 
 *             free(alld)
 *             free(flip)             # <<<<<<<<<<<<<<
 * 
 * 
 */
        free(__pyx_v_flip);
      }

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1496
 *         cit=it
 * 
 *         with nogil:             # <<<<<<<<<<<<<<
 * 
 *             alld=<float *>calloc(lenC,sizeof(float))
 */
      /*finally:*/ {
        Py_BLOCK_THREADS
      }
    }
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1549
 *     #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 = 1549; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __Pyx_DECREF(((PyObject *)__pyx_v_C));
  __pyx_v_C = __pyx_t_4;
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1550
 * 
 *     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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1552
 *     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 = 1552; __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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1552; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1553
 * 
 *         C[k]={}
 *         C[k]['hidden']=np.zeros(points*3,dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *         for j in range(points*3):
 */
    __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_6 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __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 = 1553; __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 = 1553; __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 = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_17 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    if (PyDict_SetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __pyx_t_17 = PyEval_CallObjectWithKeywords(__pyx_t_6, __pyx_t_5, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __Pyx_DECREF(__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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __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_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1553; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1555
 *         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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1556
 * 
 *         for j in range(points*3):
 *             C[k]['hidden'][j]=cluster[k].hidden[j]             # <<<<<<<<<<<<<<
 *         C[k]['hidden'].shape=(points,3)
 * 
 */
      __pyx_t_17 = PyFloat_FromDouble(((__pyx_v_cluster[__pyx_v_k]).hidden[__pyx_v_j])); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __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 = 1556; __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_17, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1556; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1557
 *         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_17 = PyInt_FromLong(__pyx_v_points); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_17);
    __Pyx_GIVEREF(__pyx_t_17);
    __Pyx_INCREF(__pyx_int_3);
    PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_3);
    __Pyx_GIVEREF(__pyx_int_3);
    __pyx_t_17 = 0;
    __pyx_t_17 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __pyx_t_4 = PyObject_GetItem(__pyx_t_17, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1557; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    if (PyObject_SetAttr(__pyx_t_4, __pyx_n_s__shape, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1557; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1559
 *         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 = 1559; __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); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1559; __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 = 1559; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1560
 * 
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __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 = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_17 = PyTuple_New(1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    PyTuple_SET_ITEM(__pyx_t_17, 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 = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__pyx_t_4));
    __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_1 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__int64); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __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 = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_17, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1560; __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 = 1560; __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;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1562
 *         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;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1563
 * 
 *         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 = 1563; __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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_17 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      if (__Pyx_SetItemInt(__pyx_t_17, __pyx_v_i, __pyx_t_1, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1563; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1565
 *             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); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_17 = PyObject_GetItem(__pyx_t_1, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __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 = 1565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_t_17);
    __Pyx_GIVEREF(__pyx_t_17);
    __pyx_t_17 = 0;
    __pyx_t_17 = PyObject_Call(((PyObject *)((PyObject*)&PyList_Type)), __pyx_t_1, NULL); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1565; __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_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1565; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1568
 * 
 *     #Free memory
 *     with nogil:             # <<<<<<<<<<<<<<
 * 
 *         for k from 0<=k<lenC:
 */
  { PyThreadState *_save;
    Py_UNBLOCK_THREADS
    /*try:*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1570
 *     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++) {

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

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

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1568
 * 
 *     #Free memory
 *     with nogil:             # <<<<<<<<<<<<<<
 * 
 *         for k from 0<=k<lenC:
 */
    /*finally:*/ {
      Py_BLOCK_THREADS
    }
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1575
 *         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_17);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_track);
  __Pyx_DECREF(__pyx_v_C);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

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

static PyObject *__pyx_pf_4dipy_8tracking_9distances_local_skeleton_clustering_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_local_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           \n    Parameters\n    -----------\n    tracks : sequence\n        of tracks as arrays, shape (N,3) .. (N,3) where N=3\n    d_thr : float, average euclidean distance threshold\n\n    Returns\n    --------\n    C : dict\n        \n    Examples\n    ----------\n    >>> from dipy.viz import fvtk\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    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 PyObject *__pyx_pf_4dipy_8tracking_9distances_local_skeleton_clustering_3pts(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_tracks = 0;
  PyObject *__pyx_v_d_thr = 0;
  PyArrayObject *__pyx_v_track;
  PyArrayObject *__pyx_v_h;
  int __pyx_v_lent;
  int __pyx_v_k;
  int __pyx_v_it;
  float __pyx_v_d[2];
  PyObject *__pyx_v_C;
  PyObject *__pyx_v_ts;
  Py_ssize_t __pyx_v_lenC;
  PyObject *__pyx_v_alld;
  PyObject *__pyx_v_flip;
  PyObject *__pyx_v_m_k;
  PyObject *__pyx_v_i_k;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  Py_buffer __pyx_bstruct_h;
  Py_ssize_t __pyx_bstride_0_h = 0;
  Py_ssize_t __pyx_bstride_1_h = 0;
  Py_ssize_t __pyx_bshape_0_h = 0;
  Py_ssize_t __pyx_bshape_1_h = 0;
  PyObject *__pyx_r = NULL;
  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;
  PyObject *__pyx_t_6 = NULL;
  int __pyx_t_7;
  int __pyx_t_8;
  PyArrayObject *__pyx_t_9 = NULL;
  int __pyx_t_10;
  PyObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  PyObject *__pyx_t_13 = NULL;
  PyArrayObject *__pyx_t_14 = NULL;
  int __pyx_t_15;
  PyObject *__pyx_t_16 = NULL;
  int __pyx_t_17;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__d_thr,0};
  __Pyx_RefNannySetupContext("local_skeleton_clustering_3pts");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    values[1] = ((PyObject *)__pyx_int_10);
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "local_skeleton_clustering_3pts") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1577; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracks = values[0];
    __pyx_v_d_thr = values[1];
  } else {
    __pyx_v_d_thr = ((PyObject *)__pyx_int_10);
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: __pyx_v_d_thr = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_tracks = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 1577; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering_3pts");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_track = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_h = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_C = ((PyObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_ts = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_alld = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_flip = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_m_k = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_i_k = Py_None; __Pyx_INCREF(Py_None);
  __pyx_bstruct_track.buf = NULL;
  __pyx_bstruct_h.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1626
 *         #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 = 1626; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_1;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1629
 * 
 *     #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 = 1629; __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 = 1629; __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 = 1629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__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 = 1629; __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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_5 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__copy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1629; __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 = 1629; __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 = 1629; __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 = 1629; __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 = 1629; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_v_C));
  __pyx_v_C = __pyx_t_2;
  __pyx_t_2 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1630
 *     #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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __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(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __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 = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_5 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_6 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__float32); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __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 = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  __pyx_t_6 = PyEval_CallObjectWithKeywords(__pyx_t_3, __pyx_t_4, ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1630; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_v_ts);
  __pyx_v_ts = __pyx_t_6;
  __pyx_t_6 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1633
 * 
 *     #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_7 = __pyx_v_lent;
  for (__pyx_t_8 = 1; __pyx_t_8 < __pyx_t_7; __pyx_t_8+=1) {
    __pyx_v_it = __pyx_t_8;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1634
 *     #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_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_2 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = __Pyx_GetItemInt(__pyx_v_tracks, __pyx_v_it, sizeof(int), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __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 = PyDict_New(); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __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 = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_4, ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_6)); __pyx_t_6 = 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 = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_9 = ((PyArrayObject *)__pyx_t_3);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
      __pyx_t_10 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (PyObject*)__pyx_t_9, &__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_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
      __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
      if (unlikely(__pyx_t_10 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1634; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_9 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1635
 *     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_3 = PyDict_Keys(((PyObject *)__pyx_v_C)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_1 = PyObject_Length(__pyx_t_3); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1635; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_v_lenC = __pyx_t_1;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1638
 *         #if it%1000==0:
 *         #    print it,lenC
 *         alld=np.zeros(lenC)             # <<<<<<<<<<<<<<
 *         flip=np.zeros(lenC)
 *         for k in range(lenC):
 */
    __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_6 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__zeros); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = PyInt_FromSsize_t(__pyx_v_lenC); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_4 = PyTuple_New(1); 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);
    PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    __pyx_t_3 = 0;
    __pyx_t_3 = PyObject_Call(__pyx_t_6, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1638; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_v_alld);
    __pyx_v_alld = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1639
 *         #    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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_4 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__zeros); 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_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_3 = PyInt_FromSsize_t(__pyx_v_lenC); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_6 = PyTuple_New(1); 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);
    PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    __pyx_t_3 = 0;
    __pyx_t_3 = PyObject_Call(__pyx_t_4, __pyx_t_6, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1639; __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_6); __pyx_t_6 = 0;
    __Pyx_DECREF(__pyx_v_flip);
    __pyx_v_flip = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1640
 *         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_1 = __pyx_v_lenC;
    for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_1; __pyx_t_10+=1) {
      __pyx_v_k = __pyx_t_10;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1641
 *         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_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_6 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_4 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __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(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __pyx_t_2 = PyObject_GetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __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_PyNumber_Divide(__pyx_t_4, __pyx_t_2); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __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_2); __pyx_t_2 = 0;
      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __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 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __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 = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_6, __pyx_t_2, ((PyObject *)__pyx_t_3)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 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 = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_14 = ((PyArrayObject *)__pyx_t_4);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
        __pyx_t_15 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_h, (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_13, &__pyx_t_12, &__pyx_t_11);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_h, (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_bstride_0_h = __pyx_bstruct_h.strides[0]; __pyx_bstride_1_h = __pyx_bstruct_h.strides[1];
        __pyx_bshape_0_h = __pyx_bstruct_h.shape[0]; __pyx_bshape_1_h = __pyx_bstruct_h.shape[1];
        if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1641; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_14 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_h));
      __pyx_v_h = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1645
 *             #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); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1645; __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 = 1645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1645; __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 = 1645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1645; __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 = 1645; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1646
 *             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_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      if (!(likely(((__pyx_t_6) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_6, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1646; __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 = 1646; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_16 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1646; __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 = 1646; __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_3)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_2)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_6)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_5)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_16)), ((float *)__pyx_v_d));
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1651
 *             #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_17 = ((__pyx_v_d[1]) < (__pyx_v_d[0]));
      if (__pyx_t_17) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1652
 *             #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]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1653
 *             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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1653; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L10;
      }
      __pyx_L10:;

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

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1655
 *                 flip[k]=1
 *             alld[k]=d[0]
 *         m_k=np.min(alld)             # <<<<<<<<<<<<<<
 *         i_k=np.argmin(alld)
 *         if m_k<d_thr:
 */
    __pyx_t_16 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __pyx_t_5 = PyObject_GetAttr(__pyx_t_16, __pyx_n_s__min); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    __pyx_t_16 = PyTuple_New(1); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_16, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_6 = PyObject_Call(__pyx_t_5, __pyx_t_16, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1655; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    __Pyx_DECREF(__pyx_v_m_k);
    __pyx_v_m_k = __pyx_t_6;
    __pyx_t_6 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1656
 *             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_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_16 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__argmin); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __Pyx_DECREF(__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 = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_5 = PyObject_Call(__pyx_t_16, __pyx_t_6, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1656; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __Pyx_DECREF(__pyx_v_i_k);
    __pyx_v_i_k = __pyx_t_5;
    __pyx_t_5 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1657
 *         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_5 = PyObject_RichCompare(__pyx_v_m_k, __pyx_v_d_thr, Py_LT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1657; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_17 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_17 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1657; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (__pyx_t_17) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1658
 *         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_5 = PyObject_GetItem(__pyx_v_flip, __pyx_v_i_k); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_6 = PyObject_RichCompare(__pyx_t_5, __pyx_int_1, Py_EQ); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_17 = __Pyx_PyObject_IsTrue(__pyx_t_6); if (unlikely(__pyx_t_17 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1658; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      if (__pyx_t_17) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1659
 *         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_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), -1, sizeof(long), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_6);
        if (__Pyx_SetItemInt(__pyx_v_ts, 0, __pyx_t_6, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
        __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_6);
        if (__Pyx_SetItemInt(__pyx_v_ts, 1, __pyx_t_6, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
        __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_6);
        if (__Pyx_SetItemInt(__pyx_v_ts, -1, __pyx_t_6, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1659; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1660
 *             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_6 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_6);
        __pyx_t_5 = PyObject_GetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __pyx_t_16 = PyNumber_InPlaceAdd(__pyx_t_5, __pyx_v_ts); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        if (PyObject_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__hidden), __pyx_t_16) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1660; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
        goto __pyx_L12;
      }
      /*else*/ {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1664
 *                 #print(track.shape)
 *                 #print(track.dtype)
 *                 C[i_k]['hidden']+=track             # <<<<<<<<<<<<<<
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)
 */
        __pyx_t_6 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_6);
        __pyx_t_16 = PyObject_GetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__hidden)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_16);
        __pyx_t_5 = PyNumber_InPlaceAdd(__pyx_t_16, ((PyObject *)__pyx_v_track)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        if (PyObject_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__hidden), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1664; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      }
      __pyx_L12:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1665
 *                 #print(track.dtype)
 *                 C[i_k]['hidden']+=track
 *             C[i_k]['N']+=1             # <<<<<<<<<<<<<<
 *             C[i_k]['indices'].append(it)
 *         else:
 */
      __pyx_t_6 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_5 = PyObject_GetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_16 = PyNumber_InPlaceAdd(__pyx_t_5, __pyx_int_1); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (PyObject_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__N), __pyx_t_16) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1665; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1666
 *                 C[i_k]['hidden']+=track
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)             # <<<<<<<<<<<<<<
 *         else:
 *             C[lenC]={}
 */
      __pyx_t_6 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_16 = PyObject_GetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __pyx_t_6 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_5 = __Pyx_PyObject_Append(__pyx_t_16, __pyx_t_6); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1666; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      goto __pyx_L11;
    }
    /*else*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1668
 *             C[i_k]['indices'].append(it)
 *         else:
 *             C[lenC]={}             # <<<<<<<<<<<<<<
 *             C[lenC]['hidden']=track.copy()
 *             C[lenC]['N']=1
 */
      __pyx_t_5 = PyDict_New(); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_5));
      if (__Pyx_SetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, ((PyObject *)__pyx_t_5), sizeof(Py_ssize_t), PyInt_FromSsize_t) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1668; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1669
 *         else:
 *             C[lenC]={}
 *             C[lenC]['hidden']=track.copy()             # <<<<<<<<<<<<<<
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]
 */
      __pyx_t_5 = PyObject_GetAttr(((PyObject *)__pyx_v_track), __pyx_n_s__copy); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_6 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, sizeof(Py_ssize_t), PyInt_FromSsize_t); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__hidden), __pyx_t_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1669; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1670
 *             C[lenC]={}
 *             C[lenC]['hidden']=track.copy()
 *             C[lenC]['N']=1             # <<<<<<<<<<<<<<
 *             C[lenC]['indices']=[it]
 *     return C
 */
      __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, sizeof(Py_ssize_t), PyInt_FromSsize_t); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      if (PyObject_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1670; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1671
 *             C[lenC]['hidden']=track.copy()
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]             # <<<<<<<<<<<<<<
 *     return C
 * 
 */
      __pyx_t_6 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_5 = PyList_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_5));
      PyList_SET_ITEM(__pyx_t_5, 0, __pyx_t_6);
      __Pyx_GIVEREF(__pyx_t_6);
      __pyx_t_6 = 0;
      __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, sizeof(Py_ssize_t), PyInt_FromSsize_t); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      if (PyObject_SetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_5)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1671; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
    }
    __pyx_L11:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1672
 *             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_6);
  __Pyx_XDECREF(__pyx_t_16);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.local_skeleton_clustering_3pts");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_track);
  __Pyx_DECREF((PyObject *)__pyx_v_h);
  __Pyx_DECREF(__pyx_v_C);
  __Pyx_DECREF(__pyx_v_ts);
  __Pyx_DECREF(__pyx_v_alld);
  __Pyx_DECREF(__pyx_v_flip);
  __Pyx_DECREF(__pyx_v_m_k);
  __Pyx_DECREF(__pyx_v_i_k);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1675
 * 
 * 
 * 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_RefNannySetupContext("track_direct_flip_3sq_dist");

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1692
 *     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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1694
 *         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++) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1695
 *     #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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1696
 *     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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1697
 *         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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1698
 *         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]))));

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1699
 *         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]))));
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1701
 *         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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1702
 * 
 *     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();
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1705
 * 
 * 
 * def larch_3split(tracks,indices=None,thr=10.):             # <<<<<<<<<<<<<<
 * 
 *     ''' Generate a first pass clustering using 3 points (first, mid and last) on the tracks only.
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_larch_3split(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_larch_3split[] = " Generate a first pass clustering using 3 points (first, mid and last) on the tracks only.\n\n    Parameters\n    ------------\n\n    tracks : sequence\n        of tracks as arrays, shape (N1,3) .. (Nm,3)\n\n    indices : sequence \n        of integer indices of tracks  \n    \n    trh : float\n        squared euclidean distance threshold\n    \n    Returns\n    ---------\n\n    C : dict, a tree graph containing the clusters\n\n    Examples\n    ----------\n    >>> import numpy as np\n    >>> from dipy.viz import fvtk        \n    >>> from dipy.tracking.distances as pf\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=pf.larch_fast_split(tracks,None,0.5)\n\n    Here is an example of how to visualize the clustering above\n\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\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    ";
static PyObject *__pyx_pf_4dipy_8tracking_9distances_larch_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;
  PyArrayObject *__pyx_v_track;
  PyArrayObject *__pyx_v_h;
  int __pyx_v_lent;
  int __pyx_v_k;
  int __pyx_v_it;
  float __pyx_v_d[2];
  PyObject *__pyx_v_C;
  PyObject *__pyx_v_itrange;
  PyObject *__pyx_v_ts;
  Py_ssize_t __pyx_v_lenC;
  PyObject *__pyx_v_alld;
  PyObject *__pyx_v_flip;
  PyObject *__pyx_v_m_k;
  PyObject *__pyx_v_i_k;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  Py_buffer __pyx_bstruct_h;
  Py_ssize_t __pyx_bstride_0_h = 0;
  Py_ssize_t __pyx_bstride_1_h = 0;
  Py_ssize_t __pyx_bshape_0_h = 0;
  Py_ssize_t __pyx_bshape_1_h = 0;
  PyObject *__pyx_r = NULL;
  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 = NULL;
  int __pyx_t_8;
  PyArrayObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  Py_ssize_t __pyx_t_13;
  PyArrayObject *__pyx_t_14 = NULL;
  int __pyx_t_15;
  PyObject *__pyx_t_16 = NULL;
  PyObject *__pyx_t_17 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__tracks,&__pyx_n_s__indices,&__pyx_n_s__thr,0};
  __Pyx_RefNannySetupContext("larch_3split");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[3] = {0,0,0};
    values[1] = ((PyObject *)Py_None);
    values[2] = __pyx_k_5;
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__tracks);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "larch_3split") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_tracks = values[0];
    __pyx_v_indices = values[1];
    __pyx_v_thr = values[2];
  } else {
    __pyx_v_indices = ((PyObject *)Py_None);
    __pyx_v_thr = __pyx_k_5;
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  3: __pyx_v_thr = PyTuple_GET_ITEM(__pyx_args, 2);
      case  2: __pyx_v_indices = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_tracks = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 1705; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3split");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_track = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_h = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_C = ((PyObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_itrange = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_ts = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_alld = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_flip = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_m_k = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_i_k = Py_None; __Pyx_INCREF(Py_None);
  __pyx_bstruct_track.buf = NULL;
  __pyx_bstruct_h.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1770
 *         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 = 1770; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lent = __pyx_t_1;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1771
 * 
 *     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); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1771; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  if (__pyx_t_3) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1772
 *     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 = 1772; __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 = 1772; __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 = 1772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__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 = 1772; __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); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __pyx_t_6 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__copy); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1772; __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 = 1772; __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 = 1772; __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 = 1772; __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 = 1772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_C));
    __pyx_v_C = __pyx_t_2;
    __pyx_t_2 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1773
 *     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 = 1773; __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 = 1773; __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, __pyx_t_4, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1773; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_v_itrange);
    __pyx_v_itrange = __pyx_t_2;
    __pyx_t_2 = 0;
    goto __pyx_L6;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1775
 *         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 = 1775; __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 = 1775; __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); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1775; __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 = 1775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(((PyObject *)__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 = 1775; __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); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1775; __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 = 1775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__copy); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1775; __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 = 1775; __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 = 1775; __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 = 1775; __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 = 1775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_C));
    __pyx_v_C = __pyx_t_2;
    __pyx_t_2 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1776
 *     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 = PySequence_GetSlice(__pyx_v_indices, 1, PY_SSIZE_T_MAX); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1776; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_v_itrange);
    __pyx_v_itrange = __pyx_t_2;
    __pyx_t_2 = 0;
  }
  __pyx_L6:;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1778
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __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 = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_2));
  __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_6);
  __pyx_t_7 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__float32); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_7);
  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__dtype), __pyx_t_7) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;
  __pyx_t_7 = PyEval_CallObjectWithKeywords(__pyx_t_4, __pyx_t_5, ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_7);
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_v_ts);
  __pyx_v_ts = __pyx_t_7;
  __pyx_t_7 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1779
 * 
 *     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_1 = 0; __pyx_t_7 = __pyx_v_itrange; __Pyx_INCREF(__pyx_t_7);
  } else {
    __pyx_t_1 = -1; __pyx_t_7 = PyObject_GetIter(__pyx_v_itrange); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_7);
  }
  for (;;) {
    if (likely(PyList_CheckExact(__pyx_t_7))) {
      if (__pyx_t_1 >= PyList_GET_SIZE(__pyx_t_7)) break;
      __pyx_t_2 = PyList_GET_ITEM(__pyx_t_7, __pyx_t_1); __Pyx_INCREF(__pyx_t_2); __pyx_t_1++;
    } else if (likely(PyTuple_CheckExact(__pyx_t_7))) {
      if (__pyx_t_1 >= PyTuple_GET_SIZE(__pyx_t_7)) break;
      __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_7, __pyx_t_1); __Pyx_INCREF(__pyx_t_2); __pyx_t_1++;
    } else {
      __pyx_t_2 = PyIter_Next(__pyx_t_7);
      if (!__pyx_t_2) {
        if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1779; __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 = 1779; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __pyx_v_it = __pyx_t_8;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1780
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_5 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1780; __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(__pyx_v_tracks, __pyx_v_it, sizeof(int), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1780; __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 = 1780; __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 = 1780; __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 = 1780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_6 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_4, ((PyObject *)__pyx_t_2)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 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 = 1780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_9 = ((PyArrayObject *)__pyx_t_6);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
      __pyx_t_8 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (PyObject*)__pyx_t_9, &__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_10, &__pyx_t_11, &__pyx_t_12);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (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_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
      __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
      if (unlikely(__pyx_t_8 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1780; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_9 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_track));
    __pyx_v_track = ((PyArrayObject *)__pyx_t_6);
    __pyx_t_6 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1781
 *     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_6 = PyDict_Keys(((PyObject *)__pyx_v_C)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_13 = PyObject_Length(__pyx_t_6); if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_v_lenC = __pyx_t_13;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1782
 *         track=np.ascontiguousarray(tracks[it],dtype=f32_dt)
 *         lenC=len(C.keys())
 *         alld=np.zeros(lenC)             # <<<<<<<<<<<<<<
 *         flip=np.zeros(lenC)
 * 
 */
    __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_2 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = PyInt_FromSsize_t(__pyx_v_lenC); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __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, __pyx_t_4, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_v_alld);
    __pyx_v_alld = __pyx_t_6;
    __pyx_t_6 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1783
 *         lenC=len(C.keys())
 *         alld=np.zeros(lenC)
 *         flip=np.zeros(lenC)             # <<<<<<<<<<<<<<
 * 
 *         for k in range(lenC):
 */
    __pyx_t_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1783; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_4 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__zeros); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1783; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
    __pyx_t_6 = PyInt_FromSsize_t(__pyx_v_lenC); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1783; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1783; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_t_6);
    __Pyx_GIVEREF(__pyx_t_6);
    __pyx_t_6 = 0;
    __pyx_t_6 = PyObject_Call(__pyx_t_4, __pyx_t_2, NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1783; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__pyx_v_flip);
    __pyx_v_flip = __pyx_t_6;
    __pyx_t_6 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1785
 *         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_13 = __pyx_v_lenC;
    for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_13; __pyx_t_8+=1) {
      __pyx_v_k = __pyx_t_8;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1786
 * 
 *         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_6 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_2 = PyObject_GetAttr(__pyx_t_6, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_4 = PyObject_GetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_k, sizeof(int), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __pyx_t_5 = PyObject_GetItem(__pyx_t_6, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __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_PyNumber_Divide(__pyx_t_4, __pyx_t_5); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __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 = 1786; __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 = 1786; __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 = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_5, ((PyObject *)__pyx_t_6)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__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 = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_14 = ((PyArrayObject *)__pyx_t_4);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
        __pyx_t_15 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_h, (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_bstruct_h, (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_bstride_0_h = __pyx_bstruct_h.strides[0]; __pyx_bstride_1_h = __pyx_bstruct_h.strides[1];
        __pyx_bshape_0_h = __pyx_bstruct_h.shape[0]; __pyx_bshape_1_h = __pyx_bstruct_h.shape[1];
        if (unlikely(__pyx_t_15 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_14 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_h));
      __pyx_v_h = ((PyArrayObject *)__pyx_t_4);
      __pyx_t_4 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1787
 *         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_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1787; __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 = 1787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_6 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_6);
      if (!(likely(((__pyx_t_6) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_6, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1787; __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 = 1787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1788
 *             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_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1788; __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 = 1788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_16 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1788; __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 = 1788; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_17 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1788; __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 = 1788; __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_6)), __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_16)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_17)), __pyx_v_d);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1789
 *             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]));
      if (__pyx_t_3) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1790
 *                                     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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1790; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L11;
      }
      __pyx_L11:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1791
 *             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_17 = PyFloat_FromDouble((__pyx_v_d[0])); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1791; __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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1791; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1792
 *                 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_17 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __pyx_t_16 = PyObject_GetAttr(__pyx_t_17, __pyx_n_s__min); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1792; __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 = 1792; __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_2 = PyObject_Call(__pyx_t_16, __pyx_t_17, NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1792; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __Pyx_DECREF(__pyx_v_m_k);
    __pyx_v_m_k = __pyx_t_2;
    __pyx_t_2 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1793
 *             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_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __pyx_t_17 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__argmin); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __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 = 1793; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_2);
    __Pyx_INCREF(__pyx_v_alld);
    PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_alld);
    __Pyx_GIVEREF(__pyx_v_alld);
    __pyx_t_16 = PyObject_Call(__pyx_t_17, __pyx_t_2, NULL); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1793; __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_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__pyx_v_i_k);
    __pyx_v_i_k = __pyx_t_16;
    __pyx_t_16 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1794
 *         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_16 = PyObject_RichCompare(__pyx_v_m_k, __pyx_v_thr, Py_LT); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_16);
    __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_16); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
    if (__pyx_t_3) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1795
 *         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_16 = PyObject_GetItem(__pyx_v_flip, __pyx_v_i_k); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_2 = PyObject_RichCompare(__pyx_t_16, __pyx_int_1, Py_EQ); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      if (__pyx_t_3) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1796
 *         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_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), -1, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_2);
        if (__Pyx_SetItemInt(__pyx_v_ts, 0, __pyx_t_2, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
        __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_2);
        if (__Pyx_SetItemInt(__pyx_v_ts, 1, __pyx_t_2, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
        __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_track), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_2);
        if (__Pyx_SetItemInt(__pyx_v_ts, -1, __pyx_t_2, sizeof(long), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1797
 *             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_2 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_2);
        __pyx_t_16 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __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 = 1797; __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_2, ((PyObject *)__pyx_n_s__rep3), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1797; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
        goto __pyx_L13;
      }
      /*else*/ {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1799
 *                 C[i_k]['rep3']+=ts
 *             else:
 *                 C[i_k]['rep3']+=track             # <<<<<<<<<<<<<<
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)
 */
        __pyx_t_2 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_2);
        __pyx_t_17 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1799; __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 = 1799; __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_2, ((PyObject *)__pyx_n_s__rep3), __pyx_t_16) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      }
      __pyx_L13:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1800
 *             else:
 *                 C[i_k]['rep3']+=track
 *             C[i_k]['N']+=1             # <<<<<<<<<<<<<<
 *             C[i_k]['indices'].append(it)
 * 
 */
      __pyx_t_2 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1800; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_16 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1800; __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 = 1800; __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_2, ((PyObject *)__pyx_n_s__N), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1800; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1801
 *                 C[i_k]['rep3']+=track
 *             C[i_k]['N']+=1
 *             C[i_k]['indices'].append(it)             # <<<<<<<<<<<<<<
 * 
 *         else:
 */
      __pyx_t_2 = __Pyx_PyDict_GetItem(((PyObject *)__pyx_v_C), __pyx_v_i_k); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_17 = PyObject_GetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __pyx_t_2 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_16 = __Pyx_PyObject_Append(__pyx_t_17, __pyx_t_2); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1801; __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_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      goto __pyx_L12;
    }
    /*else*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1804
 * 
 *         else:
 *             C[lenC]={}             # <<<<<<<<<<<<<<
 *             C[lenC]['rep3']=track.copy()
 *             C[lenC]['N']=1
 */
      __pyx_t_16 = PyDict_New(); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_16));
      if (__Pyx_SetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, ((PyObject *)__pyx_t_16), sizeof(Py_ssize_t), PyInt_FromSsize_t) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1804; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(((PyObject *)__pyx_t_16)); __pyx_t_16 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1805
 *         else:
 *             C[lenC]={}
 *             C[lenC]['rep3']=track.copy()             # <<<<<<<<<<<<<<
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]
 */
      __pyx_t_16 = PyObject_GetAttr(((PyObject *)__pyx_v_track), __pyx_n_s__copy); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      __pyx_t_2 = PyObject_Call(__pyx_t_16, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __pyx_t_16 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, sizeof(Py_ssize_t), PyInt_FromSsize_t); if (!__pyx_t_16) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_16);
      if (PyObject_SetItem(__pyx_t_16, ((PyObject *)__pyx_n_s__rep3), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1805; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_16); __pyx_t_16 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1806
 *             C[lenC]={}
 *             C[lenC]['rep3']=track.copy()
 *             C[lenC]['N']=1             # <<<<<<<<<<<<<<
 *             C[lenC]['indices']=[it]
 * 
 */
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, sizeof(Py_ssize_t), PyInt_FromSsize_t); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      if (PyObject_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__N), __pyx_int_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1806; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1807
 *             C[lenC]['rep3']=track.copy()
 *             C[lenC]['N']=1
 *             C[lenC]['indices']=[it]             # <<<<<<<<<<<<<<
 * 
 * 
 */
      __pyx_t_2 = PyInt_FromLong(__pyx_v_it); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __pyx_t_16 = PyList_New(1); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__pyx_t_16));
      PyList_SET_ITEM(__pyx_t_16, 0, __pyx_t_2);
      __Pyx_GIVEREF(__pyx_t_2);
      __pyx_t_2 = 0;
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_C), __pyx_v_lenC, sizeof(Py_ssize_t), PyInt_FromSsize_t); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      if (PyObject_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__indices), ((PyObject *)__pyx_t_16)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1807; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_t_16)); __pyx_t_16 = 0;
    }
    __pyx_L12:;
  }
  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1810
 * 
 * 
 *     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_7);
  __Pyx_XDECREF(__pyx_t_16);
  __Pyx_XDECREF(__pyx_t_17);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3split");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
  __pyx_L2:;
  __Pyx_DECREF((PyObject *)__pyx_v_track);
  __Pyx_DECREF((PyObject *)__pyx_v_h);
  __Pyx_DECREF(__pyx_v_C);
  __Pyx_DECREF(__pyx_v_itrange);
  __Pyx_DECREF(__pyx_v_ts);
  __Pyx_DECREF(__pyx_v_alld);
  __Pyx_DECREF(__pyx_v_flip);
  __Pyx_DECREF(__pyx_v_m_k);
  __Pyx_DECREF(__pyx_v_i_k);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1813
 * 
 * 
 * def larch_3merge(C,thr=10.):             # <<<<<<<<<<<<<<
 *     ''' Reassign tracks to existing clusters by merging clusters that their representative tracks are not very distant i.e. less than sqd_thr. Using tracks consisting of 3 points (first, mid and last). This is necessary after running larch_fast_split after multiple split in different levels (squared thresholds) as some of them have created independent clusters.
 *     Parameters
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_larch_3merge(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_larch_3merge[] = " Reassign tracks to existing clusters by merging clusters that their representative tracks are not very distant i.e. less than sqd_thr. Using tracks consisting of 3 points (first, mid and last). This is necessary after running larch_fast_split after multiple split in different levels (squared thresholds) as some of them have created independent clusters.\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 PyObject *__pyx_pf_4dipy_8tracking_9distances_larch_3merge(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_C = 0;
  PyObject *__pyx_v_thr = 0;
  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;
  PyObject *__pyx_v_C2;
  PyObject *__pyx_v_krange;
  Py_ssize_t __pyx_v_klen;
  PyObject *__pyx_v_alld;
  PyObject *__pyx_v_flip;
  PyObject *__pyx_v_m_k;
  PyObject *__pyx_v_i_k;
  Py_buffer __pyx_bstruct_ch;
  Py_ssize_t __pyx_bstride_0_ch = 0;
  Py_ssize_t __pyx_bstride_1_ch = 0;
  Py_ssize_t __pyx_bshape_0_ch = 0;
  Py_ssize_t __pyx_bshape_1_ch = 0;
  Py_buffer __pyx_bstruct_h;
  Py_ssize_t __pyx_bstride_0_h = 0;
  Py_ssize_t __pyx_bstride_1_h = 0;
  Py_ssize_t __pyx_bshape_0_h = 0;
  Py_ssize_t __pyx_bshape_1_h = 0;
  PyObject *__pyx_r = NULL;
  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;
  PyArrayObject *__pyx_t_6 = NULL;
  PyArrayObject *__pyx_t_7 = NULL;
  Py_ssize_t __pyx_t_8;
  long __pyx_t_9;
  int __pyx_t_10;
  int __pyx_t_11;
  PyObject *__pyx_t_12 = NULL;
  PyObject *__pyx_t_13 = NULL;
  PyObject *__pyx_t_14 = NULL;
  int __pyx_t_15;
  int __pyx_t_16;
  PyObject *__pyx_t_17 = NULL;
  int __pyx_t_18;
  long __pyx_t_19;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__C,&__pyx_n_s__thr,0};
  __Pyx_RefNannySetupContext("larch_3merge");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    values[1] = __pyx_k_6;
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__C);
      if (likely(values[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, PyTuple_GET_SIZE(__pyx_args), "larch_3merge") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1813; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_C = values[0];
    __pyx_v_thr = values[1];
  } else {
    __pyx_v_thr = __pyx_k_6;
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: __pyx_v_thr = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: __pyx_v_C = PyTuple_GET_ITEM(__pyx_args, 0);
      break;
      default: goto __pyx_L5_argtuple_error;
    }
  }
  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 = 1813; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3merge");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_v_ts = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_C2 = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_krange = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_alld = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_flip = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_m_k = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_i_k = Py_None; __Pyx_INCREF(Py_None);
  __pyx_bstruct_h.buf = NULL;
  __pyx_bstruct_ch.buf = NULL;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1828
 *     '''
 * 
 *     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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __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 = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_1, 1, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  PyTuple_SET_ITEM(__pyx_t_3, 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 = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_1));
  __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_5 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__float32); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __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_1, ((PyObject *)__pyx_n_s__dtype), __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_3, ((PyObject *)__pyx_t_1)); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 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 = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_6 = ((PyArrayObject *)__pyx_t_5);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_h, (PyObject*)__pyx_t_6, &__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_bstruct_h.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_h = __pyx_bstruct_h.strides[0]; __pyx_bstride_1_h = __pyx_bstruct_h.strides[1];
      __pyx_bshape_0_h = __pyx_bstruct_h.shape[0]; __pyx_bshape_1_h = __pyx_bstruct_h.shape[1];
    }
  }
  __pyx_t_6 = 0;
  __pyx_v_h = ((PyArrayObject *)__pyx_t_5);
  __pyx_t_5 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1829
 * 
 *     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_5 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__zeros); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_5);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __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 = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_5));
  __pyx_t_2 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s__float32); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  if (PyDict_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__dtype), __pyx_t_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
  __pyx_t_4 = PyEval_CallObjectWithKeywords(__pyx_t_1, __pyx_t_3, ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __Pyx_DECREF(__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 = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_t_7 = ((PyArrayObject *)__pyx_t_4);
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_ch, (PyObject*)__pyx_t_7, &__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_bstruct_ch.buf = NULL;
      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    } else {__pyx_bstride_0_ch = __pyx_bstruct_ch.strides[0]; __pyx_bstride_1_ch = __pyx_bstruct_ch.strides[1];
      __pyx_bshape_0_ch = __pyx_bstruct_ch.shape[0]; __pyx_bshape_1_ch = __pyx_bstruct_ch.shape[1];
    }
  }
  __pyx_t_7 = 0;
  __pyx_v_ch = ((PyArrayObject *)__pyx_t_4);
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1833
 *     cdef float d[2]
 * 
 *     ts=np.zeros((3,3),dtype=np.float32)             # <<<<<<<<<<<<<<
 * 
 *     lenC=len(C)
 */
  __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __pyx_t_5 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __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(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __Pyx_INCREF(__pyx_int_3);
  PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_int_3);
  __Pyx_GIVEREF(__pyx_int_3);
  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __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);
  __pyx_t_4 = 0;
  __pyx_t_4 = PyDict_New(); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((PyObject *)__pyx_t_4));
  __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __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_4, ((PyObject *)__pyx_n_s__dtype), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_3, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0;
  __Pyx_DECREF(__pyx_v_ts);
  __pyx_v_ts = __pyx_t_2;
  __pyx_t_2 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1835
 *     ts=np.zeros((3,3),dtype=np.float32)
 * 
 *     lenC=len(C)             # <<<<<<<<<<<<<<
 *     C2=C.copy()
 * 
 */
  __pyx_t_8 = PyObject_Length(__pyx_v_C); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_lenC = __pyx_t_8;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1836
 * 
 *     lenC=len(C)
 *     C2=C.copy()             # <<<<<<<<<<<<<<
 * 
 *     for c in range(0,lenC-1):
 */
  __pyx_t_2 = PyObject_GetAttr(__pyx_v_C, __pyx_n_s__copy); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_4);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_v_C2);
  __pyx_v_C2 = __pyx_t_4;
  __pyx_t_4 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1838
 *     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_9 = (__pyx_v_lenC - 1);
  for (__pyx_t_10 = 0; __pyx_t_10 < __pyx_t_9; __pyx_t_10+=1) {
    __pyx_v_c = __pyx_t_10;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1839
 * 
 *     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_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_2 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __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_v_C, __pyx_v_c, sizeof(int), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_c, sizeof(int), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = __Pyx_PyNumber_Divide(__pyx_t_3, __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __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_5); __pyx_t_5 = 0;
    __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __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 = 1839; __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 = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_2, __pyx_t_5, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 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 = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_t_7 = ((PyArrayObject *)__pyx_t_3);
    {
      __Pyx_BufFmt_StackElem __pyx_stack[1];
      __Pyx_SafeReleaseBuffer(&__pyx_bstruct_ch);
      __pyx_t_11 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_ch, (PyObject*)__pyx_t_7, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
      if (unlikely(__pyx_t_11 < 0)) {
        PyErr_Fetch(&__pyx_t_12, &__pyx_t_13, &__pyx_t_14);
        if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_ch, (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_12); Py_XDECREF(__pyx_t_13); Py_XDECREF(__pyx_t_14);
          __Pyx_RaiseBufferFallbackError();
        } else {
          PyErr_Restore(__pyx_t_12, __pyx_t_13, __pyx_t_14);
        }
      }
      __pyx_bstride_0_ch = __pyx_bstruct_ch.strides[0]; __pyx_bstride_1_ch = __pyx_bstruct_ch.strides[1];
      __pyx_bshape_0_ch = __pyx_bstruct_ch.shape[0]; __pyx_bshape_1_ch = __pyx_bstruct_ch.shape[1];
      if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_t_7 = 0;
    __Pyx_DECREF(((PyObject *)__pyx_v_ch));
    __pyx_v_ch = ((PyArrayObject *)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1840
 *     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_3 = PyInt_FromLong((__pyx_v_c + 1)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __pyx_t_4 = PyInt_FromLong(__pyx_v_lenC); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_3);
    __Pyx_GIVEREF(__pyx_t_3);
    PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_t_4);
    __Pyx_GIVEREF(__pyx_t_4);
    __pyx_t_3 = 0;
    __pyx_t_4 = 0;
    __pyx_t_4 = PyObject_Call(__pyx_builtin_range, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_v_krange);
    __pyx_v_krange = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1841
 *         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_8 = PyObject_Length(__pyx_v_krange); if (unlikely(__pyx_t_8 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_klen = __pyx_t_8;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1842
 *         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_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_5 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1842; __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_FromSsize_t(__pyx_v_klen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1842; __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);
    __pyx_t_4 = 0;
    __pyx_t_4 = PyObject_Call(__pyx_t_5, __pyx_t_3, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1842; __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_3); __pyx_t_3 = 0;
    __Pyx_DECREF(__pyx_v_alld);
    __pyx_v_alld = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1843
 *         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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __pyx_t_3 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__zeros); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_3);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __pyx_t_4 = PyInt_FromSsize_t(__pyx_v_klen); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __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 = 1843; __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 = PyObject_Call(__pyx_t_3, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1843; __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_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_v_flip);
    __pyx_v_flip = __pyx_t_4;
    __pyx_t_4 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1844
 *         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_11 = __pyx_v_lenC;
    for (__pyx_t_15 = (__pyx_v_c + 1); __pyx_t_15 < __pyx_t_11; __pyx_t_15+=1) {
      __pyx_v_k = __pyx_t_15;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1845
 *         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_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_5 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__ascontiguousarray); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_k, sizeof(int), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_3 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __pyx_t_4 = __Pyx_GetItemInt(__pyx_v_C, __pyx_v_k, sizeof(int), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __pyx_t_2 = PyObject_GetItem(__pyx_t_4, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __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_PyNumber_Divide(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __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_2); __pyx_t_2 = 0;
      __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      PyTuple_SET_ITEM(__pyx_t_2, 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 = 1845; __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 = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_3 = PyEval_CallObjectWithKeywords(__pyx_t_5, __pyx_t_2, ((PyObject *)__pyx_t_4)); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 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 = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_6 = ((PyArrayObject *)__pyx_t_3);
      {
        __Pyx_BufFmt_StackElem __pyx_stack[1];
        __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
        __pyx_t_16 = __Pyx_GetBufferAndValidate(&__pyx_bstruct_h, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack);
        if (unlikely(__pyx_t_16 < 0)) {
          PyErr_Fetch(&__pyx_t_14, &__pyx_t_13, &__pyx_t_12);
          if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_h, (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_14); Py_XDECREF(__pyx_t_13); Py_XDECREF(__pyx_t_12);
            __Pyx_RaiseBufferFallbackError();
          } else {
            PyErr_Restore(__pyx_t_14, __pyx_t_13, __pyx_t_12);
          }
        }
        __pyx_bstride_0_h = __pyx_bstruct_h.strides[0]; __pyx_bstride_1_h = __pyx_bstruct_h.strides[1];
        __pyx_bshape_0_h = __pyx_bstruct_h.shape[0]; __pyx_bshape_1_h = __pyx_bstruct_h.shape[1];
        if (unlikely(__pyx_t_16 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1845; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_t_6 = 0;
      __Pyx_DECREF(((PyObject *)__pyx_v_h));
      __pyx_v_h = ((PyArrayObject *)__pyx_t_3);
      __pyx_t_3 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1847
 *             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); if (!__pyx_t_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __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 = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_4 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __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 = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_2 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1847; __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 = 1847; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1848
 *             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_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __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 = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_1 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __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 = 1848; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_17 = __Pyx_GetItemInt(((PyObject *)__pyx_v_h), 2, sizeof(long), PyInt_FromLong); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1848; __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 = 1848; __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_4)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_2)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_5)), __pyx_f_4dipy_8tracking_9distances_asfp(((PyArrayObject *)__pyx_t_1)), __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_4); __pyx_t_4 = 0;
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1850
 *                 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]));
      if (__pyx_t_18) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1851
 * 
 *             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]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1852
 *             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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1852; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L10;
      }
      __pyx_L10:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1853
 *                 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 = 1853; __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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1853; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1855
 *             alld[k-c-1]=d[0]
 * 
 *         m_k=np.min(alld)             # <<<<<<<<<<<<<<
 *         i_k=np.argmin(alld)
 *         if m_k<thr:
 */
    __pyx_t_17 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __pyx_t_1 = PyObject_GetAttr(__pyx_t_17, __pyx_n_s__min); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __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 = 1855; __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_5 = PyObject_Call(__pyx_t_1, __pyx_t_17, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1855; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __Pyx_DECREF(__pyx_v_m_k);
    __pyx_v_m_k = __pyx_t_5;
    __pyx_t_5 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1856
 * 
 *         m_k=np.min(alld)
 *         i_k=np.argmin(alld)             # <<<<<<<<<<<<<<
 *         if m_k<thr:
 *             if flip[i_k]==1:
 */
    __pyx_t_5 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); 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);
    __pyx_t_17 = PyObject_GetAttr(__pyx_t_5, __pyx_n_s__argmin); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_17);
    __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 = 1856; __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_1 = PyObject_Call(__pyx_t_17, __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1856; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_DECREF(__pyx_v_i_k);
    __pyx_v_i_k = __pyx_t_1;
    __pyx_t_1 = 0;

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1857
 *         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_1 = PyObject_RichCompare(__pyx_v_m_k, __pyx_v_thr, Py_LT); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __pyx_t_18 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1857; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
    if (__pyx_t_18) {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1858
 *         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_1 = PyObject_GetItem(__pyx_v_flip, __pyx_v_i_k); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __pyx_t_5 = PyObject_RichCompare(__pyx_t_1, __pyx_int_1, Py_EQ); 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_1); __pyx_t_1 = 0;
      __pyx_t_18 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_18 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1858; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_18) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1859
 *         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_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), -1, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 1, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __pyx_t_5 = __Pyx_GetItemInt(((PyObject *)__pyx_v_ch), 0, sizeof(long), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __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) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1859; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1860
 *             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_5 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1860; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __pyx_t_1 = PyNumber_Add(__pyx_v_i_k, __pyx_t_5); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1860; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_1);
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __pyx_t_5 = PyObject_GetItem(__pyx_v_C2, __pyx_t_1); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1860; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
        __pyx_t_1 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1860; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_1);
        __pyx_t_17 = PyNumber_InPlaceAdd(__pyx_t_1, __pyx_v_ts); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1860; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
        if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__rep3), __pyx_t_17) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1860; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        goto __pyx_L12;
      }
      /*else*/ {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1862
 *                 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_5 = PyInt_FromLong(__pyx_v_c); 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_t_17 = PyNumber_Add(__pyx_v_i_k, __pyx_t_5); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __pyx_t_5 = PyObject_GetItem(__pyx_v_C2, __pyx_t_17); if (!__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_17); __pyx_t_17 = 0;
        __pyx_t_17 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__rep3)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_17);
        __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_t_17, ((PyObject *)__pyx_v_ch)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_1);
        __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
        if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__rep3), __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1862; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      }
      __pyx_L12:;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1863
 *             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_5 = __Pyx_GetItemInt(__pyx_v_C2, __pyx_v_c, sizeof(int), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_1 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_17 = PyNumber_Add(__pyx_v_i_k, __pyx_t_5); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = PyObject_GetItem(__pyx_v_C2, __pyx_t_17); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __pyx_t_17 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__N)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __pyx_t_2 = PyNumber_InPlaceAdd(__pyx_t_17, __pyx_t_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__N), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1863; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1864
 *                 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_5 = __Pyx_GetItemInt(__pyx_v_C2, __pyx_v_c, sizeof(int), PyInt_FromLong); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_2 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = PyInt_FromLong(__pyx_v_c); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_17 = PyNumber_Add(__pyx_v_i_k, __pyx_t_5); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_5 = PyObject_GetItem(__pyx_v_C2, __pyx_t_17); if (!__pyx_t_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      __pyx_t_17 = PyObject_GetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__indices)); if (!__pyx_t_17) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_17);
      __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_t_17, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_1);
      __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_DECREF(__pyx_t_17); __pyx_t_17 = 0;
      if (PyObject_SetItem(__pyx_t_5, ((PyObject *)__pyx_n_s__indices), __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1864; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1865
 *             C2[i_k+c]['N']+=C2[c]['N']
 *             C2[i_k+c]['indices']+=C2[c]['indices']
 *             del C2[c]             # <<<<<<<<<<<<<<
 * 
 *     return C2
 */
      if (__Pyx_DelItemInt(__pyx_v_C2, __pyx_v_c, sizeof(int), PyInt_FromLong) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1865; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L11;
    }
    __pyx_L11:;
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1867
 *             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_5);
  __Pyx_XDECREF(__pyx_t_17);
  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_ch);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.larch_3merge");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_ch);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_h);
  __pyx_L2:;
  __Pyx_XDECREF((PyObject *)__pyx_v_h);
  __Pyx_XDECREF((PyObject *)__pyx_v_ch);
  __Pyx_DECREF(__pyx_v_ts);
  __Pyx_DECREF(__pyx_v_C2);
  __Pyx_DECREF(__pyx_v_krange);
  __Pyx_DECREF(__pyx_v_alld);
  __Pyx_DECREF(__pyx_v_flip);
  __Pyx_DECREF(__pyx_v_m_k);
  __Pyx_DECREF(__pyx_v_i_k);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1871
 * 
 * 
 * def point_track_sq_distance_check(cnp.ndarray[float,ndim=2] track, cnp.ndarray[float,ndim=1] point, double sq_dist_thr):             # <<<<<<<<<<<<<<
 *     ''' Check if square distance of track from point is smaller than threshold
 * 
 */

static PyObject *__pyx_pf_4dipy_8tracking_9distances_point_track_sq_distance_check(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_point_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    >>> from dipy.tracking.distances import point_track_sq_distance_check\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 PyObject *__pyx_pf_4dipy_8tracking_9distances_point_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;
  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;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  Py_buffer __pyx_bstruct_point;
  Py_ssize_t __pyx_bstride_0_point = 0;
  Py_ssize_t __pyx_bshape_0_point = 0;
  PyObject *__pyx_r = NULL;
  Py_ssize_t __pyx_t_1;
  long __pyx_t_2;
  int __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__track,&__pyx_n_s__point,&__pyx_n_s__sq_dist_thr,0};
  __Pyx_RefNannySetupContext("point_track_sq_distance_check");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[3] = {0,0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__track);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__point);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("point_track_sq_distance_check", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  2:
      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sq_dist_thr);
      if (likely(values[2])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("point_track_sq_distance_check", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "point_track_sq_distance_check") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __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 = 1871; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_track = ((PyArrayObject *)PyTuple_GET_ITEM(__pyx_args, 0));
    __pyx_v_point = ((PyArrayObject *)PyTuple_GET_ITEM(__pyx_args, 1));
    __pyx_v_sq_dist_thr = __pyx_PyFloat_AsDouble(PyTuple_GET_ITEM(__pyx_args, 2)); if (unlikely((__pyx_v_sq_dist_thr == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __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 = 1871; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.point_track_sq_distance_check");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_track.buf = NULL;
  __pyx_bstruct_point.buf = NULL;
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_track), __pyx_ptype_5numpy_ndarray, 1, "track", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __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 = 1871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
  __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_point, (PyObject*)__pyx_v_point, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1871; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_bstride_0_point = __pyx_bstruct_point.strides[0];
  __pyx_bshape_0_point = __pyx_bstruct_point.shape[0];

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1900
 * 
 *     cdef:
 *         float *t=<float *>track.data             # <<<<<<<<<<<<<<
 *         float *p=<float *>point.data
 *         float a[3],b[3]
 */
  __pyx_v_t = ((float *)__pyx_v_track->data);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1901
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1903
 *         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 = 1903; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_tlen = __pyx_t_1;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1904
 *         float a[3],b[3]
 *         int tlen = len(track)
 *         int curr = 0             # <<<<<<<<<<<<<<
 *         float dist = 0
 *         int i
 */
  __pyx_v_curr = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1905
 *         int tlen = len(track)
 *         int curr = 0
 *         float dist = 0             # <<<<<<<<<<<<<<
 *         int i
 *         int intersects = 0
 */
  __pyx_v_dist = 0.0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1907
 *         float dist = 0
 *         int i
 *         int intersects = 0             # <<<<<<<<<<<<<<
 * 
 *     with nogil:
 */
  __pyx_v_intersects = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1909
 *         int intersects = 0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
  { PyThreadState *_save;
    Py_UNBLOCK_THREADS
    /*try:*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1910
 * 
 *     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++) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1911
 *     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);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1912
 *         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]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1913
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1914
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1915
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1916
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1917
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1918
 *             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);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1919
 *             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);
        if (__pyx_t_3) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1920
 *             dist=cpoint_segment_sq_dist(<float *>a,<float *>b,p)
 *             if dist<=sq_dist_thr:
 *                 intersects=1             # <<<<<<<<<<<<<<
 *                 break
 * 
 */
          __pyx_v_intersects = 1;

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1921
 *             if dist<=sq_dist_thr:
 *                 intersects=1
 *                 break             # <<<<<<<<<<<<<<
 * 
 *     if intersects==1:
 */
          goto __pyx_L10_break;
          goto __pyx_L11;
        }
        __pyx_L11:;
      }
      __pyx_L10_break:;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1909
 *         int intersects = 0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
    /*finally:*/ {
      Py_BLOCK_THREADS
    }
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1923
 *                 break
 * 
 *     if intersects==1:             # <<<<<<<<<<<<<<
 *         return True
 *     else:
 */
  __pyx_t_3 = (__pyx_v_intersects == 1);
  if (__pyx_t_3) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1924
 * 
 *     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 = 1924; __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_L12;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1926
 *         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 = 1926; __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_L12:;

  __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_bstruct_track);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_point);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.point_track_sq_distance_check");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_point);
  __pyx_L2:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1928
 *         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_track_roi_intersection_check(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static char __pyx_doc_4dipy_8tracking_9distances_track_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    >>> from dipy.tracking.distances import track_roi_intersection_check\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 PyObject *__pyx_pf_4dipy_8tracking_9distances_track_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;
  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;
  Py_buffer __pyx_bstruct_roi;
  Py_ssize_t __pyx_bstride_0_roi = 0;
  Py_ssize_t __pyx_bstride_1_roi = 0;
  Py_ssize_t __pyx_bshape_0_roi = 0;
  Py_ssize_t __pyx_bshape_1_roi = 0;
  Py_buffer __pyx_bstruct_track;
  Py_ssize_t __pyx_bstride_0_track = 0;
  Py_ssize_t __pyx_bstride_1_track = 0;
  Py_ssize_t __pyx_bshape_0_track = 0;
  Py_ssize_t __pyx_bshape_1_track = 0;
  PyObject *__pyx_r = NULL;
  Py_ssize_t __pyx_t_1;
  long __pyx_t_2;
  int __pyx_t_3;
  int __pyx_t_4;
  PyObject *__pyx_t_5 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__track,&__pyx_n_s__roi,&__pyx_n_s__sq_dist_thr,0};
  __Pyx_RefNannySetupContext("track_roi_intersection_check");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[3] = {0,0,0};
    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);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__track);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__roi);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("track_roi_intersection_check", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
      case  2:
      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sq_dist_thr);
      if (likely(values[2])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("track_roi_intersection_check", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "track_roi_intersection_check") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __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 = 1928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_track = ((PyArrayObject *)PyTuple_GET_ITEM(__pyx_args, 0));
    __pyx_v_roi = ((PyArrayObject *)PyTuple_GET_ITEM(__pyx_args, 1));
    __pyx_v_sq_dist_thr = __pyx_PyFloat_AsDouble(PyTuple_GET_ITEM(__pyx_args, 2)); if (unlikely((__pyx_v_sq_dist_thr == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __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 = 1928; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("dipy.tracking.distances.track_roi_intersection_check");
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_bstruct_track.buf = NULL;
  __pyx_bstruct_roi.buf = NULL;
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_track), __pyx_ptype_5numpy_ndarray, 1, "track", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __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 = 1928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_track, (PyObject*)__pyx_v_track, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_bstride_0_track = __pyx_bstruct_track.strides[0]; __pyx_bstride_1_track = __pyx_bstruct_track.strides[1];
  __pyx_bshape_0_track = __pyx_bstruct_track.shape[0]; __pyx_bshape_1_track = __pyx_bstruct_track.shape[1];
  {
    __Pyx_BufFmt_StackElem __pyx_stack[1];
    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_bstruct_roi, (PyObject*)__pyx_v_roi, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1928; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  __pyx_bstride_0_roi = __pyx_bstruct_roi.strides[0]; __pyx_bstride_1_roi = __pyx_bstruct_roi.strides[1];
  __pyx_bshape_0_roi = __pyx_bstruct_roi.shape[0]; __pyx_bshape_1_roi = __pyx_bstruct_roi.shape[1];

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1953
 * 
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1954
 *     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);

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1956
 *         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 = 1956; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_tlen = __pyx_t_1;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1957
 *         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 = 1957; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_rlen = __pyx_t_1;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1958
 *         int tlen = len(track)
 *         int rlen = len(roi)
 *         int curr = 0             # <<<<<<<<<<<<<<
 *         int currp = 0
 *         float dist = 0
 */
  __pyx_v_curr = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1959
 *         int rlen = len(roi)
 *         int curr = 0
 *         int currp = 0             # <<<<<<<<<<<<<<
 *         float dist = 0
 *         int i,j
 */
  __pyx_v_currp = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1960
 *         int curr = 0
 *         int currp = 0
 *         float dist = 0             # <<<<<<<<<<<<<<
 *         int i,j
 *         int intersects=0
 */
  __pyx_v_dist = 0.0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1962
 *         float dist = 0
 *         int i,j
 *         int intersects=0             # <<<<<<<<<<<<<<
 * 
 *     with nogil:
 */
  __pyx_v_intersects = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1964
 *         int intersects=0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
  { PyThreadState *_save;
    Py_UNBLOCK_THREADS
    /*try:*/ {

      /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1965
 * 
 *     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++) {

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1966
 *     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);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1967
 *         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]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1968
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1969
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1970
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1971
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1972
 *             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)]);

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1973
 *             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++) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1974
 *             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);

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1975
 *             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]);

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1976
 *                 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)]);

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1977
 *                 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)]);

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1978
 *                 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));

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1979
 *                 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);
          if (__pyx_t_4) {

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1980
 *                 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;

            /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1981
 *                 if dist<=sq_dist_thr:
 *                     intersects=1
 *                     break             # <<<<<<<<<<<<<<
 *             if intersects==1:
 *                 break
 */
            goto __pyx_L12_break;
            goto __pyx_L13;
          }
          __pyx_L13:;
        }
        __pyx_L12_break:;

        /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1982
 *                     intersects=1
 *                     break
 *             if intersects==1:             # <<<<<<<<<<<<<<
 *                 break
 *     if intersects==1:
 */
        __pyx_t_4 = (__pyx_v_intersects == 1);
        if (__pyx_t_4) {

          /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1983
 *                     break
 *             if intersects==1:
 *                 break             # <<<<<<<<<<<<<<
 *     if intersects==1:
 *         return True
 */
          goto __pyx_L10_break;
          goto __pyx_L14;
        }
        __pyx_L14:;
      }
      __pyx_L10_break:;
    }

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1964
 *         int intersects=0
 * 
 *     with nogil:             # <<<<<<<<<<<<<<
 *         for i from 0<=i<tlen-1:
 *             curr=i*3
 */
    /*finally:*/ {
      Py_BLOCK_THREADS
    }
  }

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1984
 *             if intersects==1:
 *                 break
 *     if intersects==1:             # <<<<<<<<<<<<<<
 *         return True
 *     else:
 */
  __pyx_t_4 = (__pyx_v_intersects == 1);
  if (__pyx_t_4) {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1985
 *                 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 = 1985; __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_L15;
  }
  /*else*/ {

    /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1987
 *         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 = 1987; __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_L15:;

  __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_bstruct_roi);
    __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
  __Pyx_AddTraceback("dipy.tracking.distances.track_roi_intersection_check");
  __pyx_r = NULL;
  goto __pyx_L2;
  __pyx_L0:;
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_roi);
  __Pyx_SafeReleaseBuffer(&__pyx_bstruct_track);
  __pyx_L2:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":188
 *         # 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 CYTHON_UNUSED int __pyx_pf_5numpy_7ndarray___getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /*proto*/
static CYTHON_UNUSED int __pyx_pf_5numpy_7ndarray___getbuffer__(PyObject *__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;
  int __pyx_t_1;
  int __pyx_t_2;
  int __pyx_t_3;
  PyObject *__pyx_t_4 = NULL;
  PyObject *__pyx_t_5 = NULL;
  int __pyx_t_6;
  int __pyx_t_7;
  int __pyx_t_8;
  char *__pyx_t_9;
  __Pyx_RefNannySetupContext("__getbuffer__");
  if (__pyx_v_info == NULL) return 0;
  __pyx_v_info->obj = Py_None; __Pyx_INCREF(Py_None);
  __Pyx_GIVEREF(__pyx_v_info->obj);

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":194
 *             # of flags
 *             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;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":195
 *             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);

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":197
 *             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(((PyArrayObject *)__pyx_v_self));

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":199
 *             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)));
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":200
 * 
 *             if sizeof(npy_intp) != sizeof(Py_ssize_t):
 *                 copy_shape = 1             # <<<<<<<<<<<<<<
 *             else:
 *                 copy_shape = 0
 */
    __pyx_v_copy_shape = 1;
    goto __pyx_L5;
  }
  /*else*/ {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":202
 *                 copy_shape = 1
 *             else:
 *                 copy_shape = 0             # <<<<<<<<<<<<<<
 * 
 *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
 */
    __pyx_v_copy_shape = 0;
  }
  __pyx_L5:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":204
 *                 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);
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":205
 * 
 *             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(((PyArrayObject *)__pyx_v_self), NPY_C_CONTIGUOUS));
    __pyx_t_3 = __pyx_t_2;
  } else {
    __pyx_t_3 = __pyx_t_1;
  }
  if (__pyx_t_3) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":206
 *             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 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_INCREF(((PyObject *)__pyx_kp_u_7));
    PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_kp_u_7));
    __Pyx_GIVEREF(((PyObject *)__pyx_kp_u_7));
    __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    __Pyx_Raise(__pyx_t_5, 0, 0);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 206; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    goto __pyx_L6;
  }
  __pyx_L6:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":208
 *                 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);
  if (__pyx_t_3) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":209
 * 
 *             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(((PyArrayObject *)__pyx_v_self), NPY_F_CONTIGUOUS));
    __pyx_t_2 = __pyx_t_1;
  } else {
    __pyx_t_2 = __pyx_t_3;
  }
  if (__pyx_t_2) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":210
 *             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_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_INCREF(((PyObject *)__pyx_kp_u_8));
    PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_kp_u_8));
    __Pyx_GIVEREF(((PyObject *)__pyx_kp_u_8));
    __pyx_t_4 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_4);
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    __Pyx_Raise(__pyx_t_4, 0, 0);
    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 210; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    goto __pyx_L7;
  }
  __pyx_L7:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":212
 *                 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(((PyArrayObject *)__pyx_v_self));

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":213
 * 
 *             info.buf = PyArray_DATA(self)
 *             info.ndim = ndim             # <<<<<<<<<<<<<<
 *             if copy_shape:
 *                 # Allocate new buffer for strides and shape info. This is allocated
 */
  __pyx_v_info->ndim = __pyx_v_ndim;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":214
 *             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.
 */
  if (__pyx_v_copy_shape) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":217
 *                 # 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) * ndim * 2)             # <<<<<<<<<<<<<<
 *                 info.shape = info.strides + ndim
 *                 for i in range(ndim):
 */
    __pyx_v_info->strides = ((Py_ssize_t *)malloc((((sizeof(Py_ssize_t)) * __pyx_v_ndim) * 2)));

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":218
 *                 # as one block, strides first.
 *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_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);

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":219
 *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_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_6 = __pyx_v_ndim;
    for (__pyx_t_7 = 0; __pyx_t_7 < __pyx_t_6; __pyx_t_7+=1) {
      __pyx_v_i = __pyx_t_7;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":220
 *                 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(((PyArrayObject *)__pyx_v_self))[__pyx_v_i]);

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":221
 *                 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(((PyArrayObject *)__pyx_v_self))[__pyx_v_i]);
    }
    goto __pyx_L8;
  }
  /*else*/ {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":223
 *                     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(((PyArrayObject *)__pyx_v_self)));

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":224
 *             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(((PyArrayObject *)__pyx_v_self)));
  }
  __pyx_L8:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":225
 *                 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;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":226
 *                 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(((PyArrayObject *)__pyx_v_self));

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":227
 *             info.suboffsets = NULL
 *             info.itemsize = PyArray_ITEMSIZE(self)
 *             info.readonly = not PyArray_ISWRITEABLE(self)             # <<<<<<<<<<<<<<
 * 
 *             cdef int t
 */
  __pyx_v_info->readonly = (!PyArray_ISWRITEABLE(((PyArrayObject *)__pyx_v_self)));

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":230
 * 
 *             cdef int t
 *             cdef char* f = NULL             # <<<<<<<<<<<<<<
 *             cdef dtype descr = self.descr
 *             cdef list stack
 */
  __pyx_v_f = NULL;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":231
 *             cdef int t
 *             cdef char* f = NULL
 *             cdef dtype descr = self.descr             # <<<<<<<<<<<<<<
 *             cdef list stack
 *             cdef int offset
 */
  __Pyx_INCREF(((PyObject *)((PyArrayObject *)__pyx_v_self)->descr));
  __pyx_v_descr = ((PyArrayObject *)__pyx_v_self)->descr;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":235
 *             cdef int offset
 * 
 *             cdef bint hasfields = PyDataType_HASFIELDS(descr)             # <<<<<<<<<<<<<<
 * 
 *             if not hasfields and not copy_shape:
 */
  __pyx_v_hasfields = PyDataType_HASFIELDS(__pyx_v_descr);

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":237
 *             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);
  if (__pyx_t_2) {
    __pyx_t_3 = (!__pyx_v_copy_shape);
    __pyx_t_1 = __pyx_t_3;
  } else {
    __pyx_t_1 = __pyx_t_2;
  }
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":239
 *             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_L11;
  }
  /*else*/ {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":242
 *             else:
 *                 # need to call releasebuffer
 *                 info.obj = self             # <<<<<<<<<<<<<<
 * 
 *             if not hasfields:
 */
    __Pyx_INCREF(__pyx_v_self);
    __Pyx_GIVEREF(__pyx_v_self);
    __Pyx_GOTREF(__pyx_v_info->obj);
    __Pyx_DECREF(__pyx_v_info->obj);
    __pyx_v_info->obj = __pyx_v_self;
  }
  __pyx_L11:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":244
 *                 info.obj = self
 * 
 *             if not hasfields:             # <<<<<<<<<<<<<<
 *                 t = descr.type_num
 *                 if ((descr.byteorder == '>' and little_endian) or
 */
  __pyx_t_1 = (!__pyx_v_hasfields);
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":245
 * 
 *             if not hasfields:
 *                 t = descr.type_num             # <<<<<<<<<<<<<<
 *                 if ((descr.byteorder == '>' and little_endian) or
 *                     (descr.byteorder == '<' and not little_endian)):
 */
    __pyx_v_t = __pyx_v_descr->type_num;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":246
 *             if not hasfields:
 *                 t = descr.type_num
 *                 if ((descr.byteorder == '>' and little_endian) or             # <<<<<<<<<<<<<<
 *                     (descr.byteorder == '<' and not little_endian)):
 *                     raise ValueError(u"Non-native byte order not supported")
 */
    __pyx_t_1 = (__pyx_v_descr->byteorder == '>');
    if (__pyx_t_1) {
      __pyx_t_2 = __pyx_v_little_endian;
    } else {
      __pyx_t_2 = __pyx_t_1;
    }
    if (!__pyx_t_2) {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":247
 *                 t = descr.type_num
 *                 if ((descr.byteorder == '>' and little_endian) or
 *                     (descr.byteorder == '<' 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 == '<');
      if (__pyx_t_1) {
        __pyx_t_3 = (!__pyx_v_little_endian);
        __pyx_t_8 = __pyx_t_3;
      } else {
        __pyx_t_8 = __pyx_t_1;
      }
      __pyx_t_1 = __pyx_t_8;
    } else {
      __pyx_t_1 = __pyx_t_2;
    }
    if (__pyx_t_1) {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":248
 *                 if ((descr.byteorder == '>' and little_endian) or
 *                     (descr.byteorder == '<' 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 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_INCREF(((PyObject *)__pyx_kp_u_9));
      PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_kp_u_9));
      __Pyx_GIVEREF(((PyObject *)__pyx_kp_u_9));
      __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      __Pyx_Raise(__pyx_t_5, 0, 0);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 248; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L13;
    }
    __pyx_L13:;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":249
 *                     (descr.byteorder == '<' 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_BYTE);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__b;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":250
 *                     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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_UBYTE);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__B;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":251
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_SHORT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__h;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":252
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_USHORT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__H;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":253
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_INT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__i;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":254
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_UINT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__I;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":255
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_LONG);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__l;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":256
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_ULONG);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__L;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":257
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_LONGLONG);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__q;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":258
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_ULONGLONG);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__Q;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":259
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_FLOAT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__f;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":260
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_DOUBLE);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__d;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":261
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_LONGDOUBLE);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__g;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":262
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_CFLOAT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__Zf;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":263
 *                 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"
 */
    __pyx_t_1 = (__pyx_v_t == NPY_CDOUBLE);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__Zd;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":264
 *                 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:
 */
    __pyx_t_1 = (__pyx_v_t == NPY_CLONGDOUBLE);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__Zg;
      goto __pyx_L14;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":265
 *                 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)
 */
    __pyx_t_1 = (__pyx_v_t == NPY_OBJECT);
    if (__pyx_t_1) {
      __pyx_v_f = __pyx_k__O;
      goto __pyx_L14;
    }
    /*else*/ {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":267
 *                 elif t == NPY_OBJECT:      f = "O"
 *                 else:
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)             # <<<<<<<<<<<<<<
 *                 info.format = f
 *                 return
 */
      __pyx_t_5 = PyInt_FromLong(__pyx_v_t); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_u_10), __pyx_t_5); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(((PyObject *)__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[1]; __pyx_lineno = 267; __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 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_4);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_Raise(__pyx_t_4, 0, 0);
      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 267; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }
    __pyx_L14:;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":268
 *                 else:
 *                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 *                 info.format = f             # <<<<<<<<<<<<<<
 *                 return
 *             else:
 */
    __pyx_v_info->format = __pyx_v_f;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":269
 *                     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_L12;
  }
  /*else*/ {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":271
 *                 return
 *             else:
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)             # <<<<<<<<<<<<<<
 *                 info.format[0] = '^' # Native data types, manual alignment
 *                 offset = 0
 */
    __pyx_v_info->format = ((char *)malloc(255));

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":272
 *             else:
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
 *                 info.format[0] = '^' # Native data types, manual alignment             # <<<<<<<<<<<<<<
 *                 offset = 0
 *                 f = _util_dtypestring(descr, info.format + 1,
 */
    (__pyx_v_info->format[0]) = '^';

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":273
 *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
 *                 info.format[0] = '^' # Native data types, manual alignment
 *                 offset = 0             # <<<<<<<<<<<<<<
 *                 f = _util_dtypestring(descr, info.format + 1,
 *                                       info.format + _buffer_format_string_len,
 */
    __pyx_v_offset = 0;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":276
 *                 f = _util_dtypestring(descr, info.format + 1,
 *                                       info.format + _buffer_format_string_len,
 *                                       &offset)             # <<<<<<<<<<<<<<
 *                 f[0] = 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 = 274; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __pyx_v_f = __pyx_t_9;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":277
 *                                       info.format + _buffer_format_string_len,
 *                                       &offset)
 *                 f[0] = 0 # Terminate format string             # <<<<<<<<<<<<<<
 * 
 *         def __releasebuffer__(ndarray self, Py_buffer* info):
 */
    (__pyx_v_f[0]) = 0;
  }
  __pyx_L12:;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_AddTraceback("numpy.ndarray.__getbuffer__");
  __pyx_r = -1;
  __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->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;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":279
 *                 f[0] = 0 # Terminate format string
 * 
 *         def __releasebuffer__(ndarray self, Py_buffer* info):             # <<<<<<<<<<<<<<
 *             if PyArray_HASFIELDS(self):
 *                 stdlib.free(info.format)
 */

static CYTHON_UNUSED void __pyx_pf_5numpy_7ndarray___releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info); /*proto*/
static CYTHON_UNUSED void __pyx_pf_5numpy_7ndarray___releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info) {
  int __pyx_t_1;
  __Pyx_RefNannySetupContext("__releasebuffer__");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":280
 * 
 *         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(((PyArrayObject *)__pyx_v_self));
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":281
 *         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_L5;
  }
  __pyx_L5:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":282
 *             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)));
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":283
 *                 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_L6;
  }
  __pyx_L6:;

  __Pyx_RefNannyFinishContext();
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":756
 * 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;
  PyObject *__pyx_t_1 = NULL;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew1");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":757
 * 
 * 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 = 757; __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_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":759
 *     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;
  PyObject *__pyx_t_1 = NULL;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew2");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":760
 * 
 * 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 = 760; __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_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":762
 *     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;
  PyObject *__pyx_t_1 = NULL;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew3");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":763
 * 
 * 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 = 763; __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_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":765
 *     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;
  PyObject *__pyx_t_1 = NULL;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew4");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":766
 * 
 * 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 = 766; __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_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":768
 *     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;
  PyObject *__pyx_t_1 = NULL;
  __Pyx_RefNannySetupContext("PyArray_MultiIterNew5");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":769
 * 
 * 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 = 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_MultiIterNew5");
  __pyx_r = 0;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":771
 *     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;
  int __pyx_v_endian_detector;
  int __pyx_v_little_endian;
  PyObject *__pyx_v_fields;
  PyObject *__pyx_v_childname;
  PyObject *__pyx_v_new_offset;
  PyObject *__pyx_v_t;
  char *__pyx_r;
  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;
  int __pyx_t_8;
  int __pyx_t_9;
  char *__pyx_t_10;
  __Pyx_RefNannySetupContext("_util_dtypestring");
  __pyx_v_child = ((PyArray_Descr *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_fields = ((PyObject *)Py_None); __Pyx_INCREF(Py_None);
  __pyx_v_childname = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_new_offset = Py_None; __Pyx_INCREF(Py_None);
  __pyx_v_t = Py_None; __Pyx_INCREF(Py_None);

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":778
 *     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;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":779
 *     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);

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":782
 *     cdef tuple fields
 * 
 *     for childname in descr.names:             # <<<<<<<<<<<<<<
 *         fields = descr.fields[childname]
 *         child, new_offset = fields
 */
  if (likely(((PyObject *)__pyx_v_descr->names) != Py_None)) {
    __pyx_t_1 = 0; __pyx_t_2 = ((PyObject *)__pyx_v_descr->names); __Pyx_INCREF(__pyx_t_2);
  } else {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[1]; __pyx_lineno = 782; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  }
  for (;;) {
    if (__pyx_t_1 >= PyTuple_GET_SIZE(__pyx_t_2)) break;
    __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_1); __Pyx_INCREF(__pyx_t_3); __pyx_t_1++;
    __Pyx_DECREF(__pyx_v_childname);
    __pyx_v_childname = __pyx_t_3;
    __pyx_t_3 = 0;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":783
 * 
 *     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 = 783; __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 = 783; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(((PyObject *)__pyx_v_fields));
    __pyx_v_fields = ((PyObject *)__pyx_t_3);
    __pyx_t_3 = 0;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":784
 *     for childname in descr.names:
 *         fields = descr.fields[childname]
 *         child, new_offset = fields             # <<<<<<<<<<<<<<
 * 
 *         if (end - f) - (new_offset - offset[0]) < 15:
 */
    if (likely(((PyObject *)__pyx_v_fields) != Py_None) && likely(PyTuple_GET_SIZE(((PyObject *)__pyx_v_fields)) == 2)) {
      PyObject* tuple = ((PyObject *)__pyx_v_fields);
      __pyx_t_3 = PyTuple_GET_ITEM(tuple, 0); __Pyx_INCREF(__pyx_t_3);
      if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_dtype))))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 784; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_t_4 = PyTuple_GET_ITEM(tuple, 1); __Pyx_INCREF(__pyx_t_4);
      __Pyx_DECREF(((PyObject *)__pyx_v_child));
      __pyx_v_child = ((PyArray_Descr *)__pyx_t_3);
      __pyx_t_3 = 0;
      __Pyx_DECREF(__pyx_v_new_offset);
      __pyx_v_new_offset = __pyx_t_4;
      __pyx_t_4 = 0;
    } else {
      __Pyx_UnpackTupleError(((PyObject *)__pyx_v_fields), 2);
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 784; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":786
 *         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 = 786; __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 = 786; __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 = 786; __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 = 786; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_5);
    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 786; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
    if (__pyx_t_6) {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":787
 * 
 *         if (end - f) - (new_offset - offset[0]) < 15:
 *             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")             # <<<<<<<<<<<<<<
 * 
 *         if ((child.byteorder == '>' and little_endian) or
 */
      __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_INCREF(((PyObject *)__pyx_kp_u_11));
      PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_kp_u_11));
      __Pyx_GIVEREF(((PyObject *)__pyx_kp_u_11));
      __pyx_t_3 = PyObject_Call(__pyx_builtin_RuntimeError, __pyx_t_5, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __Pyx_Raise(__pyx_t_3, 0, 0);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L5;
    }
    __pyx_L5:;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":789
 *             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")
 * 
 *         if ((child.byteorder == '>' and little_endian) or             # <<<<<<<<<<<<<<
 *             (child.byteorder == '<' and not little_endian)):
 *             raise ValueError(u"Non-native byte order not supported")
 */
    __pyx_t_6 = (__pyx_v_child->byteorder == '>');
    if (__pyx_t_6) {
      __pyx_t_7 = __pyx_v_little_endian;
    } else {
      __pyx_t_7 = __pyx_t_6;
    }
    if (!__pyx_t_7) {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":790
 * 
 *         if ((child.byteorder == '>' and little_endian) or
 *             (child.byteorder == '<' 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_6 = (__pyx_v_child->byteorder == '<');
      if (__pyx_t_6) {
        __pyx_t_8 = (!__pyx_v_little_endian);
        __pyx_t_9 = __pyx_t_8;
      } else {
        __pyx_t_9 = __pyx_t_6;
      }
      __pyx_t_6 = __pyx_t_9;
    } else {
      __pyx_t_6 = __pyx_t_7;
    }
    if (__pyx_t_6) {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":791
 *         if ((child.byteorder == '>' and little_endian) or
 *             (child.byteorder == '<' 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_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 791; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_INCREF(((PyObject *)__pyx_kp_u_9));
      PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_kp_u_9));
      __Pyx_GIVEREF(((PyObject *)__pyx_kp_u_9));
      __pyx_t_5 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 791; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_Raise(__pyx_t_5, 0, 0);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 791; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      goto __pyx_L6;
    }
    __pyx_L6:;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":801
 * 
 *         # 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 = 801; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 801; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (!__pyx_t_6) break;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":802
 *         # Output padding bytes
 *         while offset[0] < new_offset:
 *             f[0] = 120 # "x"; pad byte             # <<<<<<<<<<<<<<
 *             f += 1
 *             offset[0] += 1
 */
      (__pyx_v_f[0]) = 120;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":803
 *         while offset[0] < new_offset:
 *             f[0] = 120 # "x"; pad byte
 *             f += 1             # <<<<<<<<<<<<<<
 *             offset[0] += 1
 * 
 */
      __pyx_v_f += 1;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":804
 *             f[0] = 120 # "x"; pad byte
 *             f += 1
 *             offset[0] += 1             # <<<<<<<<<<<<<<
 * 
 *         offset[0] += child.itemsize
 */
      (__pyx_v_offset[0]) += 1;
    }

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":806
 *             offset[0] += 1
 * 
 *         offset[0] += child.itemsize             # <<<<<<<<<<<<<<
 * 
 *         if not PyDataType_HASFIELDS(child):
 */
    (__pyx_v_offset[0]) += __pyx_v_child->elsize;

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":808
 *         offset[0] += child.itemsize
 * 
 *         if not PyDataType_HASFIELDS(child):             # <<<<<<<<<<<<<<
 *             t = child.type_num
 *             if end - f < 5:
 */
    __pyx_t_6 = (!PyDataType_HASFIELDS(__pyx_v_child));
    if (__pyx_t_6) {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":809
 * 
 *         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 = 809; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_v_t);
      __pyx_v_t = __pyx_t_3;
      __pyx_t_3 = 0;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":810
 *         if not PyDataType_HASFIELDS(child):
 *             t = child.type_num
 *             if end - f < 5:             # <<<<<<<<<<<<<<
 *                 raise RuntimeError(u"Format string allocated too short.")
 * 
 */
      __pyx_t_6 = ((__pyx_v_end - __pyx_v_f) < 5);
      if (__pyx_t_6) {

        /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":811
 *             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 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_3);
        __Pyx_INCREF(((PyObject *)__pyx_kp_u_12));
        PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_kp_u_12));
        __Pyx_GIVEREF(((PyObject *)__pyx_kp_u_12));
        __pyx_t_5 = PyObject_Call(__pyx_builtin_RuntimeError, __pyx_t_3, NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
        __Pyx_Raise(__pyx_t_5, 0, 0);
        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 811; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        goto __pyx_L10;
      }
      __pyx_L10:;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":814
 * 
 *             # 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_5 = PyInt_FromLong(NPY_BYTE); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 814; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 814; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 98;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":815
 *             # 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_3 = PyInt_FromLong(NPY_UBYTE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 815; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 815; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 815; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 66;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":816
 *             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_5 = PyInt_FromLong(NPY_SHORT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 816; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 816; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 104;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":817
 *             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_3 = PyInt_FromLong(NPY_USHORT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 817; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 817; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 72;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":818
 *             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_5 = PyInt_FromLong(NPY_INT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 818; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 818; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 105;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":819
 *             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_3 = PyInt_FromLong(NPY_UINT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 819; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 819; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 73;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":820
 *             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_5 = PyInt_FromLong(NPY_LONG); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 820; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 820; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 108;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":821
 *             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_3 = PyInt_FromLong(NPY_ULONG); 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_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 76;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":822
 *             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_5 = PyInt_FromLong(NPY_LONGLONG); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 822; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 822; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 113;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":823
 *             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_3 = PyInt_FromLong(NPY_ULONGLONG); 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_t_5 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 81;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":824
 *             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_5 = PyInt_FromLong(NPY_FLOAT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 824; __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); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_3);
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 824; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 102;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":825
 *             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_3 = PyInt_FromLong(NPY_DOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 825; __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); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 825; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_GOTREF(__pyx_t_5);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 825; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 100;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":826
 *             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_5 = PyInt_FromLong(NPY_LONGDOUBLE); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __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); 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_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 103;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":827
 *             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_3 = PyInt_FromLong(NPY_CFLOAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __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); 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_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 90;
        (__pyx_v_f[1]) = 102;
        __pyx_v_f += 1;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":828
 *             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_5 = PyInt_FromLong(NPY_CDOUBLE); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __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); 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_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 90;
        (__pyx_v_f[1]) = 100;
        __pyx_v_f += 1;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":829
 *             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_3 = PyInt_FromLong(NPY_CLONGDOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __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); 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_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_5); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 90;
        (__pyx_v_f[1]) = 103;
        __pyx_v_f += 1;
        goto __pyx_L11;
      }

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":830
 *             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_5 = PyInt_FromLong(NPY_OBJECT); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __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); 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_DECREF(__pyx_t_5); __pyx_t_5 = 0;
      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
      if (__pyx_t_6) {
        (__pyx_v_f[0]) = 79;
        goto __pyx_L11;
      }
      /*else*/ {

        /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":832
 *             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_3 = PyNumber_Remainder(((PyObject *)__pyx_kp_u_10), __pyx_v_t); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(((PyObject *)__pyx_t_3));
        __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
        __Pyx_GOTREF(__pyx_t_5);
        PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_3));
        __Pyx_GIVEREF(((PyObject *)__pyx_t_3));
        __pyx_t_3 = 0;
        __pyx_t_3 = PyObject_Call(__pyx_builtin_ValueError, __pyx_t_5, NULL); 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_DECREF(__pyx_t_5); __pyx_t_5 = 0;
        __Pyx_Raise(__pyx_t_3, 0, 0);
        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      }
      __pyx_L11:;

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":833
 *             else:
 *                 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
 *             f += 1             # <<<<<<<<<<<<<<
 *         else:
 *             # Cython ignores struct boundary information ("T{...}"),
 */
      __pyx_v_f += 1;
      goto __pyx_L9;
    }
    /*else*/ {

      /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":837
 *             # Cython ignores struct boundary information ("T{...}"),
 *             # so don't output it
 *             f = _util_dtypestring(child, f, end, offset)             # <<<<<<<<<<<<<<
 *     return f
 * 
 */
      __pyx_t_10 = __pyx_f_5numpy__util_dtypestring(__pyx_v_child, __pyx_v_f, __pyx_v_end, __pyx_v_offset); if (unlikely(__pyx_t_10 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
      __pyx_v_f = __pyx_t_10;
    }
    __pyx_L9:;
  }
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":838
 *             # 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_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_4);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_AddTraceback("numpy._util_dtypestring");
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_DECREF((PyObject *)__pyx_v_child);
  __Pyx_DECREF(__pyx_v_fields);
  __Pyx_DECREF(__pyx_v_childname);
  __Pyx_DECREF(__pyx_v_new_offset);
  __Pyx_DECREF(__pyx_v_t);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":953
 * 
 * 
 * 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;
  int __pyx_t_1;
  __Pyx_RefNannySetupContext("set_array_base");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":955
 * 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);
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":956
 *      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*/ {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":958
 *          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);

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":959
 *      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:;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":960
 *          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);

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":961
 *          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();
}

/* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":963
 *      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;
  int __pyx_t_1;
  __Pyx_RefNannySetupContext("get_array_base");

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":964
 * 
 * cdef inline object get_array_base(ndarray arr):
 *     if arr.base is NULL:             # <<<<<<<<<<<<<<
 *         return None
 *     else:
 */
  __pyx_t_1 = (__pyx_v_arr->base == NULL);
  if (__pyx_t_1) {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":965
 * 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*/ {

    /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/numpy.pxd":967
 *         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[] = {
  {__Pyx_NAMESTR("normalized_3vec"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_normalized_3vec, METH_O, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_normalized_3vec)},
  {__Pyx_NAMESTR("norm_3vec"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_norm_3vec, METH_O, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_norm_3vec)},
  {__Pyx_NAMESTR("inner_3vecs"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_inner_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)},
  {__Pyx_NAMESTR("sub_3vecs"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_sub_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)},
  {__Pyx_NAMESTR("add_3vecs"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_add_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)},
  {__Pyx_NAMESTR("mul_3vecs"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_mul_3vecs, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)},
  {__Pyx_NAMESTR("mul_3vec"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_mul_3vec, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)},
  {__Pyx_NAMESTR("cut_plane"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_cut_plane, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_cut_plane)},
  {__Pyx_NAMESTR("most_similar_track_mam"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_most_similar_track_mam, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_most_similar_track_mam)},
  {__Pyx_NAMESTR("bundles_distances_mam"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_bundles_distances_mam, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_bundles_distances_mam)},
  {__Pyx_NAMESTR("mam_distances"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_mam_distances, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_mam_distances)},
  {__Pyx_NAMESTR("minimum_closest_distance"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_minimum_closest_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_minimum_closest_distance)},
  {__Pyx_NAMESTR("lee_perpendicular_distance"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_lee_perpendicular_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_lee_perpendicular_distance)},
  {__Pyx_NAMESTR("lee_angle_distance"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_lee_angle_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_lee_angle_distance)},
  {__Pyx_NAMESTR("approx_polygon_track"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_approx_polygon_track, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_approx_polygon_track)},
  {__Pyx_NAMESTR("approximate_mdl_trajectory"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_approximate_mdl_trajectory, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_approximate_mdl_trajectory)},
  {__Pyx_NAMESTR("intersect_segment_cylinder"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_intersect_segment_cylinder, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_intersect_segment_cylinder)},
  {__Pyx_NAMESTR("point_segment_sq_distance"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_point_segment_sq_distance, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_point_segment_sq_distance)},
  {__Pyx_NAMESTR("track_dist_3pts"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_track_dist_3pts, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_track_dist_3pts)},
  {__Pyx_NAMESTR("local_skeleton_clustering"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_local_skeleton_clustering, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_local_skeleton_clustering)},
  {__Pyx_NAMESTR("local_skeleton_clustering_3pts"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_local_skeleton_clustering_3pts, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_local_skeleton_clustering_3pts)},
  {__Pyx_NAMESTR("larch_3split"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_larch_3split, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_larch_3split)},
  {__Pyx_NAMESTR("larch_3merge"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_larch_3merge, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_larch_3merge)},
  {__Pyx_NAMESTR("point_track_sq_distance_check"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_point_track_sq_distance_check, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_point_track_sq_distance_check)},
  {__Pyx_NAMESTR("track_roi_intersection_check"), (PyCFunction)__pyx_pf_4dipy_8tracking_9distances_track_roi_intersection_check, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_4dipy_8tracking_9distances_track_roi_intersection_check)},
  {0, 0, 0, 0}
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef __pyx_moduledef = {
    PyModuleDef_HEAD_INIT,
    __Pyx_NAMESTR("distances"),
    __Pyx_DOCSTR(__pyx_k_13), /* 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_kp_s_1, __pyx_k_1, sizeof(__pyx_k_1), 0, 0, 1, 0},
  {&__pyx_kp_u_10, __pyx_k_10, sizeof(__pyx_k_10), 0, 1, 0, 0},
  {&__pyx_kp_u_11, __pyx_k_11, sizeof(__pyx_k_11), 0, 1, 0, 0},
  {&__pyx_kp_u_12, __pyx_k_12, sizeof(__pyx_k_12), 0, 1, 0, 0},
  {&__pyx_kp_u_14, __pyx_k_14, sizeof(__pyx_k_14), 0, 1, 0, 0},
  {&__pyx_kp_u_15, __pyx_k_15, sizeof(__pyx_k_15), 0, 1, 0, 0},
  {&__pyx_kp_u_16, __pyx_k_16, sizeof(__pyx_k_16), 0, 1, 0, 0},
  {&__pyx_kp_u_17, __pyx_k_17, sizeof(__pyx_k_17), 0, 1, 0, 0},
  {&__pyx_n_s_18, __pyx_k_18, sizeof(__pyx_k_18), 0, 0, 1, 1},
  {&__pyx_kp_u_19, __pyx_k_19, sizeof(__pyx_k_19), 0, 1, 0, 0},
  {&__pyx_kp_s_2, __pyx_k_2, sizeof(__pyx_k_2), 0, 0, 1, 0},
  {&__pyx_n_s_20, __pyx_k_20, sizeof(__pyx_k_20), 0, 0, 1, 1},
  {&__pyx_kp_u_21, __pyx_k_21, sizeof(__pyx_k_21), 0, 1, 0, 0},
  {&__pyx_kp_u_22, __pyx_k_22, sizeof(__pyx_k_22), 0, 1, 0, 0},
  {&__pyx_n_s_23, __pyx_k_23, sizeof(__pyx_k_23), 0, 0, 1, 1},
  {&__pyx_kp_u_24, __pyx_k_24, sizeof(__pyx_k_24), 0, 1, 0, 0},
  {&__pyx_n_s_25, __pyx_k_25, sizeof(__pyx_k_25), 0, 0, 1, 1},
  {&__pyx_kp_u_26, __pyx_k_26, sizeof(__pyx_k_26), 0, 1, 0, 0},
  {&__pyx_kp_u_27, __pyx_k_27, sizeof(__pyx_k_27), 0, 1, 0, 0},
  {&__pyx_n_s_28, __pyx_k_28, sizeof(__pyx_k_28), 0, 0, 1, 1},
  {&__pyx_kp_u_29, __pyx_k_29, sizeof(__pyx_k_29), 0, 1, 0, 0},
  {&__pyx_n_s_30, __pyx_k_30, sizeof(__pyx_k_30), 0, 0, 1, 1},
  {&__pyx_kp_u_31, __pyx_k_31, sizeof(__pyx_k_31), 0, 1, 0, 0},
  {&__pyx_n_s_32, __pyx_k_32, sizeof(__pyx_k_32), 0, 0, 1, 1},
  {&__pyx_kp_u_33, __pyx_k_33, sizeof(__pyx_k_33), 0, 1, 0, 0},
  {&__pyx_n_s_34, __pyx_k_34, sizeof(__pyx_k_34), 0, 0, 1, 1},
  {&__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_n_s_37, __pyx_k_37, sizeof(__pyx_k_37), 0, 0, 1, 1},
  {&__pyx_kp_u_38, __pyx_k_38, sizeof(__pyx_k_38), 0, 1, 0, 0},
  {&__pyx_n_s_39, __pyx_k_39, sizeof(__pyx_k_39), 0, 0, 1, 1},
  {&__pyx_kp_u_40, __pyx_k_40, sizeof(__pyx_k_40), 0, 1, 0, 0},
  {&__pyx_kp_u_41, __pyx_k_41, sizeof(__pyx_k_41), 0, 1, 0, 0},
  {&__pyx_kp_u_42, __pyx_k_42, sizeof(__pyx_k_42), 0, 1, 0, 0},
  {&__pyx_n_s_43, __pyx_k_43, sizeof(__pyx_k_43), 0, 0, 1, 1},
  {&__pyx_kp_u_44, __pyx_k_44, sizeof(__pyx_k_44), 0, 1, 0, 0},
  {&__pyx_n_s_45, __pyx_k_45, sizeof(__pyx_k_45), 0, 0, 1, 1},
  {&__pyx_kp_u_7, __pyx_k_7, sizeof(__pyx_k_7), 0, 1, 0, 0},
  {&__pyx_kp_u_8, __pyx_k_8, sizeof(__pyx_k_8), 0, 1, 0, 0},
  {&__pyx_kp_u_9, __pyx_k_9, sizeof(__pyx_k_9), 0, 1, 0, 0},
  {&__pyx_n_s__C, __pyx_k__C, sizeof(__pyx_k__C), 0, 0, 1, 1},
  {&__pyx_n_s__N, __pyx_k__N, sizeof(__pyx_k__N), 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____main__, __pyx_k____main__, sizeof(__pyx_k____main__), 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__all, __pyx_k__all, sizeof(__pyx_k__all), 0, 0, 1, 1},
  {&__pyx_n_s__alpha, __pyx_k__alpha, sizeof(__pyx_k__alpha), 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__base, __pyx_k__base, sizeof(__pyx_k__base), 0, 0, 1, 1},
  {&__pyx_n_s__buf, __pyx_k__buf, sizeof(__pyx_k__buf), 0, 0, 1, 1},
  {&__pyx_n_s__byteorder, __pyx_k__byteorder, sizeof(__pyx_k__byteorder), 0, 0, 1, 1},
  {&__pyx_n_s__c, __pyx_k__c, sizeof(__pyx_k__c), 0, 0, 1, 1},
  {&__pyx_n_s__copy, __pyx_k__copy, sizeof(__pyx_k__copy), 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_thr, __pyx_k__d_thr, sizeof(__pyx_k__d_thr), 0, 0, 1, 1},
  {&__pyx_n_s__data, __pyx_k__data, sizeof(__pyx_k__data), 0, 0, 1, 1},
  {&__pyx_n_s__descr, __pyx_k__descr, sizeof(__pyx_k__descr), 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__fields, __pyx_k__fields, sizeof(__pyx_k__fields), 0, 0, 1, 1},
  {&__pyx_n_s__float32, __pyx_k__float32, sizeof(__pyx_k__float32), 0, 0, 1, 1},
  {&__pyx_n_s__format, __pyx_k__format, sizeof(__pyx_k__format), 0, 0, 1, 1},
  {&__pyx_n_s__hidden, __pyx_k__hidden, sizeof(__pyx_k__hidden), 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__int64, __pyx_k__int64, sizeof(__pyx_k__int64), 0, 0, 1, 1},
  {&__pyx_n_s__itemsize, __pyx_k__itemsize, sizeof(__pyx_k__itemsize), 0, 0, 1, 1},
  {&__pyx_n_s__keys, __pyx_k__keys, sizeof(__pyx_k__keys), 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__lee_angle_distance, __pyx_k__lee_angle_distance, sizeof(__pyx_k__lee_angle_distance), 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__metric, __pyx_k__metric, sizeof(__pyx_k__metric), 0, 0, 1, 1},
  {&__pyx_n_s__min, __pyx_k__min, sizeof(__pyx_k__min), 0, 0, 1, 1},
  {&__pyx_n_s__names, __pyx_k__names, sizeof(__pyx_k__names), 0, 0, 1, 1},
  {&__pyx_n_s__ndim, __pyx_k__ndim, sizeof(__pyx_k__ndim), 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__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__obj, __pyx_k__obj, sizeof(__pyx_k__obj), 0, 0, 1, 1},
  {&__pyx_n_s__object, __pyx_k__object, sizeof(__pyx_k__object), 0, 0, 1, 1},
  {&__pyx_n_s__p, __pyx_k__p, sizeof(__pyx_k__p), 0, 0, 1, 1},
  {&__pyx_n_s__point, __pyx_k__point, sizeof(__pyx_k__point), 0, 0, 1, 1},
  {&__pyx_n_s__q, __pyx_k__q, sizeof(__pyx_k__q), 0, 0, 1, 1},
  {&__pyx_n_s__r, __pyx_k__r, sizeof(__pyx_k__r), 0, 0, 1, 1},
  {&__pyx_n_s__range, __pyx_k__range, sizeof(__pyx_k__range), 0, 0, 1, 1},
  {&__pyx_n_s__readonly, __pyx_k__readonly, sizeof(__pyx_k__readonly), 0, 0, 1, 1},
  {&__pyx_n_s__ref, __pyx_k__ref, sizeof(__pyx_k__ref), 0, 0, 1, 1},
  {&__pyx_n_s__rep3, __pyx_k__rep3, sizeof(__pyx_k__rep3), 0, 0, 1, 1},
  {&__pyx_n_s__roi, __pyx_k__roi, sizeof(__pyx_k__roi), 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__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__strides, __pyx_k__strides, sizeof(__pyx_k__strides), 0, 0, 1, 1},
  {&__pyx_n_s__suboffsets, __pyx_k__suboffsets, sizeof(__pyx_k__suboffsets), 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__track, __pyx_k__track, sizeof(__pyx_k__track), 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__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__tracksA, __pyx_k__tracksA, sizeof(__pyx_k__tracksA), 0, 0, 1, 1},
  {&__pyx_n_s__tracksB, __pyx_k__tracksB, sizeof(__pyx_k__tracksB), 0, 0, 1, 1},
  {&__pyx_n_s__type_num, __pyx_k__type_num, sizeof(__pyx_k__type_num), 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__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__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_GetName(__pyx_b, __pyx_n_s__range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 120; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_builtin_ValueError = __Pyx_GetName(__pyx_b, __pyx_n_s__ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 398; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_builtin_object = __Pyx_GetName(__pyx_b, __pyx_n_s__object); if (!__pyx_builtin_object) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 404; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_builtin_RuntimeError = __Pyx_GetName(__pyx_b, __pyx_n_s__RuntimeError); if (!__pyx_builtin_RuntimeError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 787; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  return 0;
  __pyx_L1_error:;
  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_2 = PyInt_FromLong(2); if (unlikely(!__pyx_int_2)) {__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;
  #if CYTHON_REFNANNY
  void* __pyx_refnanny = NULL;
  __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");
  }
  __pyx_refnanny = __Pyx_RefNanny->SetupContext("PyMODINIT_FUNC PyInit_distances(void)", __LINE__, __FILE__);
  #endif
  __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_binding_PyCFunctionType_USED
  if (__pyx_binding_PyCFunctionType_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_13), 0, PYTHON_API_VERSION);
  #else
  __pyx_m = PyModule_Create(&__pyx_moduledef);
  #endif
  if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  #if PY_MAJOR_VERSION < 3
  Py_INCREF(__pyx_m);
  #endif
  __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));
  if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
  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 (__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;}
  /*--- Global init code ---*/
  __pyx_v_4dipy_8tracking_9distances_f32_dt = ((PyArray_Descr *)Py_None); Py_INCREF(Py_None);
  /*--- Function export code ---*/
  /*--- Type init code ---*/
  /*--- Type import code ---*/
  __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 = 149; __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 = 159; __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 = 163; __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 = 172; __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 = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  /*--- Function import code ---*/
  /*--- Execution code ---*/

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":10
 * cimport cython
 * 
 * import time             # <<<<<<<<<<<<<<
 * import numpy as np
 * cimport numpy as cnp
 */
  __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__time), 0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyObject_SetAttr(__pyx_m, __pyx_n_s__time, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":11
 * 
 * import time
 * import numpy as np             # <<<<<<<<<<<<<<
 * cimport numpy as cnp
 * 
 */
  __pyx_t_1 = __Pyx_Import(((PyObject *)__pyx_n_s__numpy), 0); 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 (PyObject_SetAttr(__pyx_m, __pyx_n_s__np, __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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":190
 * 
 * # float 32 dtype for casting
 * cdef cnp.dtype f32_dt = np.dtype(np.float32)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  __pyx_t_1 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_2 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__dtype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __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_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_3 = PyObject_GetAttr(__pyx_t_1, __pyx_n_s__float32); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __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 = 190; __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, __pyx_t_1, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__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 = 190; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(((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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":536
 * 
 * 
 * cdef cnp.float32_t inf = np.inf             # <<<<<<<<<<<<<<
 * 
 * @cython.cdivision(True)
 */
  __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__np); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__inf); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __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_AsDouble(__pyx_t_1); if (unlikely((__pyx_t_4 == (npy_float32)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 536; __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;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":935
 * 
 * 
 * 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 = 935; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_3 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1018
 * 
 * 
 * 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 = 1018; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_4 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1705
 * 
 * 
 * def larch_3split(tracks,indices=None,thr=10.):             # <<<<<<<<<<<<<<
 * 
 *     ''' Generate a first pass clustering using 3 points (first, mid and last) on the tracks only.
 */
  __pyx_t_1 = PyFloat_FromDouble(10.); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1705; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_5 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/dipy/tracking/distances.pyx":1813
 * 
 * 
 * def larch_3merge(C,thr=10.):             # <<<<<<<<<<<<<<
 *     ''' Reassign tracks to existing clusters by merging clusters that their representative tracks are not very distant i.e. less than sqd_thr. Using tracks consisting of 3 points (first, mid and last). This is necessary after running larch_fast_split after multiple split in different levels (squared thresholds) as some of them have created independent clusters.
 *     Parameters
 */
  __pyx_t_1 = PyFloat_FromDouble(10.); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_k_6 = __pyx_t_1;
  __Pyx_GIVEREF(__pyx_t_1);
  __pyx_t_1 = 0;

  /* "/Users/mb312/dev_trees/dipy/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));
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s__normalized_3vec); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_14), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s__norm_3vec); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_15), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s__cut_plane); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_16), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s_18); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_17), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s_20); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_19), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s__mam_distances); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_21), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s_23); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_22), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s_25); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_24), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s__lee_angle_distance); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_26), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s_28); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_27), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s_30); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_29), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s_32); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_31), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s_34); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_33), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s__track_dist_3pts); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_35), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s_37); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_36), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s_39); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_38), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s__larch_3split); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_40), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s__larch_3merge); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_41), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_t_3 = PyObject_GetAttr(__pyx_m, __pyx_n_s_43); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __pyx_t_2 = __Pyx_GetAttrString(__pyx_t_3, "__doc__"); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_42), __pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyObject_GetAttr(__pyx_m, __pyx_n_s_45); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_GetAttrString(__pyx_t_2, "__doc__"); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __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_1, ((PyObject *)__pyx_kp_u_44), __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  if (PyObject_SetAttr(__pyx_m, __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;

  /* "/Users/mb312/usr/local/lib/python2.6/site-packages/Cython/Includes/libc/stdio.pxd":1
 * # 7.19 Input/output <stdio.h>             # <<<<<<<<<<<<<<
 * 
 * cdef extern from *:
 */
  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");
    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 */

static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
    PyObject *result;
    result = PyObject_GetAttr(dict, name);
    if (!result)
        PyErr_SetObject(PyExc_NameError, 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;
}

typedef struct {
  __Pyx_StructField root;
  __Pyx_BufFmt_StackElem* head;
  size_t fmt_offset;
  int new_count, enc_count;
  int is_complex;
  char enc_type;
  char packmode;
} __Pyx_BufFmt_Context;

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->packmode = '@';
  ctx->new_count = 1;
  ctx->enc_count = 0;
  ctx->enc_type = 0;
  ctx->is_complex = 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 void __Pyx_BufFmt_RaiseUnexpectedChar(char ch) {
  char msg[] = {ch, 0};
  PyErr_Format(PyExc_ValueError, "Unexpected format string character: '%s'", msg);
}

static const char* __Pyx_BufFmt_DescribeTypeChar(char ch, int is_complex) {
  switch (ch) {
    case 'b': return "'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 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': 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': 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_s_long_long;
#endif

static size_t __Pyx_BufFmt_TypeCharToAlignment(char ch, int is_complex) {
  switch (ch) {
    case '?': case 'c': case 'b': case 'B': 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_s_long_long) - 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;
    }
}

static size_t __Pyx_BufFmt_TypeCharToGroup(char ch, int is_complex) {
  switch (ch) {
    case 'c': case 'b': case 'h': case 'i': case 'l': case 'q': 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;
  if (ctx->enc_type == 0) return 0;
  group = __Pyx_BufFmt_TypeCharToGroup(ctx->enc_type, ctx->is_complex);
  do {
    __Pyx_StructField* field = ctx->head->field;
    __Pyx_TypeInfo* type = field->type;
  
    if (ctx->packmode == '@' || ctx->packmode == '^') {
      size = __Pyx_BufFmt_TypeCharToNativeSize(ctx->enc_type, ctx->is_complex);
    } else {
      size = __Pyx_BufFmt_TypeCharToStandardSize(ctx->enc_type, ctx->is_complex);
    }
    if (ctx->packmode == '@') {
      int align_at = __Pyx_BufFmt_TypeCharToAlignment(ctx->enc_type, ctx->is_complex);
      int 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 (type->size != size || type->typegroup != group) {
      if (type->typegroup == 'C' && type->fields != NULL) {
        /* special case -- treat as struct rather than complex number */
        size_t parent_offset = ctx->head->parent_offset + field->offset;
        ++ctx->head;
        ctx->head->field = type->fields;
        ctx->head->parent_offset = parent_offset;
        continue;
      }
    
      __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 %"PY_FORMAT_SIZE_T"d "
                   "but %"PY_FORMAT_SIZE_T"d expected", ctx->fmt_offset, offset);
      return -1;
    }

    ctx->fmt_offset += size;
  
    --ctx->enc_count; /* Consume from buffer string */

    /* Done checking, move to next field, pushing or popping struct stack if needed */
    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 int __Pyx_BufFmt_FirstPack(__Pyx_BufFmt_Context* ctx) {
  if (ctx->enc_type != 0 || ctx->packmode != '@') {
    PyErr_SetString(PyExc_ValueError, "Buffer packing mode currently only allowed at beginning of format string (this is a defect)");
    return -1;
  }
  return 0;
}

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;
        }
        if (__Pyx_BufFmt_FirstPack(ctx) == -1) return NULL;
        ctx->packmode = '=';
        ++ts;
        break;
      case '>':
      case '!':
        if (__Pyx_IsLittleEndian()) {
          PyErr_SetString(PyExc_ValueError, "Big-endian buffer not supported on little-endian compiler");
          return NULL;
        }
        if (__Pyx_BufFmt_FirstPack(ctx) == -1) return NULL;
        ctx->packmode = '=';
        ++ts;
        break;
      case '=':
      case '@':
      case '^':
        if (__Pyx_BufFmt_FirstPack(ctx) == -1) return NULL;
        ctx->packmode = *ts++;
        break;
      case 'T': /* substruct */
        {
          int i;
          const char* ts_after_sub;
          int struct_count = ctx->new_count;
          ctx->new_count = 1;
          ++ts;
          if (*ts != '{') {
            PyErr_SetString(PyExc_ValueError, "Buffer acquisition: Expected '{' after 'T'");
            return NULL;
          }
          ++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;
        }
        break;
      case '}': /* end of substruct; either repeat or move on */
        ++ts;
        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;
        ++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':
        if (ctx->enc_type == *ts && got_Z == ctx->is_complex) {
          /* Continue pooling same type */
          ctx->enc_count += ctx->new_count;
        } else {
          /* New type */
          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;
          ctx->enc_count = ctx->new_count;
          ctx->enc_type = *ts;
          ctx->is_complex = got_Z;
        }
        ++ts;
        ctx->new_count = 1;
        got_Z = 0;
        break;
      default:
        {
          ctx->new_count = __Pyx_BufFmt_ParseNumber(&ts);
          if (ctx->new_count == -1) { /* First char was not a digit */
            char msg[2] = { *ts, 0 };
            PyErr_Format(PyExc_ValueError,
                         "Does not understand character buffer dtype format string ('%s')", msg);
            return NULL;
          }
        }
      
    }
  }
}

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) {
    __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 (%"PY_FORMAT_SIZE_T"d byte%s) does not match size of '%s' (%"PY_FORMAT_SIZE_T"d byte%s)",
      buf->itemsize, (buf->itemsize > 1) ? "s" : "",
      dtype->name,
      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) {
    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);
}

static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {
    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;
}


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_AS_STRING(kw_name));
        #endif
}

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 *number, *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";
    }
    number = (num_expected == 1) ? "" : "s";
    PyErr_Format(PyExc_TypeError,
        #if PY_VERSION_HEX < 0x02050000
            "%s() takes %s %d positional argument%s (%d given)",
        #else
            "%s() takes %s %zd positional argument%s (%zd given)",
        #endif
        func_name, more_or_less, num_expected, number, num_found);
}

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;
        } else {
            #if PY_MAJOR_VERSION < 3
            if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) {
            #else
            if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) {
            #endif
                goto invalid_keyword_type;
            } else {
                for (name = first_kw_arg; *name; name++) {
                    #if PY_MAJOR_VERSION >= 3
                    if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                        PyUnicode_Compare(**name, key) == 0) break;
                    #else
                    if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                        _PyString_Eq(**name, key)) break;
                    #endif
                }
                if (*name) {
                    values[name-argnames] = value;
                } else {
                    /* unexpected keyword found */
                    for (name=argnames; name != first_kw_arg; name++) {
                        if (**name == key) goto arg_passed_twice;
                        #if PY_MAJOR_VERSION >= 3
                        if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
                            PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
                        #else
                        if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
                            _PyString_Eq(**name, key)) goto arg_passed_twice;
                        #endif
                    }
                    if (kwds2) {
                        if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
                    } else {
                        goto invalid_keyword;
                    }
                }
            }
        }
    }
    return 0;
arg_passed_twice:
    __Pyx_RaiseDoubleKeywordsError(function_name, **name);
    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 void __Pyx_RaiseBufferIndexError(int axis) {
  PyErr_Format(PyExc_IndexError,
     "Out of bounds on buffer access (axis %d)", axis);
}



static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
}


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_RaiseNoneNotIterableError(void) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
}

static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
    PyErr_Format(PyExc_ValueError,
        #if PY_VERSION_HEX < 0x02050000
                 "need more than %d value%s to unpack", (int)index,
        #else
                 "need more than %zd value%s to unpack", index,
        #endif
                 (index == 1) ? "" : "s");
}

static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
    PyErr_Format(PyExc_ValueError,
        #if PY_VERSION_HEX < 0x02050000
            "too many values to unpack (expected %d)", (int)expected);
        #else
            "too many values to unpack (expected %zd)", expected);
        #endif
}

static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
    if (t == Py_None) {
      __Pyx_RaiseNoneNotIterableError();
    } else if (PyTuple_GET_SIZE(t) < index) {
      __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
    } else {
      __Pyx_RaiseTooManyValuesError(index);
    }
}

#if PY_MAJOR_VERSION < 3
static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {
  #if PY_VERSION_HEX >= 0x02060000
  if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
      return PyObject_GetBuffer(obj, view, flags);
  #endif
  if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) return __pyx_pf_5numpy_7ndarray___getbuffer__(obj, view, flags);
  else {
  PyErr_Format(PyExc_TypeError, "'%100s' does not have the buffer interface", Py_TYPE(obj)->tp_name);
  return -1;
    }
}

static void __Pyx_ReleaseBuffer(Py_buffer *view) {
  PyObject* obj = view->obj;
  if (obj) {
if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) __pyx_pf_5numpy_7ndarray___releasebuffer__(obj, view);
    Py_DECREF(obj);
    view->obj = NULL;
  }
}

#endif

static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
    PyObject *py_import = 0;
    PyObject *empty_list = 0;
    PyObject *module = 0;
    PyObject *global_dict = 0;
    PyObject *empty_dict = 0;
    PyObject *list;
    py_import = __Pyx_GetAttrString(__pyx_b, "__import__");
    if (!py_import)
        goto bad;
    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;
    module = PyObject_CallFunctionObjArgs(py_import,
        name, global_dict, empty_dict, list, NULL);
bad:
    Py_XDECREF(empty_list);
    Py_XDECREF(py_import);
    Py_XDECREF(empty_dict);
    return module;
}

#if PY_MAJOR_VERSION < 3
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
    Py_XINCREF(type);
    Py_XINCREF(value);
    Py_XINCREF(tb);
    /* First, check the traceback argument, replacing None with NULL. */
    if (tb == Py_None) {
        Py_DECREF(tb);
        tb = 0;
    }
    else if (tb != NULL && !PyTraceBack_Check(tb)) {
        PyErr_SetString(PyExc_TypeError,
            "raise: arg 3 must be a traceback or None");
        goto raise_error;
    }
    /* Next, replace a missing value with None */
    if (value == NULL) {
        value = Py_None;
        Py_INCREF(value);
    }
    #if PY_VERSION_HEX < 0x02050000
    if (!PyClass_Check(type))
    #else
    if (!PyType_Check(type))
    #endif
    {
        /* Raising an instance.  The value should be a dummy. */
        if (value != Py_None) {
            PyErr_SetString(PyExc_TypeError,
                "instance exception may not have a separate value");
            goto raise_error;
        }
        /* Normalize to raise <class>, <instance> */
        Py_DECREF(value);
        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) {
    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)) {
        PyErr_SetString(PyExc_TypeError,
            "raise: exception class must be a subclass of BaseException");
        goto bad;
    }

    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:
    return;
}
#endif

#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;
    }
/*
    static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex z) {
#if HAVE_HYPOT
        return hypotf(z.real, z.imag);
#else
        return sqrtf(z.real*z.real + z.imag*z.imag);
#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;
    }
/*
    static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex z) {
#if HAVE_HYPOT
        return hypot(z.real, z.imag);
#else
        return sqrt(z.real*z.real + z.imag*z.imag);
#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);
}

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_VERSION_HEX < 0x03000000
    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 (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to unsigned long");
                return (unsigned long)-1;
            }
            return PyLong_AsUnsignedLong(x);
        } else {
            return 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;
    }
}

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_VERSION_HEX < 0x03000000
    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 (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 PyLong_AsUnsignedLongLong(x);
        } else {
            return 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;
    }
}

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_VERSION_HEX < 0x03000000
    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 (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to long");
                return (long)-1;
            }
            return PyLong_AsUnsignedLong(x);
        } else {
            return 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;
    }
}

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_VERSION_HEX < 0x03000000
    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 (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to PY_LONG_LONG");
                return (PY_LONG_LONG)-1;
            }
            return PyLong_AsUnsignedLongLong(x);
        } else {
            return 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;
    }
}

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_VERSION_HEX < 0x03000000
    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 (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to signed long");
                return (signed long)-1;
            }
            return PyLong_AsUnsignedLong(x);
        } else {
            return 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;
    }
}

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_VERSION_HEX < 0x03000000
    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 (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 PyLong_AsUnsignedLongLong(x);
        } else {
            return 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 void __Pyx_WriteUnraisable(const char *name) {
    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);
    }
}

#ifndef __PYX_HAVE_RT_ImportType
#define __PYX_HAVE_RT_ImportType
static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
    long size, int strict)
{
    PyObject *py_module = 0;
    PyObject *result = 0;
    PyObject *py_name = 0;
    char warning[200];

    py_module = __Pyx_ImportModule(module_name);
    if (!py_module)
        goto bad;
    #if PY_MAJOR_VERSION < 3
    py_name = PyString_FromString(class_name);
    #else
    py_name = PyUnicode_FromString(class_name);
    #endif
    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;
    }
    if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) {
        PyOS_snprintf(warning, sizeof(warning), 
            "%s.%s size changed, may indicate binary incompatibility",
            module_name, class_name);
        #if PY_VERSION_HEX < 0x02050000
        PyErr_Warn(NULL, warning);
        #else
        PyErr_WarnEx(NULL, warning, 0);
        #endif
    }
    else if (((PyTypeObject *)result)->tp_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 0;
}
#endif

#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;

    #if PY_MAJOR_VERSION < 3
    py_name = PyString_FromString(name);
    #else
    py_name = PyUnicode_FromString(name);
    #endif
    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

#include "compile.h"
#include "frameobject.h"
#include "traceback.h"

static void __Pyx_AddTraceback(const char *funcname) {
    PyObject *py_srcfile = 0;
    PyObject *py_funcname = 0;
    PyObject *py_globals = 0;
    PyCodeObject *py_code = 0;
    PyFrameObject *py_frame = 0;

    #if PY_MAJOR_VERSION < 3
    py_srcfile = PyString_FromString(__pyx_filename);
    #else
    py_srcfile = PyUnicode_FromString(__pyx_filename);
    #endif
    if (!py_srcfile) goto bad;
    if (__pyx_clineno) {
        #if PY_MAJOR_VERSION < 3
        py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno);
        #else
        py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, __pyx_clineno);
        #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_globals = PyModule_GetDict(__pyx_m);
    if (!py_globals) goto bad;
    py_code = PyCode_New(
        0,            /*int argcount,*/
        #if PY_MAJOR_VERSION >= 3
        0,            /*int kwonlyargcount,*/
        #endif
        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,*/
        __pyx_lineno,   /*int firstlineno,*/
        __pyx_empty_bytes  /*PyObject *lnotab*/
    );
    if (!py_code) 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 = __pyx_lineno;
    PyTraceBack_Here(py_frame);
bad:
    Py_XDECREF(py_srcfile);
    Py_XDECREF(py_funcname);
    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;
}

/* Type Conversion Functions */

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_VERSION_HEX < 0x03000000
  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_VERSION_HEX < 0x03000000
  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_VERSION_HEX < 0x03000000
    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)-1 && PyErr_Occurred())) {
       return (size_t)-1;
   } else if (unlikely(val != (unsigned PY_LONG_LONG)(size_t)val)) {
       PyErr_SetString(PyExc_OverflowError,
                       "value too large to convert to size_t");
       return (size_t)-1;
   }
   return (size_t)val;
}


#endif /* Py_PYTHON_H */
