// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rsi, %r13
movq %rdi, %r14
leaq 0x67d4d(%rip), %rsi     # 0x9caf3
movq %r13, %rdi
callq 0xe550
testq %rax, %rax
je 0x34e1d
movq %rax, %rbx
leaq 0xc(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x646f1(%rip), %rsi     # 0x994b8
xorl %r12d, %r12d
leaq 0x8(%rsp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0xe870
cmpl $0x3, %eax
jne 0x34e33
leaq 0x65a25(%rip), %r12     # 0x9a80a
xorl %ecx, %ecx
leaq 0x8(%rcx), %r15
cmpq $0x88, %r15
je 0x34e61
leaq (%rsp,%rcx), %rdx
addq $0x20, %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe870
movq %r15, %rcx
cmpl $0x1, %eax
je 0x34de7
leaq 0x646c0(%rip), %rdi     # 0x994d8
xorl %r12d, %r12d
jmp 0x34e3a
leaq 0x5e5d1(%rip), %rdi     # 0x933f5
xorl %r12d, %r12d
movq %r13, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x34e4c
leaq 0x64686(%rip), %rdi     # 0x994c0
movq %r13, %rsi
xorl %eax, %eax
callq 0x681ca
movq %rbx, %rdi
callq 0xe440
movl %r12d, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x18(%rsp)
movl 0x8(%rsp), %r15d
movl 0xc(%rsp), %eax
movl %eax, %r13d
imull %r15d, %r13d
movl 0x10(%rsp), %ecx
movl %r13d, %ebp
imull %ecx, %ebp
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x34e95
cmpl 0x24c(%r14), %ebp
jle 0x34ef1
movl %ebp, %edx
testq %r12, %r12
je 0x34ebe
movq %r12, %rdi
callq 0xe5d0
movl 0x8(%rsp), %r15d
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ecx
movl %eax, %r13d
imull %r15d, %r13d
movl %ecx, %edx
imull %r13d, %edx
movl %eax, 0x14(%rsp)
movslq %ebp, %rax
movl %ecx, %r12d
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
movl %edx, %ebp
callq 0xe030
movl %r12d, %ecx
movq %rax, %r12
movl 0x14(%rsp), %eax
movq %r12, 0x230(%r14)
movl %r15d, 0x238(%r14)
movl %eax, 0x23c(%r14)
movl %ecx, 0x240(%r14)
movl %r15d, 0x244(%r14)
movl %r13d, 0x248(%r14)
movl %ebp, 0x24c(%r14)
testl %ebp, %ebp
jle 0x34f53
leaq 0x658e4(%rip), %r13     # 0x9a80a
xorl %r15d, %r15d
cmpl %ebp, %r15d
jge 0x34f6b
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe870
cmpl $0x1, %eax
jne 0x34fc1
addq $0x8, %r12
incl %r15d
movl 0x24c(%r14), %ebp
jmp 0x34f29
leaq 0x63895(%rip), %rdi     # 0x987ef
xorl %r12d, %r12d
movl %ebp, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x34e44
leaq 0x1b8(%rsp), %r15
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x29770
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x276a0
movq %r14, %rdi
movq %r12, %rsi
callq 0x2f570
leaq 0xa8(%rsp), %rdi
callq 0x276ee
movq %rbx, %rdi
callq 0xe440
movl 0x24c(%r14), %r12d
jmp 0x34e4c
leaq 0x64395(%rip), %rdi     # 0x9935d
xorl %r12d, %r12d
movq 0x18(%rsp), %rsi
jmp 0x34e3d
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x276ee
movq %rbx, %rdi
callq 0xe810
nop