annotate org/skin.org @ 44:f3e33fd5f97d

working through blender tutorial
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Nov 2011 01:38:22 -0700
parents 117eb477d0a7
children f080f1e49fba
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@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 (use 'hello.brick-wall)
rlm@39 116
rlm@7 117 (defn ray-origin-debug
rlm@9 118 [ray color]
rlm@7 119 (make-shape
rlm@20 120 (assoc base-shape
rlm@20 121 :shape (Sphere. 5 5 0.05)
rlm@20 122 :name "arrow"
rlm@20 123 :color color
rlm@20 124 :texture false
rlm@20 125 :physical? false
rlm@20 126 :position
rlm@20 127 (.getOrigin ray))))
rlm@6 128
rlm@9 129 (defn ray-debug [ray color]
rlm@6 130 (make-shape
rlm@6 131 (assoc
rlm@6 132 base-shape
rlm@6 133 :name "debug-ray"
rlm@6 134 :physical? false
rlm@6 135 :shape (com.jme3.scene.shape.Line.
rlm@6 136 (.getOrigin ray)
rlm@6 137 (.add
rlm@6 138 (.getOrigin ray)
rlm@6 139 (.mult (.getDirection ray)
rlm@6 140 (float (.getLimit ray))))))))
rlm@6 141
rlm@6 142
rlm@10 143 (defn contact-color [contacts]
rlm@10 144 (case contacts
rlm@10 145 0 ColorRGBA/Gray
rlm@37 146 1 ColorRGBA/Red
rlm@10 147 2 ColorRGBA/Green
rlm@10 148 3 ColorRGBA/Yellow
rlm@10 149 4 ColorRGBA/Orange
rlm@10 150 5 ColorRGBA/Red
rlm@10 151 6 ColorRGBA/Magenta
rlm@10 152 7 ColorRGBA/Pink
rlm@10 153 8 ColorRGBA/White))
rlm@6 154
rlm@14 155 (defn update-ray-debug [node ray contacts]
rlm@14 156 (let [origin (.getChild node 0)]
rlm@14 157 (.setLocalTranslation origin (.getOrigin ray))
rlm@14 158 (.setColor (.getMaterial origin) "Color" (contact-color contacts))))
rlm@14 159
rlm@13 160 (defn init-node
rlm@13 161 [debug-node rays]
rlm@13 162 (println-repl "Init touch debug node.")
rlm@12 163 (.detachAllChildren debug-node)
rlm@13 164 (dorun
rlm@13 165 (for [ray rays]
rlm@13 166 (do
rlm@13 167 (.attachChild
rlm@13 168 debug-node
rlm@13 169 (doto (Node.)
rlm@14 170 (.attachChild (ray-origin-debug ray ColorRGBA/Gray))
rlm@20 171 (.attachChild (ray-debug ray ColorRGBA/Gray))
rlm@14 172 ))))))
rlm@14 173
rlm@13 174 (defn manage-ray-debug-node [debug-node geom touch-data limit]
rlm@13 175 (let [rays (normal-rays limit geom)]
rlm@13 176 (if (not= (count (.getChildren debug-node)) (count touch-data))
rlm@13 177 (init-node debug-node rays))
rlm@13 178 (dorun
rlm@13 179 (for [n (range (count touch-data))]
rlm@14 180 (update-ray-debug
rlm@14 181 (.getChild debug-node n) (nth rays n) (nth touch-data n))))))
rlm@12 182
rlm@0 183 (defn transparent-sphere []
rlm@0 184 (doto
rlm@0 185 (make-shape
rlm@0 186 (merge base-shape
rlm@0 187 {:position (Vector3f. 0 2 0)
rlm@0 188 :name "the blob."
rlm@0 189 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 190 :texture "Textures/purpleWisp.png"
rlm@0 191 :physical? true
rlm@0 192 :mass 70
rlm@0 193 :color ColorRGBA/Blue
rlm@0 194 :shape (Sphere. 10 10 1)}))
rlm@0 195 (-> (.getMaterial)
rlm@0 196 (.getAdditionalRenderState)
rlm@0 197 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 198 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 199
rlm@0 200 (defn transparent-box []
rlm@0 201 (doto
rlm@0 202 (make-shape
rlm@0 203 (merge base-shape
rlm@0 204 {:position (Vector3f. 0 2 0)
rlm@10 205 :name "box"
rlm@0 206 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@0 207 :texture "Textures/purpleWisp.png"
rlm@0 208 :physical? true
rlm@0 209 :mass 70
rlm@0 210 :color ColorRGBA/Blue
rlm@0 211 :shape (Box. 1 1 1)}))
rlm@0 212 (-> (.getMaterial)
rlm@0 213 (.getAdditionalRenderState)
rlm@0 214 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@0 215 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@0 216
rlm@6 217 (defn transparent-floor []
rlm@6 218 (doto
rlm@6 219 (box 5 0.2 5 :mass 0 :position (Vector3f. 0 -2 0)
rlm@6 220 :material "Common/MatDefs/Misc/Unshaded.j3md"
rlm@10 221 :texture "Textures/redWisp.png"
rlm@10 222 :name "floor")
rlm@6 223 (-> (.getMaterial)
rlm@6 224 (.getAdditionalRenderState)
rlm@6 225 (.setBlendMode RenderState$BlendMode/Alpha))
rlm@6 226 (.setQueueBucket RenderQueue$Bucket/Transparent)))
rlm@6 227
rlm@0 228 (defn test-skin []
rlm@0 229 (let [b
rlm@18 230 ;;(transparent-box)
rlm@18 231 (transparent-sphere)
rlm@10 232 ;;(sphere)
rlm@6 233 f (transparent-floor)
rlm@6 234 debug-node (Node.)
rlm@12 235 node (doto (Node.) (.attachChild b) (.attachChild f))
rlm@12 236 root-node (doto (Node.) (.attachChild node)
rlm@12 237 (.attachChild debug-node))
rlm@12 238 ]
rlm@0 239
rlm@0 240 (world
rlm@12 241 root-node
rlm@15 242 {"key-return" (fire-cannon-ball node)}
rlm@0 243 (fn [world]
rlm@20 244 ;; (Capture/SimpleCaptureVideo
rlm@20 245 ;; world
rlm@20 246 ;; (file-str "/home/r/proj/cortex/tmp/blob.avi"))
rlm@20 247 ;; (no-logging)
rlm@20 248 ;;(enable-debug world)
rlm@20 249 ;; (set-accuracy world (/ 1 60))
rlm@11 250 )
rlm@0 251
rlm@0 252 (fn [& _]
rlm@19 253 (let [sensitivity 0.2
rlm@18 254 touch-data (touch-percieve sensitivity b node)]
rlm@18 255 (manage-ray-debug-node debug-node b touch-data sensitivity)
rlm@18 256 )
rlm@11 257 (Thread/sleep 10)
rlm@0 258 ))))
rlm@0 259
rlm@37 260
rlm@0 261 #+end_src
rlm@0 262
rlm@0 263
rlm@10 264
rlm@10 265
rlm@6 266
rlm@0 267 * COMMENT code generation
rlm@39 268 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@0 269 <<skin-main>>
rlm@0 270 #+end_src
rlm@0 271
rlm@39 272 #+begin_src clojure :tangle ../src/test/touch.clj
rlm@39 273 <<touch-test>>
rlm@39 274 #+end_src
rlm@39 275
rlm@0 276
rlm@0 277
rlm@0 278
rlm@32 279
rlm@32 280