annotate org/touch.org @ 239:78a640e3bc55

saving progress... touch is in an inconsistent state.
author Robert McIntyre <rlm@mit.edu>
date Sun, 12 Feb 2012 12:58:01 -0700
parents 3fa49ff1649a
children 6961377c4554
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@239 109 =(sensors-in-triangle)=
rlm@239 110
rlm@238 111 - Find the coordinates of each pixel in world-space. These
rlm@238 112 coordinates are the origins of the feelers.
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@239 117 For both of these, =(feelers)=
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@239 226 (defn pixel-feelers
rlm@239 227 "Returns the coordinates of the feelers in pixel space in lists, one
rlm@239 228 list for each triangle, ordered in the same way as (triangles) and
rlm@239 229 (pixel-triangles)."
rlm@239 230 [#^Geometry geo image]
rlm@239 231
rlm@239 232
rlm@239 233
rlm@239 234
rlm@239 235
rlm@239 236
rlm@238 237 (defn sensors-in-triangle
rlm@238 238 "Locate the touch sensors in the triangle, returning a map of their
rlm@238 239 UV and geometry-relative coordinates."
rlm@238 240 [image mesh tri-index]
rlm@238 241 (let [width (.getWidth image)
rlm@238 242 height (.getHeight image)
rlm@238 243 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@238 244 bounds (convex-bounds UV-vertex-coords)
rlm@238 245
rlm@238 246 cutout-triangle (points->triangle UV-vertex-coords)
rlm@238 247 UV-sensor-coords
rlm@238 248 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@238 249 (fn [[u v]] (Vector3f. u v 0)))
rlm@238 250 (white-coordinates image bounds))
rlm@238 251 UV->geometry (triangle-transformation
rlm@238 252 cutout-triangle
rlm@238 253 (mesh-triangle mesh tri-index))
rlm@238 254 geometry-sensor-coords
rlm@238 255 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@238 256 UV-sensor-coords)]
rlm@238 257 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@238 258
rlm@238 259 (defn-memo locate-feelers
rlm@238 260 "Search the geometry's tactile UV profile for touch sensors,
rlm@238 261 returning their positions in geometry-relative coordinates."
rlm@238 262 [#^Geometry geo]
rlm@238 263 (let [mesh (.getMesh geo)
rlm@238 264 num-triangles (.getTriangleCount mesh)]
rlm@238 265 (if-let [image (tactile-sensor-profile geo)]
rlm@238 266 (map
rlm@238 267 (partial sensors-in-triangle image mesh)
rlm@238 268 (range num-triangles))
rlm@238 269 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@238 270
rlm@238 271 (defn-memo touch-topology
rlm@238 272 "Return a sequence of vectors of the form [x y] describing the
rlm@238 273 \"topology\" of the tactile sensors. Points that are close together
rlm@238 274 in the touch-topology are generally close together in the simulation."
rlm@238 275 [#^Gemoetry geo]
rlm@238 276 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@238 277
rlm@238 278 (defn-memo feeler-coordinates
rlm@238 279 "The location of the touch sensors in world-space coordinates."
rlm@238 280 [#^Geometry geo]
rlm@238 281 (vec (map :geometry (locate-feelers geo))))
rlm@238 282 #+end_src
rlm@238 283
rlm@238 284
rlm@238 285
rlm@238 286
rlm@233 287 * Visualizing Touch
rlm@233 288 #+name: visualization
rlm@233 289 #+begin_src clojure
rlm@233 290 (in-ns 'cortex.touch)
rlm@233 291
rlm@233 292 (defn touch->gray
rlm@233 293 "Convert a pair of [distance, max-distance] into a grayscale pixel"
rlm@233 294 [distance max-distance]
rlm@233 295 (gray
rlm@233 296 (- 255
rlm@233 297 (rem
rlm@233 298 (int
rlm@233 299 (* 255 (/ distance max-distance)))
rlm@233 300 256))))
rlm@233 301
rlm@233 302 (defn view-touch
rlm@233 303 "Creates a function which accepts a list of touch sensor-data and
rlm@233 304 displays each element to the screen."
rlm@233 305 []
rlm@233 306 (view-sense
rlm@233 307 (fn
rlm@233 308 [[coords sensor-data]]
rlm@233 309 (let [image (points->image coords)]
rlm@233 310 (dorun
rlm@233 311 (for [i (range (count coords))]
rlm@233 312 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@233 313 (apply touch->gray (sensor-data i)))))
rlm@233 314 image))))
rlm@233 315 #+end_src
rlm@233 316
rlm@233 317
rlm@233 318
rlm@228 319 * Triangle Manipulation Functions
rlm@228 320
rlm@229 321 The rigid bodies which make up a creature have an underlying
rlm@229 322 =Geometry=, which is a =Mesh= plus a =Material= and other important
rlm@229 323 data involved with displaying the body.
rlm@229 324
rlm@229 325 A =Mesh= is composed of =Triangles=, and each =Triangle= has three
rlm@229 326 verticies which have coordinates in XYZ space and UV space.
rlm@229 327
rlm@229 328 Here, =(triangles)= gets all the triangles which compose a mesh, and
rlm@229 329 =(triangle-UV-coord)= returns the the UV coordinates of the verticies
rlm@229 330 of a triangle.
rlm@229 331
rlm@231 332 #+name: triangles-1
rlm@228 333 #+begin_src clojure
rlm@239 334 (in-ns 'cortex.touch)
rlm@239 335
rlm@239 336 (defn vector3f-seq [#^Vector3f v]
rlm@239 337 [(.getX v) (.getY v) (.getZ v)])
rlm@239 338
rlm@239 339 (defn triangle-seq [#^Triangle tri]
rlm@239 340 [(vector3f-seq (.get1 tri))
rlm@239 341 (vector3f-seq (.get2 tri))
rlm@239 342 (vector3f-seq (.get3 tri))])
rlm@239 343
rlm@239 344 (defn ->vector3f [[x y z]] (Vector3f. x y z))
rlm@239 345
rlm@239 346 (defn ->triangle [points]
rlm@239 347 (apply #(Triangle. %1 %2 %3) (map ->vector3f points)))
rlm@239 348
rlm@239 349 (defn triangle
rlm@239 350 "Get the triangle specified by triangle-index from the mesh within
rlm@239 351 bounds."
rlm@239 352 [#^Geometry geo triangle-index]
rlm@239 353 (triangle-seq
rlm@239 354 (let [scratch (Triangle.)]
rlm@239 355 (.getTriangle (.getMesh geo) triangle-index scratch) scratch)))
rlm@239 356
rlm@228 357 (defn triangles
rlm@228 358 "Return a sequence of all the Triangles which compose a given
rlm@228 359 Geometry."
rlm@239 360 [#^Geometry geo]
rlm@239 361 (map (partial triangle geo) (range (.getTriangleCount (.getMesh geo)))))
rlm@228 362
rlm@228 363 (defn triangle-vertex-indices
rlm@228 364 "Get the triangle vertex indices of a given triangle from a given
rlm@228 365 mesh."
rlm@228 366 [#^Mesh mesh triangle-index]
rlm@228 367 (let [indices (int-array 3)]
rlm@228 368 (.getTriangle mesh triangle-index indices)
rlm@228 369 (vec indices)))
rlm@228 370
rlm@228 371 (defn vertex-UV-coord
rlm@228 372 "Get the UV-coordinates of the vertex named by vertex-index"
rlm@228 373 [#^Mesh mesh vertex-index]
rlm@228 374 (let [UV-buffer
rlm@228 375 (.getData
rlm@228 376 (.getBuffer
rlm@228 377 mesh
rlm@228 378 VertexBuffer$Type/TexCoord))]
rlm@228 379 [(.get UV-buffer (* vertex-index 2))
rlm@228 380 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@228 381
rlm@239 382 (defn pixel-triangle [#^Geometry geo image index]
rlm@239 383 (let [mesh (.getMesh geo)
rlm@239 384 width (.getWidth image)
rlm@239 385 height (.getHeight image)]
rlm@239 386 (vec (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@239 387 (map (partial vertex-UV-coord mesh)
rlm@239 388 (triangle-vertex-indices mesh index))))))
rlm@228 389
rlm@239 390 (defn pixel-triangles [#^Geometry geo image]
rlm@239 391 (let [height (.getHeight image)
rlm@239 392 width (.getWidth image)]
rlm@239 393 (map (partial pixel-triangle geo image)
rlm@239 394 (range (.getTriangleCount (.getMesh geo))))))
rlm@229 395
rlm@228 396 #+end_src
rlm@228 397
rlm@228 398 * Triangle Affine Transforms
rlm@228 399
rlm@229 400 The position of each hair is stored in a 2D image in UV
rlm@229 401 coordinates. To place the hair in 3D space we must convert from UV
rlm@229 402 coordinates to XYZ coordinates. Each =Triangle= has coordinates in
rlm@229 403 both UV-space and XYZ-space, which defines a unique [[http://mathworld.wolfram.com/AffineTransformation.html ][Affine Transform]]
rlm@229 404 for translating any coordinate within the UV triangle to the
rlm@229 405 cooresponding coordinate in the XYZ triangle.
rlm@229 406
rlm@231 407 #+name: triangles-3
rlm@228 408 #+begin_src clojure
rlm@228 409 (defn triangle->matrix4f
rlm@228 410 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@228 411 contain the vertices of the triangle; the last contains the unit
rlm@228 412 normal of the triangle. The bottom row is filled with 1s."
rlm@228 413 [#^Triangle t]
rlm@228 414 (let [mat (Matrix4f.)
rlm@228 415 [vert-1 vert-2 vert-3]
rlm@228 416 ((comp vec map) #(.get t %) (range 3))
rlm@228 417 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@228 418 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@228 419 (dorun
rlm@228 420 (for [row (range 4) col (range 3)]
rlm@228 421 (do
rlm@228 422 (.set mat col row (.get (vertices row)col))
rlm@228 423 (.set mat 3 row 1))))
rlm@228 424 mat))
rlm@228 425
rlm@228 426 (defn triangle-transformation
rlm@228 427 "Returns the affine transformation that converts each vertex in the
rlm@228 428 first triangle into the corresponding vertex in the second
rlm@228 429 triangle."
rlm@228 430 [#^Triangle tri-1 #^Triangle tri-2]
rlm@228 431 (.mult
rlm@228 432 (triangle->matrix4f tri-2)
rlm@228 433 (.invert (triangle->matrix4f tri-1))))
rlm@228 434 #+end_src
rlm@228 435
rlm@239 436
rlm@239 437 * Schrapnel Conversion Functions
rlm@239 438
rlm@239 439 It is convienent to treat a =Triangle= as a sequence of verticies, and
rlm@239 440 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion
rlm@239 441 functions make this easy. If these classes implemented =Iterable= then
rlm@239 442 this code would not be necessary. Hopefully they will in the future.
rlm@239 443
rlm@239 444 #+name: triangles-2
rlm@239 445 #+begin_src clojure
rlm@239 446 (defn point->vector2f [[u v]]
rlm@239 447 (Vector2f. u v))
rlm@239 448
rlm@239 449 (defn vector2f->vector3f [v]
rlm@239 450 (Vector3f. (.getX v) (.getY v) 0))
rlm@239 451
rlm@239 452 (defn map-triangle [f #^Triangle tri]
rlm@239 453 (Triangle.
rlm@239 454 (f 0 (.get1 tri))
rlm@239 455 (f 1 (.get2 tri))
rlm@239 456 (f 2 (.get3 tri))))
rlm@239 457
rlm@239 458 (defn points->triangle
rlm@239 459 "Convert a list of points into a triangle."
rlm@239 460 [points]
rlm@239 461 (apply #(Triangle. %1 %2 %3)
rlm@239 462 (map (fn [point]
rlm@239 463 (let [point (vec point)]
rlm@239 464 (Vector3f. (get point 0 0)
rlm@239 465 (get point 1 0)
rlm@239 466 (get point 2 0))))
rlm@239 467 (take 3 points))))
rlm@239 468 #+end_src
rlm@239 469
rlm@239 470
rlm@229 471 * Triangle Boundaries
rlm@229 472
rlm@229 473 For efficiency's sake I will divide the UV-image into small squares
rlm@229 474 which inscribe each UV-triangle, then extract the points which lie
rlm@229 475 inside the triangle and map them to 3D-space using
rlm@229 476 =(triangle-transform)= above. To do this I need a function,
rlm@229 477 =(inside-triangle?)=, which determines whether a point is inside a
rlm@229 478 triangle in 2D UV-space.
rlm@228 479
rlm@231 480 #+name: triangles-4
rlm@228 481 #+begin_src clojure
rlm@229 482 (defn convex-bounds
rlm@229 483 "Returns the smallest square containing the given vertices, as a
rlm@229 484 vector of integers [left top width height]."
rlm@229 485 [uv-verts]
rlm@229 486 (let [xs (map first uv-verts)
rlm@229 487 ys (map second uv-verts)
rlm@229 488 x0 (Math/floor (apply min xs))
rlm@229 489 y0 (Math/floor (apply min ys))
rlm@229 490 x1 (Math/ceil (apply max xs))
rlm@229 491 y1 (Math/ceil (apply max ys))]
rlm@229 492 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@229 493
rlm@229 494 (defn same-side?
rlm@229 495 "Given the points p1 and p2 and the reference point ref, is point p
rlm@229 496 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@229 497 [p1 p2 ref p]
rlm@229 498 (<=
rlm@229 499 0
rlm@229 500 (.dot
rlm@229 501 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@229 502 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@229 503
rlm@229 504 (defn inside-triangle?
rlm@229 505 "Is the point inside the triangle?"
rlm@229 506 {:author "Dylan Holmes"}
rlm@229 507 [#^Triangle tri #^Vector3f p]
rlm@229 508 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@229 509 (and
rlm@229 510 (same-side? vert-1 vert-2 vert-3 p)
rlm@229 511 (same-side? vert-2 vert-3 vert-1 p)
rlm@229 512 (same-side? vert-3 vert-1 vert-2 p))))
rlm@229 513 #+end_src
rlm@229 514
rlm@229 515
rlm@228 516 * Physics Collision Objects
rlm@230 517
rlm@234 518 The "hairs" are actually =Rays= which extend from a point on a
rlm@230 519 =Triangle= in the =Mesh= normal to the =Triangle's= surface.
rlm@230 520
rlm@231 521 #+name: rays
rlm@228 522 #+begin_src clojure
rlm@228 523 (defn get-ray-origin
rlm@228 524 "Return the origin which a Ray would have to have to be in the exact
rlm@228 525 center of a particular Triangle in the Geometry in World
rlm@228 526 Coordinates."
rlm@228 527 [geom tri]
rlm@228 528 (let [new (Vector3f.)]
rlm@228 529 (.calculateCenter tri)
rlm@228 530 (.localToWorld geom (.getCenter tri) new) new))
rlm@228 531
rlm@228 532 (defn get-ray-direction
rlm@228 533 "Return the direction which a Ray would have to have to be to point
rlm@228 534 normal to the Triangle, in coordinates relative to the center of the
rlm@228 535 Triangle."
rlm@228 536 [geom tri]
rlm@228 537 (let [n+c (Vector3f.)]
rlm@228 538 (.calculateNormal tri)
rlm@228 539 (.calculateCenter tri)
rlm@228 540 (.localToWorld
rlm@228 541 geom
rlm@228 542 (.add (.getCenter tri) (.getNormal tri)) n+c)
rlm@228 543 (.subtract n+c (get-ray-origin geom tri))))
rlm@228 544 #+end_src
rlm@226 545 * Headers
rlm@231 546
rlm@231 547 #+name: touch-header
rlm@226 548 #+begin_src clojure
rlm@226 549 (ns cortex.touch
rlm@226 550 "Simulate the sense of touch in jMonkeyEngine3. Enables any Geometry
rlm@226 551 to be outfitted with touch sensors with density determined by a UV
rlm@226 552 image. In this way a Geometry can know what parts of itself are
rlm@226 553 touching nearby objects. Reads specially prepared blender files to
rlm@226 554 construct this sense automatically."
rlm@226 555 {:author "Robert McIntyre"}
rlm@226 556 (:use (cortex world util sense))
rlm@226 557 (:use clojure.contrib.def)
rlm@226 558 (:import (com.jme3.scene Geometry Node Mesh))
rlm@226 559 (:import com.jme3.collision.CollisionResults)
rlm@226 560 (:import com.jme3.scene.VertexBuffer$Type)
rlm@226 561 (:import (com.jme3.math Triangle Vector3f Vector2f Ray Matrix4f)))
rlm@226 562 #+end_src
rlm@37 563
rlm@232 564 * Adding Touch to the Worm
rlm@232 565
rlm@232 566 #+name: test-touch
rlm@232 567 #+begin_src clojure
rlm@232 568 (ns cortex.test.touch
rlm@232 569 (:use (cortex world util sense body touch))
rlm@232 570 (:use cortex.test.body))
rlm@232 571
rlm@232 572 (cortex.import/mega-import-jme3)
rlm@232 573
rlm@232 574 (defn test-touch []
rlm@232 575 (let [the-worm (doto (worm) (body!))
rlm@232 576 touch (touch! the-worm)
rlm@232 577 touch-display (view-touch)]
rlm@232 578 (world (nodify [the-worm (floor)])
rlm@232 579 standard-debug-controls
rlm@232 580
rlm@232 581 (fn [world]
rlm@232 582 (light-up-everything world))
rlm@232 583
rlm@232 584 (fn [world tpf]
rlm@232 585 (touch-display (map #(% (.getRootNode world)) touch))))))
rlm@232 586 #+end_src
rlm@228 587 * Source Listing
rlm@228 588 * Next
rlm@228 589
rlm@228 590
rlm@226 591 * COMMENT Code Generation
rlm@39 592 #+begin_src clojure :tangle ../src/cortex/touch.clj
rlm@231 593 <<touch-header>>
rlm@231 594 <<meta-data>>
rlm@231 595 <<triangles-1>>
rlm@231 596 <<triangles-2>>
rlm@231 597 <<triangles-3>>
rlm@231 598 <<triangles-4>>
rlm@231 599 <<sensors>>
rlm@231 600 <<rays>>
rlm@231 601 <<kernel>>
rlm@231 602 <<visualization>>
rlm@0 603 #+end_src
rlm@0 604
rlm@232 605
rlm@68 606 #+begin_src clojure :tangle ../src/cortex/test/touch.clj
rlm@232 607 <<test-touch>>
rlm@39 608 #+end_src
rlm@39 609
rlm@0 610
rlm@0 611
rlm@0 612
rlm@32 613
rlm@32 614
rlm@226 615