// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0xabc6b
movq %rax, %r13
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xabc5a
movq %r14, %rdi
callq 0xabc6b
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xabc5a
subq %r13, %r12
shrq $0x4, %r12
movl %r12d, 0x8(%rbx)
testl %r12d, %r12d
je 0x2f72f
movslq %r12d, %r13
movl $0x30, %ecx
movq %r13, %rax
mulq %rcx
movq $-0x1, %r15
cmovnoq %rax, %r15
movq %r15, %rdi
callq 0x13030
movq %rax, %r12
shlq $0x4, %r13
leaq (%r13,%r13,2), %r13
xorl %ebp, %ebp
leaq (%r12,%rbp), %rdi
callq 0x2b118
addq $0x30, %rbp
cmpq %rbp, %r13
jne 0x2f5ce
movq %r12, (%rbx)
leaq 0x10(%rbx), %r15
leaq 0x506082(%rip), %rsi    # 0x535670
movq %r15, %rdi
callq 0x8412a
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x2f698
movl $0x2e, %ebp
xorl %r12d, %r12d
movq %rsp, %r13
movl $0x4, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x4, %rax
jne 0x2f700
movss 0xc(%rsp), %xmm0
mulss 0xae7cb(%rip), %xmm0    # 0xdde04
movq (%rbx), %rax
movsd (%rsp), %xmm1
movsd %xmm1, -0x2e(%rax,%rbp)
movss 0x8(%rsp), %xmm2
movss %xmm2, -0x26(%rax,%rbp)
cvttss2si %xmm0, %ecx
movb %cl, -0x2(%rax,%rbp)
movb %cl, -0x1(%rax,%rbp)
cvtps2pd %xmm1, %xmm0
movb %cl, (%rax,%rbp)
movupd 0x10(%rbx), %xmm1
addpd %xmm0, %xmm1
movupd %xmm1, 0x10(%rbx)
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x20(%rbx)
incq %r12
movslq 0x8(%rbx), %rax
addq $0x30, %rbp
cmpq %rax, %r12
jl 0x2f60c
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq %r15, %rdi
callq 0x8477a
movslq 0x8(%rbx), %rcx
movl $0x1, %r15d
testq %rcx, %rcx
jle 0x2f72f
movq (%rbx), %rax
movupd 0x10(%rbx), %xmm0
movsd 0x20(%rbx), %xmm1
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%rdx), %xmm2
subsd %xmm1, %xmm2
cvtsd2ss %xmm2, %xmm2
cvtps2pd (%rax,%rdx), %xmm3
subpd %xmm0, %xmm3
cvtpd2ps %xmm3, %xmm3
movlpd %xmm3, (%rax,%rdx)
movss %xmm2, 0x8(%rax,%rdx)
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0x2f6cd
jmp 0x2f72f
leaq 0xae8aa(%rip), %rdi     # 0xddfb1
xorl %r15d, %r15d
movl %r12d, %esi
xorl %eax, %eax
callq 0xac81a
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2f721
callq 0x13680
movq $0x0, (%rbx)
movl $0x0, 0x8(%rbx)
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
movq %r15, %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0
nop