// File: /workspace/github_cpp_makefile_star2_O3/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 0x6f814
movl 0xc(%rbx), %eax
testl %eax, %eax
je 0x52768
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 0x6fa2c
movq 0x8(%r12), %r15
movq 0x10(%r12), %r14
movq 0x18(%r12), %rbp
movq %r15, %rdi
movq %r14, %rsi
callq 0x493db
movsd %xmm0, 0x30(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x493db
movsd %xmm0, 0x38(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x493db
movsd 0x30(%rsp), %xmm1
ucomisd 0x18(%rsp), %xmm1
jbe 0x5249b
ucomisd 0x38(%rsp), %xmm1
jb 0x5249b
ucomisd %xmm0, %xmm1
jb 0x5249b
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3cf30
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x4fcb6(%rip), %xmm0    # 0xa2030
callq 0x3cf76
leaq 0x18(%r15), %rsi
leaq 0x18(%r14), %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3dc20
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x4fc8e(%rip), %xmm0    # 0xa2030
callq 0x3d962
movq %r13, %rdi
callq 0x3d2dc
movl $0x68, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x534ee
testb $0x2, 0x59(%r15)
je 0x52418
testb $0x2, 0x59(%r14)
je 0x52418
leaq 0x48(%r15), %rsi
leaq 0x48(%r14), %rdx
leaq 0x40(%rsp), %rdi
callq 0x682bb
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movsd 0x4fc32(%rip), %xmm0    # 0xa2030
callq 0x6834d
movq %rbx, %rdi
addq $0x48, %rdi
leaq 0x50(%rsp), %rsi
callq 0x681de
orb $0x2, 0x59(%rbx)
movq (%rsp), %rax
movl 0x14(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x6f8a2
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 0x6f8a2
movl $0x88, %edi
callq 0xe440
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x539b8
movq (%rsp), %rax
movl 0x24(%rax), %edx
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x6f8a2
jmp 0x5275c
movsd 0x38(%rsp), %xmm1
ucomisd 0x18(%rsp), %xmm1
jbe 0x52601
ucomisd 0x30(%rsp), %xmm1
jb 0x52601
ucomisd %xmm0, %xmm1
jb 0x52601
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3cf30
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x4fb4f(%rip), %xmm0    # 0xa2030
callq 0x3cf76
leaq 0x18(%r14), %rsi
leaq 0x18(%rbp), %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3dc20
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x4fb27(%rip), %xmm0    # 0xa2030
callq 0x3d962
movq %r13, %rdi
callq 0x3d2dc
movl $0x68, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x534ee
testb $0x2, 0x59(%r14)
je 0x5257e
testb $0x2, 0x59(%rbp)
je 0x5257e
leaq 0x48(%r14), %rsi
leaq 0x48(%rbp), %rdx
leaq 0x40(%rsp), %rdi
callq 0x682bb
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movsd 0x4facc(%rip), %xmm0    # 0xa2030
callq 0x6834d
movq %rbx, %rdi
addq $0x48, %rdi
leaq 0x50(%rsp), %rsi
callq 0x681de
orb $0x2, 0x59(%rbx)
movq (%rsp), %rax
movl 0x14(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x6f8a2
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 0x6f8a2
movl $0x88, %edi
callq 0xe440
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x539b8
movq (%rsp), %rax
movl 0x24(%rax), %edx
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
jmp 0x5275c
ucomisd %xmm1, %xmm0
jb 0x5275c
ucomisd 0x18(%rsp), %xmm0
jbe 0x5275c
ucomisd 0x30(%rsp), %xmm0
jb 0x5275c
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x3cf30
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x4f9ef(%rip), %xmm0    # 0xa2030
callq 0x3cf76
leaq 0x18(%rbp), %rsi
leaq 0x18(%r15), %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3dc20
movq %r13, %rdi
movq %rbx, %rsi
movsd 0x4f9c7(%rip), %xmm0    # 0xa2030
callq 0x3d962
movq %r13, %rdi
callq 0x3d2dc
movl $0x68, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x534ee
testb $0x2, 0x59(%rbp)
je 0x526de
testb $0x2, 0x59(%r15)
je 0x526de
leaq 0x48(%rbp), %rsi
leaq 0x48(%r15), %rdx
leaq 0x40(%rsp), %rdi
callq 0x682bb
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movsd 0x4f96c(%rip), %xmm0    # 0xa2030
callq 0x6834d
movq %rbx, %rdi
addq $0x48, %rdi
leaq 0x50(%rsp), %rsi
callq 0x681de
orb $0x2, 0x59(%rbx)
movq (%rsp), %rax
movl 0x14(%rax), %edx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x6f8a2
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 0x6f8a2
movl $0x88, %edi
callq 0xe440
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x539b8
movq (%rsp), %rax
movl 0x24(%rax), %edx
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0x522df
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
leaq 0x8(%rsp), %rdi
callq 0x6f876
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x527ef
jmp 0x527ef
jmp 0x527a5
jmp 0x527b8
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527b8
jmp 0x527ef
movq %rax, %r14
movl $0x88, %esi
movq %r12, %rdi
jmp 0x527d0
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
movq %rax, %r14
movl $0x68, %esi
movq %rbx, %rdi
jmp 0x527d0
movq %rax, %r14
movl $0x88, %esi
movq %r15, %rdi
callq 0xe450
jmp 0x527f2
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
jmp 0x527ef
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x6f876
movq %r14, %rdi
callq 0xe7e0