// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadVoxelFile(char const*)
int R3Grid::
ReadVoxelFile(const char *filename)
{
  // Voxel grid file as defined by Misha Kazhdan
  // One binary int N for resolution followed by NxNxN binary floats 

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

  // Read resolution
  int res;
  if (fread(&res, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read resolution from voxel file: %s", filename);
    return 0;
  }
  // Re-allocate grid values
  int new_size = res * res * res;
  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;
  grid_resolution[1] = res;
  grid_resolution[2] = res;
  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_size; k++) {
    float value;
    if (fread(&value, sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read value from voxel file: %s", filename);
      return 0;
    }
    *(grid_valuesp++) = (RNScalar) value;
  }

  // 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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x6bfd4(%rip), %rsi     # 0xa25ae
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x36708
movq %rax, %r15
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36711
movl 0x4(%rsp), %r13d
movl %r13d, %r12d
imull %r13d, %r12d
movl %r12d, %ebp
imull %r13d, %ebp
movq 0x230(%r14), %rdi
testq %rdi, %rdi
je 0x36637
cmpl 0x24c(%r14), %ebp
jle 0x36665
testq %rdi, %rdi
je 0x36641
callq 0xe5b0
movslq %ebp, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0x230(%r14)
movd %r13d, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqu %xmm0, 0x238(%r14)
movl %r12d, 0x248(%r14)
movl %ebp, 0x24c(%r14)
testl %ebp, %ebp
jle 0x36737
cmpl $0x0, 0x24c(%r14)
jle 0x366f7
movq 0x230(%r14), %r13
xorl %r12d, %r12d
movl $0x4, %esi
movl $0x1, %edx
movq %rsp, %rdi
movq %r15, %rcx
callq 0xe6c0
movq %rax, %rbp
cmpq $0x1, %rax
jne 0x366d4
xorps %xmm0, %xmm0
cvtss2sd (%rsp), %xmm0
movsd %xmm0, (%r13)
addq $0x8, %r13
jmp 0x366e5
leaq 0x71828(%rip), %rdi     # 0xa7f03
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
cmpq $0x1, %rbp
jne 0x3674c
incl %r12d
cmpl 0x24c(%r14), %r12d
jl 0x366a2
movq %r15, %rdi
callq 0xe430
movl 0x24c(%r14), %r14d
jmp 0x36725
leaq 0x717a8(%rip), %rdi     # 0xa7eb7
jmp 0x36718
leaq 0x717bd(%rip), %rdi     # 0xa7ed5
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x71105(%rip), %rdi     # 0xa7843
xorl %r14d, %r14d
movl %ebp, %esi
xorl %eax, %eax
callq 0x72a1e
jmp 0x36725
xorl %r14d, %r14d
jmp 0x36725
nop