// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelBlock.cpp
// Function: gaps::R3SurfelBlock::ReadUPC(_IO_FILE*)
int R3SurfelBlock::
ReadUPC(FILE *fp)
{
  // Read header
  UPCHeader header;
  if (!ReadUPCHeader(fp, header)) return 0;

  // Compute scale and offset
  double xoffset = header.xOffset;
  double yoffset = header.yOffset;
  double zoffset = header.zOffset;
  double xscale = header.xScale;
  double yscale = header.yScale;
  double zscale = header.zScale;
 
  // XXX THIS IS A HACK FOR OTTAWA XXX
  xoffset -= 444965;
  yoffset -= 5029450;

  // Set position_origin
  position_origin = R3Point(xoffset, yoffset, zoffset);

  // Allocate memory for surfels
  int target_count = (int) header.numOfPts;
  surfels = new R3Surfel [ target_count ];
  if (!surfels) {
    RNFail("Unable to allocate surfel block\n");
    return 0;
  }

  // Allocate memory for UPC points
  const int upc_point_size = 36;
  char *upc_points = new char [ target_count * upc_point_size ];

  // Read upc points
  int upc_count = 0;
  while (upc_count < target_count) {
    int status = fread(&upc_points[upc_count], upc_point_size, target_count - upc_count, fp);
    if (status <= 0) break;
    upc_count += status;
  }

  // Assign surfels
  nsurfels = 0;
  for (int i = 0; i < upc_count; i++) {
    char *upc_point = &upc_points[i * upc_point_size];
    double x = *((RNInt32 *) &upc_point[16]) * xscale;
    double y = *((RNInt32 *) &upc_point[20]) * yscale;
    double z = *((RNInt32 *) &upc_point[24]) * zscale;
    RNUInt16 r = *((RNUInt16 *) &upc_point[29]);
    RNUInt16 g = *((RNUInt16 *) &upc_point[31]);
    RNUInt16 b = *((RNUInt16 *) &upc_point[33]);
    if (r > 255) r = 255;
    if (g > 255) g = 255;
    if (b > 255) b = 255;
    surfels[nsurfels].SetPosition(x, y, z);
    surfels[nsurfels].SetColor(r, g, b);
    surfels[nsurfels].SetAerial(upc_point[9] == '0');
    nsurfels++;
  }

  // Delete memory for UPC points
  delete [] upc_points;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rsp), %rdi
movq $0x0, (%rdi)
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x4, %rax
jne 0x2fab4
xorl %r15d, %r15d
movl $0x66435055, %ebp       # imm = 0x66435055
leaq 0x18(%rsp), %r12
movl 0x18(%rsp), %eax
xorl %ebp, %eax
movzbl 0x1c(%rsp), %ecx
orl %eax, %ecx
je 0x2fad8
addq $0x4, %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x4, %rax
je 0x2fa86
leaq 0xae6b2(%rip), %rdi     # 0xde16d
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xac81a
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xabc5a
leaq 0x34(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x4, %rax
jne 0x2fd84
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x2fd90
leaq 0x35(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x2fd9c
leaq 0x36(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x2fda8
leaq 0x38(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x2fdb4
leaq 0x40(%rsp), %rdi
movl $0x8, %esi
movl $0x6, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x6, %rax
jne 0x2fdc0
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movups 0x40(%rsp), %xmm3
movaps %xmm3, 0x70(%rsp)
movsd 0x50(%rsp), %xmm3
movsd %xmm3, 0x10(%rsp)
addsd 0xae214(%rip), %xmm0    # 0xdddf0
addsd 0xae214(%rip), %xmm1    # 0xdddf8
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x83dc2
leaq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x8412a
movq 0x38(%rsp), %rbp
movq %rbp, %r15
shlq $0x20, %r15
movslq %ebp, %r13
movl $0x30, %ecx
movq %r13, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
movq %rdi, 0x8(%rsp)
callq 0x13030
movq %rax, %r12
testq %r15, %r15
je 0x2fc52
shlq $0x4, %r13
leaq (%r13,%r13,2), %r13
xorl %r15d, %r15d
leaq (%r12,%r15), %rdi
callq 0x2b118
addq $0x30, %r15
cmpq %r15, %r13
jne 0x2fc40
movq %r12, (%rbx)
movq %rbp, %rax
shlq $0x22, %rax
leaq (%rax,%rax,8), %rdi
sarq $0x20, %rdi
callq 0x13030
movq %rax, %r15
xorl %ecx, %ecx
movl %ecx, %r13d
movl %ebp, %eax
movl %ecx, %r12d
subl %ecx, %eax
jle 0x2fc96
leaq (%r15,%r12), %rdi
movslq %eax, %rdx
movl $0x24, %esi
movq %r14, %rcx
callq 0x13790
leal (%rax,%r13), %ecx
testl %eax, %eax
jg 0x2fc6e
movl $0x0, 0x8(%rbx)
testl %r13d, %r13d
movapd 0x70(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
jle 0x2fd72
movq (%rbx), %rax
shlq $0x2, %r12
leaq (%r12,%r12,8), %rcx
xorl %edx, %edx
movl $0xff, %esi
xorl %edi, %edi
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%r15,%rdx), %xmm0
mulsd %xmm3, %xmm0
movzwl 0x1d(%r15,%rdx), %r8d
cmpl %esi, %r8d
cmovael %esi, %r8d
movzwl 0x1f(%r15,%rdx), %r9d
cmpl %esi, %r9d
cmovael %esi, %r9d
movzwl 0x21(%r15,%rdx), %r10d
cmpl %esi, %r10d
cmovael %esi, %r10d
movslq %edi, %rdi
leaq (%rdi,%rdi,2), %rdi
cvtsd2ss %xmm0, %xmm0
shlq $0x4, %rdi
cvtdq2pd 0x10(%r15,%rdx), %xmm1
mulpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
movlpd %xmm1, (%rax,%rdi)
movss %xmm0, 0x8(%rax,%rdi)
movb %r8b, 0x2c(%rax,%rdi)
movb %r9b, 0x2d(%rax,%rdi)
movb %r10b, 0x2e(%rax,%rdi)
movq (%rbx), %rax
movslq 0x8(%rbx), %rdi
cmpb $0x30, 0x9(%r15,%rdx)
sete %r8b
leaq (%rdi,%rdi,2), %r9
shlq $0x4, %r9
movb 0x2f(%rax,%r9), %r10b
andb $-0x3, %r10b
addb %r8b, %r8b
orb %r10b, %r8b
movb %r8b, 0x2f(%rax,%r9)
incl %edi
movl %edi, 0x8(%rbx)
addq $0x24, %rdx
cmpq %rdx, %rcx
jne 0x2fcc6
movq %r15, %rdi
callq 0x13680
movl $0x1, %ebx
jmp 0x2fac4
leaq 0xae408(%rip), %rdi     # 0xde193
jmp 0x2fabb
leaq 0xae421(%rip), %rdi     # 0xde1b8
jmp 0x2fabb
leaq 0xae43d(%rip), %rdi     # 0xde1e0
jmp 0x2fabb
leaq 0xae459(%rip), %rdi     # 0xde208
jmp 0x2fabb
leaq 0xae46d(%rip), %rdi     # 0xde228
jmp 0x2fabb
leaq 0xae498(%rip), %rdi     # 0xde25f
jmp 0x2fabb
movq %rax, %rbx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0