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

  // Write header
  fprintf(fp, "@relation UGOR\n");

  // Write features
  for (int i = 0; i < NFeatures(); i++) {
    R3SurfelFeature *feature = Feature(i);
    fprintf(fp, "@attribute %s real\n", feature->Name());
  }

  // Write labels
  fprintf(fp, "@attribute Label { ");
  for (int i = 0; i < NLabels(); i++) {
    R3SurfelLabel *label = Label(i);
    if (i != 0) fprintf(fp, ", ");
    fprintf(fp, "%s ", label->Name());
  }
  fprintf(fp, "}\n");

  // Write data
  fprintf(fp, "@data\n");
  for (int i = 1; i < NObjects(); i++) {
    R3SurfelObject *object = Object(i);
    R3SurfelLabel *label = object->GroundTruthLabel();
    if (!label) label = object->HumanLabel();
    const R3SurfelFeatureVector& vector = object->FeatureVector();
    const char *name = (object->Name()) ? object->Name() : "None";
    const R3Point& centroid = object->Centroid();
    fprintf(fp, "%% %s %g %g %g\n", name, centroid.X(), centroid.Y(), centroid.Z());
    for (int j = 0; j < vector.NValues(); j++) 
      fprintf(fp, "%12.6f ", vector.Value(j));
    fprintf(fp, "%s\n", (label) ? label->Name() : "Unknown");;
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x177eeb(%rip), %rsi    # 0x1a6efe
callq 0x13690
movq %rax, 0xe8(%rsp)
cmpq $0x0, %rax
jne 0x2f04c
movq 0xf0(%rsp), %rsi
leaq 0x175471(%rip), %rdi    # 0x1a44a6
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x104(%rsp)
jmp 0x2f421
movq 0xe8(%rsp), %rdi
leaq 0x1758ba(%rip), %rsi    # 0x1a4915
movb $0x0, %al
callq 0x13810
movl $0x0, 0xe4(%rsp)
movq 0x70(%rsp), %rdi
movl 0xe4(%rsp), %eax
movl %eax, 0x6c(%rsp)
callq 0x16c80
movl %eax, %ecx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f0e8
movq 0x70(%rsp), %rdi
movl 0xe4(%rsp), %esi
callq 0x1c120
movq %rax, 0xd8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x30960
movq 0x60(%rsp), %rdi
movq %rax, %rdx
leaq 0x175857(%rip), %rsi    # 0x1a4925
movb $0x0, %al
callq 0x13810
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x2f06d
movq 0xe8(%rsp), %rdi
leaq 0x175842(%rip), %rsi    # 0x1a4939
movb $0x0, %al
callq 0x13810
movl $0x0, 0xd4(%rsp)
movq 0x70(%rsp), %rdi
movl 0xd4(%rsp), %eax
movl %eax, 0x5c(%rsp)
callq 0x16c40
movl %eax, %ecx
movl 0x5c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f1a7
movq 0x70(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x307a0
movq %rax, 0xc8(%rsp)
cmpl $0x0, 0xd4(%rsp)
je 0x2f161
movq 0xe8(%rsp), %rdi
leaq 0x1757f3(%rip), %rsi    # 0x1a494d
movb $0x0, %al
callq 0x13810
movq 0xe8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x30850
movq 0x50(%rsp), %rdi
movq %rax, %rdx
leaq 0x1757c6(%rip), %rsi    # 0x1a4950
movb $0x0, %al
callq 0x13810
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x2f109
movq 0xe8(%rsp), %rdi
leaq 0x17579e(%rip), %rsi    # 0x1a4954
movb $0x0, %al
callq 0x13810
movq 0xe8(%rsp), %rdi
leaq 0x17578b(%rip), %rsi    # 0x1a4957
movb $0x0, %al
callq 0x13810
movl $0x1, 0xc4(%rsp)
movq 0x70(%rsp), %rdi
movl 0xc4(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x16c20
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f409
movq 0x70(%rsp), %rdi
movl 0xc4(%rsp), %esi
callq 0x185f0
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x19f30
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
jne 0x2f24f
movq 0xb8(%rsp), %rdi
callq 0x19f80
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x19790
movq %rax, 0xa8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x1bf70
cmpq $0x0, %rax
je 0x2f28b
movq 0xb8(%rsp), %rdi
callq 0x1bf70
movq %rax, 0x40(%rsp)
jmp 0x2f299
leaq 0x175272(%rip), %rax    # 0x1a4504
movq %rax, 0x40(%rsp)
jmp 0x2f299
movq 0x40(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xb8(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x187e0
leaq 0x80(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x98(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x38(%rsp)
movq 0x98(%rsp), %rdi
callq 0x187c0
movsd %xmm0, 0x30(%rsp)
movq 0x98(%rsp), %rdi
callq 0x187a0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movsd 0x30(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x38(%rsp), %xmm0
leaq 0x175626(%rip), %rsi    # 0x1a495e
movb $0x3, %al
callq 0x13810
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1bc40
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2f3a4
movq 0xe8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x18640
movq 0x10(%rsp), %rdi
leaq 0x1755de(%rip), %rsi    # 0x1a496e
movb $0x1, %al
callq 0x13810
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x2f347
movq 0xe8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x2f3cf
movq 0xb0(%rsp), %rdi
callq 0x30850
movq %rax, (%rsp)
jmp 0x2f3dc
leaq 0x1755a0(%rip), %rax    # 0x1a4976
movq %rax, (%rsp)
jmp 0x2f3dc
movq 0x8(%rsp), %rdi
movq (%rsp), %rdx
leaq 0x17945f(%rip), %rsi    # 0x1a884b
movb $0x0, %al
callq 0x13810
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x2f1de
movq 0xe8(%rsp), %rdi
callq 0x13560
movl $0x1, 0x104(%rsp)
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq