Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Last active August 29, 2015 13:57
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 postspectacular/9877462 to your computer and use it in GitHub Desktop.
Save postspectacular/9877462 to your computer and use it in GitHub Desktop.
3D point-at demo using alignment quaternion. this example requires thi.ng/geom "0.3.0-SNAPSHOT" or later.
(ns pointatdemo
(:require
[thi.ng.geom.core :as g]
[thi.ng.geom.tetrahedron :as t]
[thi.ng.geom.meshio :as mio]
[clojure.java.io :as io]))
(defn point-at
"Takes an entity implementing PTransform, a reference point and two
direction vectors. Transforms the entity in such a way that the 1st
direction is aligned with the 2nd. Rotation is applied locally
around reference point, which is used a center of rotation. If no
such point is given, the centroid of the entity is used. The
direction vectors will be normalized automatically."
([transformable from to]
(point-at transformable (g/centroid transformable) from to))
([transformable refp from to]
(let [q (g/alignment-quat (g/normalize from) (g/normalize to))
m (->> (g/translate g/M44 (g/- refp))
(g/* (g/as-matrix q))
(g/* (g/translate g/M44 refp)))]
(g/transform transformable m))))
(def a (t/tetrahedron [0 0] [10 10] [20 0] [10 0 50]))
(with-open [o (io/output-stream "pointat-with-refp.ply")]
(->> (point-at a (get-in a [:points 1]) g/V3Z g/V3X) ;; compute transformed
(g/as-mesh) ;; convert to mesh
(g/into-mesh (g/as-mesh a)) ;; combine w/ original
(mio/write-ply o))) ;; write as PLY format
(with-open [o (io/output-stream "pointat-with-centroid.ply")]
(->> (point-at a g/V3Z g/V3X) ;; compute transformed
(g/as-mesh) ;; convert to mesh
(g/into-mesh (g/as-mesh a)) ;; combine w/ original
(mio/write-ply o))) ;; write as PLY format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment