// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDImage.cpp
// Function: gaps::RGBDImage::TransformCameraToImage(gaps::R3Point const&, gaps::R2Point&) const
int RGBDImage::
TransformCameraToImage(const R3Point& camera_position, R2Point& image_position) const
{
  // Get/check depth
  RNScalar depth = -camera_position[2];
  if (RNIsNegativeOrZero(depth)) return 0;

  // Get/check intrinsics matrix
  const R3Matrix& intrinsics_matrix = Intrinsics();
  if (RNIsZero(intrinsics_matrix[0][0])) return 0;
  if (RNIsZero(intrinsics_matrix[1][1])) return 0;
  
  // Transform from position in image coordinates to camera coordinates (where camera is looking down -Z, up is +Y, right is +X)
  image_position[0] = intrinsics_matrix[0][2] + camera_position[0] * intrinsics_matrix[0][0] / depth;
  image_position[1] = intrinsics_matrix[1][2] + camera_position[1] * intrinsics_matrix[1][1] / depth;

  // Check image position
  if ((image_position[0] < 0) || (image_position[0] >= NPixels(RN_X))) return 0; 
  if ((image_position[1] < 0) || (image_position[1] >= NPixels(RN_Y))) return 0;

  // Return success
  return 1;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rdi
movl $0x2, %esi
callq 0x305d0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
leaq 0x18746a(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
cmpl $0x0, %eax
je 0x2aa71
movl $0x0, 0x74(%rsp)
jmp 0x2ac7c
movq 0x40(%rsp), %rdi
callq 0x226a0
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x238a0
movsd (%rax), %xmm0
leaq 0x187429(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a370
cmpl $0x0, %eax
je 0x2aab2
movl $0x0, 0x74(%rsp)
jmp 0x2ac7c
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x238a0
movsd 0x8(%rax), %xmm0
leaq 0x1873f3(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a370
cmpl $0x0, %eax
je 0x2aae8
movl $0x0, 0x74(%rsp)
jmp 0x2ac7c
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x238a0
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x305d0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x238a0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
mulsd (%rax), %xmm1
divsd 0x50(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x27a30
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x238a0
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x60(%rsp), %rdi
movl $0x1, %esi
callq 0x305d0
movsd %xmm0, 0x28(%rsp)
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x238a0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
mulsd 0x8(%rax), %xmm1
divsd 0x50(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x27a30
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x27a30
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
ja 0x2ac12
movq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x27a30
movq 0x40(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %esi, %esi
callq 0x1af30
movsd 0x8(%rsp), %xmm0
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2ac1c
movl $0x0, 0x74(%rsp)
jmp 0x2ac7c
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x27a30
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
ja 0x2ac6a
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x27a30
movq 0x40(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movl $0x1, %esi
callq 0x1af30
movsd (%rsp), %xmm0
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2ac74
movl $0x0, 0x74(%rsp)
jmp 0x2ac7c
movl $0x1, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)