// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %rbx
mulsd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x18(%rdi), %rsi
movq %rsp, %rdi
movq %rsi, 0x10(%rsp)
callq 0x66312
movq %rbx, 0x60(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x80(%rsp), %r13
movslq 0xc(%rsp), %rax
testq %rax, %rax
je 0x4b8f7
movq (%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rbx
movq %rsp, %rdi
callq 0x21340
movq 0x8(%rbx), %rbp
movq 0x10(%rbx), %r12
movq 0x18(%rbx), %r14
movq %rbp, %rdi
movq %r12, %rsi
callq 0x431f1
movsd %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x431f1
movsd %xmm0, 0x30(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x431f1
movsd 0x28(%rsp), %xmm1
ucomisd 0x20(%rsp), %xmm1
jbe 0x4b677
ucomisd 0x30(%rsp), %xmm1
jb 0x4b677
ucomisd %xmm0, %xmm1
jb 0x4b677
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3827a
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x47a97(%rip), %xmm0    # 0x93010
callq 0x382c0
leaq 0x18(%rbp), %rsi
leaq 0x18(%r12), %rdx
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x38c92
movq %r13, %rdi
movq %r15, %rsi
movsd 0x47a6e(%rip), %xmm0    # 0x93010
callq 0x38a02
movq %r13, %rdi
callq 0x3860e
pushq $0x68
popq %rdi
callq 0xe450
movq %rax, %r15
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x4c506
testb $0x2, 0x59(%rbp)
je 0x4b60f
testb $0x2, 0x59(%r12)
je 0x4b60f
leaq 0x48(%rbp), %rsi
leaq 0x48(%r12), %rdx
leaq 0x38(%rsp), %rdi
callq 0x5f7a1
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movsd 0x47a13(%rip), %xmm0    # 0x93010
callq 0x5f833
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x4c398
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq *0x230(%rax)
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x22bfc
movl $0x88, %edi
callq 0xe450
movq %rax, %rbp
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x4c910
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x22bfc
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x22bfc
jmp 0x4b4db
movsd 0x30(%rsp), %xmm1
ucomisd 0x20(%rsp), %xmm1
jbe 0x4b7bc
ucomisd 0x28(%rsp), %xmm1
jb 0x4b7bc
ucomisd %xmm0, %xmm1
jb 0x4b7bc
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x3827a
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x47953(%rip), %xmm0    # 0x93010
callq 0x382c0
leaq 0x18(%r12), %rsi
leaq 0x18(%r14), %rdx
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x38c92
movq %r13, %rdi
movq %r15, %rsi
movsd 0x4792a(%rip), %xmm0    # 0x93010
callq 0x38a02
movq %r13, %rdi
callq 0x3860e
pushq $0x68
popq %rdi
callq 0xe450
movq %rax, %r15
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x4c506
testb $0x2, 0x59(%r12)
je 0x4b754
testb $0x2, 0x59(%r14)
je 0x4b754
leaq 0x48(%r12), %rsi
leaq 0x48(%r14), %rdx
leaq 0x38(%rsp), %rdi
callq 0x5f7a1
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movsd 0x478ce(%rip), %xmm0    # 0x93010
callq 0x5f833
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x4c398
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq *0x230(%rax)
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x22bfc
movl $0x88, %edi
callq 0xe450
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x4c910
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x22bfc
movq %rsp, %rdi
movq %r12, %rsi
callq 0x22bfc
jmp 0x4b4db
ucomisd %xmm1, %xmm0
jb 0x4b4db
ucomisd 0x20(%rsp), %xmm0
jbe 0x4b4db
ucomisd 0x28(%rsp), %xmm0
jb 0x4b4db
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3827a
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
movsd 0x47814(%rip), %xmm0    # 0x93010
callq 0x382c0
leaq 0x18(%r14), %rsi
leaq 0x18(%rbp), %rdx
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x38c92
movq %r13, %rdi
movq %r15, %rsi
movsd 0x477ec(%rip), %xmm0    # 0x93010
callq 0x38a02
movq %r13, %rdi
callq 0x3860e
pushq $0x68
popq %rdi
callq 0xe450
movq %rax, %r15
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x4c506
testb $0x2, 0x59(%r14)
je 0x4b88f
testb $0x2, 0x59(%rbp)
je 0x4b88f
leaq 0x48(%r14), %rsi
leaq 0x48(%rbp), %rdx
leaq 0x38(%rsp), %rdi
callq 0x5f7a1
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movsd 0x47793(%rip), %xmm0    # 0x93010
callq 0x5f833
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x4c398
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq *0x230(%rax)
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x22bfc
movl $0x88, %edi
callq 0xe450
movq %rax, %r14
movq %rax, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x4c910
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x22bfc
movq %rsp, %rdi
movq %r14, %rsi
callq 0x22bfc
jmp 0x4b4db
movq 0x60(%rsp), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
movq %rsp, %rdi
callq 0x66378
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4b986
jmp 0x4b986
movq %rax, %rbx
movl $0x88, %esi
movq %r14, %rdi
jmp 0x4b969
jmp 0x4b951
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b951
jmp 0x4b986
movq %rax, %rbx
movl $0x88, %esi
movq %r12, %rdi
jmp 0x4b969
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r15, %rdi
jmp 0x4b969
jmp 0x4b986
movq %rax, %rbx
movl $0x88, %esi
movq %rbp, %rdi
callq 0xe460
jmp 0x4b989
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
jmp 0x4b986
movq %rax, %rbx
movq %rsp, %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810
nop