PCD: Point Cloud Data Files
| (require pcd) | package: pcd |
This package reads and writes PCD point-cloud files — the native file format of the Point Cloud Library (PCL), version 0.7. All three PCD storage modes are supported: DATA ascii, DATA binary, and DATA binary_compressed. The compressed mode uses the LZF algorithm (as PCL does) and requires the pure-Racket lzf package, a declared runtime dependency.
1 Quick start
(require pcd) (define cloud (point-cloud '("x" "y" "z" "intensity") '(4 4 4 4) '(#\F #\F #\F #\F) '(1 1 1 1) 3 1 (vector 0.0 0.0 0.0 1.0 0.0 0.0 0.0) (vector (vector 0.5 1.25 -3.75 10.5) (vector 1.0 2.0 3.0 -0.5) (vector 4.0 5.0 6.0 1.5)))) (pcd-write-file "/tmp/example.pcd" cloud #:data 'binary-compressed) (define cloud2 (pcd-read-file "/tmp/example.pcd")) (equal? cloud cloud2)
2 The point-cloud record
struct
(struct point-cloud ( fields sizes types counts width height viewpoint points)) fields : (listof string?) sizes : (listof pcd-size/c) types : (listof pcd-type/c) counts : (listof pcd-count/c) width : exact-nonnegative-integer? height : exact-nonnegative-integer? viewpoint : (vectorof flonum?) points : (vectorof (vectorof real?))
fields — the FIELDS names in file order. A field named "_" is PCL’s padding marker: it occupies space in 'ascii and 'binary data and is skipped in 'binary-compressed chunks (its values read back as 0, exactly like PCL).
sizes — bytes per field element: 1, 2, 4, or 8 (the SIZE line).
types — one of #\F (float), #\U (unsigned integer), or #\I (signed integer) per field (the TYPE line). Float fields require size 4 or 8.
counts — elements per field (the COUNT line). A file that omits the line is read as all-1. A field with count n contributes n consecutive elements to every point’s value vector.
width, height — the WIDTH and HEIGHT lines. Organized clouds (height greater than 1) are supported; points is stored row-major, with width consecutive points per row. For unorganized clouds height is 1. The constraint that (* width height) equals the number of points is enforced.
viewpoint — seven flonums: translation tx ty tz then quaternion qw qx qy qz; defaults to (vector 0.0 0.0 0.0 1.0 0.0 0.0 0.0) when a file omits the VIEWPOINT line.
points — point-major vector; each point is a flat vector of every field element in file order (its length is the sum of counts). Float columns hold flonums; integer columns hold exact integers within the range of their size and signedness.
procedure
(pcd-size/c v) → boolean?
v : any/c
procedure
(pcd-type/c v) → boolean?
v : any/c
procedure
(pcd-count/c v) → boolean?
v : any/c
3 Reading and writing
procedure
(pcd-read in) → point-cloud?
in : input-port?
procedure
(pcd-read-file path) → point-cloud?
path : path-string?
procedure
(pcd-read-bytes bs) → point-cloud?
bs : bytes?
procedure
out : output-port? cloud : point-cloud? data : (or/c 'ascii 'binary 'binary-compressed) = 'ascii
procedure
(pcd-write-file path cloud [#:data data]) → void?
path : path-string? cloud : point-cloud? data : (or/c 'ascii 'binary 'binary-compressed) = 'ascii
4 Data formats
4.1 ascii
One point per line; values separated by spaces or tabs. Float values are printed so that they read back bit-exactly for float32-representable values; nan, inf, and -inf follow PCL’s conventions and round-trip.
4.2 binary
A little-endian array of structures: for each point, every field element in file order at its sizes width. Reading requires POINTS × step bytes and rejects truncated data.
4.3 binary_compressed
Matches PCL’s on-disk layout: the data is reordered field-major (all values of the first field, then the second, ...), each field’s chunk is LZF compressed, and for each field — in field order, skipping "_" padding fields — the file stores a little-endian unsigned 32-bit compressed size, then an unsigned 32-bit uncompressed size, then the compressed chunk. The reader validates each field’s uncompressed size against SIZE × COUNT × POINTS, de-interleaves the planes back to point-major order, and decodes values as in the binary format. Truncated or corrupt LZF chunks raise exn:fail naming the field.
5 Errors
All format errors raise exn:fail. Messages name the offending header line number, field, or point index, e.g. header line 3: unknown TYPE 'Q' for field 'z', truncated DATA section: needed 12 byte(s) of binary point data, got only 4, or header line 8: POINTS 5 does not match WIDTH * HEIGHT = 1.
6 Scope
This package is a file-format codec for PCD v0.7. It does not re-create PCL’s pcl::PointCloud C++ type system, Kd-trees, filters, or readers for other formats (PLY, STL, OBJ, ...).