// File: /workspace/github_cpp_makefile_star2_O2/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 0x9c473
movq %rax, %r13
xorl %r15d, %r15d
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x9c462
movq %r14, %rdi
callq 0x9c473
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x9c462
subq %r13, %r12
shrq $0x4, %r12
movl %r12d, 0x8(%rbx)
testl %r12d, %r12d
je 0x2c99c
movslq %r12d, %r13
pushq $0x30
popq %rcx
movq %r13, %rax
mulq %rcx
pushq $-0x1
popq %r15
cmovnoq %rax, %r15
movq %r15, %rdi
callq 0x13030
movq %rax, %r12
imulq $0x30, %r13, %r13
xorl %ebp, %ebp
leaq (%r12,%rbp), %rdi
callq 0x29118
addq $0x30, %rbp
cmpq %rbp, %r13
jne 0x2c850
movq %r12, (%rbx)
leaq 0x10(%rbx), %rdi
leaq 0x4f9e20(%rip), %rsi    # 0x526690
movq %rdi, 0x10(%rsp)
callq 0x784a6
pushq $0x2e
popq %r15
xorl %r12d, %r12d
movq %rsp, %r13
pushq $0x4
popq %rbp
movslq 0x8(%rbx), %rax
cmpq %rax, %r12
jge 0x2c916
movq %r13, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x137d0
cmpq $0x4, %rax
jne 0x2c973
movss 0xc(%rsp), %xmm0
mulss 0x9c547(%rip), %xmm0    # 0xc8e04
movq (%rbx), %rax
movsd (%rsp), %xmm1
movsd %xmm1, -0x2e(%rax,%r15)
movss 0x8(%rsp), %xmm2
cvttss2si %xmm0, %ecx
movss %xmm2, -0x26(%rax,%r15)
movb %cl, -0x2(%rax,%r15)
movb %cl, -0x1(%rax,%r15)
movb %cl, (%rax,%r15)
cvtps2pd %xmm1, %xmm0
movupd 0x10(%rbx), %xmm1
addpd %xmm0, %xmm1
movupd %xmm1, 0x10(%rbx)
cvtss2sd %xmm2, %xmm0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x20(%rbx)
incq %r12
addq $0x30, %r15
jmp 0x2c887
cvtsi2sd %eax, %xmm0
movq 0x10(%rsp), %rdi
callq 0x788a4
movl 0x8(%rbx), %edx
movq (%rbx), %rax
movupd 0x10(%rbx), %xmm0
movsd 0x20(%rbx), %xmm1
xorl %ecx, %ecx
testl %edx, %edx
cmovlel %ecx, %edx
imulq $0x30, %rdx, %rdx
pushq $0x1
popq %r15
cmpq %rcx, %rdx
je 0x2c99c
cvtss2sd 0x8(%rax,%rcx), %xmm2
subsd %xmm1, %xmm2
cvtsd2ss %xmm2, %xmm2
cvtps2pd (%rax,%rcx), %xmm3
subpd %xmm0, %xmm3
cvtpd2ps %xmm3, %xmm3
movlpd %xmm3, (%rax,%rcx)
movss %xmm2, 0x8(%rax,%rcx)
addq $0x30, %rcx
jmp 0x2c943
leaq 0x9c627(%rip), %rdi     # 0xc8fa1
xorl %r15d, %r15d
movl %r12d, %esi
xorl %eax, %eax
callq 0x9ce26
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2c994
callq 0x136a0
andq $0x0, (%rbx)
andl $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 0x139f0
movq %rbx, %rdi
callq 0x138f0