comparison org/test-creature.org @ 106:40e72c6943d8

fixing out-of-bounds error
author Robert McIntyre <rlm@mit.edu>
date Sun, 15 Jan 2012 00:33:06 -0700
parents 3334bf15854b
children 53fb379ac678
comparison
equal deleted inserted replaced
105:3334bf15854b 106:40e72c6943d8
61 (import javax.swing.JPanel) 61 (import javax.swing.JPanel)
62 (import javax.swing.SwingUtilities) 62 (import javax.swing.SwingUtilities)
63 (import java.awt.Dimension) 63 (import java.awt.Dimension)
64 (import javax.swing.JFrame) 64 (import javax.swing.JFrame)
65 (import java.awt.Dimension) 65 (import java.awt.Dimension)
66 (import com.aurellem.capture.RatchetTimer)
66 (declare joint-create) 67 (declare joint-create)
67 68
68 (defn view-image 69 (defn view-image
69 "Initailizes a JPanel on which you may draw a BufferedImage. 70 "Initailizes a JPanel on which you may draw a BufferedImage.
70 Returns a function that accepts a BufferedImage and draws it to the 71 Returns a function that accepts a BufferedImage and draws it to the
98 BufferedImage." 99 BufferedImage."
99 100
100 ;; TODO maybe parallelize this since it's easy 101 ;; TODO maybe parallelize this since it's easy
101 102
102 [points] 103 [points]
103 (let [xs (vec (map first points)) 104 (if (empty? points)
104 ys (vec (map second points)) 105 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
105 x0 (apply min xs) 106 (let [xs (vec (map first points))
106 y0 (apply min ys) 107 ys (vec (map second points))
107 width (- (apply max xs) x0) 108 x0 (apply min xs)
108 height (- (apply max ys) y0) 109 y0 (apply min ys)
109 image (BufferedImage. (inc width) (inc height) 110 width (- (apply max xs) x0)
110 BufferedImage/TYPE_BYTE_BINARY)] 111 height (- (apply max ys) y0)
111 (dorun 112 image (BufferedImage. (inc width) (inc height)
112 (for [index (range (count points))] 113 BufferedImage/TYPE_BYTE_BINARY)]
113 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1))) 114 (dorun
114 115 (for [index (range (count points))]
115 image)) 116 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
117
118 image)))
116 119
117 (defn test-data 120 (defn test-data
118 [] 121 []
119 (vec 122 (vec
120 (for [a (range 0 1000 2) 123 (for [a (range 0 1000 2)
403 "Common/MatDefs/Misc/Unshaded.j3md") 406 "Common/MatDefs/Misc/Unshaded.j3md")
404 (.setColor "Color" color) 407 (.setColor "Color" color)
405 (-> (.getAdditionalRenderState) 408 (-> (.getAdditionalRenderState)
406 (.setDepthTest false)))) 409 (.setDepthTest false))))
407 410
408 (defn test-creature [thing] 411
409 (let [x-axis
410 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
411 y-axis
412 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
413 z-axis
414 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
415 (world
416 (nodify [(blender-creature thing)
417 (box 10 2 10 :position (Vector3f. 0 -9 0)
418 :color ColorRGBA/Gray :mass 0)
419 x-axis y-axis z-axis
420 ])
421 standard-debug-controls
422 (fn [world]
423 (light-up-everything world)
424 (enable-debug world)
425 ;;(com.aurellem.capture.Capture/captureVideo
426 ;; world (file-str "/home/r/proj/ai-videos/hand"))
427 (.setTimer world (NanoTimer.))
428 (set-gravity world (Vector3f. 0 0 0))
429 (speed-up world)
430 )
431 no-op
432 ;;(let [timer (atom 0)]
433 ;; (fn [_ _]
434 ;; (swap! timer inc)
435 ;; (if (= (rem @timer 60) 0)
436 ;; (println-repl (float (/ @timer 60))))))
437 )))
438 412
439 (defn colorful [] 413 (defn colorful []
440 (.getChild (worm-model) "worm-21")) 414 (.getChild (worm-model) "worm-21"))
441 415
442 (import jme3tools.converters.ImageToAwt) 416 (import jme3tools.converters.ImageToAwt)
649 [x y 623 [x y
650 (- (apply max (map first verts)) x) 624 (- (apply max (map first verts)) x)
651 (- (apply max (map second verts)) y) 625 (- (apply max (map second verts)) y)
652 ])) 626 ]))
653 627
628
629 (defn sensors-in-triangle
630 "find the locations of the sensors within a triangle"
631 [image tri]
632 )
633
654 634
655 (defn locate-feelers 635 (defn locate-feelers
656 "Search the geometry's tactile UV image for touch sensors, returning 636 "Search the geometry's tactile UV image for touch sensors, returning
657 their positions in geometry-relative coordinates." 637 their positions in geometry-relative coordinates."
658 [#^Geometry geo] 638 [#^Geometry geo]
693 (repeat (count (triangles geo)) []))) 673 (repeat (count (triangles geo)) [])))
694 674
695 (use 'clojure.contrib.def) 675 (use 'clojure.contrib.def)
696 676
697 (defn-memo touch-topology [#^Gemoetry geo] 677 (defn-memo touch-topology [#^Gemoetry geo]
698 (let [feeler-coords 678 (if-let [image (touch-receptor-image geo)]
699 (map 679 (let [feeler-coords
700 #(vector (int (.getX %)) (int (.getY %))) 680 (map
701 (white-coordinates 681 #(vector (int (.getX %)) (int (.getY %)))
702 (.getProcessor (touch-receptor-image (colorful)))))] 682 (white-coordinates
703 (vec (collapse feeler-coords)))) 683 (.getProcessor image)))]
684 (vec (collapse feeler-coords)))
685 []))
704 686
705 (defn enable-touch [#^Geometry geo] 687 (defn enable-touch [#^Geometry geo]
706 (let [feeler-coords (locate-feelers geo) 688 (let [feeler-coords (locate-feelers geo)
707 tris (triangles geo) 689 tris (triangles geo)
708 limit 0.1] 690 limit 0.1]
751 (.setRGB image ((coords i) 0) ((coords i) 1) 733 (.setRGB image ((coords i) 0) ((coords i) 1)
752 ({0 -16777216 734 ({0 -16777216
753 1 -1} (sensor-data i))))) 735 1 -1} (sensor-data i)))))
754 (vi image))))) 736 (vi image)))))
755 737
756 (defn world-setup [joint] 738
757 (let [joint-position (Vector3f. 0 0 0) 739 ;;(defn test-touch [world creature]
758 joint-rotation 740
759 (.toRotationMatrix 741
760 (.mult 742 (defn test-creature [thing]
761 (doto (Quaternion.) 743 (let [x-axis
762 (.fromAngleAxis 744 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
763 (* 1 (/ Math/PI 4)) 745 y-axis
764 (Vector3f. -1 0 0))) 746 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
765 (doto (Quaternion.) 747 z-axis
766 (.fromAngleAxis 748 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
767 (* 1 (/ Math/PI 2)) 749 creature (blender-creature thing)
768 (Vector3f. 0 0 1))))) 750 touch-nerves (touch creature)
769 top-position (.mult joint-rotation (Vector3f. 8 0 0)) 751 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
770 752 ]
771 origin (doto 753 (world
772 (sphere 0.1 :physical? false :color ColorRGBA/Cyan 754 (nodify [creature
773 :position top-position)) 755 (box 10 2 10 :position (Vector3f. 0 -9 0)
774 top (doto 756 :color ColorRGBA/Gray :mass 0)
775 (sphere 0.1 :physical? false :color ColorRGBA/Yellow 757 x-axis y-axis z-axis
776 :position top-position) 758 ])
777 759 standard-debug-controls
778 (.addControl 760 (fn [world]
779 (RigidBodyControl. 761 (light-up-everything world)
780 (CapsuleCollisionShape. 0.5 1.5 1) (float 20)))) 762 (enable-debug world)
781 bottom (doto 763 ;;(com.aurellem.capture.Capture/captureVideo
782 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray 764 ;; world (file-str "/home/r/proj/ai-videos/hand"))
783 :position (Vector3f. 0 0 0)) 765 (.setTimer world (RatchetTimer. 60))
784 (.addControl 766 ;;(set-gravity world (Vector3f. 0 0 0))
785 (RigidBodyControl. 767 )
786 (CapsuleCollisionShape. 0.5 1.5 1) (float 0)))) 768 (fn [world tpf]
787 table (box 10 2 10 :position (Vector3f. 0 -20 0) 769 (dorun
788 :color ColorRGBA/Gray :mass 0) 770 (map #(%1 (%2 (.getRootNode world))) touch-debug-windows touch-nerves))
789 a (.getControl top RigidBodyControl) 771 )
790 b (.getControl bottom RigidBodyControl)] 772 ;;(let [timer (atom 0)]
791 773 ;; (fn [_ _]
792 (cond 774 ;; (swap! timer inc)
793 (= joint :cone) 775 ;; (if (= (rem @timer 60) 0)
794 776 ;; (println-repl (float (/ @timer 60))))))
795 (doto (ConeJoint. 777 )))
796 a b
797 (world-to-local top joint-position)
798 (world-to-local bottom joint-position)
799 joint-rotation
800 joint-rotation
801 )
802
803
804 (.setLimit (* (/ 10) Math/PI)
805 (* (/ 4) Math/PI)
806 0)))
807 [origin top bottom table]))
808
809 (defn test-joint [joint]
810 (let [[origin top bottom floor] (world-setup joint)
811 control (.getControl top RigidBodyControl)
812 move-up? (atom false)
813 move-down? (atom false)
814 move-left? (atom false)
815 move-right? (atom false)
816 roll-left? (atom false)
817 roll-right? (atom false)
818 timer (atom 0)]
819
820 (world
821 (nodify [top bottom floor origin])
822 (merge standard-debug-controls
823 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
824 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
825 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
826 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
827 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
828 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
829
830 (fn [world]
831 (light-up-everything world)
832 (enable-debug world)
833 (set-gravity world (Vector3f. 0 0 0))
834 )
835
836 (fn [world _]
837 (if (zero? (rem (swap! timer inc) 100))
838 (do
839 ;; (println-repl @timer)
840 (.attachChild (.getRootNode world)
841 (sphere 0.05 :color ColorRGBA/Yellow
842 :position (.getWorldTranslation top)
843 :physical? false))
844 (.attachChild (.getRootNode world)
845 (sphere 0.05 :color ColorRGBA/LightGray
846 :position (.getWorldTranslation bottom)
847 :physical? false))))
848
849 (if @move-up?
850 (.applyTorque control
851 (.mult (.getPhysicsRotation control)
852 (Vector3f. 0 0 10))))
853 (if @move-down?
854 (.applyTorque control
855 (.mult (.getPhysicsRotation control)
856 (Vector3f. 0 0 -10))))
857 (if @move-left?
858 (.applyTorque control
859 (.mult (.getPhysicsRotation control)
860 (Vector3f. 0 10 0))))
861 (if @move-right?
862 (.applyTorque control
863 (.mult (.getPhysicsRotation control)
864 (Vector3f. 0 -10 0))))
865 (if @roll-left?
866 (.applyTorque control
867 (.mult (.getPhysicsRotation control)
868 (Vector3f. -1 0 0))))
869 (if @roll-right?
870 (.applyTorque control
871 (.mult (.getPhysicsRotation control)
872 (Vector3f. 1 0 0))))))))
873
874 778
875 #+end_src 779 #+end_src
876 780
877 #+results: body-1 781 #+results: body-1
878 : #'cortex.silly/tactile-coords 782 : #'cortex.silly/tactile-coords
1064 (println) 968 (println)
1065 969
1066 )))) 970 ))))
1067 971
1068 972
973 (defn world-setup [joint]
974 (let [joint-position (Vector3f. 0 0 0)
975 joint-rotation
976 (.toRotationMatrix
977 (.mult
978 (doto (Quaternion.)
979 (.fromAngleAxis
980 (* 1 (/ Math/PI 4))
981 (Vector3f. -1 0 0)))
982 (doto (Quaternion.)
983 (.fromAngleAxis
984 (* 1 (/ Math/PI 2))
985 (Vector3f. 0 0 1)))))
986 top-position (.mult joint-rotation (Vector3f. 8 0 0))
987
988 origin (doto
989 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
990 :position top-position))
991 top (doto
992 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
993 :position top-position)
994
995 (.addControl
996 (RigidBodyControl.
997 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
998 bottom (doto
999 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
1000 :position (Vector3f. 0 0 0))
1001 (.addControl
1002 (RigidBodyControl.
1003 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
1004 table (box 10 2 10 :position (Vector3f. 0 -20 0)
1005 :color ColorRGBA/Gray :mass 0)
1006 a (.getControl top RigidBodyControl)
1007 b (.getControl bottom RigidBodyControl)]
1008
1009 (cond
1010 (= joint :cone)
1011
1012 (doto (ConeJoint.
1013 a b
1014 (world-to-local top joint-position)
1015 (world-to-local bottom joint-position)
1016 joint-rotation
1017 joint-rotation
1018 )
1019
1020
1021 (.setLimit (* (/ 10) Math/PI)
1022 (* (/ 4) Math/PI)
1023 0)))
1024 [origin top bottom table]))
1025
1026 (defn test-joint [joint]
1027 (let [[origin top bottom floor] (world-setup joint)
1028 control (.getControl top RigidBodyControl)
1029 move-up? (atom false)
1030 move-down? (atom false)
1031 move-left? (atom false)
1032 move-right? (atom false)
1033 roll-left? (atom false)
1034 roll-right? (atom false)
1035 timer (atom 0)]
1036
1037 (world
1038 (nodify [top bottom floor origin])
1039 (merge standard-debug-controls
1040 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
1041 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
1042 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
1043 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
1044 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
1045 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
1046
1047 (fn [world]
1048 (light-up-everything world)
1049 (enable-debug world)
1050 (set-gravity world (Vector3f. 0 0 0))
1051 )
1052
1053 (fn [world _]
1054 (if (zero? (rem (swap! timer inc) 100))
1055 (do
1056 ;; (println-repl @timer)
1057 (.attachChild (.getRootNode world)
1058 (sphere 0.05 :color ColorRGBA/Yellow
1059 :position (.getWorldTranslation top)
1060 :physical? false))
1061 (.attachChild (.getRootNode world)
1062 (sphere 0.05 :color ColorRGBA/LightGray
1063 :position (.getWorldTranslation bottom)
1064 :physical? false))))
1065
1066 (if @move-up?
1067 (.applyTorque control
1068 (.mult (.getPhysicsRotation control)
1069 (Vector3f. 0 0 10))))
1070 (if @move-down?
1071 (.applyTorque control
1072 (.mult (.getPhysicsRotation control)
1073 (Vector3f. 0 0 -10))))
1074 (if @move-left?
1075 (.applyTorque control
1076 (.mult (.getPhysicsRotation control)
1077 (Vector3f. 0 10 0))))
1078 (if @move-right?
1079 (.applyTorque control
1080 (.mult (.getPhysicsRotation control)
1081 (Vector3f. 0 -10 0))))
1082 (if @roll-left?
1083 (.applyTorque control
1084 (.mult (.getPhysicsRotation control)
1085 (Vector3f. -1 0 0))))
1086 (if @roll-right?
1087 (.applyTorque control
1088 (.mult (.getPhysicsRotation control)
1089 (Vector3f. 1 0 0))))))))
1090
1069 1091
1070 1092
1071 #+end_src 1093 #+end_src
1072 1094
1073 1095