// File: /workspace/github_cpp_makefile_star2_O1/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 0x38ba8
movq 0xac3fb(%rip), %rax     # 0xe4fa0
movq (%rax), %r14
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x38de4
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 0x38bfd
cmpl 0x24c(%rbx), %ebp
jle 0x38c35
movdqa %xmm1, (%rsp)
testq %rdi, %rdi
je 0x38c0c
callq 0xe5b0
movslq %ebp, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %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 0x38dfa
leaq 0x110(%rbx), %rax
movq %rax, (%rsp)
leaq 0x118(%rbx), %rbp
movl $0x10, %r13d
leaq 0x28(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0xe6c0
movq %rax, %r15
cmpq $0x1, %rax
jne 0x38caf
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
movsd %xmm0, (%rbp)
addq $0x8, %rbp
jmp 0x38cbd
leaq 0x6ebac(%rip), %rdi     # 0xa7862
xorl %eax, %eax
callq 0x72a1e
cmpq $0x1, %r15
jne 0x38df6
decl %r13d
jne 0x38c7d
cmpl $0x0, 0x24c(%rbx)
jle 0x38d40
movq 0x230(%rbx), %r13
leaq 0x28(%rsp), %r12
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0xe6c0
movq %rax, %r15
cmpq $0x1, %rax
jne 0x38d16
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
movsd %xmm0, (%r13)
addq $0x8, %r13
jmp 0x38d2c
movl 0x24c(%rbx), %edx
leaq 0x6eb72(%rip), %rdi     # 0xa7895
movl %ebp, %esi
xorl %eax, %eax
callq 0x72a1e
cmpq $0x1, %r15
jne 0x38df6
incl %ebp
cmpl 0x24c(%rbx), %ebp
jl 0x38ce3
movq (%rsp), %r14
movq %r14, %rdi
callq 0x2a326
movq %xmm0, 0x220(%rbx)
movsd 0x692dc(%rip), %xmm1    # 0xa2038
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)
movq %r14, %rdi
callq 0x2a29a
movl 0x218(%rbx), %edx
andl $0x1, %edx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x2a22e
leaq 0x8(%rbx), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2e19e
movl $0x88, %eax
leaq (%rbx,%rax), %rdi
addq %rax, %r14
movq %r14, %rsi
callq 0x2e19e
leaq 0x28(%rsp), %rdi
movq 0x108(%rdi), %rax
movq %rax, 0x108(%rbx)
callq 0x2a27c
movl 0x24c(%rbx), %ebx
jmp 0x38e0c
leaq 0x6ea34(%rip), %rdi     # 0xa781f
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x72a1e
jmp 0x38e0c
xorl %ebx, %ebx
jmp 0x38e0c
leaq 0x6ea42(%rip), %rdi     # 0xa7843
xorl %ebx, %ebx
movl %ebp, %esi
xorl %eax, %eax
callq 0x72a1e
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2a27c
movq %rbx, %rdi
callq 0xe7e0
nop