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

  // Count the number of surfels and compute centroid
  int count = 0;
  float cx = 0;
  float cy = 0;
  float cz = 0;
  char buffer[4096];
  while (fgets(buffer, 4096, fp)) {
    if (buffer[0] == 'v') {
      // Parse surfel data
      char keyword[64];
      float x, y, z;
      if (sscanf(buffer, "%s%f%f%f", keyword, &x, &y, &z) == (unsigned int) 4) {
        cx += x;
        cy += y;
        cz += z;
        count++;
      }
    }
  }

  // Check number of points
  if (count == 0) return 0;

  // Comput centroid of points
  cx /= count;
  cy /= count;
  cz /= count;

  // Set position_origin to centroid of points
  position_origin.Reset(cx, cy, cz);

  // 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 number of surfels
    if (nsurfels >= count) break;

    // Check if point
    if (buffer[0] == 'v') {
      // Parse surfel data
      char keyword[64];
      float x, y, z;
      if (sscanf(buffer, "%s%f%f%f", keyword, &x, &y, &z) != (unsigned int) 4) {
        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 - cx, y - cy, z - cz);
      nsurfels++;
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0xabc6b
movq %rax, %r15
leaq 0x80(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
testq %rax, %rax
je 0x2ee92
movq %r15, 0x38(%rsp)
movq %rbx, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
leaq 0x80(%rsp), %r13
leaq 0x14(%rsp), %rbx
leaq 0x10(%rsp), %r12
leaq 0xc(%rsp), %r15
xorl %ebp, %ebp
cmpb $0x76, 0x80(%rsp)
jne 0x2ecfa
movq %r13, %rdi
leaq 0xaf251(%rip), %rsi     # 0xddf01
leaq 0x40(%rsp), %rdx
movq %rbx, %rcx
movq %r12, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x134c0
cmpl $0x4, %eax
jne 0x2ecfa
movss 0x10(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps 0x20(%rsp), %xmm0
addps %xmm1, %xmm0
movaps %xmm0, 0x20(%rsp)
movss 0x8(%rsp), %xmm0
addss 0xc(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
incl %ebp
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
testq %rax, %rax
jne 0x2ec9c
testl %ebp, %ebp
je 0x2ee92
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
movss 0x8(%rsp), %xmm2
divss %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps 0x20(%rsp), %xmm1
divps %xmm0, %xmm1
movss %xmm2, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
movaps %xmm1, 0x20(%rsp)
cvtps2pd %xmm1, %xmm1
movq 0x18(%rsp), %rax
movups %xmm1, 0x10(%rax)
movsd %xmm0, 0x20(%rax)
xorl %r13d, %r13d
movq %r14, %rdi
movq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0xabc5a
movslq %ebp, %r12
movl $0x30, %ecx
movq %r12, %rax
mulq %rcx
movq $-0x1, %rbx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0x13030
movq %rax, %r15
shlq $0x4, %r12
leaq (%r12,%r12,2), %r12
leaq (%r15,%r13), %rdi
callq 0x2b118
addq $0x30, %r13
cmpq %r13, %r12
jne 0x2ed95
movq 0x18(%rsp), %rbx
movq %r15, (%rbx)
movl $0x0, 0x8(%rbx)
leaq 0x80(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
movl $0x1, %r15d
testq %rax, %rax
je 0x2ee95
cmpl %ebp, 0x8(%rbx)
jge 0x2ee95
leaq 0x80(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0xc(%rsp), %rbx
cmpb $0x76, 0x80(%rsp)
jne 0x2ee6d
movq %r12, %rdi
leaq 0xaf0f8(%rip), %rsi     # 0xddf01
leaq 0x40(%rsp), %rdx
leaq 0x14(%rsp), %rcx
movq %r13, %r8
movq %rbx, %r9
xorl %eax, %eax
callq 0x134c0
cmpl $0x4, %eax
jne 0x2eeaa
movq 0x18(%rsp), %rsi
movq (%rsi), %rax
movslq 0x8(%rsi), %rcx
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
movss 0xc(%rsp), %xmm0
subss 0x8(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
movss 0x14(%rsp), %xmm2
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
subps 0x20(%rsp), %xmm2
movlps %xmm2, (%rax,%rdx)
movss %xmm0, 0x8(%rax,%rdx)
leal 0x1(%rcx), %eax
movl %eax, 0x8(%rsi)
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13800
testq %rax, %rax
je 0x2ee95
movq 0x18(%rsp), %rax
cmpl %ebp, 0x8(%rax)
jl 0x2edf5
jmp 0x2ee95
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rbx
movl 0x8(%rbx), %esi
leaq 0xaf051(%rip), %rdi     # 0xddf0a
xorl %r15d, %r15d
movl %ebp, %edx
xorl %eax, %eax
callq 0xac81a
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2eed2
callq 0x13680
movq $0x0, (%rbx)
movl $0x0, 0x8(%rbx)
jmp 0x2ee95
movq %rax, %r14
movq %r15, %rdi
movq %rbx, %rsi
callq 0x139b0
movq %r14, %rdi
callq 0x138b0