comparison 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
comparison
equal deleted inserted replaced
239:78a640e3bc55 240:6961377c4554
104 world-space. 104 world-space.
105 =(triangles)= =(pixel-triangles)= 105 =(triangles)= =(pixel-triangles)=
106 106
107 - Find the coordinates of each pixel in pixel space. These 107 - Find the coordinates of each pixel in pixel space. These
108 coordinates are used to make the touch-topology. 108 coordinates are used to make the touch-topology.
109 =(sensors-in-triangle)= 109 =(feeler-pixel-coords)=
110 110
111 - Find the coordinates of each pixel in world-space. These 111 - Find the coordinates of each pixel in world-space. These
112 coordinates are the origins of the feelers. 112 coordinates are the origins of the feelers. =(feeler-origins)=
113 113
114 - Calculate the normals of the triangles in world space, and add 114 - Calculate the normals of the triangles in world space, and add
115 them to each of the origins of the feelers. These are the 115 them to each of the origins of the feelers. These are the
116 normalized coordinates of the tips of the feelers. 116 normalized coordinates of the tips of the feelers.
117 For both of these, =(feelers)= 117 For both of these, =(feeler-tips)=
118 118
119 - Generate some sort of topology for the sensors. 119 - Generate some sort of topology for the sensors.
120 =(touch-topology)= 120 =(touch-topology)=
121 121
122 #+begin_src clojure 122 #+begin_src clojure
221 location of each touch sensor from pixel coordinates to UV-coordinates 221 location of each touch sensor from pixel coordinates to UV-coordinates
222 and XYZ-coordinates. 222 and XYZ-coordinates.
223 223
224 #+name: sensors 224 #+name: sensors
225 #+begin_src clojure 225 #+begin_src clojure
226 (defn pixel-feelers 226 (in-ns 'cortex.touch)
227
228 (defn feeler-pixel-coords
227 "Returns the coordinates of the feelers in pixel space in lists, one 229 "Returns the coordinates of the feelers in pixel space in lists, one
228 list for each triangle, ordered in the same way as (triangles) and 230 list for each triangle, ordered in the same way as (triangles) and
229 (pixel-triangles)." 231 (pixel-triangles)."
230 [#^Geometry geo image] 232 [#^Geometry geo image]
231 233 (map
232 234 (fn [pixel-triangle]
233 235 (filter
236 (fn [coord]
237 (inside-triangle? (->triangle pixel-triangle)
238 (->vector3f coord)))
239 (white-coordinates image (convex-bounds pixel-triangle))))
240 (pixel-triangles geo image)))
241
242 (defn feeler-origins [#^Geometry geo image]
243 (let [transforms
244 (map #(triangles->affine-transform
245 (->triangle %1) (->triangle %2))
246 (pixel-triangles geo image)
247 (triangles geo))]
248 (mapcat (fn [transform coords]
249 (map #(.mult transform (->vector3f %)) coords))
250 transforms (feeler-pixel-coords geo image))))
251
252 (defn feeler-tips [#^Geometry geo image]
253 (let [origins (feeler-origins geo image)]
254 (
255
256 )
234 257
235 258
236 259
237 (defn sensors-in-triangle 260 (defn sensors-in-triangle
238 "Locate the touch sensors in the triangle, returning a map of their 261 "Locate the touch sensors in the triangle, returning a map of their
339 (defn triangle-seq [#^Triangle tri] 362 (defn triangle-seq [#^Triangle tri]
340 [(vector3f-seq (.get1 tri)) 363 [(vector3f-seq (.get1 tri))
341 (vector3f-seq (.get2 tri)) 364 (vector3f-seq (.get2 tri))
342 (vector3f-seq (.get3 tri))]) 365 (vector3f-seq (.get3 tri))])
343 366
344 (defn ->vector3f [[x y z]] (Vector3f. x y z)) 367 (defn ->vector3f
368 ([coords] (Vector3f. (nth coords 0 0)
369 (nth coords 1 0)
370 (nth coords 2 0))))
345 371
346 (defn ->triangle [points] 372 (defn ->triangle [points]
347 (apply #(Triangle. %1 %2 %3) (map ->vector3f points))) 373 (apply #(Triangle. %1 %2 %3) (map ->vector3f points)))
348 374
349 (defn triangle 375 (defn triangle
421 (do 447 (do
422 (.set mat col row (.get (vertices row)col)) 448 (.set mat col row (.get (vertices row)col))
423 (.set mat 3 row 1)))) 449 (.set mat 3 row 1))))
424 mat)) 450 mat))
425 451
426 (defn triangle-transformation 452 (defn triangles->affine-transform
427 "Returns the affine transformation that converts each vertex in the 453 "Returns the affine transformation that converts each vertex in the
428 first triangle into the corresponding vertex in the second 454 first triangle into the corresponding vertex in the second
429 triangle." 455 triangle."
430 [#^Triangle tri-1 #^Triangle tri-2] 456 [#^Triangle tri-1 #^Triangle tri-2]
431 (.mult 457 (.mult
480 #+name: triangles-4 506 #+name: triangles-4
481 #+begin_src clojure 507 #+begin_src clojure
482 (defn convex-bounds 508 (defn convex-bounds
483 "Returns the smallest square containing the given vertices, as a 509 "Returns the smallest square containing the given vertices, as a
484 vector of integers [left top width height]." 510 vector of integers [left top width height]."
485 [uv-verts] 511 [verts]
486 (let [xs (map first uv-verts) 512 (let [xs (map first verts)
487 ys (map second uv-verts) 513 ys (map second verts)
488 x0 (Math/floor (apply min xs)) 514 x0 (Math/floor (apply min xs))
489 y0 (Math/floor (apply min ys)) 515 y0 (Math/floor (apply min ys))
490 x1 (Math/ceil (apply max xs)) 516 x1 (Math/ceil (apply max xs))
491 y1 (Math/ceil (apply max ys))] 517 y1 (Math/ceil (apply max ys))]
492 [x0 y0 (- x1 x0) (- y1 y0)])) 518 [x0 y0 (- x1 x0) (- y1 y0)]))
503 529
504 (defn inside-triangle? 530 (defn inside-triangle?
505 "Is the point inside the triangle?" 531 "Is the point inside the triangle?"
506 {:author "Dylan Holmes"} 532 {:author "Dylan Holmes"}
507 [#^Triangle tri #^Vector3f p] 533 [#^Triangle tri #^Vector3f p]
508 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)] 534 (let [[vert-1 vert-2 vert-3] [(.get1 tri) (.get2 tri) (.get3 tri)]]
509 (and 535 (and
510 (same-side? vert-1 vert-2 vert-3 p) 536 (same-side? vert-1 vert-2 vert-3 p)
511 (same-side? vert-2 vert-3 vert-1 p) 537 (same-side? vert-2 vert-3 vert-1 p)
512 (same-side? vert-3 vert-1 vert-2 p)))) 538 (same-side? vert-3 vert-1 vert-2 p))))
513 #+end_src 539 #+end_src
540
541 #+results: triangles-4
542 : #'cortex.touch/inside-triangle?
514 543
515 544
516 * Physics Collision Objects 545 * Physics Collision Objects
517 546
518 The "hairs" are actually =Rays= which extend from a point on a 547 The "hairs" are actually =Rays= which extend from a point on a