// File: /workspace/github_cpp_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
movq %r8, %rbx
movq %rcx, %r12
movq %rdx, %r15
movl %esi, %ebp
movl %edi, %r14d
testq %rdx, %rdx
je 0x28d05
movq $0x0, (%r15)
testq %r12, %r12
je 0x28d12
movq $0x0, (%r12)
testq %rbx, %rbx
je 0x28d26
leaq 0x1462d2(%rip), %rsi    # 0x16eff0
movq %rbx, %rdi
callq 0x87c7e
testq %r15, %r15
setne %r13b
movl 0x142d15(%rip), %edi    # 0x16ba48
movl 0x142d13(%rip), %esi    # 0x16ba4c
movl 0x142d11(%rip), %edx    # 0x16ba50
addl %edi, %edx
movl 0x142d0d(%rip), %ecx    # 0x16ba54
addl %esi, %ecx
callq 0x23200
leaq 0x142c63(%rip), %rdi    # 0x16b9b8
xorl %esi, %esi
callq 0x3b0e0
movss 0xd12a0(%rip), %xmm3    # 0xfa004
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x23e50
movl $0x4100, %edi           # imm = 0x4100
callq 0x231e0
movss 0xd1288(%rip), %xmm0    # 0xfa00c
callq 0x230a0
movss 0xd127b(%rip), %xmm0    # 0xfa00c
callq 0x24170
testq %r12, %r12
sete %al
testb %al, %r13b
jne 0x28e3e
cmpl $0x0, 0x13dc24(%rip)    # 0x1669d0
je 0x28db8
movl $0x3, %edi
callq 0x27f9b
cmpl $0x0, 0x142ca5(%rip)    # 0x16ba64
je 0x28df5
cmpl $0x0, 0x142afc(%rip)    # 0x16b8c4
jle 0x28df5
xorl %r13d, %r13d
movq 0x142ae4(%rip), %rax    # 0x16b8b8
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
movl $0x3, %esi
callq *0xc8(%rax)
incq %r13
movslq 0x142ad4(%rip), %rax    # 0x16b8c4
cmpq %rax, %r13
jl 0x28dcd
cmpl $0x0, 0x142c80(%rip)    # 0x16ba7c
je 0x28e08
movl $0x3, %edi
callq 0x280b1
cmpl $0x0, 0x142c71(%rip)    # 0x16ba80
je 0x28e1b
movl $0x3, %edi
callq 0x2811a
cmpl $0x0, 0x142c62(%rip)    # 0x16ba84
je 0x28e2e
movl $0x3, %edi
callq 0x28183
testq %r12, %r12
setne %al
testq %r15, %r15
sete %cl
testb %al, %cl
jne 0x28e7d
movl 0x142c44(%rip), %eax    # 0x16ba88
orl 0x142c26(%rip), %eax    # 0x16ba70
je 0x28e7d
cmpl $0x0, 0x142a81(%rip)    # 0x16b8d4
jle 0x28e7d
xorl %r13d, %r13d
movq 0x142a69(%rip), %rax    # 0x16b8c8
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
movl $0x3, %esi
callq *0x60(%rax)
incq %r13
movslq 0x142a5c(%rip), %rax    # 0x16b8d4
cmpq %rax, %r13
jl 0x28e58
movss 0xd117f(%rip), %xmm0    # 0xfa004
callq 0x230a0
movss 0xd1172(%rip), %xmm0    # 0xfa004
callq 0x24170
callq 0x24350
leaq 0x8(%rsp), %r13
movq %r13, (%rsp)
movl %r14d, %edi
movl %ebp, %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 0x28f18
movl 0x142a00(%rip), %edx    # 0x16b8d4
imull %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0xd117a(%rip), %xmm0    # 0xfa060
addsd 0xd112a(%rip), %xmm0    # 0xfa018
cvttsd2si %xmm0, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jle 0x29064
testq %r15, %r15
je 0x28f87
cmpl %ecx, %edx
jl 0x28f87
movl $0x2, %r13d
leaq 0x1429b2(%rip), %rax    # 0x16b8c8
jmp 0x28f78
movzbl 0x9(%rsp), %ecx
movzbl 0xa(%rsp), %esi
xorl %eax, %eax
movl %esi, %edx
orb %cl, %dl
je 0x29064
movl 0x142990(%rip), %edx    # 0x16b8c4
shll $0x8, %ecx
orl %esi, %ecx
imull %edx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd 0xd111d(%rip), %xmm0    # 0xfa068
addsd 0xd10c5(%rip), %xmm0    # 0xfa018
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
jle 0x29064
testq %r12, %r12
je 0x28f8f
cmpl %ecx, %edx
jl 0x28f8f
movl $0x1, %r13d
leaq 0x142943(%rip), %rax    # 0x16b8b8
movq %r12, %r15
movq (%rax), %rax
movl %ecx, %ecx
movq -0x8(%rax,%rcx,8), %rax
movq %rax, (%r15)
jmp 0x28f9d
movl $0x2, %r13d
jmp 0x28f95
movl $0x1, %r13d
cmpl %ecx, %edx
jl 0x29064
testq %rbx, %rbx
je 0x29061
leaq 0x10(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23890
leaq 0x50(%rsp), %rsi
movl $0xba6, %edi            # imm = 0xBA6
callq 0x23a60
leaq 0xd0(%rsp), %r15
movl $0xba7, %edi            # imm = 0xBA7
movq %r15, %rsi
callq 0x23a60
leaq 0xc(%rsp), %r12
movq %r12, (%rsp)
movl %r14d, %edi
movl %ebp, %esi
movl $0x1, %edx
movl $0x1, %ecx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x233b0
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
cvtsi2sd %ebp, %xmm1
cvtss2sd (%r12), %xmm2
leaq 0x28(%rsp), %r8
leaq 0x30(%rsp), %r9
leaq 0x20(%rsp), %r14
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
movq %r14, %rcx
callq 0x24250
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x87916
movq %rbx, %rdi
movq %r14, %rsi
callq 0x87c7e
movl %r13d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq