// File: /workspace/github_cpp_makefile_star2_O3/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 0x38766
movq 0xac83d(%rip), %rax     # 0xe4fa0
movq (%rax), %r14
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x3895d
movl 0x4(%rsp), %r12d
movq 0x8(%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 0x387bb
cmpl 0x24c(%rbx), %ebp
jle 0x387f5
movdqa %xmm1, 0x10(%rsp)
testq %rdi, %rdi
je 0x387cb
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 0x10(%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 0x38966
leaq 0x110(%rbx), %r15
xorl %r13d, %r13d
leaq 0x28(%rsp), %r12
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3897a
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
movsd %xmm0, 0x118(%rbx,%r13,8)
incq %r13
cmpl $0x10, %r13d
jne 0x3882f
cmpl $0x0, 0x24c(%rbx)
jle 0x388bd
movq 0x230(%rbx), %rbp
xorl %r12d, %r12d
leaq 0x28(%rsp), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3899e
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
movsd %xmm0, (%rbp,%r12,8)
incq %r12
cmpl 0x24c(%rbx), %r12d
jl 0x38882
movq %r15, %rdi
callq 0x2a0d2
movq %xmm0, 0x220(%rbx)
movsd 0x69763(%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 %r15, %rdi
callq 0x2a046
movl 0x218(%rbx), %edx
andl $0x1, %edx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x29fda
leaq 0x8(%rbx), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2dea2
movl $0x88, %eax
leaq (%rbx,%rax), %rdi
addq %rax, %r14
movq %r14, %rsi
callq 0x2dea2
leaq 0x28(%rsp), %rdi
movq 0x108(%rdi), %rax
movq %rax, 0x108(%rbx)
callq 0x2a028
movl 0x24c(%rbx), %ebx
jmp 0x3898a
leaq 0x6eeb7(%rip), %rdi     # 0xa781b
jmp 0x38981
leaq 0x6eed2(%rip), %rdi     # 0xa783f
xorl %ebx, %ebx
movl %ebp, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x3898a
leaq 0x6eedd(%rip), %rdi     # 0xa785e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x719d2
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x24c(%rbx), %edx
leaq 0x6eee6(%rip), %rdi     # 0xa7891
xorl %ebx, %ebx
movl %r12d, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x3898a
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2a028
movq %rbx, %rdi
callq 0xe7e0