Mercurial > cortex
comparison org/sense.org @ 197:16cbce075a0b
reorganizing for website
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 05 Feb 2012 06:55:41 -0700 |
parents | 22548d48cc85 |
children | fc0bf33bded2 |
comparison
equal
deleted
inserted
replaced
196:a165d4a86e75 | 197:16cbce075a0b |
---|---|
1 #+title: General sense/effector utilities | 1 #+title: General Sense/Effector Utilities |
2 #+author: Robert McIntyre | 2 #+author: Robert McIntyre |
3 #+email: rlm@mit.edu | 3 #+email: rlm@mit.edu |
4 #+description: sensory utilities | 4 #+description: sensory utilities |
5 #+keywords: simulation, jMonkeyEngine3, clojure, simulated senses | 5 #+keywords: simulation, jMonkeyEngine3, clojure, simulated senses |
6 #+SETUPFILE: ../../aurellem/org/setup.org | 6 #+SETUPFILE: ../../aurellem/org/setup.org |
7 #+INCLUDE: ../../aurellem/org/level-0.org | 7 #+INCLUDE: ../../aurellem/org/level-0.org |
8 | 8 |
9 #+name: sense | 9 * Namespace/Imports |
10 #+begin_src clojure | 10 #+name header |
11 #+begin_src clojure | |
11 (ns cortex.sense | 12 (ns cortex.sense |
12 "Here are functions useful in the construction of two or more | 13 "Here are functions useful in the construction of two or more |
13 sensors/effectors." | 14 sensors/effectors." |
14 {:author "Robert McInytre"} | 15 {:author "Robert McInytre"} |
15 (:use (cortex world util)) | 16 (:use (cortex world util)) |
19 (:import com.jme3.collision.CollisionResults) | 20 (:import com.jme3.collision.CollisionResults) |
20 (:import com.jme3.bounding.BoundingBox) | 21 (:import com.jme3.bounding.BoundingBox) |
21 (:import (com.jme3.scene Node Spatial)) | 22 (:import (com.jme3.scene Node Spatial)) |
22 (:import com.jme3.scene.control.AbstractControl) | 23 (:import com.jme3.scene.control.AbstractControl) |
23 (:import (com.jme3.math Quaternion Vector3f))) | 24 (:import (com.jme3.math Quaternion Vector3f))) |
24 | 25 #+end_src |
26 | |
27 * Blender Utilities | |
28 #+name: blender | |
29 #+begin_src clojure | |
25 (defn meta-data | 30 (defn meta-data |
26 "Get the meta-data for a node created with blender." | 31 "Get the meta-data for a node created with blender." |
27 [blender-node key] | 32 [blender-node key] |
28 (if-let [data (.getUserData blender-node "properties")] | 33 (if-let [data (.getUserData blender-node "properties")] |
29 (.findValue data key) | 34 (.findValue data key) |
30 nil)) | 35 nil)) |
31 | 36 |
32 (defn closest-node | 37 (defn jme-to-blender |
33 "Return the node in creature which is closest to the given node." | 38 "Convert from JME coordinates to Blender coordinates" |
34 [#^Node creature #^Node eye] | 39 [#^Vector3f in] |
35 (loop [radius (float 0.01)] | 40 (Vector3f. (.getX in) |
36 (let [results (CollisionResults.)] | 41 (- (.getZ in)) |
37 (.collideWith | 42 (.getY in))) |
38 creature | 43 |
39 (BoundingBox. (.getWorldTranslation eye) | 44 (defn blender-to-jme |
40 radius radius radius) | 45 "Convert from Blender coordinates to JME coordinates" |
41 results) | 46 [#^Vector3f in] |
42 (if-let [target (first results)] | 47 (Vector3f. (.getX in) |
43 (.getGeometry target) | 48 (.getZ in) |
44 (recur (float (* 2 radius))))))) | 49 (- (.getY in)))) |
45 | 50 #+end_src |
46 (defn bind-sense | 51 |
47 "Bind the sense to the Spatial such that it will maintain its | 52 * Topology Related stuff |
48 current position relative to the Spatial no matter how the spatial | 53 #+name: topology |
49 moves. 'sense can be either a Camera or Listener object." | 54 #+begin_src clojure |
50 [#^Spatial obj sense] | 55 (defn load-image |
51 (let [sense-offset (.subtract (.getLocation sense) | 56 "Load an image as a BufferedImage using the asset-manager system." |
52 (.getWorldTranslation obj)) | 57 [asset-relative-path] |
53 initial-sense-rotation (Quaternion. (.getRotation sense)) | 58 (ImageToAwt/convert |
54 base-anti-rotation (.inverse (.getWorldRotation obj))] | 59 (.getImage (.loadTexture (asset-manager) asset-relative-path)) |
55 (.addControl | 60 false false 0)) |
56 obj | |
57 (proxy [AbstractControl] [] | |
58 (controlUpdate [tpf] | |
59 (let [total-rotation | |
60 (.mult base-anti-rotation (.getWorldRotation obj))] | |
61 (.setLocation | |
62 sense | |
63 (.add | |
64 (.mult total-rotation sense-offset) | |
65 (.getWorldTranslation obj))) | |
66 (.setRotation | |
67 sense | |
68 (.mult total-rotation initial-sense-rotation)))) | |
69 (controlRender [_ _]))))) | |
70 | 61 |
71 (def white 0xFFFFFF) | 62 (def white 0xFFFFFF) |
72 | 63 |
73 (defn white? [rgb] | 64 (defn white? [rgb] |
74 (= (bit-and white rgb) white)) | 65 (= (bit-and white rgb) white)) |
169 [(- x min-x) | 160 [(- x min-x) |
170 (- y min-y)]) | 161 (- y min-y)]) |
171 squeezed))] | 162 squeezed))] |
172 relocated))) | 163 relocated))) |
173 | 164 |
165 #+end_src | |
166 | |
167 * Node level stuff | |
168 #+name: node | |
169 #+begin_src clojure | |
170 (defn closest-node | |
171 "Return the node in creature which is closest to the given node." | |
172 [#^Node creature #^Node eye] | |
173 (loop [radius (float 0.01)] | |
174 (let [results (CollisionResults.)] | |
175 (.collideWith | |
176 creature | |
177 (BoundingBox. (.getWorldTranslation eye) | |
178 radius radius radius) | |
179 results) | |
180 (if-let [target (first results)] | |
181 (.getGeometry target) | |
182 (recur (float (* 2 radius))))))) | |
183 | |
184 (defn bind-sense | |
185 "Bind the sense to the Spatial such that it will maintain its | |
186 current position relative to the Spatial no matter how the spatial | |
187 moves. 'sense can be either a Camera or Listener object." | |
188 [#^Spatial obj sense] | |
189 (let [sense-offset (.subtract (.getLocation sense) | |
190 (.getWorldTranslation obj)) | |
191 initial-sense-rotation (Quaternion. (.getRotation sense)) | |
192 base-anti-rotation (.inverse (.getWorldRotation obj))] | |
193 (.addControl | |
194 obj | |
195 (proxy [AbstractControl] [] | |
196 (controlUpdate [tpf] | |
197 (let [total-rotation | |
198 (.mult base-anti-rotation (.getWorldRotation obj))] | |
199 (.setLocation | |
200 sense | |
201 (.add | |
202 (.mult total-rotation sense-offset) | |
203 (.getWorldTranslation obj))) | |
204 (.setRotation | |
205 sense | |
206 (.mult total-rotation initial-sense-rotation)))) | |
207 (controlRender [_ _]))))) | |
208 | |
174 (defn world-to-local | 209 (defn world-to-local |
175 "Convert the world coordinates into coordinates relative to the | 210 "Convert the world coordinates into coordinates relative to the |
176 object (i.e. local coordinates), taking into account the rotation | 211 object (i.e. local coordinates), taking into account the rotation |
177 of object." | 212 of object." |
178 [#^Spatial object world-coordinate] | 213 [#^Spatial object world-coordinate] |
191 [parent-name] | 226 [parent-name] |
192 (fn [#^Node creature] | 227 (fn [#^Node creature] |
193 (if-let [sense-node (.getChild creature parent-name)] | 228 (if-let [sense-node (.getChild creature parent-name)] |
194 (seq (.getChildren sense-node)) | 229 (seq (.getChildren sense-node)) |
195 (do (println-repl "could not find" parent-name "node") [])))) | 230 (do (println-repl "could not find" parent-name "node") [])))) |
196 | 231 #+end_src |
197 (defn load-image | 232 |
198 "Load an image as a BufferedImage using the asset-manager system." | 233 * Viewing Senses |
199 [asset-relative-path] | 234 #+name view-senses |
200 (ImageToAwt/convert | 235 #+begin_src clojure |
201 (.getImage (.loadTexture (asset-manager) asset-relative-path)) | |
202 false false 0)) | |
203 | |
204 (defn jme-to-blender | |
205 "Convert from JME coordinates to Blender coordinates" | |
206 [#^Vector3f in] | |
207 (Vector3f. (.getX in) | |
208 (- (.getZ in)) | |
209 (.getY in))) | |
210 | |
211 (defn blender-to-jme | |
212 "Convert from Blender coordinates to JME coordinates" | |
213 [#^Vector3f in] | |
214 (Vector3f. (.getX in) | |
215 (.getZ in) | |
216 (- (.getY in)))) | |
217 | |
218 | |
219 (defn view-sense | 236 (defn view-sense |
220 "Take a function that produces a BufferedImage from some sense data | 237 "Take a kernel that produces a BufferedImage from some sense data |
221 and return a function which takes a list of sense and displays | 238 and return a function which takes a list of sense data, uses the |
222 those images in multiple JFrames." | 239 kernem to convert to images, and displays those images, each in |
240 its own JFrame." | |
223 [sense-display-kernel] | 241 [sense-display-kernel] |
224 (let | 242 (let [windows (atom [])] |
225 [windows (atom [])] | |
226 (fn [data] | 243 (fn [data] |
227 (if (> (count data) (count @windows)) | 244 (if (> (count data) (count @windows)) |
228 (reset! windows (map (fn [_] (view-image)) | 245 (reset! |
229 (range (count data))))) | 246 windows (map (fn [_] (view-image)) (range (count data))))) |
230 (dorun | 247 (dorun |
231 (map | 248 (map |
232 (fn [display datum] | 249 (fn [display datum] |
233 (display (sense-display-kernel datum))) | 250 (display (sense-display-kernel datum))) |
234 @windows data))))) | 251 @windows data))))) |
235 | 252 |
236 (defn gray | 253 (defn gray |
237 "Create a gray RGB pixel with R, G, and B set to 'num" | 254 "Create a gray RGB pixel with R, G, and B set to num. num must be |
255 between 0 and 255." | |
238 [num] | 256 [num] |
239 (+ num | 257 (+ num |
240 (bit-shift-left num 8) | 258 (bit-shift-left num 8) |
241 (bit-shift-left num 16))) | 259 (bit-shift-left num 16))) |
242 #+end_src | 260 #+end_src |
243 | 261 |
244 #+results: sense | |
245 : #'cortex.sense/gray | |
246 | |
247 | |
248 * COMMENT generate source | 262 * COMMENT generate source |
249 #+begin_src clojure :tangle ../src/cortex/sense.clj | 263 #+begin_src clojure :tangle ../src/cortex/sense.clj |
250 <<sense>> | 264 <<header>> |
251 #+end_src | 265 <<blender>> |
266 <<topology>> | |
267 <<node>> | |
268 <<view-senses>> | |
269 #+end_src |