// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3TriangleArray.cpp
// Function: gaps::R3TriangleArray::LoadMesh(gaps::R3Mesh const&)
void R3TriangleArray::
LoadMesh(const R3Mesh& mesh)
{
  // Create vertices
  RNArray<R3TriangleVertex *> triangle_vertices;
  for (int i = 0; i < mesh.NVertices(); i++) {
    R3MeshVertex *mesh_vertex = mesh.Vertex(i);
    const R3Point& position = mesh.VertexPosition(mesh_vertex);
    R3TriangleVertex *triangle_vertex = new R3TriangleVertex(position);
    triangle_vertices.Insert(triangle_vertex);
    vertices.Insert(triangle_vertex);

    // Check if should assign vertex normal
    RNBoolean smooth = TRUE;
    const RNAngle max_smooth_angle = RN_PI/5.0;
    for (int j = 0; j < mesh.VertexValence(mesh_vertex); j++) {
      R3MeshEdge *mesh_edge = mesh.EdgeOnVertex(mesh_vertex, j);
      RNAngle angle = mesh.EdgeInteriorAngle(mesh_edge);
      if (fabs(angle - RN_PI) > max_smooth_angle) { smooth = FALSE; break; }
    }

    // Assign vertex normal
    if (smooth) {
      const R3Vector& normal = mesh.VertexNormal(mesh_vertex);
      triangle_vertex->SetNormal(normal);
    }
    
    // Assign vertex color
    if (!mesh.VertexColor(mesh_vertex).IsBlack()) {
      triangle_vertex->SetColor(mesh.VertexColor(mesh_vertex));
    }

    // Assign vertex texture coordinates
    triangle_vertex->SetTextureCoords(mesh.VertexTextureCoords(mesh_vertex));
  }

  // Create triangles
  for (int i = 0; i < mesh.NFaces(); i++) {
    R3MeshFace *mesh_face = mesh.Face(i);
    int i0 = mesh.VertexID(mesh.VertexOnFace(mesh_face, 0));
    int i1 = mesh.VertexID(mesh.VertexOnFace(mesh_face, 1));
    int i2 = mesh.VertexID(mesh.VertexOnFace(mesh_face, 2));
    R3TriangleVertex *v0 = triangle_vertices.Kth(i0);
    R3TriangleVertex *v1 = triangle_vertices.Kth(i1);
    R3TriangleVertex *v2 = triangle_vertices.Kth(i2);
    R3Triangle *triangle = new R3Triangle(v0, v1, v2);
    triangles.Insert(triangle);
  }

  // Update everything
  Update();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6630a
leaq 0x4f29b(%rip), %rax     # 0x9af48
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
divsd 0x4759a(%rip), %xmm0    # 0x93258
movsd %xmm0, 0x20(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x8(%rbx), %r12
xorl %r13d, %r13d
movslq 0x14(%r14), %rax
cmpq %rax, %r13
jge 0x4bd92
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rbp
pushq $0x68
popq %rdi
callq 0xe450
movq %rax, %rbx
leaq 0x18(%rbp), %rsi
movq %rax, %rdi
callq 0x4c4be
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x22bfc
movq %r12, %rdi
movq %rbx, %rsi
callq 0x22bfc
xorl %r15d, %r15d
movslq 0x14(%rbp), %rax
cmpq %rax, %r15
jge 0x4bd4a
movq 0x8(%rbp), %rax
movq (%rax,%r15,8), %rsi
movq %r14, %rdi
callq 0x11796
subsd (%rsp), %xmm0
andpd 0x47533(%rip), %xmm0    # 0x93270
incq %r15
ucomisd 0x20(%rsp), %xmm0
jbe 0x4bd17
jmp 0x4bd60
movq %r14, %rdi
movq %rbp, %rsi
callq 0x21234
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4c3ac
leaq 0x58(%rbp), %r15
movq %r15, %rdi
callq 0x2236e
testl %eax, %eax
jne 0x4bd7b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4c3d8
addq $0x48, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4c398
incq %r13
jmp 0x4bcd0
movq 0x8(%rsp), %rax
addq $0x18, %rax
movq %rax, (%rsp)
xorl %ebx, %ebx
movslq 0x34(%r14), %rax
cmpq %rax, %rbx
jge 0x4be0a
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movslq 0x70(%rcx), %rcx
movslq 0x70(%rdx), %rdx
movq 0x18(%rax), %rax
movslq 0x70(%rax), %rax
movq 0x10(%rsp), %rsi
movq (%rsi,%rcx,8), %r12
movq (%rsi,%rdx,8), %r13
movq (%rsi,%rax,8), %rbp
movl $0x88, %edi
callq 0xe450
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x4c910
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
incq %rbx
jmp 0x4bda1
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x248(%rax)
leaq 0x10(%rsp), %rdi
callq 0x66378
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4be58
movq %rax, %r14
movl $0x88, %esi
movq %r15, %rdi
jmp 0x4be49
movq %rax, %r14
pushq $0x68
popq %rsi
movq %rbx, %rdi
callq 0xe460
jmp 0x4be5b
jmp 0x4be58
jmp 0x4be58
jmp 0x4be58
jmp 0x4be58
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x66378
movq %r14, %rdi
callq 0xe810
nop