-
Notifications
You must be signed in to change notification settings - Fork 79
/
polyhedra.cljc
150 lines (140 loc) · 4.46 KB
/
polyhedra.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(ns thi.ng.geom.polyhedra
#?(:cljs (:require-macros [thi.ng.math.macros :as mm]))
(:require
[thi.ng.geom.core :as g]
[thi.ng.geom.attribs :as attr]
[thi.ng.geom.vector :as v :refer [vec3]]
[thi.ng.geom.basicmesh :as bm]
[thi.ng.geom.gmesh :as gm]
[thi.ng.math.core :as m :refer [PHI PI SQRT2 SQRT3]]
[thi.ng.dstruct.core :as d]
#?(:clj [thi.ng.math.macros :as mm])))
;; ** Polyhedra primitives / Platonic Solids
;;
;; http://paulbourke.net/geometry/platonic/
;;
;; *** Polyhedron mesh constructor
;;
;; The `polyhedron-mesh` function acts as a generic HOF constructor of
;; mesh instances for the polyhedra defined below. It takes a
;; polyhedron face list generator, mesh scale and optional subdivision
;; scheme and returns either a `BasicMesh` or `GMesh` instance.
;;
;; ```
;; (require '[thi.ng.geom.core :as g])
;; (require '[thi.ng.geom.mesh.polyhedra :as poly])
;; (require '[thi.ng.geom.mesh.subdivision :as sd])
;; (require '[thi.ng.geom.mesh.io :as mio])
;; (require '[clojure.java.io :as io])
;;
;; ; create icosahedron sphere (3x subdivided)
;; (with-open [o (io/output-stream "icosphere.stl")]
;; (->> (poly/polyhedron-mesh poly/icosahedron sd/catmull-clark 1 3)
;; (g/tessellate)
;; (mio/write-stl (mio/wrapped-output-stream o))))
;; ```
;;
;; Result: http://media.thi.ng/geom/mesh/icosphere.svg
(defn polyhedron-mesh
([f] (polyhedron-mesh f nil))
([f {:keys [scale subdiv iter attribs mesh]}]
(let [faces (f (or scale 1.0) attribs)]
(if subdiv
(let [m (d/iterate-n (or iter 1) subdiv (g/into (gm/gmesh) faces))]
(if mesh (g/into mesh m) m))
(g/into (or mesh (bm/basic-mesh)) faces)))))
(defn tetrahedron-vertices
[scale]
(let [p (/ SQRT3 3.0)
q (/ p -2.0)
r (/ (Math/sqrt 6) 6.0), r' (- r)]
(map #(g/scale % scale)
[(vec3 p 0 r')
(vec3 q -0.5 r')
(vec3 q 0.5 r')
(vec3 0 0 r)])))
(defn tetrahedron
[scale attribs]
(let [[a b c d] (tetrahedron-vertices scale)]
(map-indexed
(fn [i fverts] (attr/generate-face-attribs fverts i attribs nil))
[[a b c] [a c d] [a d b] [c b d]])))
(defn octahedron-vertices
[scale]
(let [p (/ (* 2.0 SQRT2)), p' (- p)
q 0.5, q' (- q)]
(map #(m/normalize % scale)
[(vec3 p' 0 p)
(vec3 p 0 p)
(vec3 p 0 p')
(vec3 p' 0 p')
(vec3 0 q 0)
(vec3 0 q' 0)])))
(defn octahedron
[scale attribs]
(let [[a b c d e f] (octahedron-vertices scale)]
(map-indexed
(fn [i fverts] (attr/generate-face-attribs fverts i attribs nil))
[[d a e] [c d e] [b c e] [a b e]
[d c f] [a d f] [c b f] [b a f]])))
(defn icosahedron-vertices
[scale]
(let [p 0.5, p' (- p)
q (/ (* 2 PHI)), q' (- q)]
(map #(m/normalize % scale)
[(vec3 0 q p')
(vec3 q p 0)
(vec3 q' p 0)
(vec3 0 q p)
(vec3 0 q' p)
(vec3 p' 0 q)
(vec3 p 0 q)
(vec3 0 q' p')
(vec3 p 0 q')
(vec3 p' 0 q')
(vec3 q p' 0)
(vec3 q' p' 0)])))
(defn icosahedron
[scale attribs]
(let [[a b c d e f g h i j k l] (icosahedron-vertices scale)]
(map-indexed
(fn [i fverts] (attr/generate-face-attribs fverts i attribs nil))
[[b a c] [c d b] [e d f] [g d e]
[h a i] [j a h] [k e l] [l h k]
[f c j] [j l f] [i b g] [g k i]
[f d c] [b d g] [c a j] [i a b]
[j h l] [k h i] [l e f] [g e k]])))
(defn dodecahedron-vertices
[scale]
(let [p 0.5, p' (- p)
q (/ 0.5 PHI), q' (- q)
r (* 0.5 (- 2 PHI)), r' (- r)]
(map #(m/normalize % scale)
[(vec3 r 0 p)
(vec3 r' 0 p)
(vec3 q' q q)
(vec3 0 p r)
(vec3 q q q)
(vec3 q q' q)
(vec3 0 p' r)
(vec3 q' q' q)
(vec3 r 0 p')
(vec3 r' 0 p')
(vec3 q' q' q')
(vec3 0 p' r')
(vec3 q q' q')
(vec3 q q q')
(vec3 0 p r')
(vec3 q' q q')
(vec3 p r 0)
(vec3 p' r 0)
(vec3 p' r' 0)
(vec3 p r' 0)])))
(defn dodecahedron
[scale attribs]
(let [[a b c d e f g h i j k l m n o p q r s t] (dodecahedron-vertices scale)]
(map-indexed
(fn [i fverts] (attr/generate-face-attribs fverts i attribs nil))
[[e d c b a] [h g f a b] [m l k j i] [p o n i j]
[o d e q n] [d o p r c] [l g h s k] [g l m t f]
[e a f t q] [m i n q t] [p j k s r] [h b c r s]])))