// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: TransformWithConfigurationFile(gaps::R3SurfelScene*, char const*, int)
static int
TransformWithConfigurationFile(R3SurfelScene *scene, const char *filename, RNBoolean invert = FALSE)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  int count = 0;
  
  // Get surfel tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) {
    RNFail("Scene has no surfel tree\n");
    return 0;
  }    

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

  // Parse file
  char buffer[4096];
  int line_number = 0;
  while (fgets(buffer, 4096, fp)) {
    char cmd[4096];
    line_number++;
    if (sscanf(buffer, "%s", cmd) != (unsigned int) 1) continue;
    if (cmd[0] == '#') continue;

    // Check cmd
    if (!strcmp(cmd, "scan") || !strcmp(cmd, "image") || !strcmp(cmd, "frame")) {
      // Parse image name and alignment transformation
      RNScalar m[16], depth_timestamp, color_timestamp;
      char depth_name[1024], color_name[1024];
      if (!strcmp(cmd, "frame")) {
        if (sscanf(buffer, "%s%s%lf%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", cmd, 
           depth_name, &depth_timestamp, color_name, &color_timestamp,
           &m[0], &m[1], &m[2], &m[3], &m[4], &m[5], &m[6], &m[7], 
           &m[8], &m[9], &m[10], &m[11], &m[12], &m[13], &m[14], &m[15]) != (unsigned int) 21) {
          RNFail("Error parsing line %d of %s\n", line_number, filename);
          return 0;
        }
      }
      else {
        if (sscanf(buffer, "%s%s%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", cmd, 
           depth_name, color_name,
           &m[0], &m[1], &m[2], &m[3], &m[4], &m[5], &m[6], &m[7], 
           &m[8], &m[9], &m[10], &m[11], &m[12], &m[13], &m[14], &m[15]) != (unsigned int) 19) {
          RNFail("Error parsing line %d of %s\n", line_number, filename);
          return 0;
        }
      }

      // Get transformation
      R3Affine transformation(R4Matrix(m), 0);
      if (invert) transformation.Invert();

      // Get image name
      char tmp[2048];
      strncpy(tmp, depth_name, 1048);
      char *image_name = strrchr(tmp, '/');
      if (!image_name) image_name = tmp;
      char *endp = strrchr(image_name, '.');
      if (endp) *endp = '\0';
      char node_name[4096];
      sprintf(node_name, "SCAN:%s", image_name);

      // Find node
      R3SurfelNode *node = tree->FindNodeByName(node_name);
      R3SurfelScan *scan = scene->FindScanByName(image_name);
      R3SurfelImage *image = scene->FindImageByName(image_name);
      
      // Transform image
      if (image) {
        R3CoordSystem pose = image->Pose();
        pose.Transform(transformation);
        image->SetPose(pose);
      }
      
      // Transform scan
      if (scan) {
        R3CoordSystem pose = scan->Pose();
        pose.Transform(transformation);
        scan->SetPose(pose);
      }
      
      // Transform node and all its decendents
      if (node) {
        RNArray<R3SurfelNode *> stack;
        stack.Insert(node);
        while (!stack.IsEmpty()) {
          R3SurfelNode *node = stack.Tail();
          stack.RemoveTail();
          node->Transform(transformation);
          for (int i = 0; i < node->NParts(); i++) {
            stack.InsertTail(node->Part(i));
          }
        }
      }

      // Update statistics
      count++;
    }
  }

  // Close configuration file
  fclose(fp);

  // Print statistics
  if (print_verbose) {
    printf("Tranformed nodes ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Nodes = %d\n", count);
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4248, %rsp           # imm = 0x4248
movl %edx, 0x14(%rsp)
movq %rsi, %r15
movq %rdi, %rbp
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
callq 0xc0cf4
xorl %ebx, %ebx
movq %r14, %rdi
xorl %esi, %esi
callq 0x15a10
movq 0x8(%rbp), %rax
movq %rax, 0xa8(%rsp)
testq %rax, %rax
je 0x1d307
leaq 0xf8441(%rip), %rsi     # 0x115273
movq %r15, %rdi
callq 0x15760
testq %rax, %rax
je 0x1d31a
movq %rax, %r12
leaq 0x3240(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x159d0
testq %rax, %rax
je 0x1d332
movq %r15, 0xa0(%rsp)
leaq 0x3240(%rsp), %rdi
leaq 0x130(%rsp), %r15
leaq 0xd0(%rsp), %r13
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
movq %r12, 0x18(%rsp)
incl %eax
movl %eax, 0x10(%rsp)
movq %rdi, %rbx
leaq 0xf3442(%rip), %rsi     # 0x1102e5
leaq 0x640(%rsp), %rdx
xorl %eax, %eax
callq 0x155f0
cmpl $0x1, %eax
jne 0x1d2e5
cmpb $0x23, 0x640(%rsp)
je 0x1d2e5
movl 0x640(%rsp), %eax
movl $0x6e616373, %ecx       # imm = 0x6E616373
xorl %ecx, %eax
movzbl 0x644(%rsp), %ecx
orl %eax, %ecx
movq %rbx, %rdi
je 0x1cf24
movl 0x640(%rsp), %eax
movl $0x67616d69, %ecx       # imm = 0x67616D69
xorl %ecx, %eax
movzwl 0x644(%rsp), %ecx
xorl $0x65, %ecx
orl %eax, %ecx
je 0x1cf24
movl 0x640(%rsp), %eax
movl $0x6d617266, %ecx       # imm = 0x6D617266
xorl %ecx, %eax
movzwl 0x644(%rsp), %ecx
xorl $0x65, %ecx
orl %eax, %ecx
jne 0x1d2e5
movl 0x640(%rsp), %eax
movl $0x6d617266, %ecx       # imm = 0x6D617266
xorl %ecx, %eax
movzwl 0x644(%rsp), %ecx
xorl $0x65, %ecx
orl %eax, %ecx
je 0x1d022
subq $0x8, %rsp
leaq 0xeb417(%rip), %rsi     # 0x108367
leaq 0x648(%rsp), %rdx
leaq 0x248(%rsp), %rcx
leaq 0x1648(%rsp), %r8
leaq 0x28(%rsp), %r14
movq %r14, %r9
xorl %eax, %eax
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
callq 0x155f0
addq $0x80, %rsp
cmpl $0x13, %eax
je 0x1d10e
jmp 0x1d39a
subq $0x8, %rsp
leaq 0xeabb1(%rip), %rsi     # 0x107bde
leaq 0x648(%rsp), %rdx
leaq 0x248(%rsp), %rcx
leaq 0xc0(%rsp), %r8
leaq 0x1648(%rsp), %r9
xorl %eax, %eax
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r14
pushq %r14
leaq 0x138(%rsp), %r10
pushq %r10
callq 0x155f0
addq $0x90, %rsp
cmpl $0x15, %eax
jne 0x1d39a
movq %r15, %r12
leaq 0x1a40(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x8a542
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x8816a
cmpl $0x0, 0x14(%rsp)
je 0x1d145
leaq 0x138(%rsp), %rdi
callq 0x8ac90
movl $0x418, %edx            # imm = 0x418
leaq 0x2a40(%rsp), %r14
movq %r14, %rdi
leaq 0x240(%rsp), %rsi
callq 0x15300
movq %r14, %rdi
movl $0x2f, %esi
callq 0x156a0
movq %rax, %r12
testq %rax, %rax
cmoveq %r14, %r12
movq %r12, %rdi
movl $0x2e, %esi
callq 0x156a0
testq %rax, %rax
je 0x1d18e
movb $0x0, (%rax)
leaq 0x1a40(%rsp), %r14
movq %r14, %rdi
leaq 0xeaa98(%rip), %rsi     # 0x107c38
movq %r12, %rdx
xorl %eax, %eax
callq 0x15110
movq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x3a6a0
movq %rax, %r14
movq %rbp, %rdi
movq %r12, %rsi
callq 0x42a7e
movq %rbp, %rdi
movq %rax, %rbp
movq %rdi, %r15
movq %r12, %rsi
callq 0x42ad4
movq %rax, %r12
testq %rax, %rax
je 0x1d20b
leaq 0x30(%r12), %rsi
movq %r13, %rdi
callq 0x8888e
movq %r13, %rdi
leaq 0x130(%rsp), %rsi
callq 0x88abc
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x58(%rax)
testq %rbp, %rbp
movq 0x18(%rsp), %r12
je 0x1d23e
leaq 0x28(%rbp), %rsi
movq %r13, %rdi
callq 0x8888e
movq %r13, %rdi
leaq 0x130(%rsp), %rsi
callq 0x88abc
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x10(%rax)
testq %r14, %r14
movq %r15, %rbp
leaq 0x130(%rsp), %r15
je 0x1d2d9
movq %r13, %rdi
callq 0xc1290
movl 0xdc(%rsp), %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0xc1326
movl 0xdc(%rsp), %eax
testl %eax, %eax
je 0x1d2cc
movq 0xd0(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r14
decl %esi
movq %r13, %rdi
callq 0xc14b0
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x38(%rax)
cmpl $0x0, 0x3c(%r14)
jle 0x1d26c
xorl %r12d, %r12d
movq 0x30(%r14), %rax
movq (%rax,%r12,8), %rsi
movl 0xdc(%rsp), %edx
movq %r13, %rdi
callq 0xc1326
incq %r12
movslq 0x3c(%r14), %rax
cmpq %rax, %r12
jl 0x1d2a7
jmp 0x1d26c
movq %r13, %rdi
callq 0xc12fa
movq 0x18(%rsp), %r12
incl 0xc(%rsp)
movq %r15, %rdi
callq 0x881b8
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdx
callq 0x159d0
movq %rbx, %rdi
testq %rax, %rax
movl 0x10(%rsp), %eax
jne 0x1ce93
jmp 0x1d33a
leaq 0xea7ad(%rip), %rdi     # 0x107abb
xorl %eax, %eax
callq 0xc3e02
jmp 0x1d3b6
leaq 0xea894(%rip), %rdi     # 0x107bb5
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0xc3e02
jmp 0x1d3b6
movl $0x0, 0xc(%rsp)
movq %r12, %rdi
callq 0x15600
movl $0x1, %ebx
cmpl $0x0, 0x14fa6e(%rip)    # 0x16cdbc
je 0x1d3b6
leaq 0xeab35(%rip), %rdi     # 0x107e8c
callq 0x15970
leaq 0xc0(%rsp), %rdi
callq 0xc0d56
leaq 0xea242(%rip), %rdi     # 0x1075b2
movb $0x1, %al
callq 0x15080
leaq 0xea346(%rip), %rdi     # 0x1076c4
movl 0xc(%rsp), %esi
xorl %eax, %eax
callq 0x15080
movq 0x14ebd0(%rip), %rax    # 0x16bf60
movq (%rax), %rdi
callq 0x15750
jmp 0x1d3b6
leaq 0xea87a(%rip), %rdi     # 0x107c1b
xorl %ebx, %ebx
movl 0x10(%rsp), %esi
movq 0xa0(%rsp), %rdx
xorl %eax, %eax
callq 0xc3e02
movl %ebx, %eax
addq $0x4248, %rsp           # imm = 0x4248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1d3d8
jmp 0x1d3df
jmp 0x1d3d8
jmp 0x1d3d8
jmp 0x1d3d8
jmp 0x1d3d8
jmp 0x1d3d8
movq %rax, %rbx
jmp 0x1d3ef
jmp 0x1d3df
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0xc12fa
leaq 0x130(%rsp), %rdi
callq 0x881b8
movq %rbx, %rdi
callq 0x15ad0