// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/apps/confview/confview.cpp
// Function: Pick(int, int, gaps::RGBDSurface**, gaps::RGBDImage**, gaps::R3Point*, int)
static int
Pick(int x, int y,
  RGBDSurface **picked_surface = NULL, RGBDImage **picked_image = NULL,
  R3Point *picked_position = NULL, int pick_tolerance = 10)
{
  // Initialize pick results
  int pick_result = 0;
  if (picked_surface) *picked_surface = NULL;
  if (picked_image) *picked_image = NULL;
  if (picked_position) *picked_position = R3unknown_point;

  // Draw everything
  viewer.Load();
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPointSize(pick_tolerance);
  glLineWidth(pick_tolerance);
  if (picked_image || !picked_surface) {
    if (show_cameras) DrawCameras(RGBD_INDEX_COLOR_SCHEME);
    if (show_bboxes) DrawBBoxes(RGBD_INDEX_COLOR_SCHEME);
    if (show_points) DrawPoints(RGBD_INDEX_COLOR_SCHEME);
    if (show_surfels) DrawSurfels(RGBD_INDEX_COLOR_SCHEME);
    if (show_quads) DrawQuads(RGBD_INDEX_COLOR_SCHEME);
  }
  if (picked_surface || !picked_image) {
    if (show_faces || show_textures) DrawFaces(RGBD_INDEX_COLOR_SCHEME);
  }
  glPointSize(1.0);
  glLineWidth(1.0);
  glFinish();

  // Read color buffer at cursor position
  unsigned char rgba[4];
  glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
  int r = rgba[0] & 0xFF;
  int g = rgba[1] & 0xFF;
  int b = rgba[2] & 0xFF;
  // int a = rgba[3] & 0xFF;

  // Figure out what was picked
  if (r > 0) {
    // Picked surface
    int surface_index = (int) ((configuration.NSurfaces()*r/255.0) + 0.5) - 1;
    if ((surface_index < 0) || (surface_index >= configuration.NSurfaces())) return 0;
    if (picked_surface) *picked_surface = configuration.Surface(surface_index);
    pick_result = RGBD_SURFACE_SELECTION;
  }
  else if ((g > 0) || (b > 0)) {
    // Picked image
    int image_index = (int) ((configuration.NImages()*((g << 8) | b) / 65535.0) + 0.5) - 1;
    if ((image_index < 0) || (image_index >= configuration.NImages())) return 0;
    if (picked_image) *picked_image = configuration.Image(image_index);
    pick_result = RGBD_IMAGE_SELECTION;
  }
  else {
    // Picked background
    return RGBD_NO_SELECTION;
  }

  // Return position
  if (picked_position) {
    // Find hit position
    GLfloat depth;
    GLdouble p[3];
    GLint viewport[4];
    GLdouble modelview_matrix[16];
    GLdouble projection_matrix[16];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);
    glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
    gluUnProject(x, y, depth, modelview_matrix, projection_matrix, viewport, &(p[0]), &(p[1]), &(p[2]));
    R3Point position(p[0], p[1], p[2]);
    *picked_position = position;
  }

  // Return success
  return pick_result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, %r12
movl %esi, 0x14(%rsp)
movl %edi, %r14d
testq %rdx, %rdx
je 0x28dbc
movq $0x0, (%r12)
testq %r15, %r15
je 0x28dc8
movq $0x0, (%r15)
testq %rbx, %rbx
je 0x28ddc
leaq 0x14821c(%rip), %rsi    # 0x170ff0
movq %rbx, %rdi
callq 0x8905c
testq %r12, %r12
setne %bpl
movl 0x144c5f(%rip), %edi    # 0x16da48
movl 0x144c5d(%rip), %esi    # 0x16da4c
movl 0x144c5b(%rip), %edx    # 0x16da50
addl %edi, %edx
movl 0x144c57(%rip), %ecx    # 0x16da54
addl %esi, %ecx
callq 0x23200
leaq 0x144bad(%rip), %rdi    # 0x16d9b8
xorl %esi, %esi
callq 0x3b5a0
movss 0xd31ea(%rip), %xmm3    # 0xfc004
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x23e60
movl $0x4100, %edi           # imm = 0x4100
callq 0x231e0
movss 0xd31d2(%rip), %xmm0    # 0xfc00c
callq 0x230a0
movss 0xd31c5(%rip), %xmm0    # 0xfc00c
callq 0x24180
testq %r15, %r15
sete %al
testb %al, %bpl
jne 0x28ee4
cmpl $0x0, 0x13fb6e(%rip)    # 0x1689d0
je 0x28e6e
movl $0x3, %edi
callq 0x27fcc
cmpl $0x0, 0x144bef(%rip)    # 0x16da64
je 0x28eab
cmpl $0x0, 0x144a46(%rip)    # 0x16d8c4
jle 0x28eab
xorl %r13d, %r13d
movq 0x144a2e(%rip), %rax    # 0x16d8b8
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
movl $0x3, %esi
callq *0xc8(%rax)
incq %r13
movslq 0x144a1e(%rip), %rax    # 0x16d8c4
cmpq %rax, %r13
jl 0x28e83
cmpl $0x0, 0x144bca(%rip)    # 0x16da7c
je 0x28ebe
movl $0x3, %edi
callq 0x280e2
cmpl $0x0, 0x144bbb(%rip)    # 0x16da80
je 0x28ed1
movl $0x3, %edi
callq 0x2814b
cmpl $0x0, 0x144bac(%rip)    # 0x16da84
je 0x28ee4
movl $0x3, %edi
callq 0x281b4
testq %r15, %r15
setne %al
testq %r12, %r12
sete %cl
testb %al, %cl
jne 0x28f33
movl 0x144b8e(%rip), %eax    # 0x16da88
orl 0x144b70(%rip), %eax    # 0x16da70
je 0x28f33
cmpl $0x0, 0x1449cb(%rip)    # 0x16d8d4
jle 0x28f33
xorl %r13d, %r13d
movq 0x1449b3(%rip), %rax    # 0x16d8c8
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
movl $0x3, %esi
callq *0x60(%rax)
incq %r13
movslq 0x1449a6(%rip), %rax    # 0x16d8d4
cmpq %rax, %r13
jl 0x28f0e
movss 0xd30c9(%rip), %xmm0    # 0xfc004
callq 0x230a0
movss 0xd30bc(%rip), %xmm0    # 0xfc004
callq 0x24180
callq 0x24360
leaq 0x18(%rsp), %r13
movq %r13, (%rsp)
movl %r14d, %edi
movl 0x14(%rsp), %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x233b0
movzbl (%r13), %eax
testl %eax, %eax
je 0x28feb
movl 0x144948(%rip), %edx    # 0x16d8d4
imull %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0xd30c2(%rip), %xmm0    # 0xfc060
addsd 0xd3072(%rip), %xmm0    # 0xfc018
cvttsd2si %xmm0, %esi
xorl %eax, %eax
testl %esi, %esi
jle 0x2905d
xorl %ecx, %ecx
cmpl %esi, %edx
setge %cl
movl %ecx, %ebp
addl %ecx, %ebp
testq %r12, %r12
je 0x29061
cmpl %esi, %edx
jl 0x29061
movq 0x1448f1(%rip), %rcx    # 0x16d8c8
movl %esi, %edx
movq -0x8(%rcx,%rdx,8), %rcx
movq %rcx, (%r12)
movb $0x1, %cl
movl $0x2, %ebp
jmp 0x29061
movzbl 0x19(%rsp), %edx
movzbl 0x1a(%rsp), %esi
xorl %eax, %eax
movl %esi, %ecx
orb %dl, %cl
je 0x29143
movl 0x1448bd(%rip), %ecx    # 0x16d8c4
shll $0x8, %edx
orl %esi, %edx
imull %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
divsd 0xd304a(%rip), %xmm0    # 0xfc068
addsd 0xd2ff2(%rip), %xmm0    # 0xfc018
cvttsd2si %xmm0, %esi
xorl %eax, %eax
xorl %ebp, %ebp
testl %esi, %esi
jle 0x2906a
cmpl %esi, %ecx
setge %dl
movb %dl, %bpl
testq %r15, %r15
je 0x2906c
cmpl %esi, %ecx
jl 0x2906c
movq 0x14486e(%rip), %rcx    # 0x16d8b8
movl %esi, %edx
movq -0x8(%rcx,%rdx,8), %rcx
movq %rcx, (%r15)
movb $0x1, %dl
movl $0x1, %ebp
jmp 0x2906c
xorl %ebp, %ebp
xorl %ecx, %ecx
testb %cl, %cl
jne 0x29074
jmp 0x29143
xorl %edx, %edx
testb %dl, %dl
je 0x29143
testq %rbx, %rbx
je 0x29141
leaq 0x20(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23890
leaq 0x60(%rsp), %rsi
movl $0xba6, %edi            # imm = 0xBA6
callq 0x23a70
leaq 0xe0(%rsp), %r13
movl $0xba7, %edi            # imm = 0xBA7
movq %r13, %rsi
callq 0x23a70
leaq 0x1c(%rsp), %r15
movq %r15, (%rsp)
movl %r14d, %edi
movl %r14d, %r12d
movl 0x14(%rsp), %r14d
movl %r14d, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x233b0
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
cvtsi2sd %r14d, %xmm1
cvtss2sd (%r15), %xmm2
leaq 0x38(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x30(%rsp), %r14
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x24260
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x88cee
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8905c
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq