comparison org/test-creature.org @ 102:7eeb940bcbc8

data format for touch implemented
author Robert McIntyre <rlm@mit.edu>
date Sat, 14 Jan 2012 20:44:13 -0700
parents 65332841b7d9
children 85ee8bb80edf
comparison
equal deleted inserted replaced
101:65332841b7d9 102:7eeb940bcbc8
95 (.repaint panel 0 0 (.getWidth i) (.getHeight i))))) 95 (.repaint panel 0 0 (.getWidth i) (.getHeight i)))))
96 96
97 (defn points->image 97 (defn points->image
98 "Take a sparse collection of points and visuliaze it as a 98 "Take a sparse collection of points and visuliaze it as a
99 BufferedImage." 99 BufferedImage."
100
101 ;; TODO maybe parallelize this since it's easy
102
100 [points] 103 [points]
101 (let [xs (vec (map first points)) 104 (let [xs (vec (map first points))
102 ys (vec (map second points)) 105 ys (vec (map second points))
103 x0 (apply min xs) 106 x0 (apply min xs)
104 y0 (apply min ys) 107 y0 (apply min ys)
110 (for [index (range (count points))] 113 (for [index (range (count points))]
111 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1))) 114 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
112 115
113 image)) 116 image))
114 117
115
116
117
118 ;;(defn visualize [points]
119
120 (defn test-data 118 (defn test-data
121 [] 119 []
122 (vec 120 (vec
123 (for [a (range 0 100 2) 121 (for [a (range 0 1000 2)
124 b (range 0 100 2)] 122 b (range 0 1000 2)]
125 (vector a b)) 123 (vector a b))
126 )) 124 ))
127 125
128 (defn average [coll] 126 (defn average [coll]
129 (/ (reduce + coll) (count coll))) 127 (/ (reduce + coll) (count coll)))
143 contigous bitmap." 141 contigous bitmap."
144 [points] 142 [points]
145 (let 143 (let
146 [num-points (count points) 144 [num-points (count points)
147 center (vector 145 center (vector
148 (average (map first points)) 146 (int (average (map first points)))
149 (average (map first points))) 147 (int (average (map first points))))
150 flattened 148 flattened
151 (reduce 149 (reduce
152 concat 150 concat
153 (map 151 (map
154 (fn [column] 152 (fn [column]
164 (fn [row] 162 (fn [row]
165 (map vector 163 (map vector
166 (collapse-1d (first center) 164 (collapse-1d (first center)
167 (map first row)) 165 (map first row))
168 (map second row))) 166 (map second row)))
169 (partition-by second (sort-by second flattened))))] 167 (partition-by second (sort-by second flattened))))
170 squeezed 168 ;;vi (view-image)
169 relocate
170 (let [min-x (apply min (map first squeezed))
171 min-y (apply min (map second squeezed))]
172 (map (fn [[x y]]
173 [(- x min-x)
174 (- y min-y)])
175 squeezed))
176 ]
177 ;;(vi (points->image points))
178 ;;(Thread/sleep 1000)
179 ;;(vi (points->image flattened))
180 ;;(Thread/sleep 1000)
181 ;;(vi (points->image squeezed))
182 relocate
171 )) 183 ))
172
173
174 184
175 (defn load-bullet [] 185 (defn load-bullet []
176 (let [sim (world (Node.) {} no-op no-op)] 186 (let [sim (world (Node.) {} no-op no-op)]
177 (.enqueue 187 (doto sim
178 sim 188 (.enqueue
179 (fn [] 189 (fn []
180 (.stop sim))) 190 (.stop sim)))
181 (.start sim))) 191 (.start))))
182 192
183 (defn load-blender-model 193 (defn load-blender-model
184 "Load a .blend file using an asset folder relative path." 194 "Load a .blend file using an asset folder relative path."
185 [^String model] 195 [^String model]
186 (.loadModel 196 (.loadModel
534 (defn white-coordinates 544 (defn white-coordinates
535 "List the coordinates of all the white pixels in an image." 545 "List the coordinates of all the white pixels in an image."
536 [#^ImageProcessor ip] 546 [#^ImageProcessor ip]
537 (filter-pixels #(= % white) ip)) 547 (filter-pixels #(= % white) ip))
538 548
539 (defn same-side? [p1 p2 ref p] 549 (defn same-side?
550 "Given the points p1 and p2 and the reference point ref, is point p
551 on the same side of the line that goes through p1 and p2 as ref is?"
552 [p1 p2 ref p]
540 (<= 553 (<=
541 0 554 0
542 (.dot 555 (.dot
543 (.cross (.subtract p2 p1) (.subtract p p1)) 556 (.cross (.subtract p2 p1) (.subtract p p1))
544 (.cross (.subtract p2 p1) (.subtract ref p1))))) 557 (.cross (.subtract p2 p1) (.subtract ref p1)))))
545
546 558
547 (defn triangle->matrix4f 559 (defn triangle->matrix4f
548 "Converts the triangle into a 4x4 matrix of vertices: The first 560 "Converts the triangle into a 4x4 matrix of vertices: The first
549 three columns contain the vertices of the triangle; the last 561 three columns contain the vertices of the triangle; the last
550 contains the unit normal of the triangle. The bottom row is filled 562 contains the unit normal of the triangle. The bottom row is filled
553 (let [mat (Matrix4f.) 565 (let [mat (Matrix4f.)
554 [vert-1 vert-2 vert-3] 566 [vert-1 vert-2 vert-3]
555 ((comp vec map) #(.get t %) (range 3)) 567 ((comp vec map) #(.get t %) (range 3))
556 unit-normal (do (.calculateNormal t)(.getNormal t)) 568 unit-normal (do (.calculateNormal t)(.getNormal t))
557 vertices [vert-1 vert-2 vert-3 unit-normal]] 569 vertices [vert-1 vert-2 vert-3 unit-normal]]
558
559 (dorun 570 (dorun
560 (for [row (range 4) col (range 3)] 571 (for [row (range 4) col (range 3)]
561 (do 572 (do
562 (.set mat col row (.get (vertices row)col)) 573 (.set mat col row (.get (vertices row)col))
563 (.set mat 3 row 1)))) 574 (.set mat 3 row 1))))
583 (Vector3f. 0 1 0))) 594 (Vector3f. 0 1 0)))
584 595
585 (defn vector2f->vector3f [v] 596 (defn vector2f->vector3f [v]
586 (Vector3f. (.getX v) (.getY v) 0)) 597 (Vector3f. (.getX v) (.getY v) 0))
587 598
588
589 (extend-type Triangle 599 (extend-type Triangle
590 Textual 600 Textual
591 (text [t] 601 (text [t]
592 (println "Triangle: " \newline (.get1 t) \newline 602 (println "Triangle: " \newline (.get1 t) \newline
593 (.get2 t) \newline (.get3 t)))) 603 (.get2 t) \newline (.get3 t))))
594
595 604
596 (defn map-triangle [f #^Triangle tri] 605 (defn map-triangle [f #^Triangle tri]
597 (Triangle. 606 (Triangle.
598 (f 0 (.get1 tri)) 607 (f 0 (.get1 tri))
599 (f 1 (.get2 tri)) 608 (f 1 (.get2 tri))
623 (apply #(Triangle. %1 %2 %3) 632 (apply #(Triangle. %1 %2 %3)
624 (map vector2f->vector3f 633 (map vector2f->vector3f
625 (tri-uv-coord mesh tri)))) 634 (tri-uv-coord mesh tri))))
626 635
627 (defn pixel-triangle 636 (defn pixel-triangle
628 "Convert the mesh triange into the corresponding triangle in 637 "Convert the mesh triangle into the corresponding triangle in
629 UV-pixel-space. Z compenent will be zero." 638 UV-pixel-space. Z compenent will be zero."
630 [#^Mesh mesh #^Triangle tri width height] 639 [#^Mesh mesh #^Triangle tri width height]
631 (map-triangle (fn [_ v] 640 (map-triangle (fn [_ v]
632 (Vector3f. (* width (.getX v)) 641 (Vector3f. (* width (.getX v))
633 (* height (.getY v)) 642 (* height (.getY v))
643 Assumes that the triangle lies in the XY plane." 652 Assumes that the triangle lies in the XY plane."
644 [#^Triangle tri] 653 [#^Triangle tri]
645 (let [verts (map vector3f-seq (triangle-seq tri)) 654 (let [verts (map vector3f-seq (triangle-seq tri))
646 x (apply min (map first verts)) 655 x (apply min (map first verts))
647 y (apply min (map second verts))] 656 y (apply min (map second verts))]
648
649 [x y 657 [x y
650 (- (apply max (map first verts)) x) 658 (- (apply max (map first verts)) x)
651 (- (apply max (map second verts)) y) 659 (- (apply max (map second verts)) y)
652 ])) 660 ]))
653 661
657 their positions in geometry-relative coordinates." 665 their positions in geometry-relative coordinates."
658 [#^Geometry geo] 666 [#^Geometry geo]
659 (if-let [image (touch-receptor-image geo)] 667 (if-let [image (touch-receptor-image geo)]
660 (let [mesh (.getMesh geo) 668 (let [mesh (.getMesh geo)
661 tris (triangles geo) 669 tris (triangles geo)
662 670
663
664 width (.getWidth image) 671 width (.getWidth image)
665 height (.getHeight image) 672 height (.getHeight image)
666 673
667 ;; for each triangle 674 ;; for each triangle
668 sensor-coords 675 sensor-coords
690 ;; translate pixel coordinates to world-space 697 ;; translate pixel coordinates to world-space
691 transform (triangle-transformation cutout-tri tri)] 698 transform (triangle-transformation cutout-tri tri)]
692 (map #(.mult transform %) whites))))] 699 (map #(.mult transform %) whites))))]
693 (vec (map sensor-coords tris))) 700 (vec (map sensor-coords tris)))
694 (repeat (count (triangles geo)) []))) 701 (repeat (count (triangles geo)) [])))
695 702
703 (use 'clojure.contrib.def)
704
705 (defn-memo touch-topology [#^Gemoetry geo]
706 (let [feeler-coords
707 (map
708 #(vector (int (.getX %)) (int (.getY %)))
709 (white-coordinates
710 (.getProcessor (touch-receptor-image (colorful)))))]
711 (vec (collapse feeler-coords))))
712
696 (defn enable-touch [#^Geometry geo] 713 (defn enable-touch [#^Geometry geo]
697 (let [feeler-coords (locate-feelers geo) 714 (let [feeler-coords (locate-feelers geo)
698 tris (triangles geo) 715 tris (triangles geo)
699 limit 0.1] 716 limit 0.1]
700 (fn [node] 717 (fn [node]
709 (flatten 726 (flatten
710 (map (fn [origins norm] 727 (map (fn [origins norm]
711 (map #(doto (Ray. % norm) 728 (map #(doto (Ray. % norm)
712 (.setLimit limit)) origins)) 729 (.setLimit limit)) origins))
713 sensor-origins triangle-normals))] 730 sensor-origins triangle-normals))]
714 (for [ray rays] 731 (vector
715 (do 732 (touch-topology geo)
716 (let [results (CollisionResults.)] 733 (vec
717 (.collideWith node ray results) 734 (for [ray rays]
718 (let [touch-objects 735 (do
719 (set 736 (let [results (CollisionResults.)]
720 (filter #(not (= geo %)) 737 (.collideWith node ray results)
721 (map #(.getGeometry %) results)))] 738 (let [touch-objects
722 (if (> (count touch-objects) 0) 739 (set
723 1 0))))))))) 740 (filter #(not (= geo %))
741 (map #(.getGeometry %) results)))]
742 (if (> (count touch-objects) 0)
743 1 0)))))))))))
724 744
725 (defn touch [#^Node pieces] 745 (defn touch [#^Node pieces]
726 (let [touch-components 746 (map enable-touch
727 (map enable-touch 747 (filter #(isa? (class %) Geometry)
728 (filter #(isa? (class %) Geometry) 748 (node-seq pieces))))
729 (node-seq pieces)))] 749
730 (fn [node]
731 (reduce into [] (map #(% node) touch-components)))))
732 750
733 (defn all-names [] 751 (defn all-names []
734 (concat 752 (concat
735 (re-split #"\n" (slurp (file-str 753 (re-split #"\n" (slurp (file-str
736 "/home/r/proj/names/dist.female.first"))) 754 "/home/r/proj/names/dist.female.first")))
908 (map vector2f->vector3f 926 (map vector2f->vector3f
909 (white-coordinates cutout))) 927 (white-coordinates cutout)))
910 ;; translate pixel coordinates to world-space 928 ;; translate pixel coordinates to world-space
911 transform (triangle-transformation cutout-tri tri)] 929 transform (triangle-transformation cutout-tri tri)]
912 (map #(.mult transform %) whites))))] 930 (map #(.mult transform %) whites))))]
913 931
914
915
916 (for [mesh-tri mesh-tris] 932 (for [mesh-tri mesh-tris]
917 933
918 (let [uv-tri (rasterize mesh mesh-tri width height) 934 (let [uv-tri (rasterize mesh mesh-tri width height)
919 bounding-box (vec (triangle-bounds uv-tri))] 935 bounding-box (vec (triangle-bounds uv-tri))]
920 (apply (partial (memfn setRoi) uv-image) bounding-box) 936 (apply (partial (memfn setRoi) uv-image) bounding-box)