Mercurial > cortex
diff org/touch.org @ 233:f27c9fd9134d
seperated out image generating code from touch-kernel
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 12 Feb 2012 06:21:30 -0700 |
parents | b7762699eeb5 |
children | 712bd7e5b148 |
line wrap: on
line diff
1.1 --- a/org/touch.org Sat Feb 11 19:48:32 2012 -0700 1.2 +++ b/org/touch.org Sun Feb 12 06:21:30 2012 -0700 1.3 @@ -60,12 +60,106 @@ 1.4 [#^Geometry obj] 1.5 (if-let [image-path (meta-data obj "touch")] 1.6 (load-image image-path))) 1.7 + 1.8 +(defn tactile-scale 1.9 + "Return the maximum length of a hair. All hairs are scalled between 1.10 + 0.0 and this length, depending on their color. Black is 0, and 1.11 + white is maximum length, and everything in between is scalled 1.12 + linearlly. Default scale is 0.01 jMonkeyEngine units." 1.13 + [#^Geometry obj] 1.14 + (if-let [scale (meta-data obj "scale")] 1.15 + scale 0.1)) 1.16 #+end_src 1.17 1.18 - 1.19 ** TODO add image showing example touch-uv map 1.20 ** TODO add metadata display for worm 1.21 1.22 +* Skin Creation 1.23 +#+name: kernel 1.24 +#+begin_src clojure 1.25 +(in-ns 'cortex.touch) 1.26 + 1.27 +(defn touch-kernel 1.28 + "Returns a function which returns tactile sensory data when called 1.29 + inside a running simulation." 1.30 + [#^Geometry geo] 1.31 + (let [feeler-coords (feeler-coordinates geo) 1.32 + tris (triangles geo) 1.33 + limit (tactile-scale geo)] 1.34 + (if (empty? (touch-topology geo)) 1.35 + nil 1.36 + (fn [node] 1.37 + (let [sensor-origins 1.38 + (map 1.39 + #(map (partial local-to-world geo) %) 1.40 + feeler-coords) 1.41 + triangle-normals 1.42 + (map (partial get-ray-direction geo) 1.43 + tris) 1.44 + rays 1.45 + (flatten 1.46 + (map (fn [origins norm] 1.47 + (map #(doto (Ray. % norm) 1.48 + (.setLimit limit)) origins)) 1.49 + sensor-origins triangle-normals))] 1.50 + (vector 1.51 + (touch-topology geo) 1.52 + (vec 1.53 + (for [ray rays] 1.54 + (do 1.55 + (let [results (CollisionResults.)] 1.56 + (.collideWith node ray results) 1.57 + (let [touch-objects 1.58 + (filter #(not (= geo (.getGeometry %))) 1.59 + results)] 1.60 + [(if (empty? touch-objects) 1.61 + limit (.getDistance (first touch-objects))) 1.62 + limit]))))))))))) 1.63 + 1.64 +(defn touch! 1.65 + "Endow the creature with the sense of touch. Returns a sequence of 1.66 + functions, one for each body part with a tactile-sensor-proile, 1.67 + each of which when called returns sensory data for that body part." 1.68 + [#^Node creature] 1.69 + (filter 1.70 + (comp not nil?) 1.71 + (map touch-kernel 1.72 + (filter #(isa? (class %) Geometry) 1.73 + (node-seq creature))))) 1.74 +#+end_src 1.75 + 1.76 +* Visualizing Touch 1.77 +#+name: visualization 1.78 +#+begin_src clojure 1.79 +(in-ns 'cortex.touch) 1.80 + 1.81 +(defn touch->gray 1.82 + "Convert a pair of [distance, max-distance] into a grayscale pixel" 1.83 + [distance max-distance] 1.84 + (gray 1.85 + (- 255 1.86 + (rem 1.87 + (int 1.88 + (* 255 (/ distance max-distance))) 1.89 + 256)))) 1.90 + 1.91 +(defn view-touch 1.92 + "Creates a function which accepts a list of touch sensor-data and 1.93 + displays each element to the screen." 1.94 + [] 1.95 + (view-sense 1.96 + (fn 1.97 + [[coords sensor-data]] 1.98 + (let [image (points->image coords)] 1.99 + (dorun 1.100 + (for [i (range (count coords))] 1.101 + (.setRGB image ((coords i) 0) ((coords i) 1) 1.102 + (apply touch->gray (sensor-data i))))) 1.103 + image)))) 1.104 +#+end_src 1.105 + 1.106 + 1.107 + 1.108 * Triangle Manipulation Functions 1.109 1.110 The rigid bodies which make up a creature have an underlying 1.111 @@ -341,81 +435,6 @@ 1.112 #+end_src 1.113 1.114 1.115 -* Skin Creation 1.116 -#+name: kernel 1.117 -#+begin_src clojure 1.118 -(defn touch-fn 1.119 - "Returns a function which returns tactile sensory data when called 1.120 - inside a running simulation." 1.121 - [#^Geometry geo] 1.122 - (let [feeler-coords (feeler-coordinates geo) 1.123 - tris (triangles geo) 1.124 - limit 0.1 1.125 - ;;results (CollisionResults.) 1.126 - ] 1.127 - (if (empty? (touch-topology geo)) 1.128 - nil 1.129 - (fn [node] 1.130 - (let [sensor-origins 1.131 - (map 1.132 - #(map (partial local-to-world geo) %) 1.133 - feeler-coords) 1.134 - triangle-normals 1.135 - (map (partial get-ray-direction geo) 1.136 - tris) 1.137 - rays 1.138 - (flatten 1.139 - (map (fn [origins norm] 1.140 - (map #(doto (Ray. % norm) 1.141 - (.setLimit limit)) origins)) 1.142 - sensor-origins triangle-normals))] 1.143 - (vector 1.144 - (touch-topology geo) 1.145 - (vec 1.146 - (for [ray rays] 1.147 - (do 1.148 - (let [results (CollisionResults.)] 1.149 - (.collideWith node ray results) 1.150 - (let [touch-objects 1.151 - (filter #(not (= geo (.getGeometry %))) 1.152 - results)] 1.153 - (- 255 1.154 - (if (empty? touch-objects) 255 1.155 - (rem 1.156 - (int 1.157 - (* 255 (/ (.getDistance 1.158 - (first touch-objects)) limit))) 1.159 - 256)))))))))))))) 1.160 - 1.161 -(defn touch! 1.162 - "Endow the creature with the sense of touch. Returns a sequence of 1.163 - functions, one for each body part with a tactile-sensor-proile, 1.164 - each of which when called returns sensory data for that body part." 1.165 - [#^Node creature] 1.166 - (filter 1.167 - (comp not nil?) 1.168 - (map touch-fn 1.169 - (filter #(isa? (class %) Geometry) 1.170 - (node-seq creature))))) 1.171 -#+end_src 1.172 - 1.173 -* Visualizing Touch 1.174 -#+name: visualization 1.175 -#+begin_src clojure 1.176 -(defn view-touch 1.177 - "Creates a function which accepts a list of touch sensor-data and 1.178 - displays each element to the screen." 1.179 - [] 1.180 - (view-sense 1.181 - (fn 1.182 - [[coords sensor-data]] 1.183 - (let [image (points->image coords)] 1.184 - (dorun 1.185 - (for [i (range (count coords))] 1.186 - (.setRGB image ((coords i) 0) ((coords i) 1) 1.187 - (gray (sensor-data i))))) 1.188 - image)))) 1.189 -#+end_src 1.190 1.191 * Headers 1.192 1.193 @@ -458,7 +477,6 @@ 1.194 1.195 (fn [world tpf] 1.196 (touch-display (map #(% (.getRootNode world)) touch)))))) 1.197 - 1.198 #+end_src 1.199 1.200 * Source Listing