// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdi, %r14
leaq 0x726b5(%rip), %rsi     # 0xabdd3
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x398e0
movq %rax, %rbx
leaq 0x10(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x6edc5(%rip), %rsi     # 0xa8508
xorl %ebp, %ebp
leaq 0xc(%rsp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0xe840
cmpl $0x3, %eax
jne 0x398f5
leaq 0x70116(%rip), %r12     # 0xa987a
xorl %r13d, %r13d
leaq (%rsp,%r13), %rdx
addq $0x20, %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x398fe
addq $0x8, %r13
cmpq $0x80, %r13
jne 0x39767
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ecx
movl %ecx, %ebp
imull %eax, %ebp
movl 0x14(%rsp), %edx
movl %ebp, %r13d
imull %edx, %r13d
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x397bf
cmpl 0x24c(%r14), %r13d
jle 0x39829
movl %r13d, %esi
testq %r12, %r12
je 0x397e5
movq %r12, %rdi
callq 0xe5b0
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ecx
movl 0x14(%rsp), %edx
movl %ecx, %ebp
imull %eax, %ebp
movl %edx, %esi
imull %ebp, %esi
movl %esi, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
movslq %r13d, %rax
movl %ecx, %r12d
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
movl %edx, %r13d
callq 0xe030
movl %r13d, %edx
movl %r12d, %ecx
movq %rax, %r12
movl 0x1c(%rsp), %eax
movq %r12, 0x230(%r14)
movl 0x18(%rsp), %r13d
movl %eax, 0x238(%r14)
movl %ecx, 0x23c(%r14)
movl %edx, 0x240(%r14)
movl %eax, 0x244(%r14)
movl %ebp, 0x248(%r14)
movl %r13d, 0x24c(%r14)
testl %r13d, %r13d
jle 0x39907
leaq 0x70017(%rip), %r13     # 0xa987a
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x3991c
addq $0x8, %r12
incl %ebp
cmpl 0x24c(%r14), %ebp
jl 0x39865
leaq 0x1b8(%rsp), %r15
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x2c3b2
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x29fda
movq %r14, %rdi
movq %r12, %rsi
callq 0x331b6
leaq 0xa8(%rsp), %rdi
callq 0x2a028
movq %rbx, %rdi
callq 0xe430
movl 0x24c(%r14), %ebp
jmp 0x39937
leaq 0x68b5e(%rip), %rdi     # 0xa2445
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x39937
leaq 0x6ec14(%rip), %rdi     # 0xa8510
jmp 0x39925
leaq 0x6ec23(%rip), %rdi     # 0xa8528
jmp 0x39923
leaq 0x6df31(%rip), %rdi     # 0xa783f
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x3992f
leaq 0x6ea8a(%rip), %rdi     # 0xa83ad
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
movq %rbx, %rdi
callq 0xe430
movl %ebp, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x2a028
movq %rbx, %rdi
callq 0xe7e0
nop