changeset 199:305439cec54d

added video to sense.org
author Robert McIntyre <rlm@mit.edu>
date Mon, 06 Feb 2012 01:40:22 -0700
parents fc0bf33bded2
children 7eb966144dad
files images/aurellem-1280x480.png org/integration.org org/sense.org org/util.org
diffstat 4 files changed, 153 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file images/aurellem-1280x480.png has changed
     2.1 --- a/org/integration.org	Sun Feb 05 14:01:47 2012 -0700
     2.2 +++ b/org/integration.org	Mon Feb 06 01:40:22 2012 -0700
     2.3 @@ -304,36 +304,6 @@
     2.4           (.applyTorque control
     2.5                         (.mult (.getPhysicsRotation control)
     2.6                                (Vector3f. 1 0 0))))))))
     2.7 -
     2.8 -(defn follow-test
     2.9 -  "Show a camera that stays in the same relative position to a blue cube."
    2.10 -  []
    2.11 -  (let [camera-pos (Vector3f. 0 30 0)
    2.12 -        rock (box 1 1 1 :color ColorRGBA/Blue
    2.13 -                     :position (Vector3f. 0 10 0)
    2.14 -                     :mass 30
    2.15 -                     )
    2.16 -        rot (.getWorldRotation rock)
    2.17 -        
    2.18 -        table (box 3 1 10 :color ColorRGBA/Gray :mass 0
    2.19 -                   :position (Vector3f. 0 -3 0))]
    2.20 -
    2.21 -    (world
    2.22 -     (nodify [rock table])
    2.23 -             standard-debug-controls
    2.24 -             (fn [world]
    2.25 -               (let 
    2.26 -                   [cam (doto (.clone (.getCamera world))
    2.27 -                          (.setLocation camera-pos)
    2.28 -                          (.lookAt Vector3f/ZERO
    2.29 -                                   Vector3f/UNIT_X))]
    2.30 -                 (bind-sense rock cam)
    2.31 -                 
    2.32 -                 (.setTimer world (RatchetTimer. 60))
    2.33 -                 (add-camera! world cam (comp (view-image) BufferedImage!))
    2.34 -                 (add-camera! world (.getCamera world) no-op))
    2.35 -               )
    2.36 -             (fn [_ _] (println-repl rot)))))
    2.37  #+end_src
    2.38    
    2.39  
     3.1 --- a/org/sense.org	Sun Feb 05 14:01:47 2012 -0700
     3.2 +++ b/org/sense.org	Mon Feb 06 01:40:22 2012 -0700
     3.3 @@ -225,24 +225,9 @@
     3.4  =(points->image)= helps senses generate a base image onto which they
     3.5  can overlay actual sense data.
     3.6  
     3.7 -#+name view-senses
     3.8 +#+name: view-senses
     3.9  #+begin_src clojure
    3.10 -(defn view-sense 
    3.11 -  "Take a kernel that produces a BufferedImage from some sense data
    3.12 -   and return a function which takes a list of sense data, uses the
    3.13 -   kernel to convert to images, and displays those images, each in
    3.14 -   its own JFrame."
    3.15 -  [sense-display-kernel]
    3.16 -  (let [windows (atom [])]
    3.17 -    (fn [data]
    3.18 -      (if (> (count data) (count @windows))
    3.19 -        (reset! 
    3.20 -         windows (map (fn [_] (view-image)) (range (count data)))))
    3.21 -      (dorun
    3.22 -       (map
    3.23 -        (fn [display datum]
    3.24 -          (display (sense-display-kernel datum)))
    3.25 -        @windows data)))))
    3.26 +(in-ns 'cortex.sense)
    3.27  
    3.28  (defn points->image
    3.29    "Take a collection of points and visuliaze it as a BufferedImage."
    3.30 @@ -266,6 +251,58 @@
    3.31           (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    3.32        image)))
    3.33  
    3.34 +(defn view-image
    3.35 +  "Initailizes a JPanel on which you may draw a BufferedImage.
    3.36 +   Returns a function that accepts a BufferedImage and draws it to the
    3.37 +   JPanel. If given a directory it will save the images as png files
    3.38 +   starting at 0000000.png and incrementing from there."
    3.39 +  ([#^File save]
    3.40 +     (let [idx (atom -1)
    3.41 +           image
    3.42 +           (atom
    3.43 +            (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
    3.44 +           panel 
    3.45 +           (proxy [JPanel] []
    3.46 +             (paint
    3.47 +               [graphics]
    3.48 +               (proxy-super paintComponent graphics)
    3.49 +               (.drawImage graphics @image 0 0 nil)))
    3.50 +           frame (JFrame. "Display Image")]
    3.51 +       (SwingUtilities/invokeLater
    3.52 +        (fn []
    3.53 +          (doto frame
    3.54 +            (-> (.getContentPane) (.add panel))
    3.55 +            (.pack)
    3.56 +            (.setLocationRelativeTo nil)
    3.57 +            (.setResizable true)
    3.58 +            (.setVisible true))))
    3.59 +       (fn [#^BufferedImage i]
    3.60 +         (reset! image i)
    3.61 +         (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
    3.62 +         (.repaint panel 0 0 (.getWidth i) (.getHeight i))
    3.63 +         (if save
    3.64 +           (ImageIO/write
    3.65 +            i "png"
    3.66 +            (File. save (format "%07d.png" (swap! idx inc))))))))
    3.67 +  ([] (view-image nil)))
    3.68 +
    3.69 +(defn view-sense 
    3.70 +  "Take a kernel that produces a BufferedImage from some sense data
    3.71 +   and return a function which takes a list of sense data, uses the
    3.72 +   kernel to convert to images, and displays those images, each in
    3.73 +   its own JFrame."
    3.74 +  [sense-display-kernel]
    3.75 +  (let [windows (atom [])]
    3.76 +    (fn [data]
    3.77 +      (if (> (count data) (count @windows))
    3.78 +        (reset! 
    3.79 +         windows (map (fn [_] (view-image)) (range (count data)))))
    3.80 +      (dorun
    3.81 +       (map
    3.82 +        (fn [display datum]
    3.83 +          (display (sense-display-kernel datum)))
    3.84 +        @windows data)))))
    3.85 +
    3.86  (defn gray
    3.87    "Create a gray RGB pixel with R, G, and B set to num. num must be
    3.88     between 0 and 255."
    3.89 @@ -343,8 +380,7 @@
    3.90  
    3.91  =(bind-sense)= binds either a Camera or a Listener object to any
    3.92  object so that they will follow that object no matter how it
    3.93 -moves. Here is some example code which shows a camera bound to a blue
    3.94 -box as it is buffeted by white cannonballs.
    3.95 +moves. It is used to create both eyes and ears.
    3.96  
    3.97  #+name: node-2
    3.98  #+begin_src clojure
    3.99 @@ -374,11 +410,99 @@
   3.100         (controlRender [_ _])))))
   3.101  #+end_src
   3.102  
   3.103 +Here is some example code which shows a camera bound to a blue
   3.104 +box as it is buffeted by white cannonballs.
   3.105 +
   3.106 +#+name: test
   3.107 +#+begin_src clojure 
   3.108 +(ns cortex.test.sense
   3.109 +  (:use (cortex world util sense vision))
   3.110 +  (:import
   3.111 +   java.io.File
   3.112 +   (com.jme3.math Vector3f ColorRGBA)
   3.113 +   (com.aurellem.capture RatchetTimer Capture)))
   3.114 +
   3.115 +(defn test-bind-sense
   3.116 +  "Show a camera that stays in the same relative position to a blue cube."
   3.117 +  []
   3.118 +  (let [camera-pos (Vector3f. 0 30 0)
   3.119 +        rock (box 1 1 1 :color ColorRGBA/Blue
   3.120 +                  :position (Vector3f. 0 10 0)
   3.121 +                  :mass 30)
   3.122 +        rot (.getWorldRotation rock)
   3.123 +        table (box 3 1 10 :color ColorRGBA/Gray :mass 0
   3.124 +                   :position (Vector3f. 0 -3 0))]
   3.125 +    (world
   3.126 +     (nodify [rock table])
   3.127 +     standard-debug-controls
   3.128 +     (fn [world]
   3.129 +       (let 
   3.130 +           [cam (doto (.clone (.getCamera world))
   3.131 +                  (.setLocation camera-pos)
   3.132 +                  (.lookAt Vector3f/ZERO
   3.133 +                           Vector3f/UNIT_X))]
   3.134 +         (bind-sense rock cam)
   3.135 +         (.setTimer world (RatchetTimer. 60))
   3.136 +         (Capture/captureVideo
   3.137 +          world (File. "/home/r/proj/cortex/render/bind-sense0"))
   3.138 +         (add-camera!
   3.139 +          world cam
   3.140 +          (comp (view-image
   3.141 +                 (File. "/home/r/proj/cortex/render/bind-sense1"))
   3.142 +                BufferedImage!))
   3.143 +         (add-camera! world (.getCamera world) no-op)))
   3.144 +     no-op)))
   3.145 +#+end_src
   3.146 +
   3.147 +** Demo Video
   3.148 +
   3.149 +#+begin_html
   3.150 +<video controls="controls" width="755">
   3.151 +  <source src="../video/bind-sense.ogg" type="video/ogg"
   3.152 +	  preload="none" poster="../images/aurellem-1280x480.png" />
   3.153 +</video>
   3.154 +
   3.155 +#+end_html
   3.156 +
   3.157 +note to self: the video was created with the following commands:
   3.158 +
   3.159 +
   3.160 +#+begin_src clojure :results silent
   3.161 +(in-ns 'user)
   3.162 +(import java.io.File)
   3.163 +(use 'clojure.contrib.shell-out)
   3.164 +
   3.165 +(let
   3.166 +    [idx (atom -1)
   3.167 +     left (rest 
   3.168 +           (sort
   3.169 +           (file-seq (File. "/home/r/proj/cortex/render/bind-sense0/"))))
   3.170 +     right (rest
   3.171 +            (sort
   3.172 +             (file-seq (File. "/home/r/proj/cortex/render/bind-sense1/"))))]
   3.173 +  (dorun
   3.174 +   (map  
   3.175 +    (fn [im-1 im-2]
   3.176 +      (println idx)
   3.177 +      (sh "convert" (.getCanonicalPath im-1)
   3.178 +          (.getCanonicalPath im-2) "+append"
   3.179 +          (.getCanonicalPath
   3.180 +           (File. "/home/r/proj/cortex/render/bind-sense/"
   3.181 +                  (format "%07d.png" (swap! idx inc))))))
   3.182 +    left right)))
   3.183 +#+end_src
   3.184 +
   3.185 +#+begin_src sh :results silent
   3.186 +cd /home/r/proj/cortex/render/
   3.187 +cp ../images/aurellem-1280x480.png bind-sense/0000000.png             
   3.188 +ffmpeg -r 60 -b 9000k -i bind-sense/%07d.png bind-sense.ogg
   3.189 +#+end_src
   3.190 +
   3.191  
   3.192  
   3.193  * Bookkeeping
   3.194  Here is the header for this namespace, included for completness.
   3.195 -#+name header
   3.196 +#+name: header
   3.197  #+begin_src clojure
   3.198  (ns cortex.sense
   3.199    "Here are functions useful in the construction of two or more
   3.200 @@ -392,7 +516,11 @@
   3.201    (:import com.jme3.bounding.BoundingBox)
   3.202    (:import (com.jme3.scene Node Spatial))
   3.203    (:import com.jme3.scene.control.AbstractControl)
   3.204 -  (:import (com.jme3.math Quaternion Vector3f)))
   3.205 +  (:import (com.jme3.math Quaternion Vector3f))
   3.206 +  (:import javax.imageio.ImageIO)
   3.207 +  (:import java.io.File)
   3.208 +  (:import (javax.swing JPanel JFrame SwingUtilities)))
   3.209 +
   3.210  #+end_src
   3.211  
   3.212  * Source Listing
   3.213 @@ -410,3 +538,7 @@
   3.214  <<node-2>>
   3.215  <<view-senses>>
   3.216  #+end_src
   3.217 +
   3.218 +#+begin_src clojure :tangle ../src/cortex/test/sense.clj
   3.219 +<<test>>
   3.220 +#+end_src
     4.1 --- a/org/util.org	Sun Feb 05 14:01:47 2012 -0700
     4.2 +++ b/org/util.org	Mon Feb 06 01:40:22 2012 -0700
     4.3 @@ -486,35 +486,6 @@
     4.4  #+begin_src clojure :results silent
     4.5  (in-ns 'cortex.util)
     4.6  
     4.7 -(defn view-image
     4.8 -  "Initailizes a JPanel on which you may draw a BufferedImage.
     4.9 -   Returns a function that accepts a BufferedImage and draws it to the
    4.10 -   JPanel."
    4.11 -  []
    4.12 -  (let [image
    4.13 -        (atom
    4.14 -         (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
    4.15 -        panel 
    4.16 -        (proxy [JPanel] []
    4.17 -          (paint
    4.18 -            [graphics]
    4.19 -            (proxy-super paintComponent graphics)
    4.20 -            (.drawImage graphics @image 0 0 nil)))
    4.21 -        frame (JFrame. "Display Image")]
    4.22 -    (SwingUtilities/invokeLater
    4.23 -     (fn []
    4.24 -       (doto frame
    4.25 -         (-> (.getContentPane) (.add panel))
    4.26 -         (.pack)
    4.27 -         (.setLocationRelativeTo nil)
    4.28 -         (.setResizable true)
    4.29 -         (.setVisible true))))
    4.30 -    (fn [#^BufferedImage i]
    4.31 -      (reset! image i)
    4.32 -      (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
    4.33 -      (.repaint panel 0 0 (.getWidth i) (.getHeight i))
    4.34 -      i)))
    4.35 -
    4.36  (defprotocol Viewable
    4.37    (view [something]))
    4.38