// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: RasterizeIntoZXGrid(gaps::R2Grid&, gaps::R3SceneNode*, gaps::R3Box const&)
static void
RasterizeIntoZXGrid(R2Grid& grid, R3SceneNode *node,
  const R3Box& world_bbox)
{
  // Check bounding box
  R3Box node_bbox = node->BBox();
  if (!R3Intersects(world_bbox, node_bbox)) return;
  
  // Rasterize elements into grid
  for (int j = 0; j < node->NElements(); j++) {
    R3SceneElement *element = node->Element(j);
    for (int k = 0; k < element->NShapes(); k++) {
      R3Shape *shape = element->Shape(k);
      R3Box shape_bbox = shape->BBox();
      if (!R3Intersects(world_bbox, shape_bbox)) continue;
      if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
        R3TriangleArray *triangles = (R3TriangleArray *) shape;
        for (int m = 0; m < triangles->NTriangles(); m++) {
          R3Triangle *triangle = triangles->Triangle(m);
          R3Box triangle_bbox = triangle->BBox();
          if (!R3Intersects(world_bbox, triangle_bbox)) continue;
          R3TriangleVertex *v0 = triangle->V0();
          R3Point vp0 = v0->Position();
          R2Point p0(vp0.Z(), vp0.X());
          if (!R2Contains(grid.WorldBox(), p0)) continue;
          R3TriangleVertex *v1 = triangle->V1();
          R3Point vp1 = v1->Position();
          R2Point p1(vp1.Z(), vp1.X());
          if (!R2Contains(grid.WorldBox(), p1)) continue;
          R3TriangleVertex *v2 = triangle->V2();
          R3Point vp2 = v2->Position();
          R2Point p2(vp2.Z(), vp2.X());
          if (!R2Contains(grid.WorldBox(), p2)) continue;
          grid.RasterizeWorldTriangle(p0, p1, p2, 1.0);
        }
      }
    }
  }

  // Rasterize children into grid
  for (int j = 0; j < node->NChildren(); j++) {
    R3SceneNode *child = node->Child(j);
    RasterizeIntoZXGrid(grid, child, world_bbox);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, (%rsp)
movq %rsi, %rdi
callq 0x3d966
leaq 0x108(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6af06
movq %r14, 0x8(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4a7a4
testl %eax, %eax
je 0x2af54
movq %r15, 0x10(%rsp)
xorl %ecx, %ecx
leaq 0xd0(%rsp), %rbx
leaq 0x140(%rsp), %r13
leaq 0x98(%rsp), %r12
movq 0x10(%rsp), %r14
movslq 0x3c(%r14), %rax
cmpq %rax, %rcx
jge 0x2af2e
movq 0x30(%r14), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %r14
xorl %r15d, %r15d
movq %r14, 0x20(%rsp)
movslq 0x1c(%r14), %rax
cmpq %rax, %r15
jge 0x2af21
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rbp
movq (%rbp), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x88(%rax)
movq %r15, 0x28(%rsp)
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4a7a4
testl %eax, %eax
je 0x2aeff
movq (%rbp), %rax
movq %rbp, %rdi
callq *0xb8(%rax)
movl %eax, %r14d
callq 0x6626c
cmpl %eax, %r14d
jne 0x2aeff
xorl %r15d, %r15d
movslq 0x24(%rbp), %rax
cmpq %rax, %r15
jge 0x2aeff
movq 0x18(%rbp), %rax
movq (%rax,%r15,8), %r14
movq (%r14), %rax
movq %r13, %rdi
movq %r14, %rsi
callq *0x88(%rax)
movq 0x8(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4a7a4
testl %eax, %eax
je 0x2aeef
movq 0x8(%r14), %rsi
leaq 0x80(%rsp), %rdi
callq 0x72a70
movsd 0x80(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
callq 0x8accc
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x2c35c
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
callq 0x7b410
movl %eax, %ebx
movq %r12, %rdi
callq 0x88a2e
testl %ebx, %ebx
je 0x2aeef
movq 0x10(%r14), %rsi
leaq 0x68(%rsp), %rdi
callq 0x72a70
movsd 0x68(%rsp), %xmm1
movsd 0x78(%rsp), %xmm0
leaq 0x30(%rsp), %rdi
callq 0x8accc
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x2c35c
movq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x7b410
movl %eax, %ebx
movq %r12, %rdi
callq 0x88a2e
testl %ebx, %ebx
je 0x2aeef
movq 0x18(%r14), %rsi
leaq 0x50(%rsp), %rdi
callq 0x72a70
movsd 0x50(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
callq 0x8accc
movq %r12, %rdi
movq (%rsp), %rsi
callq 0x2c35c
movq %r12, %rdi
movq %r14, %rsi
callq 0x7b410
movl %eax, %ebx
movq %r12, %rdi
callq 0x88a2e
testl %ebx, %ebx
je 0x2aeef
movq (%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r14, %rcx
movsd 0xaf199(%rip), %xmm0    # 0xda080
xorl %r8d, %r8d
callq 0x2c3ec
movq %r13, %rdi
callq 0x6bbbe
incq %r15
jmp 0x2ada1
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
callq 0x6bbbe
movq 0x28(%rsp), %r15
incq %r15
movq 0x20(%rsp), %r14
jmp 0x2ad3f
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0x2ad18
xorl %ebx, %ebx
movslq 0x2c(%r14), %rax
cmpq %rax, %rbx
jge 0x2af54
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0x2aca9
incq %rbx
jmp 0x2af30
leaq 0x108(%rsp), %rdi
callq 0x6bbbe
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2af77
jmp 0x2af77
movq %rax, %rbx
jmp 0x2afc3
jmp 0x2afd8
jmp 0x2afa6
jmp 0x2af90
jmp 0x2afa6
jmp 0x2afa6
jmp 0x2afa6
jmp 0x2af90
jmp 0x2afa6
jmp 0x2afa6
jmp 0x2afa6
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x88a2e
jmp 0x2afa9
jmp 0x2afa6
jmp 0x2afa6
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x6bbbe
leaq 0xd0(%rsp), %rdi
callq 0x6bbbe
leaq 0x108(%rsp), %rdi
callq 0x6bbbe
movq %rbx, %rdi
callq 0x230b0
movq %rax, %rbx
jmp 0x2afb6