// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/GSV/GSVMesh.cpp
// Function: gaps::GSVMesh::WritePlyFile(gaps::GSVScan*, char const*, int) const
int GSVMesh::
WritePlyFile(GSVScan *scan, const char *filename, RNBoolean binary) const
{
  typedef struct PlyVertex {
    float x, y, z;
    int scanline_index;
    int point_index;
    int boundary_type;
  } PlyVertex;

  typedef struct PlyFace {
    unsigned char nverts;
    int *verts;
    int segment;
  } PlyFace;

  // Element names
  char *elem_names[] = { (char *) "vertex", (char *) "face" };

  // List of property information for a vertex 
  static PlyProperty vert_props[] = { 
    {(char *) "x", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,x), 0, 0, 0, 0},
    {(char *) "y", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,y), 0, 0, 0, 0},
    {(char *) "z", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,z), 0, 0, 0, 0},
    {(char *) "scanline_index", PLY_INT, PLY_INT, offsetof(PlyVertex,scanline_index), 0, 0, 0, 0},
    {(char *) "point_index", PLY_INT, PLY_INT, offsetof(PlyVertex,point_index), 0, 0, 0, 0},
    {(char *) "boundary_type", PLY_INT, PLY_INT, offsetof(PlyVertex,boundary_type), 0, 0, 0, 0}
  };

  // List of property information for a vertex 
  static PlyProperty face_props[] = { 
    {(char *) "vertex_indices", PLY_INT, PLY_INT, offsetof(PlyFace,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(PlyFace,nverts)},
    {(char *) "segment", PLY_INT, PLY_INT, offsetof(PlyFace,segment), 0, 0, 0, 0}
  };

  // Open ply file
  float version;
  int file_type = (binary) ? PLY_BINARY_NATIVE : PLY_ASCII;
  PlyFile *ply = ply_open_for_writing((char *) filename, 2, elem_names, file_type, &version);
  if (!ply) return -1;

  // Describe vertex properties
  ply_element_count(ply, (char *) "vertex", NVertices());
  for (unsigned int i = 0; i < sizeof(vert_props) / sizeof(PlyProperty); i++) {
    ply_describe_property(ply, (char *) "vertex", &vert_props[i]);
  }

  // Describe face properties
  ply_element_count(ply, (char *) "face", NFaces());
  for (unsigned int i = 0; i < sizeof(face_props) / sizeof(PlyProperty); i++) {
    ply_describe_property(ply, (char *) "face", &face_props[i]);
  }

  // Describe scan properties
  char buffer[1024];
  GSVSegment *segment = (scan) ? scan->Segment() : NULL;
  GSVRun *run = (segment) ? segment->Run() : NULL;
  sprintf(buffer, "run_name %s", (run) ? run->Name() : "None");
  ply_put_obj_info(ply, buffer);
  sprintf(buffer, "segment_index %d", (segment) ? segment->RunIndex() : -1);
  ply_put_obj_info(ply, buffer);
  sprintf(buffer, "scan_index %d", (scan) ? scan->SegmentIndex() : -1);
  ply_put_obj_info(ply, buffer);

  // Complete header
  ply_header_complete(ply);

  // Write vertices
  ply_put_element_setup(ply, (char *) "vertex");
  for (int i = 0; i < NVertices(); i++) {
    GSVMeshVertex *v = (GSVMeshVertex *) Vertex(i);
    const R3Point& position = VertexPosition(v);
    PlyVertex ply_vertex;
    ply_vertex.x = position.X();
    ply_vertex.y = position.Y();
    ply_vertex.z = position.Z();
    ply_vertex.scanline_index = (v->scanline) ? v->scanline->ScanIndex() : -1;
    ply_vertex.point_index = v->point_index;
    ply_vertex.boundary_type = v->boundary_type;
    ply_put_element(ply, (void *) &ply_vertex);
  }

  // Write faces
  ply_put_element_setup(ply, (char *) "face");
  for (int i = 0; i < NFaces(); i++) {
    R3MeshFace *f = Face(i);
    static int verts[3];
    static PlyFace ply_face = { 3, verts, 0 };
    ply_face.verts[0] = VertexID(VertexOnFace(f, 0));
    ply_face.verts[1] = VertexID(VertexOnFace(f, 1));
    ply_face.verts[2] = VertexID(VertexOnFace(f, 2));
    ply_face.segment = FaceSegment(f);
    ply_put_element(ply, (void *) &ply_face);
  }

  // Close the file 
  ply_close(ply);

  // Return number of faces written
  return NFaces();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rdx, %rax
movq %rsi, %rbp
movq %rdi, %rbx
movaps 0x1887b7(%rip), %xmm0   # 0x1cce50
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
xorl %esi, %esi
testl %ecx, %ecx
setne %sil
leal 0x1(%rsi,%rsi,2), %ecx
leaq 0x14(%rsp), %r8
movq %rax, %rdi
movl $0x2, %esi
callq 0xd8cf0
testq %rax, %rax
je 0x44907
movq %rax, %r14
movl 0x14(%rbx), %edx
leaq 0x108d5b(%rip), %r12    # 0x14d430
movq %rax, %rdi
movq %r12, %rsi
callq 0xd9112
xorl %r15d, %r15d
leaq 0x191516(%rip), %r13    # 0x1d5c00
leaq (%r15,%r13), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0xd8ef0
addq $0x28, %r15
cmpq $0xf0, %r15
jne 0x446ea
movl 0x34(%rbx), %edx
leaq 0x108d27(%rip), %r12    # 0x14d437
movq %r14, %rdi
movq %r12, %rsi
callq 0xd9112
leaq 0x1915ce(%rip), %rdx    # 0x1d5cf0
movq %r14, %rdi
movq %r12, %rsi
callq 0xd8ef0
leaq 0x1915e4(%rip), %rdx    # 0x1d5d18
movq %r14, %rdi
movq %r12, %rsi
callq 0xd8ef0
leaq 0x108d02(%rip), %r13    # 0x14d448
movb $0x1, %r15b
testq %rbp, %rbp
movq %rbp, 0x8(%rsp)
je 0x44775
movq %rbp, %rdi
callq 0x3f6ac
testq %rax, %rax
je 0x44775
movq %rax, %r12
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x44770
movq 0x48(%rax), %r13
xorl %r15d, %r15d
jmp 0x44778
xorl %r12d, %r12d
leaq 0x108cbd(%rip), %rsi    # 0x14d43c
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %rbp, %rsi
callq 0xd98b9
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testb %r15b, %r15b
jne 0x447b0
movl 0x18(%r12), %edx
leaq 0x108c96(%rip), %rsi    # 0x14d44d
leaq 0x40(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r12, %rsi
callq 0xd98b9
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x447e2
callq 0x3f79a
movl %eax, %ebp
leaq 0x108c75(%rip), %rsi    # 0x14d45e
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r15, %rsi
callq 0xd98b9
movq %r14, %rdi
callq 0xd9138
leaq 0x108c1c(%rip), %rsi    # 0x14d430
movq %r14, %rdi
callq 0xd9351
cmpl $0x0, 0x14(%rbx)
jle 0x4488c
xorl %r12d, %r12d
leaq 0x18(%rsp), %r15
movq 0x8(%rbx), %rax
movq (%rax,%r12,8), %rax
movupd 0x18(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsd2ss 0x28(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x98(%rax), %rcx
testq %rcx, %rcx
je 0x44860
movl 0x10(%rcx), %ecx
jmp 0x44865
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x24(%rsp)
movq 0xa0(%rax), %rax
movq %rax, 0x28(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xd9379
incq %r12
movslq 0x14(%rbx), %rax
cmpq %rax, %r12
jl 0x4482a
leaq 0x108ba4(%rip), %rsi    # 0x14d437
movq %r14, %rdi
callq 0xd9351
cmpl $0x0, 0x34(%rbx)
jle 0x448fa
xorl %r12d, %r12d
leaq 0x191495(%rip), %r15    # 0x1d5d40
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rcx
movl 0x70(%rcx), %ecx
movq 0x191487(%rip), %rdx    # 0x1d5d48
movl %ecx, (%rdx)
movq 0x10(%rax), %rcx
movl 0x70(%rcx), %ecx
movl %ecx, 0x4(%rdx)
movq 0x18(%rax), %rcx
movl 0x70(%rcx), %ecx
movl %ecx, 0x8(%rdx)
movl 0x9c(%rax), %eax
movl %eax, 0x19146d(%rip)    # 0x1d5d50
movq %r14, %rdi
movq %r15, %rsi
callq 0xd9379
incq %r12
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jl 0x448ab
movq %r14, %rdi
callq 0xdad27
movl 0x34(%rbx), %eax
jmp 0x4490c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq