hollow sphere

This commit is contained in:
2017-05-13 21:31:35 -07:00
parent df87ba02d5
commit 2d10f00844

View File

@ -12,14 +12,8 @@
;;[thi.ng.geom.rect :as r] ;;[thi.ng.geom.rect :as r]
[thi.ng.geom.voxel.isosurface :as iso] [thi.ng.geom.voxel.isosurface :as iso]
[thi.ng.geom.voxel.svo :as svo] [thi.ng.geom.voxel.svo :as svo]
;;[thi.ng.luxor.core :refer :all]
;;[thi.ng.luxor.io :as lio]
[thi.ng.math.core :as m] [thi.ng.math.core :as m]
[clojure.math.numeric-tower :as math] [clojure.math.numeric-tower :as math]))
;;[termite.sim :as sim]
;;[termite.ui :as ui]
)
(:gen-class))
;;; ;;;
;;; geometry ;;; geometry
@ -31,6 +25,9 @@
(defn v-+ [v1 v2] (defn v-+ [v1 v2]
(mapv + v1 v2)) (mapv + v1 v2))
(defn v-- [v1 v2]
(mapv - v1 v2))
(defn round-point [v] (defn round-point [v]
(map math/round v)) (map math/round v))
@ -46,22 +43,28 @@
;;; trace ;;; trace
;;; ;;;
(def start-radius 3.0) (def start-radius 1.0)
(def start-lifespan 100) (def start-lifespan 120)
(def radius-step (/ start-radius start-lifespan)) (def radius-step 0 #_(/ start-radius start-lifespan))
(defrecord Trace [position velocity radius lifespan]) (defrecord Trace [position velocity radius lifespan start-time])
(defn new-trace [origin] (defn new-trace [origin]
(Trace. origin (random-vel) start-radius start-lifespan)) (Trace. origin (random-vel) start-radius start-lifespan (+ 70 (rand-int 10))))
(defn drawing? [t bounds]
(and (in-bounds? (:position t) bounds)
(> (:radius t) 0)
(> (:lifespan t) 0)
(< (:start-time t) 0)))
(defn update-trace [trace bounds] (defn update-trace [trace bounds]
(let [t (Trace. (next-pos (:position trace) (:velocity trace)) (let [t (Trace. (next-pos (:position trace) (:velocity trace))
(next-vel (:velocity trace)) (next-vel (:velocity trace))
(- (:radius trace) radius-step) (- (:radius trace) radius-step)
(dec (:lifespan trace)))] (dec (:lifespan trace))
(dec (:start-time trace)))]
(if (and (in-bounds? (:position t) bounds) (if (and (in-bounds? (:position t) bounds)
(> (:radius t) 0)
(> (:lifespan t) 0)) (> (:lifespan t) 0))
t t
(do #_(println "died at " (:lifespan t)) (do #_(println "died at " (:lifespan t))
@ -71,14 +74,14 @@
;;; physics ;;; physics
;;; ;;;
(def gravity [0 0 -0.1]) (def gravity [0 0 0])
(defn random-vel (defn random-vel
[] []
#_(mapv #(* 2 (- % 0.5)) (repeatedly 3 rand)) #_(mapv #(* 2 (- % 0.5)) (repeatedly 3 rand))
(vector (* 8 (- (rand) 0.5)) (vector (* 8 (- (rand) 0.5))
(* 8 (- (rand) 0.5)) (* 8 (- (rand) 0.5))
(* 12 (rand)))) (* 8 (- (rand) 0.5))))
(defn next-pos (defn next-pos
"Position at t+1" "Position at t+1"
@ -95,11 +98,27 @@
[v [lo hi]] [v [lo hi]]
(and (>= v lo) (<= v hi))) (and (>= v lo) (<= v hi)))
(defn in-bounds? #_(defn in-bounds?
"Is point inside the domain bounds?" "Inside bounds box?"
[p bounds] [p bounds]
(every? true? (map #(single-in-bounds? %1 %2) p bounds))) (every? true? (map #(single-in-bounds? %1 %2) p bounds)))
#_(defn complex-single-in-bounds?
"Is a single value in bounds?"
[v [lo hi]]
(and (>= v lo) (<= v hi)
(>= (math/abs v) (/ hi 5))))
#_(defn in-bounds?
"Inside complex box?"
[p bounds]
(every? true? (map #(complex-single-in-bounds? %1 %2) p bounds)))
(defn in-bounds?
"Inside sphere?"
[p bounds]
(<= (v-mag (v-- p [500 500 500])) 220))
(defn trace-trajectory (defn trace-trajectory
[trace bounds] [trace bounds]
(loop [traces [] (loop [traces []
@ -107,7 +126,7 @@
(let [new-trace (update-trace trace bounds)] (let [new-trace (update-trace trace bounds)]
(if (not new-trace) (if (not new-trace)
traces traces
(recur (conj traces trace) new-trace))))) (recur (if (drawing? new-trace bounds) (conj traces trace) traces) new-trace)))))
(defn trajectory-voxels [traces] (defn trajectory-voxels [traces]
(reduce set/union (map #(sphere (round-point (:position %)) (:radius %)) traces))) (reduce set/union (map #(sphere (round-point (:position %)) (:radius %)) traces)))
@ -134,11 +153,11 @@
trace (sample-traces count)] trace (sample-traces count)]
(svo/apply-voxels svo/set-at tree trace))) (svo/apply-voxels svo/set-at tree trace)))
(def sampletree (sample-tree 10)) ;;(def sampletree (sample-tree 10))
(defn sample-write-ply (defn sample-write-ply
[count] [count]
(time (time
(with-open [o (io/output-stream "sample.ply")] (with-open [o (io/output-stream "sample.ply")]
(let [tree #_sampletree (sample-tree count)] (let [tree #_sampletree (sample-tree count)]
(mio/write-ply o (g/tessellate (iso/surface-mesh tree 5 0.85))))))) (mio/write-ply o (g/tessellate (g/scale (iso/surface-mesh tree 5 0.9) 0.01)))))))