annotate org/skin.org @ 39:2ce7400825c2

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