// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadPlyFile(char const*, gaps::R3SceneNode*)
int R3Scene::
ReadPlyFile(const char *filename, R3SceneNode *parent_node)
{
  // Check/set parent node
  if (!parent_node) parent_node = root;

  // Read ply file
  R3Mesh mesh;
  if (!mesh.ReadFile(filename)) {
    RNFail("Unable to read mesh %s\n", filename);
    return 0;
  }
  
  // Create array of vertices
  RNArray<R3TriangleVertex *> 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);
    if (!mesh.VertexColor(mesh_vertex).IsBlack()) triangle_vertex->SetColor(mesh.VertexColor(mesh_vertex));
    triangle_vertex->SetTextureCoords(mesh.VertexTextureCoords(mesh_vertex));
    triangle_vertex->SetSharedFlag();
    vertices.Insert(triangle_vertex);
  }

  // Check face segments and materials
  int max_face_segment = 0;
  int max_face_material = 0;
  for (int i = 0; i < mesh.NFaces(); i++) {
    R3MeshFace *face = mesh.Face(i);
    int face_segment = mesh.FaceSegment(face);
    int face_material = mesh.FaceMaterial(face);
    if (face_segment > max_face_segment) max_face_segment = face_segment;
    if (face_material > max_face_material) max_face_material = face_material;
  }

  // Create arrays of triangles
  char node_name[1024];
  RNArray<R3Triangle *> *tris_0_0 = NULL;
  RNSymbolTable<RNArray<R3Triangle *> *> triangles;
  for (int i = 0; i < mesh.NFaces(); i++) {
    R3MeshFace *mesh_face = mesh.Face(i);
    int face_segment = mesh.FaceSegment(mesh_face);
    if (face_segment < 0) face_segment = 0;
    int face_category = mesh.FaceCategory(mesh_face);
    if (face_category < 0) face_category = 0;
    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 = vertices.Kth(i0);
    R3TriangleVertex *v1 = vertices.Kth(i1);
    R3TriangleVertex *v2 = vertices.Kth(i2);
    R3Triangle *triangle = new R3Triangle(v0, v1, v2);
    RNArray<R3Triangle *> *tris = ((face_segment == 0) && (face_category == 0)) ? tris_0_0 : NULL; 
    if (!tris) {
      sprintf(node_name, "%d_%d", face_category, face_segment);
      if (!triangles.Find(node_name, &tris)) {
        tris = new RNArray<R3Triangle *>();
        if ((face_segment == 0) && (face_category == 0)) tris_0_0 = tris;
        triangles.Insert(node_name, tris);
      }
    }
    tris->Insert(triangle);
  }

  // Create node for each set of triangles
  std::map<std::string, RNArray<R3Triangle *> *, RNMapComparator< std::string > >::iterator it;
  for (it = triangles.m->begin(); it != triangles.m->end(); it++) {
    std::string node_name = it->first;
    RNArray<R3Triangle *> *tris = it->second;
    if (tris->IsEmpty()) { delete tris; continue; }
    R3SceneNode *node = new R3SceneNode(this);
    if (!InsertSceneElement(this, node, NULL, vertices, *tris)) return 0;
    node->SetName(node_name.c_str());
    const char *category_separator = strchr(node_name.c_str(), '_');
    if (category_separator) node->InsertInfo("index", RNStrdup(category_separator+1));
    parent_node->InsertChild(node);
    for (int i = 0; i < tris->NEntries(); i++) delete tris->Kth(i);
    delete tris;
  }

  // Delete vertices 
  for (int i = 0; i < vertices.NEntries(); i++) delete vertices[i];
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5e8, %rsp            # imm = 0x5E8
movq %rsi, %rbx
movq %rdi, %r15
movq %rdx, 0x28(%rsp)
testq %rdx, %rdx
jne 0x2cbc9
movq (%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x51a10
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5ea2e
testl %eax, %eax
je 0x2d09b
movq %r15, 0xa0(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x886bc
cmpl $0x0, 0xbc(%rsp)
jle 0x2ccbb
xorl %r14d, %r14d
leaq 0x8(%rsp), %r15
movq 0xb0(%rsp), %rax
movq (%rax,%r14,8), %r12
movl $0x68, %edi
callq 0x218c0
movq %rax, %rbx
leaq 0x18(%r12), %rsi
movq %rax, %rdi
callq 0x6a2e8
movsd 0x58(%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2cc6b
jp 0x2cc6b
movsd 0x60(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x2cc6b
jp 0x2cc6b
movsd 0x68(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x2cc6b
jnp 0x2cc7d
leaq 0x58(%r12), %rsi
leaq 0x30(%rbx), %rdi
callq 0x885c2
orb $0x4, 0x59(%rbx)
addq $0x48, %r12
movq %rbx, %rdi
addq $0x48, %rdi
movq %r12, %rsi
callq 0x82bce
orq $0x10200, 0x58(%rbx)    # imm = 0x10200
movl 0x14(%rsp), %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x88752
incq %r14
movslq 0xbc(%rsp), %rax
cmpq %rax, %r14
jl 0x2cc14
movl $0x30, %edi
callq 0x218c0
movq %rax, %rcx
addq $0x8, %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq $0x0, 0x28(%rax)
movq %rax, (%rsp)
cmpl $0x0, 0xdc(%rsp)
jle 0x2cf0a
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
movq 0xd0(%rsp), %rax
movq %r13, 0x38(%rsp)
movq (%rax,%r13,8), %rax
movl 0x9c(%rax), %ecx
testl %ecx, %ecx
movl $0x0, %ebx
movl %ecx, 0x24(%rsp)
cmovgl %ecx, %ebx
movl 0xa0(%rax), %ebp
testl %ebp, %ebp
movl $0x0, %r14d
cmovgl %ebp, %r14d
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 0x8(%rsp), %rsi
movq (%rsi,%rcx,8), %r15
movq (%rsi,%rdx,8), %r13
movq (%rsi,%rax,8), %r12
movl $0x88, %edi
callq 0x218c0
movq %rax, 0x30(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x6a800
cmpl $0x0, 0x24(%rsp)
setle %al
testl %ebp, %ebp
setle %r12b
andb %al, %r12b
movl $0x0, %r15d
cmovneq 0x18(%rsp), %r15
testq %r15, %r15
je 0x2cdb8
movq 0x38(%rsp), %r13
jmp 0x2cee5
leaq 0x1e0(%rsp), %r13
movq %r13, %rdi
leaq 0xafa40(%rip), %rsi     # 0xdc80a
movl %r14d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x21140
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %r13, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x1e0, %rdx            # imm = 0x1E0
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x395ec
movq 0x38(%rsp), %r13
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x3a13a
movq %rax, %rbx
movq (%rsp), %r14
addq $0x8, %r14
cmpq %r14, %rax
je 0x2ce31
movq 0x40(%rbx), %r15
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x2ce56
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpq %r14, %rbx
jne 0x2cee5
movl $0x10, %edi
callq 0x218c0
leaq 0x60(%rsp), %r14
movq %rax, %r15
movq %rax, %rdi
callq 0x886bc
testb %r12b, %r12b
movq 0x18(%rsp), %rax
cmovneq %r15, %rax
movq %rax, 0x18(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x1e0(%rsp), %rbx
movq %rbx, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x1e0, %rdx            # imm = 0x1E0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x395ec
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x3a370
movq %r15, (%rax)
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x2cee5
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x218e0
movl 0xc(%r15), %edx
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0x88752
incq %r13
movslq 0xdc(%rsp), %rax
cmpq %rax, %r13
jl 0x2cd0c
movq (%rsp), %rax
movq 0x18(%rax), %r15
addq $0x8, %rax
cmpq %rax, %r15
je 0x2d054
leaq 0x50(%rsp), %r14
leaq 0x40(%rsp), %r12
movq %r14, 0x40(%rsp)
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3955c
movq 0x40(%r15), %rbp
cmpl $0x0, 0xc(%rbp)
je 0x2cffe
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq 0xa0(%rsp), %r13
movq %r13, %rsi
callq 0x3bab8
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
leaq 0x8(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x33a65
movq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x3d0ca
movq 0x40(%rsp), %rdi
movl $0x5f, %esi
callq 0x21330
testq %rax, %rax
je 0x2cfc5
incq %rax
movq %rax, %rdi
callq 0x8b397
movq %rbx, %rdi
leaq 0xbe29c(%rip), %rsi     # 0xeb259
movq %rax, %rdx
callq 0x3cf00
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x3ce8e
xorl %ebx, %ebx
cmpl $0x0, 0xc(%rbp)
jle 0x2d003
xorl %r13d, %r13d
movq (%rbp), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
je 0x2cff0
movq (%rdi), %rax
callq *0x8(%rax)
incq %r13
movslq 0xc(%rbp), %rax
cmpq %rax, %r13
jl 0x2cfdd
jmp 0x2d003
movl $0xd, %ebx
movq %rbp, %rdi
callq 0x88726
movl $0x10, %esi
movq %rbp, %rdi
callq 0x218e0
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d02f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpl $0xd, %ebx
je 0x2d038
testl %ebx, %ebx
jne 0x2d082
movq %r15, %rdi
callq 0x21630
movq %rax, %r15
movq (%rsp), %rax
addq $0x8, %rax
cmpq %rax, %r15
jne 0x2cf29
cmpl $0x0, 0x14(%rsp)
jle 0x2d082
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x2d075
movl $0x68, %esi
callq 0x218e0
incq %rbx
movslq 0x14(%rsp), %rax
cmpq %rax, %rbx
jl 0x2d05d
movq %rsp, %rdi
callq 0x37362
leaq 0x8(%rsp), %rdi
callq 0x88726
movl $0x1, %ebp
jmp 0x2d0ae
xorl %ebp, %ebp
leaq 0xaf74e(%rip), %rdi     # 0xdc7f2
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b53a
leaq 0xa8(%rsp), %rdi
callq 0x51f3e
movl %ebp, %eax
addq $0x5e8, %rsp            # imm = 0x5E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2d193
jmp 0x2d0d6
movq %rax, %r12
jmp 0x2d1a0
movq %rax, %r12
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x2d189
movq 0x70(%rsp), %rsi
jmp 0x2d181
jmp 0x2d16a
movq %rax, %r12
movl $0x10, %esi
movq %r15, %rdi
jmp 0x2d184
jmp 0x2d16a
movq %rax, %r12
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x2d189
movq 0x90(%rsp), %rsi
jmp 0x2d181
movq %rax, %r12
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x218e0
jmp 0x2d172
jmp 0x2d16a
jmp 0x2d16f
jmp 0x2d16a
movq %rax, %r12
movl $0x88, %esi
movq 0x30(%rsp), %rdi
jmp 0x2d184
movq %rax, %r12
movl $0x68, %esi
movq %rbx, %rdi
callq 0x218e0
jmp 0x2d196
movq %rax, %r12
jmp 0x2d189
movq %rax, %r12
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d189
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x218e0
movq %rsp, %rdi
callq 0x37362
jmp 0x2d196
movq %rax, %r12
leaq 0x8(%rsp), %rdi
callq 0x88726
leaq 0xa8(%rsp), %rdi
callq 0x51f3e
movq %r12, %rdi
callq 0x220d0
nop