changeset 175:0b9ae09eaec3

renamed functions in body
author Robert McIntyre <rlm@mit.edu>
date Sat, 04 Feb 2012 06:47:07 -0700
parents 136349ac6972
children 026f69582022
files org/body.org org/proprioception.org org/test-creature.org
diffstat 3 files changed, 82 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/body.org	Sat Feb 04 06:29:20 2012 -0700
     1.2 +++ b/org/body.org	Sat Feb 04 06:47:07 2012 -0700
     1.3 @@ -5,8 +5,8 @@
     1.4  #+SETUPFILE: ../../aurellem/org/setup.org
     1.5  #+INCLUDE: ../../aurellem/org/level-0.org
     1.6  
     1.7 -* Proprioception
     1.8 -#+name: proprioception
     1.9 +* Making a solid, connected body.
    1.10 +#+name: joints
    1.11  #+begin_src clojure 
    1.12  (ns cortex.body
    1.13    (:use (cortex world util sense))
    1.14 @@ -20,23 +20,9 @@
    1.15     com.jme3.bounding.BoundingBox
    1.16     com.jme3.scene.Node))
    1.17  
    1.18 +(use 'clojure.contrib.def)
    1.19  (cortex.import/mega-import-jme3)
    1.20  
    1.21 -(defn jme-to-blender
    1.22 -  "Convert from JME coordinates to Blender coordinates"
    1.23 -  [#^Vector3f in]
    1.24 -  (Vector3f. (.getX in)
    1.25 -             (- (.getZ in))
    1.26 -             (.getY in)))
    1.27 -
    1.28 -(defn blender-to-jme
    1.29 -  "Convert from Blender coordinates to JME coordinates"
    1.30 -  [#^Vector3f in]
    1.31 -  (Vector3f. (.getX in)
    1.32 -             (.getZ in)
    1.33 -             (- (.getY in))))
    1.34 -
    1.35 -
    1.36  (defn joint-targets
    1.37    "Return the two closest two objects to the joint object, ordered
    1.38    from bottom to top according to the joint's rotation."
    1.39 @@ -65,13 +51,6 @@
    1.40             (take 2 targets))
    1.41            (recur (float (* radius 2))))))))
    1.42  
    1.43 -(defn creature-joints 
    1.44 -  "Return the children of the creature's \"joints\" node."
    1.45 -  [#^Node creature]
    1.46 -  (if-let [joint-node (.getChild creature "joints")]
    1.47 -    (seq (.getChildren joint-node))
    1.48 -    (do (println-repl "could not find JOINTS node") [])))
    1.49 -
    1.50  (defmulti joint-dispatch
    1.51    "Translate blender pseudo-joints into real JME joints."
    1.52    (fn [constraints & _] 
    1.53 @@ -158,7 +137,10 @@
    1.54                   (float twist)))))
    1.55  
    1.56  (defn connect
    1.57 -  "here are some examples:
    1.58 +  "Create a joint between 'obj-a and 'obj-b at the location of
    1.59 +  'joint. The type of joint is determined by the metadata on 'joint.
    1.60 +
    1.61 +   Here are some examples:
    1.62     {:type :point}
    1.63     {:type :hinge  :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
    1.64     (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
    1.65 @@ -192,10 +174,16 @@
    1.66                          joint-rotation))
    1.67        (println-repl "could not find joint meta-data!"))))
    1.68  
    1.69 +(defvar 
    1.70 +  ^{:arglists '([creature])}
    1.71 +  joints
    1.72 +  (sense-nodes "joints")
    1.73 +  "Return the children of the creature's \"joints\" node.")
    1.74  
    1.75 -
    1.76 -
    1.77 -(defn assemble-creature [#^Node pieces joints]
    1.78 +(defn physical!
    1.79 +  "Iterate through the nodes in creature and make them real physical
    1.80 +   objects in the simulation."
    1.81 +  [#^Node creature]
    1.82    (dorun
    1.83     (map
    1.84      (fn [geom]
    1.85 @@ -212,94 +200,38 @@
    1.86                 
    1.87          (.addControl geom physics-control)))
    1.88      (filter #(isa? (class %) Geometry )
    1.89 -            (node-seq pieces))))
    1.90 +            (node-seq creature)))))
    1.91 +
    1.92 +(defn joints!
    1.93 +  "Connect the solid parts of the creature with physical joints. The
    1.94 +   joints are taken from the \"joints\" node in the creature."
    1.95 +  [#^Node creature]
    1.96    (dorun
    1.97     (map
    1.98      (fn [joint]
    1.99 -      (let [[obj-a obj-b] (joint-targets pieces joint)]
   1.100 +      (let [[obj-a obj-b] (joint-targets creature joint)]
   1.101          (connect obj-a obj-b joint)))
   1.102 -    joints))
   1.103 -  pieces)
   1.104 +    (joints creature))))
   1.105  
   1.106 +(defn body!
   1.107 +  "Endow the creature with a physical body connected with joints.  The
   1.108 +   particulars of the joints and the masses of each pody part are
   1.109 +   determined in blender."
   1.110 +  [#^Node creature]
   1.111 +  (physical! creature)
   1.112 +  (joints! creature))
   1.113  
   1.114 -;; TODO implement a function that adds joints in the style of the
   1.115 -;; other sense functions
   1.116  
   1.117  
   1.118  
   1.119 -(import java.awt.image.BufferedImage)
   1.120 -
   1.121 -(defn draw-sprite [image sprite x y color ]
   1.122 -  (dorun
   1.123 -   (for [[u v] sprite]
   1.124 -     (.setRGB image (+ u x) (+ v y) color))))
   1.125 -
   1.126 -(defn view-angle
   1.127 -  "create a debug view of an angle"
   1.128 -  [color]
   1.129 -  (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
   1.130 -        previous (atom [25 25])
   1.131 -        sprite [[0 0] [0 1]
   1.132 -                [0 -1] [-1 0] [1 0]]] 
   1.133 -    (fn [angle]
   1.134 -      (let [angle (float angle)]
   1.135 -        (let [position
   1.136 -              [(+ 25 (int (* 20 (Math/cos angle))))
   1.137 -               (+ 25 (int (* -20 (Math/sin angle))))]]
   1.138 -          (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
   1.139 -          (draw-sprite image sprite (position 0) (position 1) color)
   1.140 -          (reset! previous position))
   1.141 -        image))))
   1.142 -
   1.143 -(defn proprioception-debug-window
   1.144 -  []
   1.145 -  (let [heading (view-angle 0xFF0000)
   1.146 -        pitch (view-angle 0x00FF00)
   1.147 -        roll (view-angle 0xFFFFFF)
   1.148 -        v-heading (view-image)
   1.149 -        v-pitch (view-image)
   1.150 -        v-roll (view-image)
   1.151 -        ]
   1.152 -    (fn [prop-data]
   1.153 -      (dorun
   1.154 -       (map
   1.155 -        (fn [[h p r]]
   1.156 -          (v-heading (heading h))
   1.157 -          (v-pitch (pitch p))
   1.158 -          (v-roll (roll r)))
   1.159 -        prop-data)))))
   1.160 -
   1.161 -
   1.162  #+end_src
   1.163  
   1.164 +#+results: joints
   1.165 +: #'cortex.body/body!
   1.166 +
   1.167  #+results: proprioception
   1.168  : #'cortex.body/proprioception-debug-window
   1.169  
   1.170 -* Motor Control
   1.171 -#+name: motor-control
   1.172 -#+begin_src clojure
   1.173 -(in-ns 'cortex.body)
   1.174 -
   1.175 -;; surprisingly enough, terristerial creatures only move by using
   1.176 -;; torque applied about their joints.  There's not a single straight
   1.177 -;; line of force in the human body at all!  (A straight line of force
   1.178 -;; would correspond to some sort of jet or rocket propulseion.)
   1.179 -
   1.180 -(defn vector-motor-control
   1.181 -  "Create a function that accepts a sequence of Vector3f objects that
   1.182 -   describe the torque to be applied to each part of the body."
   1.183 -  [body]
   1.184 -  (let [nodes (node-seq body)
   1.185 -        controls (keep #(.getControl % RigidBodyControl) nodes)]
   1.186 -    (fn [torques]
   1.187 -      (map #(.applyTorque %1 %2)
   1.188 -           controls torques))))
   1.189 -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   1.190 -#+end_src
   1.191 - 
   1.192 -## note -- might want to add a lower dimensional, discrete version of
   1.193 -## this if it proves useful from a x-modal clustering perspective.
   1.194 -
   1.195  * Examples
   1.196  
   1.197  #+name: test-body
   1.198 @@ -1032,8 +964,7 @@
   1.199  
   1.200  * COMMENT generate Source
   1.201  #+begin_src clojure :tangle ../src/cortex/body.clj
   1.202 -<<proprioception>>
   1.203 -<<motor-control>>
   1.204 +<<joints>>
   1.205  #+end_src
   1.206  
   1.207  #+begin_src clojure :tangle ../src/cortex/test/body.clj
     2.1 --- a/org/proprioception.org	Sat Feb 04 06:29:20 2012 -0700
     2.2 +++ b/org/proprioception.org	Sat Feb 04 06:47:07 2012 -0700
     2.3 @@ -18,12 +18,6 @@
     2.4    (:import com.jme3.scene.Node)
     2.5    (:import (com.jme3.math Vector3f Quaternion)))
     2.6  
     2.7 -(defvar 
     2.8 -  ^{:arglists '([creature])}
     2.9 -  joints
    2.10 -  (sense-nodes "joints")
    2.11 -  "Return the children of the creature's \"joints\" node.")
    2.12 -
    2.13  (defn right-handed?
    2.14    "true iff the three vectors form a right handed coordinate
    2.15    system. The three vectors do not have to be normalized or
    2.16 @@ -89,6 +83,50 @@
    2.17                      (joints creature))]
    2.18      (fn []
    2.19        (map #(%) senses))))
    2.20 +
    2.21 +
    2.22 +(import java.awt.image.BufferedImage)
    2.23 +
    2.24 +(defn draw-sprite [image sprite x y color ]
    2.25 +  (dorun
    2.26 +   (for [[u v] sprite]
    2.27 +     (.setRGB image (+ u x) (+ v y) color))))
    2.28 +
    2.29 +(defn view-angle
    2.30 +  "create a debug view of an angle"
    2.31 +  [color]
    2.32 +  (let [image (BufferedImage. 50 50 BufferedImage/TYPE_INT_RGB)
    2.33 +        previous (atom [25 25])
    2.34 +        sprite [[0 0] [0 1]
    2.35 +                [0 -1] [-1 0] [1 0]]] 
    2.36 +    (fn [angle]
    2.37 +      (let [angle (float angle)]
    2.38 +        (let [position
    2.39 +              [(+ 25 (int (* 20 (Math/cos angle))))
    2.40 +               (+ 25 (int (* -20 (Math/sin angle))))]]
    2.41 +          (draw-sprite image sprite (@previous 0) (@previous 1) 0x000000)
    2.42 +          (draw-sprite image sprite (position 0) (position 1) color)
    2.43 +          (reset! previous position))
    2.44 +        image))))
    2.45 +
    2.46 +(defn proprioception-debug-window
    2.47 +  []
    2.48 +  (let [heading (view-angle 0xFF0000)
    2.49 +        pitch (view-angle 0x00FF00)
    2.50 +        roll (view-angle 0xFFFFFF)
    2.51 +        v-heading (view-image)
    2.52 +        v-pitch (view-image)
    2.53 +        v-roll (view-image)
    2.54 +        ]
    2.55 +    (fn [prop-data]
    2.56 +      (dorun
    2.57 +       (map
    2.58 +        (fn [[h p r]]
    2.59 +          (v-heading (heading h))
    2.60 +          (v-pitch (pitch p))
    2.61 +          (v-roll (roll r)))
    2.62 +        prop-data)))))
    2.63 +
    2.64  #+end_src
    2.65  
    2.66  * COMMENT generate source
     3.1 --- a/org/test-creature.org	Sat Feb 04 06:29:20 2012 -0700
     3.2 +++ b/org/test-creature.org	Sat Feb 04 06:47:07 2012 -0700
     3.3 @@ -71,15 +71,6 @@
     3.4       (.registerLoader BlenderModelLoader (into-array String ["blend"])))
     3.5     model))
     3.6  
     3.7 -(declare blender-creature)
     3.8 -
     3.9 -(defn blender-creature
    3.10 -  "Return a creature with all joints in place."
    3.11 -  [blender-path]
    3.12 -  (let [model (load-blender-model blender-path)
    3.13 -        joints (creature-joints model)]
    3.14 -    (assemble-creature model joints)))
    3.15 -
    3.16  (def hand "Models/creature1/one.blend")
    3.17  
    3.18  (def worm "Models/creature1/try-again.blend")
    3.19 @@ -178,7 +169,9 @@
    3.20          (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
    3.21          z-axis
    3.22          (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
    3.23 -        creature (blender-creature thing)
    3.24 +        creature (doto 
    3.25 +                     (load-blender-model thing)
    3.26 +                   (body!))
    3.27          touch-nerves (touch creature)
    3.28          touch-debug-windows (map (fn [_] (debug-touch-window)) touch-nerves)
    3.29          vision-data (vision! creature)