// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::RenderGridTriangle(gaps::R2Point const&, gaps::R2Point const&, gaps::R2Point const&, double, double, double, double, double, double, gaps::R2Grid&)
void R2Grid::
RenderGridTriangle(
   const R2Point& pointA, const R2Point& pointB, const R2Point& pointC,
   RNScalar valueA, RNScalar valueB, RNScalar valueC,
   RNScalar depthA, RNScalar depthB, RNScalar depthC,
   R2Grid& depth_grid)
{
  // Check values
  if (valueA == R2_GRID_UNKNOWN_VALUE) return;
  if (valueB == R2_GRID_UNKNOWN_VALUE) return;
  if (valueC == R2_GRID_UNKNOWN_VALUE) return;

  // Check depths
  if (depthA == R2_GRID_UNKNOWN_VALUE) return;
  if (depthB == R2_GRID_UNKNOWN_VALUE) return;
  if (depthC == R2_GRID_UNKNOWN_VALUE) return;

  // Get convenient variables
  R2Point points[3] = { pointA, pointB, pointC };
  RNScalar values[3] = { valueA, valueB, valueC };
  RNScalar depths[3] = { depthA, depthB, depthC };

  // Sort vertex indices by Y coordinate
  int iv0, iv1, iv2;
  if (points[0].Y() < points[1].Y()) {
    if (points[0].Y() < points[2].Y()) { 
      if (points[1].Y() < points[2].Y()) { iv0 = 0; iv1 = 1; iv2 = 2; }
      else { iv0 = 0; iv1 = 2; iv2 = 1; }
    }
    else { iv0 = 2; iv1 = 0; iv2 = 1; }
  }
  else {
    if (points[1].Y() < points[2].Y()) { 
      if (points[0].Y() < points[2].Y()) { iv0 = 1; iv1 = 0; iv2 = 2; }
      else { iv0 = 1; iv1 = 2; iv2 = 0; }
    }
    else { iv0 = 2; iv1 = 1; iv2 = 0; }
  }
  
  // Sort vertex coordinates
  double dy = points[iv2].Y() - points[iv0].Y();
  double t1 = (dy > 0) ? (points[iv1].Y() - points[iv0].Y()) / dy : 0;
  double y0 = points[iv0].Y();
  double y1 = points[iv1].Y();
  double y2 = points[iv2].Y();
  double x0 = points[iv0].X();
  double x2 = points[iv2].X();
  double x1a = points[iv1].X();
  double x1b = (1-t1)*x0 + t1*x2;
  double value0 = values[iv0];
  double value2 = values[iv2];
  double value1a = values[iv1];
  double value1b = (1-t1)*value0 + t1*value2;
  double depth0 = depths[iv0];
  double depth2 = depths[iv2];
  double depth1a = depths[iv1];
  double depth1b = (1-t1)*depth0 + t1*depth2;
  if (x1a > x1b) { 
    double swap;
    swap = x1a; x1a = x1b; x1b = swap; 
    swap = value1a; value1a = value1b; value1b = swap; 
    swap = depth1a; depth1a = depth1b; depth1b = swap; 
  }

  // Rasterize lower half of triangle
  int iy0 = (int) (y0 + 0.5);
  int iy1 = (int) (y1 + 0.5);
  int nysteps = iy1 - iy0;
  double xa_step = (nysteps > 0) ? (x1a - x0) / nysteps : 0;
  double xb_step = (nysteps > 0) ? (x1b - x0) / nysteps : 0;
  double valuea_step = (nysteps > 0) ? (value1a  - value0) / nysteps : 0;
  double valueb_step = (nysteps > 0) ? (value1b  - value0) / nysteps : 0;
  double deptha_step = (nysteps > 0) ? (depth1a  - depth0) / nysteps : 0;
  double depthb_step = (nysteps > 0) ? (depth1b  - depth0) / nysteps : 0;
  double xa = x0;
  double xb = x0;
  double valuea = value0;
  double valueb = value0;
  double deptha = depth0;
  double depthb = depth0;
  for (int iy = iy0; iy <= iy1; iy++) {
    int ixa = (int) (xa + 0.5);
    int ixb = (int) (xb + 0.5);
    int nxsteps = ixb - ixa;
    double value_step = (nxsteps > 0) ? (valueb - valuea) / nxsteps : 0;
    double depth_step = (nxsteps > 0) ? (depthb - deptha) / nxsteps : 0;
    double value = valuea;
    double depth = deptha;
    for (int ix = ixa; ix <= ixb; ix++) {
      RNScalar old_depth = depth_grid.GridValue(ix, iy);
      if ((old_depth == R2_GRID_UNKNOWN_VALUE) || (depth < old_depth)) {
        SetGridValue(ix, iy, value);
        depth_grid.SetGridValue(ix, iy, depth);
      }
      value += value_step;
      depth += depth_step;
    }
    valuea += valuea_step;
    valueb += valueb_step;
    deptha += deptha_step;
    depthb += depthb_step;
    xa += xa_step;
    xb += xb_step;
  }
  
  // Rasterize upper half of triangle
  int iy2 = (int) (y2 + 0.5);
  nysteps = iy2 - iy1;
  xa_step = (nysteps > 0) ? (x2 - x1a) / nysteps : 0;
  xb_step = (nysteps > 0) ? (x2 - x1b) / nysteps : 0;
  valuea_step = (nysteps > 0) ? (value2  - value1a) / nysteps : 0;
  valueb_step = (nysteps > 0) ? (value2  - value1b) / nysteps : 0;
  deptha_step = (nysteps > 0) ? (depth2  - depth1a) / nysteps : 0;
  depthb_step = (nysteps > 0) ? (depth2  - depth1b) / nysteps : 0;
  xa = x1a;
  xb = x1b;
  valuea = value1a;
  valueb = value1b;
  deptha = depth1a;
  depthb = depth1b;
  for (int iy = iy1; iy <= iy2; iy++) {
    int ixa = (int) (xa + 0.5);
    int ixb = (int) (xb + 0.5);
    int nxsteps = ixb - ixa;
    double value_step = (nxsteps > 0) ? (valueb - valuea) / nxsteps : 0;
    double depth_step = (nxsteps > 0) ? (depthb - deptha) / nxsteps : 0;
    double value = valuea;
    double depth = deptha;
    for (int ix = ixa; ix <= ixb; ix++) {
      RNScalar old_depth = depth_grid.GridValue(ix, iy);
      if ((old_depth == R2_GRID_UNKNOWN_VALUE) || (depth < old_depth)) {
        SetGridValue(ix, iy, value);
        depth_grid.SetGridValue(ix, iy, depth);
      }
      value += value_step;
      depth += depth_step;
    }
    valuea += valuea_step;
    valueb += valueb_step;
    deptha += deptha_step;
    depthb += depthb_step;
    xa += xa_step;
    xb += xb_step;
  }
}

// Assembly:
ucomisd 0x43662(%rip), %xmm0    # 0xa2fb8
jne 0x5f95e
jnp 0x5ff49
ucomisd 0x43652(%rip), %xmm1    # 0xa2fb8
jne 0x5f96e
jnp 0x5ff49
ucomisd 0x43642(%rip), %xmm2    # 0xa2fb8
jne 0x5f97e
jnp 0x5ff49
ucomisd 0x43632(%rip), %xmm3    # 0xa2fb8
jne 0x5f98e
jnp 0x5ff49
ucomisd 0x43622(%rip), %xmm4    # 0xa2fb8
jne 0x5f99e
jnp 0x5ff49
ucomisd 0x43612(%rip), %xmm5    # 0xa2fb8
jne 0x5f9ae
jnp 0x5ff49
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %r13
movq %rdx, %r14
movq %rdi, %rbx
movq %r8, 0x10(%rsp)
leaq 0xa0(%rsp), %r15
leaq 0x90(%rsp), %r12
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movsd %xmm5, 0x8(%rsp)
movsd %xmm4, 0x38(%rsp)
movsd %xmm3, 0x30(%rsp)
movsd %xmm2, 0x28(%rsp)
movsd %xmm1, 0x20(%rsp)
movsd %xmm0, 0x18(%rsp)
callq 0x680b0
movq %r12, %rdi
movq %r14, %rsi
callq 0x680b0
movq %r15, %rdi
movq %r13, %rsi
callq 0x680b0
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movsd 0x18(%rsp), %xmm0
movsd %xmm0, -0x10(%rdi)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, -0x8(%rdi)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rdi)
movsd 0x30(%rsp), %xmm0
movsd %xmm0, -0x10(%rsi)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, -0x8(%rsi)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsi)
movsd 0x8(%rbp), %xmm1
movsd 0x18(%rbp), %xmm2
ucomisd %xmm1, %xmm2
movsd 0x28(%rbp), %xmm0
jbe 0x5faaf
ucomisd %xmm1, %xmm0
jbe 0x5fadb
xorl %ecx, %ecx
ucomisd %xmm2, %xmm0
seta %cl
movl $0x2, %eax
subq %rcx, %rax
incq %rcx
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %r15
jmp 0x5faeb
ucomisd %xmm2, %xmm0
jbe 0x5fae4
leaq 0x48(%rsp), %rsi
xorl %eax, %eax
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
setbe %al
seta %cl
leaq 0x68(%rsp), %rdi
addl %ecx, %ecx
addl %eax, %eax
leaq 0x90(%rsp), %r15
jmp 0x5faeb
movl $0x1, %ecx
xorl %eax, %eax
jmp 0x5faeb
movl $0x1, %eax
xorl %ecx, %ecx
movl %ecx, %edx
shll $0x4, %edx
movsd 0x80(%rsp,%rdx), %xmm0
movsd 0x88(%rsp,%rdx), %xmm2
movsd (%r15), %xmm6
movsd 0x8(%r15), %xmm8
movsd %xmm2, 0x8(%rsp)
subsd %xmm8, %xmm2
movl %eax, %edx
shll $0x4, %edx
movsd 0x80(%rsp,%rdx), %xmm1
movsd 0x88(%rsp,%rdx), %xmm9
movapd %xmm9, %xmm4
subsd %xmm8, %xmm4
divsd %xmm2, %xmm4
xorpd %xmm3, %xmm3
cmpltsd %xmm2, %xmm3
andpd %xmm4, %xmm3
movsd 0x424e4(%rip), %xmm10   # 0xa2038
subsd %xmm3, %xmm10
movapd %xmm3, %xmm2
mulsd %xmm0, %xmm2
movapd %xmm10, %xmm11
mulsd %xmm6, %xmm11
addsd %xmm2, %xmm11
shll $0x3, %ecx
shll $0x3, %eax
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
movsd 0x40(%rsp,%rcx), %xmm2
movhpd 0x60(%rsp,%rcx), %xmm2  # xmm2 = xmm2[0],mem[0]
mulpd %xmm2, %xmm3
movsd (%rsi), %xmm7
movhpd (%rdi), %xmm7           # xmm7 = xmm7[0],mem[0]
unpcklpd %xmm10, %xmm10          # xmm10 = xmm10[0,0]
mulpd %xmm7, %xmm10
ucomisd %xmm11, %xmm1
addpd %xmm3, %xmm10
movsd 0x40(%rsp,%rax), %xmm3
movhpd 0x60(%rsp,%rax), %xmm3  # xmm3 = xmm3[0],mem[0]
ja 0x5fbc9
unpcklpd %xmm1, %xmm11           # xmm11 = xmm11[0],xmm1[0]
movapd %xmm3, %xmm4
movapd %xmm10, %xmm3
movapd %xmm11, %xmm1
jmp 0x5fbd3
unpcklpd %xmm11, %xmm1           # xmm1 = xmm1[0],xmm11[0]
movapd %xmm10, %xmm4
movq 0x10(%rsp), %r15
unpcklpd %xmm8, %xmm9            # xmm9 = xmm9[0],xmm8[0]
addpd 0x4242a(%rip), %xmm9    # 0xa2010
cvttpd2dq %xmm9, %xmm5
movd %xmm5, %eax
pshufd $0x55, %xmm5, %xmm5     # xmm5 = xmm5[1,1,1,1]
movd %xmm5, %edx
movl %eax, %esi
subl %edx, %esi
xorl %ecx, %ecx
testl %esi, %esi
setg %sil
movl %eax, %edi
shufpd $0x1, %xmm1, %xmm1      # xmm1 = xmm1[1,0]
subl %edx, %edi
jl 0x5fda1
xorps %xmm5, %xmm5
cvtsi2sd %edi, %xmm5
unpcklpd %xmm6, %xmm6            # xmm6 = xmm6[0,0]
movapd %xmm1, %xmm8
subpd %xmm6, %xmm8
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
divpd %xmm5, %xmm8
movb %sil, %cl
movd %ecx, %xmm9
pshufd $0x0, %xmm9, %xmm11     # xmm11 = xmm9[0,0,0,0]
pslld $0x1f, %xmm11
psrad $0x1f, %xmm11
andpd %xmm11, %xmm8
movapd %xmm4, %xmm9
subpd %xmm7, %xmm9
divpd %xmm5, %xmm9
movapd %xmm3, %xmm10
subpd %xmm7, %xmm10
divpd %xmm5, %xmm10
andpd %xmm11, %xmm9
andpd %xmm11, %xmm10
movslq 0xc8(%r15), %rcx
movslq %edx, %rdx
leal 0x1(%rax), %esi
leaq (,%rdx,8), %rdi
movq %rdx, %r8
imulq %rcx, %r8
shlq $0x3, %r8
addq 0xb8(%r15), %r8
shlq $0x3, %rcx
movapd 0x42364(%rip), %xmm11   # 0xa2010
movsd 0x43303(%rip), %xmm12   # 0xa2fb8
movapd %xmm7, %xmm13
movapd %xmm6, %xmm5
addpd %xmm11, %xmm5
cvttpd2dq %xmm5, %xmm5
movd %xmm5, %r10d
pshufd $0x55, %xmm5, %xmm5     # xmm5 = xmm5[1,1,1,1]
movd %xmm5, %r9d
movl %r9d, %ebp
subl %r10d, %ebp
xorl %r11d, %r11d
testl %ebp, %ebp
setg %bpl
subl %r10d, %r9d
jl 0x5fd80
movapd %xmm13, %xmm5
subpd %xmm7, %xmm5
xorps %xmm14, %xmm14
cvtsi2sd %r9d, %xmm14
unpcklpd %xmm14, %xmm14          # xmm14 = xmm14[0,0]
divpd %xmm14, %xmm5
movb %bpl, %r11b
movd %r11d, %xmm14
pshufd $0x0, %xmm14, %xmm14    # xmm14 = xmm14[0,0,0,0]
pslld $0x1f, %xmm14
psrad $0x1f, %xmm14
pand %xmm5, %xmm14
movslq 0xc8(%rbx), %r11
movslq %r10d, %r14
imulq %rdi, %r11
leaq (%r11,%r14,8), %r10
addq 0xb8(%rbx), %r10
leaq (%r8,%r14,8), %r11
incl %r9d
xorl %r14d, %r14d
movapd %xmm7, %xmm15
movsd (%r11,%r14,8), %xmm5
ucomisd %xmm12, %xmm5
jne 0x5fd60
jnp 0x5fd67
ucomisd %xmm15, %xmm5
jbe 0x5fd73
movhpd %xmm15, (%r10,%r14,8)
movsd %xmm15, (%r11,%r14,8)
addpd %xmm14, %xmm15
incq %r14
cmpl %r14d, %r9d
jne 0x5fd51
addpd %xmm9, %xmm7
addpd %xmm10, %xmm13
addpd %xmm8, %xmm6
incq %rdx
addq $0x8, %rdi
addq %rcx, %r8
cmpl %edx, %esi
jne 0x5fcba
movsd 0x8(%rsp), %xmm5
addsd 0x42281(%rip), %xmm5    # 0xa2030
cvttsd2si %xmm5, %ecx
movl %ecx, %esi
subl %eax, %esi
xorl %edx, %edx
testl %esi, %esi
setg %sil
movl %ecx, %edi
subl %eax, %edi
jl 0x5ff38
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
xorps %xmm6, %xmm6
cvtsi2sd %edi, %xmm6
subpd %xmm1, %xmm0
unpcklpd %xmm6, %xmm6            # xmm6 = xmm6[0,0]
divpd %xmm6, %xmm0
movb %sil, %dl
movd %edx, %xmm5
pshufd $0x0, %xmm5, %xmm7      # xmm7 = xmm5[0,0,0,0]
pslld $0x1f, %xmm7
psrad $0x1f, %xmm7
andpd %xmm7, %xmm0
movapd %xmm2, %xmm5
subpd %xmm4, %xmm5
subpd %xmm3, %xmm2
divpd %xmm6, %xmm5
divpd %xmm6, %xmm2
andpd %xmm7, %xmm5
andpd %xmm7, %xmm2
movslq 0xc8(%r15), %rdx
cltq
incl %ecx
movq %rax, %rsi
imulq %rdx, %rsi
shlq $0x3, %rsi
addq 0xb8(%r15), %rsi
leaq (,%rax,8), %rdi
shlq $0x3, %rdx
movapd 0x421c9(%rip), %xmm6    # 0xa2010
movsd 0x43169(%rip), %xmm7    # 0xa2fb8
movapd %xmm1, %xmm8
addpd %xmm6, %xmm8
cvttpd2dq %xmm8, %xmm8
movd %xmm8, %r9d
pshufd $0x55, %xmm8, %xmm8     # xmm8 = xmm8[1,1,1,1]
movd %xmm8, %r8d
movl %r8d, %r11d
subl %r9d, %r11d
xorl %r10d, %r10d
testl %r11d, %r11d
setg %r11b
subl %r9d, %r8d
jl 0x5ff1a
movapd %xmm3, %xmm9
subpd %xmm4, %xmm9
xorps %xmm8, %xmm8
cvtsi2sd %r8d, %xmm8
unpcklpd %xmm8, %xmm8            # xmm8 = xmm8[0,0]
divpd %xmm8, %xmm9
movb %r11b, %r10b
movd %r10d, %xmm8
pshufd $0x0, %xmm8, %xmm8      # xmm8 = xmm8[0,0,0,0]
pslld $0x1f, %xmm8
psrad $0x1f, %xmm8
pand %xmm9, %xmm8
movslq 0xc8(%rbx), %r10
movslq %r9d, %r11
imulq %rdi, %r10
leaq (%r10,%r11,8), %r9
addq 0xb8(%rbx), %r9
leaq (%rsi,%r11,8), %r10
incl %r8d
xorl %r11d, %r11d
movapd %xmm4, %xmm9
movsd (%r10,%r11,8), %xmm10
ucomisd %xmm7, %xmm10
jne 0x5fefa
jnp 0x5ff01
ucomisd %xmm9, %xmm10
jbe 0x5ff0d
movhpd %xmm9, (%r9,%r11,8)
movsd %xmm9, (%r10,%r11,8)
addpd %xmm8, %xmm9
incq %r11
cmpl %r11d, %r8d
jne 0x5feeb
addpd %xmm5, %xmm4
addpd %xmm2, %xmm3
addpd %xmm0, %xmm1
incq %rax
addq $0x8, %rdi
addq %rdx, %rsi
cmpl %eax, %ecx
jne 0x5fe4f
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq