// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelConstraint.cpp
// Function: gaps::R3SurfelViewConstraint::R3SurfelViewConstraint(gaps::R3Viewer const&, gaps::R2Grid const*, int)
R3SurfelViewConstraint::
R3SurfelViewConstraint(
  const R3Viewer& viewer,
  const R2Grid *image_mask,
  RNBoolean must_be_inside_frustum)
  : frustum(viewer.Camera().Frustum()),
    world_to_camera(viewer.Camera().CoordSystem().InverseMatrix()),
    w(viewer.Viewport().Width()),
    h(viewer.Viewport().Height()),
    fx(0),
    fy(0),
    cx(0.5*viewer.Viewport().Width()),
    cy(0.5*viewer.Viewport().Height()),
    neardist(viewer.Camera().Near()),
    fardist(viewer.Camera().Far()),
    image_mask(image_mask),
    must_be_inside_frustum(must_be_inside_frustum)
{
  // Update focal lengths
  const R3Camera& camera = viewer.Camera();
  RNScalar xfov = camera.XFOV();
  RNScalar yfov = camera.YFOV();
  RNScalar tan_xfov = tan(xfov);
  RNScalar tan_yfov = tan(yfov);
  this->fx = (tan_xfov != 0) ? 0.5 * w / tan(xfov) : 0;
  this->fy = (tan_yfov != 0) ? 0.5 * h / tan(yfov) : 0;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq %rdx, 0x140(%rsp)
movl %ecx, 0x13c(%rsp)
movq 0x150(%rsp), %rdi
movq %rdi, 0xe8(%rsp)
callq 0x50470
movq 0xe8(%rsp), %rax
leaq 0x19bede(%rip), %rcx    # 0x1ee3b8
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, 0xf0(%rsp)
movq 0x148(%rsp), %rdi
callq 0x549f0
movq %rax, 0xf8(%rsp)
jmp 0x52500
movq 0xf8(%rsp), %rsi
movq 0xf0(%rsp), %rdi
callq 0x54a00
jmp 0x52517
movq 0xe8(%rsp), %rax
addq $0x150, %rax            # imm = 0x150
movq %rax, 0xd8(%rsp)
movq 0x148(%rsp), %rdi
callq 0x549f0
movq %rax, 0xe0(%rsp)
jmp 0x52544
movq 0xe0(%rsp), %rdi
callq 0x54ac0
movq %rax, 0xd0(%rsp)
jmp 0x5255b
movq 0xd0(%rsp), %rsi
movq 0xd8(%rsp), %rdi
callq 0xd5640
jmp 0x52572
movq 0xe8(%rsp), %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq %rax, 0xc0(%rsp)
movq 0x148(%rsp), %rdi
callq 0x54ad0
movq %rax, 0xc8(%rsp)
jmp 0x5259f
movq 0xc8(%rsp), %rdi
callq 0x54af0
movl %eax, 0xbc(%rsp)
jmp 0x525b5
movq 0xe8(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl 0xbc(%rsp), %edx
movl %edx, (%rcx)
addq $0x1d4, %rax            # imm = 0x1D4
movq %rax, 0xa8(%rsp)
movq 0x148(%rsp), %rdi
callq 0x54ad0
movq %rax, 0xb0(%rsp)
jmp 0x525f3
movq 0xb0(%rsp), %rdi
callq 0x54b00
movl %eax, 0xa4(%rsp)
jmp 0x52609
movq 0xe8(%rsp), %rax
movq 0xa8(%rsp), %rcx
movl 0xa4(%rsp), %edx
movl %edx, (%rcx)
movq $0x0, 0x1d8(%rax)
movq $0x0, 0x1e0(%rax)
addq $0x1e8, %rax            # imm = 0x1E8
movq %rax, 0x90(%rsp)
movq 0x148(%rsp), %rdi
callq 0x54ad0
movq %rax, 0x98(%rsp)
jmp 0x5265d
movq 0x98(%rsp), %rdi
callq 0x54af0
movl %eax, 0x8c(%rsp)
jmp 0x52673
movq 0xe8(%rsp), %rax
movq 0x90(%rsp), %rcx
movl 0x8c(%rsp), %edx
cvtsi2sd %edx, %xmm0
movsd 0x151ac2(%rip), %xmm1   # 0x1a4158
mulsd %xmm1, %xmm0
movsd %xmm0, (%rcx)
addq $0x1f0, %rax            # imm = 0x1F0
movq %rax, 0x78(%rsp)
movq 0x148(%rsp), %rdi
callq 0x54ad0
movq %rax, 0x80(%rsp)
jmp 0x526c0
movq 0x80(%rsp), %rdi
callq 0x54b00
movl %eax, 0x74(%rsp)
jmp 0x526d3
movq 0xe8(%rsp), %rax
movq 0x78(%rsp), %rcx
movl 0x74(%rsp), %edx
cvtsi2sd %edx, %xmm0
movsd 0x151a68(%rip), %xmm1   # 0x1a4158
mulsd %xmm1, %xmm0
movsd %xmm0, (%rcx)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x60(%rsp)
movq 0x148(%rsp), %rdi
callq 0x549f0
movq %rax, 0x68(%rsp)
jmp 0x52717
movq 0x68(%rsp), %rdi
callq 0x54b10
movsd %xmm0, 0x58(%rsp)
jmp 0x52729
movq 0xe8(%rsp), %rax
movq 0x60(%rsp), %rcx
movsd 0x58(%rsp), %xmm0
movsd %xmm0, (%rcx)
addq $0x200, %rax            # imm = 0x200
movq %rax, 0x48(%rsp)
movq 0x148(%rsp), %rdi
callq 0x549f0
movq %rax, 0x50(%rsp)
jmp 0x5275f
movq 0x50(%rsp), %rdi
callq 0x54b20
movsd %xmm0, 0x40(%rsp)
jmp 0x52771
movq 0xe8(%rsp), %rax
movq 0x48(%rsp), %rcx
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rcx)
movq 0x140(%rsp), %rcx
movq %rcx, 0x208(%rax)
movl 0x13c(%rsp), %ecx
movl %ecx, 0x210(%rax)
movq 0x148(%rsp), %rdi
callq 0x549f0
movq %rax, 0x38(%rsp)
jmp 0x527b8
movq 0x38(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rdi
callq 0x54b30
movsd %xmm0, 0x30(%rsp)
jmp 0x527da
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x118(%rsp)
movq 0x120(%rsp), %rdi
callq 0x54b40
movsd %xmm0, 0x28(%rsp)
jmp 0x527fe
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x118(%rsp), %xmm0
callq 0x132f0
movsd %xmm0, 0x108(%rsp)
movsd 0x110(%rsp), %xmm0
callq 0x132f0
movsd %xmm0, 0x100(%rsp)
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x52851
jp 0x52851
jmp 0x52896
movq 0xe8(%rsp), %rax
cvtsi2sdl 0x1d0(%rax), %xmm1
movsd 0x1518ef(%rip), %xmm0   # 0x1a4158
mulsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x118(%rsp), %xmm0
callq 0x132f0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x528a1
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x528a1
movq 0xe8(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x1d8(%rax)
movsd 0x100(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x528cd
jp 0x528cd
jmp 0x52912
movq 0xe8(%rsp), %rax
cvtsi2sdl 0x1d4(%rax), %xmm1
movsd 0x151873(%rip), %xmm0   # 0x1a4158
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x110(%rsp), %xmm0
callq 0x132f0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x5291d
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x5291d
movq 0xe8(%rsp), %rax
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rax)
addq $0x158, %rsp            # imm = 0x158
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
jmp 0x52972
movq 0xf0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
callq 0x54b50
movq 0xe8(%rsp), %rdi
callq 0x50490
movq 0x130(%rsp), %rdi
callq 0x13970
nopl (%rax)