// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelBlock.cpp
// Function: gaps::R3SurfelBlock::ReadXYZAscii(_IO_FILE*)
int R3SurfelBlock::
ReadXYZAscii(FILE *fp)
{
  // Get original file offset
  long int file_offset = RNFileTell(fp);

  // Count the number of surfels
  int count = 0;
  char buffer[4096];
  while (fgets(buffer, 4096, fp)) {
    // Check if blank line
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!*bufferp) continue;
    count++;
  }

  // Rewind file to original file offset
  RNFileSeek(fp, file_offset, RN_FILE_SEEK_SET);

  // Allocate surfels
  surfels = new R3Surfel [ count ];
  if (!surfels) {
    RNFail("Unable to allocate surfel block\n");
    return 0;
  }

  // Read surfels
  nsurfels = 0;
  while (fgets(buffer, 4096, fp)) {
    // Check if blank line
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!*bufferp) continue;

    // Check number of surfels
    if (nsurfels >= count) break;

    // Parse surfel data
    float x, y, z;
    unsigned int r, g, b;
    if (sscanf(buffer, "%f%f%f%u%u%u", &x, &y, &z, &r, &g, &b) != (unsigned int) 6) {
      r = 255; g = 0; b = 0;
      if (sscanf(buffer, "%f%f%f", &x, &y, &z) != (unsigned int) 3) {
        RNFail("Unable to read point %d out of %d into surfel block\n", nsurfels, count);
        delete [] surfels;
        surfels = NULL;
        nsurfels = 0;
        return 0;
      }
    }

    // Assign surfel
    surfels[nsurfels].SetPosition(x, y, z);
    surfels[nsurfels].SetColor(r, g, b);
    nsurfels++;
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq %rsi, %rdi
callq 0xabc6b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
xorl %ebp, %ebp
testq %rax, %rax
je 0x2f280
leaq 0x31(%rsp), %r15
leaq 0x30(%rsp), %r12
movb 0x30(%rsp), %cl
movq %r15, %r13
movl $0x0, %edx
testb %cl, %cl
je 0x2f269
movsbl %cl, %edi
callq 0x13080
testl %eax, %eax
je 0x2f262
movb (%r13), %cl
incq %r13
testb %cl, %cl
jne 0x2f24b
xorl %edx, %edx
testl %eax, %eax
sete %dl
addl %edx, %ebp
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
testq %rax, %rax
jne 0x2f23b
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xabc5a
movl %ebp, %eax
movq %rax, 0x28(%rsp)
shlq $0x4, %rax
leaq (%rax,%rax,2), %r12
movq %r12, %rdi
callq 0x13030
movq %rax, %r13
testl %ebp, %ebp
je 0x2f2c1
xorl %ebp, %ebp
movq %r13, %rdi
addq %rbp, %rdi
callq 0x2b118
addq $0x30, %rbp
cmpq %rbp, %r12
jne 0x2f2ad
movq 0x8(%rsp), %rax
movq %r13, (%rax)
movl $0x0, 0x8(%rax)
leaq 0x30(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
movl $0x1, %ebp
testq %rax, %rax
je 0x2f420
leaq 0x31(%rsp), %rbx
leaq 0x30(%rsp), %r12
movb 0x30(%rsp), %al
movq %rbx, %r13
testb %al, %al
je 0x2f402
movsbl %al, %edi
callq 0x13080
testl %eax, %eax
je 0x2f31e
movb (%r13), %al
incq %r13
jmp 0x2f301
movq 0x28(%rsp), %rax
movq 0x8(%rsp), %rcx
cmpl %eax, 0x8(%rcx)
leaq 0x20(%rsp), %r15
leaq 0x1c(%rsp), %rbp
jge 0x2f41b
movq %r12, %rdi
leaq 0xaec32(%rip), %rsi     # 0xddf77
leaq 0x24(%rsp), %r13
movq %r13, %rdx
movq %r15, %rcx
movq %rbp, %r8
leaq 0x18(%rsp), %r9
xorl %eax, %eax
leaq 0x10(%rsp), %r10
pushq %r10
leaq 0x1c(%rsp), %r10
pushq %r10
callq 0x134c0
addq $0x10, %rsp
cmpl $0x6, %eax
je 0x2f3ab
movl $0xff, 0x18(%rsp)
xorl %eax, %eax
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movq %r12, %rdi
leaq 0xaeb71(%rip), %rsi     # 0xddf03
movq %r13, %rdx
movq %r15, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x134c0
cmpl $0x3, %eax
jne 0x2f434
movq 0x8(%rsp), %r8
movq (%r8), %rax
movslq 0x8(%r8), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movss 0x24(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
movss 0x1c(%rsp), %xmm2
movss %xmm0, (%rax,%rcx)
movss %xmm1, 0x4(%rax,%rcx)
movss %xmm2, 0x8(%rax,%rcx)
movb 0x18(%rsp), %dl
movb 0x14(%rsp), %sil
movb 0x10(%rsp), %dil
movb %dl, 0x2c(%rax,%rcx)
movb %sil, 0x2d(%rax,%rcx)
movb %dil, 0x2e(%rax,%rcx)
incl 0x8(%r8)
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
testq %rax, %rax
jne 0x2f2fa
movl $0x1, %ebp
movl %ebp, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rbx
movl 0x8(%rbx), %esi
leaq 0xaeac7(%rip), %rdi     # 0xddf0a
xorl %ebp, %ebp
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0xac81a
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2f45e
callq 0x13680
movq 0x8(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
jmp 0x2f420
movq %rax, %rbx
movq %r13, %rdi
movq %r12, %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0
nop