// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1078, %rsp           # imm = 0x1078
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x9c473
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, (%rsp)
leaq 0x70(%rsp), %r12
leaq 0x4(%rsp), %r15
xorl %ebp, %ebp
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13840
testq %rax, %rax
je 0x2c108
cmpb $0x76, 0x70(%rsp)
jne 0x2c094
movq %r12, %rdi
leaq 0x9ce37(%rip), %rsi     # 0xc8ef1
leaq 0x30(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x134d0
cmpl $0x4, %eax
jne 0x2c094
movss 0x8(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps 0x10(%rsp), %xmm0
addps %xmm1, %xmm0
movaps %xmm0, 0x10(%rsp)
movss (%rsp), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, (%rsp)
incl %ebp
jmp 0x2c094
testl %ebp, %ebp
je 0x2c22b
cvtsi2ss %ebp, %xmm0
movss (%rsp), %xmm2
divss %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movaps 0x10(%rsp), %xmm1
divps %xmm0, %xmm1
movss %xmm2, (%rsp)
cvtss2sd %xmm2, %xmm0
movaps %xmm1, 0x10(%rsp)
cvtps2pd %xmm1, %xmm1
movups %xmm1, 0x10(%rbx)
movsd %xmm0, 0x20(%rbx)
xorl %r15d, %r15d
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x9c462
movslq %ebp, %r13
pushq $0x30
popq %rcx
movq %r13, %rax
mulq %rcx
pushq $-0x1
popq %rdi
cmovnoq %rax, %rdi
movq %rdi, 0x28(%rsp)
callq 0x13030
movq %rax, %r12
imulq $0x30, %r13, %r13
leaq (%r12,%r15), %rdi
callq 0x29118
addq $0x30, %r15
cmpq %r15, %r13
jne 0x2c177
movq %r12, (%rbx)
andl $0x0, 0x8(%rbx)
leaq 0x70(%rsp), %r12
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r13
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x13840
testq %rax, %rax
je 0x2c225
cmpl %ebp, 0x8(%rbx)
jge 0x2c225
cmpb $0x76, 0x70(%rsp)
jne 0x2c19f
movq %r12, %rdi
leaq 0x9cd27(%rip), %rsi     # 0xc8ef1
leaq 0x30(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r15, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x134d0
cmpl $0x4, %eax
jne 0x2c230
movq (%rbx), %rax
movslq 0x8(%rbx), %rcx
imulq $0x30, %rcx, %rdx
movss 0x4(%rsp), %xmm0
subss (%rsp), %xmm0
movss 0x8(%rsp), %xmm1
movss 0xc(%rsp), %xmm2
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
subps 0x10(%rsp), %xmm2
movlps %xmm2, (%rax,%rdx)
movss %xmm0, 0x8(%rax,%rdx)
leal 0x1(%rcx), %eax
movl %eax, 0x8(%rbx)
jmp 0x2c19f
pushq $0x1
popq %r14
jmp 0x2c25b
xorl %r14d, %r14d
jmp 0x2c25b
movl 0x8(%rbx), %esi
leaq 0x9ccc0(%rip), %rdi     # 0xc8efa
xorl %r14d, %r14d
movl %ebp, %edx
xorl %eax, %eax
callq 0x9ce26
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2c253
callq 0x136a0
andq $0x0, (%rbx)
andl $0x0, 0x8(%rbx)
movl %r14d, %eax
addq $0x1078, %rsp           # imm = 0x1078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
movq 0x28(%rsp), %rsi
callq 0x139f0
movq %rbx, %rdi
callq 0x138f0