// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelPointGraph.cpp
// Function: gaps::R3SurfelPointGraph::RemoveOutlierEdges(double)
void R3SurfelPointGraph::
RemoveOutlierEdges(RNScalar max_zscore)
{
  // Allocate temporary memory for edge length statistics
  RNScalar *means = new RNScalar [ NPoints() ];
  RNScalar *stddevs = new RNScalar [ NPoints() ];

  // Compute edge length statistics
  for (int i = 0; i < NPoints(); i++) {
    // Compute sum
    RNLength sum = 0;
    R3SurfelPoint *point0 = Point(i);
    R3Point position0 = point0->Position();
    for (int j = 0; j < NNeighbors(i); j++) {
      R3SurfelPoint *point1 = Neighbor(i,j);
      R3Point position1 = point1->Position();
      RNLength edge_length = R3Distance(position0, position1);
      sum += edge_length;
    }
    
    // Compute mean
    means[i] = (NNeighbors(i)) ? sum / NNeighbors(i) : 0;

    // Compute sum of squared residuals
    RNLength ssd = 0;
    for (int j = 0; j < NNeighbors(i); j++) {
      R3SurfelPoint *point1 = Neighbor(i,j);
      R3Point position1 = point1->Position();
      RNLength edge_length = R3Distance(position0, position1);
      RNLength delta = edge_length - means[i];
      ssd += delta * delta;
    }

    // Compute standard deviation
    RNScalar variance = (NNeighbors(i)) ? ssd / NNeighbors(i) : 0; 
    stddevs[i] = sqrt(variance);
  }

  // Remove outlier edges
  for (int i = 0; i < NPoints(); i++) {
    if (stddevs[i] == 0) continue;
    R3SurfelPoint *point0 = Point(i);
    R3Point position0 = point0->Position();
    for (int j = 0; j < neighbors[i].NEntries(); j++) {
      R3SurfelPoint *point1 = neighbors[i].Kth(j);
      R3Point position1 = point1->Position();
      RNLength edge_length = R3Distance(position0, position1);
      RNScalar zscore = (edge_length - means[i]) / stddevs[i];
      if (zscore > max_zscore) {
        // Remove edge
        RNArrayEntry *entry = neighbors[i].KthEntry(j);
        neighbors[i].EntryContents(entry) = neighbors[i].Tail();
        neighbors[i].RemoveTail();
        j--;
      }
    }
  }    

  // Delete temporary memory for edge length statistics
  delete [] means;
  delete [] stddevs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x38(%rsp)
movq %rdi, %rbx
movslq 0x18(%rdi), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %r15
cmovnsq %rcx, %r15
movq %r15, %rdi
callq 0x13030
movq %rax, %r12
movq %r15, %rdi
callq 0x13030
movq %rax, (%rsp)
cmpl $0x0, 0x18(%rbx)
movq %r12, 0x8(%rsp)
jle 0x372dd
xorl %r15d, %r15d
leaq 0x18(%rsp), %rdi
movq 0x10(%rbx), %rax
movq %r15, %rbp
shlq $0x4, %rbp
movq (%rax,%rbp), %rcx
movq 0x8(%rax,%rbp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
callq 0x84d7e
movq 0x68(%rbx), %rcx
movl 0xc(%rcx,%rbp), %eax
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x371d0
movq %rbx, %r13
movq %r15, 0x30(%rsp)
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
leaq 0x40(%rsp), %rbx
movsd %xmm1, 0x10(%rsp)
movq (%rcx,%rbp), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
movq %rbx, %rdi
callq 0x84d7e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x508bc
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r14
movq 0x68(%r13), %rcx
movslq 0xc(%rcx,%rbp), %rax
cmpq %rax, %r14
jl 0x3715b
movq 0x30(%rsp), %r15
movq %r13, %rbx
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0x371e7
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movsd %xmm0, (%r12,%r15,8)
movq 0x68(%rbx), %rcx
movl 0xc(%rcx,%rbp), %eax
testl %eax, %eax
jle 0x37292
xorl %r14d, %r14d
movq %rbx, %r13
movq %r15, %rbx
leaq 0x18(%rsp), %r15
leaq 0x40(%rsp), %r12
movsd %xmm2, 0x10(%rsp)
movq (%rcx,%rbp), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
movq %r12, %rdi
callq 0x84d7e
movq %r15, %rdi
movq %r12, %rsi
callq 0x508bc
movsd 0x10(%rsp), %xmm2
movq 0x8(%rsp), %rax
subsd (%rax,%rbx,8), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm0, %xmm2
incq %r14
movq 0x68(%r13), %rcx
movslq 0xc(%rcx,%rbp), %rax
cmpq %rax, %r14
jl 0x37210
movq 0x8(%rsp), %r12
movq %rbx, %r15
movq %r13, %rbx
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0x372ad
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm2
movapd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x372b9
sqrtsd %xmm0, %xmm0
jmp 0x372be
callq 0x133c0
leaq 0x18(%rsp), %rdi
movq (%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
movslq 0x18(%rbx), %rax
cmpq %rax, %r15
jl 0x370ef
cmpl $0x0, 0x18(%rbx)
jle 0x37428
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
movq (%rsp), %rax
movsd (%rax,%r15,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x37304
jnp 0x37418
movq 0x10(%rbx), %rax
movq %r15, %rbp
shlq $0x4, %rbp
movq (%rax,%rbp), %rcx
movq 0x8(%rax,%rbp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
leaq 0x18(%rsp), %rdi
callq 0x84d7e
movq 0x68(%rbx), %rax
cmpl $0x0, 0xc(%rax,%rbp)
jle 0x3740f
addq %rbp, %rax
xorl %r13d, %r13d
movq (%rax), %rax
movslq %r13d, %r12
movq (%rax,%r12,8), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x84d7e
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x508bc
movq 0x8(%rsp), %rax
subsd (%rax,%r15,8), %xmm0
movq (%rsp), %rax
divsd (%rax,%r15,8), %xmm0
ucomisd 0x38(%rsp), %xmm0
jbe 0x373f9
movq 0x68(%rbx), %rax
movq (%rax,%rbp), %rcx
movslq 0xc(%rax,%rbp), %rax
movq -0x8(%rcx,%rax,8), %rax
movq %rax, (%rcx,%r12,8)
movq 0x68(%rbx), %rdi
movl 0xc(%rdi,%rbp), %esi
addq %rbp, %rdi
decl %esi
callq 0xab13e
decl %r12d
movl %r12d, %r13d
incl %r13d
movq 0x68(%rbx), %rcx
leaq (%rcx,%rbp), %rax
cmpl 0xc(%rcx,%rbp), %r13d
jl 0x3735d
xorpd %xmm1, %xmm1
movq 0x8(%rsp), %r12
incq %r15
movslq 0x18(%rbx), %rax
cmpq %rax, %r15
jl 0x372ee
movq %r12, %rdi
callq 0x13680
movq (%rsp), %rdi
callq 0x13680
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq