1+ #= =========================================================================================+
2+ | TABLE OF CONTENTS: |
3+ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4+ | - saveply |
5+ | - readply |
6+ +==========================================================================================#
7+
8+ export saveply, readply
9+
10+ """
11+ saveply(points::AbstractArray, filename::String)
12+
13+ Description:
14+ ---
15+ Saves a point cloud to a PLY file.
16+
17+ Example:
18+ ---
19+ ```julia
20+ points = rand(3, 1000) # 3D points
21+ saveply(points, "pointcloud.ply")
22+ # or
23+ saveply("pointcloud", points) # add .ply extension automatically
24+
25+ points = rand(2, 1000) # 2D points
26+ saveply("pointcloud.ply", points)
27+ ```
28+ """
29+ function saveply (points:: AbstractArray , filename:: String )
30+ # input check
31+ n, m = size (points)
32+ n in [2 , 3 ] || throw (ArgumentError (" points must be a 2D array with 2 or 3 rows" ))
33+ if ! endswith (filename, " .ply" )
34+ filename *= " .ply"
35+ end
36+ if n == 2
37+ pts = vcat (points, zeros (1 , m))'
38+ else
39+ pts = points'
40+ end
41+
42+ cloud = trimesh. points. PointCloud (pts)
43+ cloud. export (filename)
44+ @info """ point data saved at
45+ $(filename)
46+ """
47+ return nothing
48+ end
49+
50+ saveply (filename:: String , points:: AbstractArray ) = savexyz (points, filename)
51+
52+ """
53+ readply(filename::String; xy::Bool=false)
54+
55+ Description:
56+ ---
57+ Reads a point cloud from a PLY file. If `xy` is true, it returns only the 2D points (neglect z).
58+
59+ Example:
60+ ---
61+ ```julia
62+ pc = readply("pointcloud.ply")
63+ # or
64+ pc = readply("pointcloud.ply", xy=true) # returns only x and y
65+ """
66+ function readply (filename:: String ; xy:: Bool = false )
67+ isfile (filename) || throw (ArgumentError (" file $(filename) does not exist" ))
68+ pc = py2ju (Array, trimesh. load (filename). vertices. T)
69+ if xy
70+ return Array (pc[1 : 2 , :])
71+ else
72+ return pc
73+ end
74+ end
0 commit comments