comparison org/sense-util.org @ 182:f2552d61e8df

cleaned up sense-utils some more, tested system again.
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 07:44:48 -0700
parents 0f1c7921d967
children
comparison
equal deleted inserted replaced
181:0f1c7921d967 182:f2552d61e8df
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
10 #+name: sense-util 9 #+name: sense-util
11 #+begin_src clojure 10 #+begin_src clojure
12 (ns cortex.sense) 11 (ns cortex.sense
13 (cortex.import/mega-import-jme3) 12 "Here are functions usefull in the construction of two or more
14 (import ij.process.ImageProcessor) 13 senses/effectors."
15 (import java.awt.image.BufferedImage) 14 {:author "Robert McInytre"}
16 (use 'cortex.util) 15 (:use (cortex world util))
17 (use 'cortex.world) 16 (:import ij.process.ImageProcessor)
18 (import jme3tools.converters.ImageToAwt) 17 (:import jme3tools.converters.ImageToAwt)
18 (:import java.awt.image.BufferedImage)
19 (:import com.jme3.collision.CollisionResults)
20 (:import com.jme3.bounding.BoundingBox)
21 (:import (com.jme3.scene Node Spatial))
22 (:import com.jme3.scene.control.AbstractControl)
23 (:import (com.jme3.math Quaternion Vector3f)))
19 24
20 (defn meta-data 25 (defn meta-data
21 "Get the meta-data for a node created with blender." 26 "Get the meta-data for a node created with blender."
22 [blender-node key] 27 [blender-node key]
23 (if-let [data (.getUserData blender-node "properties")] 28 (if-let [data (.getUserData blender-node "properties")]
24 (.findValue data key) 29 (.findValue data key)
25 nil)) 30 nil))
26 31
27 (defn closest-node 32 (defn closest-node
28 "Return the object in creature which is closest to the given node." 33 "Return the node in creature which is closest to the given node."
29 ;;dylan"The closest object in creature to the given node."
30 [#^Node creature #^Node eye] 34 [#^Node creature #^Node eye]
31 (loop [radius (float 0.01)] 35 (loop [radius (float 0.01)]
32 (let [results (CollisionResults.)] 36 (let [results (CollisionResults.)]
33 (.collideWith 37 (.collideWith
34 creature 38 creature
69 (defn white? [rgb] 73 (defn white? [rgb]
70 (= (bit-and white rgb) white)) 74 (= (bit-and white rgb) white))
71 75
72 (defn filter-pixels 76 (defn filter-pixels
73 "List the coordinates of all pixels matching pred, within the bounds 77 "List the coordinates of all pixels matching pred, within the bounds
74 provided. Bounds -> [x0 y0 width height]" 78 provided.
79 bounds -> [x0 y0 width height]"
75 {:author "Dylan Holmes"} 80 {:author "Dylan Holmes"}
76 ([pred #^BufferedImage image] 81 ([pred #^BufferedImage image]
77 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)])) 82 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
78 ([pred #^BufferedImage image [x0 y0 width height]] 83 ([pred #^BufferedImage image [x0 y0 width height]]
79 ((fn accumulate [x y matches] 84 ((fn accumulate [x y matches]
117 122
118 (defn average [coll] 123 (defn average [coll]
119 (/ (reduce + coll) (count coll))) 124 (/ (reduce + coll) (count coll)))
120 125
121 (defn collapse-1d 126 (defn collapse-1d
122 "One dimensional analogue of collapse" 127 "One dimensional analogue of collapse."
123 [center line] 128 [center line]
124 (let [length (count line) 129 (let [length (count line)
125 num-above (count (filter (partial < center) line)) 130 num-above (count (filter (partial < center) line))
126 num-below (- length num-above)] 131 num-below (- length num-above)]
127 (range (- center num-below) 132 (range (- center num-below)
128 (+ center num-above)))) 133 (+ center num-above))))
129 134
130 (defn collapse 135 (defn collapse
131 "Take a set of pairs of integers and collapse them into a 136 "Take a set of pairs of integers and collapse them into a
132 contigous bitmap." 137 contigous bitmap with no \"holes\"."
133 [points] 138 [points]
134 (if (empty? points) [] 139 (if (empty? points) []
135 (let 140 (let
136 [num-points (count points) 141 [num-points (count points)
137 center (vector 142 center (vector
155 (map vector 160 (map vector
156 (collapse-1d (first center) 161 (collapse-1d (first center)
157 (map first row)) 162 (map first row))
158 (map second row))) 163 (map second row)))
159 (partition-by second (sort-by second flattened)))) 164 (partition-by second (sort-by second flattened))))
160 relocate 165 relocated
161 (let [min-x (apply min (map first squeezed)) 166 (let [min-x (apply min (map first squeezed))
162 min-y (apply min (map second squeezed))] 167 min-y (apply min (map second squeezed))]
163 (map (fn [[x y]] 168 (map (fn [[x y]]
164 [(- x min-x) 169 [(- x min-x)
165 (- y min-y)]) 170 (- y min-y)])
166 squeezed))] 171 squeezed))]
167 relocate))) 172 relocated)))
168 173
169 (defn world-to-local 174 (defn world-to-local
170 "Convert the world coordinates into coordinates relative to the 175 "Convert the world coordinates into coordinates relative to the
171 object (i.e. local coordinates), taking into account the rotation 176 object (i.e. local coordinates), taking into account the rotation
172 of object." 177 of object."
173 [#^Spatial object world-coordinate] 178 [#^Spatial object world-coordinate]
174 (.worldToLocal object world-coordinate nil)) 179 (.worldToLocal object world-coordinate nil))
175 180
176 (defn local-to-world 181 (defn local-to-world
177 "Convert the local coordinates into coordinates into world relative 182 "Convert the local coordinates into world relative coordinates"
178 coordinates" 183 [#^Spatial object local-coordinate]
179 [#^Spatial object local-coordinate] 184 (.localToWorld object local-coordinate nil))
180 (.localToWorld object local-coordinate nil)) 185
181 186 (defn sense-nodes
182 187 "For each sense there is a special blender node whose children are
183 (defn sense-nodes [parent-name] 188 considered markers for an instance of a that sense. This function
189 generates functions to find those children, given the name of the
190 special parent node."
191 [parent-name]
184 (fn [#^Node creature] 192 (fn [#^Node creature]
185 (if-let [sense-node (.getChild creature parent-name)] 193 (if-let [sense-node (.getChild creature parent-name)]
186 (seq (.getChildren sense-node)) 194 (seq (.getChildren sense-node))
187 (do (println-repl "could not find" parent-name "node") [])))) 195 (do (println-repl "could not find" parent-name "node") []))))
188 196