// File: /workspace/github_cpp_makefile_star2_O1/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 0xc3906(%rip), %rsi     # 0xe52f8
movq %r14, %rdi
callq 0x135d0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x22074
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 0x21a35
movb $0x0, (%rax)
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x13560
leaq 0x1(%rax), %rcx
testq %rax, %rax
cmoveq %rbx, %rcx
movq %rcx, 0x20(%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
movq %rax, 0x10(%rsp)
testq %r15, %r15
jle 0x21a99
movq %r12, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x132a0
movq %r12, 0x18(%rsp)
testl %r15d, %r15d
jle 0x21ab5
movq 0x10(%rsp), %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x132a0
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0xaaf34
movq 0x10(%r13), %rax
movq (%rax), %rsi
movl 0xc(%rbx), %edx
movq %rbx, %rdi
callq 0xaafca
movl 0x34(%rsp), %eax
testl %eax, %eax
je 0x21b88
xorl %r15d, %r15d
leaq 0x28(%rsp), %rbx
xorl %ebp, %ebp
movq 0x28(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r14
decl %esi
movq %rbx, %rdi
callq 0xab13e
movl 0x2c(%r14), %eax
testl %eax, %eax
jle 0x21b2c
xorl %r12d, %r12d
movq 0x20(%r14), %rax
movq (%rax,%r12,8), %rsi
movl 0x34(%rsp), %edx
movq %rbx, %rdi
callq 0xaafca
incq %r12
movslq 0x2c(%r14), %rax
cmpq %rax, %r12
jl 0x21b0c
testl %eax, %eax
jne 0x21b57
movq 0x18(%r14), %rax
movq 0x10(%r13), %rcx
cmpq (%rcx), %rax
jne 0x21b57
movq %r14, %rdi
callq 0x17824
testq %rax, %rax
jne 0x21b57
movq %r14, %rdi
callq 0x17876
testq %rax, %rax
je 0x21b7c
movslq 0x10(%r14), %rax
movq 0x18(%rsp), %rcx
movl %r15d, (%rcx,%rax,4)
incl %r15d
cmpl $0x0, 0x2c(%r14)
jne 0x21b7c
movslq 0x10(%r14), %rax
movq 0x10(%rsp), %rcx
movl %ebp, (%rcx,%rax,4)
incl %ebp
movl 0x34(%rsp), %eax
testl %eax, %eax
jne 0x21aea
leaq 0xbce0b(%rip), %rsi     # 0xde99a
leaq 0x19e0(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x130e0
movq %rbx, %rdi
callq 0x13420
cmpl $0x0, 0x1c(%r13)
movq %r13, 0x38(%rsp)
movq 0x18(%rsp), %r15
jle 0x21d9e
leaq 0xa8(%rsp), %r14
xorl %edx, %edx
movq 0x10(%rsp), %rax
movl (%rax,%rdx,4), %ecx
testl %ecx, %ecx
movq %rdx, 0x40(%rsp)
js 0x21d70
movq 0x10(%r13), %rax
movq (%rax,%rdx,8), %rbx
leaq 0x1e0(%rsp), %rdi
leaq 0xbcdb7(%rip), %rsi     # 0xde9b0
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x130e0
xorpd %xmm0, %xmm0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x174e6
movq %rax, %rbx
testq %rax, %rax
je 0x21d77
movq %r14, %rdi
callq 0x55550
cmpl $0x0, 0x10(%rbx)
jle 0x21d3c
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 0x84d7e
leaq 0x78(%rsp), %rdi
movq %r12, %rsi
callq 0x84d8e
movsd 0x78(%rsp), %xmm0
addsd 0xbc6fb(%rip), %xmm0    # 0xde390
movsd %xmm0, 0x78(%rsp)
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x84d8e
movsd 0x60(%rsp), %xmm0
movsd 0xbc6e2(%rip), %xmm1    # 0xde398
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x84d8e
movsd 0x50(%rsp), %xmm0
addsd 0xbc6bd(%rip), %xmm0    # 0xde398
movsd %xmm0, 0x50(%rsp)
movq %r14, %rdi
leaq 0x78(%rsp), %rsi
xorl %edx, %edx
callq 0x5892a
movq %rax, %rbp
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
callq 0x5892a
movq %rax, %r12
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x5892a
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x58cf0
incq %r13
movslq 0x10(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r13
jl 0x21c3a
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r14, %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x666ae
xorl %ebx, %ebx
testl %eax, %eax
sete %bl
movq %r14, %rdi
callq 0x55a7e
movq 0x38(%rsp), %r13
movq 0x18(%rsp), %r15
jmp 0x21d7c
movl $0xf, %ebx
jmp 0x21d7c
movl $0x1, %ebx
cmpl $0xf, %ebx
je 0x21d89
testl %ebx, %ebx
jne 0x22089
movq 0x40(%rsp), %rdx
incq %rdx
movslq 0x1c(%r13), %rax
cmpq %rax, %rdx
jl 0x21bcd
leaq 0xbcc1f(%rip), %rdi     # 0xde9c4
movl $0x7, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x139a0
leaq 0xbcc09(%rip), %rsi     # 0xde9cc
movq %rbx, %rdi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x13740
leaq 0x1e0(%rsp), %rdi
callq 0xaaf34
movq 0x10(%r13), %rax
movq (%rax), %rsi
leaq 0x1e0(%rsp), %rdi
movl 0xc(%rdi), %edx
callq 0xaafca
movl 0x1ec(%rsp), %eax
testl %eax, %eax
je 0x22044
leaq 0x1e0(%rsp), %r13
leaq 0xbcbc7(%rip), %r12     # 0xde9db
leaq 0xa8(%rsp), %r14
movq 0x1e0(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %rbp
decl %esi
movq %r13, %rdi
callq 0xab13e
cmpl $0x0, 0x2c(%rbp)
jle 0x21e61
xorl %ebx, %ebx
movq 0x20(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movl 0x1ec(%rsp), %edx
movq %r13, %rdi
callq 0xaafca
incq %rbx
movslq 0x2c(%rbp), %rax
cmpq %rax, %rbx
jl 0x21e3e
movslq 0x10(%rbp), %rax
movl (%r15,%rax,4), %edx
testl %edx, %edx
js 0x22035
movq 0x8(%rsp), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x13740
movq 0x18(%rbp), %rax
testq %rax, %rax
je 0x21e93
movslq 0x10(%rax), %rax
movl (%r15,%rax,4), %edx
jmp 0x21e98
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0xbcb41(%rip), %rsi     # 0xde9e8
xorl %eax, %eax
callq 0x13740
movl $0x9, %esi
movl $0x1, %edx
leaq 0xbcb34(%rip), %rdi     # 0xde9f3
movq %rbx, %rcx
callq 0x139a0
cmpl $0x0, 0x2c(%rbp)
jle 0x21f02
xorl %ebx, %ebx
movq 0x20(%rbp), %rax
movq (%rax,%rbx,8), %rax
movslq 0x10(%rax), %rax
movl (%r15,%rax,4), %edx
testl %edx, %edx
js 0x21ef6
movq 0x8(%rsp), %rdi
leaq 0xbc9b4(%rip), %rsi     # 0xde8a3
xorl %eax, %eax
callq 0x13740
incq %rbx
movslq 0x2c(%rbp), %rax
cmpq %rax, %rbx
jl 0x21ecf
movl $0xa, %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x13670
movl $0xa, %esi
movl $0x1, %edx
leaq 0xbcad8(%rip), %rdi     # 0xde9fd
movq %rbx, %rcx
callq 0x139a0
movq %r14, %rdi
callq 0xaaf34
movl 0xb4(%rsp), %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaafca
movl 0xb4(%rsp), %eax
testl %eax, %eax
je 0x21fcc
movq 0xa8(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %rbx
decl %esi
movq %r14, %rdi
callq 0xab13e
cmpl $0x0, 0x2c(%rbx)
jle 0x21f98
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl 0xb4(%rsp), %edx
movq %r14, %rdi
callq 0xaafca
incq %r15
movslq 0x2c(%rbx), %rax
cmpq %rax, %r15
jl 0x21f75
movslq 0x10(%rbx), %rax
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %edx
cmpl $-0x1, %edx
je 0x21fbc
movq 0x8(%rsp), %rdi
leaq 0xc7544(%rip), %rsi     # 0xe94f9
xorl %eax, %eax
callq 0x13740
movl 0xb4(%rsp), %eax
testl %eax, %eax
movq 0x18(%rsp), %r15
jne 0x21f52
movl $0xa, %edi
movq 0x8(%rsp), %rsi
callq 0x13670
movq %rbp, %rdi
callq 0x17824
testq %rax, %rax
jne 0x21ff0
movq %rbp, %rdi
callq 0x17876
testq %rax, %rax
jne 0x2200e
movq 0x38(%rsp), %rcx
movq 0x10(%rcx), %rax
cmpq (%rax), %rbp
je 0x22007
xorl %eax, %eax
jmp 0x2200e
movq 0x20(%rcx), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x2202d
movl 0x10(%rax), %edx
movq 0x60(%rax), %rcx
movq 0x8(%rsp), %rdi
leaq 0xbc9e2(%rip), %rsi     # 0xdea08
xorl %eax, %eax
callq 0x13740
movq %r14, %rdi
callq 0xaaf9e
movl 0x1ec(%rsp), %eax
testl %eax, %eax
jne 0x21e1c
movq %r15, %rdi
callq 0x13680
movq 0x10(%rsp), %rdi
callq 0x13680
movq 0x8(%rsp), %rdi
callq 0x134d0
leaq 0x1e0(%rsp), %rdi
callq 0xaaf9e
movl $0x1, %ebx
jmp 0x2208b
leaq 0xbc457(%rip), %rdi     # 0xde4d2
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0xaddb2
jmp 0x22095
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
callq 0xaaf9e
movl %ebx, %eax
addq $0x29e8, %rsp           # imm = 0x29E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x220df
jmp 0x220e4
jmp 0x220e4
jmp 0x220e4
jmp 0x220cd
jmp 0x220df
jmp 0x220e9
jmp 0x220e9
jmp 0x220e4
jmp 0x220df
jmp 0x220e4
jmp 0x220cd
jmp 0x220cd
jmp 0x220cd
jmp 0x220cd
jmp 0x220cd
jmp 0x220cd
jmp 0x220e9
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x55a7e
jmp 0x22106
movq %rax, %rbx
jmp 0x220f9
movq %rax, %rbx
jmp 0x22106
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0xaaf9e
leaq 0x1e0(%rsp), %rdi
callq 0xaaf9e
leaq 0x28(%rsp), %rdi
callq 0xaaf9e
movq %rbx, %rdi
callq 0x138b0