comparison org/test-creature.org @ 96:4a9096f31017

working on touch
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Jan 2012 21:26:41 -0700
parents e4bcd0c481ba
children 2ff8c7c4e64d
comparison
equal deleted inserted replaced
95:e4bcd0c481ba 96:4a9096f31017
122 object (i.e. local coordinates), taking into account the rotation 122 object (i.e. local coordinates), taking into account the rotation
123 of object." 123 of object."
124 [#^Spatial object world-coordinate] 124 [#^Spatial object world-coordinate]
125 (let [out (Vector3f.)] 125 (let [out (Vector3f.)]
126 (.worldToLocal object world-coordinate out) out)) 126 (.worldToLocal object world-coordinate out) out))
127
128 (defn local-to-world
129 "Convert the local coordinates into coordinates into world relative
130 coordinates"
131 [#^Spatial object local-coordinate]
132 (let [world-coordinate (Vector3f.)]
133 (.localToWorld object local-coordinate world-coordinate)
134 world-coordinate))
135
127 136
128 (defmulti joint-dispatch 137 (defmulti joint-dispatch
129 "Translate blender pseudo-joints into real JME joints." 138 "Translate blender pseudo-joints into real JME joints."
130 (fn [constraints & _] 139 (fn [constraints & _]
131 (:type constraints))) 140 (:type constraints)))
504 (Vector3f. (* width (.getX v)) 513 (Vector3f. (* width (.getX v))
505 (* height (.getY v)) 514 (* height (.getY v))
506 0)) 515 0))
507 (uv-triangle mesh tri))) 516 (uv-triangle mesh tri)))
508 517
518 (def rasterize pixel-triangle)
519
520
509 (defn triangle-bounds 521 (defn triangle-bounds
510 "Dimensions of the bounding square of the triangle in the form 522 "Dimensions of the bounding square of the triangle in the form
511 [x y width height]. 523 [x y width height].
512 Assumes that the triangle lies in the XY plane." 524 Assumes that the triangle lies in the XY plane."
513 [#^Triangle tri] 525 [#^Triangle tri]
523 535
524 (defn locate-tactile-sensors 536 (defn locate-tactile-sensors
525 "Search the geometry's tactile UV image for touch sensors, returning 537 "Search the geometry's tactile UV image for touch sensors, returning
526 their positions in geometry-relative coordinates." 538 their positions in geometry-relative coordinates."
527 [#^Geometry geo] 539 [#^Geometry geo]
528
529 ;; inside-triangle? white-coordinates triangle-transformation
530 ;; tri-uv-coord touch-receptor-image
531 (let [mesh (.getMesh geo) 540 (let [mesh (.getMesh geo)
541 tris (triangles geo)
542
532 image (touch-receptor-image geo) 543 image (touch-receptor-image geo)
533 width (.getWidth image) 544 width (.getWidth image)
534 height (.getHeight image) 545 height (.getHeight image)
535 tris (triangles geo) 546
536 547
537 ;; for each triangle 548 ;; for each triangle
538 sensor-coords 549 sensor-coords
539 (fn [tri] 550 (fn [tri]
540 ;; translate triangle to uv-pixel-space 551 ;; translate triangle to uv-pixel-space
558 (map vector2f->vector3f 569 (map vector2f->vector3f
559 (white-coordinates cutout))) 570 (white-coordinates cutout)))
560 ;; translate pixel coordinates to world-space 571 ;; translate pixel coordinates to world-space
561 transform (triangle-transformation cutout-tri tri)] 572 transform (triangle-transformation cutout-tri tri)]
562 (map #(.mult transform %) whites))))] 573 (map #(.mult transform %) whites))))]
563 (map sensor-coords tris))) 574
564 575
565 576
577 (vec (map sensor-coords tris))))
578
579 (defn locate-tactile-sensors*
580 "Search the geometry's tactile UV image for touch sensors, returning
581 their positions in geometry-relative coordinates."
582 [#^Geometry geo]
583 (let [uv-image (touch-receptor-image geo)
584 width (.getWidth uv-image)
585 height (.getHeight uv-image)
586
587 mesh (.getMesh geo)
588 mesh-tris (triangles geo)
589
590 ;; for each triangle
591 sensor-coords
592 (fn [tri]
593 ;; translate triangle to uv-pixel-space
594 (let [uv-tri
595 (rasterize mesh tri width height)
596 bounds (vec (triangle-bounds uv-tri))]
566 597
567 598 ;; get that part of the picture
599
600 (apply (partial (memfn setRoi) uv-image) bounds)
601 (let [cutout (.crop (.getProcessor uv-image))
602 ;; extract white pixels inside triangle
603 cutout-tri
604 (map-triangle
605 (fn [_ v]
606 (.subtract
607 v
608 (Vector3f. (bounds 0) (bounds 1) (float 0))))
609 uv-tri)
610 whites (filter (partial inside-triangle? cutout-tri)
611 (map vector2f->vector3f
612 (white-coordinates cutout)))
613 ;; translate pixel coordinates to world-space
614 transform (triangle-transformation cutout-tri tri)]
615 (map #(.mult transform %) whites))))]
616
617
618
619 (for [mesh-tri mesh-tris]
620
621 (let [uv-tri (rasterize mesh mesh-tri width height)
622 bounding-box (vec (triangle-bounds uv-tri))]
623 (apply (partial (memfn setRoi) uv-image) bounding-box)
624
625
626
627
628 ))
629 (vec (map sensor-coords mesh-tris))))
630
631
632
633 (defn measure-touchies [#^Geometry geo]
634 (let [tactile-sensor-coords (locate-tactile-sensors geo)
635 tris (triangles geo)]
636 (fn [world]
637 (let [sensor-origins (vec
638 (map
639 #(map (partial local-to-world geo) %)
640 tactile-sensor-coords))
641 triangle-normals (vec
642 (map (partial get-ray-direction geo)
643 tris))
644 rays (flatten
645 (map
646 (fn [origins normals]
647 (map
648 #(Ray. %1 %2)
649 origins
650 normals))
651 sensor-origins
652 (map repeat triangle-normals)))
653
654
655 ]
656 rays))))
657
658
659
660
661
662
663
664
665
666
568 667
569 668
570 669
571 670
572 671