annotate org/touch.org @ 230:f9b7d674aed8

reorganizing touch
author Robert McIntyre <rlm@mit.edu>
date Sat, 11 Feb 2012 19:28:36 -0700
parents 6f1be9525e40
children e29dd0024a9e
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@229 9
rlm@229 10
rlm@37 11 * Touch
rlm@0 12
rlm@226 13 Touch is critical to navigation and spatial reasoning and as such I
rlm@226 14 need a simulated version of it to give to my AI creatures.
rlm@0 15
rlm@228 16 However, touch in my virtual can not exactly correspond to human touch
rlm@228 17 because my creatures are made out of completely rigid segments that
rlm@228 18 don't deform like human skin.
rlm@228 19
rlm@228 20 Human skin has a wide array of touch sensors, each of which speciliaze
rlm@228 21 in detecting different vibrational modes and pressures. These sensors
rlm@228 22 can integrate a vast expanse of skin (i.e. your entire palm), or a
rlm@228 23 tiny patch of skin at the tip of your finger. The hairs of the skin
rlm@228 24 help detect objects before they even come into contact with the skin
rlm@228 25 proper.
rlm@228 26
rlm@228 27 Instead of measuring deformation or vibration, I surround each rigid
rlm@228 28 part with a plenitude of hair-like objects which do not interact with
rlm@228 29 the physical world. Physical objects can pass through them with no
rlm@228 30 effect. The hairs are able to measure contact with other objects, and
rlm@228 31 constantly report how much of their extent is covered. So, even though
rlm@228 32 the creature's body parts do not deform, the hairs create a margin
rlm@228 33 around those body parts which achieves a sense of touch which is a
rlm@228 34 hybrid between a human's sense of deformation and sense from hairs.
rlm@228 35
rlm@228 36 Implementing touch in jMonkeyEngine follows a different techinal route
rlm@228 37 than vision and hearing. Those two senses piggybacked off
rlm@228 38 jMonkeyEngine's 3D audio and video rendering subsystems. To simulate
rlm@228 39 Touch, I use jMonkeyEngine's physics system to execute many small
rlm@229 40 collision detections, one for each "hair". The placement of the
rlm@229 41 "hairs" is determined by a UV-mapped image which shows where each hair
rlm@229 42 should be on the 3D surface of the body.
rlm@228 43
rlm@229 44
rlm@229 45 * Defining Touch Meta-Data in Blender
rlm@229 46
rlm@229 47 Each geometry can have a single UV map which describes the position
rlm@229 48 and length of the "hairs" which will constitute its sense of
rlm@229 49 touch. This image path is stored under the "touch" key. The image
rlm@229 50 itself is grayscale, with black meaning a hair length of 0 (no hair is
rlm@229 51 present) and white meaning a hair length of =scale=, which is a float
rlm@229 52 stored under the key "scale". If the pixel is gray then the resultant
rlm@229 53 hair length is linearly interpolated between 0 and =scale=.
rlm@229 54
rlm@0 55 #+begin_src clojure
rlm@229 56 (defn tactile-sensor-profile
rlm@229 57 "Return the touch-sensor distribution image in BufferedImage format,
rlm@229 58 or nil if it does not exist."
rlm@229 59 [#^Geometry obj]
rlm@229 60 (if-let [image-path (meta-data obj "touch")]
rlm@229 61 (load-image image-path)))
rlm@228 62 #+end_src
rlm@156 63
rlm@229 64
rlm@229 65 ** TODO add image showing example touch-uv map
rlm@229 66 ** TODO add metadata display for worm
rlm@229 67
rlm@228 68 * Triangle Manipulation Functions
rlm@228 69
rlm@229 70 The rigid bodies which make up a creature have an underlying
rlm@229 71 =Geometry=, which is a =Mesh= plus a =Material= and other important
rlm@229 72 data involved with displaying the body.
rlm@229 73
rlm@229 74 A =Mesh= is composed of =Triangles=, and each =Triangle= has three
rlm@229 75 verticies which have coordinates in XYZ space and UV space.
rlm@229 76
rlm@229 77 Here, =(triangles)= gets all the triangles which compose a mesh, and
rlm@229 78 =(triangle-UV-coord)= returns the the UV coordinates of the verticies
rlm@229 79 of a triangle.
rlm@229 80
rlm@228 81 #+begin_src clojure
rlm@228 82 (defn triangles
rlm@228 83 "Return a sequence of all the Triangles which compose a given
rlm@228 84 Geometry."
rlm@228 85 [#^Geometry geom]
rlm@228 86 (let
rlm@228 87 [mesh (.getMesh geom)
rlm@228 88 triangles (transient [])]
rlm@228 89 (dorun
rlm@228 90 (for [n (range (.getTriangleCount mesh))]
rlm@228 91 (let [tri (Triangle.)]
rlm@228 92 (.getTriangle mesh n tri)
rlm@228 93 ;; (.calculateNormal tri)
rlm@228 94 ;; (.calculateCenter tri)
rlm@228 95 (conj! triangles tri))))
rlm@228 96 (persistent! triangles)))
rlm@228 97
rlm@228 98 (defn mesh-triangle
rlm@228 99 "Get the triangle specified by triangle-index from the mesh within
rlm@228 100 bounds."
rlm@228 101 [#^Mesh mesh triangle-index]
rlm@228 102 (let [scratch (Triangle.)]
rlm@228 103 (.getTriangle mesh triangle-index scratch)
rlm@228 104 scratch))
rlm@228 105
rlm@228 106 (defn triangle-vertex-indices
rlm@228 107 "Get the triangle vertex indices of a given triangle from a given
rlm@228 108 mesh."
rlm@228 109 [#^Mesh mesh triangle-index]
rlm@228 110 (let [indices (int-array 3)]
rlm@228 111 (.getTriangle mesh triangle-index indices)
rlm@228 112 (vec indices)))
rlm@228 113
rlm@228 114 (defn vertex-UV-coord
rlm@228 115 "Get the UV-coordinates of the vertex named by vertex-index"
rlm@228 116 [#^Mesh mesh vertex-index]
rlm@228 117 (let [UV-buffer
rlm@228 118 (.getData
rlm@228 119 (.getBuffer
rlm@228 120 mesh
rlm@228 121 VertexBuffer$Type/TexCoord))]
rlm@228 122 [(.get UV-buffer (* vertex-index 2))
rlm@228 123 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@228 124
rlm@228 125 (defn triangle-UV-coord
rlm@228 126 "Get the UV-cooridnates of the triangle's verticies."
rlm@228 127 [#^Mesh mesh width height triangle-index]
rlm@228 128 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@228 129 (map (partial vertex-UV-coord mesh)
rlm@228 130 (triangle-vertex-indices mesh triangle-index))))
rlm@228 131 #+end_src
rlm@228 132
rlm@228 133 * Schrapnel Conversion Functions
rlm@229 134
rlm@229 135 It is convienent to treat a =Triangle= as a sequence of verticies, and
rlm@229 136 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion
rlm@229 137 functions make this easy. If these classes implemented =Iterable= then
rlm@229 138 this code would not be necessary. Hopefully they will in the future.
rlm@229 139
rlm@228 140 #+begin_src clojure
rlm@228 141 (defn triangle-seq [#^Triangle tri]
rlm@228 142 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@228 143
rlm@228 144 (defn vector3f-seq [#^Vector3f v]
rlm@228 145 [(.getX v) (.getY v) (.getZ v)])
rlm@228 146
rlm@228 147 (defn point->vector2f [[u v]]
rlm@228 148 (Vector2f. u v))
rlm@228 149
rlm@228 150 (defn vector2f->vector3f [v]
rlm@228 151 (Vector3f. (.getX v) (.getY v) 0))
rlm@228 152
rlm@228 153 (defn map-triangle [f #^Triangle tri]
rlm@228 154 (Triangle.
rlm@228 155 (f 0 (.get1 tri))
rlm@228 156 (f 1 (.get2 tri))
rlm@228 157 (f 2 (.get3 tri))))
rlm@228 158
rlm@228 159 (defn points->triangle
rlm@228 160 "Convert a list of points into a triangle."
rlm@228 161 [points]
rlm@228 162 (apply #(Triangle. %1 %2 %3)
rlm@228 163 (map (fn [point]
rlm@228 164 (let [point (vec point)]
rlm@228 165 (Vector3f. (get point 0 0)
rlm@228 166 (get point 1 0)
rlm@228 167 (get point 2 0))))
rlm@228 168 (take 3 points))))
rlm@228 169 #+end_src
rlm@228 170
rlm@228 171 * Triangle Affine Transforms
rlm@228 172
rlm@229 173 The position of each hair is stored in a 2D image in UV
rlm@229 174 coordinates. To place the hair in 3D space we must convert from UV
rlm@229 175 coordinates to XYZ coordinates. Each =Triangle= has coordinates in
rlm@229 176 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]
rlm@229 177 for translating any coordinate within the UV triangle to the
rlm@229 178 cooresponding coordinate in the XYZ triangle.
rlm@229 179
rlm@228 180 #+begin_src clojure
rlm@228 181 (defn triangle->matrix4f
rlm@228 182 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@228 183 contain the vertices of the triangle; the last contains the unit
rlm@228 184 normal of the triangle. The bottom row is filled with 1s."
rlm@228 185 [#^Triangle t]
rlm@228 186 (let [mat (Matrix4f.)
rlm@228 187 [vert-1 vert-2 vert-3]
rlm@228 188 ((comp vec map) #(.get t %) (range 3))
rlm@228 189 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@228 190 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@228 191 (dorun
rlm@228 192 (for [row (range 4) col (range 3)]
rlm@228 193 (do
rlm@228 194 (.set mat col row (.get (vertices row)col))
rlm@228 195 (.set mat 3 row 1))))
rlm@228 196 mat))
rlm@228 197
rlm@228 198 (defn triangle-transformation
rlm@228 199 "Returns the affine transformation that converts each vertex in the
rlm@228 200 first triangle into the corresponding vertex in the second
rlm@228 201 triangle."
rlm@228 202 [#^Triangle tri-1 #^Triangle tri-2]
rlm@228 203 (.mult
rlm@228 204 (triangle->matrix4f tri-2)
rlm@228 205 (.invert (triangle->matrix4f tri-1))))
rlm@228 206 #+end_src
rlm@228 207
rlm@229 208 * Triangle Boundaries
rlm@229 209
rlm@229 210 For efficiency's sake I will divide the UV-image into small squares
rlm@229 211 which inscribe each UV-triangle, then extract the points which lie
rlm@229 212 inside the triangle and map them to 3D-space using
rlm@229 213 =(triangle-transform)= above. To do this I need a function,
rlm@229 214 =(inside-triangle?)=, which determines whether a point is inside a
rlm@229 215 triangle in 2D UV-space.
rlm@228 216
rlm@228 217 #+begin_src clojure
rlm@229 218 (defn convex-bounds
rlm@229 219 "Returns the smallest square containing the given vertices, as a
rlm@229 220 vector of integers [left top width height]."
rlm@229 221 [uv-verts]
rlm@229 222 (let [xs (map first uv-verts)
rlm@229 223 ys (map second uv-verts)
rlm@229 224 x0 (Math/floor (apply min xs))
rlm@229 225 y0 (Math/floor (apply min ys))
rlm@229 226 x1 (Math/ceil (apply max xs))
rlm@229 227 y1 (Math/ceil (apply max ys))]
rlm@229 228 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@229 229
rlm@229 230 (defn same-side?
rlm@229 231 "Given the points p1 and p2 and the reference point ref, is point p
rlm@229 232 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@229 233 [p1 p2 ref p]
rlm@229 234 (<=
rlm@229 235 0
rlm@229 236 (.dot
rlm@229 237 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@229 238 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@229 239
rlm@229 240 (defn inside-triangle?
rlm@229 241 "Is the point inside the triangle?"
rlm@229 242 {:author "Dylan Holmes"}
rlm@229 243 [#^Triangle tri #^Vector3f p]
rlm@229 244 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@229 245 (and
rlm@229 246 (same-side? vert-1 vert-2 vert-3 p)
rlm@229 247 (same-side? vert-2 vert-3 vert-1 p)
rlm@229 248 (same-side? vert-3 vert-1 vert-2 p))))
rlm@229 249 #+end_src
rlm@229 250
rlm@229 251
rlm@229 252
rlm@229 253 * Sensor Related Functions
rlm@229 254
rlm@229 255 These functions analyze the touch-sensor-profile image convert the
rlm@229 256 location of each touch sensor from pixel coordinates to UV-coordinates
rlm@229 257 and XYZ-coordinates.
rlm@229 258
rlm@229 259 #+begin_src clojure
rlm@229 260 (defn sensors-in-triangle
rlm@229 261 "Locate the touch sensors in the triangle, returning a map of their
rlm@229 262 UV and geometry-relative coordinates."
rlm@229 263 [image mesh tri-index]
rlm@229 264 (let [width (.getWidth image)
rlm@229 265 height (.getHeight image)
rlm@229 266 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@229 267 bounds (convex-bounds UV-vertex-coords)
rlm@229 268
rlm@229 269 cutout-triangle (points->triangle UV-vertex-coords)
rlm@229 270 UV-sensor-coords
rlm@229 271 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@229 272 (fn [[u v]] (Vector3f. u v 0)))
rlm@229 273 (white-coordinates image bounds))
rlm@229 274 UV->geometry (triangle-transformation
rlm@229 275 cutout-triangle
rlm@229 276 (mesh-triangle mesh tri-index))
rlm@229 277 geometry-sensor-coords
rlm@229 278 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@229 279 UV-sensor-coords)]
rlm@229 280 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@229 281
rlm@229 282 (defn-memo locate-feelers
rlm@229 283 "Search the geometry's tactile UV profile for touch sensors,
rlm@229 284 returning their positions in geometry-relative coordinates."
rlm@229 285 [#^Geometry geo]
rlm@229 286 (let [mesh (.getMesh geo)
rlm@229 287 num-triangles (.getTriangleCount mesh)]
rlm@229 288 (if-let [image (tactile-sensor-profile geo)]
rlm@229 289 (map
rlm@229 290 (partial sensors-in-triangle image mesh)
rlm@229 291 (range num-triangles))
rlm@229 292 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@229 293
rlm@229 294 (defn-memo touch-topology
rlm@229 295 "Return a sequence of vectors of the form [x y] describing the
rlm@229 296 \"topology\" of the tactile sensors. Points that are close together
rlm@229 297 in the touch-topology are generally close together in the simulation."
rlm@229 298 [#^Gemoetry geo]
rlm@229 299 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@229 300
rlm@229 301 (defn-memo feeler-coordinates
rlm@229 302 "The location of the touch sensors in world-space coordinates."
rlm@229 303 [#^Geometry geo]
rlm@229 304 (vec (map :geometry (locate-feelers geo))))
rlm@228 305 #+end_src
rlm@228 306
rlm@228 307 * Physics Collision Objects
rlm@230 308
rlm@230 309 The "hairs" are actually rays which extend from a point on a
rlm@230 310 =Triangle= in the =Mesh= normal to the =Triangle's= surface.
rlm@230 311
rlm@228 312 #+begin_src clojure
rlm@228 313 (defn get-ray-origin
rlm@228 314 "Return the origin which a Ray would have to have to be in the exact
rlm@228 315 center of a particular Triangle in the Geometry in World
rlm@228 316 Coordinates."
rlm@228 317 [geom tri]
rlm@228 318 (let [new (Vector3f.)]
rlm@228 319 (.calculateCenter tri)
rlm@228 320 (.localToWorld geom (.getCenter tri) new) new))
rlm@228 321
rlm@228 322 (defn get-ray-direction
rlm@228 323 "Return the direction which a Ray would have to have to be to point
rlm@228 324 normal to the Triangle, in coordinates relative to the center of the
rlm@228 325 Triangle."
rlm@228 326 [geom tri]
rlm@228 327 (let [n+c (Vector3f.)]
rlm@228 328 (.calculateNormal tri)
rlm@228 329 (.calculateCenter tri)
rlm@228 330 (.localToWorld
rlm@228 331 geom
rlm@228 332 (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@228 333 (.subtract n+c (get-ray-origin geom tri))))
rlm@228 334 #+end_src
rlm@228 335
rlm@228 336
rlm@228 337 * Skin Creation
rlm@228 338
rlm@228 339 #+begin_src clojure
rlm@178 340 (defn touch-fn
rlm@178 341 "Returns a function which returns tactile sensory data when called
rlm@178 342 inside a running simulation."
rlm@178 343 [#^Geometry geo]
rlm@156 344 (let [feeler-coords (feeler-coordinates geo)
rlm@156 345 tris (triangles geo)
rlm@156 346 limit 0.1
rlm@156 347 ;;results (CollisionResults.)
rlm@156 348 ]
rlm@156 349 (if (empty? (touch-topology geo))
rlm@156 350 nil
rlm@156 351 (fn [node]
rlm@156 352 (let [sensor-origins
rlm@156 353 (map
rlm@156 354 #(map (partial local-to-world geo) %)
rlm@156 355 feeler-coords)
rlm@156 356 triangle-normals
rlm@156 357 (map (partial get-ray-direction geo)
rlm@156 358 tris)
rlm@156 359 rays
rlm@156 360 (flatten
rlm@156 361 (map (fn [origins norm]
rlm@156 362 (map #(doto (Ray. % norm)
rlm@156 363 (.setLimit limit)) origins))
rlm@156 364 sensor-origins triangle-normals))]
rlm@156 365 (vector
rlm@156 366 (touch-topology geo)
rlm@156 367 (vec
rlm@156 368 (for [ray rays]
rlm@156 369 (do
rlm@156 370 (let [results (CollisionResults.)]
rlm@156 371 (.collideWith node ray results)
rlm@156 372 (let [touch-objects
rlm@156 373 (filter #(not (= geo (.getGeometry %)))
rlm@156 374 results)]
rlm@156 375 (- 255
rlm@156 376 (if (empty? touch-objects) 255
rlm@156 377 (rem
rlm@156 378 (int
rlm@156 379 (* 255 (/ (.getDistance
rlm@156 380 (first touch-objects)) limit)))
rlm@156 381 256))))))))))))))
rlm@156 382
rlm@178 383 (defn touch!
rlm@178 384 "Endow the creature with the sense of touch. Returns a sequence of
rlm@178 385 functions, one for each body part with a tactile-sensor-proile,
rlm@178 386 each of which when called returns sensory data for that body part."
rlm@178 387 [#^Node creature]
rlm@178 388 (filter
rlm@178 389 (comp not nil?)
rlm@178 390 (map touch-fn
rlm@178 391 (filter #(isa? (class %) Geometry)
rlm@178 392 (node-seq creature)))))
rlm@228 393 #+end_src
rlm@156 394
rlm@228 395 * Visualizing Touch
rlm@228 396
rlm@228 397 #+begin_src clojure
rlm@188 398 (defn view-touch
rlm@189 399 "Creates a function which accepts a list of touch sensor-data and
rlm@189 400 displays each element to the screen."
rlm@188 401 []
rlm@187 402 (view-sense
rlm@187 403 (fn
rlm@187 404 [[coords sensor-data]]
rlm@187 405 (let [image (points->image coords)]
rlm@187 406 (dorun
rlm@187 407 (for [i (range (count coords))]
rlm@187 408 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@187 409 (gray (sensor-data i)))))
rlm@188 410 image))))
rlm@37 411 #+end_src
rlm@37 412
rlm@226 413 * Headers
rlm@226 414 #+begin_src clojure
rlm@226 415 (ns cortex.touch
rlm@226 416 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
rlm@226 417 to be outfitted with touch sensors with density determined by a UV
rlm@226 418 image. In this way a Geometry can know what parts of itself are
rlm@226 419 touching nearby objects. Reads specially prepared blender files to
rlm@226 420 construct this sense automatically."
rlm@226 421 {:author "Robert McIntyre"}
rlm@226 422 (:use (cortex world util sense))
rlm@226 423 (:use clojure.contrib.def)
rlm@226 424 (:import (com.jme3.scene Geometry Node Mesh))
rlm@226 425 (:import com.jme3.collision.CollisionResults)
rlm@226 426 (:import com.jme3.scene.VertexBuffer$Type)
rlm@226 427 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
rlm@226 428 #+end_src
rlm@37 429
rlm@228 430
rlm@228 431 * Source Listing
rlm@228 432 * Next
rlm@228 433
rlm@228 434
rlm@226 435 * COMMENT Code Generation
rlm@39 436 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@0 437 <<skin-main>>
rlm@0 438 #+end_src
rlm@0 439
rlm@68 440 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
rlm@39 441 #+end_src
rlm@39 442
rlm@0 443
rlm@0 444
rlm@0 445
rlm@32 446
rlm@32 447
rlm@226 448