view org/sense.org @ 339:0efa36180e8a

merge changes from laptop
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Jul 2012 10:03:02 -0500
parents d37ccb6c888f
children 02cc0734a976
line wrap: on
line source
1 #+title: Helper Functions / Motivations
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: sensory utilities
5 #+keywords: simulation, jMonkeyEngine3, clojure, simulated senses
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
9 * Blender Utilities
10 In blender, any object can be assigned an arbitrary number of key-value
11 pairs which are called "Custom Properties". These are accessible in
12 jMonkeyEngine when blender files are imported with the
13 =BlenderLoader=. =meta-data= extracts these properties.
15 #+name: blender-1
16 #+begin_src clojure
17 (in-ns 'cortex.sense)
18 (defn meta-data
19 "Get the meta-data for a node created with blender."
20 [blender-node key]
21 (if-let [data (.getUserData blender-node "properties")]
22 ;; this part is to accomodate weird blender properties
23 ;; as well as sensible clojure maps.
24 (.findValue data key)
25 (.getUserData blender-node key)))
27 #+end_src
29 #+results: blender-1
30 : #'cortex.sense/meta-data
32 Blender uses a different coordinate system than jMonkeyEngine so it
33 is useful to be able to convert between the two. These only come into
34 play when the meta-data of a node refers to a vector in the blender
35 coordinate system.
37 #+name: blender-2
38 #+begin_src clojure
39 (defn jme-to-blender
40 "Convert from JME coordinates to Blender coordinates"
41 [#^Vector3f in]
42 (Vector3f. (.getX in) (- (.getZ in)) (.getY in)))
44 (defn blender-to-jme
45 "Convert from Blender coordinates to JME coordinates"
46 [#^Vector3f in]
47 (Vector3f. (.getX in) (.getZ in) (- (.getY in))))
48 #+end_src
50 * Sense Topology
52 Human beings are three-dimensional objects, and the nerves that
53 transmit data from our various sense organs to our brain are
54 essentially one-dimensional. This leaves up to two dimensions in which
55 our sensory information may flow. For example, imagine your skin: it
56 is a two-dimensional surface around a three-dimensional object (your
57 body). It has discrete touch sensors embedded at various points, and
58 the density of these sensors corresponds to the sensitivity of that
59 region of skin. Each touch sensor connects to a nerve, all of which
60 eventually are bundled together as they travel up the spinal cord to
61 the brain. Intersect the spinal nerves with a guillotining plane and
62 you will see all of the sensory data of the skin revealed in a roughly
63 circular two-dimensional image which is the cross section of the
64 spinal cord. Points on this image that are close together in this
65 circle represent touch sensors that are /probably/ close together on
66 the skin, although there is of course some cutting and rearrangement
67 that has to be done to transfer the complicated surface of the skin
68 onto a two dimensional image.
70 Most human senses consist of many discrete sensors of various
71 properties distributed along a surface at various densities. For
72 skin, it is Pacinian corpuscles, Meissner's corpuscles, Merkel's
73 disks, and Ruffini's endings, which detect pressure and vibration of
74 various intensities. For ears, it is the stereocilia distributed
75 along the basilar membrane inside the cochlea; each one is sensitive
76 to a slightly different frequency of sound. For eyes, it is rods
77 and cones distributed along the surface of the retina. In each case,
78 we can describe the sense with a surface and a distribution of sensors
79 along that surface.
81 ** UV-maps
83 Blender and jMonkeyEngine already have support for exactly this sort
84 of data structure because it is used to "skin" models for games. It is
85 called [[http://wiki.blender.org/index.php/Doc:2.6/Manual/Textures/Mapping/UV][UV-mapping]]. The three-dimensional surface of a model is cut
86 and smooshed until it fits on a two-dimensional image. You paint
87 whatever you want on that image, and when the three-dimensional shape
88 is rendered in a game the smooshing and cutting us reversed and the
89 image appears on the three-dimensional object.
91 To make a sense, interpret the UV-image as describing the distribution
92 of that senses sensors. To get different types of sensors, you can
93 either use a different color for each type of sensor, or use multiple
94 UV-maps, each labeled with that sensor type. I generally use a white
95 pixel to mean the presence of a sensor and a black pixel to mean the
96 absence of a sensor, and use one UV-map for each sensor-type within a
97 given sense. The paths to the images are not stored as the actual
98 UV-map of the blender object but are instead referenced in the
99 meta-data of the node.
101 #+CAPTION: The UV-map for an elongated 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.
102 #+ATTR_HTML: width="300"
103 [[../images/finger-UV.png]]
105 #+CAPTION: Ventral side of the UV-mapped finger. Notice the density of touch sensors at the tip.
106 #+ATTR_HTML: width="300"
107 [[../images/finger-1.png]]
109 #+CAPTION: Side view of the UV-mapped finger.
110 #+ATTR_HTML: width="300"
111 [[../images/finger-2.png]]
113 #+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.
114 #+ATTR_HTML: width="300"
115 [[../images/finger-3.png]]
117 The following code loads images and gets the locations of the white
118 pixels so that they can be used to create senses. =load-image= finds
119 images using jMonkeyEngine's asset-manager, so the image path is
120 expected to be relative to the =assets= directory. Thanks to Dylan
121 for the beautiful version of =filter-pixels=.
123 #+name: topology-1
124 #+begin_src clojure
125 (defn load-image
126 "Load an image as a BufferedImage using the asset-manager system."
127 [asset-relative-path]
128 (ImageToAwt/convert
129 (.getImage (.loadTexture (asset-manager) asset-relative-path))
130 false false 0))
132 (def white 0xFFFFFF)
134 (defn white? [rgb]
135 (= (bit-and white rgb) white))
137 (defn filter-pixels
138 "List the coordinates of all pixels matching pred, within the bounds
139 provided. If bounds are not specified then the entire image is
140 searched.
141 bounds -> [x0 y0 width height]"
142 {:author "Dylan Holmes"}
143 ([pred #^BufferedImage image]
144 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
145 ([pred #^BufferedImage image [x0 y0 width height]]
146 ((fn accumulate [x y matches]
147 (cond
148 (>= y (+ height y0)) matches
149 (>= x (+ width x0)) (recur 0 (inc y) matches)
150 (pred (.getRGB image x y))
151 (recur (inc x) y (conj matches [x y]))
152 :else (recur (inc x) y matches)))
153 x0 y0 [])))
155 (defn white-coordinates
156 "Coordinates of all the white pixels in a subset of the image."
157 ([#^BufferedImage image bounds]
158 (filter-pixels white? image bounds))
159 ([#^BufferedImage image]
160 (filter-pixels white? image)))
161 #+end_src
163 ** Topology
165 Information from the senses is transmitted to the brain via bundles of
166 axons, whether it be the optic nerve or the spinal cord. While these
167 bundles more or less preserve the overall topology of a sense's
168 two-dimensional surface, they do not preserve the precise euclidean
169 distances between every sensor. =collapse= is here to smoosh the
170 sensors described by a UV-map into a contiguous region that still
171 preserves the topology of the original sense.
173 #+name: topology-2
174 #+begin_src clojure
175 (in-ns 'cortex.sense)
177 (defn average [coll]
178 (/ (reduce + coll) (count coll)))
180 (defn- collapse-1d
181 "One dimensional helper for collapse."
182 [center line]
183 (let [length (count line)
184 num-above (count (filter (partial < center) line))
185 num-below (- length num-above)]
186 (range (- center num-below)
187 (+ center num-above))))
189 (defn collapse
190 "Take a sequence of pairs of integers and collapse them into a
191 contiguous bitmap with no \"holes\" or negative entries, as close to
192 the origin [0 0] as the shape permits. The order of the points is
193 preserved.
195 eg.
196 (collapse [[-5 5] [5 5] --> [[0 1] [1 1]
197 [-5 -5] [5 -5]]) --> [0 0] [1 0]]
199 (collapse [[-5 5] [-5 -5] --> [[0 1] [0 0]
200 [ 5 -5] [ 5 5]]) --> [1 0] [1 1]]"
201 [points]
202 (if (empty? points) []
203 (let
204 [num-points (count points)
205 center (vector
206 (int (average (map first points)))
207 (int (average (map first points))))
208 flattened
209 (reduce
210 concat
211 (map
212 (fn [column]
213 (map vector
214 (map first column)
215 (collapse-1d (second center)
216 (map second column))))
217 (partition-by first (sort-by first points))))
218 squeezed
219 (reduce
220 concat
221 (map
222 (fn [row]
223 (map vector
224 (collapse-1d (first center)
225 (map first row))
226 (map second row)))
227 (partition-by second (sort-by second flattened))))
228 relocated
229 (let [min-x (apply min (map first squeezed))
230 min-y (apply min (map second squeezed))]
231 (map (fn [[x y]]
232 [(- x min-x)
233 (- y min-y)])
234 squeezed))
235 point-correspondence
236 (zipmap (sort points) (sort relocated))
238 original-order
239 (vec (map point-correspondence points))]
240 original-order)))
241 #+end_src
242 * Viewing Sense Data
244 It's vital to /see/ the sense data to make sure that everything is
245 behaving as it should. =view-sense= and its helper, =view-image=
246 are here so that each sense can define its own way of turning
247 sense-data into pictures, while the actual rendering of said pictures
248 stays in one central place. =points->image= helps senses generate a
249 base image onto which they can overlay actual sense data.
251 #+name: view-senses
252 #+begin_src clojure
253 (in-ns 'cortex.sense)
255 (defn view-image
256 "Initializes a JPanel on which you may draw a BufferedImage.
257 Returns a function that accepts a BufferedImage and draws it to the
258 JPanel. If given a directory it will save the images as png files
259 starting at 0000000.png and incrementing from there."
260 ([#^File save]
261 (let [idx (atom -1)
262 image
263 (atom
264 (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
265 panel
266 (proxy [JPanel] []
267 (paint
268 [graphics]
269 (proxy-super paintComponent graphics)
270 (.drawImage graphics @image 0 0 nil)))
271 frame (JFrame. "Display Image")]
272 (SwingUtilities/invokeLater
273 (fn []
274 (doto frame
275 (-> (.getContentPane) (.add panel))
276 (.pack)
277 (.setLocationRelativeTo nil)
278 (.setResizable true)
279 (.setVisible true))))
280 (fn [#^BufferedImage i]
281 (reset! image i)
282 (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
283 (.repaint panel 0 0 (.getWidth i) (.getHeight i))
284 (if save
285 (ImageIO/write
286 i "png"
287 (File. save (format "%07d.png" (swap! idx inc))))))))
288 ([] (view-image nil)))
290 (defn view-sense
291 "Take a kernel that produces a BufferedImage from some sense data
292 and return a function which takes a list of sense data, uses the
293 kernel to convert to images, and displays those images, each in
294 its own JFrame."
295 [sense-display-kernel]
296 (let [windows (atom [])]
297 (fn this
298 ([data]
299 (this data nil))
300 ([data save-to]
301 (if (> (count data) (count @windows))
302 (reset!
303 windows
304 (doall
305 (map
306 (fn [idx]
307 (if save-to
308 (let [dir (File. save-to (str idx))]
309 (.mkdir dir)
310 (view-image dir))
311 (view-image))) (range (count data))))))
312 (dorun
313 (map
314 (fn [display datum]
315 (display (sense-display-kernel datum)))
316 @windows data))))))
319 (defn points->image
320 "Take a collection of points and visualize it as a BufferedImage."
321 [points]
322 (if (empty? points)
323 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
324 (let [xs (vec (map first points))
325 ys (vec (map second points))
326 x0 (apply min xs)
327 y0 (apply min ys)
328 width (- (apply max xs) x0)
329 height (- (apply max ys) y0)
330 image (BufferedImage. (inc width) (inc height)
331 BufferedImage/TYPE_INT_RGB)]
332 (dorun
333 (for [x (range (.getWidth image))
334 y (range (.getHeight image))]
335 (.setRGB image x y 0xFF0000)))
336 (dorun
337 (for [index (range (count points))]
338 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
339 image)))
341 (defn gray
342 "Create a gray RGB pixel with R, G, and B set to num. num must be
343 between 0 and 255."
344 [num]
345 (+ num
346 (bit-shift-left num 8)
347 (bit-shift-left num 16)))
348 #+end_src
350 * Building a Sense from Nodes
351 My method for defining senses in blender is the following:
353 Senses like vision and hearing are localized to a single point
354 and follow a particular object around. For these:
356 - Create a single top-level empty node whose name is the name of the sense
357 - Add empty nodes which each contain meta-data relevant
358 to the sense, including a UV-map describing the number/distribution
359 of sensors if applicable.
360 - Make each empty-node the child of the top-level
361 node. =sense-nodes= below generates functions to find these children.
363 For touch, store the path to the UV-map which describes touch-sensors in the
364 meta-data of the object to which that map applies.
366 Each sense provides code that analyzes the Node structure of the
367 creature and creates sense-functions. They also modify the Node
368 structure if necessary.
370 Empty nodes created in blender have no appearance or physical presence
371 in jMonkeyEngine, but do appear in the scene graph. Empty nodes that
372 represent a sense which "follows" another geometry (like eyes and
373 ears) follow the closest physical object. =closest-node= finds this
374 closest object given the Creature and a particular empty node.
376 #+name: node-1
377 #+begin_src clojure
378 (defn sense-nodes
379 "For some senses there is a special empty blender node whose
380 children are considered markers for an instance of that sense. This
381 function generates functions to find those children, given the name
382 of the special parent node."
383 [parent-name]
384 (fn [#^Node creature]
385 (if-let [sense-node (.getChild creature parent-name)]
386 (seq (.getChildren sense-node))
387 (do ;;(println-repl "could not find" parent-name "node")
388 []))))
390 (defn closest-node
391 "Return the physical node in creature which is closest to the given
392 node."
393 [#^Node creature #^Node empty]
394 (loop [radius (float 0.01)]
395 (let [results (CollisionResults.)]
396 (.collideWith
397 creature
398 (BoundingBox. (.getWorldTranslation empty)
399 radius radius radius)
400 results)
401 (if-let [target (first results)]
402 (.getGeometry target)
403 (recur (float (* 2 radius)))))))
405 (defn world-to-local
406 "Convert the world coordinates into coordinates relative to the
407 object (i.e. local coordinates), taking into account the rotation
408 of object."
409 [#^Spatial object world-coordinate]
410 (.worldToLocal object world-coordinate nil))
412 (defn local-to-world
413 "Convert the local coordinates into world relative coordinates"
414 [#^Spatial object local-coordinate]
415 (.localToWorld object local-coordinate nil))
416 #+end_src
418 ** Sense Binding
420 =bind-sense= binds either a Camera or a Listener object to any
421 object so that they will follow that object no matter how it
422 moves. It is used to create both eyes and ears.
424 #+name: node-2
425 #+begin_src clojure
426 (defn bind-sense
427 "Bind the sense to the Spatial such that it will maintain its
428 current position relative to the Spatial no matter how the spatial
429 moves. 'sense can be either a Camera or Listener object."
430 [#^Spatial obj sense]
431 (let [sense-offset (.subtract (.getLocation sense)
432 (.getWorldTranslation obj))
433 initial-sense-rotation (Quaternion. (.getRotation sense))
434 base-anti-rotation (.inverse (.getWorldRotation obj))]
435 (.addControl
436 obj
437 (proxy [AbstractControl] []
438 (controlUpdate [tpf]
439 (let [total-rotation
440 (.mult base-anti-rotation (.getWorldRotation obj))]
441 (.setLocation
442 sense
443 (.add
444 (.mult total-rotation sense-offset)
445 (.getWorldTranslation obj)))
446 (.setRotation
447 sense
448 (.mult total-rotation initial-sense-rotation))))
449 (controlRender [_ _])))))
450 #+end_src
452 Here is some example code which shows how a camera bound to a blue box
453 with =bind-sense= moves as the box is buffeted by white cannonballs.
455 #+name: test
456 #+begin_src clojure
457 (in-ns 'cortex.test.sense)
459 (defn test-bind-sense
460 "Show a camera that stays in the same relative position to a blue
461 cube."
462 ([] (test-bind-sense false))
463 ([record?]
464 (let [eye-pos (Vector3f. 0 30 0)
465 rock (box 1 1 1 :color ColorRGBA/Blue
466 :position (Vector3f. 0 10 0)
467 :mass 30)
468 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
469 :position (Vector3f. 0 -3 0))]
470 (world
471 (nodify [rock table])
472 standard-debug-controls
473 (fn init [world]
474 (let [cam (doto (.clone (.getCamera world))
475 (.setLocation eye-pos)
476 (.lookAt Vector3f/ZERO
477 Vector3f/UNIT_X))]
478 (bind-sense rock cam)
479 (.setTimer world (RatchetTimer. 60))
480 (if record?
481 (Capture/captureVideo
482 world
483 (File. "/home/r/proj/cortex/render/bind-sense0")))
484 (add-camera!
485 world cam
486 (comp
487 (view-image
488 (if record?
489 (File. "/home/r/proj/cortex/render/bind-sense1")))
490 BufferedImage!))
491 (add-camera! world (.getCamera world) no-op)))
492 no-op))))
493 #+end_src
495 #+begin_html
496 <video controls="controls" width="755">
497 <source src="../video/bind-sense.ogg" type="video/ogg"
498 preload="none" poster="../images/aurellem-1280x480.png" />
499 </video>
500 <br> <a href="http://youtu.be/DvoN2wWQ_6o"> YouTube </a>
501 #+end_html
503 With this, eyes are easy --- you just bind the camera closer to the
504 desired object, and set it to look outward instead of inward as it
505 does in the video.
507 (nb : the video was created with the following commands)
509 *** Combine Frames with ImageMagick
510 #+begin_src clojure :results silent
511 (ns cortex.video.magick
512 (:import java.io.File)
513 (:use clojure.java.shell))
515 (defn combine-images []
516 (let
517 [idx (atom -1)
518 left (rest
519 (sort
520 (file-seq (File. "/home/r/proj/cortex/render/bind-sense0/"))))
521 right (rest
522 (sort
523 (file-seq
524 (File. "/home/r/proj/cortex/render/bind-sense1/"))))
525 sub (rest
526 (sort
527 (file-seq
528 (File. "/home/r/proj/cortex/render/bind-senseB/"))))
529 sub* (concat sub (repeat 1000 (last sub)))]
530 (dorun
531 (map
532 (fn [im-1 im-2 sub]
533 (sh "convert" (.getCanonicalPath im-1)
534 (.getCanonicalPath im-2) "+append"
535 (.getCanonicalPath sub) "-append"
536 (.getCanonicalPath
537 (File. "/home/r/proj/cortex/render/bind-sense/"
538 (format "%07d.png" (swap! idx inc))))))
539 left right sub*))))
540 #+end_src
542 *** Encode Frames with ffmpeg
544 #+begin_src sh :results silent
545 cd /home/r/proj/cortex/render/
546 ffmpeg -r 30 -i bind-sense/%07d.png -b:v 9000k -vcodec libtheora bind-sense.ogg
547 #+end_src
549 * Headers
550 #+name: sense-header
551 #+begin_src clojure
552 (ns cortex.sense
553 "Here are functions useful in the construction of two or more
554 sensors/effectors."
555 {:author "Robert McIntyre"}
556 (:use (cortex world util))
557 (:import ij.process.ImageProcessor)
558 (:import jme3tools.converters.ImageToAwt)
559 (:import java.awt.image.BufferedImage)
560 (:import com.jme3.collision.CollisionResults)
561 (:import com.jme3.bounding.BoundingBox)
562 (:import (com.jme3.scene Node Spatial))
563 (:import com.jme3.scene.control.AbstractControl)
564 (:import (com.jme3.math Quaternion Vector3f))
565 (:import javax.imageio.ImageIO)
566 (:import java.io.File)
567 (:import (javax.swing JPanel JFrame SwingUtilities)))
568 #+end_src
570 #+name: test-header
571 #+begin_src clojure
572 (ns cortex.test.sense
573 (:use (cortex world util sense vision))
574 (:import
575 java.io.File
576 (com.jme3.math Vector3f ColorRGBA)
577 (com.aurellem.capture RatchetTimer Capture)))
578 #+end_src
580 * Source Listing
581 - [[../src/cortex/sense.clj][cortex.sense]]
582 - [[../src/cortex/test/sense.clj][cortex.test.sense]]
583 - [[../assets/Models/subtitles/subtitles.blend][subtitles.blend]]
584 - [[../assets/Models/subtitles/Lake_CraterLake03_sm.hdr][subtitles reflection map]]
585 #+html: <ul> <li> <a href="../org/sense.org">This org file</a> </li> </ul>
586 - [[http://hg.bortreb.com ][source-repository]]
588 * Next
589 Now that some of the preliminaries are out of the way, in the [[./body.org][next
590 post]] I'll create a simulated body.
593 * COMMENT generate source
594 #+begin_src clojure :tangle ../src/cortex/sense.clj
595 <<sense-header>>
596 <<blender-1>>
597 <<blender-2>>
598 <<topology-1>>
599 <<topology-2>>
600 <<node-1>>
601 <<node-2>>
602 <<view-senses>>
603 #+end_src
605 #+begin_src clojure :tangle ../src/cortex/test/sense.clj
606 <<test-header>>
607 <<test>>
608 #+end_src
610 #+begin_src clojure :tangle ../src/cortex/video/magick.clj
611 <<magick>>
612 #+end_src