// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::DrawImageInset() const
void R3SurfelViewer::
DrawImageInset(void) const
{
  // Get/check stuff
  if (!selected_image) return;
  if (selected_image->ImageWidth() <= 0) return;
  if (selected_image->ImageHeight() <= 0) return;
  if (!image_inset_visibility) return;
  if (image_inset_size <= 0) return;
  
  // Get/check color image
  ((R3SurfelViewer *) this)->UpdateSelectedImageColorPixels();
  int color_width = selected_image_color_pixels.Width();
  int color_height = selected_image_color_pixels.Height();
  if ((color_width <= 0) || (color_height <= 0)) return;
  const unsigned char *color_pixels = selected_image_color_pixels.Pixels();
  if (!color_pixels) return;

  // Determine image coordinates
  int w = viewer.Viewport().Width();
  int h = viewer.Viewport().Height();
  double x2 = w;
  double y2 = h;
  double aspect = (double) selected_image->ImageHeight() / (double) selected_image->ImageWidth();
  double x1 = x2 - image_inset_size * w;
  double y1 = y2 - image_inset_size * w * aspect;

  // Determine focal point
  R2Point focal_point(0.5*selected_image->ImageWidth(), 0.5*selected_image->ImageHeight());
  if (selected_point) {
    focal_point = selected_image->ImagePosition(selected_point->Position());
    if ((focal_point.X() >= 0) && (focal_point.Y() >= 0) &&
        (focal_point.X() <= selected_image->ImageWidth()-0.5) &&
        (focal_point.Y() <= selected_image->ImageHeight()-0.5)) {
      focal_point[0] = x1 + (x2 - x1) * focal_point.X()/selected_image->ImageWidth();
      focal_point[1] = y1 + (y2 - y1) * focal_point.Y()/selected_image->ImageHeight();
    }
  }

  // Push ortho viewing matrices
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0, w, 0, h, 0.1, 1);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  // Get image translation (so that focal point is in view)
  double tx = 0;
  double ty = 0;
  if (x1 < 0) {
    if (focal_point.X() < 0.25*w) {
      tx = 0.25*w - focal_point.X();
    }
  }
  if (y1 < 0) {
    if (focal_point.Y() < 0.25*h) {
      ty = 0.25*h - focal_point.Y();
    }
  }

#if (RN_OS == RN_MAC)
  // For some unknown reason, drawing a textured quad hangs the GPU for some images
  // (very rarely, but it is repeatable).   So, draw the with glPixelDraw instead.
  
  // Set viewport 
  glViewport(x1, y1, x2 - x1, y2 - y1);

  // Set image scale 
  GLfloat sx = (x2 - x1) / color_width;
  GLfloat sy = (y2 - y1) / color_height;
  glPixelZoom(sx, sy);

  // Set image translation
  // Can't send point outside viewport to glRasterPos, so add translation this way
  glRasterPos3d(0, 0, -0.5);
  glBitmap(0, 0, 0, 0, tx, ty, NULL);
  
  // Draw image
  glDrawPixels(color_width, color_height, GL_RGB, GL_UNSIGNED_BYTE, color_pixels);

  // Reset everything
  glPixelZoom(1, 1);
  glViewport(0, 0, w, h);
#else
  // Update selected image texture
  ((R3SurfelViewer *) this)->UpdateSelectedImageTexture();

  // Draw image as textured quad
  if (selected_image_texture_id > 0) {
    // Bind texture
    glBindTexture(GL_TEXTURE_2D, selected_image_texture_id);
    glEnable(GL_TEXTURE_2D);

    // Draw quad
    glDisable(GL_LIGHTING);
    RNLoadRgb(1.0, 1.0, 1.0);
    RNGrfxBegin(RN_GRFX_QUADS);
    R3LoadTextureCoords(0.0, 0.0);
    R3LoadPoint(x1 + tx, y1 + ty, -0.5);
    R3LoadTextureCoords(1.0, 0.0);
    R3LoadPoint(x2 + tx, y1 + ty, -0.5);
    R3LoadTextureCoords(1.0, 1.0);
    R3LoadPoint(x2 + tx, y2 + ty, -0.5);
    R3LoadTextureCoords(0.0, 1.0);
    R3LoadPoint(x1 + tx, y2 + ty, -0.5);
    RNGrfxEnd();

    // Unbind texture
    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
  }
#endif
  
  // Draw projected center point
  if (selected_point) {
    RNLoadRgb(1.0, 0.0, 0.0);
    R3Point p(focal_point.X() + tx, focal_point.Y() + ty, -0.25);
    R3Box(p[0]-4, p[1]-4, -0.25, p[0]+4, p[1]+4, -0.25).Draw();
  }

  // Pop ortho viewing matrices
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq 0x2d0(%rdi), %rax
testq %rax, %rax
je 0x2e667
cmpl $0x0, 0x98(%rax)
jle 0x2e667
cmpl $0x0, 0x9c(%rax)
jle 0x2e667
movq %rdi, %rbx
cmpl $0x0, 0x32c(%rdi)
je 0x2e667
xorpd %xmm0, %xmm0
ucomisd 0x2d8(%rbx), %xmm0
jae 0x2e667
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x258(%rax)
cmpl $0x0, 0x5c8(%rbx)
jle 0x2e667
cmpl $0x0, 0x5cc(%rbx)
jle 0x2e667
cmpq $0x0, 0x5d8(%rbx)
je 0x2e667
cvtdq2pd 0x118(%rbx), %xmm5
movq 0x2d0(%rbx), %rax
cvtdq2pd 0x98(%rax), %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
movsd 0x2d8(%rbx), %xmm3
xorpd 0xec217(%rip), %xmm3    # 0x11a490
movapd %xmm5, %xmm4
mulsd %xmm3, %xmm4
unpcklpd %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0]
movapd %xmm5, %xmm4
unpcklpd %xmm2, %xmm4            # xmm4 = xmm4[0],xmm2[0]
mulpd %xmm3, %xmm4
movapd %xmm5, 0x30(%rsp)
addpd %xmm5, %xmm4
movapd %xmm4, 0x40(%rsp)
movsd 0xebd67(%rip), %xmm2    # 0x11a010
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm1
leaq 0x10(%rsp), %rdi
callq 0xc9fbc
movq 0x2c8(%rbx), %rsi
testq %rsi, %rsi
je 0x2e385
movq 0x2d0(%rbx), %r14
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x32b2a
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x418ac
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0xca0f4
movsd (%r14), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jb 0x2e385
movsd 0x18(%rsp), %xmm1
ucomisd %xmm2, %xmm1
jb 0x2e385
movq 0x2d0(%rbx), %rax
cvtsi2sdl 0x98(%rax), %xmm2
movsd 0xec041(%rip), %xmm3    # 0x11a378
addsd %xmm2, %xmm3
ucomisd %xmm0, %xmm3
jb 0x2e385
cvtsi2sdl 0x9c(%rax), %xmm3
movsd 0xec027(%rip), %xmm4    # 0x11a378
addsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jb 0x2e385
movapd 0x30(%rsp), %xmm4
movapd 0x40(%rsp), %xmm5
subpd %xmm5, %xmm4
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
mulpd %xmm4, %xmm0
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
divpd %xmm2, %xmm0
addpd %xmm5, %xmm0
movapd %xmm0, 0x10(%rsp)
movl $0x1701, %edi           # imm = 0x1701
callq 0x27d20
callq 0x27fe0
callq 0x27cb0
movapd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
movsd 0xebfa1(%rip), %xmm4    # 0x11a350
movsd 0xebc51(%rip), %xmm5    # 0x11a008
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
movapd %xmm3, 0x50(%rsp)
callq 0x28260
movl $0x1700, %edi           # imm = 0x1700
callq 0x27d20
callq 0x27fe0
callq 0x27cb0
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
ucomisd 0x40(%rsp), %xmm0
xorpd %xmm2, %xmm2
jbe 0x2e416
movsd 0x10(%rsp), %xmm0
movsd 0xebfee(%rip), %xmm1    # 0x11a3f0
mulsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2e416
subsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movapd 0x40(%rsp), %xmm1
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
movsd %xmm2, 0x28(%rsp)
movapd %xmm1, 0x60(%rsp)
jbe 0x2e45a
movsd 0x18(%rsp), %xmm0
movsd 0xebfac(%rip), %xmm1    # 0x11a3f0
mulsd 0x50(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2e45a
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x260(%rax)
movl 0x5e8(%rbx), %esi
testl %esi, %esi
je 0x2e596
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27bc0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27870
movl $0xb50, %edi            # imm = 0xB50
callq 0x27220
movsd 0xebb6e(%rip), %xmm0    # 0x11a008
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x27ef0
pushq $0x7
popq %rdi
callq 0x271a0
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
callq 0x28370
movapd 0x40(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
movapd 0x60(%rsp), %xmm1
addsd 0x8(%rsp), %xmm1
movapd %xmm1, 0x60(%rsp)
movsd 0xebe94(%rip), %xmm2    # 0x11a378
callq 0x27ee0
xorpd %xmm1, %xmm1
movsd 0xebb13(%rip), %xmm0    # 0x11a008
callq 0x28370
movapd 0x30(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
movaps 0x60(%rsp), %xmm1
movsd 0xebe5f(%rip), %xmm2    # 0x11a378
callq 0x27ee0
movsd 0xebae2(%rip), %xmm0    # 0x11a008
movaps %xmm0, %xmm1
callq 0x28370
movapd 0x50(%rsp), %xmm1
addsd 0x8(%rsp), %xmm1
movapd %xmm1, 0x50(%rsp)
movaps 0x30(%rsp), %xmm0
movsd 0xebe2b(%rip), %xmm2    # 0x11a378
callq 0x27ee0
xorps %xmm0, %xmm0
movsd 0xebaab(%rip), %xmm1    # 0x11a008
callq 0x28370
movapd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
movsd 0xebe02(%rip), %xmm2    # 0x11a378
callq 0x27ee0
callq 0x27cc0
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x27bc0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27220
cmpq $0x0, 0x2c8(%rbx)
je 0x2e649
movsd 0xeba5c(%rip), %xmm0    # 0x11a008
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x27ef0
movsd 0x28(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd 0x18(%rsp), %xmm1
leaq 0x78(%rsp), %rbx
movsd 0xebe1a(%rip), %xmm2    # 0x11a3f8
movq %rbx, %rdi
callq 0xb2320
movsd (%rbx), %xmm3
movsd 0x8(%rbx), %xmm4
movsd 0xebe09(%rip), %xmm1    # 0x11a400
movapd %xmm3, %xmm0
addsd %xmm1, %xmm0
addsd %xmm4, %xmm1
movsd 0xebdfd(%rip), %xmm2    # 0x11a408
addsd %xmm2, %xmm3
addsd %xmm2, %xmm4
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movsd 0xebdd2(%rip), %xmm2    # 0x11a3f8
movapd %xmm2, %xmm5
callq 0x9defa
movl $0x772, %esi            # imm = 0x772
movq %rbx, %rdi
callq 0x755c8
leaq 0x90(%rsp), %rdi
callq 0x9eb12
movl $0x1701, %edi           # imm = 0x1701
callq 0x27d20
callq 0x28240
movl $0x1700, %edi           # imm = 0x1700
callq 0x27d20
callq 0x28240
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x9eb12
movq %rbx, %rdi
callq 0x28140