// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDUtil.cpp
// Function: gaps::RGBDCreateBoundaryChannel(gaps::R2Grid const&, gaps::R2Grid&, double)
int RGBDCreateBoundaryChannel(
  const R2Grid& input_depth_image, R2Grid& output_boundary_image,
  RNScalar depth_threshold)
{
  // Initialize position images
  output_boundary_image = input_depth_image; 
  output_boundary_image.Clear(R2_GRID_UNKNOWN_VALUE);

  // Mark bottom and top border boundaries
  for (int i = 0; i < output_boundary_image.XResolution(); i++) {
    for (int j = 0; j < output_boundary_image.YResolution(); j++) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
    for (int j = output_boundary_image.YResolution()-1; j >= 0; j--) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
  }

  // Mark left and right border boundaries
  for (int j = 0; j < output_boundary_image.YResolution(); j++) {
    for (int i = 0; i < output_boundary_image.XResolution(); i++) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
    for (int i = output_boundary_image.XResolution()-1; i >= 0; i--) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
  }

  // Create copy of depth image with holes filled
  R2Grid filled_input_depth_image(input_depth_image);
  filled_input_depth_image.Substitute(0, R2_GRID_UNKNOWN_VALUE);
  filled_input_depth_image.FillHoles();

  // Mark interior boundaries, silhouettes, and shadows
  for (int i = 1; i < output_boundary_image.XResolution()-1; i++) {
    for (int j = 1; j < output_boundary_image.YResolution()-1; j++) {
      // Get original depth
      RNScalar depth = input_depth_image.GridValue(i, j);

      // Check if in hole
      if (RNIsNegativeOrZero(depth)) {
        output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
        continue;
      }

      // Get filled depth
      depth = filled_input_depth_image.GridValue(i, j);
      if (RNIsNegativeOrZero(depth)) continue;

      // Check depth relative to neighbors
      for (int k = 0; k < 4; k++) {
        int s = (k < 3) ? -1 : 0;
        int t = (k < 3) ? k-1 : -1;

        // Get depth on one side
        RNScalar depthA = filled_input_depth_image.GridValue(i-s, j-t);
        if (RNIsNegativeOrZero(depthA)) {
          output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
          break;
        }

        // Get depth on other side
        RNScalar depthB = filled_input_depth_image.GridValue(i+s, j+t);
        if (RNIsNegativeOrZero(depthB)) {
          output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
          break;
        }

        // Check differences of depth for shadow/silhouette
        RNScalar deltaA = depth - depthA;
        RNScalar deltaB = depthB - depth;
        RNScalar threshold = depth * depth_threshold;
        if (threshold < 0.1) threshold = 0.1;
        if (deltaA < -threshold) {
          if (deltaA < 4*deltaB) {
            output_boundary_image.SetGridValue(i-s, j-t, RGBD_SHADOW_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SILHOUETTE_BOUNDARY);
          }
        }
        else if (deltaA > threshold) {
          if (deltaA > 4*deltaB) {
            output_boundary_image.SetGridValue(i-s, j-t, RGBD_SILHOUETTE_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SHADOW_BOUNDARY);
          }
        }
        if (deltaB < -threshold) {
          if (deltaB < 4*deltaA) {
            output_boundary_image.SetGridValue(i+s, j+t, RGBD_SILHOUETTE_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SHADOW_BOUNDARY);
          }
        }
        else if (deltaB > threshold) {
          if (deltaB > 4*deltaA) {
            output_boundary_image.SetGridValue(i+s, j+t, RGBD_SHADOW_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SILHOUETTE_BOUNDARY);
          }
        }
      }
    }
  }

  // Return success 
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movsd %xmm0, 0x48(%rsp)
movq %rsi, %r13
movq %rdi, %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x5ffa2
movsd 0x9960f(%rip), %xmm0    # 0xbb018
movq %r13, %rdi
callq 0x617b0
movslq 0xc0(%r13), %rax
movabsq $0x3ff0000000000000, %rbp # imm = 0x3FF0000000000000
testq %rax, %rax
movq %r13, 0x10(%rsp)
movq %r12, 0x8(%rsp)
jle 0x21b64
movl 0xc4(%r13), %ecx
movq 0xb8(%r13), %rdx
movslq 0xc8(%r13), %rsi
movq 0xb8(%r12), %rdi
movslq 0xc8(%r12), %r8
movslq %ecx, %r9
shlq $0x3, %rsi
shlq $0x3, %r8
leaq 0x1(%r9), %r10
movq %r10, 0x28(%rsp)
leaq -0x8(,%r9,8), %r10
movq %r10, 0x20(%rsp)
xorl %ebx, %ebx
leaq 0xe3254(%rip), %r15     # 0x104cd8
xorl %r13d, %r13d
movl %ecx, 0x30(%rsp)
testl %ecx, %ecx
jle 0x21abc
movl $0x1, %ecx
movq %rdi, %r11
movq %rdx, %r12
movq %rbp, (%r12)
movsd (%r11), %xmm0
ucomisd (%r15), %xmm0
ja 0x21abc
addq %rsi, %r12
addq %r8, %r11
leaq 0x1(%rcx), %r14
cmpq %r9, %rcx
movq %r14, %rcx
jl 0x21a9a
movq 0x10(%rsp), %r14
movslq 0xc8(%r14), %r12
movq 0x8(%rsp), %rcx
movslq 0xc8(%rcx), %rbp
movq 0x20(%rsp), %r10
movq %r10, %r11
imulq %rbp, %r11
addq %rbx, %r11
addq 0xb8(%rcx), %r11
shlq $0x3, %rbp
movq %r10, %rcx
imulq %r12, %rcx
addq %rbx, %rcx
addq 0xb8(%r14), %rcx
negq %rbp
shlq $0x3, %r12
negq %r12
movq 0x28(%rsp), %r14
decq %r14
jle 0x21b32
movabsq $0x3ff0000000000000, %r10 # imm = 0x3FF0000000000000
movq %r10, (%rcx)
addq %r12, %rcx
movsd (%r11), %xmm0
addq %rbp, %r11
ucomisd (%r15), %xmm0
jbe 0x21b0e
incq %r13
addq $0x8, %rdx
addq $0x8, %rdi
addq $0x8, %rbx
cmpq %rax, %r13
movabsq $0x3ff0000000000000, %rbp # imm = 0x3FF0000000000000
movl 0x30(%rsp), %ecx
jne 0x21a8b
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %r12
jmp 0x21b6b
movl 0xc4(%r13), %ecx
testl %ecx, %ecx
jle 0x21c3a
movq 0xb8(%r13), %rdx
movslq 0xc8(%r13), %rsi
movq 0xb8(%r12), %rdi
movslq 0xc8(%r12), %r8
movl %ecx, %ecx
shlq $0x3, %r8
shlq $0x3, %rsi
xorl %r9d, %r9d
leaq 0xe3133(%rip), %r10     # 0x104cd8
xorl %r11d, %r11d
testl %eax, %eax
jle 0x21bcf
movl $0x1, %ebx
movq %rbp, -0x8(%rdx,%rbx,8)
movsd -0x8(%rdi,%rbx,8), %xmm0
ucomisd (%r10), %xmm0
ja 0x21bcf
leaq 0x1(%rbx), %r14
cmpq %rax, %rbx
movq %r14, %rbx
jl 0x21bb1
movq 0xb8(%r13), %r14
movslq 0xc8(%r13), %r15
movq 0xb8(%r12), %rbx
movslq 0xc8(%r12), %r12
imulq %r9, %r12
addq %r12, %rbx
addq $-0x8, %rbx
imulq %r9, %r15
addq %r15, %r14
addq $-0x8, %r14
movq %rax, %r15
testq %r15, %r15
jle 0x21c1f
movq %rbp, (%r14,%r15,8)
movsd (%rbx,%r15,8), %xmm0
decq %r15
ucomisd (%r10), %xmm0
jbe 0x21c06
incq %r11
addq %r8, %rdi
addq %rsi, %rdx
addq $0x8, %r9
cmpq %rcx, %r11
movq 0x8(%rsp), %r12
jne 0x21ba8
leaq 0x68(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x6008c
movsd 0x993c6(%rip), %xmm1    # 0xbb018
xorpd %xmm0, %xmm0
movq %r15, %rdi
callq 0x63f00
leaq 0x68(%rsp), %rdi
callq 0x60e90
movl 0xc0(%r13), %eax
movq %rax, 0x18(%rsp)
cmpl $0x3, %eax
jl 0x21f2f
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
movl 0xc4(%rax), %eax
movq %rax, 0x40(%rsp)
decl %eax
movq %rax, 0x28(%rsp)
movq 0xb8(%r12), %rax
movq %rax, 0x38(%rsp)
movslq 0xc8(%r12), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %r8d
movsd 0x99340(%rip), %xmm0    # 0xbb008
movapd 0x99380(%rip), %xmm1    # 0xbb050
movsd 0x9a3b8(%rip), %xmm2    # 0xbc090
movsd 0x9a3a0(%rip), %xmm3    # 0xbc080
cmpl $0x3, 0x40(%rsp)
jl 0x21f21
movq 0x10(%rsp), %rax
movslq 0xc8(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x120(%rsp), %r14
movslq 0x130(%rsp), %r15
movq 0x38(%rsp), %rcx
leaq (%rcx,%r8,8), %rcx
movq %rcx, 0x30(%rsp)
leaq (,%r8,8), %rcx
leaq (%r14,%r8,8), %rdx
movq %rdx, 0x50(%rsp)
addq 0xb8(%rax), %rcx
movq %rcx, 0x58(%rsp)
movl $0x1, %ecx
movq %rcx, %rax
imulq 0x20(%rsp), %rax
leaq 0xe2f8c(%rip), %rdx     # 0x104cd8
movsd (%rdx), %xmm5
movq 0x30(%rsp), %rdx
ucomisd (%rdx,%rax,8), %xmm5
jae 0x21ef7
movq %rcx, %rax
imulq %r15, %rax
movq 0x50(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm4
ucomisd %xmm4, %xmm5
jae 0x21f13
movapd %xmm4, %xmm6
mulsd 0x48(%rsp), %xmm6
movapd %xmm0, %xmm5
maxsd %xmm6, %xmm5
movapd %xmm5, %xmm6
xorpd %xmm1, %xmm6
movq 0x10(%rsp), %rdx
movq 0xb8(%rdx), %rax
movl 0xc8(%rdx), %ebx
movl %ebx, %edx
imull %ecx, %edx
movslq %edx, %rdx
movq %rdx, 0x8(%rsp)
leaq (%rax,%r8,8), %r11
xorl %ebp, %ebp
movl $0x3, %edx
leal -0x1(%rbp), %edi
movl %edx, %r9d
addl $-0x1, %r9d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmovael %esi, %edi
movl %r8d, %r10d
adcl $0x0, %r10d
movl %ecx, %esi
subl %edi, %esi
movl %esi, %r12d
imull %r15d, %r12d
xorl %r13d, %r13d
addl $-0x1, %edx
setb %r9b
adcl %r8d, %r12d
movslq %r12d, %r12
movsd (%r14,%r12,8), %xmm9
leaq 0xe2ed8(%rip), %r12     # 0x104cd8
movsd (%r12), %xmm8
ucomisd %xmm9, %xmm8
jae 0x21ef7
movb %r9b, %r13b
negl %r13d
addl %r8d, %r13d
addl %ecx, %edi
movl %edi, %r9d
imull %r15d, %r9d
addl %r13d, %r9d
movslq %r9d, %r9
movsd (%r14,%r9,8), %xmm7
ucomisd %xmm7, %xmm8
jae 0x21ef7
movapd %xmm4, %xmm8
subsd %xmm9, %xmm8
subsd %xmm4, %xmm7
ucomisd %xmm8, %xmm6
movapd %xmm7, %xmm9
mulsd %xmm2, %xmm9
jbe 0x21e6c
ucomisd %xmm8, %xmm9
movapd %xmm2, %xmm9
movapd %xmm3, %xmm10
ja 0x21e84
jmp 0x21e9e
ucomisd %xmm5, %xmm8
jbe 0x21e9e
ucomisd %xmm9, %xmm8
movapd %xmm3, %xmm9
movapd %xmm2, %xmm10
jbe 0x21e9e
imull %ebx, %esi
addl %r10d, %esi
movslq %esi, %rsi
movsd %xmm9, (%rax,%rsi,8)
movq 0x8(%rsp), %rsi
movsd %xmm10, (%r11,%rsi,8)
ucomisd %xmm7, %xmm6
mulsd %xmm2, %xmm8
jbe 0x21ebb
ucomisd %xmm7, %xmm8
movapd %xmm3, %xmm7
movapd %xmm2, %xmm8
ja 0x21ed1
jmp 0x21eea
ucomisd %xmm5, %xmm7
jbe 0x21eea
ucomisd %xmm8, %xmm7
movapd %xmm2, %xmm7
movapd %xmm3, %xmm8
jbe 0x21eea
imull %ebx, %edi
addl %r13d, %edi
movslq %edi, %rsi
movsd %xmm7, (%rax,%rsi,8)
movq 0x8(%rsp), %rsi
movsd %xmm8, (%r11,%rsi,8)
incl %ebp
cmpl $0x4, %ebp
jne 0x21dbf
jmp 0x21f13
movq %rcx, %rax
imulq 0x60(%rsp), %rax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq 0x58(%rsp), %rsi
movq %rdx, (%rsi,%rax,8)
incq %rcx
cmpq 0x28(%rsp), %rcx
jne 0x21d3c
incq %r8
cmpq 0x18(%rsp), %r8
jne 0x21ce0
leaq 0x68(%rsp), %rdi
callq 0x60264
movl $0x1, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x60264
movq %rbx, %rdi
callq 0x11a60