// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/gapsview/gapsview.cpp
// Function: DrawImageDepths(ImageData*, gaps::R3Point const&, gaps::R3Vector const&, gaps::R3Vector const&, gaps::R3Vector const&, double, double, double)
static void
DrawImageDepths(ImageData *data, const R3Point& viewpoint,
  const R3Vector& towards, const R3Vector& right, const R3Vector& up,
  RNAngle xfov, RNAngle yfov, RNLength image_billboard_depth)
{
  // Check if drawing image depths
  if (!show_image_depths) return;

  // Check image data
  int w = data->image_width;
  int h = data->image_height;
  if (w == 0) return;
  if (h == 0) return;
  if (data->depth_image.XResolution() != w) return;
  if (data->depth_image.YResolution() != h) return;
  
  // Load texture
  LoadTexture(data);
  if (data->texture_id == 0) return;

  // Enable texture
  glBindTexture(GL_TEXTURE_2D, data->texture_id);
  glEnable(GL_TEXTURE_2D);
  glDisable(GL_LIGHTING);
  RNLoadRgb(1, 1, 1);

  // Check if have ray images
  if ((data->ray_origin_images[0].XResolution() == w) &&
      (data->ray_origin_images[1].XResolution() == w) &&
      (data->ray_origin_images[2].XResolution() == w) && 
      (data->ray_origin_images[0].YResolution() == h) &&
      (data->ray_origin_images[1].YResolution() == h) &&
      (data->ray_origin_images[2].YResolution() == h) && 
      (data->ray_direction_images[0].XResolution() == w) &&
      (data->ray_direction_images[1].XResolution() == w) &&
      (data->ray_direction_images[2].XResolution() == w) && 
      (data->ray_direction_images[0].YResolution() == h) &&
      (data->ray_direction_images[1].YResolution() == h) &&
      (data->ray_direction_images[2].YResolution() == h)) {
    RNGrfxBegin(RN_GRFX_POINTS);
    for (int ix = 0; ix < w; ix++) {
      for (int iy = 0; iy < h; iy++) {
        // Get/check depth
        double depth = data->depth_image.GridValue(ix, iy);
        if ((depth <= 0) || (depth == R2_GRID_UNKNOWN_VALUE)) continue;

        // Get/check ray direction
        double dx = data->ray_direction_images[0].GridValue(ix, iy);    
        double dy = data->ray_direction_images[1].GridValue(ix, iy);    
        double dz = data->ray_direction_images[2].GridValue(ix, iy);
        R3Vector direction(dx, dy, dz);
        if (direction.IsZero()) continue;

        // Get ray origin
        double ox = data->ray_origin_images[0].GridValue(ix, iy);    
        double oy = data->ray_origin_images[1].GridValue(ix, iy);    
        double oz = data->ray_origin_images[2].GridValue(ix, iy);    
        R3Point origin(ox, oy, oz);

        // Get point position
        RNScalar t = depth * direction.Dot(towards);
        R3Point p = origin + t * direction;

        // Get texture coordinates
        double u = (double) ix / (double) w;
        double v = (double) iy / (double) h;

        // Draw point
        R3LoadTextureCoords(u, v);
        R3LoadPoint(p);
      }
    }
    RNGrfxEnd();
  }
  else {
    // Compute view plane rectangle in world coordinates
    R3Vector dx = 2.0 * right * image_billboard_depth * tan(xfov);
    R3Vector dy = 2.0 * up * image_billboard_depth * tan(yfov);
    R3Point origin = viewpoint + towards * image_billboard_depth;
    R3Point lower_left = origin - 0.5*dx - 0.5*dy;

    // Backproject pixels from the view plane rectangle
    RNGrfxBegin(RN_GRFX_POINTS);
    for (int ix = 0; ix < w; ix++) {
      for (int iy = 0; iy < h; iy++) {
        double depth = data->depth_image.GridValue(ix, iy);
        if ((depth <= 0) || (depth == R2_GRID_UNKNOWN_VALUE)) continue;
        double u = (double) ix / (double) w;
        double v = (double) iy / (double) h;
        R3Point pA = lower_left + u*dx + v*dy;
        R3Vector vA = pA - viewpoint;
        double dA = vA.Dot(towards);
        if (dA <= 0) continue;
        R3Point p = viewpoint + vA * depth / dA;
        R3LoadTextureCoords(u, v);
        R3LoadPoint(p);
      }
    }
    RNGrfxEnd();
  }
  
  // Disable texture
  glDisable(GL_TEXTURE_2D);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, 0x48(%rsp)
movq %rsi, 0x30(%rsp)
cmpl $0x0, 0x194332(%rip)    # 0x1c43d4
je 0x3076d
movq %rcx, %r12
movl 0x768(%rdi), %r15d
movl 0x76c(%rdi), %edx
testl %r15d, %r15d
sete %al
testl %edx, %edx
sete %cl
orb %al, %cl
jne 0x3076d
cmpl %r15d, 0xd8(%rdi)
jne 0x3076d
cmpl %edx, 0xdc(%rdi)
jne 0x3076d
movq %r8, %rbx
movq %rdi, 0x8(%rsp)
movq 0x8(%rsp), %rdi
movl %edx, (%rsp)
movsd %xmm1, 0x20(%rsp)
movsd %xmm0, 0x18(%rsp)
callq 0x3094d
movq 0x8(%rsp), %rax
movl 0x770(%rax), %esi
testl %esi, %esi
je 0x3076d
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27be0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27890
movl $0xb50, %edi            # imm = 0xB50
callq 0x27220
movss 0xfafee(%rip), %xmm0    # 0x12b12c
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
callq 0x283c0
movq 0x8(%rsp), %rcx
cmpl %r15d, 0x348(%rcx)
jne 0x30442
cmpl %r15d, 0x418(%rcx)
jne 0x30442
cmpl %r15d, 0x4e8(%rcx)
jne 0x30442
movl (%rsp), %eax
cmpl %eax, 0x34c(%rcx)
jne 0x30442
movl (%rsp), %eax
cmpl %eax, 0x41c(%rcx)
jne 0x30442
movl (%rsp), %eax
cmpl %eax, 0x4ec(%rcx)
jne 0x30442
cmpl %r15d, 0x5b8(%rcx)
jne 0x30442
cmpl %r15d, 0x688(%rcx)
jne 0x30442
cmpl %r15d, 0x758(%rcx)
jne 0x30442
movl (%rsp), %eax
cmpl %eax, 0x5bc(%rcx)
jne 0x30442
movl (%rsp), %eax
cmpl %eax, 0x68c(%rcx)
jne 0x30442
movl (%rsp), %eax
cmpl %eax, 0x75c(%rcx)
jne 0x30442
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x271a0
movq 0x8(%rsp), %rsi
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0x40(%rsp)
movl (%rsp), %r12d
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x50(%rsp)
testl %r12d, %r12d
cmovlel %ebp, %r12d
testl %r15d, %r15d
cmovlel %ebp, %r15d
movl %r15d, 0x14(%rsp)
xorpd %xmm1, %xmm1
movsd 0xfae31(%rip), %xmm0    # 0x12b070
leaq 0x60(%rsp), %r13
xorpd %xmm2, %xmm2
movsd %xmm2, 0x28(%rsp)
movl %r12d, (%rsp)
cmpl 0x14(%rsp), %ebp
je 0x3075e
movsd 0x28(%rsp), %xmm2
divsd 0x40(%rsp), %xmm2
movsd %xmm2, 0x30(%rsp)
xorl %r15d, %r15d
xorpd %xmm2, %xmm2
cmpl %r15d, %r12d
je 0x3042b
movq 0xd0(%rsi), %rax
movl 0xe0(%rsi), %ecx
imull %r15d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm3
ucomisd %xmm3, %xmm1
jae 0x3041f
ucomisd 0xfae65(%rip), %xmm3    # 0x12b110
jne 0x302b3
jnp 0x3041f
movsd %xmm3, 0x18(%rsp)
movsd %xmm2, 0x20(%rsp)
movl 0x5c0(%rsi), %eax
imull %r15d, %eax
addl %ebp, %eax
cltq
movq 0x5b0(%rsi), %rcx
movq 0x680(%rsi), %rdx
movsd (%rcx,%rax,8), %xmm0
movl 0x690(%rsi), %eax
imull %r15d, %eax
addl %ebp, %eax
cltq
movsd (%rdx,%rax,8), %xmm1
movq 0x750(%rsi), %rax
movl 0x760(%rsi), %ecx
imull %r15d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movq %r13, %rdi
callq 0xc15bc
xorpd %xmm1, %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x30348
jp 0x30348
movsd 0x68(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x30348
jp 0x30348
movsd 0x70(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x30348
jnp 0x30408
movq 0x8(%rsp), %rsi
movl 0x350(%rsi), %eax
imull %r15d, %eax
addl %ebp, %eax
cltq
movq 0x340(%rsi), %rcx
movq 0x410(%rsi), %rdx
movsd (%rcx,%rax,8), %xmm0
movl 0x420(%rsi), %eax
imull %r15d, %eax
addl %ebp, %eax
cltq
movsd (%rdx,%rax,8), %xmm1
movq 0x4e0(%rsi), %rax
movl 0x4f0(%rsi), %ecx
imull %r15d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
callq 0xc0dfc
movq %r13, %rdi
movq 0x48(%rsp), %rsi
callq 0xc1836
mulsd 0x18(%rsp), %xmm0
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0xc1cb2
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xc10c3
movsd 0x20(%rsp), %xmm1
divsd 0x50(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
callq 0x283a0
movq %rbx, %rdi
callq 0x271c0
xorpd %xmm1, %xmm1
movl (%rsp), %r12d
movq 0x8(%rsp), %rsi
movsd 0xfac57(%rip), %xmm0    # 0x12b070
movsd 0x20(%rsp), %xmm2
addsd %xmm0, %xmm2
incl %r15d
jmp 0x30275
movsd 0x28(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x28(%rsp)
incl %ebp
jmp 0x30252
leaq 0x90(%rsp), %r13
movsd 0xfacb6(%rip), %xmm0    # 0x12b108
movq %r13, %rdi
movq %r12, %rsi
callq 0xc1cb2
leaq 0xa8(%rsp), %r14
movsd 0xfac03(%rip), %xmm0    # 0x12b070
movq %r14, %rdi
movq %r13, %rsi
callq 0xc1cb2
movsd 0x18(%rsp), %xmm0
callq 0x274e0
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xc1cb2
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd 0xfac62(%rip), %xmm0    # 0x12b108
callq 0xc1cb2
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movsd 0xfabaf(%rip), %xmm0    # 0x12b070
callq 0xc1cb2
movsd 0x20(%rsp), %xmm0
callq 0x274e0
leaq 0xa8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc1cb2
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
movsd 0xfab77(%rip), %xmm0    # 0x12b070
callq 0xc1cb2
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %rsi
movq %rbx, %rdx
callq 0xc10c3
leaq 0x120(%rsp), %rbx
movsd 0xfab12(%rip), %xmm0    # 0x12b038
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc1cb2
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xc154d
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movsd 0xfaadb(%rip), %xmm0    # 0x12b038
callq 0xc1cb2
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xc154d
movl %r15d, %ebp
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x271a0
movsd 0xfaae9(%rip), %xmm0    # 0x12b070
movq 0x8(%rsp), %rdx
cvtsi2sd %ebp, %xmm1
movsd %xmm1, 0x58(%rsp)
movl (%rsp), %r14d
cvtsi2sd %r14d, %xmm1
movsd %xmm1, 0x28(%rsp)
testl %r14d, %r14d
cmovlel %r15d, %r14d
testl %ebp, %ebp
cmovlel %r15d, %ebp
movl %ebp, 0x14(%rsp)
xorpd %xmm1, %xmm1
leaq 0x108(%rsp), %rbp
xorpd %xmm2, %xmm2
movsd %xmm2, 0x38(%rsp)
cmpl 0x14(%rsp), %r15d
je 0x3075e
movsd 0x38(%rsp), %xmm2
divsd 0x58(%rsp), %xmm2
movsd %xmm2, 0x18(%rsp)
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
cmpl %r12d, %r14d
je 0x30746
movq 0xd0(%rdx), %rax
movl 0xe0(%rdx), %ecx
imull %r12d, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm3
ucomisd %xmm3, %xmm1
jae 0x3073a
ucomisd 0xfaae9(%rip), %xmm3    # 0x12b110
jne 0x3062f
jnp 0x3073a
movsd %xmm3, 0x50(%rsp)
movsd %xmm2, 0x20(%rsp)
divsd 0x28(%rsp), %xmm2
movsd %xmm2, (%rsp)
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
leaq 0x60(%rsp), %rsi
movsd 0x18(%rsp), %xmm0
callq 0xc1cb2
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
movq %r13, %rdx
callq 0xc10c3
movq %rbp, %rdi
leaq 0xa8(%rsp), %rsi
movsd (%rsp), %xmm0
callq 0xc1cb2
leaq 0xd8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0xc10c3
movq %rbx, %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
callq 0xc0fd9
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
callq 0xc1836
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x30727
leaq 0xf0(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movsd %xmm0, 0x40(%rsp)
movsd 0x50(%rsp), %xmm0
callq 0xc1cb2
movq %rbp, %rdi
movq %r13, %rsi
movsd 0x40(%rsp), %xmm0
callq 0xc1fce
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0xc10c3
movsd 0x18(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x283a0
movq %r13, %rdi
callq 0x271c0
xorpd %xmm1, %xmm1
movq 0x8(%rsp), %rdx
movsd 0xfa93c(%rip), %xmm0    # 0x12b070
movsd 0x20(%rsp), %xmm2
addsd %xmm0, %xmm2
incl %r12d
jmp 0x305f0
movsd 0x38(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x38(%rsp)
incl %r15d
jmp 0x305cc
callq 0x27ce0
movl $0xde1, %edi            # imm = 0xDE1
callq 0x27220
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq