// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::PickNode(int, int, gaps::R3Point*, gaps::R3SurfelBlock**, int*, int)
R3SurfelNode *R3SurfelViewer::
PickNode(int x, int y, R3Point *picked_position, 
  R3SurfelBlock **picked_block, int *picked_surfel_index,
  RNBoolean exclude_nonobjects) 
{
  // How close the cursor has to be to a point (in pixels)
  int pick_tolerance = 0.005 * Viewport().Width() + 1;

  // Clear window 
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Set viewing transformation
  viewer.Camera().Load();

  // Set viewing extent
  EnableViewingExtent();

  // Set OpenGL stuff
  glPointSize(pick_tolerance);    

  // Draw surfels
  DrawSurfels(R3_SURFEL_VIEWER_COLOR_BY_PICK_INDEX);

  // Reset OpenGL stuff
  glPointSize(1);
  glFinish();

  // Reset viewing modes
  DisableViewingExtent();

  // Read color buffer at cursor position
  unsigned char rgba[4];
  glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
  if (rgba[3] == 0) return NULL;

  // Determine node index
  int r = rgba[0] & 0xFF;
  int g = rgba[1] & 0xFF;
  int b = rgba[2] & 0xFF;
  // int a = rgba[3] & 0xFF;
  // if (a != 0xFE) return NULL;
  int node_index = (r << 16) | (g << 8) | b;
  if (node_index == 0) return NULL;
  node_index--;

  // Determine node
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return NULL;
  if (node_index < 0) return NULL;
  if (node_index >= tree->NNodes()) return NULL;
  R3SurfelNode *hit_node = tree->Node(node_index);

  // Find node part of an object
  R3SurfelNode *picked_node = hit_node;
  if (exclude_nonobjects) {
    // Find node associated with object
    picked_node = NULL;

    // Check if hit node is part of an object
    if (hit_node->Object()) {
      picked_node = hit_node;
    }

    // Check if hit node has ancestor that is part of an object
    if (picked_node == NULL) {
      R3SurfelNode *ancestor = hit_node->Parent();
      while (ancestor) {
        if (ancestor->Object()) { picked_node = ancestor; break; }
        ancestor = ancestor->Parent();
      }
    }
    
    // Check if hit node has descendent that is part of an object
    if (picked_node == NULL) {
      R3Ray ray = viewer.WorldRay(x, y);
      RNScalar t, picked_t = FLT_MAX;
      RNArray<R3SurfelNode *> stack;
      stack.Insert(hit_node);
      while (!stack.IsEmpty()) {
        R3SurfelNode *node = stack.Tail();
        stack.RemoveTail();
        for (int i = 0; i < node->NParts(); i++) {
          stack.Insert(node->Part(i));
        }
        if (node->Object()) {
          if (R3Intersects(ray, node->BBox(), NULL, NULL, &t)) {
            if (t < picked_t) {
              picked_node = node;
              picked_t = t;
            }
          }
        }
      }
    }
  }
    
  // 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]);
  if (picked_position) *picked_position = position;

  // Find hit surfel
  if (picked_block || picked_surfel_index) {
    // Create pointset in vicinity of picked position
    R3Point position(p[0], p[1], p[2]);
    RNScalar radius = 0.1 * R3Distance(position, viewer.Camera().Origin());
    R3SurfelSphereConstraint sphere_constraint(R3Sphere(position, radius));
    R3SurfelPointSet *pointset = CreatePointSet(scene, hit_node, &sphere_constraint);
    if (pointset) {
      // Find surfel point closest to picked position
      R3SurfelPoint *closest_point = NULL;
      RNLength closest_distance = FLT_MAX;
      for (int i = 0; i < pointset->NPoints(); i++) {
        R3SurfelPoint *point = pointset->Point(i);
        RNLength distance = R3SquaredDistance(point->Position(), position);
        if (distance < closest_distance) {
          closest_distance = distance;
          closest_point = point;
        }
      }

      // Return closest point
      if (closest_point) {
        if (picked_position) *picked_position = closest_point->Position();
        if (picked_block) *picked_block = closest_point->Block();
        if (picked_surfel_index) *picked_surfel_index = closest_point->BlockIndex();
      }

      // Delete point set
      delete pointset;
    }
  }

  // Return picked node
  return picked_node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %r9, %r12
movq %r8, %rbp
movq %rcx, 0x48(%rsp)
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r13
cvtsi2sdl 0x118(%rdi), %xmm0
mulsd 0xe8dd4(%rip), %xmm0    # 0x11a440
addsd 0xe8994(%rip), %xmm0    # 0x11a008
movapd %xmm0, 0x20(%rsp)
movl $0x4100, %edi           # imm = 0x4100
callq 0x27210
leaq 0x80(%r13), %rdi
movq %rdi, 0x78(%rsp)
xorl %esi, %esi
callq 0x70a46
movq (%r13), %rax
movq %r13, %rdi
callq *0x230(%rax)
cvttpd2dq 0x20(%rsp), %xmm0
cvtdq2ps %xmm0, %xmm0
callq 0x270a0
movq (%r13), %rax
pushq $0xe
popq %rsi
movq %r13, %rdi
callq *0x270(%rax)
movss 0xe8976(%rip), %xmm0    # 0x11a040
callq 0x270a0
callq 0x283b0
movq (%r13), %rax
movq %r13, %rdi
callq *0x238(%rax)
leaq 0xc(%rsp), %r15
movq %r15, (%rsp)
pushq $0x1
popq %rcx
movl %r14d, %edi
movl %ebx, %esi
movl %ecx, %edx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x273e0
cmpb $0x0, 0x3(%r15)
je 0x31739
movzbl 0xc(%rsp), %ecx
movzbl 0xd(%rsp), %eax
movzbl 0xe(%rsp), %edx
shll $0x10, %ecx
shll $0x8, %eax
orl %ecx, %eax
orl %edx, %eax
je 0x31739
movq 0x8(%r13), %rcx
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
je 0x31739
cmpl 0x24(%rcx), %eax
jle 0x31750
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, 0x38(%rsp)
movq %r12, 0x40(%rsp)
movq 0x18(%rcx), %rcx
movl %eax, %eax
cmpl $0x0, 0x260(%rsp)
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x20(%rsp)
movl %ebx, %edx
movq -0x8(%rcx,%rax,8), %rbx
movl %edx, 0x1c(%rsp)
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x30(%rsp)
movq %rbx, %rbp
movq %rbx, 0x70(%rsp)
je 0x318dd
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3d228
movq %rbx, %rbp
testq %rax, %rax
jne 0x318dd
leaq 0x78(%r13), %r15
movq %rbx, %rbp
movq 0x28(%rbp), %rbp
testq %rbp, %rbp
je 0x317d4
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3d228
testq %rax, %rax
je 0x317b5
jmp 0x318dd
leaq 0x1a0(%rsp), %rdi
movq %r15, %rsi
movsd 0x20(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x6f5a8
leaq 0x120(%rsp), %r15
movq %r15, %rdi
callq 0xd0d9a
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3344c
movsd 0xe8b25(%rip), %xmm0    # 0x11a338
xorl %r12d, %r12d
leaq 0x120(%rsp), %r15
movq %r12, %rbp
movsd %xmm0, 0x10(%rsp)
movslq 0x12c(%rsp), %rax
testq %rax, %rax
je 0x318d0
movq 0x120(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %r15, %rdi
callq 0x32ce0
movq %rbp, %rbx
xorl %ebp, %ebp
movslq 0x3c(%r12), %rax
cmpq %rax, %rbp
jge 0x31872
movq 0x30(%r12), %rax
movq (%rax,%rbp,8), %rsi
movq %r15, %rdi
callq 0x3344c
incq %rbp
jmp 0x31852
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3d228
movq %rbx, %rbp
testq %rax, %rax
je 0x31827
movq %r12, %rdi
callq 0x3d094
leaq 0x1a0(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0xc0(%rsp), %r8
callq 0x7a8a1
testl %eax, %eax
je 0x31827
movsd 0xc0(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x31827
jmp 0x3181e
leaq 0x120(%rsp), %rdi
callq 0xd0e08
movq %rbp, 0x10(%rsp)
leaq 0xb0(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x278c0
leaq 0x1a0(%rsp), %r12
movl $0xba6, %edi            # imm = 0xBA6
movq %r12, %rsi
callq 0x27aa0
leaq 0x120(%rsp), %r15
movl $0xba7, %edi            # imm = 0xBA7
movq %r15, %rsi
callq 0x27aa0
leaq 0x84(%rsp), %rbp
movq %rbp, (%rsp)
pushq $0x1
popq %rcx
movl %r14d, %edi
movl 0x1c(%rsp), %esi
movl %ecx, %edx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x273e0
cvtss2sd (%rbp), %xmm2
leaq 0x58(%rsp), %r8
leaq 0x60(%rsp), %r9
leaq 0x50(%rsp), %rbx
movsd 0x20(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movq %r12, %rdi
movq %r15, %rsi
leaq 0xb0(%rsp), %rdx
movq %rbx, %rcx
callq 0x282a0
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
leaq 0x108(%rsp), %rdi
callq 0xb2320
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x319af
leaq 0x108(%rsp), %rsi
callq 0xb260a
movq 0x38(%rsp), %rax
orq 0x40(%rsp), %rax
movq 0x10(%rsp), %rbp
je 0x3173b
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
callq 0xb2320
movq %rbx, %rdi
movq 0x78(%rsp), %rsi
callq 0x7fb9a
mulsd 0xe8955(%rip), %xmm0    # 0x11a350
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9bb80
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x37bb8
leaq 0x88(%rsp), %rdi
callq 0x9eb12
movq 0x8(%r13), %rdi
leaq 0xc0(%rsp), %rdx
movq 0x70(%rsp), %rsi
callq 0x4d647
movq %rax, %rbx
testq %rax, %rax
je 0x31b1e
movsd 0xe88e3(%rip), %xmm0    # 0x11a338
xorl %r12d, %r12d
leaq 0x88(%rsp), %r15
xorl %ebp, %ebp
xorl %r14d, %r14d
movslq 0x10(%rbx), %rax
cmpq %rax, %rbp
jge 0x31ab1
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rbx), %r13
addq %r12, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x32b2a
movq %r15, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x7fb71
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmovaq %r13, %r14
minsd %xmm1, %xmm0
incq %rbp
addq $0x10, %r12
jmp 0x31a65
testq %r14, %r14
movq 0x40(%rsp), %r15
movq 0x10(%rsp), %rbp
je 0x31b15
movq 0x48(%rsp), %r13
testq %r13, %r13
movq 0x38(%rsp), %r12
je 0x31aef
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x32b2a
leaq 0x88(%rsp), %rsi
movq %r13, %rdi
callq 0xb260a
testq %r12, %r12
je 0x31afb
movq (%r14), %rax
movq %rax, (%r12)
testq %r15, %r15
je 0x31b15
movq (%r14), %rcx
movq 0x8(%r14), %rax
subq (%rcx), %rax
pushq $0x30
popq %rcx
cqto
idivq %rcx
movl %eax, (%r15)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
leaq 0xc0(%rsp), %rdi
callq 0x32cea
jmp 0x3173b
jmp 0x31b5c
jmp 0x31b4a
jmp 0x31b4a
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x9eb12
jmp 0x31b6c
jmp 0x31b5c
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x32cea
jmp 0x31b6c
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0xd0e08
movq %rbx, %rdi
callq 0x28140