// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDImage.cpp
// Function: gaps::RGBDImage::ComputeMesh(gaps::R3Mesh&, double, double)
int RGBDImage::
ComputeMesh(R3Mesh& mesh, RNLength max_depth, RNLength max_silhouette_factor)
{
  // Read the channels
  ReadChannels();

  // Allocate temporary array of vertex pointers
  int nvertices = NPixels(RN_X) * NPixels(RN_Y);
  R3MeshVertex **vertices = new R3MeshVertex * [ nvertices ];
  for (int i = 0; i < nvertices; i++) vertices[i] = NULL;

  // Compute boundary image ???
  R2Grid boundary_image(NPixels(RN_X), NPixels(RN_Y));
  if (!RGBDCreateBoundaryChannel(this, boundary_image, max_silhouette_factor)) return 0;
  boundary_image.Substitute(R2_GRID_UNKNOWN_VALUE, 0);
         
  // Create vertices
  for (int j = 0; j < NPixels(RN_Y); j++) {
    for (int i = 0; i < NPixels(RN_X); i++) {
      // Check depth
      RNScalar depth = PixelDepth(i, j);
      if (depth == R2_GRID_UNKNOWN_VALUE) continue;
      if (RNIsNegativeOrZero(depth)) continue;
      if ((max_depth > 0) && (depth > max_depth)) continue;

      // Check if on border ???
      unsigned int b = (unsigned int) (boundary_image.GridValue(i, j) + 0.5);
      if (b & RGBD_BORDER_BOUNDARY) continue;
      // if (b) continue; 
      
      // Compute vertex info
      R3Point position = PixelWorldPosition(i, j);
      RNRgb color = PixelColor(i, j);
      R2Point texcoords(i, j);

      // Create vertex
      int vertex_index = j*NPixels(RN_X) + i;
      vertices[vertex_index] = mesh.CreateVertex(position, R3zero_vector, color, texcoords);
    }
  }

  // Create faces
  for (int i = 0; i < NPixels(RN_X)-1; i++) {
    for (int j = 0; j < NPixels(RN_Y)-1; j++) {
      // Get vertices
      R3MeshVertex *v00 = vertices[(j+0)*NPixels(RN_X) + (i+0)];
      R3MeshVertex *v01 = vertices[(j+1)*NPixels(RN_X) + (i+0)];
      R3MeshVertex *v10 = vertices[(j+0)*NPixels(RN_X) + (i+1)];
      R3MeshVertex *v11 = vertices[(j+1)*NPixels(RN_X) + (i+1)];

      // Get boundary flags
      unsigned int b00 = (unsigned int) (boundary_image.GridValue(i, j) + 0.5);
      unsigned int b10 = (unsigned int) (boundary_image.GridValue(i+1, j) + 0.5);
      unsigned int b01 = (unsigned int) (boundary_image.GridValue(i, j+1) + 0.5);
      unsigned int b11 = (unsigned int) (boundary_image.GridValue(i+1, j+1) + 0.5);
      unsigned int bad_combination = RGBD_SILHOUETTE_BOUNDARY | RGBD_SHADOW_BOUNDARY;

      // Create faces
      if (!v00) {
        if (v10 && v11 && v01) {
          if (((b10 | b11 | b01) & bad_combination) != bad_combination) {
            mesh.CreateFace(v10, v11, v01);
          }
        }
      }
      else if (!v10) {
        if (v00 && v11 && v01) {
          if (((b00 | b11 | b01) & bad_combination) != bad_combination) {
            mesh.CreateFace(v00, v11, v01);
          }
        }
      }
      else if (!v01) {
        if (v00 && v10 && v11) {
          if (((b00 | b10 | b11) & bad_combination) != bad_combination) {
            mesh.CreateFace(v00, v10, v11);
          }
        }
      }
      else if (!v11) {
        if (v00 && v10 && v01) {
          if (((b00 | b10 | b01) & bad_combination) != bad_combination) {
            mesh.CreateFace(v00, v10, v01);
          }
        }
      }
      else {
        assert(v00 && v01 && v10 && v11);
        if (((b00 | b10 | b11) & bad_combination) != bad_combination) {
          mesh.CreateFace(v00, v10, v11);
        }
        if (((b00 | b11 | b01) & bad_combination) != bad_combination) {
          mesh.CreateFace(v00, v11, v01);
        }
      } 
    }
  }

  // Delete temporary array of vertex pointers
  delete [] vertices;

  // Release the channels
  ReleaseChannels();

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movsd %xmm1, (%rsp)
movaps %xmm0, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movq (%rdi), %rax
callq *0xf8(%rax)
movl 0x28(%rbx), %ebp
movl 0x2c(%rbx), %r12d
movl %r12d, %r14d
imull %ebp, %r14d
movslq %r14d, %r15
leaq (,%r15,8), %rax
testl %r15d, %r15d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x11040
movq %rax, %r13
testl %r15d, %r15d
jle 0x211f1
movl %r14d, %edx
shlq $0x3, %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x11310
movq %r13, 0x10(%rsp)
leaq 0x98(%rsp), %r13
movq %r13, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x603d0
movq %rbx, %rdi
movq %r13, %rsi
movsd (%rsp), %xmm0
callq 0x21acf
testl %eax, %eax
je 0x21606
leaq 0x98(%rsp), %rdi
movsd 0x9bde5(%rip), %xmm0    # 0xbd018
xorpd %xmm1, %xmm1
callq 0x64d52
cmpl $0x0, 0x2c(%rbx)
movsd 0x9bdd0(%rip), %xmm2    # 0xbd018
jle 0x21419
leaq 0xe5a93(%rip), %r14     # 0x106ce8
xorpd %xmm0, %xmm0
cmpltpd 0x20(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
leaq 0x80(%rsp), %r15
xorl %ebp, %ebp
cmpl $0x0, 0x28(%rbx)
jle 0x2140e
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x18(%rsp)
movslq %ebp, %rax
movq %rax, (%rsp)
xorl %r13d, %r13d
xorpd %xmm0, %xmm0
cmpl $0x4, 0x24(%rbx)
jl 0x212df
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x212c5
movq 0xb8(%rax), %rcx
movslq 0xc8(%rax), %rax
imulq (%rsp), %rax
addq %r13, %rax
movsd (%rcx,%rax,8), %xmm0
jmp 0x212df
leaq 0x9cc42(%rip), %rdi     # 0xbdf0e
xorl %eax, %eax
callq 0x79092
movsd 0x9bd3d(%rip), %xmm2    # 0xbd018
movapd %xmm2, %xmm0
ucomisd %xmm2, %xmm0
jne 0x212eb
jnp 0x21401
movsd (%r14), %xmm1
ucomisd %xmm0, %xmm1
jae 0x21401
movapd 0x20(%rsp), %xmm1
cmpltpd %xmm0, %xmm1
andpd 0x40(%rsp), %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x21401
movq 0x150(%rsp), %rax
movslq 0x160(%rsp), %rcx
imulq (%rsp), %rcx
addq %r13, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x9bce4(%rip), %xmm0    # 0xbd020
cvttsd2si %xmm0, %rax
testb $0x1, %al
jne 0x21401
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movq %r15, %rdi
movsd 0x18(%rsp), %xmm1
callq 0x6fee6
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
xorpd %xmm2, %xmm2
leaq 0x58(%rsp), %rdi
callq 0x4be46
movq %r15, %rdi
leaq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x16443
movq %r15, %r12
movq %r15, %rdi
movq %rbx, %rsi
movl %r13d, %edx
movl %ebp, %r15d
movl %ebp, %ecx
callq 0x1d9d8
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
movsd 0x18(%rsp), %xmm1
callq 0x6fee6
movl 0x28(%rbx), %r14d
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
leaq 0x58(%rsp), %rsi
leaq 0x9db73(%rip), %rdx     # 0xbef40
movq %r12, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
movl %r15d, %ebp
imull %r15d, %r14d
addl %r13d, %r14d
movslq %r14d, %rcx
movq 0x10(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
movsd 0x9bc21(%rip), %xmm2    # 0xbd018
leaq 0xe58ea(%rip), %r14     # 0x106ce8
movq %r12, %r15
incq %r13
cmpl 0x28(%rbx), %r13d
jl 0x21291
incl %ebp
cmpl 0x2c(%rbx), %ebp
jl 0x21270
cmpl $0x2, 0x28(%rbx)
jl 0x215e9
xorl %r14d, %r14d
movsd 0x9bbf2(%rip), %xmm1    # 0xbd020
movq %rbx, 0x38(%rsp)
cmpl $0x2, 0x2c(%rbx)
jl 0x215d8
leal 0x1(%r14), %r15d
xorl %r13d, %r13d
movl %r13d, %ecx
movl 0x28(%rbx), %edi
movl %edi, %eax
imull %r13d, %eax
leal (%rax,%r14), %edx
movslq %edx, %rdx
movq 0x10(%rsp), %r10
movq (%r10,%rdx,8), %rsi
leal 0x1(%rcx), %r13d
imull %r13d, %edi
leal (%rdi,%r14), %edx
movslq %edx, %rdx
movq (%r10,%rdx,8), %rbx
addl %r15d, %eax
cltq
movq (%r10,%rax,8), %rdx
addl %r15d, %edi
movq 0x150(%rsp), %r11
movl 0x160(%rsp), %r8d
imull %r8d, %ecx
leal (%rcx,%r15), %eax
cltq
movsd (%r11,%rax,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movslq %edi, %rdi
imull %r13d, %r8d
leal (%r8,%r14), %r9d
movslq %r9d, %r9
movsd (%r11,%r9,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r9
movq (%r10,%rdi,8), %rbp
addl %r15d, %r8d
movslq %r8d, %rdi
movsd (%r11,%rdi,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r12
testq %rsi, %rsi
je 0x2154e
addl %r14d, %ecx
movslq %ecx, %rcx
movsd (%r11,%rcx,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdi
testq %rdx, %rdx
je 0x2156e
testq %rbx, %rbx
je 0x215b8
movq %rbx, %rcx
testq %rbp, %rbp
je 0x215cb
orl %edi, %r12d
orl %r12d, %eax
notl %eax
testb $0x6, %al
je 0x2157e
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %rbp, %rcx
xorl %r8d, %r8d
movq %rsi, (%rsp)
movq %r9, 0x20(%rsp)
callq *0x48(%rax)
movq 0x20(%rsp), %r9
movq %rbx, %rcx
movq (%rsp), %rsi
movsd 0x9bad4(%rip), %xmm1    # 0xbd020
jmp 0x2157e
testq %rdx, %rdx
je 0x215a3
testq %rbp, %rbp
je 0x215a3
movq %rbx, %rcx
testq %rbx, %rbx
je 0x215a3
orl %eax, %r12d
orl %r9d, %r12d
notl %r12d
movq %rdx, %rsi
jmp 0x21584
testq %rbp, %rbp
je 0x215a3
movq %rbx, %rcx
testq %rbx, %rbx
je 0x215a3
orl %edi, %r12d
orl %r9d, %r12d
notl %r12d
movq %rbp, %rdx
testb $0x6, %r12b
je 0x215a3
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movsd 0x9ba7d(%rip), %xmm1    # 0xbd020
movq 0x38(%rsp), %rbx
movl 0x2c(%rbx), %eax
decl %eax
cmpl %eax, %r13d
jl 0x21444
jmp 0x215d8
testq %rbp, %rbp
je 0x215a3
orl %edi, %r12d
orl %eax, %r12d
notl %r12d
movq %rbp, %rcx
jmp 0x21587
orl %edi, %eax
orl %r9d, %eax
notl %eax
testb $0x6, %al
jne 0x2158d
jmp 0x215a3
incl %r14d
movl 0x28(%rbx), %eax
decl %eax
cmpl %eax, %r14d
jl 0x21433
movq 0x10(%rsp), %rdi
callq 0x117b0
movq (%rbx), %rax
movl $0x1, %ebp
movq %rbx, %rdi
callq *0x148(%rax)
jmp 0x21608
xorl %ebp, %ebp
leaq 0x98(%rsp), %rdi
callq 0x60ff0
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x21635
jmp 0x21635
jmp 0x21635
jmp 0x21635
jmp 0x21635
jmp 0x21635
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x60ff0
movq %rbx, %rdi
callq 0x11a70
nopl (%rax)