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

  // Read resolutions
  int res[3];
  if (fscanf(fp, "%d%d%d\n", &res[0], &res[1], &res[2]) != (unsigned int) 3) {
    RNFail("Unable to read file %s\n", filename);
    fclose(fp);
    return 0;
  }

  // Read world to grid transformation
  double m[16];
  for (int i = 0; i < 16; i++) {
    if (fscanf(fp, "%lf", &m[i]) != (unsigned int) 1) {
      RNFail("Invalid format in %s\n", filename);
      fclose(fp);
      return 0;
    }
  }

  // Re-allocate grid values
  int new_size = res[0] * res[1] * res[2];
  if (!grid_values || (new_size > grid_size)) { 
    if (grid_values) delete [] grid_values;
    grid_values = new RNScalar [ new_size ];
    assert(grid_values);
  }

  // Update grid resolution variables
  grid_resolution[0] = res[0];
  grid_resolution[1] = res[1];
  grid_resolution[2] = res[2];
  grid_row_size = grid_resolution[0];
  grid_sheet_size = grid_row_size * grid_resolution[1];
  grid_size = grid_sheet_size * grid_resolution[2];
  if (grid_size <= 0) {
    RNFail("Invalid grid size (%d) in file", grid_size);
    fclose(fp);
    return 0;
  }

  // Read data
  RNScalar *grid_valuesp  = grid_values;
  for (int i = 0; i < grid_size; i++) {
    if (fscanf(fp, "%lf", grid_valuesp++) != 1) {
      RNFail("Error reading grid values from %s\n", filename);
      fclose(fp);
      return 0;
    }
  }

  // Set world-grid transformation
  SetWorldToGridTransformation(R3Affine(R4Matrix(m)));

  // Close the file
  fclose(fp);

  // Return success
  return grid_size;
}

// Assembly:
subq $0x298, %rsp            # imm = 0x298
movq %rdi, 0x288(%rsp)
movq %rsi, 0x280(%rsp)
movq 0x288(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x280(%rsp), %rdi
leaq 0xd0030(%rip), %rsi     # 0x138673
callq 0xf5e0
movq %rax, 0x278(%rsp)
cmpq $0x0, 0x278(%rsp)
jne 0x68681
movq 0x280(%rsp), %rsi
leaq 0xcae01(%rip), %rdi     # 0x13346b
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x294(%rsp)
jmp 0x68a05
movq 0x278(%rsp), %rdi
leaq 0x26c(%rsp), %rdx
leaq 0x26c(%rsp), %rcx
addq $0x4, %rcx
leaq 0x26c(%rsp), %r8
addq $0x8, %r8
leaq 0xcc996(%rip), %rsi     # 0x135046
movb $0x0, %al
callq 0xf910
cmpl $0x3, %eax
je 0x686ef
movq 0x280(%rsp), %rsi
leaq 0xcc983(%rip), %rdi     # 0x13504e
movb $0x0, %al
callq 0xd4f30
movq 0x278(%rsp), %rdi
callq 0xf4b0
movl $0x0, 0x294(%rsp)
jmp 0x68a05
movl $0x0, 0x1dc(%rsp)
cmpl $0x10, 0x1dc(%rsp)
jge 0x68781
movq 0x278(%rsp), %rdi
movslq 0x1dc(%rsp), %rax
leaq 0x1e0(%rsp), %rdx
shlq $0x3, %rax
addq %rax, %rdx
leaq 0xcdb06(%rip), %rsi     # 0x136230
movb $0x0, %al
callq 0xf910
cmpl $0x1, %eax
je 0x68769
movq 0x280(%rsp), %rsi
leaq 0xcc921(%rip), %rdi     # 0x135066
movb $0x0, %al
callq 0xd4f30
movq 0x278(%rsp), %rdi
callq 0xf4b0
movl $0x0, 0x294(%rsp)
jmp 0x68a05
jmp 0x6876b
movl 0x1dc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1dc(%rsp)
jmp 0x686fa
movq 0x20(%rsp), %rax
movl 0x26c(%rsp), %ecx
imull 0x270(%rsp), %ecx
imull 0x274(%rsp), %ecx
movl %ecx, 0x1d8(%rsp)
cmpq $0x0, 0x230(%rax)
je 0x687c2
movq 0x20(%rsp), %rcx
movl 0x1d8(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jle 0x6882c
movq 0x20(%rsp), %rax
cmpq $0x0, 0x230(%rax)
je 0x687f4
movq 0x20(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x687f2
movq 0x18(%rsp), %rdi
callq 0xf670
jmp 0x687f4
movslq 0x1d8(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x230(%rax)
movq 0x20(%rsp), %rax
movl 0x26c(%rsp), %ecx
movl %ecx, 0x238(%rax)
movl 0x270(%rsp), %ecx
movl %ecx, 0x23c(%rax)
movl 0x274(%rsp), %ecx
movl %ecx, 0x240(%rax)
movl 0x238(%rax), %ecx
movl %ecx, 0x244(%rax)
movl 0x244(%rax), %ecx
imull 0x23c(%rax), %ecx
movl %ecx, 0x248(%rax)
movl 0x248(%rax), %ecx
imull 0x240(%rax), %ecx
movl %ecx, 0x24c(%rax)
cmpl $0x0, 0x24c(%rax)
jg 0x688c9
movq 0x20(%rsp), %rax
movl 0x24c(%rax), %esi
leaq 0xcbad4(%rip), %rdi     # 0x134379
movb $0x0, %al
callq 0xd4f30
movq 0x278(%rsp), %rdi
callq 0xf4b0
movl $0x0, 0x294(%rsp)
jmp 0x68a05
movq 0x20(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x1d0(%rsp)
movl $0x0, 0x1cc(%rsp)
movq 0x20(%rsp), %rcx
movl 0x1cc(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jge 0x68979
movq 0x278(%rsp), %rdi
movq 0x1d0(%rsp), %rdx
movq %rdx, %rax
addq $0x8, %rax
movq %rax, 0x1d0(%rsp)
leaq 0xcd90e(%rip), %rsi     # 0x136230
movb $0x0, %al
callq 0xf910
cmpl $0x1, %eax
je 0x68961
movq 0x280(%rsp), %rsi
leaq 0xcc5ae(%rip), %rdi     # 0x134eeb
movb $0x0, %al
callq 0xd4f30
movq 0x278(%rsp), %rdi
callq 0xf4b0
movl $0x0, 0x294(%rsp)
jmp 0x68a05
jmp 0x68963
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0x688e8
leaq 0x38(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x1e0(%rsp), %rsi
callq 0x51d90
movq 0x8(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x10(%rsp)
xorl %edx, %edx
callq 0x4d390
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x5e430
jmp 0x689ba
leaq 0xb8(%rsp), %rdi
callq 0x4d430
movq 0x278(%rsp), %rdi
callq 0xf4b0
movq 0x20(%rsp), %rax
movl 0x24c(%rax), %eax
movl %eax, 0x294(%rsp)
jmp 0x68a05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x4d430
jmp 0x68a14
movl 0x294(%rsp), %eax
addq $0x298, %rsp            # imm = 0x298
retq
movq 0x30(%rsp), %rdi
callq 0xf890
nop