// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdi, 0x20(%rsp)
movq %rsi, %rdi
callq 0xad15f
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
xorl %ebp, %ebp
testq %rax, %rax
je 0x2f5fc
leaq 0x31(%rsp), %r14
leaq 0x30(%rsp), %r12
movsbl 0x30(%rsp), %edi
testl %edi, %edi
setne %r15b
je 0x2f5e1
callq 0x13080
testl %eax, %eax
je 0x2f5e1
movq %r14, %r13
movsbl (%r13), %edi
testl %edi, %edi
setne %r15b
je 0x2f5e1
callq 0x13080
incq %r13
testl %eax, %eax
jne 0x2f5c8
movzbl %r15b, %eax
addl %eax, %ebp
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
testq %rax, %rax
jne 0x2f5af
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
callq 0xad14e
movl %ebp, %r15d
movq %r15, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %r12
movq %r12, %rdi
callq 0x13030
movq %rax, %r13
testl %ebp, %ebp
movq 0x20(%rsp), %rbp
je 0x2f642
xorl %r14d, %r14d
leaq (%r14,%r13), %rdi
callq 0x2b548
addq $0x30, %r14
cmpq %r14, %r12
jne 0x2f630
movq %r13, (%rbp)
movl $0x0, 0x8(%rbp)
leaq 0x30(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
movl $0x1, %ecx
testq %rax, %rax
je 0x2f810
leaq 0x30(%rsp), %r12
movsbl 0x30(%rsp), %edi
movl $0x6, %ebp
testl %edi, %edi
sete %r13b
je 0x2f7db
callq 0x13080
testl %eax, %eax
je 0x2f6b2
leaq 0x31(%rsp), %r14
movsbl (%r14), %edi
testl %edi, %edi
sete %r13b
je 0x2f7db
callq 0x13080
incq %r14
testl %eax, %eax
jne 0x2f696
testb %r13b, %r13b
jne 0x2f7db
movl $0x7, %ebp
movq 0x20(%rsp), %rax
cmpl %r15d, 0x8(%rax)
jge 0x2f7db
movq %r12, %rdi
leaq 0xaf89e(%rip), %rsi     # 0xdef77
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x10(%rsp), %r9
xorl %eax, %eax
leaq 0x8(%rsp), %r10
pushq %r10
leaq 0x14(%rsp), %r10
pushq %r10
callq 0x134c0
addq $0x10, %rsp
cmpl $0x6, %eax
je 0x2f742
movl $0xff, 0x10(%rsp)
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movq %r12, %rdi
leaq 0xaf7dc(%rip), %rsi     # 0xdef03
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x14(%rsp), %r8
xorl %eax, %eax
callq 0x134c0
cmpl $0x3, %eax
jne 0x2f79f
movq 0x20(%rsp), %r8
movq (%r8), %rax
movslq 0x8(%r8), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movss 0x1c(%rsp), %xmm0
movss 0x18(%rsp), %xmm1
movss 0x14(%rsp), %xmm2
movss %xmm0, (%rax,%rcx)
movss %xmm1, 0x4(%rax,%rcx)
movss %xmm2, 0x8(%rax,%rcx)
movb 0x10(%rsp), %dl
movb 0xc(%rsp), %sil
movb 0x8(%rsp), %dil
movb %dl, 0x2c(%rax,%rcx)
movb %sil, 0x2d(%rax,%rcx)
movb %dil, 0x2e(%rax,%rcx)
movl 0x8(%r8), %eax
incl %eax
xorl %ebp, %ebp
jmp 0x2f7d7
movq 0x20(%rsp), %r14
movl 0x8(%r14), %esi
leaq 0xaf75b(%rip), %rdi     # 0xdef0a
movl %r15d, %edx
xorl %eax, %eax
callq 0xaddb2
movq (%r14), %rdi
testq %rdi, %rdi
je 0x2f7c6
callq 0x13680
movq $0x0, (%r14)
movq %r14, %r8
movl $0x1, %ebp
xorl %eax, %eax
movl %eax, 0x8(%r8)
testl %ebp, %ebp
je 0x2f7e4
cmpl $0x6, %ebp
jne 0x2f804
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
testq %rax, %rax
jne 0x2f672
movl $0x1, %ecx
jmp 0x2f810
cmpl $0x7, %ebp
movl $0x1, %ecx
je 0x2f810
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r13, %rdi
movq %r12, %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0