diff 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
line wrap: on
line diff
     1.1 --- a/org/test-creature.org	Sat Jan 14 23:06:44 2012 -0700
     1.2 +++ b/org/test-creature.org	Sun Jan 15 00:33:06 2012 -0700
     1.3 @@ -63,6 +63,7 @@
     1.4  (import java.awt.Dimension)
     1.5  (import javax.swing.JFrame)
     1.6  (import java.awt.Dimension)
     1.7 +(import com.aurellem.capture.RatchetTimer)
     1.8  (declare joint-create)
     1.9  
    1.10  (defn view-image
    1.11 @@ -100,19 +101,21 @@
    1.12    ;; TODO maybe parallelize this since it's easy
    1.13  
    1.14    [points]
    1.15 -  (let [xs (vec (map first points))
    1.16 -        ys (vec (map second points))
    1.17 -        x0 (apply min xs)
    1.18 -        y0 (apply min ys)
    1.19 -        width (- (apply max xs) x0)
    1.20 -        height (- (apply max ys) y0)
    1.21 -        image (BufferedImage. (inc width) (inc height)
    1.22 -                              BufferedImage/TYPE_BYTE_BINARY)]
    1.23 -    (dorun 
    1.24 -     (for [index (range (count points))]
    1.25 -       (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    1.26 -
    1.27 -    image))
    1.28 +  (if (empty? points)
    1.29 +    (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
    1.30 +    (let [xs (vec (map first points))
    1.31 +          ys (vec (map second points))
    1.32 +          x0 (apply min xs)
    1.33 +          y0 (apply min ys)
    1.34 +          width (- (apply max xs) x0)
    1.35 +          height (- (apply max ys) y0)
    1.36 +          image (BufferedImage. (inc width) (inc height)
    1.37 +                                BufferedImage/TYPE_BYTE_BINARY)]
    1.38 +      (dorun 
    1.39 +       (for [index (range (count points))]
    1.40 +         (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    1.41 +      
    1.42 +      image)))
    1.43  
    1.44  (defn test-data
    1.45    []
    1.46 @@ -405,36 +408,7 @@
    1.47      (-> (.getAdditionalRenderState)
    1.48          (.setDepthTest false))))
    1.49  
    1.50 -(defn test-creature [thing]
    1.51 -  (let [x-axis
    1.52 -        (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
    1.53 -        y-axis
    1.54 -        (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
    1.55 -        z-axis
    1.56 -        (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
    1.57 -  (world
    1.58 -   (nodify [(blender-creature thing)
    1.59 -            (box 10 2 10 :position (Vector3f. 0 -9 0)
    1.60 -                 :color ColorRGBA/Gray :mass 0)
    1.61 -            x-axis y-axis z-axis
    1.62 -            ])
    1.63 -   standard-debug-controls
    1.64 -   (fn [world]
    1.65 -     (light-up-everything world)
    1.66 -     (enable-debug world)
    1.67 -     ;;(com.aurellem.capture.Capture/captureVideo
    1.68 -     ;; world (file-str "/home/r/proj/ai-videos/hand"))
    1.69 -     (.setTimer world (NanoTimer.))
    1.70 -     (set-gravity world (Vector3f. 0 0 0))
    1.71 -     (speed-up world)
    1.72 -     )
    1.73 -   no-op
    1.74 -   ;;(let [timer (atom 0)]
    1.75 -   ;;  (fn [_ _]
    1.76 -   ;;    (swap! timer inc)
    1.77 -   ;;    (if (= (rem @timer 60) 0)
    1.78 -   ;;      (println-repl (float (/ @timer 60))))))
    1.79 -   )))
    1.80 +
    1.81  
    1.82  (defn colorful []
    1.83    (.getChild (worm-model) "worm-21"))
    1.84 @@ -652,6 +626,12 @@
    1.85       ]))
    1.86    
    1.87  
    1.88 +(defn sensors-in-triangle
    1.89 +  "find the locations of the sensors within a triangle"
    1.90 +  [image tri]
    1.91 +  )
    1.92 +
    1.93 +
    1.94  (defn locate-feelers
    1.95    "Search the geometry's tactile UV image for touch sensors, returning
    1.96    their positions in geometry-relative coordinates."
    1.97 @@ -695,12 +675,14 @@
    1.98  (use 'clojure.contrib.def)
    1.99  
   1.100  (defn-memo touch-topology [#^Gemoetry geo]
   1.101 -  (let [feeler-coords
   1.102 -        (map
   1.103 -         #(vector (int (.getX %)) (int (.getY %)))
   1.104 -         (white-coordinates
   1.105 -          (.getProcessor (touch-receptor-image (colorful)))))]
   1.106 -    (vec (collapse feeler-coords))))
   1.107 +  (if-let [image (touch-receptor-image geo)]
   1.108 +    (let [feeler-coords
   1.109 +          (map
   1.110 +           #(vector (int (.getX %)) (int (.getY %)))
   1.111 +           (white-coordinates
   1.112 +            (.getProcessor image)))]
   1.113 +      (vec (collapse feeler-coords)))
   1.114 +    []))
   1.115  
   1.116  (defn enable-touch [#^Geometry geo]
   1.117    (let [feeler-coords (locate-feelers geo)
   1.118 @@ -753,124 +735,46 @@
   1.119                        1 -1} (sensor-data i)))))
   1.120          (vi image)))))
   1.121  
   1.122 -(defn world-setup [joint]
   1.123 -  (let [joint-position (Vector3f. 0 0 0)
   1.124 -        joint-rotation
   1.125 -        (.toRotationMatrix
   1.126 -         (.mult
   1.127 -          (doto (Quaternion.)
   1.128 -            (.fromAngleAxis
   1.129 -             (* 1 (/ Math/PI 4))
   1.130 -             (Vector3f. -1 0 0)))
   1.131 -          (doto (Quaternion.)
   1.132 -            (.fromAngleAxis
   1.133 -             (* 1 (/ Math/PI 2))
   1.134 -             (Vector3f. 0 0 1)))))
   1.135 -        top-position (.mult joint-rotation (Vector3f. 8 0 0))
   1.136 -        
   1.137 -        origin (doto
   1.138 -                   (sphere 0.1 :physical? false :color ColorRGBA/Cyan
   1.139 -                           :position top-position))
   1.140 -        top (doto
   1.141 -                (sphere 0.1 :physical? false :color ColorRGBA/Yellow
   1.142 -                        :position top-position)
   1.143  
   1.144 -              (.addControl
   1.145 -               (RigidBodyControl.
   1.146 -                (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
   1.147 -        bottom (doto
   1.148 -                   (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
   1.149 -                           :position (Vector3f. 0 0 0))
   1.150 -                 (.addControl
   1.151 -               (RigidBodyControl.
   1.152 -                (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
   1.153 -        table (box 10 2 10 :position (Vector3f. 0 -20 0)
   1.154 -                   :color ColorRGBA/Gray :mass 0)
   1.155 -        a (.getControl top RigidBodyControl)
   1.156 -        b (.getControl bottom RigidBodyControl)]
   1.157 +;;(defn test-touch [world creature]
   1.158  
   1.159 -    (cond
   1.160 -     (= joint :cone)
   1.161 -     
   1.162 -       (doto (ConeJoint.
   1.163 -              a b
   1.164 -              (world-to-local top joint-position)
   1.165 -              (world-to-local bottom joint-position)
   1.166 -              joint-rotation
   1.167 -              joint-rotation
   1.168 -              )
   1.169  
   1.170 -              
   1.171 -         (.setLimit (* (/ 10) Math/PI)
   1.172 -                    (* (/ 4)  Math/PI)
   1.173 -                    0)))
   1.174 -  [origin top bottom table]))
   1.175 -            
   1.176 -(defn test-joint [joint]
   1.177 -  (let [[origin top bottom floor] (world-setup joint)
   1.178 -        control (.getControl top RigidBodyControl)
   1.179 -        move-up? (atom false)
   1.180 -        move-down? (atom false)
   1.181 -        move-left? (atom false)
   1.182 -        move-right? (atom false)
   1.183 -        roll-left? (atom false)
   1.184 -        roll-right? (atom false)
   1.185 -        timer (atom 0)]
   1.186 -
   1.187 -    (world
   1.188 -     (nodify [top bottom floor origin])
   1.189 -     (merge standard-debug-controls
   1.190 -            {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
   1.191 -             "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
   1.192 -             "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
   1.193 -             "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
   1.194 -             "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
   1.195 -             "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})     
   1.196 -     
   1.197 -     (fn [world]
   1.198 -       (light-up-everything world)
   1.199 -       (enable-debug world)
   1.200 -       (set-gravity world (Vector3f. 0 0 0))
   1.201 -       )
   1.202 -     
   1.203 -     (fn [world _]
   1.204 -       (if (zero? (rem (swap! timer inc) 100))
   1.205 -         (do
   1.206 -         ;;  (println-repl @timer)
   1.207 -           (.attachChild (.getRootNode world)
   1.208 -                       (sphere 0.05 :color ColorRGBA/Yellow
   1.209 -                               :position (.getWorldTranslation top)
   1.210 -                               :physical? false))
   1.211 -           (.attachChild (.getRootNode world)
   1.212 -                         (sphere 0.05 :color ColorRGBA/LightGray
   1.213 -                                 :position (.getWorldTranslation bottom)
   1.214 -                                 :physical? false))))
   1.215 -
   1.216 -       (if @move-up?
   1.217 -         (.applyTorque control
   1.218 -                       (.mult (.getPhysicsRotation control)
   1.219 -                              (Vector3f. 0 0 10))))
   1.220 -       (if @move-down?
   1.221 -         (.applyTorque control
   1.222 -                       (.mult (.getPhysicsRotation control)
   1.223 -                              (Vector3f. 0 0 -10))))
   1.224 -       (if @move-left?
   1.225 -         (.applyTorque control
   1.226 -                       (.mult (.getPhysicsRotation control)
   1.227 -                              (Vector3f. 0 10 0))))
   1.228 -       (if @move-right?
   1.229 -         (.applyTorque control
   1.230 -                       (.mult (.getPhysicsRotation control)
   1.231 -                              (Vector3f. 0 -10 0))))
   1.232 -       (if @roll-left?
   1.233 -         (.applyTorque control
   1.234 -                       (.mult (.getPhysicsRotation control)
   1.235 -                              (Vector3f. -1 0 0))))
   1.236 -       (if @roll-right?
   1.237 -         (.applyTorque control
   1.238 -                       (.mult (.getPhysicsRotation control)
   1.239 -                              (Vector3f. 1 0 0))))))))
   1.240 -
   1.241 +(defn test-creature [thing]
   1.242 +  (let [x-axis
   1.243 +        (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
   1.244 +        y-axis
   1.245 +        (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
   1.246 +        z-axis
   1.247 +        (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
   1.248 +        creature (blender-creature thing)
   1.249 +        touch-nerves (touch creature)
   1.250 +        touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
   1.251 +        ]
   1.252 +  (world
   1.253 +   (nodify [creature
   1.254 +            (box 10 2 10 :position (Vector3f. 0 -9 0)
   1.255 +                 :color ColorRGBA/Gray :mass 0)
   1.256 +            x-axis y-axis z-axis
   1.257 +            ])
   1.258 +   standard-debug-controls
   1.259 +   (fn [world]
   1.260 +     (light-up-everything world)
   1.261 +     (enable-debug world)
   1.262 +     ;;(com.aurellem.capture.Capture/captureVideo
   1.263 +     ;; world (file-str "/home/r/proj/ai-videos/hand"))
   1.264 +     (.setTimer world (RatchetTimer. 60))
   1.265 +     ;;(set-gravity world (Vector3f. 0 0 0))
   1.266 +     )
   1.267 +   (fn [world tpf]
   1.268 +     (dorun
   1.269 +      (map #(%1 (%2 (.getRootNode world))) touch-debug-windows touch-nerves))
   1.270 +     )
   1.271 +   ;;(let [timer (atom 0)]
   1.272 +   ;;  (fn [_ _]
   1.273 +   ;;    (swap! timer inc)
   1.274 +   ;;    (if (= (rem @timer 60) 0)
   1.275 +   ;;      (println-repl (float (/ @timer 60))))))
   1.276 +   )))
   1.277  
   1.278  #+end_src
   1.279  
   1.280 @@ -1066,6 +970,124 @@
   1.281      ))))
   1.282  
   1.283  
   1.284 +(defn world-setup [joint]
   1.285 +  (let [joint-position (Vector3f. 0 0 0)
   1.286 +        joint-rotation
   1.287 +        (.toRotationMatrix
   1.288 +         (.mult
   1.289 +          (doto (Quaternion.)
   1.290 +            (.fromAngleAxis
   1.291 +             (* 1 (/ Math/PI 4))
   1.292 +             (Vector3f. -1 0 0)))
   1.293 +          (doto (Quaternion.)
   1.294 +            (.fromAngleAxis
   1.295 +             (* 1 (/ Math/PI 2))
   1.296 +             (Vector3f. 0 0 1)))))
   1.297 +        top-position (.mult joint-rotation (Vector3f. 8 0 0))
   1.298 +        
   1.299 +        origin (doto
   1.300 +                   (sphere 0.1 :physical? false :color ColorRGBA/Cyan
   1.301 +                           :position top-position))
   1.302 +        top (doto
   1.303 +                (sphere 0.1 :physical? false :color ColorRGBA/Yellow
   1.304 +                        :position top-position)
   1.305 +
   1.306 +              (.addControl
   1.307 +               (RigidBodyControl.
   1.308 +                (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
   1.309 +        bottom (doto
   1.310 +                   (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
   1.311 +                           :position (Vector3f. 0 0 0))
   1.312 +                 (.addControl
   1.313 +               (RigidBodyControl.
   1.314 +                (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
   1.315 +        table (box 10 2 10 :position (Vector3f. 0 -20 0)
   1.316 +                   :color ColorRGBA/Gray :mass 0)
   1.317 +        a (.getControl top RigidBodyControl)
   1.318 +        b (.getControl bottom RigidBodyControl)]
   1.319 +
   1.320 +    (cond
   1.321 +     (= joint :cone)
   1.322 +     
   1.323 +       (doto (ConeJoint.
   1.324 +              a b
   1.325 +              (world-to-local top joint-position)
   1.326 +              (world-to-local bottom joint-position)
   1.327 +              joint-rotation
   1.328 +              joint-rotation
   1.329 +              )
   1.330 +
   1.331 +              
   1.332 +         (.setLimit (* (/ 10) Math/PI)
   1.333 +                    (* (/ 4)  Math/PI)
   1.334 +                    0)))
   1.335 +  [origin top bottom table]))
   1.336 +            
   1.337 +(defn test-joint [joint]
   1.338 +  (let [[origin top bottom floor] (world-setup joint)
   1.339 +        control (.getControl top RigidBodyControl)
   1.340 +        move-up? (atom false)
   1.341 +        move-down? (atom false)
   1.342 +        move-left? (atom false)
   1.343 +        move-right? (atom false)
   1.344 +        roll-left? (atom false)
   1.345 +        roll-right? (atom false)
   1.346 +        timer (atom 0)]
   1.347 +
   1.348 +    (world
   1.349 +     (nodify [top bottom floor origin])
   1.350 +     (merge standard-debug-controls
   1.351 +            {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
   1.352 +             "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
   1.353 +             "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
   1.354 +             "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
   1.355 +             "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
   1.356 +             "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})     
   1.357 +     
   1.358 +     (fn [world]
   1.359 +       (light-up-everything world)
   1.360 +       (enable-debug world)
   1.361 +       (set-gravity world (Vector3f. 0 0 0))
   1.362 +       )
   1.363 +     
   1.364 +     (fn [world _]
   1.365 +       (if (zero? (rem (swap! timer inc) 100))
   1.366 +         (do
   1.367 +         ;;  (println-repl @timer)
   1.368 +           (.attachChild (.getRootNode world)
   1.369 +                       (sphere 0.05 :color ColorRGBA/Yellow
   1.370 +                               :position (.getWorldTranslation top)
   1.371 +                               :physical? false))
   1.372 +           (.attachChild (.getRootNode world)
   1.373 +                         (sphere 0.05 :color ColorRGBA/LightGray
   1.374 +                                 :position (.getWorldTranslation bottom)
   1.375 +                                 :physical? false))))
   1.376 +
   1.377 +       (if @move-up?
   1.378 +         (.applyTorque control
   1.379 +                       (.mult (.getPhysicsRotation control)
   1.380 +                              (Vector3f. 0 0 10))))
   1.381 +       (if @move-down?
   1.382 +         (.applyTorque control
   1.383 +                       (.mult (.getPhysicsRotation control)
   1.384 +                              (Vector3f. 0 0 -10))))
   1.385 +       (if @move-left?
   1.386 +         (.applyTorque control
   1.387 +                       (.mult (.getPhysicsRotation control)
   1.388 +                              (Vector3f. 0 10 0))))
   1.389 +       (if @move-right?
   1.390 +         (.applyTorque control
   1.391 +                       (.mult (.getPhysicsRotation control)
   1.392 +                              (Vector3f. 0 -10 0))))
   1.393 +       (if @roll-left?
   1.394 +         (.applyTorque control
   1.395 +                       (.mult (.getPhysicsRotation control)
   1.396 +                              (Vector3f. -1 0 0))))
   1.397 +       (if @roll-right?
   1.398 +         (.applyTorque control
   1.399 +                       (.mult (.getPhysicsRotation control)
   1.400 +                              (Vector3f. 1 0 0))))))))
   1.401 +
   1.402  
   1.403  
   1.404  #+end_src