diff org/touch.org @ 178:6fba17a74a57

refactored touch
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 07:05:54 -0700
parents 5af4ebe72b97
children 11bd5f0625ad
line wrap: on
line diff
     1.1 --- a/org/touch.org	Sat Feb 04 06:54:14 2012 -0700
     1.2 +++ b/org/touch.org	Sat Feb 04 07:05:54 2012 -0700
     1.3 @@ -6,7 +6,6 @@
     1.4  #+SETUPFILE: ../../aurellem/org/setup.org
     1.5  #+INCLUDE: ../../aurellem/org/level-0.org
     1.6  
     1.7 -
     1.8  * Touch
     1.9  
    1.10  My creatures need to be able to feel their environments. The idea here
    1.11 @@ -26,12 +25,12 @@
    1.12    Geometry to know what parts of itself are touching nearby objects."
    1.13    {:author "Robert McIntyre"}
    1.14    (:use (cortex world util sense))
    1.15 +  (:use clojure.contrib.def)
    1.16    (:import com.jme3.scene.Geometry)
    1.17    (:import com.jme3.collision.CollisionResults)
    1.18    (:import jme3tools.converters.ImageToAwt)
    1.19    (:import (com.jme3.math Triangle Vector3f Ray)))
    1.20     
    1.21 -(use 'clojure.contrib.def)
    1.22  (cortex.import/mega-import-jme3)
    1.23  
    1.24  (defn triangles
    1.25 @@ -75,19 +74,12 @@
    1.26  ;; Every Mesh has many triangles, each with its own index.
    1.27  ;; Every vertex has its own index as well.
    1.28  
    1.29 -(defn tactile-sensor-image
    1.30 +(defn tactile-sensor-profile
    1.31    "Return the touch-sensor distribution image in BufferedImage format,
    1.32     or nil if it does not exist."
    1.33    [#^Geometry obj]
    1.34    (if-let [image-path (meta-data obj "touch")]
    1.35 -    (ImageToAwt/convert
    1.36 -     (.getImage
    1.37 -      (.loadTexture
    1.38 -       (asset-manager)
    1.39 -       image-path))
    1.40 -    false false 0)))
    1.41 -     
    1.42 -
    1.43 +    (load-image image-path)))
    1.44  
    1.45  (defn triangle
    1.46    "Get the triangle specified by triangle-index from the mesh within
    1.47 @@ -199,11 +191,8 @@
    1.48                (take 3 points))))
    1.49  
    1.50  (defn convex-bounds
    1.51 -  ;;dylan
    1.52 -  "Returns the smallest square containing the given
    1.53 -vertices, as a vector of integers [left top width height]."
    1.54 - ;; "Dimensions of the smallest integer bounding square of the list of
    1.55 - ;;  2D verticies in the form: [x y width height]."
    1.56 +  "Returns the smallest square containing the given vertices, as a
    1.57 +   vector of integers [left top width height]."
    1.58    [uv-verts]
    1.59    (let [xs (map first uv-verts)
    1.60          ys (map second uv-verts)
    1.61 @@ -214,10 +203,8 @@
    1.62      [x0 y0 (- x1 x0) (- y1 y0)]))
    1.63  
    1.64  (defn sensors-in-triangle
    1.65 -  ;;dylan
    1.66 -  "Locate the touch sensors in the triangle, returning a map of their UV and geometry-relative coordinates."
    1.67 -  ;;"Find the locations of the touch sensors within a triangle in both
    1.68 -  ;; UV and gemoetry relative coordinates."
    1.69 +  "Locate the touch sensors in the triangle, returning a map of their
    1.70 +   UV and geometry-relative coordinates."
    1.71    [image mesh tri-index]
    1.72    (let [width (.getWidth image)
    1.73          height (.getHeight image)
    1.74 @@ -238,24 +225,33 @@
    1.75    {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
    1.76  
    1.77  (defn-memo locate-feelers
    1.78 -  "Search the geometry's tactile UV image for touch sensors, returning
    1.79 -  their positions in geometry-relative coordinates."
    1.80 +  "Search the geometry's tactile UV profile for touch sensors,
    1.81 +   returning their positions in geometry-relative coordinates."
    1.82    [#^Geometry geo]
    1.83    (let [mesh (.getMesh geo)
    1.84          num-triangles (.getTriangleCount mesh)]
    1.85 -    (if-let [image (tactile-sensor-image geo)]
    1.86 +    (if-let [image (tactile-sensor-profile geo)]
    1.87        (map
    1.88         (partial sensors-in-triangle image mesh)
    1.89         (range num-triangles))
    1.90        (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
    1.91  
    1.92 -(defn-memo touch-topology [#^Gemoetry geo]
    1.93 +(defn-memo touch-topology
    1.94 +  "Return a sequence of vectors of the form [x y] describing the
    1.95 +   \"topology\" of the tactile sensors. Points that are close together
    1.96 +   in the touch-topology are generally close together in the simulation."
    1.97 +  [#^Gemoetry geo]
    1.98    (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
    1.99  
   1.100 -(defn-memo feeler-coordinates [#^Geometry geo]
   1.101 +(defn-memo feeler-coordinates
   1.102 +  "The location of the touch sensors in world-space coordinates."
   1.103 +  [#^Geometry geo]
   1.104    (vec (map :geometry (locate-feelers geo))))
   1.105  
   1.106 -(defn enable-touch [#^Geometry geo]
   1.107 +(defn touch-fn
   1.108 +  "Returns a function which returns tactile sensory data when called
   1.109 +   inside a running simulation."
   1.110 +  [#^Geometry geo]
   1.111    (let [feeler-coords (feeler-coordinates geo)
   1.112          tris (triangles geo)
   1.113          limit 0.1
   1.114 @@ -294,13 +290,17 @@
   1.115                               (* 255 (/ (.getDistance
   1.116                                          (first touch-objects)) limit)))
   1.117                              256))))))))))))))
   1.118 -                         
   1.119    
   1.120 -(defn touch [#^Node pieces]
   1.121 -  (filter (comp not nil?)
   1.122 -          (map enable-touch
   1.123 -               (filter #(isa? (class %) Geometry)
   1.124 -                       (node-seq pieces)))))
   1.125 +(defn touch! 
   1.126 +  "Endow the creature with the sense of touch. Returns a sequence of
   1.127 +   functions, one for each body part with a tactile-sensor-proile,
   1.128 +   each of which when called returns sensory data for that body part."
   1.129 +  [#^Node creature]
   1.130 +  (filter
   1.131 +   (comp not nil?)
   1.132 +   (map touch-fn
   1.133 +        (filter #(isa? (class %) Geometry)
   1.134 +                (node-seq creature)))))
   1.135  
   1.136  
   1.137  #+end_src