// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/apps/scn2scn/scn2scn.cpp
// Function: CountOverlapAreas(gaps::R3SceneNode*, gaps::R3Grid*, gaps::R3Affine const&, double&, double&)
static void
CountOverlapAreas(R3SceneNode *node, R3Grid *grid, const R3Affine& parent_transformation, RNArea& overlap_area, RNArea& total_area)
{
  // Update transformation
  R3Affine transformation = R3identity_affine;
  transformation.Transform(parent_transformation);
  transformation.Transform(node->Transformation());

  // Count overlapping triangle areas in references
  for (int i = 0; i < node->NReferences(); i++) {
    R3SceneReference *reference = node->Reference(i);
    R3Scene *referenced_scene = reference->ReferencedScene();
    CountOverlapAreas(referenced_scene->Root(), grid, transformation, overlap_area, total_area);
  }

  // Count overlapping triangle areas in elements
  for (int i = 0; i < node->NElements(); i++) {
    R3SceneElement *element = node->Element(i);
    for (int j = 0; j < element->NShapes(); j++) {
      R3Shape *shape = element->Shape(j);
      if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
        R3TriangleArray *triangles = (R3TriangleArray *) shape;
        for (int k = 0; k < triangles->NTriangles(); k++) {
          R3Triangle *triangle = triangles->Triangle(k);
          R3Point centroid = triangle->Centroid();
          centroid.Transform(transformation);
          RNScalar grid_value = grid->WorldValue(centroid);
          RNArea area = triangle->Area();
          if (grid_value > RN_EPSILON) overlap_area += area;
          total_area += area;
        }
      }
    }
  }
  
  // Count overlapping triangle areas in children
  for (int i = 0; i < node->NChildren(); i++) {
    R3SceneNode *child = node->Child(i);
    CountOverlapAreas(child, grid, transformation, overlap_area, total_area);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r8, %rbx
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbp
leaq 0x13b70a(%rip), %rax    # 0x162ec8
addq $0x10, %rax
leaq 0x70(%rsp), %rdi
movq %rax, -0x8(%rdi)
leaq 0x147e1e(%rip), %r14    # 0x16f5f0
leaq 0x8(%r14), %rsi
callq 0x72326
movl $0x88, %esi
leaq 0x68(%rsp), %rdi
addq %rsi, %rdi
addq 0x13f7f1(%rip), %rsi    # 0x166fe0
callq 0x72326
movq 0x108(%r14), %rax
leaq 0x68(%rsp), %rdi
movq %rax, 0x108(%rdi)
movq %r12, %rsi
callq 0x70156
leaq 0x58(%rbp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x70156
cmpl $0x0, 0x4c(%rbp)
jle 0x27858
xorl %r14d, %r14d
leaq 0x68(%rsp), %r12
movq 0x40(%rbp), %rax
movq (%rax,%r14,8), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x27795
incq %r14
movslq 0x4c(%rbp), %rax
cmpq %rax, %r14
jl 0x2782b
cmpl $0x0, 0x3c(%rbp)
jle 0x279a0
xorl %ecx, %ecx
leaq 0x38(%rsp), %r13
movq %rbp, 0x10(%rsp)
movq 0x30(%rbp), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %r14
cmpl $0x0, 0x1c(%r14)
jle 0x27986
xorl %ecx, %ecx
movq %r14, 0x20(%rsp)
movq 0x10(%r14), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rbp
movq (%rbp), %rax
movq %rbp, %rdi
callq *0xb8(%rax)
movl %eax, %r12d
callq 0x67dec
cmpl %eax, %r12d
jne 0x2796c
cmpl $0x0, 0x24(%rbp)
jle 0x2796c
xorl %r14d, %r14d
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %r12
movq (%r12), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x68(%rax)
movq %r13, %rdi
leaq 0x68(%rsp), %rsi
callq 0x83106
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x75726
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movq %r15, %rdi
callq 0x75128
movsd %xmm0, 0x30(%rsp)
movq (%r12), %rax
movq %r12, %rdi
callq *0x58(%rax)
leaq 0x140731(%rip), %rax    # 0x168068
movsd 0x30(%rsp), %xmm1
ucomisd (%rax), %xmm1
jbe 0x27954
movq 0x8(%rsp), %rax
movsd (%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
addsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
incq %r14
movslq 0x24(%rbp), %rax
cmpq %rax, %r14
jl 0x278c5
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %r14
movslq 0x1c(%r14), %rax
cmpq %rax, %rcx
jl 0x2788d
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x10(%rsp), %rbp
movslq 0x3c(%rbp), %rax
cmpq %rax, %rcx
jl 0x2786e
cmpl $0x0, 0x2c(%rbp)
jle 0x279d5
xorl %r14d, %r14d
leaq 0x68(%rsp), %r12
movq 0x20(%rbp), %rax
movq (%rax,%r14,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
movq %rbx, %r8
callq 0x27795
incq %r14
movslq 0x2c(%rbp), %rax
cmpq %rax, %r14
jl 0x279ae
leaq 0x68(%rsp), %rdi
callq 0x6ffe8
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27a0c
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x70670
jmp 0x27a19
jmp 0x27a0c
jmp 0x27a0c
jmp 0x27a0c
jmp 0x27a0c
jmp 0x27a0c
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x6ffe8
movq %rbx, %rdi
callq 0x23060
nop