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

  // Print header lines
  const R3Box bbox = WorldBox();
  const RNScalar delta = GridToWorldScaleFactor();
  fprintf(fp, "object 1 class gridpositions counts %d %d %d\n", XResolution(), YResolution(), ZResolution());
  fprintf(fp, "origin %lf %lf %lf\n", bbox[0][0], bbox[0][1], bbox[0][2]);
  fprintf(fp, "delta %lf 0 0\ndelta 0 %lf 0\ndelta 0 0 %lf\n", delta, delta, delta);
  fprintf(fp, "\nobject 2 class gridconnections counts %d %d %d\n", XResolution(), YResolution(), ZResolution());
  fprintf(fp, "\nobject 3 class array type double rank 0 items %d data follows\n", (XResolution() * YResolution() * ZResolution()));

  // 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));
      }
    }
  }

  // Print trailer lines
  fprintf(fp, "\nend\n");

  // Close file
  fclose(fp);

  // Return OK status
  return grid_size;
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x138(%rsp)
movq %rsi, 0x130(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x130(%rsp), %rdi
leaq 0xce24d(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
jne 0x660e4
movq 0x130(%rsp), %rsi
leaq 0xcd313(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x144(%rsp)
jmp 0x6652d
movq 0xc8(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x6bb40
movq 0xc8(%rsp), %rdi
callq 0x137c0
movsd %xmm0, 0xc0(%rsp)
jmp 0x66111
movq 0xc8(%rsp), %rdi
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0xb0(%rsp)
callq 0x138c0
movl %eax, 0xbc(%rsp)
jmp 0x66149
movq 0xc8(%rsp), %rdi
callq 0x138e0
movl %eax, 0xac(%rsp)
jmp 0x6615f
movq 0xc8(%rsp), %rdi
callq 0x13900
movl %eax, 0xa8(%rsp)
jmp 0x66175
movl 0xa8(%rsp), %r8d
movl 0xac(%rsp), %ecx
movl 0xbc(%rsp), %edx
movq 0xb0(%rsp), %rdi
leaq 0xced74(%rip), %rsi     # 0x134f0e
xorl %eax, %eax
movl %eax, 0x94(%rsp)
callq 0xf730
movl 0x94(%rsp), %esi
movq 0x128(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x44f00
movq %rax, 0xa0(%rsp)
jmp 0x661d6
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x13af0
movsd %xmm0, 0x88(%rsp)
jmp 0x661f0
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x44f00
movq %rax, 0x80(%rsp)
jmp 0x66209
movq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x13af0
movsd %xmm0, 0x78(%rsp)
jmp 0x66223
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x44f00
movq %rax, 0x70(%rsp)
jmp 0x66239
movq 0x70(%rsp), %rdi
movl $0x2, %esi
callq 0x13af0
movsd %xmm0, 0x68(%rsp)
jmp 0x66250
movsd 0x68(%rsp), %xmm2
movsd 0x78(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
movq 0x98(%rsp), %rdi
leaq 0xcecc8(%rip), %rsi     # 0x134f3c
movb $0x3, %al
movb %al, 0x57(%rsp)
callq 0xf730
movb 0x57(%rsp), %al
movq 0x128(%rsp), %rdi
movsd 0xe8(%rsp), %xmm2
leaq 0xcecb5(%rip), %rsi     # 0x134f50
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xf730
movq 0xc8(%rsp), %rdi
movq 0x128(%rsp), %rax
movq %rax, 0x58(%rsp)
callq 0x138c0
movl %eax, 0x64(%rsp)
jmp 0x662c6
movq 0xc8(%rsp), %rdi
callq 0x138e0
movl %eax, 0x50(%rsp)
jmp 0x662d9
movq 0xc8(%rsp), %rdi
callq 0x13900
movl %eax, 0x4c(%rsp)
jmp 0x662ec
movl 0x4c(%rsp), %r8d
movl 0x50(%rsp), %ecx
movl 0x64(%rsp), %edx
movq 0x58(%rsp), %rdi
leaq 0xcec76(%rip), %rsi     # 0x134f7b
xorl %eax, %eax
callq 0xf730
movq 0xc8(%rsp), %rdi
movq 0x128(%rsp), %rax
movq %rax, 0x40(%rsp)
callq 0x138c0
movl %eax, 0x48(%rsp)
jmp 0x6632c
movq 0xc8(%rsp), %rdi
callq 0x138e0
movl %eax, 0x3c(%rsp)
jmp 0x6633f
movq 0xc8(%rsp), %rdi
movl 0x3c(%rsp), %ecx
movl 0x48(%rsp), %eax
imull %ecx, %eax
movl %eax, 0x34(%rsp)
callq 0x13900
movl %eax, 0x38(%rsp)
jmp 0x66361
movq 0x40(%rsp), %rdi
movl 0x38(%rsp), %eax
movl 0x34(%rsp), %edx
imull %eax, %edx
leaq 0xcec34(%rip), %rsi     # 0x134fac
movb $0x0, %al
callq 0xf730
movl $0x0, 0xd8(%rsp)
movq 0xc8(%rsp), %rdi
movl 0xd8(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x138c0
movl %eax, 0x30(%rsp)
jmp 0x663a8
movl 0x2c(%rsp), %eax
movl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jge 0x664e6
movl $0x0, 0xd4(%rsp)
movq 0xc8(%rsp), %rdi
movl 0xd4(%rsp), %eax
movl %eax, 0x24(%rsp)
callq 0x138e0
movl %eax, 0x28(%rsp)
jmp 0x663e1
movl 0x24(%rsp), %eax
movl 0x28(%rsp), %ecx
cmpl %ecx, %eax
jge 0x664ce
movl $0x0, 0xd0(%rsp)
movq 0xc8(%rsp), %rdi
movl 0xd0(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x13900
movl %eax, 0x20(%rsp)
jmp 0x6641a
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %ecx
cmpl %ecx, %eax
jge 0x664b6
movq 0xc8(%rsp), %rdi
movq 0x128(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0xd8(%rsp), %esi
movl 0xd4(%rsp), %edx
movl 0xd0(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0x10(%rsp)
jmp 0x66461
movsd 0x10(%rsp), %xmm0
movq 0x8(%rsp), %rdi
leaq 0xceb03(%rip), %rsi     # 0x134f76
movb $0x1, %al
callq 0xf730
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x663fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x36a80
jmp 0x6653c
jmp 0x664b8
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x663c3
jmp 0x664d0
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
jmp 0x6638a
movq 0x128(%rsp), %rdi
leaq 0xceaf7(%rip), %rsi     # 0x134fec
xorl %eax, %eax
callq 0xf730
movq 0x128(%rsp), %rdi
callq 0xf4b0
jmp 0x6650b
movq 0xc8(%rsp), %rax
movl 0x24c(%rax), %eax
movl %eax, 0x144(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x36a80
movl 0x144(%rsp), %eax
addq $0x148, %rsp            # imm = 0x148
retq
movq 0xe0(%rsp), %rdi
callq 0xf890
nopl (%rax)