// File: tomfunkhouser[P]gaps/apps/msh2df/msh2df.cpp
// Function: UpdateSignedDistanceUsingGrid(gaps::R3Grid*, gaps::R3Mesh*)
static int
UpdateSignedDistanceUsingGrid(R3Grid *grid, R3Mesh *mesh)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Recompute negative side
  R3Grid distance_grid(*grid);
  distance_grid.Threshold(0, 0, R3_GRID_KEEP_VALUE);
  distance_grid.SquaredDistanceTransform();
  distance_grid.Sqrt();
  distance_grid.Multiply(grid->GridToWorldScaleFactor());
  distance_grid.Negate();

  // Keep positive side, replace negative side
  for (int i = 0; i < grid->NEntries(); i++) {
    if (grid->GridValue(i) >= 0) continue;
    grid->SetGridValue(i, distance_grid.GridValue(i));
  }
  
  // Print statistics
  if (print_verbose) {
    printf("Updated signed distances using grid ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  Resolution = %d %d %d\n", grid->XResolution(), grid->YResolution(), grid->ZResolution());
    printf("  Spacing = %g\n", grid->GridToWorldScaleFactor());
    printf("  Cardinality = %d\n", grid->Cardinality());
    printf("  Volume = %g\n", grid->Volume());
    RNInterval grid_range = grid->Range();
    printf("  Minimum = %g\n", grid_range.Min());
    printf("  Maximum = %g\n", grid_range.Max());
    printf("  L1Norm = %g\n", grid->L1Norm());
    printf("  L2Norm = %g\n", grid->L2Norm());
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdi, 0x2f0(%rsp)
movq %rsi, 0x2e8(%rsp)
leaq 0x2d8(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0xddf30
movq 0x58(%rsp), %rdi
callq 0x17860
movq 0x2f0(%rsp), %rsi
leaq 0x88(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x7acf0
movq 0x60(%rsp), %rdi
leaq 0x12d367(%rip), %rax    # 0x141370
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm2
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x7dd80
jmp 0x1401e
leaq 0x88(%rsp), %rdi
callq 0x7d150
jmp 0x1402d
leaq 0x88(%rsp), %rdi
callq 0x7cd10
jmp 0x1403c
movq 0x2f0(%rsp), %rdi
callq 0x17b20
leaq 0x88(%rsp), %rdi
callq 0x81360
jmp 0x14058
leaq 0x88(%rsp), %rdi
callq 0x7cdf0
jmp 0x14067
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x2f0(%rsp), %rdi
callq 0x184a0
movl %eax, 0x54(%rsp)
jmp 0x1408a
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x14135
movq 0x2f0(%rsp), %rdi
movl 0x78(%rsp), %esi
callq 0x184c0
movsd %xmm0, 0x48(%rsp)
jmp 0x140b3
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x140e7
jmp 0x14125
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x7ad90
jmp 0x142fe
movq 0x2f0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x78(%rsp), %esi
movl %esi, 0x3c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x184c0
movsd %xmm0, 0x40(%rsp)
jmp 0x14111
movsd 0x40(%rsp), %xmm0
movl 0x3c(%rsp), %esi
movq 0x30(%rsp), %rdi
callq 0x18250
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x1406f
cmpl $0x0, 0x1724bc(%rip)    # 0x1865f8
je 0x142e4
leaq 0x12c279(%rip), %rdi    # 0x1403c2
xorl %eax, %eax
callq 0xf070
jmp 0x14152
leaq 0x2d8(%rsp), %rdi
callq 0xddff0
movsd %xmm0, 0x28(%rsp)
jmp 0x14167
movsd 0x28(%rsp), %xmm0
leaq 0x12c0af(%rip), %rdi    # 0x140223
movb $0x1, %al
callq 0xf070
jmp 0x1417d
movq 0x2f0(%rsp), %rdi
callq 0x17b40
movl %eax, 0x20(%rsp)
movq 0x2f0(%rsp), %rdi
callq 0x17b60
movl %eax, 0x24(%rsp)
movq 0x2f0(%rsp), %rdi
callq 0x17b80
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %edx
movl %eax, %ecx
leaq 0x12c0d3(%rip), %rdi    # 0x140290
xorl %eax, %eax
callq 0xf070
jmp 0x141c6
movq 0x2f0(%rsp), %rdi
callq 0x17b20
leaq 0x12c0cf(%rip), %rdi    # 0x1402a9
movb $0x1, %al
callq 0xf070
jmp 0x141e3
movq 0x2f0(%rsp), %rdi
callq 0x7b1f0
movl %eax, 0x1c(%rsp)
jmp 0x141f6
movl 0x1c(%rsp), %esi
leaq 0x12c0b8(%rip), %rdi    # 0x1402b9
xorl %eax, %eax
callq 0xf070
jmp 0x1420a
movq 0x2f0(%rsp), %rdi
callq 0x17ba0
movsd %xmm0, 0x10(%rsp)
jmp 0x1421f
movsd 0x10(%rsp), %xmm0
leaq 0x12c0a1(%rip), %rdi    # 0x1402cd
movb $0x1, %al
callq 0xf070
jmp 0x14235
movq 0x2f0(%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x7aff0
jmp 0x14249
leaq 0x68(%rsp), %rdi
callq 0x17be0
leaq 0x12c082(%rip), %rdi    # 0x1402dc
movb $0x1, %al
callq 0xf070
jmp 0x14263
leaq 0x68(%rsp), %rdi
callq 0x17bf0
leaq 0x12c078(%rip), %rdi    # 0x1402ec
movb $0x1, %al
callq 0xf070
jmp 0x1427d
movq 0x2f0(%rsp), %rdi
callq 0x7b0e0
movsd %xmm0, 0x8(%rsp)
jmp 0x14292
movsd 0x8(%rsp), %xmm0
leaq 0x12c05d(%rip), %rdi    # 0x1402fc
movb $0x1, %al
callq 0xf070
jmp 0x142a8
movq 0x2f0(%rsp), %rdi
callq 0x17c00
movsd %xmm0, (%rsp)
jmp 0x142bc
movsd (%rsp), %xmm0
leaq 0x12c043(%rip), %rdi    # 0x14030b
movb $0x1, %al
callq 0xf070
jmp 0x142d1
movq 0x168cb8(%rip), %rax    # 0x17cf90
movq (%rax), %rdi
callq 0xf5d0
jmp 0x142e2
jmp 0x142e4
leaq 0x88(%rsp), %rdi
callq 0x7ad90
movl $0x1, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
retq
movq 0x80(%rsp), %rdi
callq 0xf890
nopl (%rax,%rax)