// File: /workspace/github_cpp_makefile_star2_O1/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 0x2f9f7
cmpl $0x0, 0x98(%rax)
jle 0x2f9f7
cmpl $0x0, 0x9c(%rax)
jle 0x2f9f7
movq %rdi, %rbx
cmpl $0x0, 0x32c(%rdi)
je 0x2f9f7
xorpd %xmm0, %xmm0
ucomisd 0x2d8(%rbx), %xmm0
jae 0x2f9f7
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x258(%rax)
cmpl $0x0, 0x5c8(%rbx)
jle 0x2f9f7
cmpl $0x0, 0x5cc(%rbx)
jle 0x2f9f7
cmpq $0x0, 0x5d8(%rbx)
je 0x2f9f7
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 0x10be8f(%rip), %xmm3   # 0x13b490
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 0x10b9df(%rip), %xmm2   # 0x13b010
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm1
leaq 0x10(%rsp), %rdi
callq 0xdf9d2
movq 0x2c8(%rbx), %rsi
testq %rsi, %rsi
je 0x2f713
movq 0x2d0(%rbx), %r14
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x3455c
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x452ba
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0xdfb0a
movsd (%r14), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jb 0x2f713
movsd 0x18(%rsp), %xmm1
ucomisd %xmm2, %xmm1
jb 0x2f713
movq 0x2d0(%rbx), %rax
xorps %xmm2, %xmm2
cvtsi2sdl 0x98(%rax), %xmm2
movsd 0x10bcb6(%rip), %xmm3   # 0x13b378
addsd %xmm2, %xmm3
ucomisd %xmm0, %xmm3
jb 0x2f713
xorps %xmm3, %xmm3
cvtsi2sdl 0x9c(%rax), %xmm3
movsd 0x10bc99(%rip), %xmm4   # 0x13b378
addsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jb 0x2f713
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 0x27fd0
callq 0x27cb0
movapd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
movsd 0x10bc13(%rip), %xmm4   # 0x13b350
movsd 0x10b8c3(%rip), %xmm5   # 0x13b008
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
movapd %xmm3, 0x50(%rsp)
callq 0x28250
movl $0x1700, %edi           # imm = 0x1700
callq 0x27d20
callq 0x27fd0
callq 0x27cb0
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
ucomisd 0x40(%rsp), %xmm0
xorpd %xmm2, %xmm2
jbe 0x2f7a4
movsd 0x10(%rsp), %xmm0
movsd 0x10bc60(%rip), %xmm1   # 0x13b3f0
mulsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2f7a4
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 0x2f7e8
movsd 0x18(%rsp), %xmm0
movsd 0x10bc1e(%rip), %xmm1   # 0x13b3f0
mulsd 0x50(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2f7e8
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x260(%rax)
movl 0x5e8(%rbx), %esi
testl %esi, %esi
je 0x2f926
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27bd0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27860
movl $0xb50, %edi            # imm = 0xB50
callq 0x27200
movsd 0x10b7e0(%rip), %xmm0   # 0x13b008
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x27ee0
movl $0x7, %edi
callq 0x271a0
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
callq 0x28340
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 0x10bb04(%rip), %xmm2   # 0x13b378
callq 0x27ed0
xorpd %xmm1, %xmm1
movsd 0x10b783(%rip), %xmm0   # 0x13b008
callq 0x28340
movapd 0x30(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
movaps 0x60(%rsp), %xmm1
movsd 0x10bacf(%rip), %xmm2   # 0x13b378
callq 0x27ed0
movsd 0x10b752(%rip), %xmm0   # 0x13b008
movaps %xmm0, %xmm1
callq 0x28340
movapd 0x50(%rsp), %xmm1
addsd 0x8(%rsp), %xmm1
movapd %xmm1, 0x50(%rsp)
movaps 0x30(%rsp), %xmm0
movsd 0x10ba9b(%rip), %xmm2   # 0x13b378
callq 0x27ed0
xorps %xmm0, %xmm0
movsd 0x10b71b(%rip), %xmm1   # 0x13b008
callq 0x28340
movapd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
movsd 0x10ba72(%rip), %xmm2   # 0x13b378
callq 0x27ed0
callq 0x27cc0
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x27bd0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27200
cmpq $0x0, 0x2c8(%rbx)
je 0x2f9d9
movsd 0x10b6cc(%rip), %xmm0   # 0x13b008
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x27ee0
movsd 0x28(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd 0x18(%rsp), %xmm1
leaq 0x78(%rsp), %rbx
movsd 0x10ba8a(%rip), %xmm2   # 0x13b3f8
movq %rbx, %rdi
callq 0xc531e
movsd (%rbx), %xmm3
movsd 0x8(%rbx), %xmm4
movsd 0x10ba79(%rip), %xmm1   # 0x13b400
movapd %xmm3, %xmm0
addsd %xmm1, %xmm0
addsd %xmm4, %xmm1
movsd 0x10ba6d(%rip), %xmm2   # 0x13b408
addsd %xmm2, %xmm3
addsd %xmm2, %xmm4
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movsd 0x10ba42(%rip), %xmm2   # 0x13b3f8
movapd %xmm2, %xmm5
callq 0xae5c8
movl $0x772, %esi            # imm = 0x772
movq %rbx, %rdi
callq 0x80e08
leaq 0x90(%rsp), %rdi
callq 0xaf380
movl $0x1701, %edi           # imm = 0x1701
callq 0x27d20
callq 0x28230
movl $0x1700, %edi           # imm = 0x1700
callq 0x27d20
callq 0x28230
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xaf380
movq %rbx, %rdi
callq 0x28130