# HG changeset patch # User Robert McIntyre # Date 1328517622 25200 # Node ID 305439cec54d721d647cd4d061dcf682aed0e48b # Parent fc0bf33bded24a654a5ad7b530d99d2c84b1445b added video to sense.org diff -r fc0bf33bded2 -r 305439cec54d images/aurellem-1280x480.png Binary file images/aurellem-1280x480.png has changed diff -r fc0bf33bded2 -r 305439cec54d org/integration.org --- a/org/integration.org Sun Feb 05 14:01:47 2012 -0700 +++ b/org/integration.org Mon Feb 06 01:40:22 2012 -0700 @@ -304,36 +304,6 @@ (.applyTorque control (.mult (.getPhysicsRotation control) (Vector3f. 1 0 0)))))))) - -(defn follow-test - "Show a camera that stays in the same relative position to a blue cube." - [] - (let [camera-pos (Vector3f. 0 30 0) - rock (box 1 1 1 :color ColorRGBA/Blue - :position (Vector3f. 0 10 0) - :mass 30 - ) - rot (.getWorldRotation rock) - - table (box 3 1 10 :color ColorRGBA/Gray :mass 0 - :position (Vector3f. 0 -3 0))] - - (world - (nodify [rock table]) - standard-debug-controls - (fn [world] - (let - [cam (doto (.clone (.getCamera world)) - (.setLocation camera-pos) - (.lookAt Vector3f/ZERO - Vector3f/UNIT_X))] - (bind-sense rock cam) - - (.setTimer world (RatchetTimer. 60)) - (add-camera! world cam (comp (view-image) BufferedImage!)) - (add-camera! world (.getCamera world) no-op)) - ) - (fn [_ _] (println-repl rot))))) #+end_src diff -r fc0bf33bded2 -r 305439cec54d org/sense.org --- a/org/sense.org Sun Feb 05 14:01:47 2012 -0700 +++ b/org/sense.org Mon Feb 06 01:40:22 2012 -0700 @@ -225,24 +225,9 @@ =(points->image)= helps senses generate a base image onto which they can overlay actual sense data. -#+name view-senses +#+name: view-senses #+begin_src clojure -(defn view-sense - "Take a kernel that produces a BufferedImage from some sense data - and return a function which takes a list of sense data, uses the - kernel to convert to images, and displays those images, each in - its own JFrame." - [sense-display-kernel] - (let [windows (atom [])] - (fn [data] - (if (> (count data) (count @windows)) - (reset! - windows (map (fn [_] (view-image)) (range (count data))))) - (dorun - (map - (fn [display datum] - (display (sense-display-kernel datum))) - @windows data))))) +(in-ns 'cortex.sense) (defn points->image "Take a collection of points and visuliaze it as a BufferedImage." @@ -266,6 +251,58 @@ (.setRGB image (- (xs index) x0) (- (ys index) y0) -1))) image))) +(defn view-image + "Initailizes a JPanel on which you may draw a BufferedImage. + Returns a function that accepts a BufferedImage and draws it to the + JPanel. If given a directory it will save the images as png files + starting at 0000000.png and incrementing from there." + ([#^File save] + (let [idx (atom -1) + image + (atom + (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR)) + panel + (proxy [JPanel] [] + (paint + [graphics] + (proxy-super paintComponent graphics) + (.drawImage graphics @image 0 0 nil))) + frame (JFrame. "Display Image")] + (SwingUtilities/invokeLater + (fn [] + (doto frame + (-> (.getContentPane) (.add panel)) + (.pack) + (.setLocationRelativeTo nil) + (.setResizable true) + (.setVisible true)))) + (fn [#^BufferedImage i] + (reset! image i) + (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i))) + (.repaint panel 0 0 (.getWidth i) (.getHeight i)) + (if save + (ImageIO/write + i "png" + (File. save (format "%07d.png" (swap! idx inc)))))))) + ([] (view-image nil))) + +(defn view-sense + "Take a kernel that produces a BufferedImage from some sense data + and return a function which takes a list of sense data, uses the + kernel to convert to images, and displays those images, each in + its own JFrame." + [sense-display-kernel] + (let [windows (atom [])] + (fn [data] + (if (> (count data) (count @windows)) + (reset! + windows (map (fn [_] (view-image)) (range (count data))))) + (dorun + (map + (fn [display datum] + (display (sense-display-kernel datum))) + @windows data))))) + (defn gray "Create a gray RGB pixel with R, G, and B set to num. num must be between 0 and 255." @@ -343,8 +380,7 @@ =(bind-sense)= binds either a Camera or a Listener object to any object so that they will follow that object no matter how it -moves. Here is some example code which shows a camera bound to a blue -box as it is buffeted by white cannonballs. +moves. It is used to create both eyes and ears. #+name: node-2 #+begin_src clojure @@ -374,11 +410,99 @@ (controlRender [_ _]))))) #+end_src +Here is some example code which shows a camera bound to a blue +box as it is buffeted by white cannonballs. + +#+name: test +#+begin_src clojure +(ns cortex.test.sense + (:use (cortex world util sense vision)) + (:import + java.io.File + (com.jme3.math Vector3f ColorRGBA) + (com.aurellem.capture RatchetTimer Capture))) + +(defn test-bind-sense + "Show a camera that stays in the same relative position to a blue cube." + [] + (let [camera-pos (Vector3f. 0 30 0) + rock (box 1 1 1 :color ColorRGBA/Blue + :position (Vector3f. 0 10 0) + :mass 30) + rot (.getWorldRotation rock) + table (box 3 1 10 :color ColorRGBA/Gray :mass 0 + :position (Vector3f. 0 -3 0))] + (world + (nodify [rock table]) + standard-debug-controls + (fn [world] + (let + [cam (doto (.clone (.getCamera world)) + (.setLocation camera-pos) + (.lookAt Vector3f/ZERO + Vector3f/UNIT_X))] + (bind-sense rock cam) + (.setTimer world (RatchetTimer. 60)) + (Capture/captureVideo + world (File. "/home/r/proj/cortex/render/bind-sense0")) + (add-camera! + world cam + (comp (view-image + (File. "/home/r/proj/cortex/render/bind-sense1")) + BufferedImage!)) + (add-camera! world (.getCamera world) no-op))) + no-op))) +#+end_src + +** Demo Video + +#+begin_html + + +#+end_html + +note to self: the video was created with the following commands: + + +#+begin_src clojure :results silent +(in-ns 'user) +(import java.io.File) +(use 'clojure.contrib.shell-out) + +(let + [idx (atom -1) + left (rest + (sort + (file-seq (File. "/home/r/proj/cortex/render/bind-sense0/")))) + right (rest + (sort + (file-seq (File. "/home/r/proj/cortex/render/bind-sense1/"))))] + (dorun + (map + (fn [im-1 im-2] + (println idx) + (sh "convert" (.getCanonicalPath im-1) + (.getCanonicalPath im-2) "+append" + (.getCanonicalPath + (File. "/home/r/proj/cortex/render/bind-sense/" + (format "%07d.png" (swap! idx inc)))))) + left right))) +#+end_src + +#+begin_src sh :results silent +cd /home/r/proj/cortex/render/ +cp ../images/aurellem-1280x480.png bind-sense/0000000.png +ffmpeg -r 60 -b 9000k -i bind-sense/%07d.png bind-sense.ogg +#+end_src + * Bookkeeping Here is the header for this namespace, included for completness. -#+name header +#+name: header #+begin_src clojure (ns cortex.sense "Here are functions useful in the construction of two or more @@ -392,7 +516,11 @@ (:import com.jme3.bounding.BoundingBox) (:import (com.jme3.scene Node Spatial)) (:import com.jme3.scene.control.AbstractControl) - (:import (com.jme3.math Quaternion Vector3f))) + (:import (com.jme3.math Quaternion Vector3f)) + (:import javax.imageio.ImageIO) + (:import java.io.File) + (:import (javax.swing JPanel JFrame SwingUtilities))) + #+end_src * Source Listing @@ -410,3 +538,7 @@ <> <> #+end_src + +#+begin_src clojure :tangle ../src/cortex/test/sense.clj +<> +#+end_src diff -r fc0bf33bded2 -r 305439cec54d org/util.org --- a/org/util.org Sun Feb 05 14:01:47 2012 -0700 +++ b/org/util.org Mon Feb 06 01:40:22 2012 -0700 @@ -486,35 +486,6 @@ #+begin_src clojure :results silent (in-ns 'cortex.util) -(defn view-image - "Initailizes a JPanel on which you may draw a BufferedImage. - Returns a function that accepts a BufferedImage and draws it to the - JPanel." - [] - (let [image - (atom - (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR)) - panel - (proxy [JPanel] [] - (paint - [graphics] - (proxy-super paintComponent graphics) - (.drawImage graphics @image 0 0 nil))) - frame (JFrame. "Display Image")] - (SwingUtilities/invokeLater - (fn [] - (doto frame - (-> (.getContentPane) (.add panel)) - (.pack) - (.setLocationRelativeTo nil) - (.setResizable true) - (.setVisible true)))) - (fn [#^BufferedImage i] - (reset! image i) - (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i))) - (.repaint panel 0 0 (.getWidth i) (.getHeight i)) - i))) - (defprotocol Viewable (view [something]))