// File: /workspace/github_cpp_makefile_star2_O1/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 0x74ed5(%rip), %rsi     # 0xabd93
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x371bb
movq %rax, %r15
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x371c4
cmpb $0x23, 0xe0(%rsp)
je 0x36eda
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
leaq 0x71374(%rip), %rsi     # 0xa8282
xorl %ebp, %ebp
leaq 0xe0(%rsp), %rdi
leaq 0x4(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x371eb
leaq 0xe0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x371f4
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x3ca24
leaq 0x20(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x71384(%rip), %rsi     # 0xa82ed
xorl %ebp, %ebp
leaq 0xe0(%rsp), %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x3, %eax
jne 0x371fd
movq %r14, 0x10(%rsp)
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x30, %rdi
callq 0x3d4bc
addq $0x18, %r14
cmpq $0x48, %r14
jne 0x36f8e
leaq 0x30(%rsp), %r12
movl $0x3, %r14d
leaq 0xe0(%rsp), %r13
leaq 0x7137b(%rip), %rbp     # 0xa833a
movq %r13, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x37206
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 0x3720f
addq $0x18, %r12
decq %r14
jne 0x36fbf
leaq 0xe0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x37218
leaq 0xe0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r15, %rdx
callq 0xe730
testq %rax, %rax
je 0x37221
movslq 0x4(%rsp), %rax
movslq 0x8(%rsp), %rcx
imulq %rax, %rcx
movslq 0xc(%rsp), %r14
imulq %rcx, %r14
movq 0x10(%rsp), %rdx
movq 0x230(%rdx), %rdi
testq %rdi, %rdi
je 0x37071
cmpl 0x24c(%rdx), %r14d
jle 0x370a1
testq %rdi, %rdi
je 0x3707b
callq 0xe5b0
movq %r14, %rax
shlq $0x3, %rax
testl %r14d, %r14d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq 0x10(%rsp), %rdx
movq %rax, 0x230(%rdx)
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(%rdx)
imull %eax, %ecx
movl %ecx, 0x248(%rdx)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %esi
imull %ecx, %esi
movl %esi, 0x24c(%rdx)
testl %esi, %esi
jle 0x3722a
movq %rdx, %r14
cmpl $0x0, 0x24c(%rdx)
jle 0x3712b
movq 0x230(%r14), %r12
leaq 0x72779(%rip), %r13     # 0xa987a
xorl %ebp, %ebp
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x3723c
addq $0x8, %r12
incl %ebp
cmpl 0x24c(%r14), %ebp
jl 0x37103
movl 0x4(%rsp), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x30(%rsp), %xmm0
movl 0x8(%rsp), %eax
decl %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x50(%rsp), %xmm1
movl 0xc(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm2
mulsd 0x70(%rsp), %xmm2
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x3d4be
leaq 0xb0(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3cd71
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x27542
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2e962
leaq 0x78(%rsp), %rdi
callq 0x28348
movl $0x1, %ebp
jmp 0x371d7
leaq 0x6b286(%rip), %rdi     # 0xa2448
jmp 0x371cb
leaq 0x71099(%rip), %rdi     # 0xa8264
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebp, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x710bd(%rip), %rdi     # 0xa82af
jmp 0x371cd
leaq 0x710d5(%rip), %rdi     # 0xa82d0
jmp 0x371cb
leaq 0x710fc(%rip), %rdi     # 0xa8300
jmp 0x371cd
leaq 0x71111(%rip), %rdi     # 0xa831e
jmp 0x371cb
leaq 0x71136(%rip), %rdi     # 0xa834c
jmp 0x371cb
leaq 0x7114a(%rip), %rdi     # 0xa8369
jmp 0x371cb
leaq 0x71165(%rip), %rdi     # 0xa838d
jmp 0x371cb
leaq 0x70612(%rip), %rdi     # 0xa7843
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x72a1e
jmp 0x371d7
leaq 0x7116e(%rip), %rdi     # 0xa83b1
jmp 0x371cb
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x28348
movq %rbx, %rdi
callq 0xe7e0