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

  // Rasterize each triangle into grid
  grid->Clear(0);
  for (int i = 0; i < mesh->NFaces(); i++) {
    R3MeshFace *face = mesh->Face(i);
    const R3Point& p0 = mesh->VertexPosition(mesh->VertexOnFace(face, 0));
    const R3Point& p1 = mesh->VertexPosition(mesh->VertexOnFace(face, 1));
    const R3Point& p2 = mesh->VertexPosition(mesh->VertexOnFace(face, 2));
    grid->RasterizeWorldTriangle(p0, p1, p2, i+1, R3_GRID_REPLACE_OPERATION);
  }

  // Estimate distance to closest face (at grid resolution)
  grid->SquaredDistanceTransform();
  grid->Sqrt();
  grid->Multiply(grid->GridToWorldScaleFactor());
  
  // Print statistics
  if (print_verbose) {
    printf("Estimated distance 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 $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xddf30
leaq 0x68(%rsp), %rdi
callq 0x17860
movq 0x80(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x80aa0
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x78(%rsp), %rdi
callq 0x17880
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1362d
movq 0x78(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x17a10
movq %rax, 0x58(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rdi
movq 0x58(%rsp), %rsi
xorl %edx, %edx
callq 0x17a60
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x17a40
movq %rax, 0x50(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0x1, %edx
callq 0x17a60
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x17a40
movq %rax, 0x48(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0x2, %edx
callq 0x17a60
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x17a40
movq %rax, 0x40(%rsp)
movq 0x80(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rcx
movl 0x64(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
movl $0x2, %r8d
callq 0x17a80
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x13530
movq 0x80(%rsp), %rdi
callq 0x7d150
movq 0x80(%rsp), %rdi
callq 0x7cd10
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %rdi
callq 0x17b20
movq 0x8(%rsp), %rdi
callq 0x81360
cmpl $0x0, 0x172f86(%rip)    # 0x1865f8
je 0x137bd
leaq 0x12cbee(%rip), %rdi    # 0x14026d
movb $0x0, %al
callq 0xf070
leaq 0x68(%rsp), %rdi
callq 0xddff0
leaq 0x12cb8c(%rip), %rdi    # 0x140223
movb $0x1, %al
callq 0xf070
movq 0x80(%rsp), %rdi
callq 0x17b40
movl %eax, (%rsp)
movq 0x80(%rsp), %rdi
callq 0x17b60
movl %eax, 0x4(%rsp)
movq 0x80(%rsp), %rdi
callq 0x17b80
movl (%rsp), %esi
movl 0x4(%rsp), %edx
movl %eax, %ecx
leaq 0x12cbb4(%rip), %rdi    # 0x140290
movb $0x0, %al
callq 0xf070
movq 0x80(%rsp), %rdi
callq 0x17b20
leaq 0x12cbb2(%rip), %rdi    # 0x1402a9
movb $0x1, %al
callq 0xf070
movq 0x80(%rsp), %rdi
callq 0x7b1f0
movl %eax, %esi
leaq 0x12cba5(%rip), %rdi    # 0x1402b9
movb $0x0, %al
callq 0xf070
movq 0x80(%rsp), %rdi
callq 0x17ba0
leaq 0x12cb9e(%rip), %rdi    # 0x1402cd
movb $0x1, %al
callq 0xf070
movq 0x80(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x7aff0
leaq 0x30(%rsp), %rdi
callq 0x17be0
leaq 0x12cb83(%rip), %rdi    # 0x1402dc
movb $0x1, %al
callq 0xf070
leaq 0x30(%rsp), %rdi
callq 0x17bf0
leaq 0x12cb7b(%rip), %rdi    # 0x1402ec
movb $0x1, %al
callq 0xf070
movq 0x80(%rsp), %rdi
callq 0x7b0e0
leaq 0x12cb70(%rip), %rdi    # 0x1402fc
movb $0x1, %al
callq 0xf070
movq 0x80(%rsp), %rdi
callq 0x17c00
leaq 0x12cb64(%rip), %rdi    # 0x14030b
movb $0x1, %al
callq 0xf070
movq 0x1697db(%rip), %rax    # 0x17cf90
movq (%rax), %rdi
callq 0xf5d0
movl $0x1, %eax
addq $0x88, %rsp
retq
nopw (%rax,%rax)