annotate org/skin.org @ 66:1381a6ebd08b

updated org files to work with latest org changes
author Robert McIntyre <rlm@mit.edu>
date Wed, 07 Dec 2011 12:16:48 -0600
parents 63951929fe44
children 6cd5a034d855
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@43 13 is to create thousands of small /touch receptors/ along the geometries
rlm@43 14 which make up the creature's body. The number of touch receptors in a
rlm@43 15 given area is determined by how complicated that area is, as
rlm@43 16 determined by the total number of triangles in that region. This way,
rlm@43 17 complicated regions like the hands/face, etc. get more touch receptors
rlm@43 18 than simpler areas of the body.
rlm@0 19
rlm@66 20 #+name: 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@66 108 #+name: touch-test
rlm@37 109 #+begin_src clojure
rlm@39 110 (ns test.touch
rlm@59 111 (:use (cortex world util touch))
rlm@59 112 (:import
rlm@59 113 com.jme3.scene.shape.Sphere
rlm@59 114 com.jme3.math.ColorRGBA
rlm@59 115 com.jme3.math.Vector3f
rlm@59 116 com.jme3.material.RenderState$BlendMode
rlm@59 117 com.jme3.renderer.queue.RenderQueue$Bucket
rlm@59 118 com.jme3.scene.shape.Box
rlm@59 119 com.jme3.scene.Node
rlm@59 120 ))
rlm@37 121
rlm@52 122
rlm@39 123
rlm@7 124 (defn ray-origin-debug
rlm@9 125 [ray color]
rlm@7 126 (make-shape
rlm@20 127 (assoc base-shape
rlm@20 128 :shape (Sphere. 5 5 0.05)
rlm@20 129 :name "arrow"
rlm@20 130 :color color
rlm@20 131 :texture false
rlm@20 132 :physical? false
rlm@20 133 :position
rlm@20 134 (.getOrigin ray))))
rlm@6 135
rlm@9 136 (defn ray-debug [ray color]
rlm@6 137 (make-shape
rlm@6 138 (assoc
rlm@6 139 base-shape
rlm@6 140 :name "debug-ray"
rlm@6 141 :physical? false
rlm@6 142 :shape (com.jme3.scene.shape.Line.
rlm@6 143 (.getOrigin ray)
rlm@6 144 (.add
rlm@6 145 (.getOrigin ray)
rlm@6 146 (.mult (.getDirection ray)
rlm@6 147 (float (.getLimit ray))))))))
rlm@6 148
rlm@6 149
rlm@10 150 (defn contact-color [contacts]
rlm@10 151 (case contacts
rlm@10 152 0 ColorRGBA/Gray
rlm@37 153 1 ColorRGBA/Red
rlm@10 154 2 ColorRGBA/Green
rlm@10 155 3 ColorRGBA/Yellow
rlm@10 156 4 ColorRGBA/Orange
rlm@10 157 5 ColorRGBA/Red
rlm@10 158 6 ColorRGBA/Magenta
rlm@10 159 7 ColorRGBA/Pink
rlm@10 160 8 ColorRGBA/White))
rlm@6 161
rlm@14 162 (defn update-ray-debug [node ray contacts]
rlm@14 163 (let [origin (.getChild node 0)]
rlm@14 164 (.setLocalTranslation origin (.getOrigin ray))
rlm@14 165 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
rlm@14 166
rlm@13 167 (defn init-node
rlm@13 168 [debug-node rays]
rlm@12 169 (.detachAllChildren debug-node)
rlm@13 170 (dorun
rlm@13 171 (for [ray rays]
rlm@13 172 (do
rlm@13 173 (.attachChild
rlm@13 174 debug-node
rlm@13 175 (doto (Node.)
rlm@14 176 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
rlm@20 177 (.attachChild (ray-debug ray ColorRGBA/Gray))
rlm@14 178 ))))))
rlm@14 179
rlm@13 180 (defn manage-ray-debug-node [debug-node geom touch-data limit]
rlm@13 181 (let [rays (normal-rays limit geom)]
rlm@13 182 (if (not= (count (.getChildren debug-node)) (count touch-data))
rlm@13 183 (init-node debug-node rays))
rlm@13 184 (dorun
rlm@13 185 (for [n (range (count touch-data))]
rlm@14 186 (update-ray-debug
rlm@14 187 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
rlm@12 188
rlm@0 189 (defn transparent-sphere []
rlm@0 190 (doto
rlm@0 191 (make-shape
rlm@0 192 (merge base-shape
rlm@0 193 {:position (Vector3f. 0 2 0)
rlm@0 194 :name "the blob."
rlm@0 195 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 196 :texture "Textures/purpleWisp.png"
rlm@0 197 :physical? true
rlm@0 198 :mass 70
rlm@0 199 :color ColorRGBA/Blue
rlm@0 200 :shape (Sphere. 10 10 1)}))
rlm@0 201 (-> (.getMaterial)
rlm@0 202 (.getAdditionalRenderState)
rlm@0 203 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 204 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 205
rlm@0 206 (defn transparent-box []
rlm@0 207 (doto
rlm@0 208 (make-shape
rlm@0 209 (merge base-shape
rlm@0 210 {:position (Vector3f. 0 2 0)
rlm@10 211 :name "box"
rlm@0 212 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 213 :texture "Textures/purpleWisp.png"
rlm@0 214 :physical? true
rlm@0 215 :mass 70
rlm@0 216 :color ColorRGBA/Blue
rlm@0 217 :shape (Box. 1 1 1)}))
rlm@0 218 (-> (.getMaterial)
rlm@0 219 (.getAdditionalRenderState)
rlm@0 220 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 221 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 222
rlm@6 223 (defn transparent-floor []
rlm@6 224 (doto
rlm@6 225 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 226 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@10 227 :texture "Textures/redWisp.png"
rlm@10 228 :name "floor")
rlm@6 229 (-> (.getMaterial)
rlm@6 230 (.getAdditionalRenderState)
rlm@6 231 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 232 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 233
rlm@0 234 (defn test-skin []
rlm@0 235 (let [b
rlm@58 236 ;;(transparent-box)
rlm@58 237 (transparent-sphere)
rlm@10 238 ;;(sphere)
rlm@58 239 f (transparent-floor)
rlm@6 240 debug-node (Node.)
rlm@12 241 node (doto (Node.) (.attachChild b) (.attachChild f))
rlm@12 242 root-node (doto (Node.) (.attachChild node)
rlm@12 243 (.attachChild debug-node))
rlm@12 244 ]
rlm@0 245
rlm@0 246 (world
rlm@12 247 root-node
rlm@15 248 {"key-return" (fire-cannon-ball node)}
rlm@0 249 (fn [world]
rlm@20 250 ;; (Capture/SimpleCaptureVideo
rlm@20 251 ;; world
rlm@20 252 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@20 253 ;; (no-logging)
rlm@20 254 ;;(enable-debug world)
rlm@20 255 ;; (set-accuracy world (/ 1 60))
rlm@58 256 )
rlm@58 257
rlm@0 258 (fn [& _]
rlm@19 259 (let [sensitivity 0.2
rlm@18 260 touch-data (touch-percieve sensitivity b node)]
rlm@18 261 (manage-ray-debug-node debug-node b touch-data sensitivity)
rlm@18 262 )
rlm@11 263 (Thread/sleep 10)
rlm@0 264 ))))
rlm@0 265
rlm@37 266
rlm@0 267 #+end_src
rlm@0 268
rlm@0 269
rlm@10 270
rlm@10 271
rlm@6 272
rlm@0 273 * COMMENT code generation
rlm@39 274 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@0 275 <<skin-main>>
rlm@0 276 #+end_src
rlm@0 277
rlm@39 278 #+begin_src clojure :tangle ../src/test/touch.clj
rlm@39 279 <<touch-test>>
rlm@39 280 #+end_src
rlm@39 281
rlm@0 282
rlm@0 283
rlm@0 284
rlm@32 285
rlm@32 286