from __future__ import print_function
import sys

from laspy.file import File

# usage
# $ python las2ply > output.ply

# http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python
def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

# input file
f = File('../S-Infinity/H4.las',mode='r')
out = open('H4.ply','w+')

count = f.header.count

eprint(count)

out.write("""ply
format ascii 1.0
comment VCGLIB generated
element vertex %d
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
element face 0
property list uchar int vertex_indices
end_header
""" % count)

count = 0
for p in f.points:
    X = p[0][0]*10e-5
    Y = (p[0][1])*10e-5
    Z = (p[0][2])*10e-5
    I = (p[0][3])
    R = (p[0][-3])*256/65792
    G = (p[0][-2])*256/65792
    B = (p[0][-1])*256/65792
    out.write("%f %f %f %d %d %d\n" % (X, Y, Z, R, G, B))
    if count % 1000000 == 0:
        eprint(count)
    count += 1
    
out.close()