// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::WriteASCIIFile(char const*) const
int R3Grid::
WriteASCIIFile(const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Print resolutions
  fprintf(fp, "%d %d %d\n", XResolution(), YResolution(), ZResolution());

  // Print world to grid transformation
  const R4Matrix& m = world_to_grid_transform.Matrix();
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      fprintf(fp, "%lf ", m[i][j]);
    }
    fprintf(fp, "\n");
  }

  // Print values
  for (int i = 0; i < XResolution(); i++) {
    for (int j = 0; j < YResolution(); j++) {
      for (int k = 0; k < ZResolution(); k++) {
	fprintf(fp, "%lf\n", GridValue(i, j, k));
      }
    }
  }

  // Close file
  fclose(fp);

  // Return OK status
  return grid_size;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x60(%rsp), %rdi
leaq 0xcb8af(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x68a76
movq 0x60(%rsp), %rsi
leaq 0xca97e(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x74(%rsp)
jmp 0x68c45
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0x20(%rsp)
callq 0x138c0
movq 0x30(%rsp), %rdi
movl %eax, 0x28(%rsp)
callq 0x138e0
movq 0x30(%rsp), %rdi
movl %eax, 0x2c(%rsp)
callq 0x13900
movq 0x20(%rsp), %rdi
movl 0x28(%rsp), %edx
movl 0x2c(%rsp), %ecx
movl %eax, %r8d
leaq 0xcafb0(%rip), %rsi     # 0x133a6d
movb $0x0, %al
callq 0xf730
movq 0x30(%rsp), %rdi
addq $0x110, %rdi            # imm = 0x110
callq 0x4e2a0
movq %rax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x4, 0x4c(%rsp)
jge 0x68b5a
movl $0x0, 0x48(%rsp)
cmpl $0x4, 0x48(%rsp)
jge 0x68b3a
movq 0x58(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x4e280
movq 0x18(%rsp), %rdi
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0xcc556(%rip), %rsi     # 0x13507c
movb $0x1, %al
callq 0xf730
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x68af1
movq 0x58(%rsp), %rdi
leaq 0xcfecc(%rip), %rsi     # 0x138a12
movb $0x0, %al
callq 0xf730
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x68ae2
movl $0x0, 0x44(%rsp)
movq 0x30(%rsp), %rdi
movl 0x44(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x68c2c
movl $0x0, 0x40(%rsp)
movq 0x30(%rsp), %rdi
movl 0x40(%rsp), %eax
movl %eax, 0x10(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0x68c1a
movl $0x0, 0x3c(%rsp)
movq 0x30(%rsp), %rdi
movl 0x3c(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x68c08
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, (%rsp)
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
movl 0x3c(%rsp), %ecx
callq 0x6bad0
movq (%rsp), %rdi
leaq 0xcc382(%rip), %rsi     # 0x134f76
movb $0x1, %al
callq 0xf730
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x68bae
jmp 0x68c0a
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x68b8a
jmp 0x68c1c
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x68b62
movq 0x58(%rsp), %rdi
callq 0xf4b0
movq 0x30(%rsp), %rax
movl 0x24c(%rax), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nop