annotate org/skin.org @ 37:eeba17a4bd54

cleaning up the touch code
author Robert McIntyre <rlm@mit.edu>
date Thu, 03 Nov 2011 09:52:34 -0700
parents 97703c7f020e
children 2ce7400825c2
rev   line source
rlm@37 1 #+title: Simulated Sense of Touch
rlm@0 2 #+author: Robert McIntyre
rlm@0 3 #+email: rlm@mit.edu
rlm@37 4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.
rlm@37 5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure
rlm@4 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@4 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@0 8
rlm@37 9 * Touch
rlm@0 10
rlm@37 11 My creatures need to be able to feel their environments. The idea here
rlm@37 12 is to thousands of small /touch receptors/ along the geometries which
rlm@37 13 make up the creature's body. The number of touch receptors in a given
rlm@37 14 area is determined by how complicated that area is, as determined by
rlm@37 15 the total number of triangles in that region. This way, complicated
rlm@37 16 regions like the hands/face, etc. get more touch receptors than
rlm@37 17 simpler areas of the body.
rlm@0 18
rlm@0 19 #+srcname: skin-main
rlm@0 20 #+begin_src clojure
rlm@37 21 (ns cortex.touch
rlm@37 22 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
rlm@37 23 to be outfitted with touch sensors with density proportional to the
rlm@37 24 density of triangles along the surface of the Geometry. Enables a
rlm@37 25 Geometry to know what parts of itself are touching nearby objects."
rlm@37 26 {:author "Robert McIntyre"}
rlm@37 27 (:use (cortex world util))
rlm@37 28 (:import com.jme3.scene.Geometry)
rlm@37 29 (:import com.jme3.collision.CollisionResult)
rlm@37 30 (:import com.jme3.math Triangle Vector3f Ray))
rlm@37 31
rlm@37 32 (defn triangles
rlm@37 33 "Return a sequence of all the Triangles which compose a given
rlm@37 34 Geometry."
rlm@37 35 [#^Geometry geom]
rlm@6 36 (let
rlm@6 37 [mesh (.getMesh geom)
rlm@6 38 triangles (transient [])]
rlm@6 39 (dorun
rlm@6 40 (for [n (range (.getTriangleCount mesh))]
rlm@6 41 (let [tri (Triangle.)]
rlm@6 42 (.getTriangle mesh n tri)
rlm@37 43 ;; (.calculateNormal tri)
rlm@37 44 ;; (.calculateCenter tri)
rlm@6 45 (conj! triangles tri))))
rlm@6 46 (persistent! triangles)))
rlm@6 47
rlm@7 48 (defn get-ray-origin
rlm@37 49 "Return the origin which a Ray would have to have to be in the exact
rlm@37 50 center of a particular Triangle in the Geometry in World
rlm@37 51 Coordinates."
rlm@7 52 [geom tri]
rlm@7 53 (let [new (Vector3f.)]
rlm@7 54 (.calculateCenter tri)
rlm@37 55 (.localToWorld geom (.getCenter tri) new) new))
rlm@6 56
rlm@7 57 (defn get-ray-direction
rlm@37 58 "Return the direction which a Ray would have to have to be in the
rlm@37 59 exact center of a particular Triangle in the Geometry, pointing
rlm@37 60 normal to the Triangle, in coordinates relative to the center of the
rlm@37 61 Triangle."
rlm@7 62 [geom tri]
rlm@9 63 (let [n+c (Vector3f.)]
rlm@7 64 (.calculateNormal tri)
rlm@9 65 (.calculateCenter tri)
rlm@37 66 (.localToWorld
rlm@37 67 geom
rlm@37 68 (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@37 69 (.subtract n+c (get-ray-origin geom tri))))
rlm@37 70
rlm@37 71 (defn normal-rays
rlm@37 72 "For each Triangle which comprises the Geometry, returns a Ray which
rlm@37 73 is centered on that Triangle, points outward in a normal direction,
rlm@37 74 and extends for =limit= distance."
rlm@37 75 [limit #^Geometry geom]
rlm@37 76 (vec
rlm@37 77 (map
rlm@37 78 (fn [tri]
rlm@37 79 (doto
rlm@37 80 (Ray. (get-ray-origin geom tri)
rlm@37 81 (get-ray-direction geom tri))
rlm@37 82 (.setLimit limit)))
rlm@37 83 (triangles geom))))
rlm@37 84
rlm@37 85 (defn touch-percieve
rlm@37 86 "Augment a Geometry with the sense of touch. Returns a sequence of
rlm@37 87 non-negative integers, one for each triangle, with the value of the
rlm@37 88 integer describing how many objects a ray of length =limit=, normal
rlm@37 89 to the triangle and originating from its center, encountered. The
rlm@37 90 Geometry itself is not counted among the results."
rlm@37 91 [limit geom node]
rlm@37 92 (let [normals (normal-rays limit geom)]
rlm@37 93 (doall
rlm@37 94 (for [ray normals]
rlm@37 95 (do
rlm@37 96 (let [results (CollisionResults.)]
rlm@37 97 (.collideWith node ray results)
rlm@37 98 (let [touch-objects
rlm@37 99 (set (filter #(not (= geom %))
rlm@37 100 (map #(.getGeometry %) results)))]
rlm@37 101 (count touch-objects))))))))
rlm@37 102 #+end_src
rlm@37 103
rlm@37 104
rlm@37 105 * Example
rlm@37 106
rlm@37 107 #+begin_src clojure
rlm@37 108
rlm@7 109
rlm@7 110 (defn ray-origin-debug
rlm@9 111 [ray color]
rlm@7 112 (make-shape
rlm@20 113 (assoc base-shape
rlm@20 114 :shape (Sphere. 5 5 0.05)
rlm@20 115 :name "arrow"
rlm@20 116 :color color
rlm@20 117 :texture false
rlm@20 118 :physical? false
rlm@20 119 :position
rlm@20 120 (.getOrigin ray))))
rlm@6 121
rlm@9 122 (defn ray-debug [ray color]
rlm@6 123 (make-shape
rlm@6 124 (assoc
rlm@6 125 base-shape
rlm@6 126 :name "debug-ray"
rlm@6 127 :physical? false
rlm@6 128 :shape (com.jme3.scene.shape.Line.
rlm@6 129 (.getOrigin ray)
rlm@6 130 (.add
rlm@6 131 (.getOrigin ray)
rlm@6 132 (.mult (.getDirection ray)
rlm@6 133 (float (.getLimit ray))))))))
rlm@6 134
rlm@6 135
rlm@10 136 (defn contact-color [contacts]
rlm@10 137 (case contacts
rlm@10 138 0 ColorRGBA/Gray
rlm@37 139 1 ColorRGBA/Red
rlm@10 140 2 ColorRGBA/Green
rlm@10 141 3 ColorRGBA/Yellow
rlm@10 142 4 ColorRGBA/Orange
rlm@10 143 5 ColorRGBA/Red
rlm@10 144 6 ColorRGBA/Magenta
rlm@10 145 7 ColorRGBA/Pink
rlm@10 146 8 ColorRGBA/White))
rlm@6 147
rlm@14 148 (defn update-ray-debug [node ray contacts]
rlm@14 149 (let [origin (.getChild node 0)]
rlm@14 150 (.setLocalTranslation origin (.getOrigin ray))
rlm@14 151 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
rlm@14 152
rlm@13 153 (defn init-node
rlm@13 154 [debug-node rays]
rlm@13 155 (println-repl "Init touch debug node.")
rlm@12 156 (.detachAllChildren debug-node)
rlm@13 157 (dorun
rlm@13 158 (for [ray rays]
rlm@13 159 (do
rlm@13 160 (.attachChild
rlm@13 161 debug-node
rlm@13 162 (doto (Node.)
rlm@14 163 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
rlm@20 164 (.attachChild (ray-debug ray ColorRGBA/Gray))
rlm@14 165 ))))))
rlm@14 166
rlm@13 167 (defn manage-ray-debug-node [debug-node geom touch-data limit]
rlm@13 168 (let [rays (normal-rays limit geom)]
rlm@13 169 (if (not= (count (.getChildren debug-node)) (count touch-data))
rlm@13 170 (init-node debug-node rays))
rlm@13 171 (dorun
rlm@13 172 (for [n (range (count touch-data))]
rlm@14 173 (update-ray-debug
rlm@14 174 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
rlm@12 175
rlm@12 176
rlm@6 177
rlm@11 178 (defn no-logging []
rlm@11 179 (.setLevel (Logger/getLogger "com.jme3") Level/OFF))
rlm@11 180
rlm@11 181 (defn set-accuracy [world new-accuracy]
rlm@11 182 (let [physics-manager (.getState (.getStateManager world) BulletAppState)]
rlm@11 183 (.setAccuracy (.getPhysicsSpace physics-manager) (float new-accuracy))))
rlm@11 184
rlm@0 185 (defn transparent-sphere []
rlm@0 186 (doto
rlm@0 187 (make-shape
rlm@0 188 (merge base-shape
rlm@0 189 {:position (Vector3f. 0 2 0)
rlm@0 190 :name "the blob."
rlm@0 191 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 192 :texture "Textures/purpleWisp.png"
rlm@0 193 :physical? true
rlm@0 194 :mass 70
rlm@0 195 :color ColorRGBA/Blue
rlm@0 196 :shape (Sphere. 10 10 1)}))
rlm@0 197 (-> (.getMaterial)
rlm@0 198 (.getAdditionalRenderState)
rlm@0 199 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 200 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 201
rlm@0 202 (defn transparent-box []
rlm@0 203 (doto
rlm@0 204 (make-shape
rlm@0 205 (merge base-shape
rlm@0 206 {:position (Vector3f. 0 2 0)
rlm@10 207 :name "box"
rlm@0 208 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 209 :texture "Textures/purpleWisp.png"
rlm@0 210 :physical? true
rlm@0 211 :mass 70
rlm@0 212 :color ColorRGBA/Blue
rlm@0 213 :shape (Box. 1 1 1)}))
rlm@0 214 (-> (.getMaterial)
rlm@0 215 (.getAdditionalRenderState)
rlm@0 216 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 217 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 218
rlm@6 219 (defn transparent-floor []
rlm@6 220 (doto
rlm@6 221 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 222 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@10 223 :texture "Textures/redWisp.png"
rlm@10 224 :name "floor")
rlm@6 225 (-> (.getMaterial)
rlm@6 226 (.getAdditionalRenderState)
rlm@6 227 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 228 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 229
rlm@0 230 (defn test-skin []
rlm@0 231 (let [b
rlm@18 232 ;;(transparent-box)
rlm@18 233 (transparent-sphere)
rlm@10 234 ;;(sphere)
rlm@6 235 f (transparent-floor)
rlm@6 236 debug-node (Node.)
rlm@12 237 node (doto (Node.) (.attachChild b) (.attachChild f))
rlm@12 238 root-node (doto (Node.) (.attachChild node)
rlm@12 239 (.attachChild debug-node))
rlm@12 240 ]
rlm@0 241
rlm@0 242 (world
rlm@12 243 root-node
rlm@15 244 {"key-return" (fire-cannon-ball node)}
rlm@0 245 (fn [world]
rlm@20 246 ;; (Capture/SimpleCaptureVideo
rlm@20 247 ;; world
rlm@20 248 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@20 249 ;; (no-logging)
rlm@20 250 ;;(enable-debug world)
rlm@20 251 ;; (set-accuracy world (/ 1 60))
rlm@11 252 )
rlm@0 253
rlm@0 254 (fn [& _]
rlm@19 255 (let [sensitivity 0.2
rlm@18 256 touch-data (touch-percieve sensitivity b node)]
rlm@18 257 (manage-ray-debug-node debug-node b touch-data sensitivity)
rlm@18 258 )
rlm@11 259 (Thread/sleep 10)
rlm@0 260 ))))
rlm@0 261
rlm@37 262
rlm@0 263 #+end_src
rlm@0 264
rlm@0 265
rlm@10 266
rlm@10 267
rlm@6 268
rlm@0 269 * COMMENT code generation
rlm@37 270 #+begin_src clojure :tangle ../src/cortex/touch.clj :noweb yes
rlm@0 271 <<skin-main>>
rlm@0 272 #+end_src
rlm@0 273
rlm@0 274
rlm@0 275
rlm@0 276
rlm@32 277
rlm@32 278