// File: /workspace/github_cpp_makefile_star2_O3/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 $0x4e8, %rsp            # imm = 0x4E8
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x751c9(%rip), %rsi     # 0xabdd3
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x36f15
movq %rax, %r15
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x36f1e
cmpb $0x23, 0xe0(%rsp)
je 0x36c26
leaq 0x4(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x71624(%rip), %rsi     # 0xa827e
xorl %ebp, %ebp
leaq 0xe0(%rsp), %rdi
movq %rsp, %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x36f45
leaq 0xe0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x36f4e
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x3c530
leaq 0x20(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x71636(%rip), %rsi     # 0xa82e9
xorl %ebp, %ebp
leaq 0xe0(%rsp), %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x36f57
movq %r14, 0x10(%rsp)
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x30, %rdi
callq 0x3cfc4
addq $0x18, %r14
cmpq $0x48, %r14
jne 0x36cd8
leaq 0x30(%rsp), %r12
movl $0x3, %r14d
leaq 0xe0(%rsp), %r13
leaq 0x7162d(%rip), %rbp     # 0xa8336
movq %r13, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x36f60
leaq 0x8(%r12), %rcx
leaq 0x10(%r12), %r8
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x36f69
addq $0x18, %r12
decq %r14
jne 0x36d09
leaq 0xe0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x36f72
leaq 0xe0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x36f7b
movl (%rsp), %eax
movl 0x4(%rsp), %ecx
movl %ecx, %ebp
imull %eax, %ebp
movl 0x8(%rsp), %edx
movl %ebp, %r13d
imull %edx, %r13d
movq 0x10(%rsp), %r14
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x36dbb
cmpl 0x24c(%r14), %r13d
jle 0x36e28
movl %r13d, %r14d
testq %r12, %r12
je 0x36de2
movq %r12, %rdi
callq 0xe5b0
movl (%rsp), %eax
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %edx
movl %ecx, %ebp
imull %eax, %ebp
movl %edx, %r14d
imull %ebp, %r14d
movl %eax, 0xc(%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
movq 0x10(%rsp), %rax
movq %r12, 0x230(%rax)
movl %r14d, %r13d
movq %rax, %r14
movl 0xc(%rsp), %eax
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 0x36f84
leaq 0x72a18(%rip), %r13     # 0xa987a
xorl %ebp, %ebp
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x36f99
addq $0x8, %r12
incl %ebp
cmpl 0x24c(%r14), %ebp
jl 0x36e64
movl (%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm0
mulsd 0x30(%rsp), %xmm0
movl 0x4(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm1
movl 0x8(%rsp), %eax
mulsd 0x50(%rsp), %xmm1
decl %eax
cvtsi2sd %eax, %xmm2
mulsd 0x70(%rsp), %xmm2
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3cfc6
leaq 0xb0(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3c877
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x27326
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2e67a
leaq 0x78(%rsp), %rdi
callq 0x280f4
movl $0x1, %ebp
jmp 0x36f31
leaq 0x6b529(%rip), %rdi     # 0xa2445
jmp 0x36f25
leaq 0x7133b(%rip), %rdi     # 0xa8260
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebp, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7135f(%rip), %rdi     # 0xa82ab
jmp 0x36f27
leaq 0x71377(%rip), %rdi     # 0xa82cc
jmp 0x36f25
leaq 0x7139e(%rip), %rdi     # 0xa82fc
jmp 0x36f27
leaq 0x713b3(%rip), %rdi     # 0xa831a
jmp 0x36f25
leaq 0x713d8(%rip), %rdi     # 0xa8348
jmp 0x36f25
leaq 0x713ec(%rip), %rdi     # 0xa8365
jmp 0x36f25
leaq 0x71407(%rip), %rdi     # 0xa8389
jmp 0x36f25
leaq 0x708b4(%rip), %rdi     # 0xa783f
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x36f31
leaq 0x7140d(%rip), %rdi     # 0xa83ad
jmp 0x36f25
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x280f4
movq %rbx, %rdi
callq 0xe7e0
nop