// File: /workspace/github_cpp_makefile_star2_O2/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 0x365b2
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %edx, %ebp
movq %rdi, %rbx
movl %esi, %r14d
cmpl %edx, 0x238(%rdi,%r14,4)
jle 0x365a1
xorl %eax, %eax
cmpl $0x2, %esi
setb %al
leal (%rax,%rax,2), %eax
leal (%rsi,%rax), %r13d
addl $-0x2, %r13d
subl $0x1, %esi
pushq $0x2
popq %r15
cmovael %esi, %r15d
movl 0x238(%rbx,%r13,4), %eax
movl $0x400, %ecx            # imm = 0x400
cmpl %ecx, %eax
cmovgel %ecx, %eax
movl %eax, 0xc(%rsp)
movl 0x238(%rbx,%r15,4), %eax
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, 0x8(%rsp)
leaq 0xa5514(%rip), %rax     # 0xdb670
leaq 0xa48b5(%rip), %rcx     # 0xdaa18
cmpq %rbx, (%rax,%r14,8)
jne 0x3617e
cmpl %ebp, (%rcx,%r14,4)
jne 0x3617e
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x36417
leaq 0xa5503(%rip), %rax     # 0xdb688
leaq (%rax,%r14,4), %r12
cmpl $0x0, (%r12)
je 0x3619b
pushq $0x1
popq %rdi
movq %r12, %rsi
callq 0xe620
pushq $0x1
popq %rdi
movq %r12, %rsi
callq 0xe650
movl (%r12), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe590
movl $0x2300, %edi           # imm = 0x2300
movl $0x2200, %esi           # imm = 0x2200
movl $0x2100, %edx           # imm = 0x2100
callq 0xe500
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl $0x2900, %edx           # imm = 0x2900
callq 0xe630
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl $0x2900, %edx           # imm = 0x2900
callq 0xe630
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2601, %edx           # imm = 0x2601
callq 0xe630
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2601, %edx           # imm = 0x2601
callq 0xe630
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2bd3a
movsd 0x8(%r12), %xmm0
subsd (%r12), %xmm0
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm0, %xmm2
jb 0x3625a
leaq 0x4a7520(%rip), %rax    # 0x4dd770
movupd (%rax), %xmm0
movapd %xmm0, 0x50(%rsp)
movl %ebp, 0x20(%rsp)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0xc(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x8(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x68(%rsp)
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
movl %esi, 0x24(%rsp)
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movl %eax, 0x28(%rsp)
leaq 0xa53f7(%rip), %rbp     # 0xdb6a0
cmpl 0x28(%rsp), %edx
je 0x363c8
movl %edx, 0x2c(%rsp)
movsd 0x10(%rsp), %xmm0
addsd 0x5cd4b(%rip), %xmm0    # 0x93010
movsd %xmm0, 0x88(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd %xmm0, 0x80(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x78(%rsp)
xorpd %xmm1, %xmm1
movl 0x24(%rsp), %r12d
subl $0x1, %r12d
jb 0x363a9
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp,%r14,8)
movapd %xmm1, %xmm0
movsd %xmm1, 0x90(%rsp)
cvtsi2sdl 0x238(%rbx,%r13,4), %xmm1
addsd 0x5cce4(%rip), %xmm0    # 0x93010
mulsd %xmm0, %xmm1
divsd 0x70(%rsp), %xmm1
movsd %xmm1, 0x30(%rsp,%r13,8)
cvtsi2sdl 0x238(%rbx,%r15,4), %xmm0
mulsd 0x88(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp,%r15,8)
movsd 0x30(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movq %rbx, %rdi
callq 0x2be20
movsd 0x90(%rsp), %xmm1
subsd 0x80(%rsp), %xmm0
divsd 0x78(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rbp)
addq $0x4, %rbp
addsd 0x5cc64(%rip), %xmm1    # 0x93008
jmp 0x362f6
movsd 0x10(%rsp), %xmm0
addsd 0x5cc51(%rip), %xmm0    # 0x93008
movsd %xmm0, 0x10(%rsp)
movl 0x2c(%rsp), %edx
incl %edx
jmp 0x362a9
subq $0x8, %rsp
leaq 0xa52cd(%rip), %rax     # 0xdb6a0
pushq $0x1
popq %rdx
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %r8d
xorl %r9d, %r9d
pushq %rax
pushq $0x1406                 # imm = 0x1406
pushq $0x1909                 # imm = 0x1909
callq 0xe1b0
addq $0x20, %rsp
movl 0x20(%rsp), %eax
leaq 0xa4610(%rip), %rcx     # 0xdaa18
movl %eax, (%rcx,%r14,4)
leaq 0xa525d(%rip), %rax     # 0xdb670
movq %rbx, (%rax,%r14,8)
leaq 0xa526a(%rip), %rax     # 0xdb688
movl (%rax,%r14,4), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe590
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe3f0
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0x37b4a
leaq 0x50(%rsp), %r12
movq %r12, %rdi
callq 0x37b4a
leaq 0xb0(%rsp), %rdi
callq 0x37b4a
leaq 0x98(%rsp), %rdi
callq 0x37b4a
movsd 0x18(%rsp), %xmm1
movsd %xmm1, 0x30(%rsp,%r14,8)
andq $0x0, 0x30(%rsp,%r13,8)
andq $0x0, 0x30(%rsp,%r15,8)
movsd %xmm1, 0x50(%rsp,%r14,8)
movl 0x238(%rbx,%r13,4), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x50(%rsp,%r13,8)
andq $0x0, 0x50(%rsp,%r15,8)
movsd %xmm1, 0xb0(%rsp,%r14,8)
movsd %xmm0, 0xb0(%rsp,%r13,8)
movl 0x238(%rbx,%r15,4), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xb0(%rsp,%r15,8)
movsd %xmm1, 0x98(%rsp,%r14,8)
andq $0x0, 0x98(%rsp,%r13,8)
movsd %xmm0, 0x98(%rsp,%r15,8)
callq 0x21c40
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
callq 0xe900
movsd (%rbp), %xmm0
movsd 0x8(%rbp), %xmm1
movsd 0x10(%rbp), %xmm2
callq 0xe700
movsd 0x5caed(%rip), %xmm0    # 0x93008
xorps %xmm1, %xmm1
callq 0xe900
movsd (%r12), %xmm0
movsd 0x8(%r12), %xmm1
movsd 0x10(%r12), %xmm2
callq 0xe700
movsd 0x5cac4(%rip), %xmm0    # 0x93008
movaps %xmm0, %xmm1
callq 0xe900
leaq 0xb0(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm2
callq 0xe700
xorps %xmm0, %xmm0
movsd 0x5ca96(%rip), %xmm1    # 0x93008
callq 0xe900
leaq 0x98(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm2
callq 0xe700
callq 0xe5f0
movl $0xde1, %edi            # imm = 0xDE1
callq 0xe140
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop