Last active
March 4, 2016 21:24
-
-
Save postspectacular/513597a06799eda6782a to your computer and use it in GitHub Desktop.
T-Junction mesh repair for thi.ng/geom (0.0.908)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns tjunctions | |
(:require | |
[thi.ng.geom.core :as g] | |
[thi.ng.geom.core.utils :as gu] | |
[thi.ng.geom.line :as l] | |
[thi.ng.geom.gmesh :as gm] | |
[thi.ng.math.core :as m])) | |
(defn- edges-without-v | |
"Takes gmesh and vertex, returns lazyseq of all edges NOT related to v." | |
[m v] (filter #(not (get % v)) (g/edges m))) | |
(defn dist-to-edge | |
"Takes a vertex and edge, returns distance between v & e." | |
[v e] | |
(let [[a b] (seq e) | |
coeff (gu/closest-point-coeff v a b)] | |
(if (m/in-range? 1e-6 0.999999 coeff) | |
(g/dist v (g/mix a b coeff)) | |
m/INF+))) | |
(defn- t-junctions | |
"Takes gmesh and a vertex. Returns a vector, describing a T-junction | |
edge/face pair (or nil, if vertex is properly connected)" | |
[m v] | |
(let [edges (:edges m)] | |
(->> (edges-without-v m v) | |
(map (juxt identity (partial dist-to-edge v))) | |
(filter #(m/delta= (peek %) 0.0)) | |
(mapcat (fn [[e]] (map (fn [f] [e f]) (edges e)))) | |
set | |
first))) | |
(defn- split-tface | |
"Takes a vertex, edge and face (triangle) to be split. Returns | |
vector of 2 new faces." | |
[v e fverts] | |
(let [[a b] (seq e) | |
c (first (remove e fverts)) | |
[a b] (if (pos? (g/dot (gu/ortho-normal fverts) (gu/ortho-normal a v c))) [a b] [b a])] | |
[[a v c] [v b c]])) | |
(defn repair-tjunctions | |
"Takes a gmesh, finds all t-junctions and repairs them by | |
introducing new edges/faces. Returns updated mesh. Input mesh | |
should be pre-tessellated (triangles only)." | |
[m] | |
(reduce | |
(fn [m v] | |
(if-let [tj (t-junctions m v)] | |
(let [[e f] tj] | |
(g/into (g/remove-face m f) (split-tface v e f))) | |
m)) | |
m (g/vertices m))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment