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

  // Skip comments at top
  char buffer[1024];
  do {
    if (!fgets(buffer, 1024, fp)) {
       RNFail("Error reading comments in %s\n", filename);
       return 0;
    }
  } while (buffer[0] == '#');

  // Read dimensions
  int res[3];
  if (sscanf(buffer, "object 1 class gridpositions counts %d %d %d", &res[0], &res[1], &res[2]) != 3) {
    RNFail("Unable to read dimensions in %s\n", filename);
    return 0;
  }

  // Read origin
  if (!fgets(buffer, 1023, fp)) {
    RNFail("Unable to read origin in %s\n", filename);
    return 0;
  }

  // Parse origin
  R3Point origin;
  if (sscanf(buffer, "origin %lf %lf %lf", &origin[0], &origin[1], &origin[2]) != 3) {
    RNFail("Unable to parse origin in %s\n", filename);
    return 0;
  }

  // Read delta for each dimension
  R3Vector delta[3];
  for (RNDimension dim = RN_X; dim <= RN_Z; dim++) {
    // Read delta
    if (!fgets(buffer, 1023, fp)) {
      RNFail("Unable to read delta in %s\n", filename);
      return 0;
    }

    // Parse delta
    if (sscanf(buffer, "delta %lf %lf %lf", &delta[dim][0], &delta[dim][1], &delta[dim][2]) != 3) {
      RNFail("Unable to parse delta in %s\n", filename);
      return 0;
    }
  }

  // Read object 2 line
  if (!fgets(buffer, 1023, fp)) {
    RNFail("Unable to read object 2 line in %s\n", filename);
    return 0;
  }

  // Read object 3 line
  if (!fgets(buffer, 1023, fp)) {
    RNFail("Unable to read object 3 line in %s\n", filename);
    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);
    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);
      return 0;
    }
  }

  // Determine world-grid transformation
  R3Vector extent(delta[0][0] * (res[0]-1), delta[1][1] * (res[1]-1), delta[2][2] * (res[2]-1));
  R3Box world_box(origin, origin + extent);
  SetWorldToGridTransformation(world_box);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x6a0d7(%rip), %rsi     # 0x9caf3
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x32b67
movq %rax, %r15
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
callq 0xe760
testq %rax, %rax
je 0x32b70
cmpb $0x23, 0xf0(%rsp)
je 0x32a38
leaq 0x10(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x667c2(%rip), %rsi     # 0x9922e
xorl %ebp, %ebp
leaq 0xf0(%rsp), %rdi
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0xe430
cmpl $0x3, %eax
jne 0x32b97
leaq 0xf0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe760
testq %rax, %rax
je 0x32ba0
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x37b4a
leaq 0x30(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x667d2(%rip), %rsi     # 0x99299
xorl %ebp, %ebp
leaq 0xf0(%rsp), %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe430
cmpl $0x3, %eax
jne 0x32ba9
movq %r14, 0x18(%rsp)
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x40, %rdi
callq 0x3830a
addq $0x18, %r14
cmpq $0x48, %r14
jne 0x32aec
pushq $0x4
popq %r14
leaq 0x40(%rsp), %rbp
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe760
decq %r14
je 0x32bb2
testq %rax, %rax
je 0x32cd9
leaq 0x18(%rbp), %r13
leaq 0x8(%rbp), %rcx
leaq 0x10(%rbp), %r8
movq %r12, %rdi
leaq 0x6679a(%rip), %rsi     # 0x992e6
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe430
movq %r13, %rbp
cmpl $0x3, %eax
je 0x32b14
leaq 0x66793(%rip), %rdi     # 0x992f8
jmp 0x32b77
leaq 0x60887(%rip), %rdi     # 0x933f5
jmp 0x32b77
leaq 0x66699(%rip), %rdi     # 0x99210
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x666bd(%rip), %rdi     # 0x9925b
jmp 0x32b79
leaq 0x666d5(%rip), %rdi     # 0x9927c
jmp 0x32b77
leaq 0x666fc(%rip), %rdi     # 0x992ac
jmp 0x32b79
testq %rax, %rax
je 0x32ce5
leaq 0xf0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe760
testq %rax, %rax
je 0x32cf1
movl 0xc(%rsp), %ecx
movl 0x10(%rsp), %edx
movl %edx, %ebp
imull %ecx, %ebp
movl 0x14(%rsp), %esi
movl %ebp, %r12d
imull %esi, %r12d
movq 0x18(%rsp), %rax
movq 0x230(%rax), %r13
testq %r13, %r13
je 0x32c0b
cmpl 0x24c(%rax), %r12d
jle 0x32c72
movl %r12d, %r14d
testq %r13, %r13
je 0x32c33
movq %r13, %rdi
callq 0xe5d0
movl 0xc(%rsp), %ecx
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %esi
movl %edx, %ebp
imull %ecx, %ebp
movl %esi, %r14d
imull %ebp, %r14d
movl %ecx, 0x24(%rsp)
movslq %r12d, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
movl %edx, %r13d
movl %esi, %r12d
callq 0xe030
movl %r12d, %esi
movl %r13d, %edx
movl 0x24(%rsp), %ecx
movq %rax, %r13
movq 0x18(%rsp), %rax
movq %r13, 0x230(%rax)
movl %r14d, %r12d
movl %ecx, 0x238(%rax)
movl %edx, 0x23c(%rax)
movl %esi, 0x240(%rax)
movl %ecx, 0x244(%rax)
movl %ebp, 0x248(%rax)
movl %r12d, 0x24c(%rax)
testl %r12d, %r12d
jle 0x32cfd
leaq 0x67b67(%rip), %rbp     # 0x9a80a
xorl %r14d, %r14d
cmpl %r12d, %r14d
jge 0x32d15
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe870
cmpl $0x1, %eax
jne 0x32dab
addq $0x8, %r13
incl %r14d
movq 0x18(%rsp), %rax
movl 0x24c(%rax), %r12d
jmp 0x32ca6
leaq 0x665ea(%rip), %rdi     # 0x992ca
jmp 0x32b77
leaq 0x66629(%rip), %rdi     # 0x99315
jmp 0x32b77
leaq 0x66641(%rip), %rdi     # 0x99339
jmp 0x32b77
leaq 0x65aeb(%rip), %rdi     # 0x987ef
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x32b83
movl 0xc(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm0
mulsd 0x40(%rsp), %xmm0
movl 0x10(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm1
mulsd 0x60(%rsp), %xmm1
movl 0x14(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm2
mulsd 0x80(%rsp), %xmm2
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3830c
leaq 0xc0(%rsp), %r15
leaq 0x28(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x37e13
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x24b90
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x2b4b0
leaq 0x88(%rsp), %rdi
callq 0x25832
pushq $0x1
popq %rbp
jmp 0x32b83
leaq 0x665ab(%rip), %rdi     # 0x9935d
jmp 0x32b77
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x25832
movq %rbx, %rdi
callq 0xe810
nop