// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x6134e(%rip), %rsi     # 0x9355b
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x322fa
movq %rax, %r15
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32303
movq %rbx, 0x10(%rsp)
movl 0xc(%rsp), %r13d
movl %r13d, %ebx
imull %r13d, %ebx
movl %ebx, %ebp
imull %r13d, %ebp
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x3226a
cmpl 0x24c(%r14), %ebp
jle 0x3229a
testq %r12, %r12
je 0x32277
movq %r12, %rdi
callq 0xe5d0
movslq %ebp, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r12
movq %rax, 0x230(%r14)
movd %r13d, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqu %xmm0, 0x238(%r14)
movl %ebx, 0x248(%r14)
movl %ebp, 0x24c(%r14)
testl %ebp, %ebp
jle 0x32329
xorl %ebx, %ebx
pushq $0x1
popq %r13
cmpl %ebp, %ebx
jge 0x3233e
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3234f
cvtss2sd 0x8(%rsp), %xmm0
movsd %xmm0, (%r12,%rbx,8)
movl 0x24c(%r14), %ebp
incq %rbx
jmp 0x322c5
leaq 0x66b62(%rip), %rdi     # 0x98e63
jmp 0x3230a
leaq 0x66b77(%rip), %rdi     # 0x98e81
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x664bf(%rip), %rdi     # 0x987ef
xorl %r14d, %r14d
movl %ebp, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x32317
movq %r15, %rdi
callq 0xe440
movl 0x24c(%r14), %r14d
jmp 0x32317
leaq 0x66b59(%rip), %rdi     # 0x98eaf
xorl %r14d, %r14d
movq 0x10(%rsp), %rsi
jmp 0x32310