comparison org/vision.org @ 169:94b79c191fc7

renamed some functions in vision.org
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 04:23:32 -0700
parents 1c8e9d389ea4
children 1a00b4918529
comparison
equal deleted inserted replaced
168:1c8e9d389ea4 169:94b79c191fc7
118 provided by (vision-pipeline)." 118 provided by (vision-pipeline)."
119 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi] 119 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi]
120 (byteBuffer->bufferedImage! 120 (byteBuffer->bufferedImage!
121 (frameBuffer->byteBuffer! r fb bb) bi)) 121 (frameBuffer->byteBuffer! r fb bb) bi))
122 122
123 (defn add-eye! 123 (defn add-camera!
124 "Add an eye to the world, calling continuation on every frame 124 "Add a camera to the world, calling continuation on every frame
125 produced." 125 produced."
126 [#^Application world camera continuation] 126 [#^Application world camera continuation]
127 (let [width (.getWidth camera) 127 (let [width (.getWidth camera)
128 height (.getHeight camera) 128 height (.getHeight camera)
129 render-manager (.getRenderManager world) 129 render-manager (.getRenderManager world)
132 (.setClearFlags true true true) 132 (.setClearFlags true true true)
133 (.setBackgroundColor ColorRGBA/Black) 133 (.setBackgroundColor ColorRGBA/Black)
134 (.addProcessor (vision-pipeline continuation)) 134 (.addProcessor (vision-pipeline continuation))
135 (.attachScene (.getRootNode world))))) 135 (.attachScene (.getRootNode world)))))
136 136
137 (defn retina-sensor-image 137 (defn retina-sensor-profile
138 "Return a map of pixel selection functions to BufferedImages 138 "Return a map of pixel selection functions to BufferedImages
139 describing the distribution of light-sensitive components on this 139 describing the distribution of light-sensitive components of this
140 geometry's surface. Each function creates an integer from the rgb 140 eye. Each function creates an integer from the rgb values found in
141 values found in the pixel. :red, :green, :blue, :gray are already 141 the pixel. :red, :green, :blue, :gray are already defined as
142 defined as extracting the red green blue and average components 142 extracting the red, green, blue, and average components
143 respectively." 143 respectively."
144 [#^Spatial eye] 144 [#^Spatial eye]
145 (if-let [eye-map (meta-data eye "eye")] 145 (if-let [eye-map (meta-data eye "eye")]
146 (map-vals 146 (map-vals
147 load-image 147 load-image
148 (eval (read-string eye-map))))) 148 (eval (read-string eye-map)))))
149 149
150 (defn eye-dimensions 150 (defn eye-dimensions
151 "returns the width and height specified in the metadata of the eye" 151 "Returns [width, height] specified in the metadata of the eye"
152 [#^Spatial eye] 152 [#^Spatial eye]
153 (let [dimensions 153 (let [dimensions
154 (map #(vector (.getWidth %) (.getHeight %)) 154 (map #(vector (.getWidth %) (.getHeight %))
155 (vals (retina-sensor-image eye)))] 155 (vals (retina-sensor-profile eye)))]
156 [(apply max (map first dimensions)) 156 [(apply max (map first dimensions))
157 (apply max (map second dimensions))])) 157 (apply max (map second dimensions))]))
158 158
159 (defvar 159 (defvar
160 ^{:arglists '([creature])} 160 ^{:arglists '([creature])}
161 eyes 161 eyes
162 (sense-nodes "eyes") 162 (sense-nodes "eyes")
163 "Return the children of the creature's \"eyes\" node.") 163 "Return the children of the creature's \"eyes\" node.")
164 164
165 (defn attach-eye 165 (defn add-eye!
166 "Attach a Camera to the appropiate area and return the Camera." 166 "Create a Camera centered on the current position of 'eye which
167 follows the closest physical node in 'creature and sends visual
168 data to 'continuation."
167 [#^Node creature #^Spatial eye] 169 [#^Node creature #^Spatial eye]
168 (let [target (closest-node creature eye) 170 (let [target (closest-node creature eye)
169 [cam-width cam-height] (eye-dimensions eye) 171 [cam-width cam-height] (eye-dimensions eye)
170 cam (Camera. cam-width cam-height)] 172 cam (Camera. cam-width cam-height)]
171 (.setLocation cam (.getWorldTranslation eye)) 173 (.setLocation cam (.getWorldTranslation eye))
180 {:all 0xFFFFFF 182 {:all 0xFFFFFF
181 :red 0xFF0000 183 :red 0xFF0000
182 :blue 0x0000FF 184 :blue 0x0000FF
183 :green 0x00FF00}) 185 :green 0x00FF00})
184 186
185 (defn enable-vision 187 (defn vision-fn
186 "return [init-function sensor-functions] for a particular eye" 188 "return [init-function sensor-functions] for a particular eye"
187 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}] 189 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
188 (let [retinal-map (retina-sensor-image eye) 190 (let [retinal-map (retina-sensor-profile eye)
189 camera (attach-eye creature eye) 191 camera (add-eye! creature eye)
190 vision-image 192 vision-image
191 (atom 193 (atom
192 (BufferedImage. (.getWidth camera) 194 (BufferedImage. (.getWidth camera)
193 (.getHeight camera) 195 (.getHeight camera)
194 BufferedImage/TYPE_BYTE_BINARY))] 196 BufferedImage/TYPE_BYTE_BINARY))]
195 [(fn [world] 197 [(fn [world]
196 (add-eye! 198 (add-camera!
197 world camera 199 world camera
198 (let [counter (atom 0)] 200 (let [counter (atom 0)]
199 (fn [r fb bb bi] 201 (fn [r fb bb bi]
200 (if (zero? (rem (swap! counter inc) (inc skip))) 202 (if (zero? (rem (swap! counter inc) (inc skip)))
201 (reset! vision-image (BufferedImage! r fb bb bi))))))) 203 (reset! vision-image (BufferedImage! r fb bb bi)))))))
221 [init-b senses-b]] 223 [init-b senses-b]]
222 [(conj init-a init-b) 224 [(conj init-a init-b)
223 (into senses-a senses-b)]) 225 (into senses-a senses-b)])
224 [[][]] 226 [[][]]
225 (for [eye (eyes creature)] 227 (for [eye (eyes creature)]
226 (enable-vision creature eye)))) 228 (vision-fn creature eye))))
227 229
228 230
229 #+end_src 231 #+end_src
230 232
231 233
268 {} 270 {}
269 (fn [world] 271 (fn [world]
270 (let [cam (.clone (.getCamera world)) 272 (let [cam (.clone (.getCamera world))
271 width (.getWidth cam) 273 width (.getWidth cam)
272 height (.getHeight cam)] 274 height (.getHeight cam)]
273 (add-eye! world cam 275 (add-camera! world cam
274 ;;no-op 276 ;;no-op
275 (comp (view-image) BufferedImage!) 277 (comp (view-image) BufferedImage!)
276 ) 278 )
277 (add-eye! world 279 (add-camera! world
278 (doto (.clone cam) 280 (doto (.clone cam)
279 (.setLocation (Vector3f. -10 0 0)) 281 (.setLocation (Vector3f. -10 0 0))
280 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y)) 282 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))
281 ;;no-op 283 ;;no-op
282 (comp (view-image) BufferedImage!)) 284 (comp (view-image) BufferedImage!))
283 ;; This is here to restore the main view 285 ;; This is here to restore the main view
284 ;; after the other views have completed processing 286 ;; after the other views have completed processing
285 (add-eye! world (.getCamera world) no-op))) 287 (add-camera! world (.getCamera world) no-op)))
286 (fn [world tpf] 288 (fn [world tpf]
287 (.rotate candy (* tpf 0.2) 0 0))))) 289 (.rotate candy (* tpf 0.2) 0 0)))))
288 #+end_src 290 #+end_src
289 291
290 #+results: test-vision 292 #+results: test-vision