// File: 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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rdi
leaq 0xcfa15(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x63bf6
movq 0x30(%rsp), %rsi
leaq 0xd0e0f(%rip), %rdi     # 0x1349f1
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x44(%rsp)
jmp 0x63dfb
movq 0x28(%rsp), %rcx
leaq 0x24(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xf780
cmpq $0x1, %rax
je 0x63c35
movq 0x30(%rsp), %rsi
leaq 0xd0dee(%rip), %rdi     # 0x134a0f
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x44(%rsp)
jmp 0x63dfb
movq 0x8(%rsp), %rax
movl 0x24(%rsp), %ecx
imull 0x24(%rsp), %ecx
imull 0x24(%rsp), %ecx
movl %ecx, 0x20(%rsp)
cmpq $0x0, 0x230(%rax)
je 0x63c67
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jle 0x63ccc
movq 0x8(%rsp), %rax
cmpq $0x0, 0x230(%rax)
je 0x63c97
movq 0x8(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x63c95
movq (%rsp), %rdi
callq 0xf670
jmp 0x63c97
movslq 0x20(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x230(%rax)
movq 0x8(%rsp), %rax
movl 0x24(%rsp), %ecx
movl %ecx, 0x238(%rax)
movl 0x24(%rsp), %ecx
movl %ecx, 0x23c(%rax)
movl 0x24(%rsp), %ecx
movl %ecx, 0x240(%rax)
movl 0x238(%rax), %ecx
movl %ecx, 0x244(%rax)
movl 0x244(%rax), %ecx
imull 0x23c(%rax), %ecx
movl %ecx, 0x248(%rax)
movl 0x248(%rax), %ecx
imull 0x240(%rax), %ecx
movl %ecx, 0x24c(%rax)
cmpl $0x0, 0x24c(%rax)
jg 0x63d50
movq 0x8(%rsp), %rax
movl 0x24c(%rax), %esi
leaq 0xd063d(%rip), %rdi     # 0x134379
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x44(%rsp)
jmp 0x63dfb
movq 0x8(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jge 0x63de2
movq 0x28(%rsp), %rcx
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xf780
cmpq $0x1, %rax
je 0x63db6
movq 0x30(%rsp), %rsi
leaq 0xd0c98(%rip), %rdi     # 0x134a3d
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x44(%rsp)
jmp 0x63dfb
movss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x18(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x18(%rsp)
movsd %xmm0, (%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x63d69
movq 0x28(%rsp), %rdi
callq 0xf4b0
movq 0x8(%rsp), %rax
movl 0x24c(%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)