changeset 249:95a9f6f1cb82

added touch-cube and fixed some problems with touch which it flushed out
author Robert McIntyre <rlm@mit.edu>
date Mon, 13 Feb 2012 06:06:02 -0700
parents 267add63b168
children e9bce4f722b1
files assets/Models/test-touch/touch-cube.blend assets/Models/test-touch/touch-guide.png assets/Models/test-touch/touch-profile.png assets/Models/test-touch/touch-profile.xcf images/touch-cube-1.png images/touch-profile.png org/ideas.org org/touch.org
diffstat 8 files changed, 111 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/test-touch/touch-cube.blend has changed
     2.1 Binary file assets/Models/test-touch/touch-guide.png has changed
     3.1 Binary file assets/Models/test-touch/touch-profile.png has changed
     4.1 Binary file assets/Models/test-touch/touch-profile.xcf has changed
     5.1 Binary file images/touch-cube-1.png has changed
     6.1 Binary file images/touch-profile.png has changed
     7.1 --- a/org/ideas.org	Sun Feb 12 16:00:31 2012 -0700
     7.2 +++ b/org/ideas.org	Mon Feb 13 06:06:02 2012 -0700
     7.3 @@ -75,6 +75,7 @@
     7.4   - [ ] fix videos that were encoded wrong, test on InterNet Explorer.
     7.5   - [ ] redo videos vision with new collapse code
     7.6   - [ ] find a topology that looks good. (maybe nil topology?)
     7.7 + - [ ] fix red part of touch cube in video and image
     7.8   - [ ] write summary of project for Winston            \
     7.9   - [ ] project proposals for Winston                    \
    7.10   - [ ] additional senses to be implemented for Winston   |  -- 2 days 
     8.1 --- a/org/touch.org	Sun Feb 12 16:00:31 2012 -0700
     8.2 +++ b/org/touch.org	Mon Feb 13 06:06:02 2012 -0700
     8.3 @@ -383,10 +383,13 @@
     8.4    ;; Doing everything locally recduces garbage collection by enough to
     8.5    ;; be worth it.
     8.6    (.mult transform origin (.getOrigin ray))
     8.7 -  
     8.8    (.mult transform tip (.getDirection ray))
     8.9 -  (.subtractLocal (.getDirection ray) (.getOrigin ray)))
    8.10 +  (.subtractLocal (.getDirection ray) (.getOrigin ray))
    8.11 +  (.normalizeLocal (.getDirection ray)))
    8.12  
    8.13 +(import com.jme3.math.FastMath)
    8.14 +
    8.15 + 
    8.16  (defn touch-kernel
    8.17    "Constructs a function which will return tactile sensory data from
    8.18     'geo when called from inside a running simulation"
    8.19 @@ -397,7 +400,15 @@
    8.20            ray-reference-tips (feeler-tips geo profile)
    8.21            ray-length (tactile-scale geo)
    8.22            current-rays (map (fn [_] (Ray.)) ray-reference-origins)
    8.23 -          topology (touch-topology geo profile)]
    8.24 +          topology (touch-topology geo profile)
    8.25 +          correction (float (* ray-length -0.2))]
    8.26 +
    8.27 +      ;; slight tolerance for very close collisions.
    8.28 +      (dorun
    8.29 +       (map (fn [origin tip]
    8.30 +              (.addLocal origin (.mult (.subtract tip origin)
    8.31 +                                       correction)))
    8.32 +            ray-reference-origins ray-reference-tips))
    8.33        (dorun (map #(.setLimit % ray-length) current-rays))
    8.34        (fn [node]
    8.35          (let [transform (.getWorldMatrix geo)]
    8.36 @@ -415,11 +426,20 @@
    8.37                    (.collideWith node ray results)
    8.38                    (let [touch-objects
    8.39                          (filter #(not (= geo (.getGeometry %)))
    8.40 -                                results)]
    8.41 +                                results)
    8.42 +                        limit (.getLimit ray)]
    8.43                      [(if (empty? touch-objects)
    8.44 -                       (.getLimit ray)
    8.45 -                       (.getDistance (first touch-objects)))
    8.46 -                     (.getLimit ray)])))))))))))
    8.47 +                       limit
    8.48 +                       (let [response
    8.49 +                             (apply min (map #(.getDistance %)
    8.50 +                                             touch-objects))]
    8.51 +                         (FastMath/clamp
    8.52 +                          (float 
    8.53 +                           (if (> response limit) 0.0
    8.54 +                               (+ response correction)))
    8.55 +                           (float 0.0)
    8.56 +                           limit)))
    8.57 +                     limit])))))))))))
    8.58  
    8.59  (defn touch! 
    8.60    "Endow the creature with the sense of touch. Returns a sequence of
    8.61 @@ -433,8 +453,18 @@
    8.62                  (node-seq creature)))))
    8.63  #+end_src
    8.64  
    8.65 +#+results: kernel
    8.66 +: #'cortex.touch/touch!
    8.67 +
    8.68  * Visualizing Touch
    8.69  
    8.70 +Each feeler is represented in the image as a single pixel. The
    8.71 +grayscale value of each pixel represents how deep the feeler
    8.72 +represented by that pixel is inside another object.  Black means that
    8.73 +nothing is touching the feeler, while white means that the feeler is
    8.74 +completely inside another object, which is presumably flush with the
    8.75 +surface of the triangle from which the feeler originates.
    8.76 +
    8.77  #+name: visualization
    8.78  #+begin_src clojure
    8.79  (in-ns 'cortex.touch)
    8.80 @@ -453,9 +483,80 @@
    8.81       (let [image (points->image coords)]
    8.82         (dorun
    8.83          (for [i (range (count coords))]
    8.84 -          (.setRGB image ((coords i) 0) ((coords i) 1)
    8.85 -                   (apply touch->gray (sensor-data i))))) image))))
    8.86 +          (do
    8.87 +            (if (= i 500) (println-repl (sensor-data i)))
    8.88 +
    8.89 +            (comment
    8.90 +              (.setRGB image ((coords i) 0) ((coords i) 1)
    8.91 +                     (apply touch->gray (sensor-data i))))))) 
    8.92 +       image))))
    8.93  #+end_src
    8.94 +
    8.95 +#+results: visualization
    8.96 +: #'cortex.touch/view-touch
    8.97 +
    8.98 +* COMMENT Basic Test of Touch
    8.99 +
   8.100 +The worm's sense of touch is a bit complicated, so for this basic test
   8.101 +I'll use a new creature --- a simple cube which has touch sensors
   8.102 +evenly distributed along each of its sides.
   8.103 +
   8.104 +#+begin_src clojure
   8.105 +(in-ns 'cortex.test.touch)
   8.106 +
   8.107 +(defn touch-cube []
   8.108 +  (load-blender-model "Models/test-touch/touch-cube.blend"))
   8.109 +#+end_src
   8.110 +
   8.111 +#+begin_html
   8.112 +<br>
   8.113 +#+end_html
   8.114 +
   8.115 +#+begin_html
   8.116 +<div class="figure">
   8.117 +<center>
   8.118 +<video controls="controls" width="500">
   8.119 +  <source src="../video/touch-cube.ogg" type="video/ogg"
   8.120 +	  preload="none" poster="../images/aurellem-1280x480.png" />
   8.121 +</video>
   8.122 +</center>
   8.123 +<p>A simple creature with evenly distributed touch sensors.</p>
   8.124 +</div>
   8.125 +#+end_html
   8.126 +
   8.127 +The tactile-sensor-profile image for this simple creature looks like
   8.128 +this:
   8.129 +
   8.130 +#+attr_html: width=500
   8.131 +#+caption: The distribution of feelers along the touch-cube. The colors of the faces are irrelevant; only the white pixels specify feelers.
   8.132 +[[../images/touch-profile.png]]
   8.133 +
   8.134 +#+begin_src clojure
   8.135 +(in-ns 'cortex.test.touch)
   8.136 +
   8.137 +(defn test-basic-touch
   8.138 +  ([] (test-basic-touch false))
   8.139 +  ([record?]
   8.140 +     (let [the-cube (doto (touch-cube) (body!))
   8.141 +           touch (touch! the-cube)
   8.142 +           touch-display (view-touch)]
   8.143 +       (world (nodify [the-cube
   8.144 +                       (box 10 1 10 :position (Vector3f. 0 -10 0)
   8.145 +                            :color ColorRGBA/Gray :mass 0)])
   8.146 +
   8.147 +              standard-debug-controls
   8.148 +              
   8.149 +              (fn [world]
   8.150 +                (speed-up world)
   8.151 +                (light-up-everything world))
   8.152 +
   8.153 +              (fn [world tpf]
   8.154 +                (touch-display 
   8.155 +                 (map #(% (.getRootNode world)) touch)))))))
   8.156 +  
   8.157 +
   8.158 +#+end_src
   8.159 +
   8.160  * Adding Touch to the Worm
   8.161  
   8.162  #+name: test-touch