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

  // Write magic string
  unsigned char magic[] = { 0x93, 'N', 'U', 'M', 'P', 'Y' };
  unsigned int magic_length = sizeof(magic) / sizeof(unsigned char);
  if (fwrite(magic, 1, magic_length, fp) != magic_length) {
    RNFail("Unable to write first line to numpy file");
    return 0;
  }

  // Write version
  unsigned char version[] = { 0x01, 0x00 };
  unsigned int version_length = sizeof(version) / sizeof(unsigned char);
  if (fwrite(version, 1, version_length, fp) != version_length) {
    RNFail("Unable to write version to numpy file");
    return 0;
  }

  // Create header
  char header[1024];
  sprintf(header, "{'descr': '<f4', 'fortran_order': False, 'shape': (%d, %d), }", mesh->NVertices(), NProperties());
  unsigned int header_length = strlen(header);
 
  // Write header length
  if (fwrite(&header_length, sizeof(unsigned short), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to write header length to numpy file");
    return 0;
  }

  // Write header
  if (fwrite(header, sizeof(char), header_length, fp) != header_length) {
    RNFail("Unable to write header to numpy file");
    return 0;
  }

  // Write values
  for (int i = 0; i < mesh->NVertices(); i++) {
    for (int j = 0; j < NProperties(); j++) {
      RNScalar32 value = Property(j)->VertexValue(i);
      if (fwrite(&value, sizeof(RNScalar32), 1, fp) != (unsigned int) 1) {
        RNFail("Unable to write value to binary file: %s\n", filename);
        return 0;
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x468, %rsp            # imm = 0x468
movq %rdi, 0x458(%rsp)
movq %rsi, 0x450(%rsp)
movq 0x458(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x450(%rsp), %rdi
leaq 0x12cb58(%rip), %rsi    # 0x19832b
callq 0x22bf0
movq %rax, 0x448(%rsp)
cmpq $0x0, 0x448(%rsp)
jne 0x6b811
movq 0x450(%rsp), %rsi
leaq 0x12c7dd(%rip), %rdi    # 0x197fd7
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6baae
movl 0x12cc9d(%rip), %eax    # 0x1984b4
movl %eax, 0x442(%rsp)
movw 0x12cc93(%rip), %ax     # 0x1984b8
movw %ax, 0x446(%rsp)
movl $0x6, 0x43c(%rsp)
leaq 0x442(%rsp), %rdi
movl 0x43c(%rsp), %eax
movl %eax, %edx
movq 0x448(%rsp), %rcx
movl $0x1, %esi
callq 0x23450
movl 0x43c(%rsp), %ecx
cmpq %rcx, %rax
je 0x6b885
leaq 0x12cac0(%rip), %rdi    # 0x19832e
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6baae
movw 0x12cacb(%rip), %ax     # 0x198357
movw %ax, 0x43a(%rsp)
movl $0x2, 0x434(%rsp)
leaq 0x43a(%rsp), %rdi
movl 0x434(%rsp), %eax
movl %eax, %edx
movq 0x448(%rsp), %rcx
movl $0x1, %esi
callq 0x23450
movl 0x434(%rsp), %ecx
cmpq %rcx, %rax
je 0x6b8ec
leaq 0x12ca84(%rip), %rdi    # 0x198359
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6baae
movq 0x18(%rsp), %rax
leaq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movq 0x18(%rsp), %rdi
movl %eax, 0x14(%rsp)
callq 0x2ae90
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %edx
movl %eax, %ecx
leaq 0x12ca5c(%rip), %rsi    # 0x19837f
movb $0x0, %al
callq 0x22150
leaq 0x30(%rsp), %rdi
callq 0x223c0
movl %eax, 0x2c(%rsp)
movq 0x448(%rsp), %rcx
leaq 0x2c(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x23450
cmpq $0x1, %rax
je 0x6b978
leaq 0x12ca5c(%rip), %rdi    # 0x1983bd
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6baae
leaq 0x30(%rsp), %rdi
movl 0x2c(%rsp), %eax
movl %eax, %edx
movq 0x448(%rsp), %rcx
movl $0x1, %esi
callq 0x23450
movl 0x2c(%rsp), %ecx
cmpq %rcx, %rax
je 0x6b9bc
leaq 0x12ca44(%rip), %rdi    # 0x1983e9
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6baae
movl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rax
movl 0x28(%rsp), %ecx
movl %ecx, 0x4(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x6ba96
movl $0x0, 0x24(%rsp)
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
callq 0x2ae90
movl %eax, %ecx
movl (%rsp), %eax
cmpl %ecx, %eax
jge 0x6ba84
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0x2b330
movq %rax, %rdi
movl 0x28(%rsp), %esi
callq 0x2b0a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x448(%rsp), %rcx
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x23450
cmpq $0x1, %rax
je 0x6ba72
movq 0x450(%rsp), %rsi
leaq 0x12c9b0(%rip), %rdi    # 0x19840e
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6baae
jmp 0x6ba74
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x6b9ef
jmp 0x6ba86
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x6b9c4
movq 0x448(%rsp), %rdi
callq 0x229c0
movl $0x1, 0x464(%rsp)
movl 0x464(%rsp), %eax
addq $0x468, %rsp            # imm = 0x468
retq
nopl (%rax)