// File: /workspace/github_cpp_makefile_star2_O2/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, 0x40(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movq (%rdi), %rax
callq *0xf8(%rax)
movl 0x28(%rbx), %ebp
movq %rbx, 0x8(%rsp)
movl 0x2c(%rbx), %r12d
movl %r12d, %eax
imull %ebp, %eax
movslq %eax, %rbx
leaq (,%rbx,8), %rax
xorl %r14d, %r14d
testl %ebx, %ebx
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
cmovlel %r14d, %ebx
callq 0x11040
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpq %r14, %rbx
je 0x1f66e
andq $0x0, (%rax,%r14,8)
incq %r14
jmp 0x1f65f
leaq 0x98(%rsp), %r13
movq %r13, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x57e7c
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movsd 0x40(%rsp), %xmm0
callq 0x1ff98
testl %eax, %eax
je 0x1f9f5
leaq 0x98(%rsp), %rdi
movsd 0x88962(%rip), %xmm0    # 0xa8010
xorpd %xmm1, %xmm1
callq 0x5c1ba
xorl %r12d, %r12d
leaq 0xd8637(%rip), %rbx     # 0xf7cf8
xorpd %xmm0, %xmm0
cmpltpd 0x20(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
leaq 0x88(%rsp), %r13
movq 0x8(%rsp), %r10
movl 0x2c(%r10), %eax
cmpl %eax, %r12d
jge 0x1f809
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x10(%rsp)
movslq %r12d, %r14
xorl %ebp, %ebp
cmpl 0x28(%r10), %ebp
jge 0x1f801
movq %r10, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x1fade
ucomisd 0x888f5(%rip), %xmm0    # 0xa8010
jne 0x1f723
jnp 0x1f7f4
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jae 0x1f7f4
movapd 0x20(%rsp), %xmm1
cmpltpd %xmm0, %xmm1
andpd 0x40(%rsp), %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x1f7f4
movq 0x150(%rsp), %rax
movslq 0x160(%rsp), %rcx
imulq %r14, %rcx
addq %rbp, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x888a6(%rip), %xmm0    # 0xa8018
cvttsd2si %xmm0, %rax
testb $0x1, %al
jne 0x1f7f4
leaq 0x70(%rsp), %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x1fb0c
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x1c31e
cvtsi2sd %ebp, %xmm0
movq %r13, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x66640
movq 0x8(%rsp), %rax
movl 0x28(%rax), %r15d
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
leaq 0x70(%rsp), %rsi
leaq 0x8a69d(%rip), %rdx     # 0xa9e70
leaq 0x58(%rsp), %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
imull %r12d, %r15d
addl %ebp, %r15d
movslq %r15d, %rcx
movq 0x18(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
incq %rbp
movq 0x8(%rsp), %r10
jmp 0x1f6fc
incl %r12d
jmp 0x1f6df
xorl %r14d, %r14d
movl 0x28(%r10), %ecx
decl %ecx
cmpl %ecx, %r14d
jge 0x1f9d7
leal 0x1(%r14), %ecx
movq %rcx, 0x40(%rsp)
xorl %ebx, %ebx
movq %r14, 0x10(%rsp)
leal -0x1(%rax), %edx
cmpl %edx, %ebx
jge 0x1f9ca
movl %ebx, %ecx
movl 0x28(%r10), %edi
movl %edi, %eax
imull %ebx, %eax
leal (%rax,%r14), %edx
movslq %edx, %rdx
movq 0x18(%rsp), %r11
movq (%r11,%rdx,8), %rsi
leal 0x1(%rcx), %ebx
imull %ebx, %edi
leal (%rdi,%r14), %edx
movslq %edx, %rdx
movq (%r11,%rdx,8), %r15
movq 0x40(%rsp), %r13
addl %r13d, %eax
cltq
movq (%r11,%rax,8), %rdx
addl %r13d, %edi
movq 0x150(%rsp), %r12
movl 0x160(%rsp), %r8d
imull %r8d, %ecx
leal (%rcx,%r13), %eax
cltq
movsd (%r12,%rax,8), %xmm0
movsd 0x8877d(%rip), %xmm1    # 0xa8018
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movslq %edi, %rdi
imull %ebx, %r8d
leal (%r8,%r14), %r9d
movslq %r9d, %r9
movsd (%r12,%r9,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r9
movq %r9, 0x20(%rsp)
movq (%r11,%rdi,8), %rbp
addl %r13d, %r8d
movslq %r8d, %rdi
movsd (%r12,%rdi,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13
testq %rsi, %rsi
je 0x1f945
addl %r14d, %ecx
movslq %ecx, %rcx
movsd (%r12,%rcx,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdi
testq %rdx, %rdx
je 0x1f967
testq %r15, %r15
je 0x1f9a9
movq %r15, %rcx
testq %rbp, %rbp
je 0x1f9bc
orl %edi, %r13d
orl %r13d, %eax
notl %eax
testb $0x6, %al
je 0x1f977
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
movq %rbp, %rcx
movq %rsi, %r12
xorl %r8d, %r8d
callq *0x48(%rax)
movq 0x10(%rsp), %r14
movq %r15, %rcx
movq %r12, %rsi
movq 0x8(%rsp), %r10
jmp 0x1f977
testq %rdx, %rdx
je 0x1f9a0
testq %rbp, %rbp
je 0x1f9a0
movq %r15, %rcx
testq %r15, %r15
je 0x1f9a0
orl %eax, %r13d
orl 0x20(%rsp), %r13d
notl %r13d
movq %rdx, %rsi
jmp 0x1f97f
testq %rbp, %rbp
je 0x1f9a0
movq %r15, %rcx
testq %r15, %r15
je 0x1f9a0
orl %edi, %r13d
orl 0x20(%rsp), %r13d
notl %r13d
movq %rbp, %rdx
testb $0x6, %r13b
je 0x1f9a0
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r10
movl 0x2c(%r10), %eax
jmp 0x1f82b
testq %rbp, %rbp
je 0x1f9a0
orl %edi, %r13d
orl %eax, %r13d
notl %r13d
movq %rbp, %rcx
jmp 0x1f982
orl %edi, %eax
orl 0x20(%rsp), %eax
notl %eax
testb $0x6, %al
jne 0x1f988
jmp 0x1f9a0
movq 0x40(%rsp), %r9
movl %r9d, %r14d
jmp 0x1f80c
movq 0x18(%rsp), %rdi
movq %r10, %rbx
callq 0x117a0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x148(%rax)
pushq $0x1
popq %rbx
jmp 0x1f9f7
xorl %ebx, %ebx
leaq 0x98(%rsp), %rdi
callq 0x589c0
movl %ebx, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1fa24
jmp 0x1fa24
jmp 0x1fa24
jmp 0x1fa24
jmp 0x1fa24
jmp 0x1fa24
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x589c0
movq %rbx, %rdi
callq 0x11a80