annotate org/touch.org @ 240:6961377c4554

saving progress...
author Robert McIntyre <rlm@mit.edu>
date Sun, 12 Feb 2012 13:25:42 -0700
parents 78a640e3bc55
children f2e583be8584
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@238 53 hair length is linearly interpolated between 0 and =scale=. I call
rlm@238 54 these "hairs" /feelers/.
rlm@229 55
rlm@231 56 #+name: meta-data
rlm@0 57 #+begin_src clojure
rlm@229 58 (defn tactile-sensor-profile
rlm@229 59 "Return the touch-sensor distribution image in BufferedImage format,
rlm@229 60 or nil if it does not exist."
rlm@229 61 [#^Geometry obj]
rlm@229 62 (if-let [image-path (meta-data obj "touch")]
rlm@229 63 (load-image image-path)))
rlm@233 64
rlm@233 65 (defn tactile-scale
rlm@233 66 "Return the maximum length of a hair. All hairs are scalled between
rlm@233 67 0.0 and this length, depending on their color. Black is 0, and
rlm@233 68 white is maximum length, and everything in between is scalled
rlm@233 69 linearlly. Default scale is 0.01 jMonkeyEngine units."
rlm@233 70 [#^Geometry obj]
rlm@233 71 (if-let [scale (meta-data obj "scale")]
rlm@233 72 scale 0.1))
rlm@228 73 #+end_src
rlm@156 74
rlm@229 75 ** TODO add image showing example touch-uv map
rlm@229 76 ** TODO add metadata display for worm
rlm@229 77
rlm@234 78
rlm@233 79 * Skin Creation
rlm@234 80 * TODO get the actual lengths for each hair
rlm@234 81
rlm@234 82 #+begin_src clojure
rlm@234 83 pixel-triangles
rlm@234 84 xyz-triangles
rlm@234 85 conversions (map triangles->affine-transform pixel-triangles
rlm@234 86 xyz-triangles)
rlm@234 87
rlm@234 88 #+end_src
rlm@234 89
rlm@238 90
rlm@238 91 =(touch-kernel)= generates the functions which implement the sense of
rlm@238 92 touch for a creature. These functions must do 6 things to obtain touch
rlm@238 93 data.
rlm@238 94
rlm@238 95 - Get the tactile profile image and scale paramaters which describe
rlm@238 96 the layout of feelers along the object's surface.
rlm@239 97 =(tactile-sensor-profile)=, =(tactile-scale)=
rlm@239 98
rlm@238 99 - Get the lengths of each feeler by analyzing the color of the
rlm@238 100 pixels in the tactile profile image.
rlm@239 101 NOT IMPLEMENTED YET
rlm@239 102
rlm@238 103 - Find the triangles which make up the mesh in pixel-space and in
rlm@238 104 world-space.
rlm@239 105 =(triangles)= =(pixel-triangles)=
rlm@239 106
rlm@239 107 - Find the coordinates of each pixel in pixel space. These
rlm@239 108 coordinates are used to make the touch-topology.
rlm@240 109 =(feeler-pixel-coords)=
rlm@239 110
rlm@238 111 - Find the coordinates of each pixel in world-space. These
rlm@240 112 coordinates are the origins of the feelers. =(feeler-origins)=
rlm@239 113
rlm@238 114 - Calculate the normals of the triangles in world space, and add
rlm@238 115 them to each of the origins of the feelers. These are the
rlm@238 116 normalized coordinates of the tips of the feelers.
rlm@240 117 For both of these, =(feeler-tips)=
rlm@239 118
rlm@238 119 - Generate some sort of topology for the sensors.
rlm@239 120 =(touch-topology)=
rlm@239 121
rlm@239 122 #+begin_src clojure
rlm@239 123
rlm@239 124
rlm@239 125
rlm@239 126
rlm@239 127 #+end_src
rlm@239 128
rlm@239 129
rlm@238 130
rlm@233 131 #+name: kernel
rlm@233 132 #+begin_src clojure
rlm@233 133 (in-ns 'cortex.touch)
rlm@233 134
rlm@239 135 (declare touch-topology feelers set-ray)
rlm@234 136
rlm@233 137 (defn touch-kernel
rlm@234 138 "Constructs a function which will return tactile sensory data from
rlm@234 139 'geo when called from inside a running simulation"
rlm@234 140 [#^Geometry geo]
rlm@234 141 (let [[ray-reference-origins
rlm@234 142 ray-reference-tips
rlm@239 143 ray-lengths] (feelers geo)
rlm@234 144 current-rays (map (fn [] (Ray.)) ray-reference-origins)
rlm@234 145 topology (touch-topology geo)]
rlm@234 146 (if (empty? ray-reference-origins) nil
rlm@234 147 (fn [node]
rlm@234 148 (let [transform (.getWorldMatrix geo)]
rlm@234 149 (dorun
rlm@234 150 (map (fn [ray ref-origin ref-tip length]
rlm@234 151 (set-ray ray transform ref-origin ref-tip length))
rlm@234 152 current-rays ray-reference-origins
rlm@234 153 ray-reference-tips ray-lengths))
rlm@234 154 (vector
rlm@234 155 topology
rlm@234 156 (vec
rlm@234 157 (for [ray current-rays]
rlm@234 158 (do
rlm@234 159 (let [results (CollisionResults.)]
rlm@234 160 (.collideWith node ray results)
rlm@234 161 (let [touch-objects
rlm@234 162 (filter #(not (= geo (.getGeometry %)))
rlm@234 163 results)]
rlm@234 164 [(if (empty? touch-objects)
rlm@234 165 (.getLimit ray)
rlm@234 166 (.getDistance (first touch-objects)))
rlm@234 167 (.getLimit ray)])))))))))))
rlm@234 168
rlm@234 169 (defn touch-kernel*
rlm@233 170 "Returns a function which returns tactile sensory data when called
rlm@233 171 inside a running simulation."
rlm@233 172 [#^Geometry geo]
rlm@233 173 (let [feeler-coords (feeler-coordinates geo)
rlm@233 174 tris (triangles geo)
rlm@233 175 limit (tactile-scale geo)]
rlm@233 176 (if (empty? (touch-topology geo))
rlm@233 177 nil
rlm@233 178 (fn [node]
rlm@233 179 (let [sensor-origins
rlm@233 180 (map
rlm@233 181 #(map (partial local-to-world geo) %)
rlm@233 182 feeler-coords)
rlm@233 183 triangle-normals
rlm@233 184 (map (partial get-ray-direction geo)
rlm@233 185 tris)
rlm@233 186 rays
rlm@233 187 (flatten
rlm@233 188 (map (fn [origins norm]
rlm@233 189 (map #(doto (Ray. % norm)
rlm@233 190 (.setLimit limit)) origins))
rlm@233 191 sensor-origins triangle-normals))]
rlm@233 192 (vector
rlm@233 193 (touch-topology geo)
rlm@233 194 (vec
rlm@233 195 (for [ray rays]
rlm@233 196 (do
rlm@233 197 (let [results (CollisionResults.)]
rlm@233 198 (.collideWith node ray results)
rlm@233 199 (let [touch-objects
rlm@233 200 (filter #(not (= geo (.getGeometry %)))
rlm@233 201 results)]
rlm@233 202 [(if (empty? touch-objects)
rlm@233 203 limit (.getDistance (first touch-objects)))
rlm@233 204 limit])))))))))))
rlm@233 205
rlm@233 206 (defn touch!
rlm@233 207 "Endow the creature with the sense of touch. Returns a sequence of
rlm@233 208 functions, one for each body part with a tactile-sensor-proile,
rlm@233 209 each of which when called returns sensory data for that body part."
rlm@233 210 [#^Node creature]
rlm@233 211 (filter
rlm@233 212 (comp not nil?)
rlm@233 213 (map touch-kernel
rlm@233 214 (filter #(isa? (class %) Geometry)
rlm@233 215 (node-seq creature)))))
rlm@233 216 #+end_src
rlm@233 217
rlm@238 218 * Sensor Related Functions
rlm@238 219
rlm@238 220 These functions analyze the touch-sensor-profile image convert the
rlm@238 221 location of each touch sensor from pixel coordinates to UV-coordinates
rlm@238 222 and XYZ-coordinates.
rlm@238 223
rlm@238 224 #+name: sensors
rlm@238 225 #+begin_src clojure
rlm@240 226 (in-ns 'cortex.touch)
rlm@240 227
rlm@240 228 (defn feeler-pixel-coords
rlm@239 229 "Returns the coordinates of the feelers in pixel space in lists, one
rlm@239 230 list for each triangle, ordered in the same way as (triangles) and
rlm@239 231 (pixel-triangles)."
rlm@239 232 [#^Geometry geo image]
rlm@240 233 (map
rlm@240 234 (fn [pixel-triangle]
rlm@240 235 (filter
rlm@240 236 (fn [coord]
rlm@240 237 (inside-triangle? (->triangle pixel-triangle)
rlm@240 238 (->vector3f coord)))
rlm@240 239 (white-coordinates image (convex-bounds pixel-triangle))))
rlm@240 240 (pixel-triangles geo image)))
rlm@239 241
rlm@240 242 (defn feeler-origins [#^Geometry geo image]
rlm@240 243 (let [transforms
rlm@240 244 (map #(triangles->affine-transform
rlm@240 245 (->triangle %1) (->triangle %2))
rlm@240 246 (pixel-triangles geo image)
rlm@240 247 (triangles geo))]
rlm@240 248 (mapcat (fn [transform coords]
rlm@240 249 (map #(.mult transform (->vector3f %)) coords))
rlm@240 250 transforms (feeler-pixel-coords geo image))))
rlm@239 251
rlm@240 252 (defn feeler-tips [#^Geometry geo image]
rlm@240 253 (let [origins (feeler-origins geo image)]
rlm@240 254 (
rlm@240 255
rlm@240 256 )
rlm@239 257
rlm@239 258
rlm@239 259
rlm@238 260 (defn sensors-in-triangle
rlm@238 261 "Locate the touch sensors in the triangle, returning a map of their
rlm@238 262 UV and geometry-relative coordinates."
rlm@238 263 [image mesh tri-index]
rlm@238 264 (let [width (.getWidth image)
rlm@238 265 height (.getHeight image)
rlm@238 266 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@238 267 bounds (convex-bounds UV-vertex-coords)
rlm@238 268
rlm@238 269 cutout-triangle (points->triangle UV-vertex-coords)
rlm@238 270 UV-sensor-coords
rlm@238 271 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@238 272 (fn [[u v]] (Vector3f. u v 0)))
rlm@238 273 (white-coordinates image bounds))
rlm@238 274 UV->geometry (triangle-transformation
rlm@238 275 cutout-triangle
rlm@238 276 (mesh-triangle mesh tri-index))
rlm@238 277 geometry-sensor-coords
rlm@238 278 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@238 279 UV-sensor-coords)]
rlm@238 280 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@238 281
rlm@238 282 (defn-memo locate-feelers
rlm@238 283 "Search the geometry's tactile UV profile for touch sensors,
rlm@238 284 returning their positions in geometry-relative coordinates."
rlm@238 285 [#^Geometry geo]
rlm@238 286 (let [mesh (.getMesh geo)
rlm@238 287 num-triangles (.getTriangleCount mesh)]
rlm@238 288 (if-let [image (tactile-sensor-profile geo)]
rlm@238 289 (map
rlm@238 290 (partial sensors-in-triangle image mesh)
rlm@238 291 (range num-triangles))
rlm@238 292 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@238 293
rlm@238 294 (defn-memo touch-topology
rlm@238 295 "Return a sequence of vectors of the form [x y] describing the
rlm@238 296 \"topology\" of the tactile sensors. Points that are close together
rlm@238 297 in the touch-topology are generally close together in the simulation."
rlm@238 298 [#^Gemoetry geo]
rlm@238 299 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@238 300
rlm@238 301 (defn-memo feeler-coordinates
rlm@238 302 "The location of the touch sensors in world-space coordinates."
rlm@238 303 [#^Geometry geo]
rlm@238 304 (vec (map :geometry (locate-feelers geo))))
rlm@238 305 #+end_src
rlm@238 306
rlm@238 307
rlm@238 308
rlm@238 309
rlm@233 310 * Visualizing Touch
rlm@233 311 #+name: visualization
rlm@233 312 #+begin_src clojure
rlm@233 313 (in-ns 'cortex.touch)
rlm@233 314
rlm@233 315 (defn touch->gray
rlm@233 316 "Convert a pair of [distance, max-distance] into a grayscale pixel"
rlm@233 317 [distance max-distance]
rlm@233 318 (gray
rlm@233 319 (- 255
rlm@233 320 (rem
rlm@233 321 (int
rlm@233 322 (* 255 (/ distance max-distance)))
rlm@233 323 256))))
rlm@233 324
rlm@233 325 (defn view-touch
rlm@233 326 "Creates a function which accepts a list of touch sensor-data and
rlm@233 327 displays each element to the screen."
rlm@233 328 []
rlm@233 329 (view-sense
rlm@233 330 (fn
rlm@233 331 [[coords sensor-data]]
rlm@233 332 (let [image (points->image coords)]
rlm@233 333 (dorun
rlm@233 334 (for [i (range (count coords))]
rlm@233 335 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@233 336 (apply touch->gray (sensor-data i)))))
rlm@233 337 image))))
rlm@233 338 #+end_src
rlm@233 339
rlm@233 340
rlm@233 341
rlm@228 342 * Triangle Manipulation Functions
rlm@228 343
rlm@229 344 The rigid bodies which make up a creature have an underlying
rlm@229 345 =Geometry=, which is a =Mesh= plus a =Material= and other important
rlm@229 346 data involved with displaying the body.
rlm@229 347
rlm@229 348 A =Mesh= is composed of =Triangles=, and each =Triangle= has three
rlm@229 349 verticies which have coordinates in XYZ space and UV space.
rlm@229 350
rlm@229 351 Here, =(triangles)= gets all the triangles which compose a mesh, and
rlm@229 352 =(triangle-UV-coord)= returns the the UV coordinates of the verticies
rlm@229 353 of a triangle.
rlm@229 354
rlm@231 355 #+name: triangles-1
rlm@228 356 #+begin_src clojure
rlm@239 357 (in-ns 'cortex.touch)
rlm@239 358
rlm@239 359 (defn vector3f-seq [#^Vector3f v]
rlm@239 360 [(.getX v) (.getY v) (.getZ v)])
rlm@239 361
rlm@239 362 (defn triangle-seq [#^Triangle tri]
rlm@239 363 [(vector3f-seq (.get1 tri))
rlm@239 364 (vector3f-seq (.get2 tri))
rlm@239 365 (vector3f-seq (.get3 tri))])
rlm@239 366
rlm@240 367 (defn ->vector3f
rlm@240 368 ([coords] (Vector3f. (nth coords 0 0)
rlm@240 369 (nth coords 1 0)
rlm@240 370 (nth coords 2 0))))
rlm@239 371
rlm@239 372 (defn ->triangle [points]
rlm@239 373 (apply #(Triangle. %1 %2 %3) (map ->vector3f points)))
rlm@239 374
rlm@239 375 (defn triangle
rlm@239 376 "Get the triangle specified by triangle-index from the mesh within
rlm@239 377 bounds."
rlm@239 378 [#^Geometry geo triangle-index]
rlm@239 379 (triangle-seq
rlm@239 380 (let [scratch (Triangle.)]
rlm@239 381 (.getTriangle (.getMesh geo) triangle-index scratch) scratch)))
rlm@239 382
rlm@228 383 (defn triangles
rlm@228 384 "Return a sequence of all the Triangles which compose a given
rlm@228 385 Geometry."
rlm@239 386 [#^Geometry geo]
rlm@239 387 (map (partial triangle geo) (range (.getTriangleCount (.getMesh geo)))))
rlm@228 388
rlm@228 389 (defn triangle-vertex-indices
rlm@228 390 "Get the triangle vertex indices of a given triangle from a given
rlm@228 391 mesh."
rlm@228 392 [#^Mesh mesh triangle-index]
rlm@228 393 (let [indices (int-array 3)]
rlm@228 394 (.getTriangle mesh triangle-index indices)
rlm@228 395 (vec indices)))
rlm@228 396
rlm@228 397 (defn vertex-UV-coord
rlm@228 398 "Get the UV-coordinates of the vertex named by vertex-index"
rlm@228 399 [#^Mesh mesh vertex-index]
rlm@228 400 (let [UV-buffer
rlm@228 401 (.getData
rlm@228 402 (.getBuffer
rlm@228 403 mesh
rlm@228 404 VertexBuffer$Type/TexCoord))]
rlm@228 405 [(.get UV-buffer (* vertex-index 2))
rlm@228 406 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@228 407
rlm@239 408 (defn pixel-triangle [#^Geometry geo image index]
rlm@239 409 (let [mesh (.getMesh geo)
rlm@239 410 width (.getWidth image)
rlm@239 411 height (.getHeight image)]
rlm@239 412 (vec (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@239 413 (map (partial vertex-UV-coord mesh)
rlm@239 414 (triangle-vertex-indices mesh index))))))
rlm@228 415
rlm@239 416 (defn pixel-triangles [#^Geometry geo image]
rlm@239 417 (let [height (.getHeight image)
rlm@239 418 width (.getWidth image)]
rlm@239 419 (map (partial pixel-triangle geo image)
rlm@239 420 (range (.getTriangleCount (.getMesh geo))))))
rlm@229 421
rlm@228 422 #+end_src
rlm@228 423
rlm@228 424 * Triangle Affine Transforms
rlm@228 425
rlm@229 426 The position of each hair is stored in a 2D image in UV
rlm@229 427 coordinates. To place the hair in 3D space we must convert from UV
rlm@229 428 coordinates to XYZ coordinates. Each =Triangle= has coordinates in
rlm@229 429 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]
rlm@229 430 for translating any coordinate within the UV triangle to the
rlm@229 431 cooresponding coordinate in the XYZ triangle.
rlm@229 432
rlm@231 433 #+name: triangles-3
rlm@228 434 #+begin_src clojure
rlm@228 435 (defn triangle->matrix4f
rlm@228 436 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@228 437 contain the vertices of the triangle; the last contains the unit
rlm@228 438 normal of the triangle. The bottom row is filled with 1s."
rlm@228 439 [#^Triangle t]
rlm@228 440 (let [mat (Matrix4f.)
rlm@228 441 [vert-1 vert-2 vert-3]
rlm@228 442 ((comp vec map) #(.get t %) (range 3))
rlm@228 443 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@228 444 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@228 445 (dorun
rlm@228 446 (for [row (range 4) col (range 3)]
rlm@228 447 (do
rlm@228 448 (.set mat col row (.get (vertices row)col))
rlm@228 449 (.set mat 3 row 1))))
rlm@228 450 mat))
rlm@228 451
rlm@240 452 (defn triangles->affine-transform
rlm@228 453 "Returns the affine transformation that converts each vertex in the
rlm@228 454 first triangle into the corresponding vertex in the second
rlm@228 455 triangle."
rlm@228 456 [#^Triangle tri-1 #^Triangle tri-2]
rlm@228 457 (.mult
rlm@228 458 (triangle->matrix4f tri-2)
rlm@228 459 (.invert (triangle->matrix4f tri-1))))
rlm@228 460 #+end_src
rlm@228 461
rlm@239 462
rlm@239 463 * Schrapnel Conversion Functions
rlm@239 464
rlm@239 465 It is convienent to treat a =Triangle= as a sequence of verticies, and
rlm@239 466 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion
rlm@239 467 functions make this easy. If these classes implemented =Iterable= then
rlm@239 468 this code would not be necessary. Hopefully they will in the future.
rlm@239 469
rlm@239 470 #+name: triangles-2
rlm@239 471 #+begin_src clojure
rlm@239 472 (defn point->vector2f [[u v]]
rlm@239 473 (Vector2f. u v))
rlm@239 474
rlm@239 475 (defn vector2f->vector3f [v]
rlm@239 476 (Vector3f. (.getX v) (.getY v) 0))
rlm@239 477
rlm@239 478 (defn map-triangle [f #^Triangle tri]
rlm@239 479 (Triangle.
rlm@239 480 (f 0 (.get1 tri))
rlm@239 481 (f 1 (.get2 tri))
rlm@239 482 (f 2 (.get3 tri))))
rlm@239 483
rlm@239 484 (defn points->triangle
rlm@239 485 "Convert a list of points into a triangle."
rlm@239 486 [points]
rlm@239 487 (apply #(Triangle. %1 %2 %3)
rlm@239 488 (map (fn [point]
rlm@239 489 (let [point (vec point)]
rlm@239 490 (Vector3f. (get point 0 0)
rlm@239 491 (get point 1 0)
rlm@239 492 (get point 2 0))))
rlm@239 493 (take 3 points))))
rlm@239 494 #+end_src
rlm@239 495
rlm@239 496
rlm@229 497 * Triangle Boundaries
rlm@229 498
rlm@229 499 For efficiency's sake I will divide the UV-image into small squares
rlm@229 500 which inscribe each UV-triangle, then extract the points which lie
rlm@229 501 inside the triangle and map them to 3D-space using
rlm@229 502 =(triangle-transform)= above. To do this I need a function,
rlm@229 503 =(inside-triangle?)=, which determines whether a point is inside a
rlm@229 504 triangle in 2D UV-space.
rlm@228 505
rlm@231 506 #+name: triangles-4
rlm@228 507 #+begin_src clojure
rlm@229 508 (defn convex-bounds
rlm@229 509 "Returns the smallest square containing the given vertices, as a
rlm@229 510 vector of integers [left top width height]."
rlm@240 511 [verts]
rlm@240 512 (let [xs (map first verts)
rlm@240 513 ys (map second verts)
rlm@229 514 x0 (Math/floor (apply min xs))
rlm@229 515 y0 (Math/floor (apply min ys))
rlm@229 516 x1 (Math/ceil (apply max xs))
rlm@229 517 y1 (Math/ceil (apply max ys))]
rlm@229 518 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@229 519
rlm@229 520 (defn same-side?
rlm@229 521 "Given the points p1 and p2 and the reference point ref, is point p
rlm@229 522 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@229 523 [p1 p2 ref p]
rlm@229 524 (<=
rlm@229 525 0
rlm@229 526 (.dot
rlm@229 527 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@229 528 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@229 529
rlm@229 530 (defn inside-triangle?
rlm@229 531 "Is the point inside the triangle?"
rlm@229 532 {:author "Dylan Holmes"}
rlm@229 533 [#^Triangle tri #^Vector3f p]
rlm@240 534 (let [[vert-1 vert-2 vert-3] [(.get1 tri) (.get2 tri) (.get3 tri)]]
rlm@229 535 (and
rlm@229 536 (same-side? vert-1 vert-2 vert-3 p)
rlm@229 537 (same-side? vert-2 vert-3 vert-1 p)
rlm@229 538 (same-side? vert-3 vert-1 vert-2 p))))
rlm@229 539 #+end_src
rlm@229 540
rlm@240 541 #+results: triangles-4
rlm@240 542 : #'cortex.touch/inside-triangle?
rlm@240 543
rlm@229 544
rlm@228 545 * Physics Collision Objects
rlm@230 546
rlm@234 547 The "hairs" are actually =Rays= which extend from a point on a
rlm@230 548 =Triangle= in the =Mesh= normal to the =Triangle's= surface.
rlm@230 549
rlm@231 550 #+name: rays
rlm@228 551 #+begin_src clojure
rlm@228 552 (defn get-ray-origin
rlm@228 553 "Return the origin which a Ray would have to have to be in the exact
rlm@228 554 center of a particular Triangle in the Geometry in World
rlm@228 555 Coordinates."
rlm@228 556 [geom tri]
rlm@228 557 (let [new (Vector3f.)]
rlm@228 558 (.calculateCenter tri)
rlm@228 559 (.localToWorld geom (.getCenter tri) new) new))
rlm@228 560
rlm@228 561 (defn get-ray-direction
rlm@228 562 "Return the direction which a Ray would have to have to be to point
rlm@228 563 normal to the Triangle, in coordinates relative to the center of the
rlm@228 564 Triangle."
rlm@228 565 [geom tri]
rlm@228 566 (let [n+c (Vector3f.)]
rlm@228 567 (.calculateNormal tri)
rlm@228 568 (.calculateCenter tri)
rlm@228 569 (.localToWorld
rlm@228 570 geom
rlm@228 571 (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@228 572 (.subtract n+c (get-ray-origin geom tri))))
rlm@228 573 #+end_src
rlm@226 574 * Headers
rlm@231 575
rlm@231 576 #+name: touch-header
rlm@226 577 #+begin_src clojure
rlm@226 578 (ns cortex.touch
rlm@226 579 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
rlm@226 580 to be outfitted with touch sensors with density determined by a UV
rlm@226 581 image. In this way a Geometry can know what parts of itself are
rlm@226 582 touching nearby objects. Reads specially prepared blender files to
rlm@226 583 construct this sense automatically."
rlm@226 584 {:author "Robert McIntyre"}
rlm@226 585 (:use (cortex world util sense))
rlm@226 586 (:use clojure.contrib.def)
rlm@226 587 (:import (com.jme3.scene Geometry Node Mesh))
rlm@226 588 (:import com.jme3.collision.CollisionResults)
rlm@226 589 (:import com.jme3.scene.VertexBuffer$Type)
rlm@226 590 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
rlm@226 591 #+end_src
rlm@37 592
rlm@232 593 * Adding Touch to the Worm
rlm@232 594
rlm@232 595 #+name: test-touch
rlm@232 596 #+begin_src clojure
rlm@232 597 (ns cortex.test.touch
rlm@232 598 (:use (cortex world util sense body touch))
rlm@232 599 (:use cortex.test.body))
rlm@232 600
rlm@232 601 (cortex.import/mega-import-jme3)
rlm@232 602
rlm@232 603 (defn test-touch []
rlm@232 604 (let [the-worm (doto (worm) (body!))
rlm@232 605 touch (touch! the-worm)
rlm@232 606 touch-display (view-touch)]
rlm@232 607 (world (nodify [the-worm (floor)])
rlm@232 608 standard-debug-controls
rlm@232 609
rlm@232 610 (fn [world]
rlm@232 611 (light-up-everything world))
rlm@232 612
rlm@232 613 (fn [world tpf]
rlm@232 614 (touch-display (map #(% (.getRootNode world)) touch))))))
rlm@232 615 #+end_src
rlm@228 616 * Source Listing
rlm@228 617 * Next
rlm@228 618
rlm@228 619
rlm@226 620 * COMMENT Code Generation
rlm@39 621 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@231 622 <<touch-header>>
rlm@231 623 <<meta-data>>
rlm@231 624 <<triangles-1>>
rlm@231 625 <<triangles-2>>
rlm@231 626 <<triangles-3>>
rlm@231 627 <<triangles-4>>
rlm@231 628 <<sensors>>
rlm@231 629 <<rays>>
rlm@231 630 <<kernel>>
rlm@231 631 <<visualization>>
rlm@0 632 #+end_src
rlm@0 633
rlm@232 634
rlm@68 635 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
rlm@232 636 <<test-touch>>
rlm@39 637 #+end_src
rlm@39 638
rlm@0 639
rlm@0 640
rlm@0 641
rlm@32 642
rlm@32 643
rlm@226 644