// File: 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:
subq $0x10c8, %rsp           # imm = 0x10C8
movq %rdi, 0x10b8(%rsp)
movq %rsi, 0x10b0(%rsp)
movq 0x10b8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x10b0(%rsp), %rdi
callq 0x1453b0
movq %rax, 0x10a8(%rsp)
movl $0x0, 0x10a4(%rsp)
leaq 0xa0(%rsp), %rdi
movq 0x10b0(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x138c0
cmpq $0x0, %rax
je 0x4e16d
leaq 0xa0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x5f(%rsp)
je 0x4e125
movq 0x98(%rsp), %rax
movsbl (%rax), %edi
callq 0x13080
cmpl $0x0, %eax
setne %al
movb %al, 0x5f(%rsp)
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x4e12f
jmp 0x4e145
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x4e0f8
movq 0x98(%rsp), %rax
cmpb $0x0, (%rax)
jne 0x4e157
jmp 0x4e0c4
movl 0x10a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10a4(%rsp)
jmp 0x4e0c4
movq 0x10b0(%rsp), %rdi
movq 0x10a8(%rsp), %rsi
xorl %edx, %edx
callq 0x145360
movslq 0x10a4(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x30, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x40(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x50(%rsp)
cmpq $0x0, %rax
je 0x4e211
movq 0x50(%rsp), %rax
movq 0x48(%rsp), %rcx
imulq $0x30, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x47310
jmp 0x4e1f9
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0x38(%rsp)
jne 0x4e1e8
movq 0x60(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x4e26a
leaq 0x156db3(%rip), %rdi    # 0x1a4fde
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x10c4(%rsp)
jmp 0x4e47c
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x13a90
jmp 0x4e48b
movq 0x60(%rsp), %rax
movl $0x0, 0x8(%rax)
leaq 0xa0(%rsp), %rdi
movq 0x10b0(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x138c0
cmpq $0x0, %rax
je 0x4e471
leaq 0xa0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x27(%rsp)
je 0x4e2d7
movq 0x80(%rsp), %rax
movsbl (%rax), %edi
callq 0x13080
cmpl $0x0, %eax
setne %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x4e2e1
jmp 0x4e2f7
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x4e2aa
movq 0x80(%rsp), %rax
cmpb $0x0, (%rax)
jne 0x4e309
jmp 0x4e276
movq 0x60(%rsp), %rax
movl 0x8(%rax), %eax
cmpl 0x10a4(%rsp), %eax
jl 0x4e31f
jmp 0x4e471
leaq 0xa0(%rsp), %rdi
leaq 0x156d77(%rip), %rsi    # 0x1a50a5
leaq 0x7c(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x74(%rsp), %r8
leaq 0x70(%rsp), %r9
leaq 0x6c(%rsp), %r10
leaq 0x68(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x13550
cmpl $0x6, %eax
je 0x4e403
movl $0xff, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl $0x0, 0x68(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x156c4b(%rip), %rsi    # 0x1a4fd7
leaq 0x7c(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x74(%rsp), %r8
movb $0x0, %al
callq 0x13550
cmpl $0x3, %eax
je 0x4e401
movq 0x60(%rsp), %rax
movl 0x8(%rax), %esi
movl 0x10a4(%rsp), %edx
leaq 0x156c42(%rip), %rdi    # 0x1a4fff
movb $0x0, %al
callq 0x1464b0
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x4e3e1
movq 0x18(%rsp), %rdi
callq 0x13750
movq 0x60(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
movl $0x0, 0x10c4(%rsp)
jmp 0x4e47c
jmp 0x4e403
movq 0x60(%rsp), %rax
movq (%rax), %rdi
movslq 0x8(%rax), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movss 0x7c(%rsp), %xmm0
movss 0x78(%rsp), %xmm1
movss 0x74(%rsp), %xmm2
callq 0x44cd0
movq 0x60(%rsp), %rax
movq (%rax), %rdi
movslq 0x8(%rax), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl 0x70(%rsp), %eax
movb %al, %dl
movl 0x6c(%rsp), %eax
movb %al, %cl
movl 0x68(%rsp), %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x47a50
movq 0x60(%rsp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x4e276
movl $0x1, 0x10c4(%rsp)
movl 0x10c4(%rsp), %eax
addq $0x10c8, %rsp           # imm = 0x10C8
retq
movq 0x90(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)