// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelBlock.cpp
// Function: gaps::R3SurfelBlock::ReadXYZBinary(_IO_FILE*)
int R3SurfelBlock::
ReadXYZBinary(FILE *fp)
{
  // Determine the number of surfels
  long int start_file_offset = RNFileTell(fp);
  RNFileSeek(fp, 0, RN_FILE_SEEK_END);
  long int end_file_offset = RNFileTell(fp);
  RNFileSeek(fp, start_file_offset, RN_FILE_SEEK_SET);
  long int file_size = end_file_offset - start_file_offset;
  nsurfels = file_size / (4*sizeof(RNScalar32));
  if (nsurfels == 0) return 0;
  
  // Allocate surfels
  surfels = new R3Surfel [ nsurfels ];
  if (!surfels) {
    RNFail("Unable to allocate surfel block\n");
    return 0;
  }

  // Read surfels
  position_origin = R3zero_point;
  for (int i = 0; i < nsurfels; i++) {
    // Read data
    float xyzr[4];
    if (fread(xyzr, sizeof(RNScalar32), 4, fp) != (size_t) 4) {
      RNFail("Unable to read point %d\n", i);
      delete [] surfels;
      surfels = NULL;
      nsurfels = 0;
      return 0;
    }

    // Compute color
    float c = 255 * xyzr[3];

    // Assign surfel position
    surfels[i].SetPosition(xyzr);
    surfels[i].SetColor(c, c, c);

    // Update position_origin
    position_origin[0] += xyzr[0];
    position_origin[1] += xyzr[1];
    position_origin[2] += xyzr[2];
  }

  // Put position_origin at centroid
  position_origin /= nsurfels;

  // Update surfels to be relative to position_origin
  for (int i = 0; i < nsurfels; i++) {
    const float *xyz = surfels[i].PositionPtr();
    surfels[i].SetPosition(xyz[0] - position_origin[0], xyz[1] - position_origin[1], xyz[2] - position_origin[2]);
  }  
  
  // Return success
  return 1;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x1453b0
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x145360
movq 0xd0(%rsp), %rdi
callq 0x1453b0
movq %rax, 0xc0(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
xorl %edx, %edx
callq 0x145360
movq 0x78(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq 0xc8(%rsp), %rcx
movq %rcx, 0xb8(%rsp)
movq 0xb8(%rsp), %rcx
shrq $0x4, %rcx
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x8(%rax)
jne 0x4e66d
movl $0x0, 0xe4(%rsp)
jmp 0x4ea7f
movq 0x78(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
movl $0x30, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x60(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x68(%rsp), %rax
movq %rcx, 0x70(%rsp)
cmpq $0x0, %rax
je 0x4e6fb
movq 0x70(%rsp), %rax
movq 0x68(%rsp), %rcx
imulq $0x30, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x47310
jmp 0x4e6e3
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %rax
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0x58(%rsp)
jne 0x4e6d2
movq 0x78(%rsp), %rax
movq 0x70(%rsp), %rcx
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x4e754
leaq 0x1568c9(%rip), %rdi    # 0x1a4fde
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0xe4(%rsp)
jmp 0x4ea7f
movq 0x60(%rsp), %rsi
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
callq 0x13a90
jmp 0x4ea8e
movq 0x78(%rsp), %rdi
addq $0x10, %rdi
leaq 0x5af94c(%rip), %rsi    # 0x5fe0b0
callq 0xf89e0
movl $0x0, 0xa8(%rsp)
movq 0x78(%rsp), %rcx
movl 0xa8(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x4e92c
leaq 0x90(%rsp), %rdi
movq 0xd0(%rsp), %rcx
movl $0x4, %edx
movq %rdx, %rsi
callq 0x13860
cmpq $0x4, %rax
je 0x4e801
movl 0xa8(%rsp), %esi
leaq 0x156925(%rip), %rdi    # 0x1a50df
movb $0x0, %al
callq 0x1464b0
movq 0x78(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x4e7de
movq 0x40(%rsp), %rdi
callq 0x13750
movq 0x78(%rsp), %rax
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
movl $0x0, 0xe4(%rsp)
jmp 0x4ea7f
movq 0x78(%rsp), %rax
movss 0x9c(%rsp), %xmm0
movss 0x1566e9(%rip), %xmm1   # 0x1a4f00
mulss %xmm1, %xmm0
movss %xmm0, 0x8c(%rsp)
movq (%rax), %rdi
movslq 0xa8(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
leaq 0x90(%rsp), %rsi
callq 0x501c0
movq 0x78(%rsp), %rax
movq (%rax), %rdi
movslq 0xa8(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdi
cvttss2si 0x8c(%rsp), %eax
movb %al, %dl
movb %al, %cl
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x47a50
movq 0x78(%rsp), %rdi
movss 0x90(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1bde0
movsd 0x28(%rsp), %xmm0
movq 0x78(%rsp), %rdi
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movss 0x94(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
addq $0x10, %rdi
movl $0x1, %esi
callq 0x1bde0
movsd 0x30(%rsp), %xmm0
movq 0x78(%rsp), %rdi
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movss 0x98(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
addq $0x10, %rdi
movl $0x2, %esi
callq 0x1bde0
movsd 0x38(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x4e774
movq 0x78(%rsp), %rdi
cvtsi2sdl 0x8(%rdi), %xmm0
addq $0x10, %rdi
callq 0xf9160
movl $0x0, 0x88(%rsp)
movq 0x78(%rsp), %rcx
movl 0x88(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x4ea74
movq 0x78(%rsp), %rax
movq (%rax), %rdi
movslq 0x88(%rsp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
callq 0x4feb0
movq 0x78(%rsp), %rdi
movq %rax, 0x80(%rsp)
movq (%rdi), %rax
movslq 0x88(%rsp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x80(%rsp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rsp)
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1bde0
movsd (%rsp), %xmm0
movq 0x78(%rsp), %rdi
subsd (%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x80(%rsp), %rax
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
addq $0x10, %rdi
movl $0x1, %esi
callq 0x1bde0
movsd 0x8(%rsp), %xmm0
movq 0x78(%rsp), %rdi
subsd (%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x80(%rsp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
addq $0x10, %rdi
movl $0x2, %esi
callq 0x1bde0
movsd 0x10(%rsp), %xmm2
movq 0x18(%rsp), %rdi
movss 0x20(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
subsd (%rax), %xmm2
cvtsd2ss %xmm2, %xmm2
callq 0x44cd0
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x4e94a
movl $0x1, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
movq 0xb0(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)