// File: /workspace/github_cpp_makefile_star2_O2/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 0x611dd(%rip), %rsi     # 0x9355b
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x32523
movq %rax, %r15
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3252c
movl 0x18(%rsp), %esi
cmpl $0x3c, %esi
jne 0x32553
leaq 0xe0(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x3c
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x3c, %rax
jne 0x32568
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3252c
movl 0x18(%rsp), %esi
cmpl $0x3c, %esi
jne 0x32553
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32824
movl 0x14(%rsp), %esi
cmpl $0x48, %esi
jne 0x32830
leaq 0x38(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32571
leaq 0x34(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3257d
leaq 0x30(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32589
leaq 0x6c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x32595
leaq 0x60(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x325a1
xorl %ebp, %ebp
pushq $0x1
popq %r13
leaq 0x4(%rbp), %r12
cmpq $0x10, %r12
je 0x325ad
movq %r13, %rax
leaq (%rsp,%rbp), %rdi
addq $0x54, %rdi
pushq $0x4
popq %rsi
movq %rax, %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3283c
leaq (%rsp,%rbp), %rdi
addq $0x48, %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xe6f0
movq %r12, %rbp
cmpq $0x1, %rax
je 0x324c7
leaq 0x66bd9(%rip), %rdi     # 0x990f7
jmp 0x3284c
leaq 0x669ae(%rip), %rdi     # 0x98ed8
jmp 0x32533
leaq 0x669c5(%rip), %rdi     # 0x98ef8
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x669d6(%rip), %rdi     # 0x98f30
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x3253f
leaq 0x669f4(%rip), %rdi     # 0x98f63
jmp 0x32533
leaq 0x66a83(%rip), %rdi     # 0x98ffb
jmp 0x3284c
leaq 0x66a9e(%rip), %rdi     # 0x99022
jmp 0x3284c
leaq 0x66ab9(%rip), %rdi     # 0x99049
jmp 0x3284c
leaq 0x66ad5(%rip), %rdi     # 0x99071
jmp 0x3284c
leaq 0x66af1(%rip), %rdi     # 0x99099
jmp 0x3284c
leaq 0x3c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x32845
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32824
movl 0x14(%rsp), %esi
cmpl $0x48, %esi
jne 0x32830
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 0x3285a
cmpl $0x4, %edx
jne 0x3285a
testl %ecx, %ecx
jne 0x3285a
ucomiss 0x6194a(%rip), %xmm0    # 0x93f7c
jne 0x3285a
jp 0x3285a
ucomiss 0x61937(%rip), %xmm1    # 0x93f7c
jne 0x3285a
jp 0x3285a
ucomiss 0x61924(%rip), %xmm2    # 0x93f7c
jne 0x3285a
jp 0x3285a
movslq 0x3c(%rsp), %rbp
incq %rbp
movslq 0x40(%rsp), %r13
incq %r13
movslq 0x44(%rsp), %rdx
incq %rdx
movq %r13, 0x28(%rsp)
imulq %rbp, %r13
movq %r13, 0x20(%rsp)
movq %rdx, 0x8(%rsp)
imulq %rdx, %r13
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x326a8
cmpl 0x24c(%r14), %r13d
jle 0x326d5
testq %r12, %r12
je 0x326b5
movq %r12, %rdi
callq 0xe5d0
movq %r13, %rax
shlq $0x3, %rax
testl %r13d, %r13d
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r12
movq %rax, 0x230(%r14)
movl %ebp, 0x238(%r14)
movq 0x28(%rsp), %rax
movl %eax, 0x23c(%r14)
movq 0x8(%rsp), %rax
movl %eax, 0x240(%r14)
movl %ebp, 0x244(%r14)
movq 0x20(%rsp), %rax
movl %eax, 0x248(%r14)
movl %r13d, 0x24c(%r14)
testl %r13d, %r13d
jle 0x32894
movl $0x0, 0x20(%rsp)
pushq $0x1
popq %r13
movq 0x8(%rsp), %rax
cmpl %eax, 0x20(%rsp)
jge 0x328d2
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x28(%rsp), %eax
jge 0x3280f
leaq 0x78(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x328ac
movl 0x78(%rsp), %esi
movslq 0x238(%r14), %rax
leaq (,%rax,4), %rcx
cmpq %rsi, %rcx
jne 0x328bf
xorl %ebp, %ebp
cmpl %eax, %ebp
jge 0x327c5
leaq 0xb0(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32876
cvtss2sd 0xb0(%rsp), %xmm0
movsd %xmm0, (%r12)
addq $0x8, %r12
incl %ebp
movl 0x238(%r14), %eax
jmp 0x32783
leaq 0x78(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x328ac
movl 0x78(%rsp), %esi
movslq 0x238(%r14), %rax
shlq $0x2, %rax
cmpq %rsi, %rax
jne 0x328bf
incl 0x8(%rsp)
movl 0x23c(%r14), %eax
movq %rax, 0x28(%rsp)
jmp 0x3273a
incl 0x20(%rsp)
movl 0x240(%r14), %eax
movq %rax, 0x8(%rsp)
jmp 0x32723
leaq 0x66767(%rip), %rdi     # 0x98f92
jmp 0x32533
leaq 0x66792(%rip), %rdi     # 0x98fc9
jmp 0x3255a
leaq 0x66882(%rip), %rdi     # 0x990c5
jmp 0x3284c
leaq 0x668db(%rip), %rdi     # 0x99127
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x681ca
jmp 0x3253f
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
leaq 0x668ef(%rip), %rdi     # 0x9915c
movb $0x3, %al
callq 0x681ca
jmp 0x3288d
movl 0x24c(%r14), %edx
leaq 0x65fbd(%rip), %rdi     # 0x98841
movl %ebp, %esi
xorl %eax, %eax
callq 0x681ca
xorl %ebp, %ebp
jmp 0x3253f
leaq 0x65f54(%rip), %rdi     # 0x987ef
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x3253f
leaq 0x668f8(%rip), %rdi     # 0x991ab
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x3288d
leaq 0x6691a(%rip), %rdi     # 0x991e0
movq %rbx, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x3288d
pushq $-0x1
popq %r12
leaq 0x1f(%rsp), %r13
pushq $0x1
popq %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xe6f0
incl %r12d
cmpq $0x1, %rax
je 0x328de
testl %r12d, %r12d
je 0x3290e
leaq 0x6642b(%rip), %rdi     # 0x98d2f
xorl %ebp, %ebp
movl %r12d, %esi
jmp 0x3255c
movss 0x6c(%rsp), %xmm1
movss %xmm1, 0x28(%rsp)
movss 0x70(%rsp), %xmm2
movss %xmm2, 0x8(%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, 0x20(%rsp)
movss 0x5c(%rsp), %xmm2
mulss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x37b4c
movss 0x28(%rsp), %xmm0
mulss 0x48(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x8(%rsp), %xmm1
mulss 0x4c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x20(%rsp), %xmm2
mulss 0x50(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xc8(%rsp), %r12
movq %r12, %rdi
callq 0x37b4c
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x24b90
movq %r14, %rdi
movq %r13, %rsi
callq 0x2b4b0
movq %r15, %rdi
callq 0xe440
movl 0x24c(%r14), %ebp
leaq 0x78(%rsp), %rdi
callq 0x25832
jmp 0x3253f
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x25832
movq %rbx, %rdi
callq 0xe810
nop