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@151
|
168 (defn average [coll]
|
rlm@151
|
169 (/ (reduce + coll) (count coll)))
|
rlm@151
|
170
|
rlm@151
|
171 (defn collapse-1d
|
rlm@182
|
172 "One dimensional analogue of collapse."
|
rlm@151
|
173 [center line]
|
rlm@151
|
174 (let [length (count line)
|
rlm@151
|
175 num-above (count (filter (partial < center) line))
|
rlm@151
|
176 num-below (- length num-above)]
|
rlm@151
|
177 (range (- center num-below)
|
rlm@151
|
178 (+ center num-above))))
|
rlm@151
|
179
|
rlm@151
|
180 (defn collapse
|
rlm@151
|
181 "Take a set of pairs of integers and collapse them into a
|
rlm@182
|
182 contigous bitmap with no \"holes\"."
|
rlm@151
|
183 [points]
|
rlm@151
|
184 (if (empty? points) []
|
rlm@151
|
185 (let
|
rlm@151
|
186 [num-points (count points)
|
rlm@151
|
187 center (vector
|
rlm@151
|
188 (int (average (map first points)))
|
rlm@151
|
189 (int (average (map first points))))
|
rlm@151
|
190 flattened
|
rlm@151
|
191 (reduce
|
rlm@151
|
192 concat
|
rlm@151
|
193 (map
|
rlm@151
|
194 (fn [column]
|
rlm@151
|
195 (map vector
|
rlm@151
|
196 (map first column)
|
rlm@151
|
197 (collapse-1d (second center)
|
rlm@151
|
198 (map second column))))
|
rlm@151
|
199 (partition-by first (sort-by first points))))
|
rlm@151
|
200 squeezed
|
rlm@151
|
201 (reduce
|
rlm@151
|
202 concat
|
rlm@151
|
203 (map
|
rlm@151
|
204 (fn [row]
|
rlm@151
|
205 (map vector
|
rlm@151
|
206 (collapse-1d (first center)
|
rlm@151
|
207 (map first row))
|
rlm@151
|
208 (map second row)))
|
rlm@151
|
209 (partition-by second (sort-by second flattened))))
|
rlm@182
|
210 relocated
|
rlm@151
|
211 (let [min-x (apply min (map first squeezed))
|
rlm@151
|
212 min-y (apply min (map second squeezed))]
|
rlm@151
|
213 (map (fn [[x y]]
|
rlm@151
|
214 [(- x min-x)
|
rlm@151
|
215 (- y min-y)])
|
rlm@151
|
216 squeezed))]
|
rlm@182
|
217 relocated)))
|
rlm@198
|
218 #+end_src
|
rlm@198
|
219 * Viewing Sense Data
|
rlm@151
|
220
|
rlm@198
|
221 It's vital to /see/ the sense data to make sure that everything is
|
rlm@200
|
222 behaving as it should. =(view-sense)= and its helper, =(view-image)=
|
rlm@200
|
223 are here so that each sense can define its own way of turning
|
rlm@200
|
224 sense-data into pictures, while the actual rendering of said pictures
|
rlm@200
|
225 stays in one central place. =(points->image)= helps senses generate a
|
rlm@200
|
226 base image onto which they can overlay actual sense data.
|
rlm@198
|
227
|
rlm@199
|
228 #+name: view-senses
|
rlm@198
|
229 #+begin_src clojure
|
rlm@199
|
230 (in-ns 'cortex.sense)
|
rlm@198
|
231
|
rlm@199
|
232 (defn view-image
|
rlm@199
|
233 "Initailizes a JPanel on which you may draw a BufferedImage.
|
rlm@199
|
234 Returns a function that accepts a BufferedImage and draws it to the
|
rlm@199
|
235 JPanel. If given a directory it will save the images as png files
|
rlm@199
|
236 starting at 0000000.png and incrementing from there."
|
rlm@199
|
237 ([#^File save]
|
rlm@199
|
238 (let [idx (atom -1)
|
rlm@199
|
239 image
|
rlm@199
|
240 (atom
|
rlm@199
|
241 (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
|
rlm@199
|
242 panel
|
rlm@199
|
243 (proxy [JPanel] []
|
rlm@199
|
244 (paint
|
rlm@199
|
245 [graphics]
|
rlm@199
|
246 (proxy-super paintComponent graphics)
|
rlm@199
|
247 (.drawImage graphics @image 0 0 nil)))
|
rlm@199
|
248 frame (JFrame. "Display Image")]
|
rlm@199
|
249 (SwingUtilities/invokeLater
|
rlm@199
|
250 (fn []
|
rlm@199
|
251 (doto frame
|
rlm@199
|
252 (-> (.getContentPane) (.add panel))
|
rlm@199
|
253 (.pack)
|
rlm@199
|
254 (.setLocationRelativeTo nil)
|
rlm@199
|
255 (.setResizable true)
|
rlm@199
|
256 (.setVisible true))))
|
rlm@199
|
257 (fn [#^BufferedImage i]
|
rlm@199
|
258 (reset! image i)
|
rlm@199
|
259 (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
|
rlm@199
|
260 (.repaint panel 0 0 (.getWidth i) (.getHeight i))
|
rlm@199
|
261 (if save
|
rlm@199
|
262 (ImageIO/write
|
rlm@199
|
263 i "png"
|
rlm@199
|
264 (File. save (format "%07d.png" (swap! idx inc))))))))
|
rlm@199
|
265 ([] (view-image nil)))
|
rlm@199
|
266
|
rlm@199
|
267 (defn view-sense
|
rlm@199
|
268 "Take a kernel that produces a BufferedImage from some sense data
|
rlm@199
|
269 and return a function which takes a list of sense data, uses the
|
rlm@199
|
270 kernel to convert to images, and displays those images, each in
|
rlm@199
|
271 its own JFrame."
|
rlm@199
|
272 [sense-display-kernel]
|
rlm@199
|
273 (let [windows (atom [])]
|
rlm@199
|
274 (fn [data]
|
rlm@199
|
275 (if (> (count data) (count @windows))
|
rlm@199
|
276 (reset!
|
rlm@199
|
277 windows (map (fn [_] (view-image)) (range (count data)))))
|
rlm@199
|
278 (dorun
|
rlm@199
|
279 (map
|
rlm@199
|
280 (fn [display datum]
|
rlm@199
|
281 (display (sense-display-kernel datum)))
|
rlm@199
|
282 @windows data)))))
|
rlm@199
|
283
|
rlm@200
|
284 (defn points->image
|
rlm@200
|
285 "Take a collection of points and visuliaze it as a BufferedImage."
|
rlm@200
|
286 [points]
|
rlm@200
|
287 (if (empty? points)
|
rlm@200
|
288 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
|
rlm@200
|
289 (let [xs (vec (map first points))
|
rlm@200
|
290 ys (vec (map second points))
|
rlm@200
|
291 x0 (apply min xs)
|
rlm@200
|
292 y0 (apply min ys)
|
rlm@200
|
293 width (- (apply max xs) x0)
|
rlm@200
|
294 height (- (apply max ys) y0)
|
rlm@200
|
295 image (BufferedImage. (inc width) (inc height)
|
rlm@200
|
296 BufferedImage/TYPE_INT_RGB)]
|
rlm@200
|
297 (dorun
|
rlm@200
|
298 (for [x (range (.getWidth image))
|
rlm@200
|
299 y (range (.getHeight image))]
|
rlm@200
|
300 (.setRGB image x y 0xFF0000)))
|
rlm@200
|
301 (dorun
|
rlm@200
|
302 (for [index (range (count points))]
|
rlm@200
|
303 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
|
rlm@200
|
304 image)))
|
rlm@200
|
305
|
rlm@198
|
306 (defn gray
|
rlm@198
|
307 "Create a gray RGB pixel with R, G, and B set to num. num must be
|
rlm@198
|
308 between 0 and 255."
|
rlm@198
|
309 [num]
|
rlm@198
|
310 (+ num
|
rlm@198
|
311 (bit-shift-left num 8)
|
rlm@198
|
312 (bit-shift-left num 16)))
|
rlm@197
|
313 #+end_src
|
rlm@197
|
314
|
rlm@198
|
315 * Building a Sense from Nodes
|
rlm@198
|
316 My method for defining senses in blender is the following:
|
rlm@198
|
317
|
rlm@198
|
318 Senses like vision and hearing are localized to a single point
|
rlm@198
|
319 and follow a particular object around. For these:
|
rlm@198
|
320
|
rlm@198
|
321 - Create a single top-level empty node whose name is the name of the sense
|
rlm@198
|
322 - Add empty nodes which each contain meta-data relevant
|
rlm@198
|
323 to the sense, including a UV-map describing the number/distribution
|
rlm@198
|
324 of sensors if applicipable.
|
rlm@198
|
325 - Make each empty-node the child of the top-level
|
rlm@198
|
326 node. =(sense-nodes)= below generates functions to find these children.
|
rlm@198
|
327
|
rlm@198
|
328 For touch, store the path to the UV-map which describes touch-sensors in the
|
rlm@198
|
329 meta-data of the object to which that map applies.
|
rlm@198
|
330
|
rlm@198
|
331 Each sense provides code that analyzes the Node structure of the
|
rlm@198
|
332 creature and creates sense-functions. They also modify the Node
|
rlm@198
|
333 structure if necessary.
|
rlm@198
|
334
|
rlm@198
|
335 Empty nodes created in blender have no appearance or physical presence
|
rlm@198
|
336 in jMonkeyEngine, but do appear in the scene graph. Empty nodes that
|
rlm@198
|
337 represent a sense which "follows" another geometry (like eyes and
|
rlm@198
|
338 ears) follow the closest physical object. =(closest-node)= finds this
|
rlm@198
|
339 closest object given the Creature and a particular empty node.
|
rlm@198
|
340
|
rlm@198
|
341 #+name: node-1
|
rlm@197
|
342 #+begin_src clojure
|
rlm@198
|
343 (defn sense-nodes
|
rlm@198
|
344 "For some senses there is a special empty blender node whose
|
rlm@198
|
345 children are considered markers for an instance of that sense. This
|
rlm@198
|
346 function generates functions to find those children, given the name
|
rlm@198
|
347 of the special parent node."
|
rlm@198
|
348 [parent-name]
|
rlm@198
|
349 (fn [#^Node creature]
|
rlm@198
|
350 (if-let [sense-node (.getChild creature parent-name)]
|
rlm@198
|
351 (seq (.getChildren sense-node))
|
rlm@198
|
352 (do (println-repl "could not find" parent-name "node") []))))
|
rlm@198
|
353
|
rlm@197
|
354 (defn closest-node
|
rlm@201
|
355 "Return the physical node in creature which is closest to the given
|
rlm@201
|
356 node."
|
rlm@198
|
357 [#^Node creature #^Node empty]
|
rlm@197
|
358 (loop [radius (float 0.01)]
|
rlm@197
|
359 (let [results (CollisionResults.)]
|
rlm@197
|
360 (.collideWith
|
rlm@197
|
361 creature
|
rlm@198
|
362 (BoundingBox. (.getWorldTranslation empty)
|
rlm@197
|
363 radius radius radius)
|
rlm@197
|
364 results)
|
rlm@197
|
365 (if-let [target (first results)]
|
rlm@197
|
366 (.getGeometry target)
|
rlm@197
|
367 (recur (float (* 2 radius)))))))
|
rlm@197
|
368
|
rlm@198
|
369 (defn world-to-local
|
rlm@198
|
370 "Convert the world coordinates into coordinates relative to the
|
rlm@198
|
371 object (i.e. local coordinates), taking into account the rotation
|
rlm@198
|
372 of object."
|
rlm@198
|
373 [#^Spatial object world-coordinate]
|
rlm@198
|
374 (.worldToLocal object world-coordinate nil))
|
rlm@198
|
375
|
rlm@198
|
376 (defn local-to-world
|
rlm@198
|
377 "Convert the local coordinates into world relative coordinates"
|
rlm@198
|
378 [#^Spatial object local-coordinate]
|
rlm@198
|
379 (.localToWorld object local-coordinate nil))
|
rlm@198
|
380 #+end_src
|
rlm@198
|
381
|
rlm@200
|
382 ** Sense Binding
|
rlm@200
|
383
|
rlm@198
|
384 =(bind-sense)= binds either a Camera or a Listener object to any
|
rlm@198
|
385 object so that they will follow that object no matter how it
|
rlm@199
|
386 moves. It is used to create both eyes and ears.
|
rlm@198
|
387
|
rlm@198
|
388 #+name: node-2
|
rlm@198
|
389 #+begin_src clojure
|
rlm@197
|
390 (defn bind-sense
|
rlm@197
|
391 "Bind the sense to the Spatial such that it will maintain its
|
rlm@197
|
392 current position relative to the Spatial no matter how the spatial
|
rlm@197
|
393 moves. 'sense can be either a Camera or Listener object."
|
rlm@197
|
394 [#^Spatial obj sense]
|
rlm@197
|
395 (let [sense-offset (.subtract (.getLocation sense)
|
rlm@197
|
396 (.getWorldTranslation obj))
|
rlm@197
|
397 initial-sense-rotation (Quaternion. (.getRotation sense))
|
rlm@197
|
398 base-anti-rotation (.inverse (.getWorldRotation obj))]
|
rlm@197
|
399 (.addControl
|
rlm@197
|
400 obj
|
rlm@197
|
401 (proxy [AbstractControl] []
|
rlm@197
|
402 (controlUpdate [tpf]
|
rlm@197
|
403 (let [total-rotation
|
rlm@197
|
404 (.mult base-anti-rotation (.getWorldRotation obj))]
|
rlm@197
|
405 (.setLocation
|
rlm@197
|
406 sense
|
rlm@197
|
407 (.add
|
rlm@197
|
408 (.mult total-rotation sense-offset)
|
rlm@197
|
409 (.getWorldTranslation obj)))
|
rlm@197
|
410 (.setRotation
|
rlm@197
|
411 sense
|
rlm@197
|
412 (.mult total-rotation initial-sense-rotation))))
|
rlm@197
|
413 (controlRender [_ _])))))
|
rlm@197
|
414 #+end_src
|
rlm@164
|
415
|
rlm@200
|
416 Here is some example code which shows how a camera bound to a blue box
|
rlm@200
|
417 with =(bind-sense)= moves as the box is buffeted by white cannonballs.
|
rlm@199
|
418
|
rlm@199
|
419 #+name: test
|
rlm@199
|
420 #+begin_src clojure
|
rlm@199
|
421 (defn test-bind-sense
|
rlm@201
|
422 "Show a camera that stays in the same relative position to a blue
|
rlm@201
|
423 cube."
|
rlm@199
|
424 []
|
rlm@201
|
425 (let [eye-pos (Vector3f. 0 30 0)
|
rlm@199
|
426 rock (box 1 1 1 :color ColorRGBA/Blue
|
rlm@199
|
427 :position (Vector3f. 0 10 0)
|
rlm@199
|
428 :mass 30)
|
rlm@199
|
429 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
|
rlm@199
|
430 :position (Vector3f. 0 -3 0))]
|
rlm@199
|
431 (world
|
rlm@199
|
432 (nodify [rock table])
|
rlm@199
|
433 standard-debug-controls
|
rlm@201
|
434 (fn init [world]
|
rlm@201
|
435 (let [cam (doto (.clone (.getCamera world))
|
rlm@201
|
436 (.setLocation eye-pos)
|
rlm@201
|
437 (.lookAt Vector3f/ZERO
|
rlm@201
|
438 Vector3f/UNIT_X))]
|
rlm@199
|
439 (bind-sense rock cam)
|
rlm@199
|
440 (.setTimer world (RatchetTimer. 60))
|
rlm@199
|
441 (Capture/captureVideo
|
rlm@199
|
442 world (File. "/home/r/proj/cortex/render/bind-sense0"))
|
rlm@199
|
443 (add-camera!
|
rlm@199
|
444 world cam
|
rlm@199
|
445 (comp (view-image
|
rlm@199
|
446 (File. "/home/r/proj/cortex/render/bind-sense1"))
|
rlm@199
|
447 BufferedImage!))
|
rlm@199
|
448 (add-camera! world (.getCamera world) no-op)))
|
rlm@199
|
449 no-op)))
|
rlm@199
|
450 #+end_src
|
rlm@199
|
451
|
rlm@199
|
452 #+begin_html
|
rlm@199
|
453 <video controls="controls" width="755">
|
rlm@199
|
454 <source src="../video/bind-sense.ogg" type="video/ogg"
|
rlm@199
|
455 preload="none" poster="../images/aurellem-1280x480.png" />
|
rlm@199
|
456 </video>
|
rlm@199
|
457 #+end_html
|
rlm@199
|
458
|
rlm@200
|
459 With this, eyes are easy --- you just bind the camera closer to the
|
rlm@200
|
460 desired object, and set it to look outward instead of inward as it
|
rlm@200
|
461 does in the video.
|
rlm@199
|
462
|
rlm@200
|
463 (nb : the video was created with the following commands)
|
rlm@199
|
464
|
rlm@200
|
465 *** Combine Frames with ImageMagick
|
rlm@199
|
466 #+begin_src clojure :results silent
|
rlm@199
|
467 (in-ns 'user)
|
rlm@199
|
468 (import java.io.File)
|
rlm@199
|
469 (use 'clojure.contrib.shell-out)
|
rlm@199
|
470 (let
|
rlm@199
|
471 [idx (atom -1)
|
rlm@199
|
472 left (rest
|
rlm@199
|
473 (sort
|
rlm@199
|
474 (file-seq (File. "/home/r/proj/cortex/render/bind-sense0/"))))
|
rlm@199
|
475 right (rest
|
rlm@199
|
476 (sort
|
rlm@200
|
477 (file-seq
|
rlm@200
|
478 (File. "/home/r/proj/cortex/render/bind-sense1/"))))
|
rlm@200
|
479 sub (rest
|
rlm@200
|
480 (sort
|
rlm@200
|
481 (file-seq
|
rlm@200
|
482 (File. "/home/r/proj/cortex/render/bind-senseB/"))))
|
rlm@200
|
483 sub* (concat sub (repeat 1000 (last sub)))]
|
rlm@199
|
484 (dorun
|
rlm@199
|
485 (map
|
rlm@200
|
486 (fn [im-1 im-2 sub]
|
rlm@199
|
487 (sh "convert" (.getCanonicalPath im-1)
|
rlm@199
|
488 (.getCanonicalPath im-2) "+append"
|
rlm@200
|
489 (.getCanonicalPath sub) "-append"
|
rlm@199
|
490 (.getCanonicalPath
|
rlm@199
|
491 (File. "/home/r/proj/cortex/render/bind-sense/"
|
rlm@199
|
492 (format "%07d.png" (swap! idx inc))))))
|
rlm@200
|
493 left right sub*)))
|
rlm@199
|
494 #+end_src
|
rlm@199
|
495
|
rlm@200
|
496 *** Encode Frames with ffmpeg
|
rlm@200
|
497
|
rlm@199
|
498 #+begin_src sh :results silent
|
rlm@199
|
499 cd /home/r/proj/cortex/render/
|
rlm@199
|
500 ffmpeg -r 60 -b 9000k -i bind-sense/%07d.png bind-sense.ogg
|
rlm@199
|
501 #+end_src
|
rlm@199
|
502
|
rlm@211
|
503 * Headers
|
rlm@211
|
504 #+name: sense-header
|
rlm@197
|
505 #+begin_src clojure
|
rlm@198
|
506 (ns cortex.sense
|
rlm@198
|
507 "Here are functions useful in the construction of two or more
|
rlm@198
|
508 sensors/effectors."
|
rlm@198
|
509 {:author "Robert McInytre"}
|
rlm@198
|
510 (:use (cortex world util))
|
rlm@198
|
511 (:import ij.process.ImageProcessor)
|
rlm@198
|
512 (:import jme3tools.converters.ImageToAwt)
|
rlm@198
|
513 (:import java.awt.image.BufferedImage)
|
rlm@198
|
514 (:import com.jme3.collision.CollisionResults)
|
rlm@198
|
515 (:import com.jme3.bounding.BoundingBox)
|
rlm@198
|
516 (:import (com.jme3.scene Node Spatial))
|
rlm@198
|
517 (:import com.jme3.scene.control.AbstractControl)
|
rlm@199
|
518 (:import (com.jme3.math Quaternion Vector3f))
|
rlm@199
|
519 (:import javax.imageio.ImageIO)
|
rlm@199
|
520 (:import java.io.File)
|
rlm@199
|
521 (:import (javax.swing JPanel JFrame SwingUtilities)))
|
rlm@198
|
522 #+end_src
|
rlm@187
|
523
|
rlm@211
|
524 #+name: test-header
|
rlm@211
|
525 #+begin_src clojure
|
rlm@211
|
526 (ns cortex.test.sense
|
rlm@211
|
527 (:use (cortex world util sense vision))
|
rlm@211
|
528 (:import
|
rlm@211
|
529 java.io.File
|
rlm@211
|
530 (com.jme3.math Vector3f ColorRGBA)
|
rlm@211
|
531 (com.aurellem.capture RatchetTimer Capture)))
|
rlm@211
|
532 #+end_src
|
rlm@211
|
533
|
rlm@198
|
534 * Source Listing
|
rlm@211
|
535 - [[../src/cortex/sense.clj][cortex.sense]]
|
rlm@211
|
536 - [[../src/cortex/test/sense.clj][cortex.test.sense]]
|
rlm@211
|
537 - [[../assets/Models/subtitles/subtitles.blend][subtitles.blend]]
|
rlm@211
|
538 - [[../assets/Models/subtitles/Lake_CraterLake03_sm.hdr][subtitles reflection map]]
|
rlm@211
|
539 #+html: <ul> <li> <a href="../org/sense.org">This org file</a> </li> </ul>
|
rlm@211
|
540
|
rlm@211
|
541 * Next
|
rlm@211
|
542 Now that some of the preliminaries are out of the way, in the [[./body.org][next
|
rlm@211
|
543 post]] I'll create a simulated body.
|
rlm@198
|
544
|
rlm@187
|
545
|
rlm@151
|
546 * COMMENT generate source
|
rlm@151
|
547 #+begin_src clojure :tangle ../src/cortex/sense.clj
|
rlm@211
|
548 <<sense-header>>
|
rlm@198
|
549 <<blender-1>>
|
rlm@198
|
550 <<blender-2>>
|
rlm@198
|
551 <<topology-1>>
|
rlm@198
|
552 <<topology-2>>
|
rlm@198
|
553 <<node-1>>
|
rlm@198
|
554 <<node-2>>
|
rlm@197
|
555 <<view-senses>>
|
rlm@151
|
556 #+end_src
|
rlm@199
|
557
|
rlm@199
|
558 #+begin_src clojure :tangle ../src/cortex/test/sense.clj
|
rlm@211
|
559 <<test-header>>
|
rlm@199
|
560 <<test>>
|
rlm@199
|
561 #+end_src
|