From 2d10f00844e98c87dea635748ab494edd2ac1096 Mon Sep 17 00:00:00 2001 From: Sasha Kovar Date: Sat, 13 May 2017 21:31:35 -0700 Subject: [PATCH] hollow sphere --- src/voxelburst/core.clj | 61 +++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/src/voxelburst/core.clj b/src/voxelburst/core.clj index b3db4fb..b93063d 100644 --- a/src/voxelburst/core.clj +++ b/src/voxelburst/core.clj @@ -12,14 +12,8 @@ ;;[thi.ng.geom.rect :as r] [thi.ng.geom.voxel.isosurface :as iso] [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] - [clojure.math.numeric-tower :as math] - ;;[termite.sim :as sim] - ;;[termite.ui :as ui] - ) - (:gen-class)) + [clojure.math.numeric-tower :as math])) ;;; ;;; geometry @@ -31,6 +25,9 @@ (defn v-+ [v1 v2] (mapv + v1 v2)) +(defn v-- [v1 v2] + (mapv - v1 v2)) + (defn round-point [v] (map math/round v)) @@ -46,22 +43,28 @@ ;;; trace ;;; -(def start-radius 3.0) -(def start-lifespan 100) -(def radius-step (/ start-radius start-lifespan)) +(def start-radius 1.0) +(def start-lifespan 120) +(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] - (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] (let [t (Trace. (next-pos (:position trace) (:velocity trace)) (next-vel (:velocity trace)) (- (:radius trace) radius-step) - (dec (:lifespan trace)))] + (dec (:lifespan trace)) + (dec (:start-time trace)))] (if (and (in-bounds? (:position t) bounds) - (> (:radius t) 0) (> (:lifespan t) 0)) t (do #_(println "died at " (:lifespan t)) @@ -71,14 +74,14 @@ ;;; physics ;;; -(def gravity [0 0 -0.1]) +(def gravity [0 0 0]) (defn random-vel [] #_(mapv #(* 2 (- % 0.5)) (repeatedly 3 rand)) (vector (* 8 (- (rand) 0.5)) (* 8 (- (rand) 0.5)) - (* 12 (rand)))) + (* 8 (- (rand) 0.5)))) (defn next-pos "Position at t+1" @@ -95,11 +98,27 @@ [v [lo hi]] (and (>= v lo) (<= v hi))) -(defn in-bounds? - "Is point inside the domain bounds?" +#_(defn in-bounds? + "Inside bounds box?" [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 [trace bounds] (loop [traces [] @@ -107,7 +126,7 @@ (let [new-trace (update-trace trace bounds)] (if (not new-trace) traces - (recur (conj traces trace) new-trace))))) + (recur (if (drawing? new-trace bounds) (conj traces trace) traces) new-trace))))) (defn trajectory-voxels [traces] (reduce set/union (map #(sphere (round-point (:position %)) (:radius %)) traces))) @@ -134,11 +153,11 @@ trace (sample-traces count)] (svo/apply-voxels svo/set-at tree trace))) -(def sampletree (sample-tree 10)) +;;(def sampletree (sample-tree 10)) (defn sample-write-ply [count] (time (with-open [o (io/output-stream "sample.ply")] (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)))))))