// File: /workspace/github_cpp_makefile_star2_O2/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, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x70(%rsp), %r12
andq $0x0, (%r12)
xorl %r15d, %r15d
pushq $0x1
popq %r13
pushq $0x4
popq %rbp
leaq 0x9c606(%rip), %rbx     # 0xc9288
pushq $0x5
popq %r14
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq 0x8(%rsp), %rcx
callq 0x137d0
cmpq $0x4, %rax
jne 0x2ccb7
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x13520
testl %eax, %eax
je 0x2ccc3
addq $0x4, %r15
jmp 0x2cc86
leaq 0x9c49f(%rip), %rdi     # 0xc915d
jmp 0x2cf82
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x9c462
leaq 0x24(%rsp), %rbx
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x137d0
cmpq $0x4, %rax
jne 0x2cf4e
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rdx, %rsi
movq %r14, %rcx
callq 0x137d0
cmpq $0x1, %rax
jne 0x2cf57
leaq 0x25(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %r14, %rcx
callq 0x137d0
cmpq $0x1, %rax
jne 0x2cf60
leaq 0x26(%rsp), %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x137d0
cmpq $0x1, %rax
jne 0x2cf69
leaq 0x28(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x137d0
cmpq $0x1, %rax
jne 0x2cf72
leaq 0x30(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x6
popq %rdx
movq %r14, %rcx
callq 0x137d0
cmpq $0x6, %rax
jne 0x2cf7b
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movups 0x30(%rsp), %xmm3
movaps %xmm3, 0x60(%rsp)
movsd 0x40(%rsp), %xmm3
movsd %xmm3, 0x10(%rsp)
addsd 0x9c03c(%rip), %xmm0    # 0xc8df0
addsd 0x9c03c(%rip), %xmm1    # 0xc8df8
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x781bc
movq 0x18(%rsp), %rbp
leaq 0x10(%rbp), %rdi
movq %rbx, %rsi
callq 0x784a6
movq 0x28(%rsp), %r14
movq %r14, %r13
shlq $0x20, %r13
movslq %r14d, %rbx
pushq $0x30
popq %rcx
movq %rbx, %rax
mulq %rcx
pushq $-0x1
popq %r15
cmovnoq %rax, %r15
movq %r15, %rdi
callq 0x13030
movq %rax, %r12
testq %r13, %r13
je 0x2ce23
imulq $0x30, %rbx, %rbx
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
callq 0x29118
addq $0x30, %r13
cmpq %r13, %rbx
jne 0x2ce11
movq %r12, (%rbp)
movabsq $0x2400000000, %rax     # imm = 0x2400000000
movq %r14, %rdi
imulq %rax, %rdi
sarq $0x20, %rdi
callq 0x13030
movq %rax, %r15
xorl %ecx, %ecx
pushq $0x24
popq %rbx
movq 0x8(%rsp), %r13
movl %ecx, %r12d
movl %r14d, %eax
subl %ecx, %eax
jle 0x2ce74
movl %r12d, %edi
addq %r15, %rdi
movslq %eax, %rdx
movq %rbx, %rsi
movq %r13, %rcx
callq 0x137d0
leal (%r12,%rax), %ecx
testl %eax, %eax
jg 0x2ce4e
andl $0x0, 0x8(%rbp)
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
imulq $0x24, %r12, %rcx
movw $0xff, %dx
movl $0xff, %esi
xorl %edi, %edi
movapd 0x60(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
cmpq %rax, %rcx
je 0x2cf9f
cvtsi2sdl 0x18(%r15,%rax), %xmm0
mulsd %xmm3, %xmm0
movzwl 0x1d(%r15,%rax), %r8d
movzwl 0x1f(%r15,%rax), %r9d
movzwl 0x21(%r15,%rax), %r10d
cmpw %dx, %r8w
cmovael %esi, %r8d
cmpw %dx, %r9w
cmovael %esi, %r9d
cmpw %dx, %r10w
cmovael %esi, %r10d
cvtsd2ss %xmm0, %xmm0
movq (%rbp), %r11
cvtdq2pd 0x10(%r15,%rax), %xmm1
movslq %edi, %rdi
mulpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
imulq $0x30, %rdi, %rdi
movlpd %xmm1, (%r11,%rdi)
movss %xmm0, 0x8(%r11,%rdi)
movb %r8b, 0x2c(%r11,%rdi)
movb %r9b, 0x2d(%r11,%rdi)
movb %r10b, 0x2e(%r11,%rdi)
movq (%rbp), %rdi
movslq 0x8(%rbp), %r8
cmpb $0x30, 0x9(%r15,%rax)
sete %r9b
imulq $0x30, %r8, %r10
movb 0x2f(%rdi,%r10), %r11b
andb $-0x3, %r11b
addb %r9b, %r9b
orb %r11b, %r9b
movb %r9b, 0x2f(%rdi,%r10)
leal 0x1(%r8), %edi
movl %edi, 0x8(%rbp)
addq $0x24, %rax
jmp 0x2ce9c
leaq 0x9c22e(%rip), %rdi     # 0xc9183
jmp 0x2cf82
leaq 0x9c24a(%rip), %rdi     # 0xc91a8
jmp 0x2cf82
leaq 0x9c269(%rip), %rdi     # 0xc91d0
jmp 0x2cf82
leaq 0x9c288(%rip), %rdi     # 0xc91f8
jmp 0x2cf82
leaq 0x9c29f(%rip), %rdi     # 0xc9218
jmp 0x2cf82
leaq 0x9c2cd(%rip), %rdi     # 0xc924f
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x9ce26
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x136a0
pushq $0x1
popq %rbx
jmp 0x2cf8b
movq %rax, %rbx
movq %r12, %rdi
movq %r15, %rsi
callq 0x139f0
movq %rbx, %rdi
callq 0x138f0