// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshPropertySet.cpp
// Function: gaps::R3MeshPropertySet::ReadBinary(char const*)
int R3MeshPropertySet::
ReadBinary(const char *filename)
{
  // Just checking
  if (!mesh) return 0;
  
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open binary file: %s\n", filename);
    return 0;
  }

  // Read number of vertices
  int vertex_count = 0;
  if (fread(&vertex_count, sizeof(int), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to read binary file: %s\n", filename);
    return 0;
  }

  // Read number of properties
  int property_count = 0;
  if (fread(&property_count, sizeof(int), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to read binary file: %s\n", filename);
    return 0;
  }
  
  // Check number of vertices
  if (vertex_count != mesh->NVertices()) {
    RNFail("Mismatching number of vertices in %s\n", filename);
    return 0;
  }
  
  // Check number of properties
  if (property_count == 0) return 1;


  // Read property names and create properties
  char property_name[128];
  for (int j = 0; j < property_count; j++) {
    fread(property_name, sizeof(char), 128, fp);
    R3MeshProperty *property = new R3MeshProperty(mesh, property_name);
    Insert(property);
  }

  // Read data and assign property values
  RNScalar32 value;
  for (int i = 0; i < vertex_count; i++) {
    for (int j = 0; j < property_count; j++) {
      fread(&value, sizeof(RNScalar32), 1, fp);
      Property(j)->SetVertexValue(i, value);
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, (%rax)
jne 0x6a99a
movl $0x0, 0xf4(%rsp)
jmp 0x6ac45
movq 0xe0(%rsp), %rdi
leaq 0x12baa0(%rip), %rsi    # 0x196449
callq 0x22bf0
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x6a9e7
movq 0xe0(%rsp), %rsi
leaq 0x12d762(%rip), %rdi    # 0x198132
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0xf4(%rsp)
jmp 0x6ac45
movl $0x0, 0xd4(%rsp)
movq 0xd8(%rsp), %rcx
leaq 0xd4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22fd0
cmpq $0x1, %rax
je 0x6aa3d
movq 0xe0(%rsp), %rsi
leaq 0x12d72c(%rip), %rdi    # 0x198152
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0xf4(%rsp)
jmp 0x6ac45
movl $0x0, 0xd0(%rsp)
movq 0xd8(%rsp), %rcx
leaq 0xd0(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22fd0
cmpq $0x1, %rax
je 0x6aa93
movq 0xe0(%rsp), %rsi
leaq 0x12d6d6(%rip), %rdi    # 0x198152
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0xf4(%rsp)
jmp 0x6ac45
movq 0x20(%rsp), %rax
movl 0xd4(%rsp), %ecx
movl %ecx, 0x1c(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
je 0x6aadb
movq 0xe0(%rsp), %rsi
leaq 0x12d6ae(%rip), %rdi    # 0x198172
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0xf4(%rsp)
jmp 0x6ac45
cmpl $0x0, 0xd0(%rsp)
jne 0x6aaf5
movl $0x1, 0xf4(%rsp)
jmp 0x6ac45
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jge 0x6aba7
movq 0xd8(%rsp), %rcx
leaq 0x50(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movl $0x1, %esi
movl $0x80, %edx
callq 0x22fd0
movl $0x448, %edi            # imm = 0x448
callq 0x229d0
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq 0x20(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x10(%rsp)
movq (%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
callq 0x6c2b0
jmp 0x6ab5c
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rsi
callq 0x69220
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x6aafd
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x229f0
jmp 0x6ac54
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0xd4(%rsp), %eax
jge 0x6ac2d
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jge 0x6ac1e
movq 0xd8(%rsp), %rcx
leaq 0x30(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22fd0
movq 0x20(%rsp), %rdi
movl 0x28(%rsp), %esi
callq 0x2b330
movq %rax, %rdi
movl 0x2c(%rsp), %esi
movss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x6f210
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x6abc4
jmp 0x6ac20
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x6abaf
movq 0xd8(%rsp), %rdi
callq 0x229c0
movl $0x1, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
movq 0x38(%rsp), %rdi
callq 0x23270
nop