// File: /workspace/github_cpp_makefile_star2_O1/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 $0x228, %rsp            # imm = 0x228
movq %rsi, %r15
movq %rdi, %r14
leaq 0x72257(%rip), %rsi     # 0xabd93
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x39ce5
movq %rax, %rbx
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
leaq 0x6e9ab(%rip), %rsi     # 0xa850c
xorl %ebp, %ebp
leaq 0x4(%rsp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0xe840
cmpl $0x3, %eax
jne 0x39cfa
leaq 0x6fcf8(%rip), %r12     # 0xa987a
xorl %r13d, %r13d
leaq (%rsp,%r13), %rdx
addq $0x10, %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x39d03
addq $0x8, %r13
cmpq $0x80, %r13
jne 0x39b85
movslq 0x4(%rsp), %rax
movslq 0x8(%rsp), %rcx
imulq %rax, %rcx
movslq 0xc(%rsp), %r12
imulq %rcx, %r12
movq 0x230(%r14), %rdi
testq %rdi, %rdi
je 0x39bdc
cmpl 0x24c(%r14), %r12d
jle 0x39c07
testq %rdi, %rdi
je 0x39be6
callq 0xe5b0
movq %r12, %rax
shlq $0x3, %rax
testl %r12d, %r12d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0x230(%r14)
movl 0x4(%rsp), %eax
movq 0x8(%rsp), %rcx
movq %rcx, %xmm0
movd %eax, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
pshufd $0x34, %xmm1, %xmm1     # xmm1 = xmm1[0,1,3,0]
movdqu %xmm1, 0x238(%r14)
imull %eax, %ecx
movl %ecx, 0x248(%r14)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %esi
imull %ecx, %esi
movl %esi, 0x24c(%r14)
testl %esi, %esi
jle 0x39d32
cmpl $0x0, 0x24c(%r14)
jle 0x39c92
movq 0x230(%r14), %r12
leaq 0x6fc12(%rip), %r13     # 0xa987a
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x39d44
addq $0x8, %r12
incl %ebp
cmpl 0x24c(%r14), %ebp
jl 0x39c6a
leaq 0x1a8(%rsp), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x2c6ae
leaq 0x98(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2a22e
movq %r14, %rdi
movq %r12, %rsi
callq 0x3336c
leaq 0x98(%rsp), %rdi
callq 0x2a27c
movq %rbx, %rdi
callq 0xe430
movl 0x24c(%r14), %ebp
jmp 0x39d1e
leaq 0x6875c(%rip), %rdi     # 0xa2448
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x39d1e
leaq 0x6e813(%rip), %rdi     # 0xa8514
jmp 0x39d0c
leaq 0x6e822(%rip), %rdi     # 0xa852c
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
movq %rbx, %rdi
callq 0xe430
movl %ebp, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6db0a(%rip), %rdi     # 0xa7843
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x72a1e
jmp 0x39d16
leaq 0x6e666(%rip), %rdi     # 0xa83b1
jmp 0x39d0a
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x2a27c
movq %rbx, %rdi
callq 0xe7e0
nop