// File: /workspace/github_cpp_makefile_star2_O1/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 0x2fe8e
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 0x2feb2
addq $0x4, %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x4, %rax
je 0x2fe60
leaq 0xaf2d8(%rip), %rdi     # 0xdf16d
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xaddb2
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xad14e
leaq 0x4c(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x4, %rax
jne 0x30178
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x30184
leaq 0x4d(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x30190
leaq 0x4e(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x3019c
leaq 0x50(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x1, %rax
jne 0x301a8
leaq 0x58(%rsp), %rdi
movl $0x8, %esi
movl $0x6, %edx
movq %r14, %rcx
callq 0x13790
cmpq $0x6, %rax
jne 0x301b4
movsd 0x70(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x80(%rsp), %xmm2
movups 0x58(%rsp), %xmm3
movaps %xmm3, 0x30(%rsp)
movsd 0x68(%rsp), %xmm3
movsd %xmm3, 0x8(%rsp)
addsd 0xaee45(%rip), %xmm0    # 0xdee00
addsd 0xaee45(%rip), %xmm1    # 0xdee08
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x84d7e
leaq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x850ec
movq 0x50(%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, 0x10(%rsp)
callq 0x13030
movq %rax, %r12
testq %r15, %r15
je 0x30031
shlq $0x4, %r13
leaq (%r13,%r13,2), %r13
xorl %r15d, %r15d
leaq (%r12,%r15), %rdi
callq 0x2b548
addq $0x30, %r15
cmpq %r15, %r13
jne 0x3001f
movq %r12, (%rbx)
movq %rbp, %rax
shlq $0x22, %rax
leaq (%rax,%rax,8), %rdi
sarq $0x20, %rdi
callq 0x13030
movq %rax, %r15
xorl %r13d, %r13d
xorl %r12d, %r12d
movapd 0x30(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
movl %ebp, %eax
subl %r12d, %eax
jle 0x30095
movl %r12d, %edi
addq %r15, %rdi
movslq %eax, %rdx
movl $0x24, %esi
movq %r14, %rcx
callq 0x13790
movsd 0x8(%rsp), %xmm3
movapd 0x30(%rsp), %xmm2
testl %eax, %eax
movl %eax, %ecx
cmovlel %r13d, %ecx
addl %ecx, %r12d
testl %eax, %eax
jg 0x3005d
movl $0x0, 0x8(%rbx)
testl %r12d, %r12d
jle 0x30166
movl %r12d, %eax
shlq $0x2, %rax
leaq (%rax,%rax,8), %rax
xorl %ecx, %ecx
movl $0xff, %edx
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%r15,%rcx), %xmm0
mulsd %xmm3, %xmm0
movzwl 0x1d(%r15,%rcx), %esi
cmpl %edx, %esi
cmovael %edx, %esi
movzwl 0x1f(%r15,%rcx), %edi
cmpl %edx, %edi
cmovael %edx, %edi
movzwl 0x21(%r15,%rcx), %r8d
cmpl %edx, %r8d
cmovael %edx, %r8d
movq (%rbx), %r9
movslq 0x8(%rbx), %r10
leaq (%r10,%r10,2), %r10
cvtsd2ss %xmm0, %xmm0
shlq $0x4, %r10
cvtdq2pd 0x10(%r15,%rcx), %xmm1
mulpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
movlpd %xmm1, (%r9,%r10)
movss %xmm0, 0x8(%r9,%r10)
movb %sil, 0x2c(%r9,%r10)
movb %dil, 0x2d(%r9,%r10)
movb %r8b, 0x2e(%r9,%r10)
movq (%rbx), %rsi
movslq 0x8(%rbx), %rdi
cmpb $0x30, 0x9(%r15,%rcx)
sete %r8b
leaq (%rdi,%rdi,2), %r9
shlq $0x4, %r9
movb 0x2f(%rsi,%r9), %r10b
andb $-0x3, %r10b
addb %r8b, %r8b
orb %r10b, %r8b
movb %r8b, 0x2f(%rsi,%r9)
leal 0x1(%rdi), %esi
movl %esi, 0x8(%rbx)
addq $0x24, %rcx
cmpq %rcx, %rax
jne 0x300b7
movq %r15, %rdi
callq 0x13680
movl $0x1, %ebp
jmp 0x2fe9e
leaq 0xaf014(%rip), %rdi     # 0xdf193
jmp 0x2fe97
leaq 0xaf02d(%rip), %rdi     # 0xdf1b8
jmp 0x2fe95
leaq 0xaf049(%rip), %rdi     # 0xdf1e0
jmp 0x2fe95
leaq 0xaf065(%rip), %rdi     # 0xdf208
jmp 0x2fe95
leaq 0xaf079(%rip), %rdi     # 0xdf228
jmp 0x2fe95
leaq 0xaf0a4(%rip), %rdi     # 0xdf25f
jmp 0x2fe95
movq %rax, %rbx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x139b0
movq %rbx, %rdi
callq 0x138b0