// File: /workspace/github_cpp_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movsd %xmm1, (%rsp)
movaps %xmm0, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
movq (%rdi), %rax
callq *0xf8(%rax)
movl 0x28(%rbx), %ebp
movq %rbx, 0x10(%rsp)
movl 0x2c(%rbx), %r12d
movl %r12d, %ebx
imull %ebp, %ebx
movslq %ebx, %r14
leaq (,%r14,8), %rax
testl %r14d, %r14d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x11040
movq %rax, %r15
testl %r14d, %r14d
jle 0x20f66
movl %ebx, %edx
shlq $0x3, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x11310
leaq 0x88(%rsp), %r13
movq %r13, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x5f630
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movsd (%rsp), %xmm0
callq 0x21821
testl %eax, %eax
je 0x2134f
leaq 0x88(%rsp), %rdi
movsd 0x9a070(%rip), %xmm0    # 0xbb018
xorpd %xmm1, %xmm1
callq 0x63f00
movq %r15, (%rsp)
movl 0x28(%rbx), %eax
movl 0x2c(%rbx), %ecx
testl %ecx, %ecx
jle 0x21171
xorpd %xmm0, %xmm0
cmpltpd 0x20(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
leaq 0x70(%rsp), %r12
xorl %ebp, %ebp
testl %eax, %eax
jle 0x21167
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x18(%rsp)
movslq %ebp, %r15
xorl %r13d, %r13d
xorpd %xmm0, %xmm0
cmpl $0x4, 0x24(%rbx)
jge 0x2110b
leaq 0xe3ccd(%rip), %rax     # 0x104cd8
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x21155
movapd 0x20(%rsp), %xmm1
cmpltpd %xmm0, %xmm1
andpd 0x30(%rsp), %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x21155
movq 0x140(%rsp), %rax
movslq 0x150(%rsp), %rcx
imulq %r15, %rcx
addq %r13, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x99fc6(%rip), %xmm0    # 0xbb020
cvttsd2si %xmm0, %rax
testb $0x1, %al
jne 0x21155
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movq %r12, %rdi
movsd %xmm0, 0x40(%rsp)
movsd 0x18(%rsp), %xmm1
callq 0x6f0d6
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
leaq 0x48(%rsp), %rdi
callq 0x4b602
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x16377
movq %r12, %rdi
movq %rbx, %rsi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x1d72e
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movsd 0x40(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
callq 0x6f0d6
movl 0x28(%rbx), %ebx
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
leaq 0x48(%rsp), %rsi
leaq 0x9be59(%rip), %rdx     # 0xbcf40
movq %r12, %rcx
movq %r14, %r8
xorl %r9d, %r9d
callq *0x38(%rax)
imull %ebp, %ebx
addl %r13d, %ebx
movslq %ebx, %rcx
movq (%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
movq 0x10(%rsp), %rbx
jmp 0x21155
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x21147
movq 0xb8(%rax), %rcx
movslq 0xc8(%rax), %rax
imulq %r15, %rax
addq %r13, %rax
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x99ede(%rip), %xmm0    # 0xbb018
jne 0x21004
jnp 0x21155
jmp 0x21004
leaq 0x9adc0(%rip), %rdi     # 0xbbf0e
xorl %eax, %eax
callq 0x77f42
movl 0x28(%rbx), %eax
incq %r13
cmpl %eax, %r13d
jl 0x20ff6
movl 0x2c(%rbx), %ecx
incl %ebp
cmpl %ecx, %ebp
jl 0x20fdb
cmpl $0x2, %eax
jl 0x21333
xorl %r15d, %r15d
movsd 0x99e9b(%rip), %xmm1    # 0xbb020
leal 0x1(%r15), %r14d
cmpl $0x2, %ecx
jl 0x21324
xorl %r13d, %r13d
movl %r13d, %ecx
movl 0x28(%rbx), %edi
movl %edi, %eax
imull %r13d, %eax
leal (%rax,%r15), %edx
movslq %edx, %rdx
movq (%rsp), %r10
movq (%r10,%rdx,8), %rsi
leal 0x1(%rcx), %r13d
imull %r13d, %edi
leal (%rdi,%r15), %edx
movslq %edx, %rdx
movq (%r10,%rdx,8), %r12
addl %r14d, %eax
cltq
movq (%r10,%rax,8), %rdx
addl %r14d, %edi
movq 0x140(%rsp), %r11
movl 0x150(%rsp), %r8d
imull %r8d, %ecx
leal (%rcx,%r14), %eax
cltq
movsd (%r11,%rax,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movslq %edi, %rdi
imull %r13d, %r8d
leal (%r8,%r15), %r9d
movslq %r9d, %r9
movsd (%r11,%r9,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r9
movq (%r10,%rdi,8), %rbp
addl %r14d, %r8d
movslq %r8d, %rdi
movsd (%r11,%rdi,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
testq %rsi, %rsi
je 0x2129e
addl %r15d, %ecx
movslq %ecx, %rcx
movsd (%r11,%rcx,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdi
testq %rdx, %rdx
je 0x212bc
testq %r12, %r12
je 0x21304
movq %r12, %rcx
testq %rbp, %rbp
je 0x21314
orl %edi, %ebx
orl %ebx, %eax
notl %eax
testb $0x6, %al
je 0x212cb
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %rbp, %rcx
xorl %r8d, %r8d
movq %rsi, 0x20(%rsp)
movq %r9, 0x30(%rsp)
callq *0x48(%rax)
movq 0x30(%rsp), %r9
movq %r12, %rcx
movq 0x20(%rsp), %rsi
movsd 0x99d84(%rip), %xmm1    # 0xbb020
jmp 0x212cb
testq %rdx, %rdx
je 0x212ee
testq %rbp, %rbp
je 0x212ee
movq %r12, %rcx
testq %r12, %r12
je 0x212ee
orl %eax, %ebx
orl %r9d, %ebx
notl %ebx
movq %rdx, %rsi
jmp 0x212d0
testq %rbp, %rbp
je 0x212ee
movq %r12, %rcx
testq %r12, %r12
je 0x212ee
orl %edi, %ebx
orl %r9d, %ebx
notl %ebx
movq %rbp, %rdx
testb $0x6, %bl
je 0x212ee
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
xorl %r8d, %r8d
callq *0x48(%rax)
movsd 0x99d32(%rip), %xmm1    # 0xbb020
movq 0x10(%rsp), %rbx
movl 0x2c(%rbx), %ecx
leal -0x1(%rcx), %eax
cmpl %eax, %r13d
jl 0x21195
jmp 0x21321
testq %rbp, %rbp
je 0x212ee
orl %edi, %ebx
orl %eax, %ebx
notl %ebx
movq %rbp, %rcx
jmp 0x212d3
orl %edi, %eax
orl %r9d, %eax
notl %eax
testb $0x6, %al
jne 0x212d8
jmp 0x212ee
movl 0x28(%rbx), %eax
leal -0x1(%rax), %edx
movl %r14d, %r15d
cmpl %edx, %r14d
jl 0x21185
movq (%rsp), %rdi
callq 0x117a0
movq (%rbx), %rax
movl $0x1, %ebp
movq %rbx, %rdi
callq *0x148(%rax)
jmp 0x21351
xorl %ebp, %ebp
leaq 0x88(%rsp), %rdi
callq 0x60264
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2137e
jmp 0x2137e
jmp 0x2137e
jmp 0x2137e
jmp 0x2137e
jmp 0x2137e
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x60264
movq %rbx, %rdi
callq 0x11a60
nop