// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3TriangleArray.cpp
// Function: gaps::R3TriangleArray::Subdivide(double)
void R3TriangleArray::
Subdivide(RNLength max_edge_length) 
{
  // Get convenient variables
  RNLength max_edge_length_squared = max_edge_length * max_edge_length;
  
  // Subdivide triangles with edges that are too long
  RNArray<R3Triangle *> stack = triangles;
  while (!stack.IsEmpty()) {
    // Get triangle from stack
    R3Triangle *triangle = stack.Tail();
    stack.RemoveTail();

    // Get vertices
    R3TriangleVertex *v0 = triangle->V0();
    R3TriangleVertex *v1 = triangle->V1();
    R3TriangleVertex *v2 = triangle->V2();
    const R3Point& p0 = v0->Position();
    const R3Point& p1 = v1->Position();
    const R3Point& p2 = v2->Position();
    RNLength dd01 = R3SquaredDistance(p0, p1);
    RNLength dd12 = R3SquaredDistance(p1, p2);
    RNLength dd20 = R3SquaredDistance(p2, p0);

    // Check if edge is too long
    if ((dd01 > max_edge_length_squared) && (dd01 >= dd12) && (dd01 >= dd20)) {
      // Create new vertex at midpoint of edge between v0 and v1
      R3Point position = 0.5*(p0 + p1);
      R3Vector normal = 0.5*(v0->Normal() + v1->Normal()); normal.Normalize();
      R3TriangleVertex *v = new R3TriangleVertex(position, normal);
      if (v0->flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG] && v1->flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG])
        v->SetTextureCoords(0.5*(v0->TextureCoords() + v1->TextureCoords()));
      vertices.Insert(v);

      // Adjust existing triangle
      triangle->Reset(v0, v, v2);
      stack.Insert(triangle);

      // Create new triangle
      R3Triangle *t = new R3Triangle(v1, v2, v);
      triangles.Insert(t);
      stack.Insert(t);
    }
    else if ((dd12 > max_edge_length_squared) && (dd12 >= dd01) && (dd12 >= dd20)) {
      // Create new vertex at midpoint of edge between v1 and v2
      R3Point position = 0.5*(p1 + p2);
      R3Vector normal = 0.5*(v1->Normal() + v2->Normal()); normal.Normalize();
      R3TriangleVertex *v = new R3TriangleVertex(position, normal);
      if (v1->flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG] && v2->flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG])
        v->SetTextureCoords(0.5*(v1->TextureCoords() + v2->TextureCoords()));
      vertices.Insert(v);

      // Adjust existing triangle
      triangle->Reset(v1, v, v0);
      stack.Insert(triangle);

      // Create new triangle
      R3Triangle *t = new R3Triangle(v2, v0, v);
      triangles.Insert(t);
      stack.Insert(t);
    }
    else if ((dd20 > max_edge_length_squared) && (dd20 >= dd01) && (dd20 >= dd12)) {
      // Create new vertex at midpoint of edge between v2 and v0
      R3Point position = 0.5*(p2 + p0);
      R3Vector normal = 0.5*(v2->Normal() + v0->Normal()); normal.Normalize();
      R3TriangleVertex *v = new R3TriangleVertex(position, normal);
      if (v2->flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG] && v0->flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG])
        v->SetTextureCoords(0.5*(v2->TextureCoords() + v0->TextureCoords()));
      vertices.Insert(v);

      // Adjust existing triangle
      triangle->Reset(v2, v, v1);
      stack.Insert(triangle);

      // Create new triangle
      R3Triangle *t = new R3Triangle(v0, v1, v);
      triangles.Insert(t);
      stack.Insert(t);
    }
  }

  // Update
  Update();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movsd %xmm0, 0x18(%rsp)
movq %rdi, (%rsp)
leaq 0x18(%rdi), %rsi
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rsi, 0x28(%rsp)
callq 0x705f4
movl 0xc(%rbx), %eax
testl %eax, %eax
je 0x533fa
movsd 0x18(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq (%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x20(%rsp)
leaq 0x80(%rsp), %r13
movq 0x8(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r12
decl %esi
leaq 0x8(%rsp), %rdi
callq 0x707f6
movq 0x8(%r12), %r15
movq 0x10(%r12), %r14
movq 0x18(%r12), %rbp
movq %r15, %rdi
movq %r14, %rsi
callq 0x49ed3
movsd %xmm0, 0x30(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x49ed3
movsd %xmm0, 0x38(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x49ed3
movsd 0x30(%rsp), %xmm1
ucomisd 0x18(%rsp), %xmm1
jbe 0x5312d
ucomisd 0x38(%rsp), %xmm1
jb 0x5312d
ucomisd %xmm0, %xmm1
jb 0x5312d
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3d42a
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x4f024(%rip), %xmm0    # 0xa2030
callq 0x3d470
leaq 0x18(%r15), %rsi
leaq 0x18(%r14), %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3e174
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x4effc(%rip), %xmm0    # 0xa2030
callq 0x3de8e
movq %r13, %rdi
callq 0x3d7e0
movl $0x68, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x5415e
testb $0x2, 0x59(%r15)
je 0x530aa
testb $0x2, 0x59(%r14)
je 0x530aa
leaq 0x48(%r15), %rsi
leaq 0x48(%r14), %rdx
leaq 0x40(%rsp), %rdi
callq 0x69043
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movsd 0x4efa0(%rip), %xmm0    # 0xa2030
callq 0x690d5
movq %rbx, %rdi
addq $0x48, %rdi
leaq 0x50(%rsp), %rsi
callq 0x68f66
orb $0x2, 0x59(%rbx)
movq (%rsp), %rax
movl 0x14(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x70682
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq *0x230(%rax)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movl $0x88, %edi
callq 0xe440
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x54628
movq (%rsp), %rax
movl 0x24(%rax), %edx
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x70682
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x70682
jmp 0x533ee
movsd 0x38(%rsp), %xmm1
ucomisd 0x18(%rsp), %xmm1
jbe 0x53293
ucomisd 0x30(%rsp), %xmm1
jb 0x53293
ucomisd %xmm0, %xmm1
jb 0x53293
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3d42a
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x4eebd(%rip), %xmm0    # 0xa2030
callq 0x3d470
leaq 0x18(%r14), %rsi
leaq 0x18(%rbp), %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3e174
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x4ee95(%rip), %xmm0    # 0xa2030
callq 0x3de8e
movq %r13, %rdi
callq 0x3d7e0
movl $0x68, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x5415e
testb $0x2, 0x59(%r14)
je 0x53210
testb $0x2, 0x59(%rbp)
je 0x53210
leaq 0x48(%r14), %rsi
leaq 0x48(%rbp), %rdx
leaq 0x40(%rsp), %rdi
callq 0x69043
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movsd 0x4ee3a(%rip), %xmm0    # 0xa2030
callq 0x690d5
movq %rbx, %rdi
addq $0x48, %rdi
leaq 0x50(%rsp), %rsi
callq 0x68f66
orb $0x2, 0x59(%rbx)
movq (%rsp), %rax
movl 0x14(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x70682
movq (%r12), %rax
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq *0x230(%rax)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movl $0x88, %edi
callq 0xe440
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x54628
movq (%rsp), %rax
movl 0x24(%rax), %edx
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
jmp 0x533ee
ucomisd %xmm1, %xmm0
jb 0x533ee
ucomisd 0x18(%rsp), %xmm0
jbe 0x533ee
ucomisd 0x30(%rsp), %xmm0
jb 0x533ee
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x3d42a
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x4ed5d(%rip), %xmm0    # 0xa2030
callq 0x3d470
leaq 0x18(%rbp), %rsi
leaq 0x18(%r15), %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3e174
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x4ed35(%rip), %xmm0    # 0xa2030
callq 0x3de8e
movq %r13, %rdi
callq 0x3d7e0
movl $0x68, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x5415e
testb $0x2, 0x59(%rbp)
je 0x53370
testb $0x2, 0x59(%r15)
je 0x53370
leaq 0x48(%rbp), %rsi
leaq 0x48(%r15), %rdx
leaq 0x40(%rsp), %rdi
callq 0x69043
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movsd 0x4ecda(%rip), %xmm0    # 0xa2030
callq 0x690d5
movq %rbx, %rdi
addq $0x48, %rdi
leaq 0x50(%rsp), %rsi
callq 0x68f66
orb $0x2, 0x59(%rbx)
movq (%rsp), %rax
movl 0x14(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x70682
movq (%r12), %rax
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq *0x230(%rax)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movl $0x88, %edi
callq 0xe440
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x54628
movq (%rsp), %rax
movl 0x24(%rax), %edx
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x70682
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0x52f71
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
leaq 0x8(%rsp), %rdi
callq 0x70656
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x53481
jmp 0x53481
jmp 0x53437
jmp 0x5344a
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x5344a
jmp 0x53481
movq %rax, %r14
movl $0x88, %esi
movq %r12, %rdi
jmp 0x53462
jmp 0x53481
jmp 0x53481
jmp 0x53481
movq %rax, %r14
movl $0x68, %esi
movq %rbx, %rdi
jmp 0x53462
movq %rax, %r14
movl $0x88, %esi
movq %r15, %rdi
callq 0xe450
jmp 0x53484
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
jmp 0x53481
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x70656
movq %r14, %rdi
callq 0xe7e0