// File: tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: OutputBlobs(gaps::R3SurfelScene*, char const*)
static int
OutputBlobs(R3SurfelScene *scene, const char *directory_name)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Outputing blobs to %s ...\n", directory_name);
    fflush(stdout);
  }

  // Create directory
  char cmd[1024];
  sprintf(cmd, "mkdir -p %s", directory_name);
  system(cmd);

  // Output blob file for each object
  for (int i = 0; i < scene->NObjects(); i++) {
    R3SurfelObject *object = scene->Object(i);

    // Get object label
    R3SurfelLabel *label = object->GroundTruthLabel();
    if (!label) label = object->HumanLabel();
    int label_identifier = (label) ? label->Identifier() : 0;

    // Create pointset
    R3SurfelPointSet *pointset = object->PointSet();
    if (!pointset) continue;
    if (pointset->NPoints() == 0) { 
      delete pointset; 
      continue; 
    }

    // Create filename
    char filename[1024];
    R3Point centroid = object->Centroid();
    sprintf(filename, "%s/%d_%.3f_%.3f_%.3f.xyz", directory_name,
      label_identifier, centroid.X(), centroid.Y(), centroid.Z());

    // Open file
    FILE *fp = fopen(filename, "w");
    if (!fp) {
      RNFail("Unable to open xyz file %s\n", filename);
      delete pointset;
      return 0;
    }

    // Write points to file
    for (int j = 0; j < pointset->NPoints(); j++) {
      R3SurfelPoint *point = pointset->Point(j);
      R3Point position = point->Position();
      // RNRgb rgb = point->Rgb();
      fprintf(fp, "%.6f %.6f %.6f\n", position.X(), position.Y(), position.Z());
    }

    // Close file
    fclose(fp);

    // Print message
    if (print_debug) {
      printf("%3d %8.3f %8.3f %8.3f : %6d %g\n", label_identifier, 
        centroid.X(), centroid.Y(), centroid.Z(),
        pointset->NPoints(), object->Complexity());
    }

    // Delete pointset
    delete pointset;
  }

  // Print statistics
  if (print_verbose) {
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Blobs = %d\n", scene->NObjects());
    fflush(stdout);
  }
  
  // Return success
  return 1;
}

// Assembly:
subq $0x938, %rsp            # imm = 0x938
movq %rdi, 0x928(%rsp)
movq %rsi, 0x920(%rsp)
leaq 0x910(%rsp), %rdi
callq 0x16dd30
leaq 0x910(%rsp), %rdi
callq 0x263f0
cmpl $0x0, 0x23f408(%rip)    # 0x2647c0
je 0x253df
movq 0x920(%rsp), %rsi
leaq 0x1d3b8e(%rip), %rdi    # 0x1f8f57
movb $0x0, %al
callq 0x16090
movq 0x235bb9(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
leaq 0x510(%rsp), %rdi
movq 0x920(%rsp), %rdx
leaq 0x1d3b7c(%rip), %rsi    # 0x1f8f72
movb $0x0, %al
callq 0x16130
leaq 0x510(%rsp), %rdi
callq 0x16630
movl $0x0, 0x50c(%rsp)
movl 0x50c(%rsp), %eax
movl %eax, 0x94(%rsp)
movq 0x928(%rsp), %rdi
callq 0x266c0
movl %eax, %ecx
movl 0x94(%rsp), %eax
cmpl %ecx, %eax
jge 0x257f1
movq 0x928(%rsp), %rdi
movl 0x50c(%rsp), %esi
callq 0x29d70
movq %rax, 0x500(%rsp)
movq 0x500(%rsp), %rdi
callq 0x6c340
movq %rax, 0x4f8(%rsp)
cmpq $0x0, 0x4f8(%rsp)
jne 0x25492
movq 0x500(%rsp), %rdi
callq 0x6c390
movq %rax, 0x4f8(%rsp)
cmpq $0x0, 0x4f8(%rsp)
je 0x254b3
movq 0x4f8(%rsp), %rdi
callq 0x6f0e0
movl %eax, 0x90(%rsp)
jmp 0x254be
xorl %eax, %eax
movl %eax, 0x90(%rsp)
jmp 0x254be
movl 0x90(%rsp), %eax
movl %eax, 0x4f4(%rsp)
movq 0x500(%rsp), %rdi
xorl %esi, %esi
xorps %xmm0, %xmm0
callq 0x6bee0
movq %rax, 0x4e8(%rsp)
cmpq $0x0, 0x4e8(%rsp)
jne 0x254f6
jmp 0x257db
movq 0x4e8(%rsp), %rdi
callq 0x29df0
cmpl $0x0, %eax
jne 0x25531
movq 0x4e8(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, %rax
je 0x2552c
movq 0x88(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x257db
movq 0x500(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x2b1a0
leaq 0xe0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x920(%rsp), %rax
movq %rax, 0x68(%rsp)
movl 0x4f4(%rsp), %eax
movl %eax, 0x74(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x26ee0
movsd %xmm0, 0x80(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x26ef0
movsd %xmm0, 0x78(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x26e50
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rdx
movl 0x74(%rsp), %ecx
movsd 0x78(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x80(%rsp), %xmm0
leaq 0x1d39b6(%rip), %rsi    # 0x1f8f7e
movb $0x3, %al
callq 0x16130
leaq 0xe0(%rsp), %rdi
leaq 0x1d71f0(%rip), %rsi    # 0x1fc7ce
callq 0x168d0
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0xc0(%rsp)
jne 0x2563a
leaq 0xe0(%rsp), %rsi
leaq 0x1d3992(%rip), %rdi    # 0x1f8f97
movb $0x0, %al
callq 0x172cb0
movq 0x4e8(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x2562a
movq 0x58(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x0, 0x934(%rsp)
jmp 0x2584c
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x4e8(%rsp), %rdi
callq 0x29df0
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x25714
movq 0x4e8(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x29e10
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x29e30
movq 0xc0(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x26ee0
movsd %xmm0, 0x48(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x26ef0
movsd %xmm0, 0x40(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x26e50
movq 0x38(%rsp), %rdi
movsd 0x40(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x48(%rsp), %xmm0
leaq 0x1d38bc(%rip), %rsi    # 0x1f8fb3
movb $0x3, %al
callq 0x16ab0
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x25645
movq 0xc0(%rsp), %rdi
callq 0x16730
cmpl $0x0, 0x23f09c(%rip)    # 0x2647c4
je 0x257bb
movl 0x4f4(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x26ee0
movsd %xmm0, 0x30(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x26ef0
movsd %xmm0, 0x18(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x26e50
movsd %xmm0, 0x20(%rsp)
movq 0x4e8(%rsp), %rdi
callq 0x29df0
movl %eax, 0x2c(%rsp)
movq 0x500(%rsp), %rdi
callq 0x2b1e0
movl 0x14(%rsp), %esi
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movl 0x2c(%rsp), %edx
movaps %xmm0, %xmm3
movsd 0x30(%rsp), %xmm0
leaq 0x1d380f(%rip), %rdi    # 0x1f8fc3
movb $0x4, %al
callq 0x16090
movq 0x4e8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x257d9
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x257db
movl 0x50c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50c(%rsp)
jmp 0x25415
cmpl $0x0, 0x23efc8(%rip)    # 0x2647c0
je 0x25841
leaq 0x910(%rsp), %rdi
callq 0x16ddf0
leaq 0x1d2d71(%rip), %rdi    # 0x1f857f
movb $0x1, %al
callq 0x16090
movq 0x928(%rsp), %rdi
callq 0x266c0
movl %eax, %esi
leaq 0x1d37b8(%rip), %rdi    # 0x1f8fe3
movb $0x0, %al
callq 0x16090
movq 0x235757(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
movl $0x1, 0x934(%rsp)
movl 0x934(%rsp), %eax
addq $0x938, %rsp            # imm = 0x938
retq
nopl (%rax,%rax)