changeset 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
files assets/Models/creature1/tip.png assets/Models/creature1/try-again.blend org/test-creature.org
diffstat 3 files changed, 186 insertions(+), 164 deletions(-) [+]
line wrap: on
line diff
     1.1 Binary file assets/Models/creature1/tip.png has changed
     2.1 Binary file assets/Models/creature1/try-again.blend has changed
     3.1 --- a/org/test-creature.org	Sat Jan 14 23:06:44 2012 -0700
     3.2 +++ b/org/test-creature.org	Sun Jan 15 00:33:06 2012 -0700
     3.3 @@ -63,6 +63,7 @@
     3.4  (import java.awt.Dimension)
     3.5  (import javax.swing.JFrame)
     3.6  (import java.awt.Dimension)
     3.7 +(import com.aurellem.capture.RatchetTimer)
     3.8  (declare joint-create)
     3.9  
    3.10  (defn view-image
    3.11 @@ -100,19 +101,21 @@
    3.12    ;; TODO maybe parallelize this since it's easy
    3.13  
    3.14    [points]
    3.15 -  (let [xs (vec (map first points))
    3.16 -        ys (vec (map second points))
    3.17 -        x0 (apply min xs)
    3.18 -        y0 (apply min ys)
    3.19 -        width (- (apply max xs) x0)
    3.20 -        height (- (apply max ys) y0)
    3.21 -        image (BufferedImage. (inc width) (inc height)
    3.22 -                              BufferedImage/TYPE_BYTE_BINARY)]
    3.23 -    (dorun 
    3.24 -     (for [index (range (count points))]
    3.25 -       (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    3.26 -
    3.27 -    image))
    3.28 +  (if (empty? points)
    3.29 +    (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
    3.30 +    (let [xs (vec (map first points))
    3.31 +          ys (vec (map second points))
    3.32 +          x0 (apply min xs)
    3.33 +          y0 (apply min ys)
    3.34 +          width (- (apply max xs) x0)
    3.35 +          height (- (apply max ys) y0)
    3.36 +          image (BufferedImage. (inc width) (inc height)
    3.37 +                                BufferedImage/TYPE_BYTE_BINARY)]
    3.38 +      (dorun 
    3.39 +       (for [index (range (count points))]
    3.40 +         (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
    3.41 +      
    3.42 +      image)))
    3.43  
    3.44  (defn test-data
    3.45    []
    3.46 @@ -405,36 +408,7 @@
    3.47      (-> (.getAdditionalRenderState)
    3.48          (.setDepthTest false))))
    3.49  
    3.50 -(defn test-creature [thing]
    3.51 -  (let [x-axis
    3.52 -        (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
    3.53 -        y-axis
    3.54 -        (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
    3.55 -        z-axis
    3.56 -        (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)]
    3.57 -  (world
    3.58 -   (nodify [(blender-creature thing)
    3.59 -            (box 10 2 10 :position (Vector3f. 0 -9 0)
    3.60 -                 :color ColorRGBA/Gray :mass 0)
    3.61 -            x-axis y-axis z-axis
    3.62 -            ])
    3.63 -   standard-debug-controls
    3.64 -   (fn [world]
    3.65 -     (light-up-everything world)
    3.66 -     (enable-debug world)
    3.67 -     ;;(com.aurellem.capture.Capture/captureVideo
    3.68 -     ;; world (file-str "/home/r/proj/ai-videos/hand"))
    3.69 -     (.setTimer world (NanoTimer.))
    3.70 -     (set-gravity world (Vector3f. 0 0 0))
    3.71 -     (speed-up world)
    3.72 -     )
    3.73 -   no-op
    3.74 -   ;;(let [timer (atom 0)]
    3.75 -   ;;  (fn [_ _]
    3.76 -   ;;    (swap! timer inc)
    3.77 -   ;;    (if (= (rem @timer 60) 0)
    3.78 -   ;;      (println-repl (float (/ @timer 60))))))
    3.79 -   )))
    3.80 +
    3.81  
    3.82  (defn colorful []
    3.83    (.getChild (worm-model) "worm-21"))
    3.84 @@ -652,6 +626,12 @@
    3.85       ]))
    3.86    
    3.87  
    3.88 +(defn sensors-in-triangle
    3.89 +  "find the locations of the sensors within a triangle"
    3.90 +  [image tri]
    3.91 +  )
    3.92 +
    3.93 +
    3.94  (defn locate-feelers
    3.95    "Search the geometry's tactile UV image for touch sensors, returning
    3.96    their positions in geometry-relative coordinates."
    3.97 @@ -695,12 +675,14 @@
    3.98  (use 'clojure.contrib.def)
    3.99  
   3.100  (defn-memo touch-topology [#^Gemoetry geo]
   3.101 -  (let [feeler-coords
   3.102 -        (map
   3.103 -         #(vector (int (.getX %)) (int (.getY %)))
   3.104 -         (white-coordinates
   3.105 -          (.getProcessor (touch-receptor-image (colorful)))))]
   3.106 -    (vec (collapse feeler-coords))))
   3.107 +  (if-let [image (touch-receptor-image geo)]
   3.108 +    (let [feeler-coords
   3.109 +          (map
   3.110 +           #(vector (int (.getX %)) (int (.getY %)))
   3.111 +           (white-coordinates
   3.112 +            (.getProcessor image)))]
   3.113 +      (vec (collapse feeler-coords)))
   3.114 +    []))
   3.115  
   3.116  (defn enable-touch [#^Geometry geo]
   3.117    (let [feeler-coords (locate-feelers geo)
   3.118 @@ -753,124 +735,46 @@
   3.119                        1 -1} (sensor-data i)))))
   3.120          (vi image)))))
   3.121  
   3.122 -(defn world-setup [joint]
   3.123 -  (let [joint-position (Vector3f. 0 0 0)
   3.124 -        joint-rotation
   3.125 -        (.toRotationMatrix
   3.126 -         (.mult
   3.127 -          (doto (Quaternion.)
   3.128 -            (.fromAngleAxis
   3.129 -             (* 1 (/ Math/PI 4))
   3.130 -             (Vector3f. -1 0 0)))
   3.131 -          (doto (Quaternion.)
   3.132 -            (.fromAngleAxis
   3.133 -             (* 1 (/ Math/PI 2))
   3.134 -             (Vector3f. 0 0 1)))))
   3.135 -        top-position (.mult joint-rotation (Vector3f. 8 0 0))
   3.136 -        
   3.137 -        origin (doto
   3.138 -                   (sphere 0.1 :physical? false :color ColorRGBA/Cyan
   3.139 -                           :position top-position))
   3.140 -        top (doto
   3.141 -                (sphere 0.1 :physical? false :color ColorRGBA/Yellow
   3.142 -                        :position top-position)
   3.143  
   3.144 -              (.addControl
   3.145 -               (RigidBodyControl.
   3.146 -                (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
   3.147 -        bottom (doto
   3.148 -                   (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
   3.149 -                           :position (Vector3f. 0 0 0))
   3.150 -                 (.addControl
   3.151 -               (RigidBodyControl.
   3.152 -                (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
   3.153 -        table (box 10 2 10 :position (Vector3f. 0 -20 0)
   3.154 -                   :color ColorRGBA/Gray :mass 0)
   3.155 -        a (.getControl top RigidBodyControl)
   3.156 -        b (.getControl bottom RigidBodyControl)]
   3.157 +;;(defn test-touch [world creature]
   3.158  
   3.159 -    (cond
   3.160 -     (= joint :cone)
   3.161 -     
   3.162 -       (doto (ConeJoint.
   3.163 -              a b
   3.164 -              (world-to-local top joint-position)
   3.165 -              (world-to-local bottom joint-position)
   3.166 -              joint-rotation
   3.167 -              joint-rotation
   3.168 -              )
   3.169  
   3.170 -              
   3.171 -         (.setLimit (* (/ 10) Math/PI)
   3.172 -                    (* (/ 4)  Math/PI)
   3.173 -                    0)))
   3.174 -  [origin top bottom table]))
   3.175 -            
   3.176 -(defn test-joint [joint]
   3.177 -  (let [[origin top bottom floor] (world-setup joint)
   3.178 -        control (.getControl top RigidBodyControl)
   3.179 -        move-up? (atom false)
   3.180 -        move-down? (atom false)
   3.181 -        move-left? (atom false)
   3.182 -        move-right? (atom false)
   3.183 -        roll-left? (atom false)
   3.184 -        roll-right? (atom false)
   3.185 -        timer (atom 0)]
   3.186 -
   3.187 -    (world
   3.188 -     (nodify [top bottom floor origin])
   3.189 -     (merge standard-debug-controls
   3.190 -            {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
   3.191 -             "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
   3.192 -             "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
   3.193 -             "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
   3.194 -             "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
   3.195 -             "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})     
   3.196 -     
   3.197 -     (fn [world]
   3.198 -       (light-up-everything world)
   3.199 -       (enable-debug world)
   3.200 -       (set-gravity world (Vector3f. 0 0 0))
   3.201 -       )
   3.202 -     
   3.203 -     (fn [world _]
   3.204 -       (if (zero? (rem (swap! timer inc) 100))
   3.205 -         (do
   3.206 -         ;;  (println-repl @timer)
   3.207 -           (.attachChild (.getRootNode world)
   3.208 -                       (sphere 0.05 :color ColorRGBA/Yellow
   3.209 -                               :position (.getWorldTranslation top)
   3.210 -                               :physical? false))
   3.211 -           (.attachChild (.getRootNode world)
   3.212 -                         (sphere 0.05 :color ColorRGBA/LightGray
   3.213 -                                 :position (.getWorldTranslation bottom)
   3.214 -                                 :physical? false))))
   3.215 -
   3.216 -       (if @move-up?
   3.217 -         (.applyTorque control
   3.218 -                       (.mult (.getPhysicsRotation control)
   3.219 -                              (Vector3f. 0 0 10))))
   3.220 -       (if @move-down?
   3.221 -         (.applyTorque control
   3.222 -                       (.mult (.getPhysicsRotation control)
   3.223 -                              (Vector3f. 0 0 -10))))
   3.224 -       (if @move-left?
   3.225 -         (.applyTorque control
   3.226 -                       (.mult (.getPhysicsRotation control)
   3.227 -                              (Vector3f. 0 10 0))))
   3.228 -       (if @move-right?
   3.229 -         (.applyTorque control
   3.230 -                       (.mult (.getPhysicsRotation control)
   3.231 -                              (Vector3f. 0 -10 0))))
   3.232 -       (if @roll-left?
   3.233 -         (.applyTorque control
   3.234 -                       (.mult (.getPhysicsRotation control)
   3.235 -                              (Vector3f. -1 0 0))))
   3.236 -       (if @roll-right?
   3.237 -         (.applyTorque control
   3.238 -                       (.mult (.getPhysicsRotation control)
   3.239 -                              (Vector3f. 1 0 0))))))))
   3.240 -
   3.241 +(defn test-creature [thing]
   3.242 +  (let [x-axis
   3.243 +        (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
   3.244 +        y-axis
   3.245 +        (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
   3.246 +        z-axis
   3.247 +        (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
   3.248 +        creature (blender-creature thing)
   3.249 +        touch-nerves (touch creature)
   3.250 +        touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
   3.251 +        ]
   3.252 +  (world
   3.253 +   (nodify [creature
   3.254 +            (box 10 2 10 :position (Vector3f. 0 -9 0)
   3.255 +                 :color ColorRGBA/Gray :mass 0)
   3.256 +            x-axis y-axis z-axis
   3.257 +            ])
   3.258 +   standard-debug-controls
   3.259 +   (fn [world]
   3.260 +     (light-up-everything world)
   3.261 +     (enable-debug world)
   3.262 +     ;;(com.aurellem.capture.Capture/captureVideo
   3.263 +     ;; world (file-str "/home/r/proj/ai-videos/hand"))
   3.264 +     (.setTimer world (RatchetTimer. 60))
   3.265 +     ;;(set-gravity world (Vector3f. 0 0 0))
   3.266 +     )
   3.267 +   (fn [world tpf]
   3.268 +     (dorun
   3.269 +      (map #(%1 (%2 (.getRootNode world))) touch-debug-windows touch-nerves))
   3.270 +     )
   3.271 +   ;;(let [timer (atom 0)]
   3.272 +   ;;  (fn [_ _]
   3.273 +   ;;    (swap! timer inc)
   3.274 +   ;;    (if (= (rem @timer 60) 0)
   3.275 +   ;;      (println-repl (float (/ @timer 60))))))
   3.276 +   )))
   3.277  
   3.278  #+end_src
   3.279  
   3.280 @@ -1066,6 +970,124 @@
   3.281      ))))
   3.282  
   3.283  
   3.284 +(defn world-setup [joint]
   3.285 +  (let [joint-position (Vector3f. 0 0 0)
   3.286 +        joint-rotation
   3.287 +        (.toRotationMatrix
   3.288 +         (.mult
   3.289 +          (doto (Quaternion.)
   3.290 +            (.fromAngleAxis
   3.291 +             (* 1 (/ Math/PI 4))
   3.292 +             (Vector3f. -1 0 0)))
   3.293 +          (doto (Quaternion.)
   3.294 +            (.fromAngleAxis
   3.295 +             (* 1 (/ Math/PI 2))
   3.296 +             (Vector3f. 0 0 1)))))
   3.297 +        top-position (.mult joint-rotation (Vector3f. 8 0 0))
   3.298 +        
   3.299 +        origin (doto
   3.300 +                   (sphere 0.1 :physical? false :color ColorRGBA/Cyan
   3.301 +                           :position top-position))
   3.302 +        top (doto
   3.303 +                (sphere 0.1 :physical? false :color ColorRGBA/Yellow
   3.304 +                        :position top-position)
   3.305 +
   3.306 +              (.addControl
   3.307 +               (RigidBodyControl.
   3.308 +                (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
   3.309 +        bottom (doto
   3.310 +                   (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
   3.311 +                           :position (Vector3f. 0 0 0))
   3.312 +                 (.addControl
   3.313 +               (RigidBodyControl.
   3.314 +                (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
   3.315 +        table (box 10 2 10 :position (Vector3f. 0 -20 0)
   3.316 +                   :color ColorRGBA/Gray :mass 0)
   3.317 +        a (.getControl top RigidBodyControl)
   3.318 +        b (.getControl bottom RigidBodyControl)]
   3.319 +
   3.320 +    (cond
   3.321 +     (= joint :cone)
   3.322 +     
   3.323 +       (doto (ConeJoint.
   3.324 +              a b
   3.325 +              (world-to-local top joint-position)
   3.326 +              (world-to-local bottom joint-position)
   3.327 +              joint-rotation
   3.328 +              joint-rotation
   3.329 +              )
   3.330 +
   3.331 +              
   3.332 +         (.setLimit (* (/ 10) Math/PI)
   3.333 +                    (* (/ 4)  Math/PI)
   3.334 +                    0)))
   3.335 +  [origin top bottom table]))
   3.336 +            
   3.337 +(defn test-joint [joint]
   3.338 +  (let [[origin top bottom floor] (world-setup joint)
   3.339 +        control (.getControl top RigidBodyControl)
   3.340 +        move-up? (atom false)
   3.341 +        move-down? (atom false)
   3.342 +        move-left? (atom false)
   3.343 +        move-right? (atom false)
   3.344 +        roll-left? (atom false)
   3.345 +        roll-right? (atom false)
   3.346 +        timer (atom 0)]
   3.347 +
   3.348 +    (world
   3.349 +     (nodify [top bottom floor origin])
   3.350 +     (merge standard-debug-controls
   3.351 +            {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
   3.352 +             "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
   3.353 +             "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
   3.354 +             "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
   3.355 +             "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
   3.356 +             "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})     
   3.357 +     
   3.358 +     (fn [world]
   3.359 +       (light-up-everything world)
   3.360 +       (enable-debug world)
   3.361 +       (set-gravity world (Vector3f. 0 0 0))
   3.362 +       )
   3.363 +     
   3.364 +     (fn [world _]
   3.365 +       (if (zero? (rem (swap! timer inc) 100))
   3.366 +         (do
   3.367 +         ;;  (println-repl @timer)
   3.368 +           (.attachChild (.getRootNode world)
   3.369 +                       (sphere 0.05 :color ColorRGBA/Yellow
   3.370 +                               :position (.getWorldTranslation top)
   3.371 +                               :physical? false))
   3.372 +           (.attachChild (.getRootNode world)
   3.373 +                         (sphere 0.05 :color ColorRGBA/LightGray
   3.374 +                                 :position (.getWorldTranslation bottom)
   3.375 +                                 :physical? false))))
   3.376 +
   3.377 +       (if @move-up?
   3.378 +         (.applyTorque control
   3.379 +                       (.mult (.getPhysicsRotation control)
   3.380 +                              (Vector3f. 0 0 10))))
   3.381 +       (if @move-down?
   3.382 +         (.applyTorque control
   3.383 +                       (.mult (.getPhysicsRotation control)
   3.384 +                              (Vector3f. 0 0 -10))))
   3.385 +       (if @move-left?
   3.386 +         (.applyTorque control
   3.387 +                       (.mult (.getPhysicsRotation control)
   3.388 +                              (Vector3f. 0 10 0))))
   3.389 +       (if @move-right?
   3.390 +         (.applyTorque control
   3.391 +                       (.mult (.getPhysicsRotation control)
   3.392 +                              (Vector3f. 0 -10 0))))
   3.393 +       (if @roll-left?
   3.394 +         (.applyTorque control
   3.395 +                       (.mult (.getPhysicsRotation control)
   3.396 +                              (Vector3f. -1 0 0))))
   3.397 +       (if @roll-right?
   3.398 +         (.applyTorque control
   3.399 +                       (.mult (.getPhysicsRotation control)
   3.400 +                              (Vector3f. 1 0 0))))))))
   3.401 +
   3.402  
   3.403  
   3.404  #+end_src