// File: /workspace/github_c_makefile_star2_O1/zpole[P]IICV/vl/mser.c
// Function: vl_mser_ell_fit
VL_EXPORT
void
vl_mser_ell_fit (VlMserFilt* f)
{
  /* shortcuts */
  int                nel = f-> nel ;
  int                dof = f-> dof ;
  int              *dims = f-> dims ;
  int              ndims = f-> ndims ;
  int              *subs = f-> subs ;
  int             njoins = f-> njoins ;
  vl_uint         *joins = f-> joins ;
  VlMserReg           *r = f-> r ;
  vl_uint           *mer = f-> mer ;
  int               nmer = f-> nmer ;
  vl_mser_acc       *acc = f-> acc ;
  vl_mser_acc       *ell = f-> ell ;

  int d, index, i, j ;

  /* already fit ? */
  if (f->nell == f->nmer) return ;

  /* make room */
  if (f->rell < f->nmer) {
    if (f->ell) vl_free (f->ell) ;
    f->ell  = vl_malloc (sizeof(float) * f->nmer * f->dof) ;
    f->rell = f-> nmer ;
  }

  if (f->acc == 0) {
    f->acc = vl_malloc (sizeof(float) * f->nel) ;
  }

  acc = f-> acc ;
  ell = f-> ell ;

  /* -----------------------------------------------------------------
   *                                                 Integrate moments
   * -------------------------------------------------------------- */

  /* for each dof */
  for(d = 0 ; d < f->dof ; ++d) {

    /* start from the upper-left pixel (0,0,...,0) */
    memset (subs, 0, sizeof(int) * ndims) ;

    /* step 1: fill acc pretending that each region has only one pixel */
    if(d < ndims) {
      /* 1-order ................................................... */

      for(index = 0 ; index < nel ; ++ index) {
        acc [index] = subs [d] ;
        adv(ndims, dims, subs) ;
      }
    }
    else {
      /* 2-order ................................................... */

      /* map the dof d to a second order moment E[x_i x_j] */
      i = d - ndims ;
      j = 0 ;
      while(i > j) {
        i -= j + 1 ;
        j ++ ;
      }
      /* initialize acc with  x_i * x_j */
      for(index = 0 ; index < nel ; ++ index){
        acc [index] = subs [i] * subs [j] ;
        adv(ndims, dims, subs) ;
      }
    }

    /* step 2: integrate */
    for(i = 0 ; i < njoins ; ++i) {
      vl_uint index  = joins [i] ;
      vl_uint parent = r [index] .parent ;
      acc [parent] += acc [index] ;
    }

    /* step 3: save back to ellpises */
    for(i = 0 ; i < nmer ; ++i) {
      vl_uint idx = mer [i] ;
      ell [d + dof*i] = acc [idx] ;
    }

  }  /* next dof */

  /* -----------------------------------------------------------------
   *                                           Compute central moments
   * -------------------------------------------------------------- */

  for(index = 0 ; index < nmer ; ++index) {
    float  *pt  = ell + index * dof ;
    vl_uint    idx  = mer [index] ;
    float  area = r [idx] .area ;

    for(d = 0 ; d < dof ; ++d) {

      pt [d] /= area ;

      if(d >= ndims) {
        /* remove squared mean from moment to get variance */
        i = d - ndims ;
        j = 0 ;
        while(i > j) {
          i -= j + 1 ;
          j ++ ;
        }
        pt [d] -= pt [i] * pt [j] ;
      }

    }
  }

  /* save back */
  f-> nell = nmer ;
}

// Assembly:
movl 0x64(%rdi), %r9d
cmpl %r9d, 0x84(%rdi)
je 0x283b5
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r8
movl 0x10(%rdi), %eax
movq %rax, 0x30(%rsp)
movslq 0x88(%rdi), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rdi), %r15
movl (%rdi), %r13d
movq 0x18(%rdi), %r14
movl 0x40(%rdi), %eax
movq %rax, 0x58(%rsp)
movq 0x38(%rdi), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rdi), %r11
movq 0x58(%rdi), %rax
movq %rax, 0x20(%rsp)
cmpl %r9d, 0x80(%rdi)
movq %rdi, 0x8(%rsp)
movq %r9, 0x18(%rsp)
movq %r11, 0x10(%rsp)
jge 0x2810b
movq 0x78(%r8), %rdi
testq %rdi, %rdi
je 0x280d5
callq 0x9580
movq 0x8(%rsp), %r8
movslq 0x64(%r8), %rax
movslq 0x88(%r8), %rdi
imulq %rax, %rdi
shlq $0x2, %rdi
callq 0x95b0
movq 0x8(%rsp), %r8
movq %rax, 0x78(%r8)
movl 0x64(%r8), %eax
movl %eax, 0x80(%r8)
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r11
cmpq $0x0, 0x70(%r8)
jne 0x28132
movslq 0x10(%r8), %rdi
shlq $0x2, %rdi
callq 0x95b0
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, 0x70(%r8)
movslq %r13d, %rbx
movq 0x78(%r8), %rax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x88(%r8)
jle 0x28315
movq 0x70(%r8), %rbp
leaq (,%rbx,4), %rax
movq %rax, 0x50(%rsp)
movq 0x40(%rsp), %rax
leaq (,%rax,4), %rsi
xorl %edi, %edi
movq 0x38(%rsp), %r12
movq %rsi, 0x48(%rsp)
movq %r12, 0x60(%rsp)
movq %rdi, %r12
movq %r14, %rdi
xorl %esi, %esi
movq 0x50(%rsp), %rdx
callq 0x9400
movq %r12, %rdi
movq %r12, %rcx
subq %rbx, %rcx
jge 0x28202
cmpl $0x0, 0x30(%rsp)
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rsi
jle 0x282a1
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ssl (%r14,%rdi,4), %xmm0
movss %xmm0, (%rbp,%rax,4)
testl %ebx, %ebx
jle 0x281f3
xorl %ecx, %ecx
movl (%r14,%rcx,4), %edx
incl %edx
movl %edx, (%r14,%rcx,4)
cmpl (%r15,%rcx,4), %edx
jl 0x281f3
movl $0x0, (%r14,%rcx,4)
incq %rcx
cmpq %rcx, %r13
jne 0x281d3
incq %rax
cmpq 0x30(%rsp), %rax
jne 0x281be
jmp 0x282a1
xorl %eax, %eax
testq %rcx, %rcx
jle 0x2822d
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rsi
incq %rax
subl %eax, %ecx
cmpl %eax, %ecx
jg 0x28222
jmp 0x28246
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rsi
cmpl $0x0, 0x30(%rsp)
jle 0x282a1
movslq %ecx, %rcx
xorl %edx, %edx
movl (%r14,%rax,4), %esi
imull (%r14,%rcx,4), %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movss %xmm0, (%rbp,%rdx,4)
testl %r13d, %r13d
jle 0x2828f
xorl %esi, %esi
movl (%r14,%rsi,4), %edi
incl %edi
movl %edi, (%r14,%rsi,4)
cmpl (%r15,%rsi,4), %edi
jl 0x2828f
movl $0x0, (%r14,%rsi,4)
incq %rsi
cmpq %rsi, %r13
jne 0x2826f
incq %rdx
cmpq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rsi
movq %r12, %rdi
jne 0x28252
movq 0x58(%rsp), %rdx
testl %edx, %edx
jle 0x282d1
xorl %eax, %eax
movl (%rsi,%rax,4), %ecx
movss (%rbp,%rcx,4), %xmm0
shlq $0x4, %rcx
movl (%r11,%rcx), %ecx
addss (%rbp,%rcx,4), %xmm0
movss %xmm0, (%rbp,%rcx,4)
incq %rax
cmpq %rax, %rdx
jne 0x282ac
testl %r9d, %r9d
movq 0x48(%rsp), %rsi
movq 0x60(%rsp), %r12
jle 0x282fe
movq %r12, %rax
xorl %ecx, %ecx
movl (%r10,%rcx,4), %edx
movss (%rbp,%rdx,4), %xmm0
movss %xmm0, (%rax)
incq %rcx
addq %rsi, %rax
cmpq %rcx, %r9
jne 0x282e5
incq %rdi
movslq 0x88(%r8), %rax
addq $0x4, %r12
cmpq %rax, %rdi
jl 0x28176
testl %r9d, %r9d
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %r14
jle 0x283a0
xorl %eax, %eax
testl %r10d, %r10d
jle 0x28398
movq %rax, %rcx
imulq %r10, %rcx
movq 0x20(%rsp), %rdx
movl (%rdx,%rax,4), %edx
shlq $0x4, %rdx
leaq (%r14,%rcx,4), %rcx
movl 0xc(%r11,%rdx), %edx
xorps %xmm0, %xmm0
cvtsi2ss %rdx, %xmm0
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
cmpq %rbx, %rdx
jl 0x28390
movq %rdx, %rsi
subq %rbx, %rsi
xorl %edi, %edi
testq %rsi, %rsi
jle 0x2837a
incq %rdi
subl %edi, %esi
cmpl %edi, %esi
jg 0x28371
movslq %esi, %rsi
movss (%rcx,%rsi,4), %xmm2
mulss (%rcx,%rdi,4), %xmm2
subss %xmm2, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %r10, %rdx
jne 0x28351
incq %rax
cmpq %r9, %rax
jne 0x28326
movl %r9d, 0x84(%r8)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop