// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
testq %rdx, %rdx
jne 0x2ca21
movq (%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x50e64
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5de58
testl %eax, %eax
je 0x2cedf
movq %r15, 0x98(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x8758c
cmpl $0x0, 0xbc(%rsp)
jle 0x2cb14
xorl %r14d, %r14d
leaq 0x8(%rsp), %rbx
movq 0xb0(%rsp), %rax
movq (%rax,%r14,8), %r12
movl $0x68, %edi
callq 0x218c0
movq %rax, %r15
leaq 0x18(%r12), %rsi
movq %rax, %rdi
callq 0x69528
movsd 0x58(%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2cac3
jp 0x2cac3
movsd 0x60(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x2cac3
jp 0x2cac3
movsd 0x68(%r12), %xmm0
ucomisd %xmm1, %xmm0
jne 0x2cac3
jnp 0x2cad6
leaq 0x58(%r12), %rsi
leaq 0x30(%r15), %rdi
callq 0x87492
orb $0x4, 0x59(%r15)
addq $0x48, %r12
movq %r15, %rdi
addq $0x48, %rdi
movq %r12, %rsi
callq 0x81af6
orq $0x10200, 0x58(%r15)    # imm = 0x10200
movl 0x14(%rsp), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x87622
incq %r14
movslq 0xbc(%rsp), %rax
cmpq %rax, %r14
jl 0x2ca6c
movl 0xdc(%rsp), %ebx
movl $0x30, %edi
callq 0x218c0
movq %rax, %r15
addq $0x8, %r15
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rcx, 0x10(%rax)
movq %r15, 0x18(%rax)
movq %r15, 0x20(%rax)
movq %rcx, 0x28(%rax)
movq %rax, (%rsp)
testl %ebx, %ebx
jle 0x2cd58
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %rbp, 0xa0(%rsp)
movq 0xd0(%rsp), %rax
movq %r13, 0x30(%rsp)
movq (%rax,%r13,8), %rax
movl 0x9c(%rax), %ecx
testl %ecx, %ecx
movl $0x0, %ebx
movl %ecx, 0x1c(%rsp)
cmovgl %ecx, %ebx
movl 0xa0(%rax), %ebp
testl %ebp, %ebp
movl $0x0, %r15d
cmovgl %ebp, %r15d
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), %r14
movq (%rsi,%rdx,8), %r13
movq (%rsi,%rax,8), %r12
movl $0x88, %edi
callq 0x218c0
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x69a40
cmpl $0x0, 0x1c(%rsp)
setg %al
testl %ebp, %ebp
setg %r14b
orb %al, %r14b
movq 0xa0(%rsp), %rbp
testq %rbp, %rbp
sete %al
orb %r14b, %al
cmpb $0x1, %al
jne 0x2cc7a
leaq 0x1e0(%rsp), %r12
movq %r12, %rdi
leaq 0xadc03(%rip), %rsi     # 0xda81a
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x21140
leaq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %r12, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x1e0, %rdx            # imm = 0x1E0
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x39150
movq 0x30(%rsp), %r13
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x39c6e
movq %rax, %r15
movq (%rsp), %r12
addq $0x8, %r12
cmpq %r12, %rax
je 0x2cc87
movq 0x40(%r15), %rbx
jmp 0x2cc89
movq %rbp, %rbx
movq 0x30(%rsp), %r13
jmp 0x2cd2c
xorl %ebx, %ebx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x2ccab
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpq %r12, %r15
jne 0x2cd2c
movl $0x10, %edi
callq 0x218c0
leaq 0x58(%rsp), %r15
movq %rax, %rbx
movq %rax, %rdi
callq 0x8758c
testb %r14b, %r14b
cmoveq %rbx, %rbp
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x1e0(%rsp), %r14
movq %r14, %rdi
callq 0x21380
leaq (%rsp,%rax), %rdx
addq $0x1e0, %rdx            # imm = 0x1E0
movq %r15, %rdi
movq %r14, %rsi
callq 0x39150
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x39e94
movq %rbx, (%rax)
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x2cd2c
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x218e0
movl 0xc(%rbx), %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x87622
incq %r13
movslq 0xdc(%rsp), %rax
cmpq %rax, %r13
jl 0x2cb57
movq (%rsp), %rax
movq 0x18(%rax), %r15
addq $0x8, %rax
cmpq %rax, %r15
je 0x2ce9d
leaq 0x48(%rsp), %r14
leaq 0x38(%rsp), %r12
movq %r14, 0x38(%rsp)
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x390ba
movq 0x40(%r15), %rbp
cmpl $0x0, 0xc(%rbp)
je 0x2ce47
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq 0x98(%rsp), %r13
movq %r13, %rsi
callq 0x3b560
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
leaq 0x8(%rsp), %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x33708
movq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x3cc1a
movq 0x38(%rsp), %rdi
movl $0x5f, %esi
callq 0x21330
testq %rax, %rax
je 0x2ce0b
incq %rax
movq %rax, %rdi
callq 0x89f5f
movq %rbx, %rdi
leaq 0xbc4a6(%rip), %rsi     # 0xe92a9
movq %rax, %rdx
callq 0x3ca2a
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c97e
movl 0xc(%rbp), %eax
xorl %ebx, %ebx
testl %eax, %eax
jle 0x2ce4c
xorl %r13d, %r13d
movq (%rbp), %rcx
movq (%rcx,%r13,8), %rdi
testq %rdi, %rdi
je 0x2ce3a
movq (%rdi), %rax
callq *0x8(%rax)
movl 0xc(%rbp), %eax
incq %r13
movslq %eax, %rcx
cmpq %rcx, %r13
jl 0x2ce24
jmp 0x2ce4c
movl $0xd, %ebx
movq %rbp, %rdi
callq 0x875f6
movl $0x10, %esi
movq %rbp, %rdi
callq 0x218e0
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x2ce78
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x218e0
cmpl $0xd, %ebx
je 0x2ce81
testl %ebx, %ebx
jne 0x2cec6
movq %r15, %rdi
callq 0x21630
movq %rax, %r15
movq (%rsp), %rax
addq $0x8, %rax
cmpq %rax, %r15
jne 0x2cd6f
cmpl $0x0, 0x14(%rsp)
jle 0x2cec6
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
movl $0x68, %esi
callq 0x218e0
incq %rbx
movslq 0x14(%rsp), %rax
cmpq %rax, %rbx
jl 0x2cea6
movq %rsp, %rdi
callq 0x36f4e
leaq 0x8(%rsp), %rdi
callq 0x875f6
movl $0x1, %ebp
jmp 0x2cef2
xorl %ebp, %ebp
leaq 0xad91a(%rip), %rdi     # 0xda802
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8a0fe
leaq 0xa8(%rsp), %rdi
callq 0x513a2
movl %ebp, %eax
addq $0x5e8, %rsp            # imm = 0x5E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2cfd7
jmp 0x2cf1a
movq %rax, %r12
jmp 0x2cfe4
jmp 0x2cfae
movq %rax, %r12
movl $0x10, %esi
movq %rbx, %rdi
jmp 0x2cfc8
movq %rax, %r12
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x2cfcd
movq 0x68(%rsp), %rsi
jmp 0x2cfc5
movq %rax, %r12
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x218e0
jmp 0x2cfb6
jmp 0x2cfae
movq %rax, %r12
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x2cfcd
movq 0x88(%rsp), %rsi
jmp 0x2cfc5
jmp 0x2cfae
jmp 0x2cfb3
jmp 0x2cfae
movq %rax, %r12
movl $0x88, %esi
movq 0x28(%rsp), %rdi
jmp 0x2cfc8
movq %rax, %r12
movl $0x68, %esi
movq %r15, %rdi
callq 0x218e0
jmp 0x2cfda
movq %rax, %r12
jmp 0x2cfcd
movq %rax, %r12
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x2cfcd
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x218e0
movq %rsp, %rdi
callq 0x36f4e
jmp 0x2cfda
movq %rax, %r12
leaq 0x8(%rsp), %rdi
callq 0x875f6
leaq 0xa8(%rsp), %rdi
callq 0x513a2
movq %r12, %rdi
callq 0x220c0
nop