// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadInsightFile(char const*)
int R3Grid::
ReadInsightFile(const char *filename)
{
  // Delphi writes insight files as follows.
  // Each fortran write appears as <leading_record_size> data <trailing_record_size>
  // write(14)toplbl
  // write(14)ivary,nbyte,intdat,extent,extent,extent,
  //   xang,yang,zang,xstart,xend,ystart,yend,zstart,
  //   zend,intx,inty,intz
  // do 9041 k = 1,igrid
  //   do 9042 j = 1,igrid
  //     write(14)(phimap(i,j,k),i=1,igrid)

  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open insight file %s\n", filename);
    return 0;
  }

  // Read leading record size for toplbl
  int toplbl_record_size;
  if (fread(&toplbl_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read toplbl_record_size from insight file: %s", filename);
    return 0;
  }
  else if (toplbl_record_size != 60) {
    RNFail("Wrong toplbl record size (%d) in insight file: %s\n", toplbl_record_size, filename);
    return 0;
  }

  // Read toplbl from file 
  char toplabel[128];
  if (fread(toplabel, sizeof(char), 60, fp) != 60) {
    RNFail("Unable to read toplabel from insight file: %s\n", filename);
    return 0;
  }

  // Read trailing record size for toplbl
  if (fread(&toplbl_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read toplbl_record_size from insight file: %s", filename);
    return 0;
  }
  else if (toplbl_record_size != 60) {
    RNFail("Wrong toplbl record size (%d) in insight file: %s\n", toplbl_record_size, filename);
    return 0;
  }

  // Read leading record size for ivary-intervals
  int ivary_record_size;
  if (fread(&ivary_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read ivary_record_size from insight file: %s", filename);
    return 0;
  }
  else if (ivary_record_size != 72) {
    RNFail("Wrong ivary record size (%d) in insight file: %s\n", ivary_record_size, filename);
    return 0;
  }

  // Read ivary (0 means x varies most quickly)
  int ivary;
  if (fread(&ivary, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read ivary from insight file");
    return 0;
  }

  // Read nbyte (# of bytes in data (per entry?)
  int nbyte;
  if (fread(&nbyte, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read nbyte from insight file");
    return 0;
  }

  // Read intdat (0 means floating point)
  int intdat;
  if (fread(&intdat, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read intdat from insight file");
    return 0;
  }

  // Read extent
  float extent[3];
  if (fread(extent, sizeof(float), 3, fp) != 3) {
    RNFail("Unable to read extent from insight file");
    return 0;
  }

  // Read unit cell angle (90,90,90)
  float unit_cell_angle[3];
  if (fread(unit_cell_angle, sizeof(float), 3, fp) != 3) {
    RNFail("Unable to read cell angle from insight file");
    return 0;
  }

  // Read start and end values
  float start[3], end[3];
  for (int i = 0; i < 3; i++) {
    if (fread(&start[i], sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read start coordinate from insight file");
      return 0;
    }
    if (fread(&end[i], sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read end coordinate from insight file");
      return 0;
    }
  }

  // Read grid resolution
  int intervals[3];
  if (fread(intervals, sizeof(int), 3, fp) != 3) {
    RNFail("Unable to read number of intervals from insight file");
    return 0;
  }

  // Read trailing record size for ivary-intervals
  if (fread(&ivary_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read ivary_record_size from insight file: %s", filename);
    return 0;
  }
  else if (ivary_record_size != 72) {
    RNFail("Wrong ivary record size (%d) in insight file: %s\n", ivary_record_size, filename);
    return 0;
  }

  // Determine resolution
  int res[3];
  res[0] = intervals[0]+1;
  res[1] = intervals[1]+1;
  res[2] = intervals[2]+1;

  // Check standard values
  if ((ivary != 0) || (nbyte != 4) || (intdat != 0) ||
      (unit_cell_angle[0] != 90) || (unit_cell_angle[1] != 90) || (unit_cell_angle[2] != 90)) {
    RNFail("Problem with header info in insight file: %d %d %d (should be 0 4 0 90 90 90)\n", 
           ivary, nbyte, intdat, unit_cell_angle[0], unit_cell_angle[1], unit_cell_angle[2]);
    return 0;
  }

#if 0
  printf("%d %d %d   (%g %g %g)   (%g %g %g)    (%g %g %g) (%g %g %g)    (%d %d %d)   (%d %d %d)\n",
    ivary, nbyte, intdat,
    extent[0], extent[1], extent[2],
    unit_cell_angle[0], unit_cell_angle[1], unit_cell_angle[2],
    start[0], start[1], start[2],
    end[0], end[1], end[2],
    intervals[0], intervals[1], intervals[2], 
    res[0], res[1], res[2]);
#endif

  // 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 grid values
  RNScalar *grid_valuesp = grid_values;
  for (int k = 0; k < grid_resolution[2]; k++) {
    for (int j = 0; j < grid_resolution[1]; j++) {
      // Read leading record size for grid row
      unsigned int row_record_size;
      if (fread(&row_record_size, sizeof(int), 1, fp) != 1) {
        RNFail("Unable to read row_record_size from insight file: %s", filename);
        return 0;
      }
      else if (row_record_size != grid_resolution[0]*sizeof(float)) {
        RNFail("Wrong row record size (%d) in insight file: %s\n", row_record_size, filename);
        return 0;
      }

      // Read row
      for (int i = 0; i < grid_resolution[0]; i++) {
        float value;
        if (fread(&value, sizeof(float), 1, fp) != 1) {
          RNFail("Unable to read grid value %d of %d from file", i, grid_size);
          return 0;
        }
        else {
          *(grid_valuesp++) = (RNScalar) value;
        }
      }

      // Read trailing record size for grid row
      if (fread(&row_record_size, sizeof(int), 1, fp) != 1) {
        RNFail("Unable to read row_record_size from insight file: %s", filename);
        return 0;
      }
      else if (row_record_size != grid_resolution[0]*sizeof(float)) {
        RNFail("Wrong row record size (%d) in insight file: %s\n", row_record_size, filename);
        return 0;
      }
    }
  }

  // Make sure at end of file
  int count = 0;
  char dummytrailer;
  while (fread(&dummytrailer, sizeof(char), 1, fp) == 1) count++;
  if (count != 0) {
    RNFail("%d bytes of extra data at end of delphi file %s\n", count, filename);
    return 0;
  }

  // Determine world-grid transformation
  R3Point start_point(start[0]*extent[0], start[1]*extent[1], start[2]*extent[2]);
  R3Point end_point(end[0]*extent[0], end[1]*extent[1], end[2]*extent[2]);
  R3Box world_box(start_point, end_point);
  SetWorldToGridTransformation(world_box);

  // Close file
  fclose(fp);

  // Return number of grid values read
  return grid_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x6be3e(%rip), %rsi     # 0xa25ae
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x36c78
movq %rax, %r15
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36c81
movl 0x10(%rsp), %esi
cmpl $0x3c, %esi
jne 0x36ca8
leaq 0xe0(%rsp), %rdi
movl $0x1, %esi
movl $0x3c, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3c, %rax
jne 0x36cbd
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36c81
movl 0x10(%rsp), %esi
cmpl $0x3c, %esi
jne 0x36ca8
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36cc6
movl 0xc(%rsp), %esi
cmpl $0x48, %esi
jne 0x36ccf
leaq 0x38(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36cd8
leaq 0x34(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36ce1
leaq 0x30(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36cea
leaq 0x6c(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36cf3
leaq 0x60(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36cfc
xorl %r12d, %r12d
leaq (%rsp,%r12), %rdi
addq $0x54, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36d05
leaq (%rsp,%r12), %rdi
addq $0x48, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36d0e
addq $0x4, %r12
cmpq $0xc, %r12
jne 0x368da
leaq 0x3c(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36d17
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36cc6
movl 0xc(%rsp), %esi
cmpl $0x48, %esi
jne 0x36ccf
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x30(%rsp), %ecx
movss 0x60(%rsp), %xmm0
movss 0x64(%rsp), %xmm1
movss 0x68(%rsp), %xmm2
testl %esi, %esi
jne 0x36d2c
cmpl $0x4, %edx
jne 0x36d2c
testl %ecx, %ecx
jne 0x36d2c
ucomiss 0x6c613(%rip), %xmm0    # 0xa2fcc
jne 0x36d2c
jp 0x36d2c
ucomiss 0x6c600(%rip), %xmm1    # 0xa2fcc
jne 0x36d2c
jp 0x36d2c
ucomiss 0x6c5ed(%rip), %xmm2    # 0xa2fcc
jne 0x36d2c
jp 0x36d2c
movslq 0x3c(%rsp), %r13
incq %r13
movslq 0x40(%rsp), %rbp
incq %rbp
movslq 0x44(%rsp), %rcx
incq %rcx
movq %rbp, 0x20(%rsp)
imulq %r13, %rbp
movq %rbp, %r12
movq %rcx, 0x18(%rsp)
imulq %rcx, %r12
movq 0x230(%r14), %rdi
testq %rdi, %rdi
je 0x36a2d
cmpl 0x24c(%r14), %r12d
jle 0x36a58
testq %rdi, %rdi
je 0x36a37
callq 0xe5b0
movq %r12, %rax
shlq $0x3, %rax
testl %r12d, %r12d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0x230(%r14)
movl %r13d, 0x238(%r14)
movq 0x20(%rsp), %rax
movl %eax, 0x23c(%r14)
movq 0x18(%rsp), %rax
movl %eax, 0x240(%r14)
movl %r13d, 0x244(%r14)
movl %ebp, 0x248(%r14)
movl %r12d, 0x24c(%r14)
testl %r12d, %r12d
jle 0x36d4d
cmpl $0x0, 0x240(%r14)
setg 0x7(%rsp)
jle 0x36d72
movq 0x230(%r14), %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x0, 0x23c(%r14)
setg 0x18(%rsp)
jle 0x36c4c
xorl %r13d, %r13d
leaq 0x78(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36ba2
movl %r13d, 0x8(%rsp)
movl 0x78(%rsp), %esi
movslq 0x238(%r14), %rax
shlq $0x2, %rax
cmpq %rsi, %rax
jne 0x36bb7
cmpl $0x0, 0x238(%r14)
setg %r13b
jle 0x36b95
xorl %r12d, %r12d
movl $0x4, %esi
movl $0x1, %edx
leaq 0xb0(%rsp), %rdi
movq %r15, %rcx
callq 0xe6c0
movq %rax, %rbp
cmpq $0x1, %rax
jne 0x36b67
xorps %xmm0, %xmm0
cvtss2sd 0xb0(%rsp), %xmm0
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax)
addq $0x8, %rax
movq %rax, 0x20(%rsp)
jmp 0x36b7f
movl 0x24c(%r14), %edx
leaq 0x70d20(%rip), %rdi     # 0xa7895
movl %r12d, %esi
xorl %eax, %eax
callq 0x72a1e
cmpq $0x1, %rbp
jne 0x36b95
incl %r12d
cmpl 0x238(%r14), %r12d
setl %r13b
jl 0x36b24
testb $0x1, %r13b
je 0x36bcc
xorl %ebp, %ebp
jmp 0x36c28
xorl %ebp, %ebp
leaq 0x71654(%rip), %rdi     # 0xa81ff
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x36c32
xorl %ebp, %ebp
leaq 0x71674(%rip), %rdi     # 0xa8234
movq %rbx, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x36c2d
movl $0x4, %esi
movl $0x1, %edx
leaq 0x78(%rsp), %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36c15
movl 0x78(%rsp), %esi
movslq 0x238(%r14), %rax
shlq $0x2, %rax
movb $0x1, %bpl
cmpq %rsi, %rax
je 0x36c28
xorl %ebp, %ebp
leaq 0x7162b(%rip), %rdi     # 0xa8234
movq %rbx, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x36c28
xorl %ebp, %ebp
leaq 0x715e1(%rip), %rdi     # 0xa81ff
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
leaq 0x78(%rsp), %r12
movl 0x8(%rsp), %r13d
testb %bpl, %bpl
je 0x36c4c
incl %r13d
cmpl 0x23c(%r14), %r13d
setl 0x18(%rsp)
jl 0x36ad7
testb $0x1, 0x18(%rsp)
jne 0x36d65
movl 0x2c(%rsp), %eax
incl %eax
movl %eax, 0x2c(%rsp)
cmpl 0x240(%r14), %eax
setl 0x7(%rsp)
jl 0x36abc
jmp 0x36d72
leaq 0x712ad(%rip), %rdi     # 0xa7f2c
jmp 0x36c88
leaq 0x712c4(%rip), %rdi     # 0xa7f4c
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x712d5(%rip), %rdi     # 0xa7f84
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x36c94
leaq 0x712f3(%rip), %rdi     # 0xa7fb7
jmp 0x36c88
leaq 0x71319(%rip), %rdi     # 0xa7fe6
jmp 0x36c88
leaq 0x71347(%rip), %rdi     # 0xa801d
jmp 0x36caf
leaq 0x71370(%rip), %rdi     # 0xa804f
jmp 0x36d1e
leaq 0x7138e(%rip), %rdi     # 0xa8076
jmp 0x36d1e
leaq 0x713ac(%rip), %rdi     # 0xa809d
jmp 0x36d1e
leaq 0x713cb(%rip), %rdi     # 0xa80c5
jmp 0x36d1e
leaq 0x713ea(%rip), %rdi     # 0xa80ed
jmp 0x36d1e
leaq 0x7140d(%rip), %rdi     # 0xa8119
jmp 0x36d1e
leaq 0x71436(%rip), %rdi     # 0xa814b
jmp 0x36d1e
leaq 0x7145d(%rip), %rdi     # 0xa817b
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x72a1e
jmp 0x36c94
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
leaq 0x71471(%rip), %rdi     # 0xa81b0
movb $0x3, %al
callq 0x72a1e
xorl %ebp, %ebp
jmp 0x36c94
leaq 0x70aef(%rip), %rdi     # 0xa7843
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x72a1e
jmp 0x36c94
xorl %ebp, %ebp
testb $0x1, 0x7(%rsp)
jne 0x36c94
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x17(%rsp), %r13
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0xe6c0
incl %r12d
cmpq $0x1, %rax
je 0x36d7d
testl %r12d, %r12d
je 0x36db1
leaq 0x70fdc(%rip), %rdi     # 0xa7d83
xorl %ebp, %ebp
movl %r12d, %esi
jmp 0x36cb1
movss 0x6c(%rsp), %xmm1
movss %xmm1, 0x20(%rsp)
movss 0x70(%rsp), %xmm2
movss %xmm2, 0x18(%rsp)
movss 0x54(%rsp), %xmm0
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x58(%rsp), %xmm1
mulss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x74(%rsp), %xmm3
movss %xmm3, 0x8(%rsp)
movss 0x5c(%rsp), %xmm2
mulss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3ca26
movss 0x20(%rsp), %xmm0
mulss 0x48(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x18(%rsp), %xmm1
mulss 0x4c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x8(%rsp), %xmm2
mulss 0x50(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
callq 0x3ca26
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x27542
movq %r14, %rdi
movq %r13, %rsi
callq 0x2e962
movq %r15, %rdi
callq 0xe430
movl 0x24c(%r14), %ebp
leaq 0x78(%rsp), %rdi
callq 0x28348
jmp 0x36c94
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x28348
movq %rbx, %rdi
callq 0xe7e0