/*
 * MLS_getDeform.c
 *
 * Code generation for function 'MLS_getDeform'
 *
 */

/* Include files */
#include "rt_nonfinite.h"
#include "MLS_getDeform.h"
#include "MLS_getDeform_emxutil.h"
#include "error.h"
#include "eml_int_forloop_overflow_check.h"
#include "MLS_getDeform_mexutil.h"
#include "MLS_getDeform_data.h"

/* Variable Definitions */
static emlrtRTEInfo emlrtRTEI = { 27,  /* lineNo */
  5,                                   /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtRSInfo emlrtRSI = { 18,    /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo b_emlrtRSI = { 20,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo c_emlrtRSI = { 22,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo d_emlrtRSI = { 27,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo e_emlrtRSI = { 97,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo f_emlrtRSI = { 114, /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo g_emlrtRSI = { 115, /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo h_emlrtRSI = { 20,  /* lineNo */
  "cat",                               /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\eml\\+coder\\+internal\\cat.m"/* pathName */
};

static emlrtRSInfo i_emlrtRSI = { 100, /* lineNo */
  "cat",                               /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\eml\\+coder\\+internal\\cat.m"/* pathName */
};

static emlrtRSInfo j_emlrtRSI = { 9,   /* lineNo */
  "sum",                               /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\datafun\\sum.m"/* pathName */
};

static emlrtRSInfo k_emlrtRSI = { 58,  /* lineNo */
  "sumprod",                           /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\datafun\\private\\sumprod.m"/* pathName */
};

static emlrtRSInfo l_emlrtRSI = { 106, /* lineNo */
  "combineVectorElements",             /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\datafun\\private\\combineVectorElements.m"/* pathName */
};

static emlrtRSInfo m_emlrtRSI = { 166, /* lineNo */
  "combineVectorElements",             /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\datafun\\private\\combineVectorElements.m"/* pathName */
};

static emlrtRSInfo n_emlrtRSI = { 21,  /* lineNo */
  "eml_int_forloop_overflow_check",    /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\eml\\eml_int_forloop_overflow_check.m"/* pathName */
};

static emlrtRSInfo o_emlrtRSI = { 18,  /* lineNo */
  "repmat",                            /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\elmat\\repmat.m"/* pathName */
};

static emlrtRSInfo p_emlrtRSI = { 117, /* lineNo */
  "repmat",                            /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\elmat\\repmat.m"/* pathName */
};

static emlrtRSInfo q_emlrtRSI = { 71,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo r_emlrtRSI = { 43,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo s_emlrtRSI = { 51,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo t_emlrtRSI = { 53,  /* lineNo */
  "MLS_getDeform",                     /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pathName */
};

static emlrtRSInfo u_emlrtRSI = { 12,  /* lineNo */
  "sqrt",                              /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\elfun\\sqrt.m"/* pathName */
};

static emlrtRSInfo v_emlrtRSI = { 36,  /* lineNo */
  "fprintf",                           /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\iofun\\fprintf.m"/* pathName */
};

static emlrtMCInfo emlrtMCI = { 37,    /* lineNo */
  5,                                   /* colNo */
  "repmat",                            /* fName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\elmat\\repmat.m"/* pName */
};

static emlrtMCInfo b_emlrtMCI = { 79,  /* lineNo */
  18,                                  /* colNo */
  "fprintf",                           /* fName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\iofun\\fprintf.m"/* pName */
};

static emlrtRTEInfo b_emlrtRTEI = { 14,/* lineNo */
  1,                                   /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtRTEInfo c_emlrtRTEI = { 1, /* lineNo */
  20,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtRTEInfo d_emlrtRTEI = { 41,/* lineNo */
  5,                                   /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtRTEInfo e_emlrtRTEI = { 112,/* lineNo */
  28,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtRTEInfo f_emlrtRTEI = { 115,/* lineNo */
  17,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtBCInfo emlrtBCI = { -1,    /* iFirst */
  -1,                                  /* iLast */
  37,                                  /* lineNo */
  11,                                  /* colNo */
  "V",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo b_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  39,                                  /* lineNo */
  21,                                  /* colNo */
  "pre.w_i",                           /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo c_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  40,                                  /* lineNo */
  27,                                  /* colNo */
  "pre.sum_w_i",                       /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo d_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  42,                                  /* lineNo */
  23,                                  /* colNo */
  "pre.p_ast",                         /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo e_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  49,                                  /* lineNo */
  33,                                  /* colNo */
  "q_hat_i",                           /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo emlrtDCI = { 56,    /* lineNo */
  7,                                   /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo f_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  56,                                  /* lineNo */
  7,                                   /* colNo */
  "f",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo b_emlrtDCI = { 56,  /* lineNo */
  12,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo g_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  56,                                  /* lineNo */
  12,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo h_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  65,                                  /* lineNo */
  11,                                  /* colNo */
  "V",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo i_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  67,                                  /* lineNo */
  21,                                  /* colNo */
  "pre.w_i",                           /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo j_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  68,                                  /* lineNo */
  27,                                  /* colNo */
  "pre.sum_w_i",                       /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo k_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  76,                                  /* lineNo */
  33,                                  /* colNo */
  "q_hat_i",                           /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo c_emlrtDCI = { 82,  /* lineNo */
  7,                                   /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo l_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  82,                                  /* lineNo */
  7,                                   /* colNo */
  "f",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo d_emlrtDCI = { 82,  /* lineNo */
  12,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo m_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  82,                                  /* lineNo */
  12,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo n_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  91,                                  /* lineNo */
  11,                                  /* colNo */
  "V",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo o_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  93,                                  /* lineNo */
  21,                                  /* colNo */
  "pre.w_i",                           /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo p_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  94,                                  /* lineNo */
  27,                                  /* colNo */
  "pre.sum_w_i",                       /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo q_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  95,                                  /* lineNo */
  23,                                  /* colNo */
  "pre.Affine",                        /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo r_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  102,                                 /* lineNo */
  28,                                  /* colNo */
  "Aj",                                /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo s_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  102,                                 /* lineNo */
  39,                                  /* colNo */
  "q_hat_i",                           /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo e_emlrtDCI = { 107, /* lineNo */
  7,                                   /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo t_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  107,                                 /* lineNo */
  7,                                   /* colNo */
  "f",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo f_emlrtDCI = { 107, /* lineNo */
  12,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo u_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  107,                                 /* lineNo */
  12,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo g_emlrtDCI = { 33,  /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  4                                    /* checkKind */
};

static emlrtDCInfo h_emlrtDCI = { 33,  /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo v_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  41,                                  /* lineNo */
  21,                                  /* colNo */
  "pre.Rigid",                         /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo w_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  49,                                  /* lineNo */
  41,                                  /* colNo */
  "Ai",                                /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo i_emlrtDCI = { 61,  /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  4                                    /* checkKind */
};

static emlrtDCInfo j_emlrtDCI = { 61,  /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo x_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  69,                                  /* lineNo */
  29,                                  /* colNo */
  "pre.Similarity",                    /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtBCInfo y_emlrtBCI = { -1,  /* iFirst */
  -1,                                  /* iLast */
  76,                                  /* lineNo */
  44,                                  /* colNo */
  "Ai_mu",                             /* aName */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo k_emlrtDCI = { 88,  /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  4                                    /* checkKind */
};

static emlrtDCInfo l_emlrtDCI = { 88,  /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtECInfo emlrtECI = { 2,     /* nDims */
  114,                                 /* lineNo */
  13,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtECInfo b_emlrtECI = { 2,   /* nDims */
  115,                                 /* lineNo */
  11,                                  /* colNo */
  "MLS_getDeform",                     /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_getDeform.m"/* pName */
};

static emlrtRSInfo w_emlrtRSI = { 37,  /* lineNo */
  "repmat",                            /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\elmat\\repmat.m"/* pathName */
};

static emlrtRSInfo x_emlrtRSI = { 79,  /* lineNo */
  "fprintf",                           /* fcnName */
  "C:\\Program Files\\MATLAB\\R2017b\\toolbox\\eml\\lib\\matlab\\iofun\\fprintf.m"/* pathName */
};

/* Function Declarations */
static void MLS_q(const emlrtStack *sp, const emxArray_real_T *w_i, real_T
                  sum_w_i, const emxArray_real_T *q_i, real_T q_ast[2],
                  emxArray_real_T *q_hat_i);
static void b_error(const emlrtStack *sp, const mxArray *b, emlrtMCInfo
                    *location);
static const mxArray *feval(const emlrtStack *sp, const mxArray *b, const
  mxArray *c, const mxArray *d, emlrtMCInfo *location);

/* Function Definitions */
static void MLS_q(const emlrtStack *sp, const emxArray_real_T *w_i, real_T
                  sum_w_i, const emxArray_real_T *q_i, real_T q_ast[2],
                  emxArray_real_T *q_hat_i)
{
  emxArray_real_T *x;
  int32_T vlen;
  int32_T outsize_idx_0;
  int32_T b_x[2];
  int32_T b_q_i[2];
  boolean_T overflow;
  int32_T xpageoffset;
  int32_T k;
  const mxArray *y;
  const mxArray *m1;
  static const int32_T iv2[2] = { 1, 15 };

  static const char_T u[15] = { 'M', 'A', 'T', 'L', 'A', 'B', ':', 'p', 'm', 'a',
    'x', 's', 'i', 'z', 'e' };

  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  emlrtStack g_st;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  e_st.prev = &d_st;
  e_st.tls = d_st.tls;
  f_st.prev = &e_st;
  f_st.tls = e_st.tls;
  g_st.prev = &f_st;
  g_st.tls = f_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);
  emxInit_real_T1(sp, &x, 2, &e_emlrtRTEI, true);

  /*  parameters for deformation function, q = deformed positions of the control points p */
  /*  [w_i,w_i] to make matlab coder happy */
  st.site = &f_emlrtRSI;
  b_st.site = &h_emlrtRSI;
  c_st.site = &i_emlrtRSI;
  vlen = x->size[0] * x->size[1];
  x->size[0] = w_i->size[0];
  x->size[1] = 2;
  emxEnsureCapacity_real_T(&b_st, x, vlen, &e_emlrtRTEI);
  outsize_idx_0 = w_i->size[0];
  for (vlen = 0; vlen < outsize_idx_0; vlen++) {
    x->data[vlen] = w_i->data[vlen];
  }

  outsize_idx_0 = w_i->size[0];
  for (vlen = 0; vlen < outsize_idx_0; vlen++) {
    x->data[vlen + x->size[0]] = w_i->data[vlen];
  }

  for (vlen = 0; vlen < 2; vlen++) {
    b_x[vlen] = x->size[vlen];
  }

  for (vlen = 0; vlen < 2; vlen++) {
    b_q_i[vlen] = q_i->size[vlen];
  }

  if ((b_x[0] != b_q_i[0]) || (b_x[1] != b_q_i[1])) {
    emlrtSizeEqCheckNDR2012b(&b_x[0], &b_q_i[0], &emlrtECI, sp);
  }

  st.site = &f_emlrtRSI;
  vlen = x->size[0] * x->size[1];
  x->size[1] = 2;
  emxEnsureCapacity_real_T(&st, x, vlen, &e_emlrtRTEI);
  outsize_idx_0 = x->size[0];
  vlen = x->size[1];
  outsize_idx_0 *= vlen;
  for (vlen = 0; vlen < outsize_idx_0; vlen++) {
    x->data[vlen] *= q_i->data[vlen];
  }

  b_st.site = &j_emlrtRSI;
  c_st.site = &k_emlrtRSI;
  vlen = x->size[0];
  if (x->size[0] == 0) {
    for (vlen = 0; vlen < 2; vlen++) {
      q_ast[vlen] = 0.0;
    }
  } else {
    d_st.site = &l_emlrtRSI;
    overflow = ((!(2 > vlen)) && (vlen > 2147483646));
    for (outsize_idx_0 = 0; outsize_idx_0 < 2; outsize_idx_0++) {
      xpageoffset = outsize_idx_0 * x->size[0];
      q_ast[outsize_idx_0] = x->data[xpageoffset];
      e_st.site = &m_emlrtRSI;
      if (overflow) {
        f_st.site = &n_emlrtRSI;
        g_st.site = &n_emlrtRSI;
        check_forloop_overflow_error(&g_st);
      }

      for (k = 2; k <= vlen; k++) {
        q_ast[outsize_idx_0] += x->data[(xpageoffset + k) - 1];
      }
    }
  }

  emxFree_real_T(&x);
  for (vlen = 0; vlen < 2; vlen++) {
    q_ast[vlen] /= sum_w_i;
  }

  st.site = &g_emlrtRSI;
  b_st.site = &o_emlrtRSI;
  outsize_idx_0 = q_i->size[0];
  if (outsize_idx_0 != q_i->size[0]) {
    y = NULL;
    m1 = emlrtCreateCharArray(2, iv2);
    emlrtInitCharArrayR2013a(&st, 15, m1, &u[0]);
    emlrtAssign(&y, m1);
    b_st.site = &w_emlrtRSI;
    b_error(&b_st, y, &emlrtMCI);
  }

  vlen = q_hat_i->size[0] * q_hat_i->size[1];
  q_hat_i->size[0] = outsize_idx_0;
  q_hat_i->size[1] = 2;
  emxEnsureCapacity_real_T(&st, q_hat_i, vlen, &f_emlrtRTEI);
  if (!(outsize_idx_0 == 0)) {
    outsize_idx_0 = q_i->size[0];
    overflow = ((!(1 > outsize_idx_0)) && (outsize_idx_0 > 2147483646));
    for (vlen = 0; vlen < 2; vlen++) {
      xpageoffset = vlen * outsize_idx_0;
      b_st.site = &p_emlrtRSI;
      if (overflow) {
        c_st.site = &n_emlrtRSI;
        d_st.site = &n_emlrtRSI;
        check_forloop_overflow_error(&d_st);
      }

      for (k = 1; k <= outsize_idx_0; k++) {
        q_hat_i->data[(xpageoffset + k) - 1] = q_ast[vlen];
      }
    }
  }

  for (vlen = 0; vlen < 2; vlen++) {
    b_q_i[vlen] = q_i->size[vlen];
  }

  for (vlen = 0; vlen < 2; vlen++) {
    b_x[vlen] = q_hat_i->size[vlen];
  }

  if ((b_q_i[0] != b_x[0]) || (b_q_i[1] != b_x[1])) {
    emlrtSizeEqCheckNDR2012b(&b_q_i[0], &b_x[0], &b_emlrtECI, sp);
  }

  vlen = q_hat_i->size[0] * q_hat_i->size[1];
  q_hat_i->size[0] = q_i->size[0];
  q_hat_i->size[1] = 2;
  emxEnsureCapacity_real_T(sp, q_hat_i, vlen, &e_emlrtRTEI);
  outsize_idx_0 = q_i->size[0] * q_i->size[1];
  for (vlen = 0; vlen < outsize_idx_0; vlen++) {
    q_hat_i->data[vlen] = q_i->data[vlen] - q_hat_i->data[vlen];
  }

  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}

static void b_error(const emlrtStack *sp, const mxArray *b, emlrtMCInfo
                    *location)
{
  const mxArray *pArray;
  pArray = b;
  emlrtCallMATLABR2012b(sp, 0, NULL, 1, &pArray, "error", true, location);
}

static const mxArray *feval(const emlrtStack *sp, const mxArray *b, const
  mxArray *c, const mxArray *d, emlrtMCInfo *location)
{
  const mxArray *pArrays[3];
  const mxArray *m5;
  pArrays[0] = b;
  pArrays[1] = c;
  pArrays[2] = d;
  return emlrtCallMATLABR2012b(sp, 1, &m5, 3, pArrays, "feval", true, location);
}

void MLS_getDeform(const emlrtStack *sp, const emxArray_real_T *control_points,
                   const emxArray_real_T *c_deformed_position_of_control_, const
                   emxArray_real_T *points_of_evaluation, real_T alpha, const
                   emxArray_char_T *type, const struct0_T *precomputed, const
                   real_T size_of_img[2], emxArray_real_T *f, struct0_T *pre)
{
  int32_T i0;
  boolean_T b_bool;
  int32_T kstr;
  emxArray_real_T *b_f;
  emxArray_real_T *q_hat_i;
  int32_T exitg1;
  emxArray_real_T *b_precomputed;
  static const char_T cv0[6] = { 'A', 'f', 'f', 'i', 'n', 'e' };

  emxArray_real_T *Ai;
  real_T dv0[3];
  static const char_T cv1[10] = { 'S', 'i', 'm', 'i', 'l', 'a', 'r', 'i', 't',
    'y' };

  static const char_T cv2[5] = { 'R', 'i', 'g', 'i', 'd' };

  int32_T i;
  const mxArray *y;
  const mxArray *m0;
  static const int32_T iv0[2] = { 1, 7 };

  static const char_T u[7] = { 'f', 'p', 'r', 'i', 'n', 't', 'f' };

  int32_T b_points_of_evaluation;
  const mxArray *b_y;
  const mxArray *c_y;
  static const int32_T iv1[2] = { 1, 19 };

  static const char_T formatSpec[19] = { 'P', 'a', 's', 's', 'e', 'd', 'W', 'r',
    'o', 'n', 'g', 'V', 'a', 'r', 'i', 'a', 'b', 'l', 'e' };

  real_T nbytes;
  int32_T c_precomputed;
  real_T q_ast[2];
  real_T f_vec[2];
  int32_T i1;
  real_T x;
  real_T z1[2];
  real_T b[4];
  real_T len_f_vec;
  real_T a[2];
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  (void)control_points;
  (void)alpha;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);

  /* UNTITLED2 Summary of this function goes here */
  /*    type: */
  /*         Affine = translation, rotation, nonuniform scaling and shear */
  /*         Similarity = translation, rotation and uniform scaling */
  /*         Rigid = translation, rotation */
  /*    alpha: for weigth function (precompute) */
  /*    p_i,q_i,v_i are row vectros eg. [x,y] so p,q,v = [x1,y1;x2,y2;x3,y3;...] */
  /*    size_of_img = [height,width] */
  emxCopyStruct_struct0_T(sp, pre, precomputed, &b_emlrtRTEI);
  i0 = f->size[0] * f->size[1] * f->size[2];
  f->size[0] = 0;
  f->size[1] = 0;
  f->size[2] = 1;
  emxEnsureCapacity_real_T2(sp, f, i0, &c_emlrtRTEI);

  /*  switch the MLS deformation type */
  b_bool = false;
  if (type->size[1] == 6) {
    kstr = 0;
    do {
      exitg1 = 0;
      if (kstr + 1 < 7) {
        if (type->data[kstr] != cv0[kstr]) {
          exitg1 = 1;
        } else {
          kstr++;
        }
      } else {
        b_bool = true;
        exitg1 = 1;
      }
    } while (exitg1 == 0);
  }

  emxInit_real_T(sp, &b_f, 3, &c_emlrtRTEI, true);
  emxInit_real_T1(sp, &q_hat_i, 2, &c_emlrtRTEI, true);
  emxInit_real_T2(sp, &b_precomputed, 1, &c_emlrtRTEI, true);
  if (b_bool) {
    st.site = &emlrtRSI;

    /*  %% affine deformation */
    for (i0 = 0; i0 < 2; i0++) {
      if (!(size_of_img[i0] >= 0.0)) {
        emlrtNonNegativeCheckR2012b(size_of_img[i0], &k_emlrtDCI, &st);
      }

      if (size_of_img[i0] != (int32_T)muDoubleScalarFloor(size_of_img[i0])) {
        emlrtIntegerCheckR2012b(size_of_img[i0], &l_emlrtDCI, &st);
      }

      dv0[i0] = size_of_img[i0];
    }

    i0 = b_f->size[0] * b_f->size[1] * b_f->size[2];
    b_f->size[0] = (int32_T)dv0[0];
    b_f->size[1] = (int32_T)dv0[1];
    b_f->size[2] = 2;
    emxEnsureCapacity_real_T2(&st, b_f, i0, &c_emlrtRTEI);
    kstr = ((int32_T)dv0[0] * (int32_T)dv0[1]) << 1;
    for (i0 = 0; i0 < kstr; i0++) {
      b_f->data[i0] = 0.0;
    }

    /*  loop through evaluation points v */
    i = 0;
    while (i <= points_of_evaluation->size[0] - 1) {
      i0 = points_of_evaluation->size[0];
      b_points_of_evaluation = i + 1;
      if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0))) {
        emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0, &n_emlrtBCI,
          &st);
      }

      /*  get param for given evaluation point v */
      i0 = precomputed->w_i->size[1];
      b_points_of_evaluation = i + 1;
      if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0))) {
        emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0, &o_emlrtBCI,
          &st);
      }

      i0 = precomputed->sum_w_i->size[0];
      b_points_of_evaluation = i + 1;
      if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0))) {
        emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0, &p_emlrtBCI,
          &st);
      }

      i0 = precomputed->Affine->size[1];
      b_points_of_evaluation = i + 1;
      if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0))) {
        emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0, &q_emlrtBCI,
          &st);
      }

      kstr = precomputed->w_i->size[0];
      i0 = b_precomputed->size[0];
      b_precomputed->size[0] = kstr;
      emxEnsureCapacity_real_T1(&st, b_precomputed, i0, &c_emlrtRTEI);
      for (i0 = 0; i0 < kstr; i0++) {
        b_precomputed->data[i0] = precomputed->w_i->data[i0 + precomputed->
          w_i->size[0] * i];
      }

      b_st.site = &e_emlrtRSI;
      MLS_q(&b_st, b_precomputed, precomputed->sum_w_i->data[i],
            c_deformed_position_of_control_, q_ast, q_hat_i);

      /*  loop through control points */
      for (i0 = 0; i0 < 2; i0++) {
        f_vec[i0] = 0.0;
      }

      kstr = 0;
      while (kstr <= c_deformed_position_of_control_->size[0] - 1) {
        i0 = precomputed->Affine->size[0];
        b_points_of_evaluation = 1 + kstr;
        if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
        {
          emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
            &r_emlrtBCI, &st);
        }

        nbytes = precomputed->Affine->data[kstr + precomputed->Affine->size[0] *
          i];
        i0 = q_hat_i->size[0];
        b_points_of_evaluation = kstr + 1;
        if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
        {
          emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
            &s_emlrtBCI, &st);
        }

        for (i0 = 0; i0 < 2; i0++) {
          x = f_vec[i0] + nbytes * q_hat_i->data[kstr + q_hat_i->size[0] * i0];
          f_vec[i0] = x;
        }

        kstr++;
        if (*emlrtBreakCheckR2012bFlagVar != 0) {
          emlrtBreakCheckR2012b(&st);
        }
      }

      for (i0 = 0; i0 < 2; i0++) {
        f_vec[i0] += q_ast[i0];
      }

      /*  save deformation for given control point */
      /* if isequal(v,p) */
      c_precomputed = b_f->size[0];
      kstr = b_f->size[1];
      nbytes = points_of_evaluation->data[i + points_of_evaluation->size[0]];
      if (nbytes != (int32_T)muDoubleScalarFloor(nbytes)) {
        emlrtIntegerCheckR2012b(nbytes, &f_emlrtDCI, &st);
      }

      b_points_of_evaluation = (int32_T)nbytes;
      if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= kstr)))
      {
        emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, kstr,
          &u_emlrtBCI, &st);
      }

      nbytes = points_of_evaluation->data[i];
      if (nbytes != (int32_T)muDoubleScalarFloor(nbytes)) {
        emlrtIntegerCheckR2012b(nbytes, &e_emlrtDCI, &st);
      }

      kstr = (int32_T)nbytes;
      if (!((kstr >= 1) && (kstr <= c_precomputed))) {
        emlrtDynamicBoundsCheckR2012b(kstr, 1, c_precomputed, &t_emlrtBCI, &st);
      }

      for (i0 = 0; i0 < 2; i0++) {
        b_f->data[((kstr + b_f->size[0] * (b_points_of_evaluation - 1)) +
                   b_f->size[0] * b_f->size[1] * i0) - 1] = f_vec[i0];
      }

      i++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(&st);
      }
    }

    i0 = f->size[0] * f->size[1] * f->size[2];
    f->size[0] = b_f->size[0];
    f->size[1] = b_f->size[1];
    f->size[2] = 2;
    emxEnsureCapacity_real_T2(sp, f, i0, &c_emlrtRTEI);
    kstr = b_f->size[0] * b_f->size[1] * b_f->size[2];
    for (i0 = 0; i0 < kstr; i0++) {
      f->data[i0] = b_f->data[i0];
    }
  } else {
    b_bool = false;
    if (type->size[1] == 10) {
      kstr = 0;
      do {
        exitg1 = 0;
        if (kstr + 1 < 11) {
          if (type->data[kstr] != cv1[kstr]) {
            exitg1 = 1;
          } else {
            kstr++;
          }
        } else {
          b_bool = true;
          exitg1 = 1;
        }
      } while (exitg1 == 0);
    }

    emxInit_real_T1(sp, &Ai, 2, &d_emlrtRTEI, true);
    if (b_bool) {
      st.site = &b_emlrtRSI;

      /*  similatrity defromation */
      for (i0 = 0; i0 < 2; i0++) {
        if (!(size_of_img[i0] >= 0.0)) {
          emlrtNonNegativeCheckR2012b(size_of_img[i0], &i_emlrtDCI, &st);
        }

        if (size_of_img[i0] != (int32_T)muDoubleScalarFloor(size_of_img[i0])) {
          emlrtIntegerCheckR2012b(size_of_img[i0], &j_emlrtDCI, &st);
        }

        dv0[i0] = size_of_img[i0];
      }

      i0 = b_f->size[0] * b_f->size[1] * b_f->size[2];
      b_f->size[0] = (int32_T)dv0[0];
      b_f->size[1] = (int32_T)dv0[1];
      b_f->size[2] = 2;
      emxEnsureCapacity_real_T2(&st, b_f, i0, &c_emlrtRTEI);
      kstr = ((int32_T)dv0[0] * (int32_T)dv0[1]) << 1;
      for (i0 = 0; i0 < kstr; i0++) {
        b_f->data[i0] = 0.0;
      }

      /*  loop through evaluation points v */
      i = 0;
      while (i <= points_of_evaluation->size[0] - 1) {
        i0 = points_of_evaluation->size[0];
        b_points_of_evaluation = i + 1;
        if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
        {
          emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
            &h_emlrtBCI, &st);
        }

        /*  get param for given evaluation point v */
        i0 = precomputed->w_i->size[1];
        b_points_of_evaluation = i + 1;
        if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
        {
          emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
            &i_emlrtBCI, &st);
        }

        i0 = precomputed->sum_w_i->size[0];
        b_points_of_evaluation = i + 1;
        if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
        {
          emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
            &j_emlrtBCI, &st);
        }

        kstr = precomputed->Similarity->size[0];
        nbytes = 2.0 * (1.0 + (real_T)i);
        c_precomputed = precomputed->Similarity->size[1];
        i0 = Ai->size[0] * Ai->size[1];
        Ai->size[0] = kstr;
        Ai->size[1] = 2;
        emxEnsureCapacity_real_T(&st, Ai, i0, &c_emlrtRTEI);
        for (i0 = 0; i0 < 2; i0++) {
          for (b_points_of_evaluation = 0; b_points_of_evaluation < kstr;
               b_points_of_evaluation++) {
            i1 = (int32_T)(nbytes + (-1.0 + (real_T)i0));
            if (!((i1 >= 1) && (i1 <= c_precomputed))) {
              emlrtDynamicBoundsCheckR2012b(i1, 1, c_precomputed, &x_emlrtBCI,
                &st);
            }

            Ai->data[b_points_of_evaluation + Ai->size[0] * i0] =
              precomputed->Similarity->data[b_points_of_evaluation +
              precomputed->Similarity->size[0] * (i1 - 1)];
          }
        }

        kstr = precomputed->w_i->size[0];
        i0 = b_precomputed->size[0];
        b_precomputed->size[0] = kstr;
        emxEnsureCapacity_real_T1(&st, b_precomputed, i0, &c_emlrtRTEI);
        for (i0 = 0; i0 < kstr; i0++) {
          b_precomputed->data[i0] = precomputed->w_i->data[i0 + precomputed->
            w_i->size[0] * i];
        }

        b_st.site = &q_emlrtRSI;
        MLS_q(&b_st, b_precomputed, precomputed->sum_w_i->data[i],
              c_deformed_position_of_control_, q_ast, q_hat_i);

        /*  loop through control points */
        for (i0 = 0; i0 < 2; i0++) {
          f_vec[i0] = 0.0;
        }

        kstr = 0;
        while (kstr <= c_deformed_position_of_control_->size[0] - 1) {
          i0 = q_hat_i->size[0];
          b_points_of_evaluation = kstr + 1;
          if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
          {
            emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
              &k_emlrtBCI, &st);
          }

          i0 = precomputed->Similarity->size[0];
          nbytes = 2.0 * (1.0 + (real_T)kstr);
          for (b_points_of_evaluation = 0; b_points_of_evaluation < 2;
               b_points_of_evaluation++) {
            for (i1 = 0; i1 < 2; i1++) {
              c_precomputed = (int32_T)(nbytes + (-1.0 + (real_T)i1));
              if (!((c_precomputed >= 1) && (c_precomputed <= i0))) {
                emlrtDynamicBoundsCheckR2012b(c_precomputed, 1, i0, &y_emlrtBCI,
                  &st);
              }

              b[i1 + (b_points_of_evaluation << 1)] = Ai->data[(c_precomputed +
                Ai->size[0] * b_points_of_evaluation) - 1];
            }
          }

          for (i0 = 0; i0 < 2; i0++) {
            nbytes = 0.0;
            for (b_points_of_evaluation = 0; b_points_of_evaluation < 2;
                 b_points_of_evaluation++) {
              nbytes += q_hat_i->data[kstr + q_hat_i->size[0] *
                b_points_of_evaluation] * b[b_points_of_evaluation + (i0 << 1)];
            }

            f_vec[i0] += nbytes;
          }

          kstr++;
          if (*emlrtBreakCheckR2012bFlagVar != 0) {
            emlrtBreakCheckR2012b(&st);
          }
        }

        for (i0 = 0; i0 < 2; i0++) {
          f_vec[i0] += q_ast[i0];
        }

        /*  save deformation for given control point */
        c_precomputed = b_f->size[0];
        kstr = b_f->size[1];
        nbytes = points_of_evaluation->data[i + points_of_evaluation->size[0]];
        if (nbytes != (int32_T)muDoubleScalarFloor(nbytes)) {
          emlrtIntegerCheckR2012b(nbytes, &d_emlrtDCI, &st);
        }

        b_points_of_evaluation = (int32_T)nbytes;
        if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= kstr)))
        {
          emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, kstr,
            &m_emlrtBCI, &st);
        }

        nbytes = points_of_evaluation->data[i];
        if (nbytes != (int32_T)muDoubleScalarFloor(nbytes)) {
          emlrtIntegerCheckR2012b(nbytes, &c_emlrtDCI, &st);
        }

        kstr = (int32_T)nbytes;
        if (!((kstr >= 1) && (kstr <= c_precomputed))) {
          emlrtDynamicBoundsCheckR2012b(kstr, 1, c_precomputed, &l_emlrtBCI, &st);
        }

        for (i0 = 0; i0 < 2; i0++) {
          b_f->data[((kstr + b_f->size[0] * (b_points_of_evaluation - 1)) +
                     b_f->size[0] * b_f->size[1] * i0) - 1] = f_vec[i0];
        }

        i++;
        if (*emlrtBreakCheckR2012bFlagVar != 0) {
          emlrtBreakCheckR2012b(&st);
        }
      }

      i0 = f->size[0] * f->size[1] * f->size[2];
      f->size[0] = b_f->size[0];
      f->size[1] = b_f->size[1];
      f->size[2] = 2;
      emxEnsureCapacity_real_T2(sp, f, i0, &c_emlrtRTEI);
      kstr = b_f->size[0] * b_f->size[1] * b_f->size[2];
      for (i0 = 0; i0 < kstr; i0++) {
        f->data[i0] = b_f->data[i0];
      }
    } else {
      b_bool = false;
      if (type->size[1] == 5) {
        kstr = 0;
        do {
          exitg1 = 0;
          if (kstr + 1 < 6) {
            if (type->data[kstr] != cv2[kstr]) {
              exitg1 = 1;
            } else {
              kstr++;
            }
          } else {
            b_bool = true;
            exitg1 = 1;
          }
        } while (exitg1 == 0);
      }

      if (b_bool) {
        st.site = &c_emlrtRSI;

        /* MLS deformation */
        /*  rigid deformation */
        for (i0 = 0; i0 < 2; i0++) {
          if (!(size_of_img[i0] >= 0.0)) {
            emlrtNonNegativeCheckR2012b(size_of_img[i0], &g_emlrtDCI, &st);
          }

          if (size_of_img[i0] != (int32_T)muDoubleScalarFloor(size_of_img[i0]))
          {
            emlrtIntegerCheckR2012b(size_of_img[i0], &h_emlrtDCI, &st);
          }

          dv0[i0] = size_of_img[i0];
        }

        i0 = b_f->size[0] * b_f->size[1] * b_f->size[2];
        b_f->size[0] = (int32_T)dv0[0];
        b_f->size[1] = (int32_T)dv0[1];
        b_f->size[2] = 2;
        emxEnsureCapacity_real_T2(&st, b_f, i0, &c_emlrtRTEI);
        kstr = ((int32_T)dv0[0] * (int32_T)dv0[1]) << 1;
        for (i0 = 0; i0 < kstr; i0++) {
          b_f->data[i0] = 0.0;
        }

        /*  loop through evaluation points v */
        i = 0;
        while (i <= points_of_evaluation->size[0] - 1) {
          i0 = points_of_evaluation->size[0];
          b_points_of_evaluation = i + 1;
          if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
          {
            emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
              &emlrtBCI, &st);
          }

          /*  get param for given evaluation point v */
          i0 = precomputed->w_i->size[1];
          b_points_of_evaluation = i + 1;
          if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
          {
            emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
              &b_emlrtBCI, &st);
          }

          i0 = precomputed->sum_w_i->size[0];
          b_points_of_evaluation = i + 1;
          if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
          {
            emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
              &c_emlrtBCI, &st);
          }

          kstr = precomputed->Rigid->size[0];
          nbytes = 2.0 * (1.0 + (real_T)i);
          c_precomputed = precomputed->Rigid->size[1];
          i0 = Ai->size[0] * Ai->size[1];
          Ai->size[0] = kstr;
          Ai->size[1] = 2;
          emxEnsureCapacity_real_T(&st, Ai, i0, &c_emlrtRTEI);
          for (i0 = 0; i0 < 2; i0++) {
            for (b_points_of_evaluation = 0; b_points_of_evaluation < kstr;
                 b_points_of_evaluation++) {
              i1 = (int32_T)(nbytes + (-1.0 + (real_T)i0));
              if (!((i1 >= 1) && (i1 <= c_precomputed))) {
                emlrtDynamicBoundsCheckR2012b(i1, 1, c_precomputed, &v_emlrtBCI,
                  &st);
              }

              Ai->data[b_points_of_evaluation + Ai->size[0] * i0] =
                precomputed->Rigid->data[b_points_of_evaluation +
                precomputed->Rigid->size[0] * (i1 - 1)];
            }
          }

          i0 = precomputed->p_ast->size[0];
          b_points_of_evaluation = i + 1;
          if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
          {
            emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
              &d_emlrtBCI, &st);
          }

          kstr = precomputed->w_i->size[0];
          i0 = b_precomputed->size[0];
          b_precomputed->size[0] = kstr;
          emxEnsureCapacity_real_T1(&st, b_precomputed, i0, &c_emlrtRTEI);
          for (i0 = 0; i0 < kstr; i0++) {
            b_precomputed->data[i0] = precomputed->w_i->data[i0 +
              precomputed->w_i->size[0] * i];
          }

          b_st.site = &r_emlrtRSI;
          MLS_q(&b_st, b_precomputed, precomputed->sum_w_i->data[i],
                c_deformed_position_of_control_, q_ast, q_hat_i);

          /*  loop through control points */
          for (i0 = 0; i0 < 2; i0++) {
            f_vec[i0] = 0.0;
          }

          kstr = 0;
          while (kstr <= c_deformed_position_of_control_->size[0] - 1) {
            i0 = q_hat_i->size[0];
            b_points_of_evaluation = kstr + 1;
            if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= i0)))
            {
              emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, i0,
                &e_emlrtBCI, &st);
            }

            i0 = precomputed->Rigid->size[0];
            nbytes = 2.0 * (1.0 + (real_T)kstr);
            for (b_points_of_evaluation = 0; b_points_of_evaluation < 2;
                 b_points_of_evaluation++) {
              for (i1 = 0; i1 < 2; i1++) {
                c_precomputed = (int32_T)(nbytes + (-1.0 + (real_T)i1));
                if (!((c_precomputed >= 1) && (c_precomputed <= i0))) {
                  emlrtDynamicBoundsCheckR2012b(c_precomputed, 1, i0,
                    &w_emlrtBCI, &st);
                }

                b[i1 + (b_points_of_evaluation << 1)] = Ai->data[(c_precomputed
                  + Ai->size[0] * b_points_of_evaluation) - 1];
              }
            }

            for (i0 = 0; i0 < 2; i0++) {
              nbytes = 0.0;
              for (b_points_of_evaluation = 0; b_points_of_evaluation < 2;
                   b_points_of_evaluation++) {
                nbytes += q_hat_i->data[kstr + q_hat_i->size[0] *
                  b_points_of_evaluation] * b[b_points_of_evaluation + (i0 << 1)];
              }

              f_vec[i0] += nbytes;
            }

            kstr++;
            if (*emlrtBreakCheckR2012bFlagVar != 0) {
              emlrtBreakCheckR2012b(&st);
            }
          }

          for (kstr = 0; kstr < 2; kstr++) {
            z1[kstr] = f_vec[kstr] * f_vec[kstr];
          }

          x = z1[0] + z1[1];
          b_st.site = &s_emlrtRSI;
          if (x < 0.0) {
            c_st.site = &u_emlrtRSI;
            d_st.site = &u_emlrtRSI;
            error(&d_st);
          }

          len_f_vec = muDoubleScalarSqrt(x);
          for (i0 = 0; i0 < 2; i0++) {
            a[i0] = points_of_evaluation->data[i + points_of_evaluation->size[0]
              * i0] - precomputed->p_ast->data[i + precomputed->p_ast->size[0] *
              i0];
          }

          for (kstr = 0; kstr < 2; kstr++) {
            z1[kstr] = a[kstr] * a[kstr];
          }

          x = z1[0] + z1[1];
          b_st.site = &t_emlrtRSI;
          if (x < 0.0) {
            c_st.site = &u_emlrtRSI;
            d_st.site = &u_emlrtRSI;
            error(&d_st);
          }

          x = muDoubleScalarSqrt(x);

          /*  save deformation for given control point */
          c_precomputed = b_f->size[0];
          kstr = b_f->size[1];
          nbytes = points_of_evaluation->data[i + points_of_evaluation->size[0]];
          if (nbytes != (int32_T)muDoubleScalarFloor(nbytes)) {
            emlrtIntegerCheckR2012b(nbytes, &b_emlrtDCI, &st);
          }

          b_points_of_evaluation = (int32_T)nbytes;
          if (!((b_points_of_evaluation >= 1) && (b_points_of_evaluation <= kstr)))
          {
            emlrtDynamicBoundsCheckR2012b(b_points_of_evaluation, 1, kstr,
              &g_emlrtBCI, &st);
          }

          nbytes = points_of_evaluation->data[i];
          if (nbytes != (int32_T)muDoubleScalarFloor(nbytes)) {
            emlrtIntegerCheckR2012b(nbytes, &emlrtDCI, &st);
          }

          kstr = (int32_T)nbytes;
          if (!((kstr >= 1) && (kstr <= c_precomputed))) {
            emlrtDynamicBoundsCheckR2012b(kstr, 1, c_precomputed, &f_emlrtBCI,
              &st);
          }

          for (i0 = 0; i0 < 2; i0++) {
            b_f->data[((kstr + b_f->size[0] * (b_points_of_evaluation - 1)) +
                       b_f->size[0] * b_f->size[1] * i0) - 1] = x * f_vec[i0] /
              len_f_vec + q_ast[i0];
          }

          i++;
          if (*emlrtBreakCheckR2012bFlagVar != 0) {
            emlrtBreakCheckR2012b(&st);
          }
        }

        i0 = f->size[0] * f->size[1] * f->size[2];
        f->size[0] = b_f->size[0];
        f->size[1] = b_f->size[1];
        f->size[2] = 2;
        emxEnsureCapacity_real_T2(sp, f, i0, &c_emlrtRTEI);
        kstr = b_f->size[0] * b_f->size[1] * b_f->size[2];
        for (i0 = 0; i0 < kstr; i0++) {
          f->data[i0] = b_f->data[i0];
        }
      } else {
        /* ME = MException('type:PassedWrongVariable', ... */
        /*     'Variable %s should be one of following: Affine, Similarity, Rigid',type); */
        /* throw(ME) */
        st.site = &d_emlrtRSI;
        b_st.site = &v_emlrtRSI;
        y = NULL;
        m0 = emlrtCreateCharArray(2, iv0);
        emlrtInitCharArrayR2013a(&b_st, 7, m0, &u[0]);
        emlrtAssign(&y, m0);
        b_y = NULL;
        m0 = emlrtCreateDoubleScalar(1.0);
        emlrtAssign(&b_y, m0);
        c_y = NULL;
        m0 = emlrtCreateCharArray(2, iv1);
        emlrtInitCharArrayR2013a(&b_st, 19, m0, &formatSpec[0]);
        emlrtAssign(&c_y, m0);
        c_st.site = &x_emlrtRSI;
        nbytes = emlrt_marshallIn(&c_st, feval(&c_st, y, b_y, c_y, &b_emlrtMCI),
          "feval");
        y = NULL;
        m0 = emlrtCreateDoubleScalar(nbytes);
        emlrtAssign(&y, m0);
        emlrtDisplayR2012b(y, "ans", &emlrtRTEI, sp);
      }
    }

    emxFree_real_T(&Ai);
  }

  emxFree_real_T(&b_precomputed);
  emxFree_real_T(&q_hat_i);
  emxFree_real_T(&b_f);
  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}

/* End of code generation (MLS_getDeform.c) */
