// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::DrawSlice(int, int) const
void R3Grid::
DrawSlice(RNDimension dim, int coord) const
{
  // Check coordinates
  if ((dim < 0) || (dim > 2)) return;
  if ((coord < 0) || (coord >= Resolution(dim))) return;

  // Get useful variables
  RNDimension dim1 = (dim+1) % 3;
  RNDimension dim2 = (dim+2) % 3;
  int width = Resolution(dim1);
  int height = Resolution(dim2);
  const int max_resolution = 1024;
  if (width > max_resolution) width = max_resolution;
  if (height > max_resolution) height = max_resolution;

  // Define slice texture
  static const R3Grid *previous_grid[3] = { NULL, NULL, NULL };
  static int previous_coord[3] = { -1, -1, -1 };
  static GLuint texture_id[3] = { 0, 0, 0 };
  if ((this != previous_grid[dim]) || (coord != previous_coord[dim])) {
    if (texture_id[dim] != 0) glDeleteTextures(1, &texture_id[dim]);
    glGenTextures(1, &texture_id[dim]);
    glBindTexture(GL_TEXTURE_2D, texture_id[dim]);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    static GLfloat pixels[max_resolution * max_resolution];
    RNInterval range = Range();
    if (range.Diameter() <= 0) range = RNunit_interval;
    GLfloat *pixelsp = pixels;
    for (int j = 0; j < height; j++) {
      for (int i = 0; i < width; i++) {
        RNCoord p[3];
        p[dim] = coord;
        p[dim1] = (i+0.5) * Resolution(dim1) / width;
        p[dim2] = (j+0.5) * Resolution(dim2) / height;
        RNScalar value = GridValue(p[0], p[1], p[2]);
        *(pixelsp++) = (GLfloat) ((value - range.Min()) / range.Diameter());
      }
    }
    glTexImage2D(GL_TEXTURE_2D, 0, 1, width, height, 0, GL_LUMINANCE, GL_FLOAT, pixels);
    previous_coord[dim] = coord;
    previous_grid[dim] = this;
  }

  // Set OpenGL modes
  assert(texture_id[dim]);
  glBindTexture(GL_TEXTURE_2D, texture_id[dim]);
  glEnable(GL_TEXTURE_2D);

  // Create quad
  R3Point p0, p1, p2, p3;
  p0[dim] = coord; p0[dim1] = 0;                  p0[dim2] = 0;
  p1[dim] = coord; p1[dim1] = Resolution(dim1)-1; p1[dim2] = 0;
  p2[dim] = coord; p2[dim1] = Resolution(dim1)-1; p2[dim2] = Resolution(dim2)-1;
  p3[dim] = coord; p3[dim1] = 0;                  p3[dim2] = Resolution(dim2)-1;

  // Draw quad 
  RNScalar one = 1.0;
  RNScalar zero = 0;
  R3BeginPolygon();
  R3LoadTextureCoords(zero, zero);
  R3LoadPoint(p0[0], p0[1], p0[2]);
  R3LoadTextureCoords(one, zero);
  R3LoadPoint(p1[0], p1[1], p1[2]);
  R3LoadTextureCoords(one, one);
  R3LoadPoint(p2[0], p2[1], p2[2]);
  R3LoadTextureCoords(zero, one);
  R3LoadPoint(p3[0], p3[1], p3[2]);
  R3EndPolygon();

  // Reset OpenGL modes
  glDisable(GL_TEXTURE_2D);
}

// Assembly:
cmpl $0x3, %esi
setae %al
testl %edx, %edx
sets %cl
orb %al, %cl
jne 0x3b509
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
movl %esi, %r13d
cmpl %edx, 0x238(%rdi,%r13,4)
jle 0x3b4f8
leal 0x1(%rsi), %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rcx, %rax
shrq $0x21, %rax
leal (%rax,%rax,2), %eax
negl %eax
leal (%rsi,%rax), %ebp
incl %ebp
movl %esi, %eax
addl $0x2, %eax
imulq %rcx, %rax
shrq $0x21, %rax
leal (%rax,%rax,2), %eax
negl %eax
leal (%rsi,%rax), %r15d
addl $0x2, %r15d
movl 0x238(%rbx,%rbp,4), %esi
movl 0x238(%rbx,%r15,4), %r14d
movl $0x400, %eax            # imm = 0x400
cmpl %eax, %esi
movl $0x400, %ecx            # imm = 0x400
movl %esi, 0x18(%rsp)
cmovll %esi, %ecx
movl %ecx, 0xc(%rsp)
cmpl %eax, %r14d
cmovll %r14d, %eax
movl %eax, 0x10(%rsp)
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0xab5d2(%rip), %rax     # 0xe6660
leaq 0xaa973(%rip), %rcx     # 0xe5a08
cmpq %rbx, (%rax,%r13,8)
jne 0x3b0a5
cmpl %edx, (%rcx,%r13,4)
je 0x3b366
movl %edx, 0x14(%rsp)
leaq 0xab5c8(%rip), %rax     # 0xe6678
leaq (%rax,%r13,4), %r12
cmpl $0x0, (%r12)
je 0x3b0c8
movl $0x1, %edi
movq %r12, %rsi
callq 0xe600
movq %r13, 0x38(%rsp)
movl $0x1, %edi
movq %r12, %rsi
callq 0xe630
movl (%r12), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe570
movl $0x2300, %edi           # imm = 0x2300
movl $0x2200, %esi           # imm = 0x2200
movl $0x2100, %edx           # imm = 0x2100
callq 0xe4e0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl $0x2900, %edx           # imm = 0x2900
callq 0xe610
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl $0x2900, %edx           # imm = 0x2900
callq 0xe610
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2601, %edx           # imm = 0x2601
callq 0xe610
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2601, %edx           # imm = 0x2601
callq 0xe610
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2f2c8
movsd 0x8(%r12), %xmm0
subsd (%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jb 0x3b184
leaq 0x4ad5e6(%rip), %rax    # 0x4e8760
movupd (%rax), %xmm0
movapd %xmm0, 0x50(%rsp)
testl %r14d, %r14d
jle 0x3b310
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sdl 0x10(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x50(%rsp), %xmm2
movsd 0x58(%rsp), %xmm3
subsd %xmm2, %xmm3
xorl %eax, %eax
xorpd %xmm0, %xmm0
leaq 0xab4ce(%rip), %r14     # 0xe6690
movsd 0x66e66(%rip), %xmm4    # 0xa2030
movsd 0x66e66(%rip), %xmm5    # 0xa2038
movsd %xmm1, 0x90(%rsp)
movsd %xmm2, 0x88(%rsp)
movsd %xmm3, 0x80(%rsp)
movq 0x38(%rsp), %r12
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x18(%rsp)
movsd %xmm0, 0x78(%rsp)
jle 0x3b2f6
xorps %xmm5, %xmm5
cvtsi2sdl 0x238(%rbx,%rbp,4), %xmm5
movsd %xmm5, 0xa0(%rsp)
addsd %xmm4, %xmm0
xorps %xmm5, %xmm5
cvtsi2sdl 0x238(%rbx,%r15,4), %xmm5
mulsd %xmm0, %xmm5
divsd 0x70(%rsp), %xmm5
movsd %xmm5, 0x98(%rsp)
xorpd %xmm0, %xmm0
movl 0xc(%rsp), %r13d
movsd %xmm0, 0x48(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp,%r12,8)
movsd 0x48(%rsp), %xmm0
addsd %xmm4, %xmm0
mulsd 0xa0(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp,%rbp,8)
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp,%r15,8)
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq %rbx, %rdi
callq 0x2f3bc
movsd 0x66d8d(%rip), %xmm5    # 0xa2038
movsd 0x66d7d(%rip), %xmm4    # 0xa2030
movsd 0x80(%rsp), %xmm3
movsd 0x88(%rsp), %xmm2
movsd 0x90(%rsp), %xmm1
subsd %xmm2, %xmm0
divsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14)
movsd 0x48(%rsp), %xmm0
addq $0x4, %r14
addsd %xmm5, %xmm0
decl %r13d
jne 0x3b249
movsd 0x78(%rsp), %xmm0
addsd %xmm5, %xmm0
movl 0x1c(%rsp), %eax
incl %eax
cmpl 0x10(%rsp), %eax
jne 0x3b1f2
subq $0x8, %rsp
leaq 0xab375(%rip), %rax     # 0xe6690
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1, %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x1406                 # imm = 0x1406
pushq $0x1909                 # imm = 0x1909
callq 0xe1b0
addq $0x20, %rsp
movq 0x38(%rsp), %r13
movl 0x14(%rsp), %eax
leaq 0xaa6b1(%rip), %rcx     # 0xe5a08
movl %eax, (%rcx,%r13,4)
leaq 0xab2fe(%rip), %rax     # 0xe6660
movq %rbx, (%rax,%r13,8)
leaq 0xab30b(%rip), %rax     # 0xe6678
movl (%rax,%r13,4), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe570
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe3e0
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x3ca24
leaq 0x50(%rsp), %r12
movq %r12, %rdi
callq 0x3ca24
leaq 0xc0(%rsp), %rdi
callq 0x3ca24
leaq 0xa8(%rsp), %rdi
callq 0x3ca24
movsd 0x40(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp,%r13,8)
xorl %eax, %eax
movq %rax, 0x20(%rsp,%rbp,8)
movq %rax, 0x20(%rsp,%r15,8)
movsd %xmm1, 0x50(%rsp,%r13,8)
movl 0x238(%rbx,%rbp,4), %ecx
decl %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x50(%rsp,%rbp,8)
movq %rax, 0x50(%rsp,%r15,8)
movsd %xmm1, 0xc0(%rsp,%r13,8)
movsd %xmm0, 0xc0(%rsp,%rbp,8)
movl 0x238(%rbx,%r15,4), %ecx
decl %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0xc0(%rsp,%r15,8)
movsd %xmm1, 0xa8(%rsp,%r13,8)
movq %rax, 0xa8(%rsp,%rbp,8)
movsd %xmm0, 0xa8(%rsp,%r15,8)
movl $0x9, %edi
callq 0xe110
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
callq 0xe8d0
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
callq 0xe6d0
movsd 0x66bc6(%rip), %xmm0    # 0xa2038
xorps %xmm1, %xmm1
callq 0xe8d0
movsd (%r12), %xmm0
movsd 0x8(%r12), %xmm1
movsd 0x10(%r12), %xmm2
callq 0xe6d0
movsd 0x66b9d(%rip), %xmm0    # 0xa2038
movaps %xmm0, %xmm1
callq 0xe8d0
leaq 0xc0(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm2
callq 0xe6d0
xorps %xmm0, %xmm0
movsd 0x66b6f(%rip), %xmm1    # 0xa2038
callq 0xe8d0
leaq 0xa8(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm2
callq 0xe6d0
callq 0xe5d0
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe140
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop