annotate org/sense.org @ 235:be78d7bd6920

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