comparison org/test-creature.org @ 118:1261444da2c7

got rough vision pipeline working
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Jan 2012 01:08:35 -0700
parents 94c005f7f9dd
children ebfd62779ab4
comparison
equal deleted inserted replaced
117:94c005f7f9dd 118:1261444da2c7
81 x0 (apply min xs) 81 x0 (apply min xs)
82 y0 (apply min ys) 82 y0 (apply min ys)
83 width (- (apply max xs) x0) 83 width (- (apply max xs) x0)
84 height (- (apply max ys) y0) 84 height (- (apply max ys) y0)
85 image (BufferedImage. (inc width) (inc height) 85 image (BufferedImage. (inc width) (inc height)
86 BufferedImage/TYPE_BYTE_BINARY)] 86 BufferedImage/TYPE_4BYTE_ABGR)]
87 (dorun
88 (for [x (range (.getWidth image))
89 y (range (.getHeight image))]
90 (.setRGB image x y 0xFFFF0000)))
87 (dorun 91 (dorun
88 (for [index (range (count points))] 92 (for [index (range (count points))]
89 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1))) 93 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
90 94
91 image))) 95 image)))
734 (.setRotation cam 738 (.setRotation cam
735 (.mult total-rotation initial-cam-rotation)))) 739 (.mult total-rotation initial-cam-rotation))))
736 (controlRender [_ _]))))) 740 (controlRender [_ _])))))
737 741
738 742
739 (defn attach-eyes 743 (defn attach-eye
740 "For each eye in the creature, attach a Camera to the appropiate 744 "Attach a Camera to the appropiate area and return the Camera."
741 area and return the Camera." 745 [#^Node creature #^Spatial eye]
742 [#^Node creature] 746
743 (for [eye (creature-eyes creature)] 747 (let [target (eye-target creature eye)
744 (let [target (eye-target creature eye) 748 [cam-width cam-height] (eye-dimensions eye)
745 [cam-width cam-height] (eye-dimensions eye) 749 cam (Camera. cam-width cam-height)]
746 cam (Camera. cam-width cam-height)] 750 (.setLocation cam (.getWorldTranslation eye))
747 (.setLocation cam (.getWorldTranslation eye)) 751 (.setRotation cam (.getWorldRotation eye))
748 (.setRotation cam (.getWorldRotation eye)) 752 (bind-camera target cam)
749 753 cam))
750 ) 754
751 755
752 756 (def presets
753 )) 757 {:gray identity})
754 758
759 (defn enable-vision
760 "return [init-function sensor-functions] for a particular eye"
761 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
762 (let [retinal-map (retina-sensor-image eye)
763 vision-image (atom nil)
764 camera (attach-eye creature eye)]
765 [
766 (fn [world]
767 (add-eye
768 world camera
769 (let [counter (atom 0)]
770 (fn [r fb bb bi]
771 (if (zero? (rem (swap! counter inc) (inc skip)))
772 (reset! vision-image (BufferedImage! r fb bb bi)))))))
773 (vector
774 (let [whites (white-coordinates (:gray retinal-map))
775 topology (vec (collapse whites))]
776 (fn []
777 (vector
778 topology
779 (vec
780 (for [[x y] whites]
781 (.getRGB @vision-image x y)))))))
782 ]))
783
755 (defn vision 784 (defn vision
756 785
757 ;; need to create a camera based on uv image, 786 ;; need to create a camera based on UV image,
758 ;; update this camera every frame based on the position of this 787 ;; update this camera every frame based on the position of this
759 ;; geometry. (maybe can get cam to follow the object) 788 ;; geometry. (maybe can get cam to follow the object)
760 789
761 ;; use a stack for the continuation to grab the image. 790 ;; use a stack for the continuation to grab the image.
762 791
796 (.setRGB image ((coords i) 0) ((coords i) 1) 825 (.setRGB image ((coords i) 0) ((coords i) 1)
797 ({0 -16777216 826 ({0 -16777216
798 1 -1} (sensor-data i))))) 827 1 -1} (sensor-data i)))))
799 (vi image))))) 828 (vi image)))))
800 829
830 (defn debug-vision-window
831 "creates function that offers a debug view of sensor data"
832 []
833 (let [vi (view-image)]
834 (fn
835 [[coords sensor-data]]
836 (let [image (points->image coords)]
837 (dorun
838 (for [i (range (count coords))]
839 (.setRGB image ((coords i) 0) ((coords i) 1)
840 (sensor-data i))))
841 (vi image)))))
842
843
801 844
802 ;;(defn test-touch [world creature] 845 ;;(defn test-touch [world creature]
803 846
804 847
805 (defn test-creature [thing] 848 (defn test-creature [thing]
810 z-axis 853 z-axis
811 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue) 854 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
812 creature (blender-creature thing) 855 creature (blender-creature thing)
813 touch-nerves (touch creature) 856 touch-nerves (touch creature)
814 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves) 857 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
858 [init-vision [vision-data]]
859 (enable-vision creature (test-eye))
860 vision-debug (debug-vision-window)
861 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
815 ] 862 ]
816 (world 863 (world
817 (nodify [creature 864 (nodify [creature
818 (box 10 2 10 :position (Vector3f. 0 -9 0) 865 (box 10 2 10 :position (Vector3f. 0 -9 0)
819 :color ColorRGBA/Gray :mass 0) 866 :color ColorRGBA/Gray :mass 0)
820 x-axis y-axis z-axis 867 x-axis y-axis z-axis
868 me
821 ]) 869 ])
822 standard-debug-controls 870 standard-debug-controls
823 (fn [world] 871 (fn [world]
824 (light-up-everything world) 872 (light-up-everything world)
825 (enable-debug world) 873 (enable-debug world)
874 (init-vision world)
875
876 (add-eye world
877 (attach-eye creature (test-eye))
878 (comp (view-image) BufferedImage!))
879
880 (add-eye world (.getCamera world) no-op)
881
826 ;;(com.aurellem.capture.Capture/captureVideo 882 ;;(com.aurellem.capture.Capture/captureVideo
827 ;; world (file-str "/home/r/proj/ai-videos/hand")) 883 ;; world (file-str "/home/r/proj/ai-videos/hand"))
828 ;;(.setTimer world (RatchetTimer. 60)) 884 ;;(.setTimer world (RatchetTimer. 60))
829 ;;(speed-up world) 885 ;;(speed-up world)
830 ;;(set-gravity world (Vector3f. 0 0 0)) 886 ;;(set-gravity world (Vector3f. 0 0 0))
835 891
836 (dorun 892 (dorun
837 (map #(%1 (%2 (.getRootNode world))) 893 (map #(%1 (%2 (.getRootNode world)))
838 touch-debug-windows touch-nerves) 894 touch-debug-windows touch-nerves)
839 ) 895 )
840 896 ;;(println-repl (vision-data))
897 (.setLocalTranslation me (.getLocation (.getCamera world)))
898
899 (vision-debug (vision-data))
841 ) 900 )
842 ;;(let [timer (atom 0)] 901 ;;(let [timer (atom 0)]
843 ;; (fn [_ _] 902 ;; (fn [_ _]
844 ;; (swap! timer inc) 903 ;; (swap! timer inc)
845 ;; (if (= (rem @timer 60) 0) 904 ;; (if (= (rem @timer 60) 0)
916 (let [camera-pos (Vector3f. 0 30 0) 975 (let [camera-pos (Vector3f. 0 30 0)
917 rock (box 1 1 1 :color ColorRGBA/Blue 976 rock (box 1 1 1 :color ColorRGBA/Blue
918 :position (Vector3f. 0 10 0) 977 :position (Vector3f. 0 10 0)
919 :mass 30 978 :mass 30
920 ) 979 )
980 rot (.getWorldRotation rock)
921 981
922 table (box 3 1 10 :color ColorRGBA/Gray :mass 0 982 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
923 :position (Vector3f. 0 -3 0))] 983 :position (Vector3f. 0 -3 0))]
924 984
925 (world 985 (world
935 995
936 (.setTimer world (RatchetTimer. 60)) 996 (.setTimer world (RatchetTimer. 60))
937 (add-eye world cam (comp (view-image) BufferedImage!)) 997 (add-eye world cam (comp (view-image) BufferedImage!))
938 (add-eye world (.getCamera world) no-op)) 998 (add-eye world (.getCamera world) no-op))
939 ) 999 )
940 no-op))) 1000 (fn [_ _] (println-repl rot)))))
941 1001
1002
942 #+end_src 1003 #+end_src
943 1004
944 #+results: body-1 1005 #+results: body-1
945 : #'cortex.silly/test-creature 1006 : #'cortex.silly/test-creature
946 1007