comparison org/test-creature.org @ 94:69174ed0f9f6

Working sensor coordinate code. Dylan helped!
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Jan 2012 08:38:04 -0700
parents 7b739503836a
children e4bcd0c481ba
comparison
equal deleted inserted replaced
93:7b739503836a 94:69174ed0f9f6
382 (extend-type ImagePlus 382 (extend-type ImagePlus
383 Frame 383 Frame
384 (frame [image+] 384 (frame [image+]
385 (frame (.getBufferedImage image+)))) 385 (frame (.getBufferedImage image+))))
386 386
387
387 (def white -1) 388 (def white -1)
388 389
389 (defn filter-pixels 390 (defn filter-pixels
390 "List the coordinates of all pixels matching pred." 391 "List the coordinates of all pixels matching pred."
391 {:author "Dylan Holmes"} 392 {:author "Dylan Holmes"}
392 [pred #^ImageProcessor ip] 393 [pred #^ImageProcessor ip]
393 (let 394 (let
412 0 413 0
413 (.dot 414 (.dot
414 (.cross (.subtract p2 p1) (.subtract p p1)) 415 (.cross (.subtract p2 p1) (.subtract p p1))
415 (.cross (.subtract p2 p1) (.subtract ref p1))))) 416 (.cross (.subtract p2 p1) (.subtract ref p1)))))
416 417
418
419 (defn triangle->matrix4f
420 "Converts the triangle into a 4x4 matrix of vertices: The first
421 three columns contain the vertices of the triangle; the last
422 contains the unit normal of the triangle. The bottom row is filled
423 with 1s."
424 [#^Triangle t]
425 (let [mat (Matrix4f.)
426 [vert-1 vert-2 vert-3]
427 ((comp vec map) #(.get t %) (range 3))
428 unit-normal (do (.calculateNormal t)(.getNormal t))
429 vertices [vert-1 vert-2 vert-3 unit-normal]]
430
431 (dorun
432 (for [row (range 4) col (range 3)]
433 (do
434 (.set mat col row (.get (vertices row)col))
435 (.set mat 3 row 1))))
436 mat))
437
438 (defn triangle-transformation
439 "Returns the affine transformation that converts each vertex in the
440 first triangle into the corresponding vertex in the second
441 triangle."
442 [#^Triangle tri-1 #^Triangle tri-2]
443 (.mult
444 (triangle->matrix4f tri-2)
445 (.invert (triangle->matrix4f tri-1))))
446
447 (def death (Triangle.
448 (Vector3f. 1 1 1)
449 (Vector3f. 1 2 3)
450 (Vector3f. 5 6 7)))
451
452 (def death-2 (Triangle.
453 (Vector3f. 2 2 2)
454 (Vector3f. 1 1 1)
455 (Vector3f. 0 1 0)))
456
457 (defn vector2f->vector3f [v]
458 (Vector3f. (.getX v) (.getY v) 0))
459
460
461 (extend-type Triangle
462 Textual
463 (text [t]
464 (println "Triangle: " \newline (.get1 t) \newline
465 (.get2 t) \newline (.get3 t))))
466
467
468 (defn map-triangle [f #^Triangle tri]
469 (Triangle.
470 (f 0 (.get1 tri))
471 (f 1 (.get2 tri))
472 (f 2 (.get3 tri))))
473
474 (defn triangle-seq [#^Triangle tri]
475 [(.get1 tri) (.get2 tri) (.get3 tri)])
476
477 (defn vector3f-seq [#^Vector3f v]
478 [(.getX v) (.getY v) (.getZ v)])
479
417 (defn inside-triangle? 480 (defn inside-triangle?
418 [vert-1 vert-2 vert-3 p] 481 "Is the point inside the triangle? Now what do we do?
419 (and 482 You might want to hold on there"
420 (same-side? vert-1 vert-2 vert-3 p) 483 {:author "God"}
421 (same-side? vert-2 vert-3 vert-1 p) 484 [tri p]
422 (same-side? vert-3 vert-1 vert-2 p))) 485 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
423 486 (and
424 487 (same-side? vert-1 vert-2 vert-3 p)
425 (defn analyze-triangle [#^Geometry obj #^Triangle tri] 488 (same-side? vert-2 vert-3 vert-1 p)
426 ;; first, calculate the transformation matrix that will take us 489 (same-side? vert-3 vert-1 vert-2 p))))
427 ;; from uv-coordinates to the real world coordinates 490
428 (let [mesh (.getMesh obj) 491 (defn uv-triangle
429 world [(.get1 tri) (.get2 tri) (.get3 tri)] 492 "Convert the mesh triangle into the cooresponding triangle in
430 uv (tri-uv-coord mesh tri) 493 UV-space. Z-component of these triangles is always zero."
431 494 [#^Mesh mesh #^Triangle tri]
432 495 (apply #(Triangle. %1 %2 %3)
433 496 (map vector2f->vector3f
434 497 (tri-uv-coord mesh tri))))
435 (println-repl world uv))) 498
499 (defn pixel-triangle
500 "Convert the mesh triange into the corresponding triangle in
501 UV-pixel-space. Z compenent will be zero."
502 [#^Mesh mesh #^Triangle tri width height]
503 (map-triangle (fn [_ v]
504 (Vector3f. (* width (.getX v))
505 (* height (.getY v))
506 0))
507 (uv-triangle mesh tri)))
508
509 (defn triangle-bounds
510 "Dimensions of the bounding square of the triangle in the form
511 [x y width height].
512 Assumes that the triangle lies in the XY plane."
513 [#^Triangle tri]
514 (let [verts (map vector3f-seq (triangle-seq tri))
515 x (apply min (map first verts))
516 y (apply min (map second verts))]
517
518 [x y
519 (- (apply max (map first verts)) x)
520 (- (apply max (map second verts)) y)
521 ]))
436 522
437 523
438 524 (defn locate-tactile-sensors
439 525 "Search the geometry's tactile UV image for touch sensors, returning
440 526 their positions in geometry-relative coordinates."
441 (defn tactile-coords* 527 [#^Geometry geo]
442 [#^Geometry obj] 528
443 (let 529 ;; inside-triangle? white-coordinates triangle-transformation
444 [tris (triangles obj) 530 ;; tri-uv-coord touch-receptor-image
445 mesh (.getMesh obj) 531 (let [mesh (.getMesh geo)
446 532 image (touch-receptor-image geo)
447 533 width (.getWidth image)
448 ) 534 height (.getHeight image)
535 tris (triangles geo)
536
537 ;; for each triangle
538 sensor-coords
539 (fn [tri]
540 ;; translate triangle to uv-pixel-space
541 (let [uv-tri
542 (pixel-triangle mesh tri width height)
543 bounds (vec (triangle-bounds uv-tri))]
544
545 ;; get that part of the picture
546
547 (apply #(.setRoi image %1 %2 %3 %4) bounds)
548 (let [cutout (.crop (.getProcessor image))
549 ;; extract white pixels inside triangle
550 cutout-tri
551 (map-triangle
552 (fn [_ v]
553 (.subtract
554 v
555 (Vector3f. (bounds 0) (bounds 1) (float 0))))
556 uv-tri)
557 whites (filter (partial inside-triangle? cutout-tri)
558 (map vector2f->vector3f
559 (white-coordinates cutout)))
560 ;; translate pixel coordinates to world-space
561 transform (triangle-transformation cutout-tri tri)]
562 (map #(.mult transform %) whites))))]
563 (map sensor-coords tris)))
564
565
566
567
568
569
570
571
572
573
449 574
450 575
451 576
452 ;; for each triangle in the mesh, 577 ;; for each triangle in the mesh,
453 ;; get the normal to the triangle, 578 ;; get the normal to the triangle,
483 (* height (.getY uv-2))) 608 (* height (.getY uv-2)))
484 image-3 (Vector2f. (* width (.getX uv-3)) 609 image-3 (Vector2f. (* width (.getX uv-3))
485 (* height (.getY uv-3))) 610 (* height (.getY uv-3)))
486 left-corner 611 left-corner
487 (Vector2f. min-x min-y) 612 (Vector2f. min-x min-y)
488
489 ] 613 ]
490 614
491 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y)) 615 (.setRoi receptors min-x min-y (- max-x min-x) (- max-y min-y))
492 (let [processor (.crop (.getProcessor receptors))] 616 (let [processor (.crop (.getProcessor receptors))]
493 (map 617 (map
789 * COMMENT generate source 913 * COMMENT generate source
790 #+begin_src clojure :tangle ../src/cortex/silly.clj 914 #+begin_src clojure :tangle ../src/cortex/silly.clj
791 <<body-1>> 915 <<body-1>>
792 #+end_src 916 #+end_src
793 917
918
919
920
921
922 (defn transform-trianglesdsd
923 "Transform that converts each vertex in the first triangle
924 into the corresponding vertex in the second triangle."
925 [#^Triangle tri-1 #^Triangle tri-2]
926 (let [in [(.get1 tri-1)
927 (.get2 tri-1)
928 (.get3 tri-1)]
929 out [(.get1 tri-2)
930 (.get2 tri-2)
931 (.get3 tri-2)]]
932 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
933 in* [(.mult translate (in 0))
934 (.mult translate (in 1))
935 (.mult translate (in 2))]
936 final-translation
937 (doto (Matrix4f.)
938 (.setTranslation (out 1)))
939
940 rotate-1
941 (doto (Matrix3f.)
942 (.fromStartEndVectors
943 (.normalize
944 (.subtract
945 (in* 1) (in* 0)))
946 (.normalize
947 (.subtract
948 (out 1) (out 0)))))
949 in** [(.mult rotate-1 (in* 0))
950 (.mult rotate-1 (in* 1))
951 (.mult rotate-1 (in* 2))]
952 scale-factor-1
953 (.mult
954 (.normalize
955 (.subtract
956 (out 1)
957 (out 0)))
958 (/ (.length
959 (.subtract (out 1)
960 (out 0)))
961 (.length
962 (.subtract (in** 1)
963 (in** 0)))))
964 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
965 in*** [(.mult scale-1 (in** 0))
966 (.mult scale-1 (in** 1))
967 (.mult scale-1 (in** 2))]
968
969
970
971
972
973 ]
974
975 (dorun (map println in))
976 (println)
977 (dorun (map println in*))
978 (println)
979 (dorun (map println in**))
980 (println)
981 (dorun (map println in***))
982 (println)
983
984 )))
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003 )
1004
1005