Skip to content

Commit

Permalink
[meshops] update thi.ng/dstruct dependency w/ new streams, update
Browse files Browse the repository at this point in the history
mesh.io ns to support CLJS
  • Loading branch information
postspectacular committed Oct 10, 2015
1 parent 8bd0f2a commit 265de8e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 113 deletions.
2 changes: 2 additions & 0 deletions geom-meshops/src/index.org
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ target/geom-meshops-<<conf-version()>>.js
<<dep-cljs>>
[thi.ng/geom-core "<<conf-version()>>"]
[thi.ng/geom-types "<<conf-version()>>"]
<<dep-strf>>
<<dep-dstruct>>
<<dep-xerror>>]

:profiles {:dev {:dependencies [<<dep-criterium>>]
Expand Down
209 changes: 97 additions & 112 deletions geom-meshops/src/meshio.org
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

* Contents :toc_4_gh:
- [[#thinggeommeshio][thi.ng.geom.mesh.io]]
- [[#todo-make-cljs-compatible][TODO make CLJS compatible]]
- [[#configuration-parameters][Configuration parameters]]
- [[#stl][STL]]
- [[#ply][PLY]]
- [[#todo-obj][TODO OBJ]]
- [[#obj][OBJ]]
- [[#off][OFF]]
- [[#complete-namespace-definition][Complete namespace definition]]

* thi.ng.geom.mesh.io

** TODO make CLJS compatible
** Configuration parameters

#+BEGIN_SRC clojure :noweb-ref config
Expand All @@ -25,32 +23,32 @@
(defn write-stl
"Writes the given mesh as binary STL to output stream. Mesh is
automatically tessellated into triangles."
[^OutputStream stream mesh]
(let [faces (g/faces mesh)
[out mesh]
(let [faces (g/faces mesh)
fnormals (g/face-normals mesh true)]
(.write stream #?(:clj (byte-array 80) :cljs (into-array (repeat 80 0))))
(b/write-int32-le stream (count faces))
(streams/skip out 80)
(streams/write-uint32-le out (count faces))
(doseq [[a b c :as f] faces]
(b/write-vec3-le stream (or (get fnormals f) (gu/ortho-normal f)))
(b/write-vec3-le stream a)
(b/write-vec3-le stream b)
(b/write-vec3-le stream c)
(b/write-int16-le stream 0))
stream))
(streams/write-vec3f-le out (or (get fnormals f) (gu/ortho-normal f)))
(streams/write-vec3f-le out a)
(streams/write-vec3f-le out b)
(streams/write-vec3f-le out c)
(streams/write-uint16-le out 0))
out))

(defn read-stl
"Reads faces from binary STL inputstream and adds them to given (or new) mesh."
([stream]
(read-stl stream (bm/basic-mesh)))
([^InputStream stream mesh]
(.read stream #?(:clj (byte-array 80) :cljs (make-array 80)))
(loop [mesh mesh, i (b/read-int32-le stream)]
([in]
(read-stl in (bm/basic-mesh)))
([in mesh]
(streams/skip in 80)
(loop [mesh mesh, i (streams/read-uint32-le in)]
(if (pos? i)
(let [_ (b/read-vec3-le stream)
a (vec3 (b/read-vec3-le stream))
b (vec3 (b/read-vec3-le stream))
c (vec3 (b/read-vec3-le stream))
_ (b/read-int16-le stream)]
(let [_ (streams/read-vec3f-le in)
a (vec3 (streams/read-vec3f-le in))
b (vec3 (streams/read-vec3f-le in))
c (vec3 (streams/read-vec3f-le in))]
(streams/skip in 2)
(recur (g/add-face mesh [a b c]) (dec i)))
mesh))))
#+END_SRC
Expand All @@ -62,114 +60,99 @@
"Writes the given mesh as binary PLY to output stream. For
compatibility with external tools mesh should already have been
tessellated before calling this fn."
[^OutputStream stream mesh]
(let [vertices (g/vertices mesh)
vindex (zipmap vertices (range))
vnormals (g/vertex-normals mesh false)
faces (g/faces mesh)
^bytes fhead #?(:clj (byte-array 1) :cljs (make-array 1))
[out mesh]
(let [vertices (g/vertices mesh)
vindex (zipmap vertices (range))
vnormals (g/vertex-normals mesh false)
faces (g/faces mesh)
vnorms? (not (nil? (seq vnormals)))
write-props (fn [props]
(doseq [p props]
(b/write-str-bytes
stream (str "property float32 " p "\n"))))
write-vindex (fn [face]
(doseq [v face]
(b/write-int32-le stream (get vindex v))))]
(doto stream
(b/write-str-bytes "ply\n")
(b/write-str-bytes "format binary_little_endian 1.0\n")
(b/write-str-bytes (str "element vertex " (count vertices) "\n")))
(streams/write-utf8-bytes
out (str "property float32 " p "\n"))))]
(-> out
(streams/write-utf8-bytes "ply\n")
(streams/write-utf8-bytes "format binary_little_endian 1.0\n")
(streams/write-utf8-bytes (str "element vertex " (count vertices) "\n")))
(write-props ['x 'y 'z])
(when (seq vnormals) (write-props ['nx 'ny 'nz]))
(doto stream
(b/write-str-bytes (str "element face " (count faces) "\n"))
(b/write-str-bytes "property list uint8 uint32 vertex_indices\n")
(b/write-str-bytes "end_header\n"))
(doseq [v vertices]
(b/write-vec3-le stream v)
(when (seq vnormals)
(b/write-vec3-le stream (get vnormals v))))
(when vnorms?
(write-props ['nx 'ny 'nz]))
(-> out
(streams/write-utf8-bytes (str "element face " (count faces) "\n"))
(streams/write-utf8-bytes "property list uint8 uint32 vertex_indices\n")
(streams/write-utf8-bytes "end_header\n"))
(if vnorms?
(doseq [v vertices]
(streams/write-vec3f-le out v)
(streams/write-vec3f-le out (get vnormals v)))
(doseq [v vertices]
(streams/write-vec3f-le out v)))
(doseq [f (g/faces mesh)]
(aset fhead 0 (byte (count f)))
(.write stream fhead)
(write-vindex f))
stream))
(streams/write-uint8 out (unchecked-byte (count f)))
(doseq [v f]
(streams/write-uint32-le out (get vindex v))))
out))
#+END_SRC

** TODO OBJ
- Note taken on [2015-03-15 Sun 01:49] \\
update to use protocols
** OBJ

#+BEGIN_SRC clojure :noweb-ref obj
(defn write-obj
[^OutputStream stream {:keys [vertices vnormals faces]}]
(let [vertices (keys vertices)
has-vnormals? (not (nil? (seq vnormals)))
fmt-fn (fn [prefix]
#?(:clj
(let [ff (str "%1." *precision* "f ")
ff (str prefix " " ff ff ff "\n")]
#(format ff (double %1) (double %2) (double %3)))
:cljs
#(str
prefix " "
(.toFixed (js/Number. %1) *precision*) " "
(.toFixed (js/Number. %2) *precision*) " "
(.toFixed (js/Number. %3) *precision*) "\n")))
fmt-vertex (fmt-fn "v")
[out {:keys [vertices vnormals faces]}]
(let [vertices (keys vertices)
vnorms? (not (nil? (seq vnormals)))
ff (f/float *precision*)
fmt-fn (fn [prefix]
(let [fmt [prefix " " ff " " ff " " ff "\n"]]
#(f/format fmt (double %1) (double %2) (double %3))))
fmt-vertex (fmt-fn "v")
fmt-vnormal (fmt-fn "vn")
fmt-fvert (if has-vnormals? #(str % "//" %2 " ") #(str % " "))
fmt-face (if has-vnormals?
(fn [verts normals]
(apply str (concat "f " (map fmt-fvert verts normals)) "\n"))
(fn [verts]
(apply str (concat "f " (map fmt-fvert verts)) "\n")))
vindex (zipmap vertices (range))
nindex (zipmap (vals vnormals) (range))]
fmt-face (if vnorms?
(fn [verts normals]
(str "f " (str/join " " (map #(str % "//" %2) verts normals)) "\n"))
(fn [verts]
(str "f " (str/join " " verts) "\n")))
vindex (zipmap vertices (range))
nindex (zipmap (vals vnormals) (range))]
(doseq [[x y z] vertices]
(b/write-str-bytes
stream (fmt-vertex x y z)))
(streams/write-utf8-bytes
out (fmt-vertex x y z)))
(doseq [[x y z] (vals vnormals)]
(b/write-str-bytes stream (fmt-vnormal x y z)))
(b/write-str-bytes stream "g\n")
(doseq [fverts faces]
(b/write-str-bytes
stream
(if has-vnormals?
(fmt-face
(map #(inc (get vindex %)) fverts)
(map #(inc (get nindex (get vnormals %))) fverts))
(fmt-face (map #(inc (get vindex %)) fverts)))))
stream))
(streams/write-utf8-bytes out (fmt-vnormal x y z)))
(streams/write-utf8-bytes out "g\n")
(if vnorms?
(doseq [fverts faces]
(streams/write-utf8-bytes
out (fmt-face
(map #(inc (get vindex %)) fverts)
(map #(inc (get nindex (get vnormals %))) fverts))))
(doseq [fverts faces]
(streams/write-utf8-bytes
out (fmt-face (map #(inc (get vindex %)) fverts)))))
out))
#+END_SRC

** OFF

#+BEGIN_SRC clojure :noweb-ref off
(defn write-off
[^OutputStream stream mesh]
(let [vertices (g/vertices mesh)
faces (g/faces mesh)
fmt-vertex
#?(:clj
(let [ff (str "%1." *precision* "f ")
ff (str ff ff ff "\n")]
#(format ff (double %1) (double %2) (double %3)))
:cljs
#(str (.toFixed (js/Number. %1) *precision*) " "
(.toFixed (js/Number. %2) *precision*) " "
(.toFixed (js/Number. %3) *precision*) "\n"))
vindex (zipmap vertices (range))]
(b/write-str-bytes stream "OFF\n")
(b/write-str-bytes stream (str (count vertices) " " (count faces) " 0\n"))
[out mesh]
(let [vertices (g/vertices mesh)
faces (g/faces mesh)
vindex (zipmap vertices (range))
fmt-float (f/float *precision*)
fmt-vertex [fmt-float " " fmt-float " " fmt-float "\n"]]
(streams/write-utf8-bytes out "OFF\n")
(streams/write-utf8-bytes out (str (count vertices) " " (count faces) " 0\n"))
(doseq [[x y z] vertices]
(b/write-str-bytes
stream (fmt-vertex x y z)))
(streams/write-utf8-bytes
out (f/format fmt-vertex (double x) (double y) (double z))))
(doseq [fverts faces]
(b/write-str-bytes
stream
(->> (concat [(count fverts)] " " (map #(str (vindex %) " ") fverts) "\n")
(apply str))))))
(streams/write-utf8-bytes
out
(str (count fverts) " "
(str/join " " (map vindex fverts))
"\n")))))
#+END_SRC

** Complete namespace definition
Expand All @@ -182,7 +165,9 @@
[thi.ng.geom.core.vector :as v :refer [vec3]]
[thi.ng.geom.basicmesh :as bm]
[thi.ng.geom.triangle :as t]
[thi.ng.common.data.byteutils :as b])
[thi.ng.dstruct.streams :as streams]
[thi.ng.strf.core :as f]
[clojure.string :as str])
#?(:clj
(:import
[java.io OutputStream InputStream])))
Expand Down
2 changes: 1 addition & 1 deletion src/config.org
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ target/test-classes
**** [[https://github.com/thi-ng/dstruct][thi.ng/dstruct]]
#+NAME: dep-dstruct
#+BEGIN_SRC clojure
[thi.ng/dstruct "0.1.1"]
[thi.ng/dstruct "0.1.2-SNAPSHOT"]
#+END_SRC

**** [[https://github.com/thi-ng/strf][thi.ng/strf]]
Expand Down

0 comments on commit 265de8e

Please sign in to comment.