// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0xad15f
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
xorps %xmm0, %xmm0
testq %rax, %rax
je 0x2f081
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
leaq 0x70(%rsp), %r13
leaq 0x10(%rsp), %r12
leaq 0xc(%rsp), %r15
xorl %ebp, %ebp
cmpb $0x76, 0x70(%rsp)
jne 0x2f06a
movq %r13, %rdi
leaq 0xafee3(%rip), %rsi     # 0xdef01
leaq 0x30(%rsp), %rdx
leaq 0x14(%rsp), %rcx
movq %r12, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x134c0
cmpl $0x4, %eax
jne 0x2f06a
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 %rbx, %rdx
callq 0x13800
testq %rax, %rax
jne 0x2f00d
jmp 0x2f091
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
testl %ebp, %ebp
movl $0x0, %ecx
je 0x2f23d
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
movups %xmm1, 0x10(%r14)
movsd %xmm0, 0x20(%r14)
xorl %r15d, %r15d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0xad14e
movslq %ebp, %r13
movl $0x30, %ecx
movq %r13, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
movq %rdi, 0x18(%rsp)
callq 0x13030
movq %rax, %r12
shlq $0x4, %r13
leaq (%r13,%r13,2), %r13
leaq (%r12,%r15), %rdi
callq 0x2b548
addq $0x30, %r15
cmpq %r15, %r13
jne 0x2f11c
movq %r12, (%r14)
movl $0x0, 0x8(%r14)
leaq 0x70(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
movl $0x1, %ecx
testq %rax, %rax
je 0x2f23d
cmpl %ebp, 0x8(%r14)
jge 0x2f23d
leaq 0x70(%rsp), %r13
leaq 0xc(%rsp), %r12
cmpb $0x76, 0x70(%rsp)
jne 0x2f215
movq %r13, %rdi
leaq 0xafd7f(%rip), %rsi     # 0xdef01
leaq 0x30(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x134c0
movl %eax, %r15d
cmpl $0x4, %eax
jne 0x2f1e1
movq (%r14), %rax
movslq 0x8(%r14), %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
jmp 0x2f20b
movl 0x8(%r14), %esi
leaq 0xafd1e(%rip), %rdi     # 0xdef0a
movl %ebp, %edx
xorl %eax, %eax
callq 0xaddb2
movq (%r14), %rdi
testq %rdi, %rdi
je 0x2f202
callq 0x13680
movq $0x0, (%r14)
xorl %eax, %eax
movl %eax, 0x8(%r14)
cmpl $0x4, %r15d
jne 0x2f23b
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdx
callq 0x13800
testq %rax, %rax
je 0x2f234
cmpl %ebp, 0x8(%r14)
jl 0x2f16d
movl $0x1, %ecx
jmp 0x2f23d
xorl %ecx, %ecx
movl %ecx, %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 0x18(%rsp), %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0
nop