convert.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/python
  2. import plyfile
  3. import argparse
  4. parser = argparse.ArgumentParser(description="Convert a PLY file to C arrays.")
  5. parser.add_argument("input_file", help="the input PLY file")
  6. parser.add_argument("output_file", help="the output C file")
  7. args = parser.parse_args()
  8. # Open the PLY file
  9. plydata = plyfile.PlyData.read(args.input_file)
  10. # Extract the vertices
  11. vertices = plydata["vertex"].data
  12. num_vertices = len(vertices)
  13. with open(args.output_file, "w") as f:
  14. f.write("#define NUM_VERTICES %d\n" % num_vertices)
  15. f.write("float vertexCoords[NUM_VERTICES][3] = {\n")
  16. for i in range(num_vertices):
  17. x, y, z = vertices[i][0], vertices[i][1], vertices[i][2]
  18. f.write(" {%f, %f, %f},\n" % (x, y, z))
  19. f.write("};")
  20. # Extract the faces
  21. faces = plydata["face"].data
  22. num_faces = len(faces)
  23. f.write("int edgeIndices[][3] = {\n")
  24. for i in range(num_faces):
  25. face = faces[i][0]
  26. if len(face) == 3:
  27. f.write(" {%d, %d, %d},\n" % (face[0], face[1], face[2]))
  28. elif len(face) == 4:
  29. # Convert 4-index face to 2-index edges
  30. edges = [
  31. (face[0], face[1]),
  32. (face[1], face[2]),
  33. (face[2], face[3]),
  34. (face[3], face[0]),
  35. ]
  36. for edge in edges:
  37. f.write(" {%d, %d},\n" % (edge[0], edge[1]))
  38. f.write("};\n")