// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelScene.cpp
// Function: gaps::R3SurfelScene::WriteTianqiangFile(char const*)
int R3SurfelScene::
WriteTianqiangFile(const char *filename) 
{
  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open file %s\n", filename);
    return 0;
  }
  
  // Get scene name
  char scene_name_buffer[2048];
  strncpy(scene_name_buffer, filename, 2047);
  char *scene_name = strrchr(scene_name_buffer, '.');
  if (scene_name) *scene_name = '\0';
  scene_name = strrchr(scene_name_buffer, '/');
  if (scene_name) scene_name++;
  else scene_name = scene_name_buffer;

  // Allocate node and leaf index
  int node_count = 0;
  int leaf_count = 0;
  int *node_index = new int [ NObjects() ];
  int *leaf_index = new int [ NObjects() ];
  for (int i = 0; i < NObjects(); i++) node_index[i] = -1;
  for (int i = 0; i < NObjects(); i++) leaf_index[i] = -1;

  // Compute node and leaf index
  RNArray<R3SurfelObject *> index_stack;
  index_stack.Insert(RootObject());
  while (!index_stack.IsEmpty()) {
    // Pop object off stack
    R3SurfelObject *object = index_stack.Tail();
    index_stack.RemoveTail();

    // Push children on stack
    for (int j = 0; j < object->NParts(); j++) {
      index_stack.Insert(object->Part(j));
    }

    // Check object
    if (object->NParts() == 0) {
      if (object->Parent() == RootObject()) {
        if (!object->GroundTruthLabel() && !object->HumanLabel()) {
          continue;
        }
      }
    }

    // Add object to node index
    node_index[object->SceneIndex()] = node_count++;

    // Add object to leaf index
    if (object->NParts() == 0) {
      leaf_index[object->SceneIndex()] = leaf_count++;
    }
  }

  // Create off directory
  char mkdir_command[4096];
  sprintf(mkdir_command, "mkdir -p %s_off_files", scene_name);
  system(mkdir_command);

  // Write off files
  for (int i = 0; i < NObjects(); i++) {
    R3SurfelObject *object = Object(i);
    if (leaf_index[i] < 0) continue;
    char off_filename[4096];
    sprintf(off_filename, "%s_off_files/%d.off", scene_name, leaf_index[i]);
    if (!WriteOffFile(object, off_filename)) return 0;
  }

  // Write header
  fprintf(fp, "root 0\n");
  fprintf(fp, "scene_name %s\n", scene_name);
  
  // Write objects
  RNArray<R3SurfelObject *> object_stack;
  object_stack.Insert(RootObject());
  while (!object_stack.IsEmpty()) {
    // Pop object off stack
    R3SurfelObject *object = object_stack.Tail();
    object_stack.RemoveTail();

    // Push children on stack
    for (int j = 0; j < object->NParts(); j++) {
      object_stack.Insert(object->Part(j));
    }

    // Check object
    if (node_index[object->SceneIndex()] < 0) continue;

    // Write object header
    fprintf(fp, "newModel %d\n", node_index[object->SceneIndex()]);

    // Write parent 
    R3SurfelObject *parent = object->Parent();
    int parent_index = (parent) ? node_index[parent->SceneIndex()] : -1;
    fprintf(fp, "parent %d\n", parent_index);

    // Write children
    fprintf(fp, "children ");
    for (int j = 0; j < object->NParts(); j++) {
      R3SurfelObject *part = object->Part(j);
      if (node_index[part->SceneIndex()] < 0) continue;
      fprintf(fp, "%d ", node_index[part->SceneIndex()]);
    }
    fprintf(fp, "\n");

    // Write leaf group
    fprintf(fp, "leaf_group");
    RNArray<R3SurfelObject *> part_stack;
    part_stack.Insert(object);
    while (!part_stack.IsEmpty()) {
      R3SurfelObject *part = part_stack.Tail();
      part_stack.RemoveTail();
      for (int j = 0; j < part->NParts(); j++) part_stack.Insert(part->Part(j));
      int g = leaf_index[part->SceneIndex()];
      if (g != -1) fprintf(fp, " %d", g);
    }
    fprintf(fp, "\n");
    
    // Write label
    R3SurfelLabel *label = object->GroundTruthLabel();
    if (!label) label = object->HumanLabel();
    if (!label && (object == RootObject())) label = RootLabel();
    if (label) fprintf(fp, "label %d %s\n", label->SceneIndex(), label->Name());;

    // Write feature vector
    // const R3SurfelFeatureVector& vector = object->FeatureVector();
    // fprintf(fp, "feature_vector ");
    // for (int j = 0; j < vector.NValues(); j++) 
    //   fprintf(fp, "%12.6f ", vector.Value(j));
    // fprintf(fp, "\n");
  }
  
  // Delete temporary data
  delete [] node_index;
  delete [] leaf_index;

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x29e8, %rsp           # imm = 0x29E8
movq %rsi, %r14
movq %rdi, %r13
leaq 0xc2ae2(%rip), %rsi     # 0xe42e4
movq %r14, %rdi
callq 0x135d0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x21e70
leaq 0x11e0(%rsp), %rbx
movl $0x7ff, %edx            # imm = 0x7FF
movq %rbx, %rdi
movq %r14, %rsi
callq 0x13280
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x13560
testq %rax, %rax
je 0x21845
movb $0x0, (%rax)
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x13560
leaq 0x1(%rax), %rcx
testq %rax, %rax
cmoveq %rbx, %rcx
movq %rcx, 0x28(%rsp)
movslq 0x1c(%r13), %r15
leaq (,%r15,4), %rbx
testq %r15, %r15
movq $-0x1, %r14
cmovnsq %rbx, %r14
movq %r14, %rdi
callq 0x13030
movq %rax, %r12
movq %r14, %rdi
callq 0x13030
testq %r15, %r15
movq %rax, %r15
jle 0x218b7
movq %r12, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x132a0
movq %r15, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x132a0
movq %r15, 0x10(%rsp)
movq %r12, 0x18(%rsp)
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0xa9ca8
movq 0x10(%r13), %rax
movq (%rax), %rsi
movl 0xc(%rbx), %edx
movq %rbx, %rdi
callq 0xa9d3e
movl 0x3c(%rsp), %eax
testl %eax, %eax
je 0x21990
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbx
xorl %ebp, %ebp
movq 0x30(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r14
decl %esi
movq %rbx, %rdi
callq 0xa9ec8
movl 0x2c(%r14), %eax
testl %eax, %eax
jle 0x21938
xorl %r12d, %r12d
movq 0x20(%r14), %rax
movq (%rax,%r12,8), %rsi
movl 0x3c(%rsp), %edx
movq %rbx, %rdi
callq 0xa9d3e
incq %r12
movslq 0x2c(%r14), %rax
cmpq %rax, %r12
jl 0x21918
testl %eax, %eax
jne 0x21963
movq 0x18(%r14), %rax
movq 0x10(%r13), %rcx
cmpq (%rcx), %rax
jne 0x21963
movq %r14, %rdi
callq 0x17788
testq %rax, %rax
jne 0x21963
movq %r14, %rdi
callq 0x177d6
testq %rax, %rax
je 0x21984
movslq 0x10(%r14), %rax
movq 0x18(%rsp), %rcx
movl %r15d, (%rcx,%rax,4)
incl %r15d
cmpl $0x0, 0x2c(%r14)
jne 0x21984
movq 0x10(%rsp), %rcx
movl %ebp, (%rcx,%rax,4)
incl %ebp
movl 0x3c(%rsp), %eax
testl %eax, %eax
jne 0x218f6
leaq 0xbc003(%rip), %rsi     # 0xdd99a
leaq 0x19e0(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x130e0
movq %rbx, %rdi
callq 0x13420
movl 0x1c(%r13), %eax
testl %eax, %eax
movq %r13, 0x20(%rsp)
movq 0x18(%rsp), %r15
jle 0x21b92
leaq 0xa8(%rsp), %r14
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movl (%rcx,%rdx,4), %ecx
testl %ecx, %ecx
js 0x21b83
movq 0x10(%r13), %rax
movq %rdx, 0x40(%rsp)
movq (%rax,%rdx,8), %rbx
leaq 0x1e0(%rsp), %rdi
leaq 0xbbfae(%rip), %rsi     # 0xdd9b0
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x130e0
xorpd %xmm0, %xmm0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x174a6
movq %rax, %rbx
testq %rax, %rax
je 0x21e85
movq %r14, %rdi
callq 0x54af4
cmpl $0x0, 0x10(%rbx)
jle 0x21b45
movl $0x8, %r15d
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq -0x8(%rax,%r15), %rcx
movq (%rax,%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
leaq 0x90(%rsp), %r12
movq %r12, %rdi
callq 0x83dc2
leaq 0x78(%rsp), %rdi
movq %r12, %rsi
callq 0x83dd2
movsd 0x78(%rsp), %xmm0
addsd 0xbb8f2(%rip), %xmm0    # 0xdd390
movsd %xmm0, 0x78(%rsp)
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x83dd2
movsd 0x60(%rsp), %xmm0
movsd 0xbb8d9(%rip), %xmm1    # 0xdd398
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x83dd2
movsd 0x50(%rsp), %xmm0
addsd 0xbb8b4(%rip), %xmm0    # 0xdd398
movsd %xmm0, 0x50(%rsp)
movq %r14, %rdi
leaq 0x78(%rsp), %rsi
xorl %edx, %edx
callq 0x57f3e
movq %rax, %rbp
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
callq 0x57f3e
movq %rax, %r12
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x57f3e
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x58304
incq %r13
movslq 0x10(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r13
jl 0x21a43
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r14, %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x659f6
movl %eax, %ebx
movq %r14, %rdi
callq 0x55032
testl %ebx, %ebx
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r15
je 0x21e85
movl 0x1c(%r13), %eax
movq 0x40(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x219d6
leaq 0xbbe2b(%rip), %rdi     # 0xdd9c4
movl $0x7, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x139a0
leaq 0xbbe15(%rip), %rsi     # 0xdd9cc
movq %rbx, %rdi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x13740
leaq 0x1e0(%rsp), %rdi
callq 0xa9ca8
movq 0x10(%r13), %rax
movq (%rax), %rsi
leaq 0x1e0(%rsp), %rdi
movl 0xc(%rdi), %edx
callq 0xa9d3e
movl 0x1ec(%rsp), %eax
testl %eax, %eax
je 0x21e40
leaq 0x1e0(%rsp), %r13
leaq 0xbbdd3(%rip), %r12     # 0xdd9db
leaq 0xa8(%rsp), %r14
movq 0x1e0(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %rbp
decl %esi
movq %r13, %rdi
callq 0xa9ec8
cmpl $0x0, 0x2c(%rbp)
jle 0x21c55
xorl %ebx, %ebx
movq 0x20(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movl 0x1ec(%rsp), %edx
movq %r13, %rdi
callq 0xa9d3e
incq %rbx
movslq 0x2c(%rbp), %rax
cmpq %rax, %rbx
jl 0x21c32
movslq 0x10(%rbp), %rax
movl (%r15,%rax,4), %edx
testl %edx, %edx
js 0x21e0e
movq 0x8(%rsp), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x13740
movq 0x18(%rbp), %rax
testq %rax, %rax
je 0x21c87
movslq 0x10(%rax), %rax
movl (%r15,%rax,4), %edx
jmp 0x21c8c
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0xbbd4d(%rip), %rsi     # 0xdd9e8
xorl %eax, %eax
callq 0x13740
movl $0x9, %esi
movl $0x1, %edx
leaq 0xbbd40(%rip), %rdi     # 0xdd9f3
movq %rbx, %rcx
callq 0x139a0
movl 0x2c(%rbp), %eax
testl %eax, %eax
jle 0x21cf9
xorl %ebx, %ebx
movq 0x20(%rbp), %rcx
movq (%rcx,%rbx,8), %rcx
movslq 0x10(%rcx), %rcx
movl (%r15,%rcx,4), %edx
testl %edx, %edx
js 0x21cee
movq 0x8(%rsp), %rdi
leaq 0xbbbbf(%rip), %rsi     # 0xdd8a3
xorl %eax, %eax
callq 0x13740
movl 0x2c(%rbp), %eax
incq %rbx
movslq %eax, %rcx
cmpq %rcx, %rbx
jl 0x21cc4
movl $0xa, %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x13670
movl $0xa, %esi
movl $0x1, %edx
leaq 0xbbce1(%rip), %rdi     # 0xdd9fd
movq %rbx, %rcx
callq 0x139a0
movq %r14, %rdi
callq 0xa9ca8
movl 0xb4(%rsp), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xa9d3e
movl 0xb4(%rsp), %eax
testl %eax, %eax
je 0x21dc3
movq 0xa8(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %rbx
decl %esi
movq %r14, %rdi
callq 0xa9ec8
cmpl $0x0, 0x2c(%rbx)
jle 0x21d8f
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl 0xb4(%rsp), %edx
movq %r14, %rdi
callq 0xa9d3e
incq %r15
movslq 0x2c(%rbx), %rax
cmpq %rax, %r15
jl 0x21d6c
movslq 0x10(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %edx
cmpl $-0x1, %edx
je 0x21db3
movq 0x8(%rsp), %rdi
leaq 0xc675d(%rip), %rsi     # 0xe8509
xorl %eax, %eax
callq 0x13740
movl 0xb4(%rsp), %eax
testl %eax, %eax
movq 0x18(%rsp), %r15
jne 0x21d49
movl $0xa, %edi
movq 0x8(%rsp), %rsi
callq 0x13670
movq %rbp, %rdi
callq 0x17788
testq %rax, %rax
jne 0x21dec
movq %rbp, %rdi
callq 0x177d6
testq %rax, %rax
je 0x21e1f
movl 0x10(%rax), %edx
movq 0x60(%rax), %rcx
movq 0x8(%rsp), %rdi
leaq 0xbbc09(%rip), %rsi     # 0xdda08
xorl %eax, %eax
callq 0x13740
movq %r14, %rdi
callq 0xa9d12
movl 0x1ec(%rsp), %eax
testl %eax, %eax
jne 0x21c10
jmp 0x21e40
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
cmpq (%rax), %rbp
jne 0x21e06
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rax
testq %rax, %rax
jne 0x21dec
jmp 0x21e06
movq %r15, %rdi
callq 0x13680
movq 0x10(%rsp), %rdi
callq 0x13680
movq 0x8(%rsp), %rdi
callq 0x134d0
leaq 0x1e0(%rsp), %rdi
callq 0xa9d12
movl $0x1, %ebx
jmp 0x21e87
leaq 0xbb65b(%rip), %rdi     # 0xdd4d2
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0xac81a
jmp 0x21e91
xorl %ebx, %ebx
leaq 0x30(%rsp), %rdi
callq 0xa9d12
movl %ebx, %eax
addq $0x29e8, %rsp           # imm = 0x29E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x21edb
jmp 0x21ee0
jmp 0x21ee0
jmp 0x21ee0
jmp 0x21ec9
jmp 0x21edb
jmp 0x21ee5
jmp 0x21ee5
jmp 0x21ee0
jmp 0x21edb
jmp 0x21ee0
jmp 0x21ec9
jmp 0x21ec9
jmp 0x21ec9
jmp 0x21ec9
jmp 0x21ec9
jmp 0x21ec9
jmp 0x21ee5
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x55032
jmp 0x21f02
movq %rax, %rbx
jmp 0x21ef5
movq %rax, %rbx
jmp 0x21f02
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0xa9d12
leaq 0x1e0(%rsp), %rdi
callq 0xa9d12
leaq 0x30(%rsp), %rdi
callq 0xa9d12
movq %rbx, %rdi
callq 0x138b0