diff org/sense.org @ 197:16cbce075a0b

reorganizing for website
author Robert McIntyre <rlm@mit.edu>
date Sun, 05 Feb 2012 06:55:41 -0700
parents 22548d48cc85
children fc0bf33bded2
line wrap: on
line diff
     1.1 --- a/org/sense.org	Sun Feb 05 00:21:23 2012 -0700
     1.2 +++ b/org/sense.org	Sun Feb 05 06:55:41 2012 -0700
     1.3 @@ -1,4 +1,4 @@
     1.4 -#+title: General sense/effector utilities
     1.5 +#+title: General Sense/Effector Utilities
     1.6  #+author: Robert McIntyre
     1.7  #+email: rlm@mit.edu
     1.8  #+description: sensory utilities
     1.9 @@ -6,8 +6,9 @@
    1.10  #+SETUPFILE: ../../aurellem/org/setup.org
    1.11  #+INCLUDE: ../../aurellem/org/level-0.org
    1.12  
    1.13 -#+name: sense
    1.14 -#+begin_src clojure 
    1.15 +* Namespace/Imports
    1.16 +#+name header
    1.17 +#+begin_src clojure
    1.18  (ns cortex.sense
    1.19    "Here are functions useful in the construction of two or more
    1.20     sensors/effectors."
    1.21 @@ -21,7 +22,11 @@
    1.22    (:import (com.jme3.scene Node Spatial))
    1.23    (:import com.jme3.scene.control.AbstractControl)
    1.24    (:import (com.jme3.math Quaternion Vector3f)))
    1.25 +#+end_src
    1.26  
    1.27 +* Blender Utilities
    1.28 +#+name: blender
    1.29 +#+begin_src clojure
    1.30  (defn meta-data
    1.31    "Get the meta-data for a node created with blender."
    1.32    [blender-node key]
    1.33 @@ -29,44 +34,30 @@
    1.34      (.findValue data key)
    1.35      nil))
    1.36  
    1.37 -(defn closest-node
    1.38 -  "Return the node in creature which is closest to the given node."
    1.39 -  [#^Node creature #^Node eye]
    1.40 -  (loop [radius (float 0.01)]
    1.41 -    (let [results (CollisionResults.)]
    1.42 -      (.collideWith
    1.43 -       creature
    1.44 -       (BoundingBox. (.getWorldTranslation eye)
    1.45 -                     radius radius radius)
    1.46 -       results)
    1.47 -      (if-let [target (first results)]
    1.48 -        (.getGeometry target)
    1.49 -        (recur (float (* 2 radius)))))))
    1.50 +(defn jme-to-blender
    1.51 +  "Convert from JME coordinates to Blender coordinates"
    1.52 +  [#^Vector3f in]
    1.53 +  (Vector3f. (.getX in)
    1.54 +             (- (.getZ in))
    1.55 +             (.getY in)))
    1.56  
    1.57 -(defn bind-sense
    1.58 -  "Bind the sense to the Spatial such that it will maintain its
    1.59 -   current position relative to the Spatial no matter how the spatial
    1.60 -   moves. 'sense can be either a Camera or Listener object."
    1.61 -  [#^Spatial obj sense]
    1.62 -  (let [sense-offset (.subtract (.getLocation sense)
    1.63 -                                (.getWorldTranslation obj))
    1.64 -        initial-sense-rotation (Quaternion. (.getRotation sense))
    1.65 -        base-anti-rotation (.inverse (.getWorldRotation obj))]
    1.66 -    (.addControl
    1.67 -     obj
    1.68 -     (proxy [AbstractControl] []
    1.69 -       (controlUpdate [tpf]
    1.70 -         (let [total-rotation
    1.71 -               (.mult base-anti-rotation (.getWorldRotation obj))]
    1.72 -           (.setLocation
    1.73 -            sense
    1.74 -            (.add
    1.75 -             (.mult total-rotation sense-offset)
    1.76 -             (.getWorldTranslation obj)))
    1.77 -           (.setRotation
    1.78 -            sense
    1.79 -            (.mult total-rotation initial-sense-rotation))))
    1.80 -       (controlRender [_ _])))))
    1.81 +(defn blender-to-jme
    1.82 +  "Convert from Blender coordinates to JME coordinates"
    1.83 +  [#^Vector3f in]
    1.84 +  (Vector3f. (.getX in)
    1.85 +             (.getZ in)
    1.86 +             (- (.getY in))))
    1.87 +#+end_src
    1.88 +
    1.89 +* Topology Related stuff
    1.90 +#+name: topology
    1.91 +#+begin_src clojure
    1.92 +(defn load-image
    1.93 +  "Load an image as a BufferedImage using the asset-manager system." 
    1.94 +  [asset-relative-path]
    1.95 +  (ImageToAwt/convert
    1.96 +   (.getImage (.loadTexture (asset-manager) asset-relative-path))
    1.97 +   false false 0))
    1.98  
    1.99  (def white 0xFFFFFF)
   1.100  
   1.101 @@ -171,6 +162,50 @@
   1.102                    squeezed))]
   1.103          relocated)))
   1.104  
   1.105 +#+end_src
   1.106 +
   1.107 +* Node level stuff
   1.108 +#+name: node
   1.109 +#+begin_src clojure
   1.110 +(defn closest-node
   1.111 +  "Return the node in creature which is closest to the given node."
   1.112 +  [#^Node creature #^Node eye]
   1.113 +  (loop [radius (float 0.01)]
   1.114 +    (let [results (CollisionResults.)]
   1.115 +      (.collideWith
   1.116 +       creature
   1.117 +       (BoundingBox. (.getWorldTranslation eye)
   1.118 +                     radius radius radius)
   1.119 +       results)
   1.120 +      (if-let [target (first results)]
   1.121 +        (.getGeometry target)
   1.122 +        (recur (float (* 2 radius)))))))
   1.123 +
   1.124 +(defn bind-sense
   1.125 +  "Bind the sense to the Spatial such that it will maintain its
   1.126 +   current position relative to the Spatial no matter how the spatial
   1.127 +   moves. 'sense can be either a Camera or Listener object."
   1.128 +  [#^Spatial obj sense]
   1.129 +  (let [sense-offset (.subtract (.getLocation sense)
   1.130 +                                (.getWorldTranslation obj))
   1.131 +        initial-sense-rotation (Quaternion. (.getRotation sense))
   1.132 +        base-anti-rotation (.inverse (.getWorldRotation obj))]
   1.133 +    (.addControl
   1.134 +     obj
   1.135 +     (proxy [AbstractControl] []
   1.136 +       (controlUpdate [tpf]
   1.137 +         (let [total-rotation
   1.138 +               (.mult base-anti-rotation (.getWorldRotation obj))]
   1.139 +           (.setLocation
   1.140 +            sense
   1.141 +            (.add
   1.142 +             (.mult total-rotation sense-offset)
   1.143 +             (.getWorldTranslation obj)))
   1.144 +           (.setRotation
   1.145 +            sense
   1.146 +            (.mult total-rotation initial-sense-rotation))))
   1.147 +       (controlRender [_ _])))))
   1.148 +
   1.149  (defn world-to-local
   1.150    "Convert the world coordinates into coordinates relative to the 
   1.151     object (i.e. local coordinates), taking into account the rotation
   1.152 @@ -193,40 +228,22 @@
   1.153      (if-let [sense-node (.getChild creature parent-name)]
   1.154        (seq (.getChildren sense-node))
   1.155        (do (println-repl "could not find" parent-name "node") []))))
   1.156 +#+end_src
   1.157  
   1.158 -(defn load-image
   1.159 -  "Load an image as a BufferedImage using the asset-manager system." 
   1.160 -  [asset-relative-path]
   1.161 -  (ImageToAwt/convert
   1.162 -   (.getImage (.loadTexture (asset-manager) asset-relative-path))
   1.163 -   false false 0))
   1.164 -
   1.165 -(defn jme-to-blender
   1.166 -  "Convert from JME coordinates to Blender coordinates"
   1.167 -  [#^Vector3f in]
   1.168 -  (Vector3f. (.getX in)
   1.169 -             (- (.getZ in))
   1.170 -             (.getY in)))
   1.171 -
   1.172 -(defn blender-to-jme
   1.173 -  "Convert from Blender coordinates to JME coordinates"
   1.174 -  [#^Vector3f in]
   1.175 -  (Vector3f. (.getX in)
   1.176 -             (.getZ in)
   1.177 -             (- (.getY in))))
   1.178 -
   1.179 -
   1.180 +* Viewing Senses
   1.181 +#+name view-senses
   1.182 +#+begin_src clojure
   1.183  (defn view-sense 
   1.184 -  "Take a function that produces a BufferedImage from some sense data
   1.185 -   and return a function which takes a list of sense and displays
   1.186 -   those images in multiple JFrames."
   1.187 +  "Take a kernel that produces a BufferedImage from some sense data
   1.188 +   and return a function which takes a list of sense data, uses the
   1.189 +   kernem to convert to images, and displays those images, each in
   1.190 +   its own JFrame."
   1.191    [sense-display-kernel]
   1.192 -  (let
   1.193 -      [windows (atom [])]
   1.194 +  (let [windows (atom [])]
   1.195      (fn [data]
   1.196        (if (> (count data) (count @windows))
   1.197 -        (reset! windows (map (fn [_] (view-image))
   1.198 -                             (range (count data)))))
   1.199 +        (reset! 
   1.200 +         windows (map (fn [_] (view-image)) (range (count data)))))
   1.201        (dorun
   1.202         (map
   1.203          (fn [display datum]
   1.204 @@ -234,18 +251,19 @@
   1.205          @windows data)))))
   1.206  
   1.207  (defn gray
   1.208 -  "Create a gray RGB pixel with R, G, and B set to 'num"
   1.209 +  "Create a gray RGB pixel with R, G, and B set to num. num must be
   1.210 +   between 0 and 255."
   1.211    [num]
   1.212    (+ num
   1.213       (bit-shift-left num 8)
   1.214       (bit-shift-left num 16)))
   1.215  #+end_src
   1.216  
   1.217 -#+results: sense
   1.218 -: #'cortex.sense/gray
   1.219 -
   1.220 -
   1.221  * COMMENT generate source
   1.222  #+begin_src clojure :tangle ../src/cortex/sense.clj
   1.223 -<<sense>>
   1.224 +<<header>>
   1.225 +<<blender>>
   1.226 +<<topology>>
   1.227 +<<node>>
   1.228 +<<view-senses>>
   1.229  #+end_src