// File: /workspace/github_cpp_makefile_star2_O1/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 0x60d3c
movsd 0x9b91c(%rip), %xmm0    # 0xbd018
movq %r12, %rdi
callq 0x62582
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x60d3c
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rsi
callq 0x60d3c
cmpl $0x0, 0xc0(%rbp)
jle 0x2188a
xorl %ebx, %ebx
leaq 0xe55a9(%rip), %r12     # 0x106ce8
cmpl $0x0, 0xc4(%rbp)
jle 0x2187c
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
addsd 0x9b8c5(%rip), %xmm0    # 0xbd020
movsd %xmm0, 0x30(%rsp)
xorl %r15d, %r15d
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 0x2186c
movsd 0x30(%rsp), %xmm0
movq 0x8(%rsp), %rax
subsd 0x10(%rax), %xmm0
mulsd %xmm2, %xmm0
divsd (%rax), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
addsd 0x9b869(%rip), %xmm1    # 0xbd020
subsd 0x28(%rax), %xmm1
mulsd %xmm2, %xmm1
divsd 0x20(%rax), %xmm1
xorpd 0x9b883(%rip), %xmm2    # 0xbd050
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x4be46
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x4a83e
movq %r13, %rdi
movq %r14, %rsi
callq 0x4c1b4
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
cmpl 0xc4(%rbp), %r15d
jl 0x21764
incl %ebx
cmpl 0xc0(%rbp), %ebx
jl 0x2173f
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq