// File: 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:
subq $0x5b8, %rsp            # imm = 0x5B8
movq %rdi, 0x5a8(%rsp)
movq %rsi, 0x5a0(%rsp)
movq 0x5a8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x5a0(%rsp), %rdi
leaq 0xd3c9d(%rip), %rsi     # 0x138673
callq 0xf5e0
movq %rax, 0x598(%rsp)
cmpq $0x0, 0x598(%rsp)
jne 0x64a14
movq 0x5a0(%rsp), %rsi
leaq 0xcea6e(%rip), %rdi     # 0x13346b
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
jmp 0x64a16
leaq 0x190(%rsp), %rdi
movq 0x598(%rsp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0xf7e0
cmpq $0x0, %rax
jne 0x64a5c
movq 0x5a0(%rsp), %rsi
leaq 0xd0359(%rip), %rdi     # 0x134d9e
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
jmp 0x64a5e
movsbl 0x190(%rsp), %eax
cmpl $0x23, %eax
je 0x64a16
leaq 0x190(%rsp), %rdi
leaq 0x184(%rsp), %rdx
leaq 0x184(%rsp), %rcx
addq $0x4, %rcx
leaq 0x184(%rsp), %r8
addq $0x8, %r8
leaq 0xd0322(%rip), %rsi     # 0x134dbc
movb $0x0, %al
callq 0xf4a0
cmpl $0x3, %eax
je 0x64acc
movq 0x5a0(%rsp), %rsi
leaq 0xd0334(%rip), %rdi     # 0x134de9
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
leaq 0x190(%rsp), %rdi
movq 0x598(%rsp), %rdx
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
cmpq $0x0, %rax
jne 0x64b12
movq 0x5a0(%rsp), %rsi
leaq 0xd030f(%rip), %rdi     # 0x134e0a
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
leaq 0x168(%rsp), %rdi
callq 0x6eb40
leaq 0x190(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movq %rax, 0x78(%rsp)
leaq 0x168(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movq %rax, 0x80(%rsp)
leaq 0x168(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq %rax, %r8
leaq 0xd029f(%rip), %rsi     # 0x134e27
movb $0x0, %al
callq 0xf4a0
cmpl $0x3, %eax
je 0x64bba
movq 0x5a0(%rsp), %rsi
leaq 0xd0297(%rip), %rdi     # 0x134e3a
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
leaq 0x120(%rsp), %rax
movq %rax, %rcx
addq $0x48, %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x6fca0
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x68(%rsp)
jne 0x64bd3
movl $0x0, 0x11c(%rsp)
cmpl $0x2, 0x11c(%rsp)
jg 0x64d33
leaq 0x190(%rsp), %rdi
movq 0x598(%rsp), %rdx
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
cmpq $0x0, %rax
jne 0x64c59
movq 0x5a0(%rsp), %rsi
leaq 0xd0216(%rip), %rdi     # 0x134e58
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
leaq 0x190(%rsp), %rax
movq %rax, 0x40(%rsp)
movslq 0x11c(%rsp), %rax
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x36c00
movq %rax, 0x48(%rsp)
movslq 0x11c(%rsp), %rax
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl $0x1, %esi
callq 0x36c00
movq %rax, 0x50(%rsp)
movslq 0x11c(%rsp), %rax
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl $0x2, %esi
callq 0x36c00
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq %rax, %r8
leaq 0xd018b(%rip), %rsi     # 0x134e74
movb $0x0, %al
callq 0xf4a0
cmpl $0x3, %eax
je 0x64d1b
movq 0x5a0(%rsp), %rsi
leaq 0xd0182(%rip), %rdi     # 0x134e86
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
jmp 0x64d1d
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x64c05
leaq 0x190(%rsp), %rdi
movq 0x598(%rsp), %rdx
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
cmpq $0x0, %rax
jne 0x64d79
movq 0x5a0(%rsp), %rsi
leaq 0xd0141(%rip), %rdi     # 0x134ea3
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
leaq 0x190(%rsp), %rdi
movq 0x598(%rsp), %rdx
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
cmpq $0x0, %rax
jne 0x64dbf
movq 0x5a0(%rsp), %rsi
leaq 0xd011f(%rip), %rdi     # 0x134ec7
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
movq 0x88(%rsp), %rax
movl 0x184(%rsp), %ecx
imull 0x188(%rsp), %ecx
imull 0x18c(%rsp), %ecx
movl %ecx, 0x118(%rsp)
cmpq $0x0, 0x230(%rax)
je 0x64e06
movq 0x88(%rsp), %rcx
movl 0x118(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jle 0x64e79
movq 0x88(%rsp), %rax
cmpq $0x0, 0x230(%rax)
je 0x64e3e
movq 0x88(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x64e3c
movq 0x38(%rsp), %rdi
callq 0xf670
jmp 0x64e3e
movslq 0x118(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x230(%rax)
movq 0x88(%rsp), %rax
movl 0x184(%rsp), %ecx
movl %ecx, 0x238(%rax)
movl 0x188(%rsp), %ecx
movl %ecx, 0x23c(%rax)
movl 0x18c(%rsp), %ecx
movl %ecx, 0x240(%rax)
movl 0x238(%rax), %ecx
movl %ecx, 0x244(%rax)
movl 0x244(%rax), %ecx
imull 0x23c(%rax), %ecx
movl %ecx, 0x248(%rax)
movl 0x248(%rax), %ecx
imull 0x240(%rax), %ecx
movl %ecx, 0x24c(%rax)
cmpl $0x0, 0x24c(%rax)
jg 0x64f0f
movq 0x88(%rsp), %rax
movl 0x24c(%rax), %esi
leaq 0xcf481(%rip), %rdi     # 0x134379
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
movq 0x88(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x110(%rsp)
movl $0x0, 0x10c(%rsp)
movq 0x88(%rsp), %rcx
movl 0x10c(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jge 0x64fb8
movq 0x598(%rsp), %rdi
movq 0x110(%rsp), %rdx
movq %rdx, %rax
addq $0x8, %rax
movq %rax, 0x110(%rsp)
leaq 0xd12c2(%rip), %rsi     # 0x136230
movb $0x0, %al
callq 0xf910
cmpl $0x1, %eax
je 0x64fa0
movq 0x5a0(%rsp), %rsi
leaq 0xcff62(%rip), %rdi     # 0x134eeb
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5b4(%rsp)
jmp 0x650e5
jmp 0x64fa2
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x64f31
leaq 0x120(%rsp), %rdi
xorl %esi, %esi
callq 0x36c00
movsd (%rax), %xmm0
movl 0x184(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x138(%rsp), %rdi
movl $0x1, %esi
callq 0x36c00
movsd (%rax), %xmm0
movl 0x188(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x150(%rsp), %rdi
movl $0x2, %esi
callq 0x36c00
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd (%rax), %xmm2
movl 0x18c(%rsp), %eax
decl %eax
cvtsi2sd %eax, %xmm3
mulsd %xmm3, %xmm2
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x6fcb0
movq 0x18(%rsp), %rdx
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x28(%rsp)
leaq 0x168(%rsp), %rsi
movq %rsi, 0x20(%rsp)
callq 0x6f110
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x42940
movq 0x88(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x559e0
jmp 0x650a8
movl $0x1, 0x5b4(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x36a80
jmp 0x650e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x36a80
jmp 0x650f4
movl 0x5b4(%rsp), %eax
addq $0x5b8, %rsp            # imm = 0x5B8
retq
movq 0x98(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)