// File: 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:
subq $0x1128, %rsp           # imm = 0x1128
movq %rdi, 0x1118(%rsp)
movq %rsi, 0x1110(%rsp)
movq 0x1118(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x1110(%rsp), %rdi
callq 0x1453b0
movq %rax, 0x1108(%rsp)
movl $0x0, 0x1104(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x1100(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x10fc(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x10f8(%rsp)
leaq 0xf0(%rsp), %rdi
movq 0x1110(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x138c0
cmpq $0x0, %rax
je 0x4d671
movsbl 0xf0(%rsp), %eax
cmpl $0x76, %eax
jne 0x4d66c
leaq 0xf0(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
leaq 0x1579f1(%rip), %rsi    # 0x1a4fd5
leaq 0xac(%rsp), %rcx
leaq 0xa8(%rsp), %r8
leaq 0xa4(%rsp), %r9
movb $0x0, %al
callq 0x13550
cmpl $0x4, %eax
jne 0x4d66a
movss 0xac(%rsp), %xmm0
addss 0x1100(%rsp), %xmm0
movss %xmm0, 0x1100(%rsp)
movss 0xa8(%rsp), %xmm0
addss 0x10fc(%rsp), %xmm0
movss %xmm0, 0x10fc(%rsp)
movss 0xa4(%rsp), %xmm0
addss 0x10f8(%rsp), %xmm0
movss %xmm0, 0x10f8(%rsp)
movl 0x1104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1104(%rsp)
jmp 0x4d66c
jmp 0x4d598
cmpl $0x0, 0x1104(%rsp)
jne 0x4d68b
movl $0x0, 0x1124(%rsp)
jmp 0x4d95b
movq 0x38(%rsp), %rdi
cvtsi2ssl 0x1104(%rsp), %xmm1
movss 0x1100(%rsp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, 0x1100(%rsp)
cvtsi2ssl 0x1104(%rsp), %xmm1
movss 0x10fc(%rsp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, 0x10fc(%rsp)
cvtsi2ssl 0x1104(%rsp), %xmm1
movss 0x10f8(%rsp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, 0x10f8(%rsp)
addq $0x10, %rdi
movss 0x1100(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x10fc(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x10f8(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
callq 0x50000
movq 0x1110(%rsp), %rdi
movq 0x1108(%rsp), %rsi
xorl %edx, %edx
callq 0x145360
movslq 0x1104(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x30, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x20(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x30(%rsp)
cmpq $0x0, %rax
je 0x4d7c1
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
imulq $0x30, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x47310
jmp 0x4d7a9
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jne 0x4d798
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x4d81a
leaq 0x157803(%rip), %rdi    # 0x1a4fde
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x1124(%rsp)
jmp 0x4d95b
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
callq 0x13a90
jmp 0x4d96a
movq 0x38(%rsp), %rax
movl $0x0, 0x8(%rax)
leaq 0xf0(%rsp), %rdi
movq 0x1110(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x138c0
cmpq $0x0, %rax
je 0x4d950
movq 0x38(%rsp), %rax
movl 0x8(%rax), %eax
cmpl 0x1104(%rsp), %eax
jl 0x4d860
jmp 0x4d950
movsbl 0xf0(%rsp), %eax
cmpl $0x76, %eax
jne 0x4d94b
leaq 0xf0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x157750(%rip), %rsi    # 0x1a4fd5
leaq 0x4c(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x44(%rsp), %r9
movb $0x0, %al
callq 0x13550
cmpl $0x4, %eax
je 0x4d8f8
movq 0x38(%rsp), %rax
movl 0x8(%rax), %esi
movl 0x1104(%rsp), %edx
leaq 0x157749(%rip), %rdi    # 0x1a4fff
movb $0x0, %al
callq 0x1464b0
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x4d8d8
movq (%rsp), %rdi
callq 0x13750
movq 0x38(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
movl $0x0, 0x1124(%rsp)
jmp 0x4d95b
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movslq 0x8(%rax), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movss 0x4c(%rsp), %xmm0
subss 0x1100(%rsp), %xmm0
movss 0x48(%rsp), %xmm1
subss 0x10fc(%rsp), %xmm1
movss 0x44(%rsp), %xmm2
subss 0x10f8(%rsp), %xmm2
callq 0x44cd0
movq 0x38(%rsp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x4d826
movl $0x1, 0x1124(%rsp)
movl 0x1124(%rsp), %eax
addq $0x1128, %rsp           # imm = 0x1128
retq
movq 0x98(%rsp), %rdi
callq 0x13970
nopw (%rax,%rax)