# HG changeset patch # User Robert McIntyre # Date 1329052890 25200 # Node ID f27c9fd9134dcce3d484d319ae06da731a7974b1 # Parent b7762699eeb59a809d359fd17166434b5c644957 seperated out image generating code from touch-kernel diff -r b7762699eeb5 -r f27c9fd9134d org/touch.org --- a/org/touch.org Sat Feb 11 19:48:32 2012 -0700 +++ b/org/touch.org Sun Feb 12 06:21:30 2012 -0700 @@ -60,12 +60,106 @@ [#^Geometry obj] (if-let [image-path (meta-data obj "touch")] (load-image image-path))) + +(defn tactile-scale + "Return the maximum length of a hair. All hairs are scalled between + 0.0 and this length, depending on their color. Black is 0, and + white is maximum length, and everything in between is scalled + linearlly. Default scale is 0.01 jMonkeyEngine units." + [#^Geometry obj] + (if-let [scale (meta-data obj "scale")] + scale 0.1)) #+end_src - ** TODO add image showing example touch-uv map ** TODO add metadata display for worm +* Skin Creation +#+name: kernel +#+begin_src clojure +(in-ns 'cortex.touch) + +(defn touch-kernel + "Returns a function which returns tactile sensory data when called + inside a running simulation." + [#^Geometry geo] + (let [feeler-coords (feeler-coordinates geo) + tris (triangles geo) + limit (tactile-scale geo)] + (if (empty? (touch-topology geo)) + nil + (fn [node] + (let [sensor-origins + (map + #(map (partial local-to-world geo) %) + feeler-coords) + triangle-normals + (map (partial get-ray-direction geo) + tris) + rays + (flatten + (map (fn [origins norm] + (map #(doto (Ray. % norm) + (.setLimit limit)) origins)) + sensor-origins triangle-normals))] + (vector + (touch-topology geo) + (vec + (for [ray rays] + (do + (let [results (CollisionResults.)] + (.collideWith node ray results) + (let [touch-objects + (filter #(not (= geo (.getGeometry %))) + results)] + [(if (empty? touch-objects) + limit (.getDistance (first touch-objects))) + limit]))))))))))) + +(defn touch! + "Endow the creature with the sense of touch. Returns a sequence of + functions, one for each body part with a tactile-sensor-proile, + each of which when called returns sensory data for that body part." + [#^Node creature] + (filter + (comp not nil?) + (map touch-kernel + (filter #(isa? (class %) Geometry) + (node-seq creature))))) +#+end_src + +* Visualizing Touch +#+name: visualization +#+begin_src clojure +(in-ns 'cortex.touch) + +(defn touch->gray + "Convert a pair of [distance, max-distance] into a grayscale pixel" + [distance max-distance] + (gray + (- 255 + (rem + (int + (* 255 (/ distance max-distance))) + 256)))) + +(defn view-touch + "Creates a function which accepts a list of touch sensor-data and + displays each element to the screen." + [] + (view-sense + (fn + [[coords sensor-data]] + (let [image (points->image coords)] + (dorun + (for [i (range (count coords))] + (.setRGB image ((coords i) 0) ((coords i) 1) + (apply touch->gray (sensor-data i))))) + image)))) +#+end_src + + + * Triangle Manipulation Functions The rigid bodies which make up a creature have an underlying @@ -341,81 +435,6 @@ #+end_src -* Skin Creation -#+name: kernel -#+begin_src clojure -(defn touch-fn - "Returns a function which returns tactile sensory data when called - inside a running simulation." - [#^Geometry geo] - (let [feeler-coords (feeler-coordinates geo) - tris (triangles geo) - limit 0.1 - ;;results (CollisionResults.) - ] - (if (empty? (touch-topology geo)) - nil - (fn [node] - (let [sensor-origins - (map - #(map (partial local-to-world geo) %) - feeler-coords) - triangle-normals - (map (partial get-ray-direction geo) - tris) - rays - (flatten - (map (fn [origins norm] - (map #(doto (Ray. % norm) - (.setLimit limit)) origins)) - sensor-origins triangle-normals))] - (vector - (touch-topology geo) - (vec - (for [ray rays] - (do - (let [results (CollisionResults.)] - (.collideWith node ray results) - (let [touch-objects - (filter #(not (= geo (.getGeometry %))) - results)] - (- 255 - (if (empty? touch-objects) 255 - (rem - (int - (* 255 (/ (.getDistance - (first touch-objects)) limit))) - 256)))))))))))))) - -(defn touch! - "Endow the creature with the sense of touch. Returns a sequence of - functions, one for each body part with a tactile-sensor-proile, - each of which when called returns sensory data for that body part." - [#^Node creature] - (filter - (comp not nil?) - (map touch-fn - (filter #(isa? (class %) Geometry) - (node-seq creature))))) -#+end_src - -* Visualizing Touch -#+name: visualization -#+begin_src clojure -(defn view-touch - "Creates a function which accepts a list of touch sensor-data and - displays each element to the screen." - [] - (view-sense - (fn - [[coords sensor-data]] - (let [image (points->image coords)] - (dorun - (for [i (range (count coords))] - (.setRGB image ((coords i) 0) ((coords i) 1) - (gray (sensor-data i))))) - image)))) -#+end_src * Headers @@ -458,7 +477,6 @@ (fn [world tpf] (touch-display (map #(% (.getRootNode world)) touch)))))) - #+end_src * Source Listing