// File: /workspace/github_cpp_makefile_star2_O3/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 0x2f8a9
cmpl $0x0, 0x98(%rax)
jle 0x2f8a9
cmpl $0x0, 0x9c(%rax)
jle 0x2f8a9
movq %rdi, %rbx
cmpl $0x0, 0x32c(%rdi)
je 0x2f8a9
xorpd %xmm0, %xmm0
ucomisd 0x2d8(%rbx), %xmm0
jae 0x2f8a9
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x258(%rax)
cmpl $0x0, 0x5c8(%rbx)
jle 0x2f8a9
cmpl $0x0, 0x5cc(%rbx)
jle 0x2f8a9
cmpq $0x0, 0x5d8(%rbx)
je 0x2f8a9
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 0x108fdd(%rip), %xmm3   # 0x138490
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 0x108b2d(%rip), %xmm2   # 0x138010
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm1
leaq 0x10(%rsp), %rdi
callq 0xde1e6
movq 0x2c8(%rbx), %rsi
testq %rsi, %rsi
je 0x2f5c5
movq 0x2d0(%rbx), %r14
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x344fc
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x452ca
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0xde31e
movsd (%r14), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jb 0x2f5c5
movsd 0x18(%rsp), %xmm1
ucomisd %xmm2, %xmm1
jb 0x2f5c5
movq 0x2d0(%rbx), %rax
xorps %xmm2, %xmm2
cvtsi2sdl 0x98(%rax), %xmm2
movsd 0x108e04(%rip), %xmm3   # 0x138378
addsd %xmm2, %xmm3
ucomisd %xmm0, %xmm3
jb 0x2f5c5
xorps %xmm3, %xmm3
cvtsi2sdl 0x9c(%rax), %xmm3
movsd 0x108de7(%rip), %xmm4   # 0x138378
addsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jb 0x2f5c5
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 0x27d10
callq 0x27fc0
callq 0x27ca0
movapd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
movsd 0x108d61(%rip), %xmm4   # 0x138350
movsd 0x108a11(%rip), %xmm5   # 0x138008
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
movapd %xmm3, 0x50(%rsp)
callq 0x28240
movl $0x1700, %edi           # imm = 0x1700
callq 0x27d10
callq 0x27fc0
callq 0x27ca0
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
ucomisd 0x40(%rsp), %xmm0
xorpd %xmm2, %xmm2
jbe 0x2f656
movsd 0x10(%rsp), %xmm0
movsd 0x108dae(%rip), %xmm1   # 0x1383f0
mulsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2f656
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 0x2f69a
movsd 0x18(%rsp), %xmm0
movsd 0x108d6c(%rip), %xmm1   # 0x1383f0
mulsd 0x50(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2f69a
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x260(%rax)
movl 0x5e8(%rbx), %esi
testl %esi, %esi
je 0x2f7d8
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27bc0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27860
movl $0xb50, %edi            # imm = 0xB50
callq 0x27200
movsd 0x10892e(%rip), %xmm0   # 0x138008
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x27ed0
movl $0x7, %edi
callq 0x271a0
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
callq 0x28330
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 0x108c52(%rip), %xmm2   # 0x138378
callq 0x27ec0
xorpd %xmm1, %xmm1
movsd 0x1088d1(%rip), %xmm0   # 0x138008
callq 0x28330
movapd 0x30(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
movaps 0x60(%rsp), %xmm1
movsd 0x108c1d(%rip), %xmm2   # 0x138378
callq 0x27ec0
movsd 0x1088a0(%rip), %xmm0   # 0x138008
movaps %xmm0, %xmm1
callq 0x28330
movapd 0x50(%rsp), %xmm1
addsd 0x8(%rsp), %xmm1
movapd %xmm1, 0x50(%rsp)
movaps 0x30(%rsp), %xmm0
movsd 0x108be9(%rip), %xmm2   # 0x138378
callq 0x27ec0
xorps %xmm0, %xmm0
movsd 0x108869(%rip), %xmm1   # 0x138008
callq 0x28330
movapd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
movsd 0x108bc0(%rip), %xmm2   # 0x138378
callq 0x27ec0
callq 0x27cb0
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
callq 0x27bc0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27200
cmpq $0x0, 0x2c8(%rbx)
je 0x2f88b
movsd 0x10881a(%rip), %xmm0   # 0x138008
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x27ed0
movsd 0x28(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd 0x18(%rsp), %xmm1
leaq 0x78(%rsp), %rbx
movsd 0x108bd8(%rip), %xmm2   # 0x1383f8
movq %rbx, %rdi
callq 0xc3d32
movsd (%rbx), %xmm3
movsd 0x8(%rbx), %xmm4
movsd 0x108bc7(%rip), %xmm1   # 0x138400
movapd %xmm3, %xmm0
addsd %xmm1, %xmm0
addsd %xmm4, %xmm1
movsd 0x108bbb(%rip), %xmm2   # 0x138408
addsd %xmm2, %xmm3
addsd %xmm2, %xmm4
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movsd 0x108b90(%rip), %xmm2   # 0x1383f8
movapd %xmm2, %xmm5
callq 0xad364
movl $0x772, %esi            # imm = 0x772
movq %rbx, %rdi
callq 0x804b4
leaq 0x90(%rsp), %rdi
callq 0xae0e4
movl $0x1701, %edi           # imm = 0x1701
callq 0x27d10
callq 0x28220
movl $0x1700, %edi           # imm = 0x1700
callq 0x27d10
callq 0x28220
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xae0e4
movq %rbx, %rdi
callq 0x28120