// File: /workspace/github_cpp_makefile_star2_O1/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 0xad15f
movq %rax, %r13
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xad14e
movq %r14, %rdi
callq 0xad15f
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xad14e
subq %r13, %r12
shrq $0x4, %r12
movl %r12d, 0x8(%rbx)
testl %r12d, %r12d
je 0x2faee
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 0x2b548
addq $0x30, %rbp
cmpq %rbp, %r13
jne 0x2f97e
movq %r12, (%rbx)
leaq 0x10(%rbx), %rdi
leaq 0x506cd2(%rip), %rsi    # 0x536670
movq %rdi, 0x10(%rsp)
callq 0x850ec
cmpl $0x0, 0x8(%rbx)
jle 0x2fa80
movl $0x2e, %r13d
movq %rsp, %r12
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x4, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x13790
movq %rax, %r15
cmpq $0x4, %rax
jne 0x2fa3b
movss 0xc(%rsp), %xmm0
mulss 0xaf42b(%rip), %xmm0    # 0xdee14
movq (%rbx), %rax
movsd (%rsp), %xmm1
movsd %xmm1, -0x2e(%rax,%r13)
movss 0x8(%rsp), %xmm2
movss %xmm2, -0x26(%rax,%r13)
cvttss2si %xmm0, %ecx
movb %cl, -0x2(%rax,%r13)
movb %cl, -0x1(%rax,%r13)
cvtps2pd %xmm1, %xmm0
movb %cl, (%rax,%r13)
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)
jmp 0x2fa66
leaq 0xaf56f(%rip), %rdi     # 0xdefb1
movl %ebp, %esi
xorl %eax, %eax
callq 0xaddb2
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2fa58
callq 0x13680
movq $0x0, (%rbx)
movl $0x0, 0x8(%rbx)
cmpq $0x4, %r15
jne 0x2faeb
incq %rbp
movslq 0x8(%rbx), %rax
addq $0x30, %r13
cmpq %rax, %rbp
jl 0x2f9bd
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rbx), %xmm0
movq 0x10(%rsp), %rdi
callq 0x8573c
movslq 0x8(%rbx), %rcx
movl $0x1, %r15d
testq %rcx, %rcx
jle 0x2faee
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 0x2fab8
jmp 0x2faee
xorl %r15d, %r15d
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