// File: /workspace/github_cpp_makefile_star2_O3/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 0x6c1e0(%rip), %rsi     # 0xa25ab
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x364d1
movq %rax, %r15
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x364da
movq %rbx, 0x10(%rsp)
movl 0xc(%rsp), %ebx
movl %ebx, %r13d
imull %ebx, %r13d
movl %r13d, %ebp
imull %ebx, %ebp
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x3642b
cmpl 0x24c(%r14), %ebp
jle 0x3645f
testq %r12, %r12
je 0x36438
movq %r12, %rdi
callq 0xe5b0
movslq %ebp, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r12
movq %rax, 0x230(%r14)
movd %ebx, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqu %xmm0, 0x238(%r14)
movl %r13d, 0x248(%r14)
movl %ebp, 0x24c(%r14)
testl %ebp, %ebp
jle 0x36500
xorl %ebx, %ebx
leaq 0x8(%rsp), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36515
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rsp), %xmm0
movsd %xmm0, (%r12,%rbx,8)
incq %rbx
cmpl 0x24c(%r14), %ebx
jl 0x3648a
movq %r15, %rdi
callq 0xe430
movl 0x24c(%r14), %r14d
jmp 0x364ee
leaq 0x719db(%rip), %rdi     # 0xa7eb3
jmp 0x364e1
leaq 0x719f0(%rip), %rdi     # 0xa7ed1
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x719d2
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x71338(%rip), %rdi     # 0xa783f
xorl %r14d, %r14d
movl %ebp, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x364ee
leaq 0x719e3(%rip), %rdi     # 0xa7eff
xorl %r14d, %r14d
movq 0x10(%rsp), %rsi
jmp 0x364e7