// File: tomfunkhouser[P]gaps/apps/msh2df/msh2df.cpp
// Function: main
int 
main(int argc, char **argv)
{
  // Parse program arguments
  if (!ParseArgs(argc, argv)) exit(-1);

  // Read mesh file
  R3Mesh *mesh = ReadMesh(input_mesh_filename);
  if (!mesh) exit(-1);

  // Update program arguments based on mesh properties
  if (grid_bbox.IsEmpty()) grid_bbox = mesh->BBox();
  if (IsManifold(mesh)) input_is_manifold = TRUE;
  if (input_is_manifold) sign_estimation_method = 1;

  // Initialize grid with appropriate dimensions
  R3Grid grid(grid_bbox, target_grid_spacing, 5, grid_max_resolution, grid_border);

  // Estimate distance
  if (!EstimateDistanceUsingGrid(&grid, mesh)) return 0;

  // Refine distance
  if (refinement_radius > 0) {
    if (!RefineDistanceUsingKdtree(&grid, mesh, refinement_radius)) return 0;
  }

  // Estimate sign
  if (estimate_sign) {
    if (!EstimateSign(&grid, mesh)) exit(-1);
  }
  
  // Update signed distance
  RNBoolean update_distances = (estimate_sign && !input_is_manifold) ? TRUE : FALSE;
  if (update_distances) {
    if (!UpdateSignedDistanceUsingGrid(&grid, mesh)) return 0;
  }

  // Create point samples
  RNArray<Point *> points;
  if (output_points_filename || update_distances) {
    if (!CreatePoints(&grid, mesh, points)) return 0;
  }

  // Write point sample file
  if (output_points_filename && !points.IsEmpty()) {
    if (!WritePoints(points, output_points_filename)) return 0;
  }
  
  // Refine signed distance
  if (update_distances && !points.IsEmpty() && (refinement_radius > 0)) {
    if (!RefineSignedDistanceUsingKdtree(&grid, points, refinement_radius)) return 0;
  }

  // Delete point samples
  if (!points.IsEmpty()) {
    if (!DeletePoints(points)) return 0;
  }

  // Truncate distance
  if (refinement_radius > 0) {
    if (!SmoothDistanceAtRefinementBoundary(&grid, refinement_radius)) exit(-1);
  }

  // Truncate distance
  if (!TruncateDistance(&grid, truncation_distance)) exit(-1);

  // Write grid
  if (output_grid_filename) {
    if (!WriteGrid(&grid, output_grid_filename)) exit(-1);
  }

  // Write mesh file
  if (output_mesh_filename) {
    // Create isosurface
    R3Mesh *isosurface = ExtractIsosurface(&grid);
    if (!isosurface) exit(-1);

    // Write mesh
    if (!WriteMesh(isosurface, output_mesh_filename)) exit(-1);

    // Delete mesh
    delete isosurface;
  }

  // Return success
  return 0;
}

// Assembly:
subq $0x2f8, %rsp            # imm = 0x2F8
movl $0x0, 0x2f4(%rsp)
movl %edi, 0x2f0(%rsp)
movq %rsi, 0x2e8(%rsp)
movl 0x2f0(%rsp), %edi
movq 0x2e8(%rsp), %rsi
callq 0x12ae0
cmpl $0x0, %eax
jne 0x125b4
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
movq 0x17400d(%rip), %rdi    # 0x1865c8
callq 0x132c0
movq %rax, 0x2e0(%rsp)
cmpq $0x0, 0x2e0(%rsp)
jne 0x125dd
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
leaq 0x173f94(%rip), %rdi    # 0x186578
callq 0x17680
cmpl $0x0, %eax
je 0x1260a
movq 0x2e0(%rsp), %rdi
callq 0x17740
movq %rax, %rsi
leaq 0x173f73(%rip), %rdi    # 0x186578
callq 0x17760
movq 0x2e0(%rsp), %rdi
callq 0x13450
cmpl $0x0, %eax
je 0x12626
movl $0x1, 0x173faa(%rip)    # 0x1865d0
cmpl $0x0, 0x173fa3(%rip)    # 0x1865d0
je 0x12639
movl $0x1, 0x16aec7(%rip)    # 0x17d500
movsd 0x16aec7(%rip), %xmm0   # 0x17d508
movl 0x16aec9(%rip), %ecx    # 0x17d510
cvttsd2si 0x16aec8(%rip), %r8d # 0x17d518
leaq 0x173f21(%rip), %rsi    # 0x186578
leaq 0x90(%rsp), %rdi
movq %rdi, 0x50(%rsp)
movl $0x5, %edx
callq 0x7a260
movq 0x50(%rsp), %rdi
movq 0x2e0(%rsp), %rsi
callq 0x134f0
movl %eax, 0x5c(%rsp)
jmp 0x12686
movl 0x5c(%rsp), %eax
cmpl $0x0, %eax
jne 0x126c3
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12aa7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x12ac3
cmpl $0x0, 0x16ae56(%rip)    # 0x17d520
jle 0x12713
movq 0x2e0(%rsp), %rsi
movl 0x16ae46(%rip), %edx    # 0x17d520
leaq 0x90(%rsp), %rdi
callq 0x137d0
movl %eax, 0x4c(%rsp)
jmp 0x126ed
movl 0x4c(%rsp), %eax
cmpl $0x0, %eax
jne 0x12711
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12aa7
jmp 0x12713
cmpl $0x0, 0x173eba(%rip)    # 0x1865d4
je 0x1274c
movq 0x2e0(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x13dc0
movl %eax, 0x48(%rsp)
jmp 0x12737
movl 0x48(%rsp), %eax
cmpl $0x0, %eax
jne 0x1274a
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
jmp 0x1274c
xorl %eax, %eax
cmpl $0x0, 0x173e7f(%rip)    # 0x1865d4
movb %al, 0x47(%rsp)
je 0x1276b
cmpl $0x0, 0x173e6e(%rip)    # 0x1865d0
setne %al
xorb $-0x1, %al
movb %al, 0x47(%rsp)
movb 0x47(%rsp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x7c(%rsp)
cmpl $0x0, 0x7c(%rsp)
je 0x127c8
movq 0x2e0(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x13fb0
movl %eax, 0x40(%rsp)
jmp 0x127a2
movl 0x40(%rsp), %eax
cmpl $0x0, %eax
jne 0x127c6
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12aa7
jmp 0x127c8
leaq 0x68(%rsp), %rdi
callq 0x177c0
jmp 0x127d4
cmpq $0x0, 0x173dfc(%rip)    # 0x1865d8
jne 0x127e5
cmpl $0x0, 0x7c(%rsp)
je 0x1284e
movq 0x2e0(%rsp), %rsi
leaq 0x90(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x14310
movl %eax, 0x3c(%rsp)
jmp 0x12805
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
jne 0x1284c
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12a9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x177f0
jmp 0x12ac3
jmp 0x1284e
cmpq $0x0, 0x173d82(%rip)    # 0x1865d8
je 0x128ae
leaq 0x68(%rsp), %rdi
callq 0x177d0
movl %eax, 0x38(%rsp)
jmp 0x12868
movl 0x38(%rsp), %eax
cmpl $0x0, %eax
jne 0x128ae
movq 0x173d60(%rip), %rsi    # 0x1865d8
leaq 0x68(%rsp), %rdi
callq 0x14980
movl %eax, 0x34(%rsp)
jmp 0x12888
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
jne 0x128ac
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12a9d
jmp 0x128ae
cmpl $0x0, 0x7c(%rsp)
je 0x1291b
leaq 0x68(%rsp), %rdi
callq 0x177d0
movl %eax, 0x30(%rsp)
jmp 0x128c5
movl 0x30(%rsp), %eax
cmpl $0x0, %eax
jne 0x1291b
cmpl $0x0, 0x16ac4b(%rip)    # 0x17d520
jle 0x1291b
movl 0x16ac43(%rip), %edx    # 0x17d520
leaq 0x90(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x14b70
movl %eax, 0x2c(%rsp)
jmp 0x128f5
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
jne 0x12919
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12a9d
jmp 0x1291b
leaq 0x68(%rsp), %rdi
callq 0x177d0
movl %eax, 0x28(%rsp)
jmp 0x1292b
movl 0x28(%rsp), %eax
cmpl $0x0, %eax
jne 0x1296a
leaq 0x68(%rsp), %rdi
callq 0x15420
movl %eax, 0x24(%rsp)
jmp 0x12944
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
jne 0x12968
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x12a9d
jmp 0x1296a
cmpl $0x0, 0x16abaf(%rip)    # 0x17d520
jle 0x129a1
movl 0x16aba7(%rip), %esi    # 0x17d520
leaq 0x90(%rsp), %rdi
callq 0x15490
movl %eax, 0x20(%rsp)
jmp 0x1298c
movl 0x20(%rsp), %eax
cmpl $0x0, %eax
jne 0x1299f
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
jmp 0x129a1
movsd 0x173c37(%rip), %xmm0   # 0x1865e0
leaq 0x90(%rsp), %rdi
callq 0x15b80
movl %eax, 0x1c(%rsp)
jmp 0x129bc
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jne 0x129cf
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
cmpq $0x0, 0x173c11(%rip)    # 0x1865e8
je 0x12a08
movq 0x173c08(%rip), %rsi    # 0x1865e8
leaq 0x90(%rsp), %rdi
callq 0x15da0
movl %eax, 0x18(%rsp)
jmp 0x129f3
movl 0x18(%rsp), %eax
cmpl $0x0, %eax
jne 0x12a06
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
jmp 0x12a08
cmpq $0x0, 0x173be0(%rip)    # 0x1865f0
je 0x12a87
leaq 0x90(%rsp), %rdi
callq 0x15e90
movq %rax, 0x10(%rsp)
jmp 0x12a26
movq 0x10(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x60(%rsp)
jne 0x12a42
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
movq 0x60(%rsp), %rdi
movq 0x173ba2(%rip), %rsi    # 0x1865f0
callq 0x16020
movl %eax, 0xc(%rsp)
jmp 0x12a59
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x12a6c
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xf600
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x12a85
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x12a87
movl $0x0, 0x2f4(%rsp)
movl $0x1, 0x80(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x177f0
leaq 0x90(%rsp), %rdi
callq 0x7ad90
movl 0x2f4(%rsp), %eax
addq $0x2f8, %rsp            # imm = 0x2F8
retq
leaq 0x90(%rsp), %rdi
callq 0x7ad90
movq 0x88(%rsp), %rdi
callq 0xf890
nopl (%rax)