changeset 178:6fba17a74a57

refactored touch
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 07:05:54 -0700
parents 5af4ebe72b97
children 11bd5f0625ad
files org/test-creature.org org/touch.org
diffstat 2 files changed, 33 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Sat Feb 04 06:54:14 2012 -0700
     1.2 +++ b/org/test-creature.org	Sat Feb 04 07:05:54 2012 -0700
     1.3 @@ -172,7 +172,7 @@
     1.4          creature (doto 
     1.5                       (load-blender-model thing)
     1.6                     (body!))
     1.7 -        touch-nerves (touch creature)
     1.8 +        touch-nerves (touch! creature)
     1.9          touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)
    1.10          vision-data (vision! creature)
    1.11          vision-debug (map (fn [_] (debug-vision-window)) vision-data)
     2.1 --- a/org/touch.org	Sat Feb 04 06:54:14 2012 -0700
     2.2 +++ b/org/touch.org	Sat Feb 04 07:05:54 2012 -0700
     2.3 @@ -6,7 +6,6 @@
     2.4  #+SETUPFILE: ../../aurellem/org/setup.org
     2.5  #+INCLUDE: ../../aurellem/org/level-0.org
     2.6  
     2.7 -
     2.8  * Touch
     2.9  
    2.10  My creatures need to be able to feel their environments. The idea here
    2.11 @@ -26,12 +25,12 @@
    2.12    Geometry to know what parts of itself are touching nearby objects."
    2.13    {:author "Robert McIntyre"}
    2.14    (:use (cortex world util sense))
    2.15 +  (:use clojure.contrib.def)
    2.16    (:import com.jme3.scene.Geometry)
    2.17    (:import com.jme3.collision.CollisionResults)
    2.18    (:import jme3tools.converters.ImageToAwt)
    2.19    (:import (com.jme3.math Triangle Vector3f Ray)))
    2.20     
    2.21 -(use 'clojure.contrib.def)
    2.22  (cortex.import/mega-import-jme3)
    2.23  
    2.24  (defn triangles
    2.25 @@ -75,19 +74,12 @@
    2.26  ;; Every Mesh has many triangles, each with its own index.
    2.27  ;; Every vertex has its own index as well.
    2.28  
    2.29 -(defn tactile-sensor-image
    2.30 +(defn tactile-sensor-profile
    2.31    "Return the touch-sensor distribution image in BufferedImage format,
    2.32     or nil if it does not exist."
    2.33    [#^Geometry obj]
    2.34    (if-let [image-path (meta-data obj "touch")]
    2.35 -    (ImageToAwt/convert
    2.36 -     (.getImage
    2.37 -      (.loadTexture
    2.38 -       (asset-manager)
    2.39 -       image-path))
    2.40 -    false false 0)))
    2.41 -     
    2.42 -
    2.43 +    (load-image image-path)))
    2.44  
    2.45  (defn triangle
    2.46    "Get the triangle specified by triangle-index from the mesh within
    2.47 @@ -199,11 +191,8 @@
    2.48                (take 3 points))))
    2.49  
    2.50  (defn convex-bounds
    2.51 -  ;;dylan
    2.52 -  "Returns the smallest square containing the given
    2.53 -vertices, as a vector of integers [left top width height]."
    2.54 - ;; "Dimensions of the smallest integer bounding square of the list of
    2.55 - ;;  2D verticies in the form: [x y width height]."
    2.56 +  "Returns the smallest square containing the given vertices, as a
    2.57 +   vector of integers [left top width height]."
    2.58    [uv-verts]
    2.59    (let [xs (map first uv-verts)
    2.60          ys (map second uv-verts)
    2.61 @@ -214,10 +203,8 @@
    2.62      [x0 y0 (- x1 x0) (- y1 y0)]))
    2.63  
    2.64  (defn sensors-in-triangle
    2.65 -  ;;dylan
    2.66 -  "Locate the touch sensors in the triangle, returning a map of their UV and geometry-relative coordinates."
    2.67 -  ;;"Find the locations of the touch sensors within a triangle in both
    2.68 -  ;; UV and gemoetry relative coordinates."
    2.69 +  "Locate the touch sensors in the triangle, returning a map of their
    2.70 +   UV and geometry-relative coordinates."
    2.71    [image mesh tri-index]
    2.72    (let [width (.getWidth image)
    2.73          height (.getHeight image)
    2.74 @@ -238,24 +225,33 @@
    2.75    {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
    2.76  
    2.77  (defn-memo locate-feelers
    2.78 -  "Search the geometry's tactile UV image for touch sensors, returning
    2.79 -  their positions in geometry-relative coordinates."
    2.80 +  "Search the geometry's tactile UV profile for touch sensors,
    2.81 +   returning their positions in geometry-relative coordinates."
    2.82    [#^Geometry geo]
    2.83    (let [mesh (.getMesh geo)
    2.84          num-triangles (.getTriangleCount mesh)]
    2.85 -    (if-let [image (tactile-sensor-image geo)]
    2.86 +    (if-let [image (tactile-sensor-profile geo)]
    2.87        (map
    2.88         (partial sensors-in-triangle image mesh)
    2.89         (range num-triangles))
    2.90        (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
    2.91  
    2.92 -(defn-memo touch-topology [#^Gemoetry geo]
    2.93 +(defn-memo touch-topology
    2.94 +  "Return a sequence of vectors of the form [x y] describing the
    2.95 +   \"topology\" of the tactile sensors. Points that are close together
    2.96 +   in the touch-topology are generally close together in the simulation."
    2.97 +  [#^Gemoetry geo]
    2.98    (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
    2.99  
   2.100 -(defn-memo feeler-coordinates [#^Geometry geo]
   2.101 +(defn-memo feeler-coordinates
   2.102 +  "The location of the touch sensors in world-space coordinates."
   2.103 +  [#^Geometry geo]
   2.104    (vec (map :geometry (locate-feelers geo))))
   2.105  
   2.106 -(defn enable-touch [#^Geometry geo]
   2.107 +(defn touch-fn
   2.108 +  "Returns a function which returns tactile sensory data when called
   2.109 +   inside a running simulation."
   2.110 +  [#^Geometry geo]
   2.111    (let [feeler-coords (feeler-coordinates geo)
   2.112          tris (triangles geo)
   2.113          limit 0.1
   2.114 @@ -294,13 +290,17 @@
   2.115                               (* 255 (/ (.getDistance
   2.116                                          (first touch-objects)) limit)))
   2.117                              256))))))))))))))
   2.118 -                         
   2.119    
   2.120 -(defn touch [#^Node pieces]
   2.121 -  (filter (comp not nil?)
   2.122 -          (map enable-touch
   2.123 -               (filter #(isa? (class %) Geometry)
   2.124 -                       (node-seq pieces)))))
   2.125 +(defn touch! 
   2.126 +  "Endow the creature with the sense of touch. Returns a sequence of
   2.127 +   functions, one for each body part with a tactile-sensor-proile,
   2.128 +   each of which when called returns sensory data for that body part."
   2.129 +  [#^Node creature]
   2.130 +  (filter
   2.131 +   (comp not nil?)
   2.132 +   (map touch-fn
   2.133 +        (filter #(isa? (class %) Geometry)
   2.134 +                (node-seq creature)))))
   2.135  
   2.136  
   2.137  #+end_src