diff org/test-creature.org @ 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
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Fri Jan 20 05:47:56 2012 -0700
     1.2 +++ b/org/test-creature.org	Sat Jan 21 01:08:35 2012 -0700
     1.3 @@ -83,7 +83,11 @@
     1.4            width (- (apply max xs) x0)
     1.5            height (- (apply max ys) y0)
     1.6            image (BufferedImage. (inc width) (inc height)
     1.7 -                                BufferedImage/TYPE_BYTE_BINARY)]
     1.8 +                                BufferedImage/TYPE_4BYTE_ABGR)]
     1.9 +      (dorun
    1.10 +       (for [x (range (.getWidth image))
    1.11 +             y (range (.getHeight image))]
    1.12 +         (.setRGB image x y 0xFFFF0000)))
    1.13        (dorun 
    1.14         (for [index (range (count points))]
    1.15           (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    1.16 @@ -736,25 +740,50 @@
    1.17         (controlRender [_ _])))))
    1.18  
    1.19  
    1.20 -(defn attach-eyes
    1.21 -  "For each eye in the creature, attach a Camera to the appropiate
    1.22 -   area and return the Camera."
    1.23 -  [#^Node creature]
    1.24 -  (for [eye (creature-eyes creature)]
    1.25 -    (let [target (eye-target creature eye)
    1.26 -          [cam-width cam-height] (eye-dimensions eye)
    1.27 -          cam (Camera. cam-width cam-height)]
    1.28 -      (.setLocation cam (.getWorldTranslation eye))
    1.29 -      (.setRotation cam (.getWorldRotation eye))
    1.30 -      
    1.31 -      )
    1.32 -      
    1.33 -      
    1.34 -))
    1.35 -  
    1.36 +(defn attach-eye
    1.37 +  "Attach a Camera to the appropiate area and return the Camera."
    1.38 +  [#^Node creature #^Spatial eye]
    1.39 +
    1.40 +  (let [target (eye-target creature eye)
    1.41 +        [cam-width cam-height] (eye-dimensions eye)
    1.42 +        cam (Camera. cam-width cam-height)]
    1.43 +    (.setLocation cam (.getWorldTranslation eye))
    1.44 +    (.setRotation cam (.getWorldRotation eye))
    1.45 +    (bind-camera target cam)
    1.46 +    cam))
    1.47 +
    1.48 +
    1.49 +(def presets
    1.50 +  {:gray identity})
    1.51 +
    1.52 +(defn enable-vision
    1.53 +  "return [init-function sensor-functions] for a particular eye"
    1.54 +  [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
    1.55 +  (let [retinal-map (retina-sensor-image eye)
    1.56 +        vision-image (atom nil)
    1.57 +        camera (attach-eye creature eye)]
    1.58 +    [
    1.59 +    (fn [world]
    1.60 +      (add-eye
    1.61 +       world camera
    1.62 +       (let [counter  (atom 0)]
    1.63 +         (fn [r fb bb bi]
    1.64 +           (if (zero? (rem (swap! counter inc) (inc skip)))
    1.65 +             (reset! vision-image (BufferedImage! r fb bb bi)))))))
    1.66 +    (vector
    1.67 +     (let [whites (white-coordinates (:gray retinal-map))
    1.68 +           topology (vec (collapse whites))]
    1.69 +     (fn []
    1.70 +       (vector
    1.71 +        topology
    1.72 +        (vec 
    1.73 +         (for [[x y] whites]
    1.74 +           (.getRGB @vision-image x y)))))))
    1.75 +    ]))
    1.76 +
    1.77  (defn vision
    1.78  
    1.79 -  ;; need to create a camera based on uv image,
    1.80 +  ;; need to create a camera based on UV image,
    1.81    ;; update this camera every frame based on the position of this
    1.82    ;; geometry. (maybe can get cam to follow the object)
    1.83  
    1.84 @@ -798,6 +827,20 @@
    1.85                        1 -1} (sensor-data i)))))
    1.86          (vi image)))))
    1.87  
    1.88 +(defn debug-vision-window
    1.89 +  "creates function that offers a debug view of sensor data"
    1.90 +  []
    1.91 +  (let [vi (view-image)]
    1.92 +    (fn 
    1.93 +      [[coords sensor-data]]
    1.94 +      (let [image (points->image coords)]
    1.95 +        (dorun
    1.96 +         (for [i (range (count coords))]
    1.97 +           (.setRGB image ((coords i) 0) ((coords i) 1)
    1.98 +                   (sensor-data i))))
    1.99 +        (vi image)))))
   1.100 +
   1.101 +
   1.102  
   1.103  ;;(defn test-touch [world creature]
   1.104  
   1.105 @@ -812,17 +855,30 @@
   1.106          creature (blender-creature thing)
   1.107          touch-nerves (touch creature)
   1.108          touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
   1.109 +        [init-vision [vision-data]]
   1.110 +        (enable-vision creature (test-eye))
   1.111 +        vision-debug (debug-vision-window)
   1.112 +        me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
   1.113          ]
   1.114    (world
   1.115     (nodify [creature
   1.116              (box 10 2 10 :position (Vector3f. 0 -9 0)
   1.117                   :color ColorRGBA/Gray :mass 0)
   1.118              x-axis y-axis z-axis
   1.119 +            me
   1.120              ])
   1.121     standard-debug-controls
   1.122     (fn [world]
   1.123       (light-up-everything world)
   1.124       (enable-debug world)
   1.125 +     (init-vision world)
   1.126 +     
   1.127 +     (add-eye world
   1.128 +              (attach-eye creature (test-eye))
   1.129 +              (comp (view-image) BufferedImage!))
   1.130 +     
   1.131 +     (add-eye world (.getCamera world) no-op)
   1.132 +     
   1.133       ;;(com.aurellem.capture.Capture/captureVideo
   1.134       ;; world (file-str "/home/r/proj/ai-videos/hand"))
   1.135       ;;(.setTimer world (RatchetTimer. 60))
   1.136 @@ -837,7 +893,10 @@
   1.137        (map #(%1 (%2 (.getRootNode world)))
   1.138             touch-debug-windows touch-nerves)
   1.139       )
   1.140 -
   1.141 +     ;;(println-repl (vision-data))
   1.142 +     (.setLocalTranslation me (.getLocation (.getCamera world)))
   1.143 +     
   1.144 +     (vision-debug (vision-data))
   1.145       )
   1.146     ;;(let [timer (atom 0)]
   1.147     ;;  (fn [_ _]
   1.148 @@ -918,6 +977,7 @@
   1.149                       :position (Vector3f. 0 10 0)
   1.150                       :mass 30
   1.151                       )
   1.152 +        rot (.getWorldRotation rock)
   1.153          
   1.154          table (box 3 1 10 :color ColorRGBA/Gray :mass 0
   1.155                     :position (Vector3f. 0 -3 0))]
   1.156 @@ -937,8 +997,9 @@
   1.157                   (add-eye world cam (comp (view-image) BufferedImage!))
   1.158                   (add-eye world (.getCamera world) no-op))
   1.159                 )
   1.160 -             no-op)))
   1.161 +             (fn [_ _] (println-repl rot)))))
   1.162         
   1.163 +
   1.164  #+end_src
   1.165  
   1.166  #+results: body-1