Mercurial > cortex
view org/sense.org @ 215:f283c62bd212
fixed long standing problem with orientation of eyes in blender, fleshed out text in vision.org
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 10 Feb 2012 02:19:24 -0700 |
parents | 97b8caf66824 |
children | 7bf3e3d8fb26 |
line wrap: on
line source
1 #+title: Helper Functions / Motivations2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: sensory utilities5 #+keywords: simulation, jMonkeyEngine3, clojure, simulated senses6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org10 * Blender Utilities11 In blender, any object can be assigned an arbitray number of key-value12 pairs which are called "Custom Properties". These are accessable in13 jMonkyeEngine when blender files are imported with the14 =BlenderLoader=. =(meta-data)= extracts these properties.16 #+name: blender-117 #+begin_src clojure18 (defn meta-data19 "Get the meta-data for a node created with blender."20 [blender-node key]21 (if-let [data (.getUserData blender-node "properties")]22 (.findValue data key) nil))23 #+end_src25 Blender uses a different coordinate system than jMonkeyEngine so it26 is useful to be able to convert between the two. These only come into27 play when the meta-data of a node refers to a vector in the blender28 coordinate system.30 #+name: blender-231 #+begin_src clojure32 (defn jme-to-blender33 "Convert from JME coordinates to Blender coordinates"34 [#^Vector3f in]35 (Vector3f. (.getX in) (- (.getZ in)) (.getY in)))37 (defn blender-to-jme38 "Convert from Blender coordinates to JME coordinates"39 [#^Vector3f in]40 (Vector3f. (.getX in) (.getZ in) (- (.getY in))))41 #+end_src43 * Sense Topology45 Human beings are three-dimensional objects, and the nerves that46 transmit data from our various sense organs to our brain are47 essentially one-dimensional. This leaves up to two dimensions in which48 our sensory information may flow. For example, imagine your skin: it49 is a two-dimensional surface around a three-dimensional object (your50 body). It has discrete touch sensors embedded at various points, and51 the density of these sensors corresponds to the sensitivity of that52 region of skin. Each touch sensor connects to a nerve, all of which53 eventually are bundled together as they travel up the spinal cord to54 the brain. Intersect the spinal nerves with a guillotining plane and55 you will see all of the sensory data of the skin revealed in a roughly56 circular two-dimensional image which is the cross section of the57 spinal cord. Points on this image that are close together in this58 circle represent touch sensors that are /probably/ close together on59 the skin, although there is of course some cutting and rerangement60 that has to be done to transfer the complicated surface of the skin61 onto a two dimensional image.63 Most human senses consist of many discrete sensors of various64 properties distributed along a surface at various densities. For65 skin, it is Pacinian corpuscles, Meissner's corpuscles, Merkel's66 disks, and Ruffini's endings, which detect pressure and vibration of67 various intensities. For ears, it is the stereocilia distributed68 along the basilar membrane inside the cochlea; each one is sensitive69 to a slightly different frequency of sound. For eyes, it is rods70 and cones distributed along the surface of the retina. In each case,71 we can describe the sense with a surface and a distribution of sensors72 along that surface.74 ** UV-maps76 Blender and jMonkeyEngine already have support for exactly this sort77 of data structure because it is used to "skin" models for games. It is78 called [[http://wiki.blender.org/index.php/Doc:2.6/Manual/Textures/Mapping/UV][UV-mapping]]. The three-dimensional surface of a model is cut79 and smooshed until it fits on a two-dimensional image. You paint80 whatever you want on that image, and when the three-dimensional shape81 is rendered in a game the smooshing and cutting us reversed and the82 image appears on the three-dimensional object.84 To make a sense, interpret the UV-image as describing the distribution85 of that senses sensors. To get different types of sensors, you can86 either use a different color for each type of sensor, or use multiple87 UV-maps, each labeled with that sensor type. I generally use a white88 pixel to mean the presense of a sensor and a black pixel to mean the89 absense of a sensor, and use one UV-map for each sensor-type within a90 given sense. The paths to the images are not stored as the actual91 UV-map of the blender object but are instead referenced in the92 meta-data of the node.94 #+CAPTION: The UV-map for an enlongated icososphere. The white dots each represent a touch sensor. They are dense in the regions that describe the tip of the finger, and less dense along the dorsal side of the finger opposite the tip.95 #+ATTR_HTML: width="300"96 [[../images/finger-UV.png]]98 #+CAPTION: Ventral side of the UV-mapped finger. Notice the density of touch sensors at the tip.99 #+ATTR_HTML: width="300"100 [[../images/finger-1.png]]102 #+CAPTION: Side view of the UV-mapped finger.103 #+ATTR_HTML: width="300"104 [[../images/finger-2.png]]106 #+CAPTION: Head on view of the finger. In both the head and side views you can see the divide where the touch-sensors transition from high density to low density.107 #+ATTR_HTML: width="300"108 [[../images/finger-3.png]]110 The following code loads images and gets the locations of the white111 pixels so that they can be used to create senses. =(load-image)= finds112 images using jMonkeyEngine's asset-manager, so the image path is113 expected to be relative to the =assets= directory. Thanks to Dylan114 for the beautiful version of =(filter-pixels)=.116 #+name: topology-1117 #+begin_src clojure118 (defn load-image119 "Load an image as a BufferedImage using the asset-manager system."120 [asset-relative-path]121 (ImageToAwt/convert122 (.getImage (.loadTexture (asset-manager) asset-relative-path))123 false false 0))125 (def white 0xFFFFFF)127 (defn white? [rgb]128 (= (bit-and white rgb) white))130 (defn filter-pixels131 "List the coordinates of all pixels matching pred, within the bounds132 provided. If bounds are not specified then the entire image is133 searched.134 bounds -> [x0 y0 width height]"135 {:author "Dylan Holmes"}136 ([pred #^BufferedImage image]137 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))138 ([pred #^BufferedImage image [x0 y0 width height]]139 ((fn accumulate [x y matches]140 (cond141 (>= y (+ height y0)) matches142 (>= x (+ width x0)) (recur 0 (inc y) matches)143 (pred (.getRGB image x y))144 (recur (inc x) y (conj matches [x y]))145 :else (recur (inc x) y matches)))146 x0 y0 [])))148 (defn white-coordinates149 "Coordinates of all the white pixels in a subset of the image."150 ([#^BufferedImage image bounds]151 (filter-pixels white? image bounds))152 ([#^BufferedImage image]153 (filter-pixels white? image)))154 #+end_src156 ** Topology158 Information from the senses is transmitted to the brain via bundles of159 axons, whether it be the optic nerve or the spinal cord. While these160 bundles more or less perserve the overall topology of a sense's161 two-dimensional surface, they do not perserve the percise euclidean162 distances between every sensor. =(collapse)= is here to smoosh the163 sensors described by a UV-map into a contigous region that still164 perserves the topology of the original sense.166 #+name: topology-2167 #+begin_src clojure168 (defn average [coll]169 (/ (reduce + coll) (count coll)))171 (defn collapse-1d172 "One dimensional analogue of collapse."173 [center line]174 (let [length (count line)175 num-above (count (filter (partial < center) line))176 num-below (- length num-above)]177 (range (- center num-below)178 (+ center num-above))))180 (defn collapse181 "Take a set of pairs of integers and collapse them into a182 contigous bitmap with no \"holes\"."183 [points]184 (if (empty? points) []185 (let186 [num-points (count points)187 center (vector188 (int (average (map first points)))189 (int (average (map first points))))190 flattened191 (reduce192 concat193 (map194 (fn [column]195 (map vector196 (map first column)197 (collapse-1d (second center)198 (map second column))))199 (partition-by first (sort-by first points))))200 squeezed201 (reduce202 concat203 (map204 (fn [row]205 (map vector206 (collapse-1d (first center)207 (map first row))208 (map second row)))209 (partition-by second (sort-by second flattened))))210 relocated211 (let [min-x (apply min (map first squeezed))212 min-y (apply min (map second squeezed))]213 (map (fn [[x y]]214 [(- x min-x)215 (- y min-y)])216 squeezed))]217 relocated)))218 #+end_src219 * Viewing Sense Data221 It's vital to /see/ the sense data to make sure that everything is222 behaving as it should. =(view-sense)= and its helper, =(view-image)=223 are here so that each sense can define its own way of turning224 sense-data into pictures, while the actual rendering of said pictures225 stays in one central place. =(points->image)= helps senses generate a226 base image onto which they can overlay actual sense data.228 #+name: view-senses229 #+begin_src clojure230 (in-ns 'cortex.sense)232 (defn view-image233 "Initailizes a JPanel on which you may draw a BufferedImage.234 Returns a function that accepts a BufferedImage and draws it to the235 JPanel. If given a directory it will save the images as png files236 starting at 0000000.png and incrementing from there."237 ([#^File save]238 (let [idx (atom -1)239 image240 (atom241 (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))242 panel243 (proxy [JPanel] []244 (paint245 [graphics]246 (proxy-super paintComponent graphics)247 (.drawImage graphics @image 0 0 nil)))248 frame (JFrame. "Display Image")]249 (SwingUtilities/invokeLater250 (fn []251 (doto frame252 (-> (.getContentPane) (.add panel))253 (.pack)254 (.setLocationRelativeTo nil)255 (.setResizable true)256 (.setVisible true))))257 (fn [#^BufferedImage i]258 (reset! image i)259 (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))260 (.repaint panel 0 0 (.getWidth i) (.getHeight i))261 (if save262 (ImageIO/write263 i "png"264 (File. save (format "%07d.png" (swap! idx inc))))))))265 ([] (view-image nil)))267 (defn view-sense268 "Take a kernel that produces a BufferedImage from some sense data269 and return a function which takes a list of sense data, uses the270 kernel to convert to images, and displays those images, each in271 its own JFrame."272 [sense-display-kernel]273 (let [windows (atom [])]274 (fn this275 ([data]276 (this data nil))277 ([data save-to]278 (if (> (count data) (count @windows))279 (reset!280 windows281 (doall282 (map283 (fn [idx]284 (if save-to285 (let [dir (File. save-to (str idx))]286 (.mkdir dir)287 (view-image dir))288 (view-image))) (range (count data))))))289 (dorun290 (map291 (fn [display datum]292 (display (sense-display-kernel datum)))293 @windows data))))))296 (defn points->image297 "Take a collection of points and visuliaze it as a BufferedImage."298 [points]299 (if (empty? points)300 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)301 (let [xs (vec (map first points))302 ys (vec (map second points))303 x0 (apply min xs)304 y0 (apply min ys)305 width (- (apply max xs) x0)306 height (- (apply max ys) y0)307 image (BufferedImage. (inc width) (inc height)308 BufferedImage/TYPE_INT_RGB)]309 (dorun310 (for [x (range (.getWidth image))311 y (range (.getHeight image))]312 (.setRGB image x y 0xFF0000)))313 (dorun314 (for [index (range (count points))]315 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))316 image)))318 (defn gray319 "Create a gray RGB pixel with R, G, and B set to num. num must be320 between 0 and 255."321 [num]322 (+ num323 (bit-shift-left num 8)324 (bit-shift-left num 16)))325 #+end_src327 * Building a Sense from Nodes328 My method for defining senses in blender is the following:330 Senses like vision and hearing are localized to a single point331 and follow a particular object around. For these:333 - Create a single top-level empty node whose name is the name of the sense334 - Add empty nodes which each contain meta-data relevant335 to the sense, including a UV-map describing the number/distribution336 of sensors if applicipable.337 - Make each empty-node the child of the top-level338 node. =(sense-nodes)= below generates functions to find these children.340 For touch, store the path to the UV-map which describes touch-sensors in the341 meta-data of the object to which that map applies.343 Each sense provides code that analyzes the Node structure of the344 creature and creates sense-functions. They also modify the Node345 structure if necessary.347 Empty nodes created in blender have no appearance or physical presence348 in jMonkeyEngine, but do appear in the scene graph. Empty nodes that349 represent a sense which "follows" another geometry (like eyes and350 ears) follow the closest physical object. =(closest-node)= finds this351 closest object given the Creature and a particular empty node.353 #+name: node-1354 #+begin_src clojure355 (defn sense-nodes356 "For some senses there is a special empty blender node whose357 children are considered markers for an instance of that sense. This358 function generates functions to find those children, given the name359 of the special parent node."360 [parent-name]361 (fn [#^Node creature]362 (if-let [sense-node (.getChild creature parent-name)]363 (seq (.getChildren sense-node))364 (do (println-repl "could not find" parent-name "node") []))))366 (defn closest-node367 "Return the physical node in creature which is closest to the given368 node."369 [#^Node creature #^Node empty]370 (loop [radius (float 0.01)]371 (let [results (CollisionResults.)]372 (.collideWith373 creature374 (BoundingBox. (.getWorldTranslation empty)375 radius radius radius)376 results)377 (if-let [target (first results)]378 (.getGeometry target)379 (recur (float (* 2 radius)))))))381 (defn world-to-local382 "Convert the world coordinates into coordinates relative to the383 object (i.e. local coordinates), taking into account the rotation384 of object."385 [#^Spatial object world-coordinate]386 (.worldToLocal object world-coordinate nil))388 (defn local-to-world389 "Convert the local coordinates into world relative coordinates"390 [#^Spatial object local-coordinate]391 (.localToWorld object local-coordinate nil))392 #+end_src394 ** Sense Binding396 =(bind-sense)= binds either a Camera or a Listener object to any397 object so that they will follow that object no matter how it398 moves. It is used to create both eyes and ears.400 #+name: node-2401 #+begin_src clojure402 (defn bind-sense403 "Bind the sense to the Spatial such that it will maintain its404 current position relative to the Spatial no matter how the spatial405 moves. 'sense can be either a Camera or Listener object."406 [#^Spatial obj sense]407 (let [sense-offset (.subtract (.getLocation sense)408 (.getWorldTranslation obj))409 initial-sense-rotation (Quaternion. (.getRotation sense))410 base-anti-rotation (.inverse (.getWorldRotation obj))]411 (.addControl412 obj413 (proxy [AbstractControl] []414 (controlUpdate [tpf]415 (let [total-rotation416 (.mult base-anti-rotation (.getWorldRotation obj))]417 (.setLocation418 sense419 (.add420 (.mult total-rotation sense-offset)421 (.getWorldTranslation obj)))422 (.setRotation423 sense424 (.mult total-rotation initial-sense-rotation))))425 (controlRender [_ _])))))426 #+end_src428 Here is some example code which shows how a camera bound to a blue box429 with =(bind-sense)= moves as the box is buffeted by white cannonballs.431 #+name: test432 #+begin_src clojure433 (defn test-bind-sense434 "Show a camera that stays in the same relative position to a blue435 cube."436 []437 (let [eye-pos (Vector3f. 0 30 0)438 rock (box 1 1 1 :color ColorRGBA/Blue439 :position (Vector3f. 0 10 0)440 :mass 30)441 table (box 3 1 10 :color ColorRGBA/Gray :mass 0442 :position (Vector3f. 0 -3 0))]443 (world444 (nodify [rock table])445 standard-debug-controls446 (fn init [world]447 (let [cam (doto (.clone (.getCamera world))448 (.setLocation eye-pos)449 (.lookAt Vector3f/ZERO450 Vector3f/UNIT_X))]451 (bind-sense rock cam)452 (.setTimer world (RatchetTimer. 60))453 (Capture/captureVideo454 world (File. "/home/r/proj/cortex/render/bind-sense0"))455 (add-camera!456 world cam457 (comp (view-image458 (File. "/home/r/proj/cortex/render/bind-sense1"))459 BufferedImage!))460 (add-camera! world (.getCamera world) no-op)))461 no-op)))462 #+end_src464 #+begin_html465 <video controls="controls" width="755">466 <source src="../video/bind-sense.ogg" type="video/ogg"467 preload="none" poster="../images/aurellem-1280x480.png" />468 </video>469 #+end_html471 With this, eyes are easy --- you just bind the camera closer to the472 desired object, and set it to look outward instead of inward as it473 does in the video.475 (nb : the video was created with the following commands)477 *** Combine Frames with ImageMagick478 #+begin_src clojure :results silent479 (ns cortex.video.magick480 (:import java.io.File)481 (:use clojure.contrib.shell-out))483 (defn combine-images []484 (let485 [idx (atom -1)486 left (rest487 (sort488 (file-seq (File. "/home/r/proj/cortex/render/bind-sense0/"))))489 right (rest490 (sort491 (file-seq492 (File. "/home/r/proj/cortex/render/bind-sense1/"))))493 sub (rest494 (sort495 (file-seq496 (File. "/home/r/proj/cortex/render/bind-senseB/"))))497 sub* (concat sub (repeat 1000 (last sub)))]498 (dorun499 (map500 (fn [im-1 im-2 sub]501 (sh "convert" (.getCanonicalPath im-1)502 (.getCanonicalPath im-2) "+append"503 (.getCanonicalPath sub) "-append"504 (.getCanonicalPath505 (File. "/home/r/proj/cortex/render/bind-sense/"506 (format "%07d.png" (swap! idx inc))))))507 left right sub*))))508 #+end_src510 *** Encode Frames with ffmpeg512 #+begin_src sh :results silent513 cd /home/r/proj/cortex/render/514 ffmpeg -r 60 -b 9000k -i bind-sense/%07d.png bind-sense.ogg515 #+end_src517 * Headers518 #+name: sense-header519 #+begin_src clojure520 (ns cortex.sense521 "Here are functions useful in the construction of two or more522 sensors/effectors."523 {:author "Robert McInytre"}524 (:use (cortex world util))525 (:import ij.process.ImageProcessor)526 (:import jme3tools.converters.ImageToAwt)527 (:import java.awt.image.BufferedImage)528 (:import com.jme3.collision.CollisionResults)529 (:import com.jme3.bounding.BoundingBox)530 (:import (com.jme3.scene Node Spatial))531 (:import com.jme3.scene.control.AbstractControl)532 (:import (com.jme3.math Quaternion Vector3f))533 (:import javax.imageio.ImageIO)534 (:import java.io.File)535 (:import (javax.swing JPanel JFrame SwingUtilities)))536 #+end_src538 #+name: test-header539 #+begin_src clojure540 (ns cortex.test.sense541 (:use (cortex world util sense vision))542 (:import543 java.io.File544 (com.jme3.math Vector3f ColorRGBA)545 (com.aurellem.capture RatchetTimer Capture)))546 #+end_src548 * Source Listing549 - [[../src/cortex/sense.clj][cortex.sense]]550 - [[../src/cortex/test/sense.clj][cortex.test.sense]]551 - [[../assets/Models/subtitles/subtitles.blend][subtitles.blend]]552 - [[../assets/Models/subtitles/Lake_CraterLake03_sm.hdr][subtitles reflection map]]553 #+html: <ul> <li> <a href="../org/sense.org">This org file</a> </li> </ul>555 * Next556 Now that some of the preliminaries are out of the way, in the [[./body.org][next557 post]] I'll create a simulated body.560 * COMMENT generate source561 #+begin_src clojure :tangle ../src/cortex/sense.clj562 <<sense-header>>563 <<blender-1>>564 <<blender-2>>565 <<topology-1>>566 <<topology-2>>567 <<node-1>>568 <<node-2>>569 <<view-senses>>570 #+end_src572 #+begin_src clojure :tangle ../src/cortex/test/sense.clj573 <<test-header>>574 <<test>>575 #+end_src577 #+begin_src clojure :tangle ../src/cortex/video/magick.clj578 <<magick>>579 #+end_src