annotate org/skin.org @ 105:3334bf15854b

removed useless code
author Robert McIntyre <rlm@mit.edu>
date Sat, 14 Jan 2012 23:06:44 -0700
parents 39e4e1542e4a
children e8df6e76c3e5
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@68 110 (ns cortex.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@68 119 com.jme3.scene.Node))
rlm@39 120
rlm@7 121 (defn ray-origin-debug
rlm@9 122 [ray color]
rlm@7 123 (make-shape
rlm@20 124 (assoc base-shape
rlm@20 125 :shape (Sphere. 5 5 0.05)
rlm@20 126 :name "arrow"
rlm@20 127 :color color
rlm@20 128 :texture false
rlm@20 129 :physical? false
rlm@20 130 :position
rlm@20 131 (.getOrigin ray))))
rlm@6 132
rlm@9 133 (defn ray-debug [ray color]
rlm@6 134 (make-shape
rlm@6 135 (assoc
rlm@6 136 base-shape
rlm@6 137 :name "debug-ray"
rlm@6 138 :physical? false
rlm@6 139 :shape (com.jme3.scene.shape.Line.
rlm@6 140 (.getOrigin ray)
rlm@6 141 (.add
rlm@6 142 (.getOrigin ray)
rlm@6 143 (.mult (.getDirection ray)
rlm@6 144 (float (.getLimit ray))))))))
rlm@6 145
rlm@6 146
rlm@10 147 (defn contact-color [contacts]
rlm@10 148 (case contacts
rlm@10 149 0 ColorRGBA/Gray
rlm@37 150 1 ColorRGBA/Red
rlm@10 151 2 ColorRGBA/Green
rlm@10 152 3 ColorRGBA/Yellow
rlm@10 153 4 ColorRGBA/Orange
rlm@10 154 5 ColorRGBA/Red
rlm@10 155 6 ColorRGBA/Magenta
rlm@10 156 7 ColorRGBA/Pink
rlm@10 157 8 ColorRGBA/White))
rlm@6 158
rlm@14 159 (defn update-ray-debug [node ray contacts]
rlm@14 160 (let [origin (.getChild node 0)]
rlm@14 161 (.setLocalTranslation origin (.getOrigin ray))
rlm@14 162 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
rlm@14 163
rlm@13 164 (defn init-node
rlm@13 165 [debug-node rays]
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@0 186 (defn transparent-sphere []
rlm@0 187 (doto
rlm@0 188 (make-shape
rlm@0 189 (merge base-shape
rlm@0 190 {:position (Vector3f. 0 2 0)
rlm@0 191 :name "the blob."
rlm@0 192 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 193 :texture "Textures/purpleWisp.png"
rlm@0 194 :physical? true
rlm@0 195 :mass 70
rlm@0 196 :color ColorRGBA/Blue
rlm@0 197 :shape (Sphere. 10 10 1)}))
rlm@0 198 (-> (.getMaterial)
rlm@0 199 (.getAdditionalRenderState)
rlm@0 200 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 201 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 202
rlm@0 203 (defn transparent-box []
rlm@0 204 (doto
rlm@0 205 (make-shape
rlm@0 206 (merge base-shape
rlm@0 207 {:position (Vector3f. 0 2 0)
rlm@10 208 :name "box"
rlm@0 209 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 210 :texture "Textures/purpleWisp.png"
rlm@0 211 :physical? true
rlm@0 212 :mass 70
rlm@0 213 :color ColorRGBA/Blue
rlm@0 214 :shape (Box. 1 1 1)}))
rlm@0 215 (-> (.getMaterial)
rlm@0 216 (.getAdditionalRenderState)
rlm@0 217 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 218 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 219
rlm@6 220 (defn transparent-floor []
rlm@6 221 (doto
rlm@6 222 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 223 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@10 224 :texture "Textures/redWisp.png"
rlm@10 225 :name "floor")
rlm@6 226 (-> (.getMaterial)
rlm@6 227 (.getAdditionalRenderState)
rlm@6 228 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 229 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 230
rlm@69 231 (defn test-skin
rlm@69 232 "Testing touch:
rlm@69 233 you should see a ball which responds to the table
rlm@69 234 and whatever balls hit it."
rlm@69 235 []
rlm@0 236 (let [b
rlm@58 237 ;;(transparent-box)
rlm@58 238 (transparent-sphere)
rlm@10 239 ;;(sphere)
rlm@58 240 f (transparent-floor)
rlm@6 241 debug-node (Node.)
rlm@12 242 node (doto (Node.) (.attachChild b) (.attachChild f))
rlm@12 243 root-node (doto (Node.) (.attachChild node)
rlm@12 244 (.attachChild debug-node))
rlm@12 245 ]
rlm@0 246
rlm@0 247 (world
rlm@12 248 root-node
rlm@15 249 {"key-return" (fire-cannon-ball node)}
rlm@0 250 (fn [world]
rlm@20 251 ;; (Capture/SimpleCaptureVideo
rlm@20 252 ;; world
rlm@20 253 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@20 254 ;; (no-logging)
rlm@20 255 ;;(enable-debug world)
rlm@20 256 ;; (set-accuracy world (/ 1 60))
rlm@58 257 )
rlm@58 258
rlm@0 259 (fn [& _]
rlm@19 260 (let [sensitivity 0.2
rlm@18 261 touch-data (touch-percieve sensitivity b node)]
rlm@68 262 (manage-ray-debug-node debug-node b touch-data sensitivity))
rlm@0 263 ))))
rlm@0 264
rlm@37 265
rlm@0 266 #+end_src
rlm@0 267
rlm@0 268
rlm@10 269
rlm@10 270
rlm@6 271
rlm@0 272 * COMMENT code generation
rlm@39 273 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@0 274 <<skin-main>>
rlm@0 275 #+end_src
rlm@0 276
rlm@68 277 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
rlm@39 278 <<touch-test>>
rlm@39 279 #+end_src
rlm@39 280
rlm@0 281
rlm@0 282
rlm@0 283
rlm@32 284
rlm@32 285