Mercurial > cortex
comparison org/test-creature.org @ 91:2bcc7636cfea
faster touch creation code
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 09 Jan 2012 06:02:06 -0700 |
parents | 6d7c17c847a3 |
children | e70ec4bba96b |
comparison
equal
deleted
inserted
replaced
90:6d7c17c847a3 | 91:2bcc7636cfea |
---|---|
305 ;; (if (= (rem @timer 60) 0) | 305 ;; (if (= (rem @timer 60) 0) |
306 ;; (println-repl (float (/ @timer 60)))))) | 306 ;; (println-repl (float (/ @timer 60)))))) |
307 ))) | 307 ))) |
308 | 308 |
309 | 309 |
310 (def colorful (.getChild (worm-model) "worm-21")) | 310 (defn colorful [] |
311 | 311 (.getChild (worm-model) "worm-21")) |
312 (def im-data | |
313 (let [sink (byte-array 3145727) | |
314 data (.getData (.getImage texture) 0)] | |
315 (.rewind data) | |
316 (.get data sink) | |
317 (vec (seq sink)))) | |
318 | |
319 | 312 |
320 (import jme3tools.converters.ImageToAwt) | 313 (import jme3tools.converters.ImageToAwt) |
321 | 314 |
322 (import ij.ImagePlus) | 315 (import ij.ImagePlus) |
323 | |
324 | |
325 | 316 |
326 (defn triangle-indices | 317 (defn triangle-indices |
327 "Get the triangle vertex indices of a given triangle from a given | 318 "Get the triangle vertex indices of a given triangle from a given |
328 mesh." | 319 mesh." |
329 [#^Mesh mesh triangle-index] | 320 [#^Mesh mesh triangle-index] |
337 (let [UV-buffer | 328 (let [UV-buffer |
338 (.getData | 329 (.getData |
339 (.getBuffer | 330 (.getBuffer |
340 mesh | 331 mesh |
341 VertexBuffer$Type/TexCoord))] | 332 VertexBuffer$Type/TexCoord))] |
342 [(.get UV-buffer (* vertex-index 2)) | 333 (Vector2f. |
343 (.get UV-buffer (+ 1 (* vertex-index 2)))])) | 334 (.get UV-buffer (* vertex-index 2)) |
344 | 335 (.get UV-buffer (+ 1 (* vertex-index 2)))))) |
345 (defn touch-receptor-image [#^Geometry obj] | 336 |
346 | 337 (defn touch-receptor-image |
347 | 338 "Return the touch-sensor distribution image in ImagePlus format." |
348 | 339 [#^Geometry obj] |
349 ;; yay there's a converter! | |
350 | |
351 (defn uv-image [] | |
352 (let | 340 (let |
353 [colorful (.getChild (worm-model) "worm-21") | 341 [mat (.getMaterial obj) |
354 mat (.getMaterial colorful) | |
355 texture | 342 texture |
356 (.getTextureValue | 343 (.getTextureValue |
357 (.getTextureParam | 344 (.getTextureParam |
358 mat | 345 mat |
359 MaterialHelper/TEXTURE_TYPE_DIFFUSE)) | 346 MaterialHelper/TEXTURE_TYPE_DIFFUSE)) |
360 im (.getImage texture)] | 347 im (.getImage texture)] |
361 (ImagePlus. | 348 (ImagePlus. |
362 "UV-map" | 349 "UV-map" |
363 (ImageToAwt/convert im false false 0)))) | 350 (ImageToAwt/convert im false false 0)))) |
364 | 351 |
352 | |
353 (import ij.process.ImageProcessor) | |
354 (import java.awt.image.BufferedImage) | |
355 | |
356 (defprotocol Frame | |
357 (frame [this])) | |
358 | |
359 (extend-type BufferedImage | |
360 Frame | |
361 (frame [image] | |
362 (merge | |
363 (apply | |
364 hash-map | |
365 (interleave | |
366 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))] | |
367 (vector x y))) | |
368 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))] | |
369 (let [data (.getRGB image x y)] | |
370 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16) | |
371 :g (bit-shift-right (bit-and 0x00ff00 data) 8) | |
372 :b (bit-and 0x0000ff data))))))) | |
373 {:width (.getWidth image) :height (.getHeight image)}))) | |
374 | |
375 | |
376 (extend-type ImagePlus | |
377 Frame | |
378 (frame [image+] | |
379 (frame (.getBufferedImage image+)))) | |
380 | |
381 (defn rgb->int [r g b] | |
382 (+ (bit-shift-left r 16) | |
383 (bit-shift-left g 8) | |
384 b)) | |
385 | |
386 | |
387 | |
388 (defn filter-pixels | |
389 "List the coordinates of all pixels matching pred." | |
390 [pred #^ImageProcessor ip] | |
391 (let | |
392 [width (.getWidth ip) | |
393 height (.getHeight ip)] | |
394 ((fn accumulate [x y matches] | |
395 (cond | |
396 (>= y height) matches | |
397 (>= x width) (recur 0 (inc y) matches) | |
398 (pred (.getPixel ip x y)) | |
399 (recur (inc x) y (conj matches (Vector2f. x y))) | |
400 :else (recur (inc x) y matches))) | |
401 0 0 []))) | |
402 | |
403 | |
404 | |
405 | |
406 | |
407 (defn filter-pixels* | |
408 [pred #^ImageProcessor ip] | |
409 (let | |
410 [width (.getWidth ip) | |
411 height (.getHeight ip) | |
412 coords (ref []) | |
413 process | |
414 (fn [[start end]] | |
415 (loop [i start] | |
416 (if (<= i end) | |
417 (do | |
418 (let [column (rem i height) | |
419 row (unchecked-divide i width)] | |
420 (if (pred (.getPixel ip row column)) | |
421 (dosync (ref-set | |
422 coords | |
423 (conj @coords (Vector2f. column row))))) | |
424 | |
425 (recur (inc i))))))) | |
426 ] | |
427 | |
428 | |
429 (dorun | |
430 (pmap process (partition | |
431 2 | |
432 (conj (vec (range 0 (* width height) 100)) | |
433 (* width height))))) | |
434 @coords)) | |
435 | |
436 | |
437 | |
438 (comment | |
439 ((-> | |
440 f | |
441 (partial x) | |
442 (partial y) | |
443 (partial z)))) | |
444 | |
445 (defn filter-pixels** | |
446 [pred #^ImageProcessor ip] | |
447 (let [width (.getWidth ip) | |
448 height (.getHeight ip)] | |
449 ((fn f [x1 x2 y1 y2] | |
450 (println x1) | |
451 (if | |
452 (and | |
453 (= x1 (dec x2)) | |
454 (= y1 (dec y2))) | |
455 (if (pred (.getPixel ip x1 y1)) | |
456 [[x1 y1]] | |
457 []) | |
458 (let | |
459 [xm (+ x1 (/ (- x2 x1) 2)) | |
460 ym (+ y1 (/ (- y2 y1) 2))] | |
461 (apply concat | |
462 (pvalues | |
463 ;;(f x1 xm y1 ym) | |
464 ;;(f xm x2 y1 ym) | |
465 ;;(f x1 xm ym y2) | |
466 (f xm x2 ym y2)))))) | |
467 0 width 0 height))) | |
468 | |
469 | |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 (defn white-coordinates* | |
477 [#^ImageProcessor ip] | |
478 (filter-pixels** #(== % -1) ip)) | |
479 | |
480 | |
481 (defn white-coordinates | |
482 "List the coordinates of all the white pixels in an image." | |
483 [#^ImageProcessor ip] | |
484 (let [height (.getHeight ip) | |
485 width (.getWidth ip) | |
486 coords (transient [])] | |
487 (dorun | |
488 (for [x (range width) | |
489 y (range height)] | |
490 (let [pixel (.getPixel ip x y)] | |
491 (if (= pixel -1) | |
492 (conj! coords (Vector2f. x y)))))) | |
493 (persistent! coords))) | |
494 | |
495 | |
496 | |
497 | |
498 | |
499 | |
500 | |
501 | |
502 (def white {:r 255, :g 255, :b 255}) | |
503 (def black {:r 0, :g 0, :b 0}) | |
504 | |
505 | |
506 (defn same-side? [p1 p2 ref p] | |
507 (<= | |
508 0 | |
509 (.dot | |
510 (.cross (.subtract p2 p1) (.subtract p p1)) | |
511 (.cross (.subtract p2 p1) (.subtract ref p1))))) | |
512 | |
513 (defn inside-triangle? | |
514 [vert-1 vert-2 vert-3 p] | |
515 (and | |
516 (same-side? vert-1 vert-2 vert-3 p) | |
517 (same-side? vert-2 vert-3 vert-1 p) | |
518 (same-side? vert-3 vert-1 vert-2 p))) | |
519 | |
520 | |
521 (defn white? [color] | |
522 (and | |
523 (= (:r color) 255) | |
524 (= (:b color) 255) | |
525 (= (:g color) 255))) | |
526 | |
527 | |
365 ;; for each triangle in the mesh, | 528 ;; for each triangle in the mesh, |
366 ;; get the normal to the triangle, | 529 ;; get the normal to the triangle, |
367 ;; look at the UV touch map, restricted to that triangle, | 530 ;; look at the UV touch map, restricted to that triangle, |
368 ;; get the positions of those touch sensors in geometry-relative | 531 ;; get the positions of those touch sensors in geometry-relative |
369 ;; coordinates. | 532 ;; coordinates. |
533 (defn tactile-coords [#^Geometry obj] | |
534 (let [mesh (.getMesh obj) | |
535 num-triangles (.getTriangleCount mesh) | |
536 num-verticies (.getVertexCount mesh) | |
537 uv-coord (partial uv-coord mesh) | |
538 triangle-indices (partial triangle-indices mesh) | |
539 receptors (touch-receptor-image obj) | |
540 ] | |
541 (map | |
542 (fn [[tri-1 tri-2 tri-3]] | |
543 (let [width (.getWidth receptors) | |
544 height (.getHeight receptors) | |
545 uv-1 (uv-coord tri-1) | |
546 uv-2 (uv-coord tri-2) | |
547 uv-3 (uv-coord tri-3) | |
548 x-coords (map #(.getX %) [uv-1 uv-2 uv-3]) | |
549 y-coords (map #(.getY %) [uv-1 uv-2 uv-3]) | |
550 max-x (Math/ceil (* width (apply max x-coords))) | |
551 min-x (Math/floor (* width (apply min x-coords))) | |
552 max-y (Math/ceil (* height (apply max y-coords))) | |
553 min-y (Math/floor (* height (apply min y-coords))) | |
554 | |
555 image-1 (Vector2f. (* width (.getX uv-1)) | |
556 (* height (.getY uv-1))) | |
557 image-2 (Vector2f. (* width (.getX uv-2)) | |
558 (* height (.getY uv-2))) | |
559 image-3 (Vector2f. (* width (.getX uv-3)) | |
560 (* height (.getY uv-3))) | |
561 left-corner | |
562 (Vector2f. min-x min-y) | |
563 | |
564 ] | |
565 | |
566 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y)) | |
567 (let [processor (.crop (.getProcessor receptors)) | |
568 image (frame (.getBufferedImage processor))] | |
569 (with-meta | |
570 (filter-keys | |
571 (fn [[x y]] | |
572 (inside-triangle? | |
573 (.subtract image-1 left-corner) | |
574 (.subtract image-2 left-corner) | |
575 (.subtract image-3 left-corner) | |
576 (Vector2f. x y))) | |
577 | |
578 | |
579 (filter-vals white? image)) | |
580 {:image | |
581 (comment | |
582 (.getBufferedImage | |
583 (doto processor | |
584 (.flipVertical)))) | |
585 } | |
586 )) | |
587 )) (map triangle-indices (range num-triangles))))) | |
588 | |
589 | |
590 | |
591 | |
592 | |
593 | |
594 | |
595 (defn all-names [] | |
596 (concat | |
597 (re-split #"\n" (slurp (file-str | |
598 "/home/r/proj/names/dist.female.first"))) | |
599 (re-split #"\n" (slurp (file-str | |
600 "/home/r/proj/names/dist.male.first"))) | |
601 (re-split #"\n" (slurp (file-str | |
602 "/home/r/proj/names/dist.all.last"))))) | |
370 | 603 |
371 | 604 |
372 | 605 |
373 | 606 |
374 | 607 |
379 (defrecord LulzLoader []) | 612 (defrecord LulzLoader []) |
380 (defprotocol Lulzable (load-lulz [this])) | 613 (defprotocol Lulzable (load-lulz [this])) |
381 (extend-type LulzLoader | 614 (extend-type LulzLoader |
382 Lulzable | 615 Lulzable |
383 (load-lulz [this] (println "the lulz have arrived!"))) | 616 (load-lulz [this] (println "the lulz have arrived!"))) |
384 | |
385 (defn | |
386 | |
387 | 617 |
388 | 618 |
389 (defn world-setup [joint] | 619 (defn world-setup [joint] |
390 (let [joint-position (Vector3f. 0 0 0) | 620 (let [joint-position (Vector3f. 0 0 0) |
391 joint-rotation | 621 joint-rotation |