changeset 118:1261444da2c7

got rough vision pipeline working
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Jan 2012 01:08:35 -0700
parents 94c005f7f9dd
children ebfd62779ab4
files assets/Models/creature1/retina-UV-small.png assets/Models/creature1/retina-small.png assets/Models/creature1/retina-small.xcf assets/Models/creature1/try-again.blend org/test-creature.org
diffstat 5 files changed, 81 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/retina-UV-small.png has changed
     2.1 Binary file assets/Models/creature1/retina-small.png has changed
     3.1 Binary file assets/Models/creature1/retina-small.xcf has changed
     4.1 Binary file assets/Models/creature1/try-again.blend has changed
     5.1 --- a/org/test-creature.org	Fri Jan 20 05:47:56 2012 -0700
     5.2 +++ b/org/test-creature.org	Sat Jan 21 01:08:35 2012 -0700
     5.3 @@ -83,7 +83,11 @@
     5.4            width (- (apply max xs) x0)
     5.5            height (- (apply max ys) y0)
     5.6            image (BufferedImage. (inc width) (inc height)
     5.7 -                                BufferedImage/TYPE_BYTE_BINARY)]
     5.8 +                                BufferedImage/TYPE_4BYTE_ABGR)]
     5.9 +      (dorun
    5.10 +       (for [x (range (.getWidth image))
    5.11 +             y (range (.getHeight image))]
    5.12 +         (.setRGB image x y 0xFFFF0000)))
    5.13        (dorun 
    5.14         (for [index (range (count points))]
    5.15           (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    5.16 @@ -736,25 +740,50 @@
    5.17         (controlRender [_ _])))))
    5.18  
    5.19  
    5.20 -(defn attach-eyes
    5.21 -  "For each eye in the creature, attach a Camera to the appropiate
    5.22 -   area and return the Camera."
    5.23 -  [#^Node creature]
    5.24 -  (for [eye (creature-eyes creature)]
    5.25 -    (let [target (eye-target creature eye)
    5.26 -          [cam-width cam-height] (eye-dimensions eye)
    5.27 -          cam (Camera. cam-width cam-height)]
    5.28 -      (.setLocation cam (.getWorldTranslation eye))
    5.29 -      (.setRotation cam (.getWorldRotation eye))
    5.30 -      
    5.31 -      )
    5.32 -      
    5.33 -      
    5.34 -))
    5.35 -  
    5.36 +(defn attach-eye
    5.37 +  "Attach a Camera to the appropiate area and return the Camera."
    5.38 +  [#^Node creature #^Spatial eye]
    5.39 +
    5.40 +  (let [target (eye-target creature eye)
    5.41 +        [cam-width cam-height] (eye-dimensions eye)
    5.42 +        cam (Camera. cam-width cam-height)]
    5.43 +    (.setLocation cam (.getWorldTranslation eye))
    5.44 +    (.setRotation cam (.getWorldRotation eye))
    5.45 +    (bind-camera target cam)
    5.46 +    cam))
    5.47 +
    5.48 +
    5.49 +(def presets
    5.50 +  {:gray identity})
    5.51 +
    5.52 +(defn enable-vision
    5.53 +  "return [init-function sensor-functions] for a particular eye"
    5.54 +  [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
    5.55 +  (let [retinal-map (retina-sensor-image eye)
    5.56 +        vision-image (atom nil)
    5.57 +        camera (attach-eye creature eye)]
    5.58 +    [
    5.59 +    (fn [world]
    5.60 +      (add-eye
    5.61 +       world camera
    5.62 +       (let [counter  (atom 0)]
    5.63 +         (fn [r fb bb bi]
    5.64 +           (if (zero? (rem (swap! counter inc) (inc skip)))
    5.65 +             (reset! vision-image (BufferedImage! r fb bb bi)))))))
    5.66 +    (vector
    5.67 +     (let [whites (white-coordinates (:gray retinal-map))
    5.68 +           topology (vec (collapse whites))]
    5.69 +     (fn []
    5.70 +       (vector
    5.71 +        topology
    5.72 +        (vec 
    5.73 +         (for [[x y] whites]
    5.74 +           (.getRGB @vision-image x y)))))))
    5.75 +    ]))
    5.76 +
    5.77  (defn vision
    5.78  
    5.79 -  ;; need to create a camera based on uv image,
    5.80 +  ;; need to create a camera based on UV image,
    5.81    ;; update this camera every frame based on the position of this
    5.82    ;; geometry. (maybe can get cam to follow the object)
    5.83  
    5.84 @@ -798,6 +827,20 @@
    5.85                        1 -1} (sensor-data i)))))
    5.86          (vi image)))))
    5.87  
    5.88 +(defn debug-vision-window
    5.89 +  "creates function that offers a debug view of sensor data"
    5.90 +  []
    5.91 +  (let [vi (view-image)]
    5.92 +    (fn 
    5.93 +      [[coords sensor-data]]
    5.94 +      (let [image (points->image coords)]
    5.95 +        (dorun
    5.96 +         (for [i (range (count coords))]
    5.97 +           (.setRGB image ((coords i) 0) ((coords i) 1)
    5.98 +                   (sensor-data i))))
    5.99 +        (vi image)))))
   5.100 +
   5.101 +
   5.102  
   5.103  ;;(defn test-touch [world creature]
   5.104  
   5.105 @@ -812,17 +855,30 @@
   5.106          creature (blender-creature thing)
   5.107          touch-nerves (touch creature)
   5.108          touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
   5.109 +        [init-vision [vision-data]]
   5.110 +        (enable-vision creature (test-eye))
   5.111 +        vision-debug (debug-vision-window)
   5.112 +        me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
   5.113          ]
   5.114    (world
   5.115     (nodify [creature
   5.116              (box 10 2 10 :position (Vector3f. 0 -9 0)
   5.117                   :color ColorRGBA/Gray :mass 0)
   5.118              x-axis y-axis z-axis
   5.119 +            me
   5.120              ])
   5.121     standard-debug-controls
   5.122     (fn [world]
   5.123       (light-up-everything world)
   5.124       (enable-debug world)
   5.125 +     (init-vision world)
   5.126 +     
   5.127 +     (add-eye world
   5.128 +              (attach-eye creature (test-eye))
   5.129 +              (comp (view-image) BufferedImage!))
   5.130 +     
   5.131 +     (add-eye world (.getCamera world) no-op)
   5.132 +     
   5.133       ;;(com.aurellem.capture.Capture/captureVideo
   5.134       ;; world (file-str "/home/r/proj/ai-videos/hand"))
   5.135       ;;(.setTimer world (RatchetTimer. 60))
   5.136 @@ -837,7 +893,10 @@
   5.137        (map #(%1 (%2 (.getRootNode world)))
   5.138             touch-debug-windows touch-nerves)
   5.139       )
   5.140 -
   5.141 +     ;;(println-repl (vision-data))
   5.142 +     (.setLocalTranslation me (.getLocation (.getCamera world)))
   5.143 +     
   5.144 +     (vision-debug (vision-data))
   5.145       )
   5.146     ;;(let [timer (atom 0)]
   5.147     ;;  (fn [_ _]
   5.148 @@ -918,6 +977,7 @@
   5.149                       :position (Vector3f. 0 10 0)
   5.150                       :mass 30
   5.151                       )
   5.152 +        rot (.getWorldRotation rock)
   5.153          
   5.154          table (box 3 1 10 :color ColorRGBA/Gray :mass 0
   5.155                     :position (Vector3f. 0 -3 0))]
   5.156 @@ -937,8 +997,9 @@
   5.157                   (add-eye world cam (comp (view-image) BufferedImage!))
   5.158                   (add-eye world (.getCamera world) no-op))
   5.159                 )
   5.160 -             no-op)))
   5.161 +             (fn [_ _] (println-repl rot)))))
   5.162         
   5.163 +
   5.164  #+end_src
   5.165  
   5.166  #+results: body-1