// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelConstraint.cpp
// Function: gaps::R3SurfelViewConstraint::R3SurfelViewConstraint(gaps::R3Point const&, gaps::R3Vector const&, gaps::R3Vector const&, int, int, double, double, double, double, double, double, gaps::R2Grid const*, int)
R3SurfelViewConstraint::
R3SurfelViewConstraint(const R3Point& world_viewpoint,
  const R3Vector& world_towards, const R3Vector& world_up,
  int w, int h, RNScalar fx, RNScalar fy, RNScalar cx, RNScalar cy,
  RNLength neardist, RNLength fardist, 
  const R2Grid *image_mask,
  RNBoolean must_be_inside_frustum)
  : frustum(world_viewpoint, world_towards, world_up, atan(0.5*w/fx), atan(0.5*h/fy), neardist, fardist),
    world_to_camera(),
    w(w), h(h),
    fx(fx), fy(fy),
    cx(cx), cy(cy),
    neardist(neardist), fardist(fardist),
    image_mask(image_mask),
    must_be_inside_frustum(must_be_inside_frustum)
{
  // Compute default image center
  if (this->cx <= 0) this->cx = 0.5*w;
  if (this->cy <= 0) this->cy = 0.5*h;

  // Compute default near and far distances
  if (this->neardist <= 0) this->neardist = RN_EPSILON;
  if (this->fardist <= 0) this->fardist = RN_INFINITY;

  // Compute world_to_camera matrix
  R3CoordSystem cs(world_viewpoint, R3Triad(world_towards, world_up));
  world_to_camera = cs.InverseMatrix();
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movl 0x1e8(%rsp), %eax
movq 0x1e0(%rsp), %rax
movq %rdi, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
movq %rdx, 0x1c0(%rsp)
movq %rcx, 0x1b8(%rsp)
movl %r8d, 0x1b4(%rsp)
movl %r9d, 0x1b0(%rsp)
movsd %xmm0, 0x1a8(%rsp)
movsd %xmm1, 0x1a0(%rsp)
movsd %xmm2, 0x198(%rsp)
movsd %xmm3, 0x190(%rsp)
movsd %xmm4, 0x188(%rsp)
movsd %xmm5, 0x180(%rsp)
movq 0x1d0(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x50470
movq 0x10(%rsp), %rax
leaq 0x19b63e(%rip), %rcx    # 0x1ee3b8
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x38(%rsp)
cvtsi2sdl 0x1b4(%rsp), %xmm0
movsd 0x15139a(%rip), %xmm1   # 0x1a4158
movsd %xmm1, 0x18(%rsp)
mulsd %xmm1, %xmm0
movsd 0x1a8(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x13480
movsd 0x18(%rsp), %xmm1
movsd %xmm0, 0x40(%rsp)
cvtsi2sdl 0x1b0(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd 0x1a0(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x13480
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
movsd 0x188(%rsp), %xmm2
movsd 0x180(%rsp), %xmm3
callq 0xc5850
jmp 0x52e3b
movq 0x10(%rsp), %rdi
addq $0x150, %rdi            # imm = 0x150
callq 0xd8b30
jmp 0x52e4e
movq 0x10(%rsp), %rax
movl 0x1b4(%rsp), %ecx
movl %ecx, 0x1d0(%rax)
movl 0x1b0(%rsp), %ecx
movl %ecx, 0x1d4(%rax)
movsd 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x1d8(%rax)
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rax)
movsd 0x198(%rsp), %xmm0
movsd %xmm0, 0x1e8(%rax)
movsd 0x190(%rsp), %xmm0
movsd %xmm0, 0x1f0(%rax)
movsd 0x188(%rsp), %xmm0
movsd %xmm0, 0x1f8(%rax)
movsd 0x180(%rsp), %xmm0
movsd %xmm0, 0x200(%rax)
movq 0x1e0(%rsp), %rcx
movq %rcx, 0x208(%rax)
movl 0x1e8(%rsp), %ecx
movl %ecx, 0x210(%rax)
xorps %xmm0, %xmm0
ucomisd 0x1e8(%rax), %xmm0
jb 0x52f5c
movq 0x10(%rsp), %rax
cvtsi2sdl 0x1b4(%rsp), %xmm1
movsd 0x151246(%rip), %xmm0   # 0x1a4158
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1e8(%rax)
jmp 0x52f5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0x53060
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
callq 0x54b50
jmp 0x53060
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
ucomisd 0x1f0(%rax), %xmm0
jb 0x52f90
movq 0x10(%rsp), %rax
cvtsi2sdl 0x1b0(%rsp), %xmm1
movsd 0x1511d4(%rip), %xmm0   # 0x1a4158
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1f0(%rax)
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
ucomisd 0x1f8(%rax), %xmm0
jb 0x52fba
movq 0x10(%rsp), %rax
leaq 0x1a959a(%rip), %rcx    # 0x1fc548
movsd (%rcx), %xmm0
movsd %xmm0, 0x1f8(%rax)
movq 0x10(%rsp), %rax
xorps %xmm0, %xmm0
ucomisd 0x200(%rax), %xmm0
jb 0x52fe4
movq 0x10(%rsp), %rax
leaq 0x1a9580(%rip), %rcx    # 0x1fc558
movsd (%rcx), %xmm0
movsd %xmm0, 0x200(%rax)
movq 0x1c8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x1c0(%rsp), %rsi
movq 0x1b8(%rsp), %rdx
leaq 0xc8(%rsp), %rdi
callq 0xd5d60
jmp 0x53010
movq 0x8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xd5570
jmp 0x5302c
leaq 0x48(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xd5640
jmp 0x53040
movq 0x10(%rsp), %rdi
addq $0x150, %rdi            # imm = 0x150
leaq 0x48(%rsp), %rsi
callq 0xdb240
jmp 0x53058
addq $0x1d8, %rsp            # imm = 0x1D8
retq
movq 0x10(%rsp), %rdi
callq 0x50490
movq 0x178(%rsp), %rdi
callq 0x13970
nopw (%rax,%rax)