// File: 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:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1b0(%rsp)
movq %rsi, 0x1a8(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x9a220
movl $0x0, 0x194(%rsp)
movl 0x194(%rsp), %eax
movl %eax, 0xe8(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0x135e0
movl %eax, 0xec(%rsp)
jmp 0x98f93
movl 0xe8(%rsp), %eax
movl 0xec(%rsp), %ecx
cmpl %ecx, %eax
jge 0x9931f
movq 0x1a8(%rsp), %rdi
movl 0x194(%rsp), %esi
callq 0x13630
movq %rax, 0xe0(%rsp)
jmp 0x98fc7
movq 0xe0(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x13660
movq %rax, 0xd8(%rsp)
jmp 0x98ff6
movq 0xd8(%rsp), %rax
movq %rax, 0x170(%rsp)
movl $0x68, %edi
callq 0xf4c0
movq %rax, 0xd0(%rsp)
jmp 0x9901a
movq 0xd0(%rsp), %rdi
movq 0x170(%rsp), %rsi
callq 0x9a8d0
jmp 0x99031
movq 0xd0(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x168(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x9a280
jmp 0x99058
movq 0xf0(%rsp), %rdi
addq $0x8, %rdi
movq 0x168(%rsp), %rsi
callq 0x9a280
jmp 0x99073
movl $0x1, 0x164(%rsp)
leaq 0x9d853(%rip), %rax     # 0x1368d8
movsd (%rax), %xmm0
movsd 0x9a1c7(%rip), %xmm1    # 0x133258
divsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movl $0x0, 0x154(%rsp)
movl 0x154(%rsp), %eax
movl %eax, 0xc8(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x36b50
movl %eax, 0xcc(%rsp)
jmp 0x990d5
movl 0xc8(%rsp), %eax
movl 0xcc(%rsp), %ecx
cmpl %ecx, %eax
jge 0x991e6
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
movl 0x154(%rsp), %edx
callq 0x36ec0
movq %rax, 0xc0(%rsp)
jmp 0x99111
movq 0xc0(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x15630
movsd %xmm0, 0xb8(%rsp)
jmp 0x99141
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x140(%rsp), %xmm0
leaq 0x9d775(%rip), %rax     # 0x1368d8
subsd (%rax), %xmm0
movaps 0x9a102(%rip), %xmm1    # 0x133270
pand %xmm1, %xmm0
ucomisd 0x158(%rsp), %xmm0
jbe 0x991ce
movl $0x0, 0x164(%rsp)
jmp 0x991e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x188(%rsp)
movl %eax, 0x184(%rsp)
jmp 0x9958c
movq 0xd0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x188(%rsp)
movl %eax, 0x184(%rsp)
movl $0x68, %esi
callq 0xf4d0
jmp 0x9958c
jmp 0x991d0
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
jmp 0x990a9
cmpl $0x0, 0x164(%rsp)
je 0x99238
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x36860
movq %rax, 0xb0(%rsp)
jmp 0x9920f
movq 0xb0(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x168(%rsp), %rdi
movq 0x138(%rsp), %rsi
callq 0x9a4a0
jmp 0x99236
jmp 0x99238
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x368d0
movq %rax, 0xa8(%rsp)
jmp 0x99257
movq 0xa8(%rsp), %rdi
callq 0x3a440
movl %eax, 0xa4(%rsp)
jmp 0x9926d
movl 0xa4(%rsp), %eax
cmpl $0x0, %eax
jne 0x992c1
movq 0x168(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x368d0
movq %rax, 0x98(%rsp)
jmp 0x992a8
movq 0x98(%rsp), %rsi
movq 0x90(%rsp), %rdi
callq 0x9a540
jmp 0x992bf
jmp 0x992c1
movq 0x168(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x178(%rsp), %rsi
callq 0x368f0
movq %rax, 0x88(%rsp)
jmp 0x992f0
movq 0x88(%rsp), %rsi
movq 0x80(%rsp), %rdi
callq 0x9a440
jmp 0x99307
jmp 0x99309
movl 0x194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x194(%rsp)
jmp 0x98f6f
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
movl %eax, 0x78(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0x135a0
movl %eax, 0x7c(%rsp)
jmp 0x99348
movl 0x78(%rsp), %eax
movl 0x7c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x99561
movq 0x1a8(%rsp), %rdi
movl 0x134(%rsp), %esi
callq 0x136d0
movq %rax, 0x70(%rsp)
jmp 0x99373
movq 0x70(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x1a8(%rsp), %rdi
movq %rdi, 0x60(%rsp)
movq 0x128(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq %rax, 0x68(%rsp)
jmp 0x993a3
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdi
callq 0x36910
movl %eax, 0x5c(%rsp)
jmp 0x993b8
movl 0x5c(%rsp), %eax
movl %eax, 0x124(%rsp)
movq 0x1a8(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movq 0x128(%rsp), %rsi
movl $0x1, %edx
callq 0x13700
movq %rax, 0x50(%rsp)
jmp 0x993e9
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x36910
movl %eax, 0x44(%rsp)
jmp 0x993fe
movl 0x44(%rsp), %eax
movl %eax, 0x120(%rsp)
movq 0x1a8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0x128(%rsp), %rsi
movl $0x2, %edx
callq 0x13700
movq %rax, 0x38(%rsp)
jmp 0x9942f
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x36910
movl %eax, 0x2c(%rsp)
jmp 0x99444
movl 0x2c(%rsp), %eax
movl %eax, 0x11c(%rsp)
movl 0x124(%rsp), %esi
leaq 0x198(%rsp), %rdi
callq 0x9a260
movq %rax, 0x20(%rsp)
jmp 0x9946a
movq 0x20(%rsp), %rax
movq %rax, 0x110(%rsp)
movl 0x120(%rsp), %esi
leaq 0x198(%rsp), %rdi
callq 0x9a260
movq %rax, 0x18(%rsp)
jmp 0x99492
movq 0x18(%rsp), %rax
movq %rax, 0x108(%rsp)
movl 0x11c(%rsp), %esi
leaq 0x198(%rsp), %rdi
callq 0x9a260
movq %rax, 0x10(%rsp)
jmp 0x994ba
movq 0x10(%rsp), %rax
movq %rax, 0x100(%rsp)
movl $0x88, %edi
callq 0xf4c0
movq %rax, 0x8(%rsp)
jmp 0x994d8
movq 0x8(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0x108(%rsp), %rdx
movq 0x100(%rsp), %rcx
callq 0x9b320
jmp 0x994fc
movq 0xf0(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0xf8(%rsp)
addq $0x18, %rdi
movq 0xf8(%rsp), %rsi
callq 0x9a340
jmp 0x99524
jmp 0x99526
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x9932a
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x188(%rsp)
movl %eax, 0x184(%rsp)
movl $0x88, %esi
callq 0xf4d0
jmp 0x9958c
movq 0xf0(%rsp), %rdi
movq (%rdi), %rax
movq 0x248(%rax), %rax
callq *%rax
jmp 0x99577
leaq 0x198(%rsp), %rdi
callq 0x9a250
addq $0x1b8, %rsp            # imm = 0x1B8
retq
leaq 0x198(%rsp), %rdi
callq 0x9a250
movq 0x188(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)