# HG changeset patch # User Robert McIntyre # Date 1328364354 25200 # Node ID 6fba17a74a570519e0c0d76277ef09f1ea7dde63 # Parent 5af4ebe72b9709bb2cd15d91abbaaf864e06e627 refactored touch diff -r 5af4ebe72b97 -r 6fba17a74a57 org/test-creature.org --- a/org/test-creature.org Sat Feb 04 06:54:14 2012 -0700 +++ b/org/test-creature.org Sat Feb 04 07:05:54 2012 -0700 @@ -172,7 +172,7 @@ creature (doto (load-blender-model thing) (body!)) - touch-nerves (touch creature) + touch-nerves (touch! creature) touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves) vision-data (vision! creature) vision-debug (map (fn [_] (debug-vision-window)) vision-data) diff -r 5af4ebe72b97 -r 6fba17a74a57 org/touch.org --- a/org/touch.org Sat Feb 04 06:54:14 2012 -0700 +++ b/org/touch.org Sat Feb 04 07:05:54 2012 -0700 @@ -6,7 +6,6 @@ #+SETUPFILE: ../../aurellem/org/setup.org #+INCLUDE: ../../aurellem/org/level-0.org - * Touch My creatures need to be able to feel their environments. The idea here @@ -26,12 +25,12 @@ Geometry to know what parts of itself are touching nearby objects." {:author "Robert McIntyre"} (:use (cortex world util sense)) + (:use clojure.contrib.def) (:import com.jme3.scene.Geometry) (:import com.jme3.collision.CollisionResults) (:import jme3tools.converters.ImageToAwt) (:import (com.jme3.math Triangle Vector3f Ray))) -(use 'clojure.contrib.def) (cortex.import/mega-import-jme3) (defn triangles @@ -75,19 +74,12 @@ ;; Every Mesh has many triangles, each with its own index. ;; Every vertex has its own index as well. -(defn tactile-sensor-image +(defn tactile-sensor-profile "Return the touch-sensor distribution image in BufferedImage format, or nil if it does not exist." [#^Geometry obj] (if-let [image-path (meta-data obj "touch")] - (ImageToAwt/convert - (.getImage - (.loadTexture - (asset-manager) - image-path)) - false false 0))) - - + (load-image image-path))) (defn triangle "Get the triangle specified by triangle-index from the mesh within @@ -199,11 +191,8 @@ (take 3 points)))) (defn convex-bounds - ;;dylan - "Returns the smallest square containing the given -vertices, as a vector of integers [left top width height]." - ;; "Dimensions of the smallest integer bounding square of the list of - ;; 2D verticies in the form: [x y width height]." + "Returns the smallest square containing the given vertices, as a + vector of integers [left top width height]." [uv-verts] (let [xs (map first uv-verts) ys (map second uv-verts) @@ -214,10 +203,8 @@ [x0 y0 (- x1 x0) (- y1 y0)])) (defn sensors-in-triangle - ;;dylan - "Locate the touch sensors in the triangle, returning a map of their UV and geometry-relative coordinates." - ;;"Find the locations of the touch sensors within a triangle in both - ;; UV and gemoetry relative coordinates." + "Locate the touch sensors in the triangle, returning a map of their + UV and geometry-relative coordinates." [image mesh tri-index] (let [width (.getWidth image) height (.getHeight image) @@ -238,24 +225,33 @@ {:UV UV-sensor-coords :geometry geometry-sensor-coords})) (defn-memo locate-feelers - "Search the geometry's tactile UV image for touch sensors, returning - their positions in geometry-relative coordinates." + "Search the geometry's tactile UV profile for touch sensors, + returning their positions in geometry-relative coordinates." [#^Geometry geo] (let [mesh (.getMesh geo) num-triangles (.getTriangleCount mesh)] - (if-let [image (tactile-sensor-image geo)] + (if-let [image (tactile-sensor-profile geo)] (map (partial sensors-in-triangle image mesh) (range num-triangles)) (repeat (.getTriangleCount mesh) {:UV nil :geometry nil})))) -(defn-memo touch-topology [#^Gemoetry geo] +(defn-memo touch-topology + "Return a sequence of vectors of the form [x y] describing the + \"topology\" of the tactile sensors. Points that are close together + in the touch-topology are generally close together in the simulation." + [#^Gemoetry geo] (vec (collapse (reduce concat (map :UV (locate-feelers geo)))))) -(defn-memo feeler-coordinates [#^Geometry geo] +(defn-memo feeler-coordinates + "The location of the touch sensors in world-space coordinates." + [#^Geometry geo] (vec (map :geometry (locate-feelers geo)))) -(defn enable-touch [#^Geometry geo] +(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 @@ -294,13 +290,17 @@ (* 255 (/ (.getDistance (first touch-objects)) limit))) 256)))))))))))))) - -(defn touch [#^Node pieces] - (filter (comp not nil?) - (map enable-touch - (filter #(isa? (class %) Geometry) - (node-seq pieces))))) +(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