Mercurial > cortex
comparison 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 |
comparison
equal
deleted
inserted
replaced
238:3fa49ff1649a | 239:78a640e3bc55 |
---|---|
92 touch for a creature. These functions must do 6 things to obtain touch | 92 touch for a creature. These functions must do 6 things to obtain touch |
93 data. | 93 data. |
94 | 94 |
95 - Get the tactile profile image and scale paramaters which describe | 95 - Get the tactile profile image and scale paramaters which describe |
96 the layout of feelers along the object's surface. | 96 the layout of feelers along the object's surface. |
97 =(tactile-sensor-profile)=, =(tactile-scale)= | |
98 | |
97 - Get the lengths of each feeler by analyzing the color of the | 99 - Get the lengths of each feeler by analyzing the color of the |
98 pixels in the tactile profile image. | 100 pixels in the tactile profile image. |
101 NOT IMPLEMENTED YET | |
102 | |
99 - Find the triangles which make up the mesh in pixel-space and in | 103 - Find the triangles which make up the mesh in pixel-space and in |
100 world-space. | 104 world-space. |
105 =(triangles)= =(pixel-triangles)= | |
106 | |
107 - Find the coordinates of each pixel in pixel space. These | |
108 coordinates are used to make the touch-topology. | |
109 =(sensors-in-triangle)= | |
110 | |
101 - Find the coordinates of each pixel in world-space. These | 111 - Find the coordinates of each pixel in world-space. These |
102 coordinates are the origins of the feelers. | 112 coordinates are the origins of the feelers. |
113 | |
103 - Calculate the normals of the triangles in world space, and add | 114 - Calculate the normals of the triangles in world space, and add |
104 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 |
105 normalized coordinates of the tips of the feelers. | 116 normalized coordinates of the tips of the feelers. |
117 For both of these, =(feelers)= | |
118 | |
106 - Generate some sort of topology for the sensors. | 119 - Generate some sort of topology for the sensors. |
120 =(touch-topology)= | |
121 | |
122 #+begin_src clojure | |
123 | |
124 | |
125 | |
126 | |
127 #+end_src | |
128 | |
129 | |
107 | 130 |
108 #+name: kernel | 131 #+name: kernel |
109 #+begin_src clojure | 132 #+begin_src clojure |
110 (in-ns 'cortex.touch) | 133 (in-ns 'cortex.touch) |
111 | 134 |
112 (declare touch-topology touch-hairs set-ray) | 135 (declare touch-topology feelers set-ray) |
113 | 136 |
114 (defn touch-kernel | 137 (defn touch-kernel |
115 "Constructs a function which will return tactile sensory data from | 138 "Constructs a function which will return tactile sensory data from |
116 'geo when called from inside a running simulation" | 139 'geo when called from inside a running simulation" |
117 [#^Geometry geo] | 140 [#^Geometry geo] |
118 (let [[ray-reference-origins | 141 (let [[ray-reference-origins |
119 ray-reference-tips | 142 ray-reference-tips |
120 ray-lengths] (touch-hairs geo) | 143 ray-lengths] (feelers geo) |
121 current-rays (map (fn [] (Ray.)) ray-reference-origins) | 144 current-rays (map (fn [] (Ray.)) ray-reference-origins) |
122 topology (touch-topology geo)] | 145 topology (touch-topology geo)] |
123 (if (empty? ray-reference-origins) nil | 146 (if (empty? ray-reference-origins) nil |
124 (fn [node] | 147 (fn [node] |
125 (let [transform (.getWorldMatrix geo)] | 148 (let [transform (.getWorldMatrix geo)] |
198 location of each touch sensor from pixel coordinates to UV-coordinates | 221 location of each touch sensor from pixel coordinates to UV-coordinates |
199 and XYZ-coordinates. | 222 and XYZ-coordinates. |
200 | 223 |
201 #+name: sensors | 224 #+name: sensors |
202 #+begin_src clojure | 225 #+begin_src clojure |
226 (defn pixel-feelers | |
227 "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 | |
229 (pixel-triangles)." | |
230 [#^Geometry geo image] | |
231 | |
232 | |
233 | |
234 | |
235 | |
236 | |
203 (defn sensors-in-triangle | 237 (defn sensors-in-triangle |
204 "Locate the touch sensors in the triangle, returning a map of their | 238 "Locate the touch sensors in the triangle, returning a map of their |
205 UV and geometry-relative coordinates." | 239 UV and geometry-relative coordinates." |
206 [image mesh tri-index] | 240 [image mesh tri-index] |
207 (let [width (.getWidth image) | 241 (let [width (.getWidth image) |
295 =(triangle-UV-coord)= returns the the UV coordinates of the verticies | 329 =(triangle-UV-coord)= returns the the UV coordinates of the verticies |
296 of a triangle. | 330 of a triangle. |
297 | 331 |
298 #+name: triangles-1 | 332 #+name: triangles-1 |
299 #+begin_src clojure | 333 #+begin_src clojure |
334 (in-ns 'cortex.touch) | |
335 | |
336 (defn vector3f-seq [#^Vector3f v] | |
337 [(.getX v) (.getY v) (.getZ v)]) | |
338 | |
339 (defn triangle-seq [#^Triangle tri] | |
340 [(vector3f-seq (.get1 tri)) | |
341 (vector3f-seq (.get2 tri)) | |
342 (vector3f-seq (.get3 tri))]) | |
343 | |
344 (defn ->vector3f [[x y z]] (Vector3f. x y z)) | |
345 | |
346 (defn ->triangle [points] | |
347 (apply #(Triangle. %1 %2 %3) (map ->vector3f points))) | |
348 | |
349 (defn triangle | |
350 "Get the triangle specified by triangle-index from the mesh within | |
351 bounds." | |
352 [#^Geometry geo triangle-index] | |
353 (triangle-seq | |
354 (let [scratch (Triangle.)] | |
355 (.getTriangle (.getMesh geo) triangle-index scratch) scratch))) | |
356 | |
300 (defn triangles | 357 (defn triangles |
301 "Return a sequence of all the Triangles which compose a given | 358 "Return a sequence of all the Triangles which compose a given |
302 Geometry." | 359 Geometry." |
303 [#^Geometry geom] | 360 [#^Geometry geo] |
304 (let | 361 (map (partial triangle geo) (range (.getTriangleCount (.getMesh geo))))) |
305 [mesh (.getMesh geom) | |
306 triangles (transient [])] | |
307 (dorun | |
308 (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))) | |
315 | |
316 (defn mesh-triangle | |
317 "Get the triangle specified by triangle-index from the mesh within | |
318 bounds." | |
319 [#^Mesh mesh triangle-index] | |
320 (let [scratch (Triangle.)] | |
321 (.getTriangle mesh triangle-index scratch) | |
322 scratch)) | |
323 | 362 |
324 (defn triangle-vertex-indices | 363 (defn triangle-vertex-indices |
325 "Get the triangle vertex indices of a given triangle from a given | 364 "Get the triangle vertex indices of a given triangle from a given |
326 mesh." | 365 mesh." |
327 [#^Mesh mesh triangle-index] | 366 [#^Mesh mesh triangle-index] |
338 mesh | 377 mesh |
339 VertexBuffer$Type/TexCoord))] | 378 VertexBuffer$Type/TexCoord))] |
340 [(.get UV-buffer (* vertex-index 2)) | 379 [(.get UV-buffer (* vertex-index 2)) |
341 (.get UV-buffer (+ 1 (* vertex-index 2)))])) | 380 (.get UV-buffer (+ 1 (* vertex-index 2)))])) |
342 | 381 |
343 (defn triangle-UV-coord | 382 (defn pixel-triangle [#^Geometry geo image index] |
344 "Get the UV-cooridnates of the triangle's verticies." | 383 (let [mesh (.getMesh geo) |
345 [#^Mesh mesh width height triangle-index] | 384 width (.getWidth image) |
346 (map (fn [[u v]] (vector (* width u) (* height v))) | 385 height (.getHeight image)] |
347 (map (partial vertex-UV-coord mesh) | 386 (vec (map (fn [[u v]] (vector (* width u) (* height v))) |
348 (triangle-vertex-indices mesh triangle-index)))) | 387 (map (partial vertex-UV-coord mesh) |
349 #+end_src | 388 (triangle-vertex-indices mesh index)))))) |
350 | 389 |
351 * Schrapnel Conversion Functions | 390 (defn pixel-triangles [#^Geometry geo image] |
352 | 391 (let [height (.getHeight image) |
353 It is convienent to treat a =Triangle= as a sequence of verticies, and | 392 width (.getWidth image)] |
354 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion | 393 (map (partial pixel-triangle geo image) |
355 functions make this easy. If these classes implemented =Iterable= then | 394 (range (.getTriangleCount (.getMesh geo)))))) |
356 this code would not be necessary. Hopefully they will in the future. | 395 |
357 | |
358 #+name: triangles-2 | |
359 #+begin_src clojure | |
360 (defn triangle-seq [#^Triangle tri] | |
361 [(.get1 tri) (.get2 tri) (.get3 tri)]) | |
362 | |
363 (defn vector3f-seq [#^Vector3f v] | |
364 [(.getX v) (.getY v) (.getZ v)]) | |
365 | |
366 (defn point->vector2f [[u v]] | |
367 (Vector2f. u v)) | |
368 | |
369 (defn vector2f->vector3f [v] | |
370 (Vector3f. (.getX v) (.getY v) 0)) | |
371 | |
372 (defn map-triangle [f #^Triangle tri] | |
373 (Triangle. | |
374 (f 0 (.get1 tri)) | |
375 (f 1 (.get2 tri)) | |
376 (f 2 (.get3 tri)))) | |
377 | |
378 (defn points->triangle | |
379 "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_src | 396 #+end_src |
389 | 397 |
390 * Triangle Affine Transforms | 398 * Triangle Affine Transforms |
391 | 399 |
392 The position of each hair is stored in a 2D image in UV | 400 The position of each hair is stored in a 2D image in UV |
423 (.mult | 431 (.mult |
424 (triangle->matrix4f tri-2) | 432 (triangle->matrix4f tri-2) |
425 (.invert (triangle->matrix4f tri-1)))) | 433 (.invert (triangle->matrix4f tri-1)))) |
426 #+end_src | 434 #+end_src |
427 | 435 |
436 | |
437 * Schrapnel Conversion Functions | |
438 | |
439 It is convienent to treat a =Triangle= as a sequence of verticies, and | |
440 a =Vector2f= and =Vector3f= as a sequence of floats. These conversion | |
441 functions make this easy. If these classes implemented =Iterable= then | |
442 this code would not be necessary. Hopefully they will in the future. | |
443 | |
444 #+name: triangles-2 | |
445 #+begin_src clojure | |
446 (defn point->vector2f [[u v]] | |
447 (Vector2f. u v)) | |
448 | |
449 (defn vector2f->vector3f [v] | |
450 (Vector3f. (.getX v) (.getY v) 0)) | |
451 | |
452 (defn map-triangle [f #^Triangle tri] | |
453 (Triangle. | |
454 (f 0 (.get1 tri)) | |
455 (f 1 (.get2 tri)) | |
456 (f 2 (.get3 tri)))) | |
457 | |
458 (defn points->triangle | |
459 "Convert a list of points into a triangle." | |
460 [points] | |
461 (apply #(Triangle. %1 %2 %3) | |
462 (map (fn [point] | |
463 (let [point (vec point)] | |
464 (Vector3f. (get point 0 0) | |
465 (get point 1 0) | |
466 (get point 2 0)))) | |
467 (take 3 points)))) | |
468 #+end_src | |
469 | |
470 | |
428 * Triangle Boundaries | 471 * Triangle Boundaries |
429 | 472 |
430 For efficiency's sake I will divide the UV-image into small squares | 473 For efficiency's sake I will divide the UV-image into small squares |
431 which inscribe each UV-triangle, then extract the points which lie | 474 which inscribe each UV-triangle, then extract the points which lie |
432 inside the triangle and map them to 3D-space using | 475 inside the triangle and map them to 3D-space using |