Skip to content

Instantly share code, notes, and snippets.

@tonyarkles
Created December 20, 2018 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyarkles/0d26b63f81724c2e44ac8ea41ac22711 to your computer and use it in GitHub Desktop.
Save tonyarkles/0d26b63f81724c2e44ac8ea41ac22711 to your computer and use it in GitHub Desktop.
(defstruct stl-file header num-points points)
(defstruct stl-point normal point attribs)
(defun read-header (stream)
(let ((data (make-array 80 :initial-element nil)))
(read-sequence data stream)
data))
(defun read-num-triangles (stream)
(let ((data (make-array 4 :element-type '(unsigned-byte 8))))
(read-sequence data stream)
(cl-pack:unpack "l" data)))
(defun read-point (stream)
(let ((data (make-array (+ (* 4 6) 2) :element-type '(unsigned-byte 8))))
(read-sequence data stream)
(multiple-value-bind (nx ny nz px py pz attr) (cl-pack:unpack "ffffffv" data)
(make-stl-point :normal (list nx ny nz) :point (list px py pz) :attribs attr)
)))
(defun parse-stl-file (filename)
(with-open-file (stream filename :element-type '(unsigned-byte 8))
(let* ((header (read-header stream))
(ntris (read-num-triangles stream))
(points (make-array ntris :element-type 'stl-point)))
(loop for i from 0 to (- ntris 1) do
(setf (aref points i) (read-point stream)))
(format t "Header read: ~A~%" header)
(format t "Number of triangles: ~A~%" ntris)
(make-stl-file :header header :num-points ntris :points points))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment