Mercurial > cortex
view org/touch.org @ 238:3fa49ff1649a
going to recode touch.org to make it look better
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 12 Feb 2012 12:10:51 -0700 |
parents | 712bd7e5b148 |
children | 78a640e3bc55 |
line wrap: on
line source
1 #+title: Simulated Sense of Touch2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulated touch for AI research using JMonkeyEngine and clojure.5 #+keywords: simulation, tactile sense, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org11 * Touch13 Touch is critical to navigation and spatial reasoning and as such I14 need a simulated version of it to give to my AI creatures.16 However, touch in my virtual can not exactly correspond to human touch17 because my creatures are made out of completely rigid segments that18 don't deform like human skin.20 Human skin has a wide array of touch sensors, each of which speciliaze21 in detecting different vibrational modes and pressures. These sensors22 can integrate a vast expanse of skin (i.e. your entire palm), or a23 tiny patch of skin at the tip of your finger. The hairs of the skin24 help detect objects before they even come into contact with the skin25 proper.27 Instead of measuring deformation or vibration, I surround each rigid28 part with a plenitude of hair-like objects which do not interact with29 the physical world. Physical objects can pass through them with no30 effect. The hairs are able to measure contact with other objects, and31 constantly report how much of their extent is covered. So, even though32 the creature's body parts do not deform, the hairs create a margin33 around those body parts which achieves a sense of touch which is a34 hybrid between a human's sense of deformation and sense from hairs.36 Implementing touch in jMonkeyEngine follows a different techinal route37 than vision and hearing. Those two senses piggybacked off38 jMonkeyEngine's 3D audio and video rendering subsystems. To simulate39 Touch, I use jMonkeyEngine's physics system to execute many small40 collision detections, one for each "hair". The placement of the41 "hairs" is determined by a UV-mapped image which shows where each hair42 should be on the 3D surface of the body.45 * Defining Touch Meta-Data in Blender47 Each geometry can have a single UV map which describes the position48 and length of the "hairs" which will constitute its sense of49 touch. This image path is stored under the "touch" key. The image50 itself is grayscale, with black meaning a hair length of 0 (no hair is51 present) and white meaning a hair length of =scale=, which is a float52 stored under the key "scale". If the pixel is gray then the resultant53 hair length is linearly interpolated between 0 and =scale=. I call54 these "hairs" /feelers/.56 #+name: meta-data57 #+begin_src clojure58 (defn tactile-sensor-profile59 "Return the touch-sensor distribution image in BufferedImage format,60 or nil if it does not exist."61 [#^Geometry obj]62 (if-let [image-path (meta-data obj "touch")]63 (load-image image-path)))65 (defn tactile-scale66 "Return the maximum length of a hair. All hairs are scalled between67 0.0 and this length, depending on their color. Black is 0, and68 white is maximum length, and everything in between is scalled69 linearlly. Default scale is 0.01 jMonkeyEngine units."70 [#^Geometry obj]71 (if-let [scale (meta-data obj "scale")]72 scale 0.1))73 #+end_src75 ** TODO add image showing example touch-uv map76 ** TODO add metadata display for worm79 * Skin Creation80 * TODO get the actual lengths for each hair82 #+begin_src clojure83 pixel-triangles84 xyz-triangles85 conversions (map triangles->affine-transform pixel-triangles86 xyz-triangles)88 #+end_src91 =(touch-kernel)= generates the functions which implement the sense of92 touch for a creature. These functions must do 6 things to obtain touch93 data.95 - Get the tactile profile image and scale paramaters which describe96 the layout of feelers along the object's surface.97 - Get the lengths of each feeler by analyzing the color of the98 pixels in the tactile profile image.99 - Find the triangles which make up the mesh in pixel-space and in100 world-space.101 - Find the coordinates of each pixel in world-space. These102 coordinates are the origins of the feelers.103 - Calculate the normals of the triangles in world space, and add104 them to each of the origins of the feelers. These are the105 normalized coordinates of the tips of the feelers.106 - Generate some sort of topology for the sensors.108 #+name: kernel109 #+begin_src clojure110 (in-ns 'cortex.touch)112 (declare touch-topology touch-hairs set-ray)114 (defn touch-kernel115 "Constructs a function which will return tactile sensory data from116 'geo when called from inside a running simulation"117 [#^Geometry geo]118 (let [[ray-reference-origins119 ray-reference-tips120 ray-lengths] (touch-hairs geo)121 current-rays (map (fn [] (Ray.)) ray-reference-origins)122 topology (touch-topology geo)]123 (if (empty? ray-reference-origins) nil124 (fn [node]125 (let [transform (.getWorldMatrix geo)]126 (dorun127 (map (fn [ray ref-origin ref-tip length]128 (set-ray ray transform ref-origin ref-tip length))129 current-rays ray-reference-origins130 ray-reference-tips ray-lengths))131 (vector132 topology133 (vec134 (for [ray current-rays]135 (do136 (let [results (CollisionResults.)]137 (.collideWith node ray results)138 (let [touch-objects139 (filter #(not (= geo (.getGeometry %)))140 results)]141 [(if (empty? touch-objects)142 (.getLimit ray)143 (.getDistance (first touch-objects)))144 (.getLimit ray)])))))))))))146 (defn touch-kernel*147 "Returns a function which returns tactile sensory data when called148 inside a running simulation."149 [#^Geometry geo]150 (let [feeler-coords (feeler-coordinates geo)151 tris (triangles geo)152 limit (tactile-scale geo)]153 (if (empty? (touch-topology geo))154 nil155 (fn [node]156 (let [sensor-origins157 (map158 #(map (partial local-to-world geo) %)159 feeler-coords)160 triangle-normals161 (map (partial get-ray-direction geo)162 tris)163 rays164 (flatten165 (map (fn [origins norm]166 (map #(doto (Ray. % norm)167 (.setLimit limit)) origins))168 sensor-origins triangle-normals))]169 (vector170 (touch-topology geo)171 (vec172 (for [ray rays]173 (do174 (let [results (CollisionResults.)]175 (.collideWith node ray results)176 (let [touch-objects177 (filter #(not (= geo (.getGeometry %)))178 results)]179 [(if (empty? touch-objects)180 limit (.getDistance (first touch-objects)))181 limit])))))))))))183 (defn touch!184 "Endow the creature with the sense of touch. Returns a sequence of185 functions, one for each body part with a tactile-sensor-proile,186 each of which when called returns sensory data for that body part."187 [#^Node creature]188 (filter189 (comp not nil?)190 (map touch-kernel191 (filter #(isa? (class %) Geometry)192 (node-seq creature)))))193 #+end_src195 * Sensor Related Functions197 These functions analyze the touch-sensor-profile image convert the198 location of each touch sensor from pixel coordinates to UV-coordinates199 and XYZ-coordinates.201 #+name: sensors202 #+begin_src clojure203 (defn sensors-in-triangle204 "Locate the touch sensors in the triangle, returning a map of their205 UV and geometry-relative coordinates."206 [image mesh tri-index]207 (let [width (.getWidth image)208 height (.getHeight image)209 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)210 bounds (convex-bounds UV-vertex-coords)212 cutout-triangle (points->triangle UV-vertex-coords)213 UV-sensor-coords214 (filter (comp (partial inside-triangle? cutout-triangle)215 (fn [[u v]] (Vector3f. u v 0)))216 (white-coordinates image bounds))217 UV->geometry (triangle-transformation218 cutout-triangle219 (mesh-triangle mesh tri-index))220 geometry-sensor-coords221 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))222 UV-sensor-coords)]223 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))225 (defn-memo locate-feelers226 "Search the geometry's tactile UV profile for touch sensors,227 returning their positions in geometry-relative coordinates."228 [#^Geometry geo]229 (let [mesh (.getMesh geo)230 num-triangles (.getTriangleCount mesh)]231 (if-let [image (tactile-sensor-profile geo)]232 (map233 (partial sensors-in-triangle image mesh)234 (range num-triangles))235 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))237 (defn-memo touch-topology238 "Return a sequence of vectors of the form [x y] describing the239 \"topology\" of the tactile sensors. Points that are close together240 in the touch-topology are generally close together in the simulation."241 [#^Gemoetry geo]242 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))244 (defn-memo feeler-coordinates245 "The location of the touch sensors in world-space coordinates."246 [#^Geometry geo]247 (vec (map :geometry (locate-feelers geo))))248 #+end_src253 * Visualizing Touch254 #+name: visualization255 #+begin_src clojure256 (in-ns 'cortex.touch)258 (defn touch->gray259 "Convert a pair of [distance, max-distance] into a grayscale pixel"260 [distance max-distance]261 (gray262 (- 255263 (rem264 (int265 (* 255 (/ distance max-distance)))266 256))))268 (defn view-touch269 "Creates a function which accepts a list of touch sensor-data and270 displays each element to the screen."271 []272 (view-sense273 (fn274 [[coords sensor-data]]275 (let [image (points->image coords)]276 (dorun277 (for [i (range (count coords))]278 (.setRGB image ((coords i) 0) ((coords i) 1)279 (apply touch->gray (sensor-data i)))))280 image))))281 #+end_src285 * Triangle Manipulation Functions287 The rigid bodies which make up a creature have an underlying288 =Geometry=, which is a =Mesh= plus a =Material= and other important289 data involved with displaying the body.291 A =Mesh= is composed of =Triangles=, and each =Triangle= has three292 verticies which have coordinates in XYZ space and UV space.294 Here, =(triangles)= gets all the triangles which compose a mesh, and295 =(triangle-UV-coord)= returns the the UV coordinates of the verticies296 of a triangle.298 #+name: triangles-1299 #+begin_src clojure300 (defn triangles301 "Return a sequence of all the Triangles which compose a given302 Geometry."303 [#^Geometry geom]304 (let305 [mesh (.getMesh geom)306 triangles (transient [])]307 (dorun308 (for [n (range (.getTriangleCount mesh))]309 (let [tri (Triangle.)]310 (.getTriangle mesh n tri)311 ;; (.calculateNormal tri)312 ;; (.calculateCenter tri)313 (conj! triangles tri))))314 (persistent! triangles)))316 (defn mesh-triangle317 "Get the triangle specified by triangle-index from the mesh within318 bounds."319 [#^Mesh mesh triangle-index]320 (let [scratch (Triangle.)]321 (.getTriangle mesh triangle-index scratch)322 scratch))324 (defn triangle-vertex-indices325 "Get the triangle vertex indices of a given triangle from a given326 mesh."327 [#^Mesh mesh triangle-index]328 (let [indices (int-array 3)]329 (.getTriangle mesh triangle-index indices)330 (vec indices)))332 (defn vertex-UV-coord333 "Get the UV-coordinates of the vertex named by vertex-index"334 [#^Mesh mesh vertex-index]335 (let [UV-buffer336 (.getData337 (.getBuffer338 mesh339 VertexBuffer$Type/TexCoord))]340 [(.get UV-buffer (* vertex-index 2))341 (.get UV-buffer (+ 1 (* vertex-index 2)))]))343 (defn triangle-UV-coord344 "Get the UV-cooridnates of the triangle's verticies."345 [#^Mesh mesh width height triangle-index]346 (map (fn [[u v]] (vector (* width u) (* height v)))347 (map (partial vertex-UV-coord mesh)348 (triangle-vertex-indices mesh triangle-index))))349 #+end_src351 * Schrapnel Conversion Functions353 It is convienent to treat a =Triangle= as a sequence of verticies, and354 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion355 functions make this easy. If these classes implemented =Iterable= then356 this code would not be necessary. Hopefully they will in the future.358 #+name: triangles-2359 #+begin_src clojure360 (defn triangle-seq [#^Triangle tri]361 [(.get1 tri) (.get2 tri) (.get3 tri)])363 (defn vector3f-seq [#^Vector3f v]364 [(.getX v) (.getY v) (.getZ v)])366 (defn point->vector2f [[u v]]367 (Vector2f. u v))369 (defn vector2f->vector3f [v]370 (Vector3f. (.getX v) (.getY v) 0))372 (defn map-triangle [f #^Triangle tri]373 (Triangle.374 (f 0 (.get1 tri))375 (f 1 (.get2 tri))376 (f 2 (.get3 tri))))378 (defn points->triangle379 "Convert a list of points into a triangle."380 [points]381 (apply #(Triangle. %1 %2 %3)382 (map (fn [point]383 (let [point (vec point)]384 (Vector3f. (get point 0 0)385 (get point 1 0)386 (get point 2 0))))387 (take 3 points))))388 #+end_src390 * Triangle Affine Transforms392 The position of each hair is stored in a 2D image in UV393 coordinates. To place the hair in 3D space we must convert from UV394 coordinates to XYZ coordinates. Each =Triangle= has coordinates in395 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]396 for translating any coordinate within the UV triangle to the397 cooresponding coordinate in the XYZ triangle.399 #+name: triangles-3400 #+begin_src clojure401 (defn triangle->matrix4f402 "Converts the triangle into a 4x4 matrix: The first three columns403 contain the vertices of the triangle; the last contains the unit404 normal of the triangle. The bottom row is filled with 1s."405 [#^Triangle t]406 (let [mat (Matrix4f.)407 [vert-1 vert-2 vert-3]408 ((comp vec map) #(.get t %) (range 3))409 unit-normal (do (.calculateNormal t)(.getNormal t))410 vertices [vert-1 vert-2 vert-3 unit-normal]]411 (dorun412 (for [row (range 4) col (range 3)]413 (do414 (.set mat col row (.get (vertices row)col))415 (.set mat 3 row 1))))416 mat))418 (defn triangle-transformation419 "Returns the affine transformation that converts each vertex in the420 first triangle into the corresponding vertex in the second421 triangle."422 [#^Triangle tri-1 #^Triangle tri-2]423 (.mult424 (triangle->matrix4f tri-2)425 (.invert (triangle->matrix4f tri-1))))426 #+end_src428 * Triangle Boundaries430 For efficiency's sake I will divide the UV-image into small squares431 which inscribe each UV-triangle, then extract the points which lie432 inside the triangle and map them to 3D-space using433 =(triangle-transform)= above. To do this I need a function,434 =(inside-triangle?)=, which determines whether a point is inside a435 triangle in 2D UV-space.437 #+name: triangles-4438 #+begin_src clojure439 (defn convex-bounds440 "Returns the smallest square containing the given vertices, as a441 vector of integers [left top width height]."442 [uv-verts]443 (let [xs (map first uv-verts)444 ys (map second uv-verts)445 x0 (Math/floor (apply min xs))446 y0 (Math/floor (apply min ys))447 x1 (Math/ceil (apply max xs))448 y1 (Math/ceil (apply max ys))]449 [x0 y0 (- x1 x0) (- y1 y0)]))451 (defn same-side?452 "Given the points p1 and p2 and the reference point ref, is point p453 on the same side of the line that goes through p1 and p2 as ref is?"454 [p1 p2 ref p]455 (<=456 0457 (.dot458 (.cross (.subtract p2 p1) (.subtract p p1))459 (.cross (.subtract p2 p1) (.subtract ref p1)))))461 (defn inside-triangle?462 "Is the point inside the triangle?"463 {:author "Dylan Holmes"}464 [#^Triangle tri #^Vector3f p]465 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]466 (and467 (same-side? vert-1 vert-2 vert-3 p)468 (same-side? vert-2 vert-3 vert-1 p)469 (same-side? vert-3 vert-1 vert-2 p))))470 #+end_src473 * Physics Collision Objects475 The "hairs" are actually =Rays= which extend from a point on a476 =Triangle= in the =Mesh= normal to the =Triangle's= surface.478 #+name: rays479 #+begin_src clojure480 (defn get-ray-origin481 "Return the origin which a Ray would have to have to be in the exact482 center of a particular Triangle in the Geometry in World483 Coordinates."484 [geom tri]485 (let [new (Vector3f.)]486 (.calculateCenter tri)487 (.localToWorld geom (.getCenter tri) new) new))489 (defn get-ray-direction490 "Return the direction which a Ray would have to have to be to point491 normal to the Triangle, in coordinates relative to the center of the492 Triangle."493 [geom tri]494 (let [n+c (Vector3f.)]495 (.calculateNormal tri)496 (.calculateCenter tri)497 (.localToWorld498 geom499 (.add (.getCenter tri) (.getNormal tri)) n+c)500 (.subtract n+c (get-ray-origin geom tri))))501 #+end_src502 * Headers504 #+name: touch-header505 #+begin_src clojure506 (ns cortex.touch507 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry508 to be outfitted with touch sensors with density determined by a UV509 image. In this way a Geometry can know what parts of itself are510 touching nearby objects. Reads specially prepared blender files to511 construct this sense automatically."512 {:author "Robert McIntyre"}513 (:use (cortex world util sense))514 (:use clojure.contrib.def)515 (:import (com.jme3.scene Geometry Node Mesh))516 (:import com.jme3.collision.CollisionResults)517 (:import com.jme3.scene.VertexBuffer$Type)518 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))519 #+end_src521 * Adding Touch to the Worm523 #+name: test-touch524 #+begin_src clojure525 (ns cortex.test.touch526 (:use (cortex world util sense body touch))527 (:use cortex.test.body))529 (cortex.import/mega-import-jme3)531 (defn test-touch []532 (let [the-worm (doto (worm) (body!))533 touch (touch! the-worm)534 touch-display (view-touch)]535 (world (nodify [the-worm (floor)])536 standard-debug-controls538 (fn [world]539 (light-up-everything world))541 (fn [world tpf]542 (touch-display (map #(% (.getRootNode world)) touch))))))543 #+end_src544 * Source Listing545 * Next548 * COMMENT Code Generation549 #+begin_src clojure :tangle ../src/cortex/touch.clj550 <<touch-header>>551 <<meta-data>>552 <<triangles-1>>553 <<triangles-2>>554 <<triangles-3>>555 <<triangles-4>>556 <<sensors>>557 <<rays>>558 <<kernel>>559 <<visualization>>560 #+end_src563 #+begin_src clojure :tangle ../src/cortex/test/touch.clj564 <<test-touch>>565 #+end_src