// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadGrid(_IO_FILE*)
int R3Grid::
ReadGrid(FILE *fp)
{
  // Check file
  if (!fp) fp = stdin;

  // Read grid resolution from file
  int res[3];
  if (fread(res, sizeof(int), 3, fp) != 3) {
    RNFail("Unable to read resolution from file");
    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 world_to_grid transformation to file
  RNScalar *m = (RNScalar *) &(world_to_grid_transform.Matrix()[0][0]);
  for (int i = 0; i < 16; i++) {
    float value;
    if (fread(&value, sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read transformation matrix value to file");
      return 0;
    }
    else {
      *(m++) = (RNScalar) value;
    }
  }

  // Read grid values
  RNScalar *grid_valuesp = grid_values;
  for (int i = 0; i < grid_size; 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;
    }
  }

  // Update transformation variables
  world_to_grid_scale_factor = world_to_grid_transform.ScaleFactor();
  grid_to_world_scale_factor = (world_to_grid_scale_factor != 0) ? 1 / world_to_grid_scale_factor : 1.0;
  grid_to_world_transform = world_to_grid_transform.Inverse();

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %r14
movq %rdi, %rbx
testq %rsi, %rsi
jne 0x34986
movq 0xa5625(%rip), %rax     # 0xd9fa8
movq (%rax), %r14
leaq 0x1c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x34a7b
movl 0x1c(%rsp), %r12d
movq 0x20(%rsp), %r15
movq %r15, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
movd %xmm0, %ebp
imull %r12d, %r15d
imull %r15d, %ebp
movq 0x230(%rbx), %rdi
testq %rdi, %rdi
je 0x349d7
cmpl 0x24c(%rbx), %ebp
jle 0x34a0b
movdqa %xmm1, (%rsp)
testq %rdi, %rdi
je 0x349e6
callq 0xe5d0
movslq %ebp, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0x230(%rbx)
movdqa (%rsp), %xmm1
movd %r12d, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
pshufd $0x34, %xmm0, %xmm0     # xmm0 = xmm0[0,1,3,0]
movdqu %xmm0, 0x238(%rbx)
movl %r15d, 0x248(%rbx)
movl %ebp, 0x24c(%rbx)
testl %ebp, %ebp
jle 0x34a84
leaq 0x110(%rbx), %rax
movq %rax, (%rsp)
pushq $0x23
popq %r15
leaq 0x28(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x1
popq %rbp
cmpl $0x33, %r15d
je 0x34a98
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x34ae1
cvtss2sd 0x28(%rsp), %xmm0
movsd %xmm0, (%rbx,%r15,8)
incq %r15
jmp 0x34a4d
leaq 0x63d49(%rip), %rdi     # 0x987cb
jmp 0x34ae8
leaq 0x63d64(%rip), %rdi     # 0x987ef
xorl %ebx, %ebx
movl %ebp, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x34af1
movq 0x230(%rbx), %r13
xorl %r12d, %r12d
pushq $0x4
popq %rbp
pushq $0x1
popq %r15
cmpl 0x24c(%rbx), %r12d
jge 0x34b05
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x34b76
cvtss2sd 0x28(%rsp), %xmm0
movsd %xmm0, (%r13,%r12,8)
incq %r12
jmp 0x34aa9
leaq 0x63d26(%rip), %rdi     # 0x9880e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %r15
movq %r15, %rdi
callq 0x27796
movq %xmm0, 0x220(%rbx)
movsd 0x5e4e7(%rip), %xmm1    # 0x93008
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
xorpd %xmm3, %xmm3
cmpneqsd %xmm0, %xmm3
andpd %xmm3, %xmm2
andnpd %xmm1, %xmm3
orpd %xmm2, %xmm3
movlpd %xmm3, 0x228(%rbx)
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x366c6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21db8
leaq 0x28(%rsp), %rdi
callq 0x276ee
movl 0x24c(%rbx), %ebx
jmp 0x34af1
movl 0x24c(%rbx), %edx
leaq 0x63cbe(%rip), %rdi     # 0x98841
xorl %ebx, %ebx
movl %r12d, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x34af1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x276ee
movq %rbx, %rdi
callq 0xe810
nop