// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDUtil.cpp
// Function: gaps::RGBDCreatePositionChannels(gaps::R2Grid const&, gaps::R2Grid&, gaps::R2Grid&, gaps::R2Grid&, gaps::R3Matrix const&, gaps::R4Matrix const&)
int RGBDCreatePositionChannels(
  const R2Grid& input_undistorted_depth_image,
  R2Grid& output_px_image, R2Grid& output_py_image, R2Grid& output_pz_image,
  const R3Matrix& intrinsics_matrix, const R4Matrix& camera_to_world_matrix)
{
  // Initialize position images
  output_px_image = input_undistorted_depth_image; 
  output_px_image.Clear(R2_GRID_UNKNOWN_VALUE);
  output_py_image = output_px_image;
  output_pz_image = output_px_image;

  // Fill position images
  for (int i = 0; i < input_undistorted_depth_image.XResolution(); i++) {
    for (int j = 0; j < input_undistorted_depth_image.YResolution(); j++) {
      // Get depth
      RNScalar depth = input_undistorted_depth_image.GridValue(i, j);
      if (RNIsNegativeOrZero(depth)) continue;

      // Get position in camera coordinate system 
      RNScalar x = ((i+0.5) - intrinsics_matrix[0][2]) * depth / intrinsics_matrix[0][0];
      RNScalar y = ((j+0.5) - intrinsics_matrix[1][2]) * depth / intrinsics_matrix[1][1];
      RNScalar z = -depth;
      R3Point position(x, y, z);

      // Transform by extrinsics matrix
      position = camera_to_world_matrix * position;

      // Fill position images
      output_px_image.SetGridValue(i, j, position.X());
      output_py_image.SetGridValue(i, j, position.Y());
      output_pz_image.SetGridValue(i, j, position.Z());
    }
  }

  // Return success 
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x28(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbp
movq %rsi, %rdi
movq %rbp, %rsi
callq 0x5872a
movsd 0x8841a(%rip), %xmm0    # 0xa8010
movq %r12, %rdi
callq 0x59dcc
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x5872a
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rsi
callq 0x5872a
xorl %ebx, %ebx
leaq 0xd80cc(%rip), %r12     # 0xf7cf8
cmpl 0xc0(%rbp), %ebx
jge 0x1fd6e
cvtsi2sd %ebx, %xmm0
addsd 0x883d4(%rip), %xmm0    # 0xa8018
movsd %xmm0, 0x30(%rsp)
xorl %r15d, %r15d
cmpl 0xc4(%rbp), %r15d
jge 0x1fd67
movq 0xb8(%rbp), %rax
movl 0xc8(%rbp), %ecx
imull %r15d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movsd (%r12), %xmm0
ucomisd %xmm2, %xmm0
jae 0x1fd5f
movsd 0x30(%rsp), %xmm0
movq 0x8(%rsp), %rax
subsd 0x10(%rax), %xmm0
mulsd %xmm2, %xmm0
divsd (%rax), %xmm0
cvtsi2sd %r15d, %xmm1
addsd 0x8836e(%rip), %xmm1    # 0xa8018
subsd 0x28(%rax), %xmm1
mulsd %xmm2, %xmm1
divsd 0x20(%rax), %xmm1
xorpd 0x88390(%rip), %xmm2    # 0xa8050
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x45d4c
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x447df
movq %r13, %rdi
movq %r14, %rsi
callq 0x46036
movsd 0x38(%rsp), %xmm0
movq 0x10(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %r15d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x40(%rsp), %xmm0
movq 0x18(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %r15d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x48(%rsp), %xmm0
movq 0x20(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %r15d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
incl %r15d
jmp 0x1fc4d
incl %ebx
jmp 0x1fc2c
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq