annotate org/test-creature.org @ 125:3d65633dd736

organizing priorities for winston
author Robert McIntyre <rlm@mit.edu>
date Tue, 24 Jan 2012 18:20:57 -0700
parents 90154bd674e9
children 0efe6f04bc26
rev   line source
rlm@73 1 #+title: First attempt at a creature!
rlm@73 2 #+author: Robert McIntyre
rlm@73 3 #+email: rlm@mit.edu
rlm@73 4 #+description:
rlm@73 5 #+keywords: simulation, jMonkeyEngine3, clojure
rlm@73 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@73 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@73 8
rlm@124 9 * ideas
rlm@125 10
rlm@125 11 ** have to get done before winston
rlm@124 12 - [ ] write an explination for why greyscale bitmaps for senses is appropiate
rlm@125 13 - [ ] muscle control
rlm@125 14 - [ ] proprioception sensor map in the style of the other senses
rlm@125 15 - [ ] enable greyscale bitmaps for touch
rlm@125 16 - [ ] refactor integration code to distribute to each of the senses
rlm@125 17 - [ ] create video showing all the senses for Winston
rlm@125 18 - [ ] write summary for Winston
rlm@125 19 - [ ] project proposals for Winston
rlm@125 20 - [ ] send winston package
rlm@125 21
rlm@125 22 ** would be cool to get done before winston
rlm@124 23 - [ ] use sawfish to auto-tile sense windows
rlm@124 24 - [ ] sawfish keybinding to automatically delete all sense windows
rlm@125 25 - [ ] proof of concept C sense manipulation
rlm@125 26 - [ ] proof of concept GPU sense manipulation
rlm@125 27 - [ ] fourier view of sound
rlm@125 28 - [ ] directly change the UV-pixels to show sensor activation
rlm@125 29
rlm@125 30 ** don't have to get done before winston
rlm@125 31 - [ ] write tests for integration
rlm@125 32 - [ ] usertime/gametime clock HUD display
rlm@125 33 - [ ] find papers for each of the senses justifying my own representation
rlm@125 34 - [ ] show sensor maps in HUD display?
rlm@124 35
rlm@99 36
rlm@73 37 * Intro
rlm@73 38 So far, I've made the following senses --
rlm@73 39 - Vision
rlm@73 40 - Hearing
rlm@73 41 - Touch
rlm@73 42 - Proprioception
rlm@73 43
rlm@73 44 And one effector:
rlm@73 45 - Movement
rlm@73 46
rlm@73 47 However, the code so far has only enabled these senses, but has not
rlm@73 48 actually implemented them. For example, there is still a lot of work
rlm@73 49 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 50 simulation that can be moved around and see the world from different
rlm@73 51 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 52 for the visual input, and I need to determine how/wether to
rlm@73 53 disceritise the visual input.
rlm@73 54
rlm@73 55 I also want to be able to visualize both the sensors and the
rlm@104 56 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 57 first attempt at bringing everything together.
rlm@73 58
rlm@73 59 * The creature's body
rlm@73 60
rlm@73 61 Still going to do an eve-like body in blender, but due to problems
rlm@104 62 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 63 the connecting here in clojure code, using the names of the individual
rlm@73 64 components and trial and error. Later, I'll maybe make some sort of
rlm@73 65 creature-building modifications to blender that support whatever
rlm@73 66 discreitized senses I'm going to make.
rlm@73 67
rlm@73 68 #+name: body-1
rlm@73 69 #+begin_src clojure
rlm@73 70 (ns cortex.silly
rlm@73 71 "let's play!"
rlm@73 72 {:author "Robert McIntyre"})
rlm@73 73
rlm@73 74 ;; TODO remove this!
rlm@73 75 (require 'cortex.import)
rlm@73 76 (cortex.import/mega-import-jme3)
rlm@73 77 (use '(cortex world util body hearing touch vision))
rlm@73 78
rlm@73 79 (rlm.rlm-commands/help)
rlm@99 80 (import java.awt.image.BufferedImage)
rlm@99 81 (import javax.swing.JPanel)
rlm@99 82 (import javax.swing.SwingUtilities)
rlm@99 83 (import java.awt.Dimension)
rlm@99 84 (import javax.swing.JFrame)
rlm@99 85 (import java.awt.Dimension)
rlm@106 86 (import com.aurellem.capture.RatchetTimer)
rlm@99 87 (declare joint-create)
rlm@108 88 (use 'clojure.contrib.def)
rlm@73 89
rlm@100 90 (defn points->image
rlm@100 91 "Take a sparse collection of points and visuliaze it as a
rlm@100 92 BufferedImage."
rlm@102 93
rlm@102 94 ;; TODO maybe parallelize this since it's easy
rlm@102 95
rlm@100 96 [points]
rlm@106 97 (if (empty? points)
rlm@106 98 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
rlm@106 99 (let [xs (vec (map first points))
rlm@106 100 ys (vec (map second points))
rlm@106 101 x0 (apply min xs)
rlm@106 102 y0 (apply min ys)
rlm@106 103 width (- (apply max xs) x0)
rlm@106 104 height (- (apply max ys) y0)
rlm@106 105 image (BufferedImage. (inc width) (inc height)
rlm@119 106 BufferedImage/TYPE_INT_RGB)]
rlm@118 107 (dorun
rlm@118 108 (for [x (range (.getWidth image))
rlm@118 109 y (range (.getHeight image))]
rlm@119 110 (.setRGB image x y 0xFF0000)))
rlm@106 111 (dorun
rlm@106 112 (for [index (range (count points))]
rlm@106 113 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
rlm@106 114
rlm@106 115 image)))
rlm@100 116
rlm@101 117 (defn average [coll]
rlm@101 118 (/ (reduce + coll) (count coll)))
rlm@101 119
rlm@101 120 (defn collapse-1d
rlm@101 121 "One dimensional analogue of collapse"
rlm@101 122 [center line]
rlm@101 123 (let [length (count line)
rlm@101 124 num-above (count (filter (partial < center) line))
rlm@101 125 num-below (- length num-above)]
rlm@101 126 (range (- center num-below)
rlm@115 127 (+ center num-above))))
rlm@99 128
rlm@99 129 (defn collapse
rlm@99 130 "Take a set of pairs of integers and collapse them into a
rlm@99 131 contigous bitmap."
rlm@99 132 [points]
rlm@108 133 (if (empty? points) []
rlm@108 134 (let
rlm@108 135 [num-points (count points)
rlm@108 136 center (vector
rlm@108 137 (int (average (map first points)))
rlm@108 138 (int (average (map first points))))
rlm@108 139 flattened
rlm@108 140 (reduce
rlm@108 141 concat
rlm@108 142 (map
rlm@108 143 (fn [column]
rlm@108 144 (map vector
rlm@108 145 (map first column)
rlm@108 146 (collapse-1d (second center)
rlm@108 147 (map second column))))
rlm@108 148 (partition-by first (sort-by first points))))
rlm@108 149 squeezed
rlm@108 150 (reduce
rlm@108 151 concat
rlm@108 152 (map
rlm@108 153 (fn [row]
rlm@108 154 (map vector
rlm@108 155 (collapse-1d (first center)
rlm@108 156 (map first row))
rlm@108 157 (map second row)))
rlm@108 158 (partition-by second (sort-by second flattened))))
rlm@108 159 relocate
rlm@108 160 (let [min-x (apply min (map first squeezed))
rlm@108 161 min-y (apply min (map second squeezed))]
rlm@108 162 (map (fn [[x y]]
rlm@108 163 [(- x min-x)
rlm@108 164 (- y min-y)])
rlm@108 165 squeezed))]
rlm@115 166 relocate)))
rlm@83 167
rlm@83 168 (defn load-bullet []
rlm@84 169 (let [sim (world (Node.) {} no-op no-op)]
rlm@102 170 (doto sim
rlm@102 171 (.enqueue
rlm@102 172 (fn []
rlm@102 173 (.stop sim)))
rlm@102 174 (.start))))
rlm@83 175
rlm@73 176 (defn load-blender-model
rlm@73 177 "Load a .blend file using an asset folder relative path."
rlm@73 178 [^String model]
rlm@73 179 (.loadModel
rlm@73 180 (doto (asset-manager)
rlm@73 181 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 182 model))
rlm@73 183
rlm@74 184 (defn meta-data [blender-node key]
rlm@74 185 (if-let [data (.getUserData blender-node "properties")]
rlm@74 186 (.findValue data key)
rlm@74 187 nil))
rlm@73 188
rlm@78 189 (defn blender-to-jme
rlm@78 190 "Convert from Blender coordinates to JME coordinates"
rlm@78 191 [#^Vector3f in]
rlm@78 192 (Vector3f. (.getX in)
rlm@78 193 (.getZ in)
rlm@78 194 (- (.getY in))))
rlm@74 195
rlm@79 196 (defn jme-to-blender
rlm@79 197 "Convert from JME coordinates to Blender coordinates"
rlm@79 198 [#^Vector3f in]
rlm@79 199 (Vector3f. (.getX in)
rlm@79 200 (- (.getZ in))
rlm@79 201 (.getY in)))
rlm@79 202
rlm@78 203 (defn joint-targets
rlm@78 204 "Return the two closest two objects to the joint object, ordered
rlm@78 205 from bottom to top according to the joint's rotation."
rlm@78 206 [#^Node parts #^Node joint]
rlm@78 207 (loop [radius (float 0.01)]
rlm@78 208 (let [results (CollisionResults.)]
rlm@78 209 (.collideWith
rlm@78 210 parts
rlm@78 211 (BoundingBox. (.getWorldTranslation joint)
rlm@78 212 radius radius radius)
rlm@78 213 results)
rlm@78 214 (let [targets
rlm@78 215 (distinct
rlm@78 216 (map #(.getGeometry %) results))]
rlm@78 217 (if (>= (count targets) 2)
rlm@78 218 (sort-by
rlm@79 219 #(let [v
rlm@79 220 (jme-to-blender
rlm@79 221 (.mult
rlm@79 222 (.inverse (.getWorldRotation joint))
rlm@79 223 (.subtract (.getWorldTranslation %)
rlm@79 224 (.getWorldTranslation joint))))]
rlm@79 225 (println-repl (.getName %) ":" v)
rlm@79 226 (.dot (Vector3f. 1 1 1)
rlm@79 227 v))
rlm@78 228 (take 2 targets))
rlm@78 229 (recur (float (* radius 2))))))))
rlm@74 230
rlm@87 231 (defn world-to-local
rlm@87 232 "Convert the world coordinates into coordinates relative to the
rlm@87 233 object (i.e. local coordinates), taking into account the rotation
rlm@87 234 of object."
rlm@87 235 [#^Spatial object world-coordinate]
rlm@87 236 (let [out (Vector3f.)]
rlm@88 237 (.worldToLocal object world-coordinate out) out))
rlm@87 238
rlm@96 239 (defn local-to-world
rlm@96 240 "Convert the local coordinates into coordinates into world relative
rlm@96 241 coordinates"
rlm@96 242 [#^Spatial object local-coordinate]
rlm@96 243 (let [world-coordinate (Vector3f.)]
rlm@96 244 (.localToWorld object local-coordinate world-coordinate)
rlm@96 245 world-coordinate))
rlm@96 246
rlm@87 247 (defmulti joint-dispatch
rlm@87 248 "Translate blender pseudo-joints into real JME joints."
rlm@88 249 (fn [constraints & _]
rlm@87 250 (:type constraints)))
rlm@87 251
rlm@87 252 (defmethod joint-dispatch :point
rlm@87 253 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 254 (println-repl "creating POINT2POINT joint")
rlm@87 255 (Point2PointJoint.
rlm@87 256 control-a
rlm@87 257 control-b
rlm@87 258 pivot-a
rlm@87 259 pivot-b))
rlm@87 260
rlm@87 261 (defmethod joint-dispatch :hinge
rlm@87 262 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 263 (println-repl "creating HINGE joint")
rlm@87 264 (let [axis
rlm@87 265 (if-let
rlm@87 266 [axis (:axis constraints)]
rlm@87 267 axis
rlm@87 268 Vector3f/UNIT_X)
rlm@87 269 [limit-1 limit-2] (:limit constraints)
rlm@87 270 hinge-axis
rlm@87 271 (.mult
rlm@87 272 rotation
rlm@87 273 (blender-to-jme axis))]
rlm@87 274 (doto
rlm@87 275 (HingeJoint.
rlm@87 276 control-a
rlm@87 277 control-b
rlm@87 278 pivot-a
rlm@87 279 pivot-b
rlm@87 280 hinge-axis
rlm@87 281 hinge-axis)
rlm@87 282 (.setLimit limit-1 limit-2))))
rlm@87 283
rlm@87 284 (defmethod joint-dispatch :cone
rlm@87 285 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 286 (let [limit-xz (:limit-xz constraints)
rlm@87 287 limit-xy (:limit-xy constraints)
rlm@87 288 twist (:twist constraints)]
rlm@87 289
rlm@87 290 (println-repl "creating CONE joint")
rlm@87 291 (println-repl rotation)
rlm@87 292 (println-repl
rlm@87 293 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 294 (println-repl
rlm@87 295 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 296 (println-repl
rlm@87 297 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 298 (doto
rlm@87 299 (ConeJoint.
rlm@87 300 control-a
rlm@87 301 control-b
rlm@87 302 pivot-a
rlm@87 303 pivot-b
rlm@87 304 rotation
rlm@87 305 rotation)
rlm@87 306 (.setLimit (float limit-xz)
rlm@87 307 (float limit-xy)
rlm@87 308 (float twist)))))
rlm@87 309
rlm@88 310 (defn connect
rlm@87 311 "here are some examples:
rlm@87 312 {:type :point}
rlm@87 313 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 314 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 315
rlm@89 316 {:type :cone :limit-xz 0]
rlm@89 317 :limit-xy 0]
rlm@89 318 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 319 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 320 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 321 control-b (.getControl obj-b RigidBodyControl)
rlm@87 322 joint-center (.getWorldTranslation joint)
rlm@87 323 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 324 pivot-a (world-to-local obj-a joint-center)
rlm@87 325 pivot-b (world-to-local obj-b joint-center)]
rlm@89 326
rlm@87 327 (if-let [constraints
rlm@87 328 (map-vals
rlm@87 329 eval
rlm@87 330 (read-string
rlm@87 331 (meta-data joint "joint")))]
rlm@89 332 ;; A side-effect of creating a joint registers
rlm@89 333 ;; it with both physics objects which in turn
rlm@89 334 ;; will register the joint with the physics system
rlm@89 335 ;; when the simulation is started.
rlm@87 336 (do
rlm@87 337 (println-repl "creating joint between"
rlm@87 338 (.getName obj-a) "and" (.getName obj-b))
rlm@87 339 (joint-dispatch constraints
rlm@87 340 control-a control-b
rlm@87 341 pivot-a pivot-b
rlm@87 342 joint-rotation))
rlm@87 343 (println-repl "could not find joint meta-data!"))))
rlm@87 344
rlm@78 345 (defn assemble-creature [#^Node pieces joints]
rlm@78 346 (dorun
rlm@78 347 (map
rlm@78 348 (fn [geom]
rlm@78 349 (let [physics-control
rlm@78 350 (RigidBodyControl.
rlm@78 351 (HullCollisionShape.
rlm@78 352 (.getMesh geom))
rlm@78 353 (if-let [mass (meta-data geom "mass")]
rlm@78 354 (do
rlm@78 355 (println-repl
rlm@78 356 "setting" (.getName geom) "mass to" (float mass))
rlm@78 357 (float mass))
rlm@78 358 (float 1)))]
rlm@78 359
rlm@78 360 (.addControl geom physics-control)))
rlm@78 361 (filter #(isa? (class %) Geometry )
rlm@78 362 (node-seq pieces))))
rlm@78 363 (dorun
rlm@78 364 (map
rlm@78 365 (fn [joint]
rlm@78 366 (let [[obj-a obj-b]
rlm@78 367 (joint-targets pieces joint)]
rlm@88 368 (connect obj-a obj-b joint)))
rlm@78 369 joints))
rlm@78 370 pieces)
rlm@74 371
rlm@116 372 (declare blender-creature)
rlm@74 373
rlm@78 374 (def hand "Models/creature1/one.blend")
rlm@74 375
rlm@78 376 (def worm "Models/creature1/try-again.blend")
rlm@78 377
rlm@90 378 (def touch "Models/creature1/touch.blend")
rlm@90 379
rlm@90 380 (defn worm-model [] (load-blender-model worm))
rlm@90 381
rlm@80 382 (defn x-ray [#^ColorRGBA color]
rlm@80 383 (doto (Material. (asset-manager)
rlm@80 384 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 385 (.setColor "Color" color)
rlm@80 386 (-> (.getAdditionalRenderState)
rlm@80 387 (.setDepthTest false))))
rlm@80 388
rlm@91 389 (defn colorful []
rlm@91 390 (.getChild (worm-model) "worm-21"))
rlm@90 391
rlm@90 392 (import jme3tools.converters.ImageToAwt)
rlm@90 393
rlm@90 394 (import ij.ImagePlus)
rlm@90 395
rlm@108 396 ;; Every Mesh has many triangles, each with its own index.
rlm@108 397 ;; Every vertex has its own index as well.
rlm@90 398
rlm@108 399 (defn tactile-sensor-image
rlm@110 400 "Return the touch-sensor distribution image in BufferedImage format,
rlm@110 401 or nil if it does not exist."
rlm@91 402 [#^Geometry obj]
rlm@110 403 (if-let [image-path (meta-data obj "touch")]
rlm@110 404 (ImageToAwt/convert
rlm@110 405 (.getImage
rlm@110 406 (.loadTexture
rlm@110 407 (asset-manager)
rlm@110 408 image-path))
rlm@110 409 false false 0)))
rlm@110 410
rlm@91 411 (import ij.process.ImageProcessor)
rlm@91 412 (import java.awt.image.BufferedImage)
rlm@91 413
rlm@92 414 (def white -1)
rlm@94 415
rlm@91 416 (defn filter-pixels
rlm@108 417 "List the coordinates of all pixels matching pred, within the bounds
rlm@108 418 provided. Bounds -> [x0 y0 width height]"
rlm@92 419 {:author "Dylan Holmes"}
rlm@108 420 ([pred #^BufferedImage image]
rlm@108 421 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
rlm@108 422 ([pred #^BufferedImage image [x0 y0 width height]]
rlm@108 423 ((fn accumulate [x y matches]
rlm@108 424 (cond
rlm@108 425 (>= y (+ height y0)) matches
rlm@108 426 (>= x (+ width x0)) (recur 0 (inc y) matches)
rlm@108 427 (pred (.getRGB image x y))
rlm@108 428 (recur (inc x) y (conj matches [x y]))
rlm@108 429 :else (recur (inc x) y matches)))
rlm@108 430 x0 y0 [])))
rlm@91 431
rlm@91 432 (defn white-coordinates
rlm@108 433 "Coordinates of all the white pixels in a subset of the image."
rlm@112 434 ([#^BufferedImage image bounds]
rlm@112 435 (filter-pixels #(= % white) image bounds))
rlm@112 436 ([#^BufferedImage image]
rlm@112 437 (filter-pixels #(= % white) image)))
rlm@108 438
rlm@108 439 (defn triangle
rlm@112 440 "Get the triangle specified by triangle-index from the mesh within
rlm@112 441 bounds."
rlm@108 442 [#^Mesh mesh triangle-index]
rlm@108 443 (let [scratch (Triangle.)]
rlm@108 444 (.getTriangle mesh triangle-index scratch)
rlm@108 445 scratch))
rlm@108 446
rlm@108 447 (defn triangle-vertex-indices
rlm@108 448 "Get the triangle vertex indices of a given triangle from a given
rlm@108 449 mesh."
rlm@108 450 [#^Mesh mesh triangle-index]
rlm@108 451 (let [indices (int-array 3)]
rlm@108 452 (.getTriangle mesh triangle-index indices)
rlm@108 453 (vec indices)))
rlm@108 454
rlm@108 455 (defn vertex-UV-coord
rlm@108 456 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@108 457 [#^Mesh mesh vertex-index]
rlm@108 458 (let [UV-buffer
rlm@108 459 (.getData
rlm@108 460 (.getBuffer
rlm@108 461 mesh
rlm@108 462 VertexBuffer$Type/TexCoord))]
rlm@108 463 [(.get UV-buffer (* vertex-index 2))
rlm@108 464 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@108 465
rlm@108 466 (defn triangle-UV-coord
rlm@108 467 "Get the uv-cooridnates of the triangle's verticies."
rlm@108 468 [#^Mesh mesh width height triangle-index]
rlm@108 469 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@108 470 (map (partial vertex-UV-coord mesh)
rlm@108 471 (triangle-vertex-indices mesh triangle-index))))
rlm@91 472
rlm@102 473 (defn same-side?
rlm@102 474 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 475 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 476 [p1 p2 ref p]
rlm@91 477 (<=
rlm@91 478 0
rlm@91 479 (.dot
rlm@91 480 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 481 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 482
rlm@108 483 (defn triangle-seq [#^Triangle tri]
rlm@108 484 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@108 485
rlm@108 486 (defn vector3f-seq [#^Vector3f v]
rlm@108 487 [(.getX v) (.getY v) (.getZ v)])
rlm@108 488
rlm@108 489 (defn inside-triangle?
rlm@108 490 "Is the point inside the triangle?"
rlm@108 491 {:author "Dylan Holmes"}
rlm@108 492 [#^Triangle tri #^Vector3f p]
rlm@108 493 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@108 494 (and
rlm@108 495 (same-side? vert-1 vert-2 vert-3 p)
rlm@108 496 (same-side? vert-2 vert-3 vert-1 p)
rlm@108 497 (same-side? vert-3 vert-1 vert-2 p))))
rlm@108 498
rlm@94 499 (defn triangle->matrix4f
rlm@108 500 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@108 501 contain the vertices of the triangle; the last contains the unit
rlm@108 502 normal of the triangle. The bottom row is filled with 1s."
rlm@94 503 [#^Triangle t]
rlm@94 504 (let [mat (Matrix4f.)
rlm@94 505 [vert-1 vert-2 vert-3]
rlm@94 506 ((comp vec map) #(.get t %) (range 3))
rlm@94 507 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 508 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 509 (dorun
rlm@94 510 (for [row (range 4) col (range 3)]
rlm@94 511 (do
rlm@94 512 (.set mat col row (.get (vertices row)col))
rlm@94 513 (.set mat 3 row 1))))
rlm@94 514 mat))
rlm@94 515
rlm@94 516 (defn triangle-transformation
rlm@94 517 "Returns the affine transformation that converts each vertex in the
rlm@94 518 first triangle into the corresponding vertex in the second
rlm@94 519 triangle."
rlm@94 520 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 521 (.mult
rlm@94 522 (triangle->matrix4f tri-2)
rlm@94 523 (.invert (triangle->matrix4f tri-1))))
rlm@94 524
rlm@108 525 (defn point->vector2f [[u v]]
rlm@108 526 (Vector2f. u v))
rlm@94 527
rlm@94 528 (defn vector2f->vector3f [v]
rlm@94 529 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 530
rlm@94 531 (defn map-triangle [f #^Triangle tri]
rlm@94 532 (Triangle.
rlm@94 533 (f 0 (.get1 tri))
rlm@94 534 (f 1 (.get2 tri))
rlm@94 535 (f 2 (.get3 tri))))
rlm@94 536
rlm@108 537 (defn points->triangle
rlm@108 538 "Convert a list of points into a triangle."
rlm@108 539 [points]
rlm@108 540 (apply #(Triangle. %1 %2 %3)
rlm@108 541 (map (fn [point]
rlm@108 542 (let [point (vec point)]
rlm@108 543 (Vector3f. (get point 0 0)
rlm@108 544 (get point 1 0)
rlm@108 545 (get point 2 0))))
rlm@108 546 (take 3 points))))
rlm@94 547
rlm@108 548 (defn convex-bounds
rlm@108 549 "Dimensions of the smallest integer bounding square of the list of
rlm@108 550 2D verticies in the form: [x y width height]."
rlm@108 551 [uv-verts]
rlm@108 552 (let [xs (map first uv-verts)
rlm@108 553 ys (map second uv-verts)
rlm@108 554 x0 (Math/floor (apply min xs))
rlm@108 555 y0 (Math/floor (apply min ys))
rlm@108 556 x1 (Math/ceil (apply max xs))
rlm@108 557 y1 (Math/ceil (apply max ys))]
rlm@108 558 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@93 559
rlm@106 560 (defn sensors-in-triangle
rlm@107 561 "Find the locations of the touch sensors within a triangle in both
rlm@107 562 UV and gemoetry relative coordinates."
rlm@107 563 [image mesh tri-index]
rlm@107 564 (let [width (.getWidth image)
rlm@108 565 height (.getHeight image)
rlm@108 566 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@108 567 bounds (convex-bounds UV-vertex-coords)
rlm@108 568
rlm@108 569 cutout-triangle (points->triangle UV-vertex-coords)
rlm@108 570 UV-sensor-coords
rlm@108 571 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@108 572 (fn [[u v]] (Vector3f. u v 0)))
rlm@108 573 (white-coordinates image bounds))
rlm@108 574 UV->geometry (triangle-transformation
rlm@108 575 cutout-triangle
rlm@108 576 (triangle mesh tri-index))
rlm@108 577 geometry-sensor-coords
rlm@108 578 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@108 579 UV-sensor-coords)]
rlm@108 580 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@107 581
rlm@108 582 (defn-memo locate-feelers
rlm@94 583 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 584 their positions in geometry-relative coordinates."
rlm@94 585 [#^Geometry geo]
rlm@108 586 (let [mesh (.getMesh geo)
rlm@108 587 num-triangles (.getTriangleCount mesh)]
rlm@108 588 (if-let [image (tactile-sensor-image geo)]
rlm@108 589 (map
rlm@108 590 (partial sensors-in-triangle image mesh)
rlm@108 591 (range num-triangles))
rlm@108 592 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@102 593
rlm@102 594 (use 'clojure.contrib.def)
rlm@102 595
rlm@102 596 (defn-memo touch-topology [#^Gemoetry geo]
rlm@108 597 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@108 598
rlm@108 599 (defn-memo feeler-coordinates [#^Geometry geo]
rlm@108 600 (vec (map :geometry (locate-feelers geo))))
rlm@102 601
rlm@97 602 (defn enable-touch [#^Geometry geo]
rlm@108 603 (let [feeler-coords (feeler-coordinates geo)
rlm@96 604 tris (triangles geo)
rlm@109 605 limit 0.1
rlm@109 606 ;;results (CollisionResults.)
rlm@109 607 ]
rlm@111 608 (if (empty? (touch-topology geo))
rlm@111 609 nil
rlm@111 610 (fn [node]
rlm@111 611 (let [sensor-origins
rlm@111 612 (map
rlm@111 613 #(map (partial local-to-world geo) %)
rlm@111 614 feeler-coords)
rlm@111 615 triangle-normals
rlm@111 616 (map (partial get-ray-direction geo)
rlm@111 617 tris)
rlm@111 618 rays
rlm@111 619 (flatten
rlm@111 620 (map (fn [origins norm]
rlm@111 621 (map #(doto (Ray. % norm)
rlm@97 622 (.setLimit limit)) origins))
rlm@111 623 sensor-origins triangle-normals))]
rlm@111 624 (vector
rlm@111 625 (touch-topology geo)
rlm@111 626 (vec
rlm@111 627 (for [ray rays]
rlm@111 628 (do
rlm@111 629 (let [results (CollisionResults.)]
rlm@111 630 (.collideWith node ray results)
rlm@111 631 (let [touch-objects
rlm@111 632 (set
rlm@111 633 (filter #(not (= geo %))
rlm@111 634 (map #(.getGeometry %) results)))]
rlm@111 635 (if (> (count touch-objects) 0)
rlm@111 636 1 0))))))))))))
rlm@111 637
rlm@111 638 (defn touch [#^Node pieces]
rlm@111 639 (filter (comp not nil?)
rlm@111 640 (map enable-touch
rlm@111 641 (filter #(isa? (class %) Geometry)
rlm@111 642 (node-seq pieces)))))
rlm@94 643
rlm@109 644
rlm@111 645 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
rlm@111 646 ;; http://en.wikipedia.org/wiki/Retina
rlm@109 647
rlm@111 648 (defn test-eye []
rlm@117 649 (.getChild
rlm@117 650 (.getChild (worm-model) "eyes")
rlm@117 651 "eye"))
rlm@111 652
rlm@111 653
rlm@111 654 (defn retina-sensor-image
rlm@111 655 "Return a map of pixel selection functions to BufferedImages
rlm@111 656 describing the distribution of light-sensitive components on this
rlm@111 657 geometry's surface. Each function creates an integer from the rgb
rlm@111 658 values found in the pixel. :red, :green, :blue, :gray are already
rlm@111 659 defined as extracting the red green blue and average components
rlm@111 660 respectively."
rlm@117 661 [#^Spatial eye]
rlm@111 662 (if-let [eye-map (meta-data eye "eye")]
rlm@111 663 (map-vals
rlm@111 664 #(ImageToAwt/convert
rlm@111 665 (.getImage (.loadTexture (asset-manager) %))
rlm@111 666 false false 0)
rlm@120 667 (eval (read-string eye-map)))))
rlm@111 668
rlm@117 669 (defn eye-dimensions
rlm@117 670 "returns the width and height specified in the metadata of the eye"
rlm@117 671 [#^Spatial eye]
rlm@117 672 (let [dimensions
rlm@117 673 (map #(vector (.getWidth %) (.getHeight %))
rlm@117 674 (vals (retina-sensor-image eye)))]
rlm@117 675 [(apply max (map first dimensions))
rlm@117 676 (apply max (map second dimensions))]))
rlm@117 677
rlm@116 678 (defn creature-eyes
rlm@116 679 "The eye nodes which are children of the \"eyes\" node in the
rlm@116 680 creature."
rlm@116 681 [#^Node creature]
rlm@116 682 (if-let [eye-node (.getChild creature "eyes")]
rlm@116 683 (seq (.getChildren eye-node))
rlm@116 684 (do (println-repl "could not find eyes node") [])))
rlm@111 685
rlm@123 686 ;; Here's how vision will work.
rlm@112 687
rlm@123 688 ;; Make the continuation in scene-processor take FrameBuffer,
rlm@123 689 ;; byte-buffer, BufferedImage already sized to the correct
rlm@123 690 ;; dimensions. the continuation will decide wether to "mix" them
rlm@123 691 ;; into the BufferedImage, lazily ignore them, or mix them halfway
rlm@123 692 ;; and call c/graphics card routines.
rlm@112 693
rlm@123 694 ;; (vision creature) will take an optional :skip argument which will
rlm@123 695 ;; inform the continuations in scene processor to skip the given
rlm@123 696 ;; number of cycles; 0 means that no cycles will be skipped.
rlm@112 697
rlm@123 698 ;; (vision creature) will return [init-functions sensor-functions].
rlm@123 699 ;; The init-functions are each single-arg functions that take the
rlm@123 700 ;; world and register the cameras and must each be called before the
rlm@123 701 ;; corresponding sensor-functions. Each init-function returns the
rlm@123 702 ;; viewport for that eye which can be manipulated, saved, etc. Each
rlm@123 703 ;; sensor-function is a thunk and will return data in the same
rlm@123 704 ;; format as the tactile-sensor functions; the structure is
rlm@123 705 ;; [topology, sensor-data]. Internally, these sensor-functions
rlm@123 706 ;; maintain a reference to sensor-data which is periodically updated
rlm@123 707 ;; by the continuation function established by its init-function.
rlm@123 708 ;; They can be queried every cycle, but their information may not
rlm@123 709 ;; necessairly be different every cycle.
rlm@112 710
rlm@123 711 ;; Each eye in the creature in blender will work the same way as
rlm@123 712 ;; joints -- a zero dimensional object with no geometry whose local
rlm@123 713 ;; coordinate system determines the orientation of the resulting
rlm@123 714 ;; eye. All eyes will have a parent named "eyes" just as all joints
rlm@123 715 ;; have a parent named "joints". The resulting camera will be a
rlm@123 716 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
rlm@123 717 ;; the eye marker. The eye marker will contain the metadata for the
rlm@123 718 ;; eye, and will be moved by it's bound geometry. The dimensions of
rlm@123 719 ;; the eye's camera are equal to the dimensions of the eye's "UV"
rlm@123 720 ;; map.
rlm@116 721
rlm@123 722
rlm@123 723 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@123 724
rlm@123 725 ;; Ears work the same way as vision.
rlm@123 726
rlm@123 727 ;; (hearing creature) will return [init-functions
rlm@123 728 ;; sensor-functions]. The init functions each take the world and
rlm@123 729 ;; register a SoundProcessor that does foureier transforms on the
rlm@123 730 ;; incommong sound data, making it available to each sensor function.
rlm@123 731
rlm@123 732 (defn creature-ears
rlm@123 733 "The ear nodes which are children of the \"ears\" node in the
rlm@123 734 creature."
rlm@123 735 [#^Node creature]
rlm@123 736 (if-let [ear-node (.getChild creature "ears")]
rlm@123 737 (seq (.getChildren ear-node))
rlm@123 738 (do (println-repl "could not find ears node") [])))
rlm@123 739
rlm@123 740 (defn closest-node
rlm@123 741 "The closest object in creature to the given node."
rlm@116 742 [#^Node creature #^Node eye]
rlm@116 743 (loop [radius (float 0.01)]
rlm@116 744 (let [results (CollisionResults.)]
rlm@116 745 (.collideWith
rlm@116 746 creature
rlm@116 747 (BoundingBox. (.getWorldTranslation eye)
rlm@116 748 radius radius radius)
rlm@116 749 results)
rlm@116 750 (if-let [target (first results)]
rlm@116 751 (.getGeometry target)
rlm@116 752 (recur (float (* 2 radius)))))))
rlm@116 753
rlm@123 754 (defn bind-sense
rlm@123 755 "Bind the sense to the Spatial such that it will maintain its
rlm@117 756 current position relative to the Spatial no matter how the spatial
rlm@123 757 moves. 'sense can be either a Camera or Listener object."
rlm@123 758 [#^Spatial obj sense]
rlm@123 759 (let [sense-offset (.subtract (.getLocation sense)
rlm@123 760 (.getWorldTranslation obj))
rlm@123 761 initial-sense-rotation (Quaternion. (.getRotation sense))
rlm@117 762 base-anti-rotation (.inverse (.getWorldRotation obj))]
rlm@117 763 (.addControl
rlm@117 764 obj
rlm@117 765 (proxy [AbstractControl] []
rlm@117 766 (controlUpdate [tpf]
rlm@117 767 (let [total-rotation
rlm@117 768 (.mult base-anti-rotation (.getWorldRotation obj))]
rlm@123 769 (.setLocation sense
rlm@117 770 (.add
rlm@123 771 (.mult total-rotation sense-offset)
rlm@117 772 (.getWorldTranslation obj)))
rlm@123 773 (.setRotation sense
rlm@123 774 (.mult total-rotation initial-sense-rotation))))
rlm@117 775 (controlRender [_ _])))))
rlm@117 776
rlm@117 777
rlm@123 778 (defn update-listener-velocity
rlm@123 779 "Update the listener's velocity every update loop."
rlm@123 780 [#^Spatial obj #^Listener lis]
rlm@123 781 (let [old-position (atom (.getLocation lis))]
rlm@123 782 (.addControl
rlm@123 783 obj
rlm@123 784 (proxy [AbstractControl] []
rlm@123 785 (controlUpdate [tpf]
rlm@123 786 (let [new-position (.getLocation lis)]
rlm@123 787 (.setVelocity
rlm@123 788 lis
rlm@123 789 (.mult (.subtract new-position @old-position)
rlm@123 790 (float (/ tpf))))
rlm@123 791 (reset! old-position new-position)))
rlm@123 792 (controlRender [_ _])))))
rlm@123 793
rlm@123 794 (import com.aurellem.capture.audio.AudioSendRenderer)
rlm@123 795
rlm@123 796 (defn attach-ear
rlm@123 797 [#^Application world #^Node creature #^Spatial ear continuation]
rlm@123 798 (let [target (closest-node creature ear)
rlm@123 799 lis (Listener.)
rlm@123 800 audio-renderer (.getAudioRenderer world)
rlm@123 801 sp (sound-processor continuation)]
rlm@123 802 (.setLocation lis (.getWorldTranslation ear))
rlm@123 803 (.setRotation lis (.getWorldRotation ear))
rlm@123 804 (bind-sense target lis)
rlm@123 805 (update-listener-velocity target lis)
rlm@123 806 (.addListener audio-renderer lis)
rlm@123 807 (.registerSoundProcessor audio-renderer lis sp)))
rlm@123 808
rlm@123 809 (defn enable-hearing
rlm@123 810 [#^Node creature #^Spatial ear]
rlm@123 811 (let [hearing-data (atom [])]
rlm@123 812 [(fn [world]
rlm@123 813 (attach-ear world creature ear
rlm@123 814 (fn [data]
rlm@123 815 (reset! hearing-data (vec data)))))
rlm@123 816 [(fn []
rlm@123 817 (let [data @hearing-data
rlm@123 818 topology
rlm@123 819 (vec (map #(vector % 0) (range 0 (count data))))
rlm@123 820 scaled-data
rlm@123 821 (vec
rlm@123 822 (map
rlm@123 823 #(rem (int (* 255 (/ (+ 1 %) 2))) 256)
rlm@123 824 data))]
rlm@123 825 [topology scaled-data]))
rlm@123 826 ]]))
rlm@123 827
rlm@123 828 (defn hearing
rlm@123 829 [#^Node creature]
rlm@123 830 (reduce
rlm@123 831 (fn [[init-a senses-a]
rlm@123 832 [init-b senses-b]]
rlm@123 833 [(conj init-a init-b)
rlm@123 834 (into senses-a senses-b)])
rlm@123 835 [[][]]
rlm@123 836 (for [ear (creature-ears creature)]
rlm@123 837 (enable-hearing creature ear))))
rlm@123 838
rlm@118 839 (defn attach-eye
rlm@118 840 "Attach a Camera to the appropiate area and return the Camera."
rlm@118 841 [#^Node creature #^Spatial eye]
rlm@123 842 (let [target (closest-node creature eye)
rlm@118 843 [cam-width cam-height] (eye-dimensions eye)
rlm@118 844 cam (Camera. cam-width cam-height)]
rlm@118 845 (.setLocation cam (.getWorldTranslation eye))
rlm@118 846 (.setRotation cam (.getWorldRotation eye))
rlm@119 847 (.setFrustumPerspective
rlm@119 848 cam 45 (/ (.getWidth cam) (.getHeight cam))
rlm@119 849 1 1000)
rlm@123 850 (bind-sense target cam)
rlm@118 851 cam))
rlm@118 852
rlm@118 853 (def presets
rlm@121 854 {:all 0xFFFFFF
rlm@119 855 :red 0xFF0000
rlm@119 856 :blue 0x0000FF
rlm@119 857 :green 0x00FF00})
rlm@119 858
rlm@118 859 (defn enable-vision
rlm@118 860 "return [init-function sensor-functions] for a particular eye"
rlm@118 861 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]
rlm@118 862 (let [retinal-map (retina-sensor-image eye)
rlm@123 863 camera (attach-eye creature eye)
rlm@123 864 vision-image
rlm@123 865 (atom
rlm@123 866 (BufferedImage. (.getWidth camera)
rlm@123 867 (.getHeight camera)
rlm@123 868 BufferedImage/TYPE_BYTE_BINARY))]
rlm@123 869 [(fn [world]
rlm@123 870 (add-eye
rlm@123 871 world camera
rlm@123 872 (let [counter (atom 0)]
rlm@123 873 (fn [r fb bb bi]
rlm@123 874 (if (zero? (rem (swap! counter inc) (inc skip)))
rlm@123 875 (reset! vision-image (BufferedImage! r fb bb bi)))))))
rlm@123 876 (vec
rlm@123 877 (map
rlm@123 878 (fn [[key image]]
rlm@123 879 (let [whites (white-coordinates image)
rlm@123 880 topology (vec (collapse whites))
rlm@123 881 mask (presets key)]
rlm@123 882 (fn []
rlm@123 883 (vector
rlm@123 884 topology
rlm@123 885 (vec
rlm@123 886 (for [[x y] whites]
rlm@123 887 (bit-and
rlm@123 888 mask (.getRGB @vision-image x y))))))))
rlm@123 889 retinal-map))]))
rlm@118 890
rlm@116 891 (defn vision
rlm@121 892 [#^Node creature & {skip :skip :or {skip 0}}]
rlm@121 893 (reduce
rlm@121 894 (fn [[init-a senses-a]
rlm@121 895 [init-b senses-b]]
rlm@121 896 [(conj init-a init-b)
rlm@121 897 (into senses-a senses-b)])
rlm@121 898 [[][]]
rlm@121 899 (for [eye (creature-eyes creature)]
rlm@121 900 (enable-vision creature eye))))
rlm@121 901
rlm@116 902
rlm@116 903 (defn blender-creature
rlm@116 904 "Return a creature with all joints in place."
rlm@116 905 [blender-path]
rlm@116 906 (let [model (load-blender-model blender-path)
rlm@116 907 joints
rlm@116 908 (if-let [joint-node (.getChild model "joints")]
rlm@116 909 (seq (.getChildren joint-node))
rlm@116 910 (do (println-repl "could not find joints node") []))]
rlm@116 911 (assemble-creature model joints)))
rlm@116 912
rlm@103 913 (defn debug-window
rlm@103 914 "creates function that offers a debug view of sensor data"
rlm@103 915 []
rlm@103 916 (let [vi (view-image)]
rlm@103 917 (fn
rlm@103 918 [[coords sensor-data]]
rlm@103 919 (let [image (points->image coords)]
rlm@103 920 (dorun
rlm@103 921 (for [i (range (count coords))]
rlm@103 922 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@123 923 ({0 0x000000
rlm@123 924 1 0xFFFFFF} (sensor-data i)))))
rlm@103 925 (vi image)))))
rlm@103 926
rlm@118 927 (defn debug-vision-window
rlm@118 928 "creates function that offers a debug view of sensor data"
rlm@118 929 []
rlm@118 930 (let [vi (view-image)]
rlm@118 931 (fn
rlm@118 932 [[coords sensor-data]]
rlm@118 933 (let [image (points->image coords)]
rlm@118 934 (dorun
rlm@118 935 (for [i (range (count coords))]
rlm@118 936 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@118 937 (sensor-data i))))
rlm@118 938 (vi image)))))
rlm@118 939
rlm@123 940 (defn debug-hearing-window
rlm@123 941 "view audio data"
rlm@123 942 [height]
rlm@123 943 (let [vi (view-image)]
rlm@123 944 (fn [[coords sensor-data]]
rlm@123 945 (let [image (BufferedImage. (count coords) height
rlm@123 946 BufferedImage/TYPE_INT_RGB)]
rlm@123 947 (dorun
rlm@123 948 (for [x (range (count coords))]
rlm@123 949 (dorun
rlm@123 950 (for [y (range height)]
rlm@123 951 (let [raw-sensor (sensor-data x)]
rlm@123 952 (.setRGB image x y
rlm@123 953 (+ raw-sensor
rlm@123 954 (bit-shift-left raw-sensor 8)
rlm@123 955 (bit-shift-left raw-sensor 16))))))))
rlm@123 956 (vi image)))))
rlm@123 957
rlm@123 958
rlm@123 959
rlm@106 960 ;;(defn test-touch [world creature]
rlm@83 961
rlm@78 962
rlm@123 963
rlm@123 964
rlm@123 965
rlm@123 966
rlm@106 967 (defn test-creature [thing]
rlm@106 968 (let [x-axis
rlm@106 969 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 970 y-axis
rlm@106 971 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 972 z-axis
rlm@106 973 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 974 creature (blender-creature thing)
rlm@106 975 touch-nerves (touch creature)
rlm@106 976 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@121 977 [init-vision-fns vision-data] (vision creature)
rlm@121 978 vision-debug (map (fn [_] (debug-vision-window)) vision-data)
rlm@118 979 me (sphere 0.5 :color ColorRGBA/Blue :physical? false)
rlm@123 980 [init-hearing-fns hearing-senses] (hearing creature)
rlm@123 981 hearing-windows (map (fn [_] (debug-hearing-window 50))
rlm@123 982 hearing-senses)
rlm@124 983 bell (AudioNode. (asset-manager)
rlm@124 984 "Sounds/ear-and-eye.wav" false)
rlm@123 985 ;; dream
rlm@123 986
rlm@106 987 ]
rlm@106 988 (world
rlm@106 989 (nodify [creature
rlm@106 990 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 991 :color ColorRGBA/Gray :mass 0)
rlm@106 992 x-axis y-axis z-axis
rlm@118 993 me
rlm@106 994 ])
rlm@123 995 (merge standard-debug-controls
rlm@123 996 {"key-return"
rlm@123 997 (fn [_ value]
rlm@123 998 (if value
rlm@123 999 (do
rlm@123 1000 (println-repl "play-sound")
rlm@124 1001 (.play bell))))})
rlm@106 1002 (fn [world]
rlm@106 1003 (light-up-everything world)
rlm@106 1004 (enable-debug world)
rlm@122 1005 (dorun (map #(% world) init-vision-fns))
rlm@123 1006 (dorun (map #(% world) init-hearing-fns))
rlm@118 1007
rlm@118 1008 (add-eye world
rlm@118 1009 (attach-eye creature (test-eye))
rlm@118 1010 (comp (view-image) BufferedImage!))
rlm@118 1011
rlm@118 1012 (add-eye world (.getCamera world) no-op)
rlm@118 1013
rlm@106 1014 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 1015 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@110 1016 ;;(.setTimer world (RatchetTimer. 60))
rlm@119 1017 (speed-up world)
rlm@106 1018 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 1019 )
rlm@106 1020 (fn [world tpf]
rlm@109 1021 ;;(dorun
rlm@109 1022 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@123 1023
rlm@123 1024
rlm@123 1025
rlm@106 1026 (dorun
rlm@109 1027 (map #(%1 (%2 (.getRootNode world)))
rlm@121 1028 touch-debug-windows touch-nerves))
rlm@123 1029
rlm@121 1030 (dorun
rlm@121 1031 (map #(%1 (%2))
rlm@121 1032 vision-debug vision-data))
rlm@123 1033 (dorun
rlm@123 1034 (map #(%1 (%2)) hearing-windows hearing-senses))
rlm@123 1035
rlm@123 1036
rlm@118 1037 ;;(println-repl (vision-data))
rlm@118 1038 (.setLocalTranslation me (.getLocation (.getCamera world)))
rlm@118 1039
rlm@121 1040
rlm@106 1041 )
rlm@106 1042 ;;(let [timer (atom 0)]
rlm@106 1043 ;; (fn [_ _]
rlm@106 1044 ;; (swap! timer inc)
rlm@106 1045 ;; (if (= (rem @timer 60) 0)
rlm@106 1046 ;; (println-repl (float (/ @timer 60))))))
rlm@106 1047 )))
rlm@83 1048
rlm@109 1049
rlm@109 1050
rlm@109 1051
rlm@109 1052
rlm@109 1053
rlm@109 1054
rlm@109 1055
rlm@109 1056
rlm@109 1057 ;;; experiments in collisions
rlm@109 1058
rlm@109 1059
rlm@109 1060
rlm@109 1061 (defn collision-test []
rlm@110 1062 (let [b-radius 1
rlm@110 1063 b-position (Vector3f. 0 0 0)
rlm@109 1064 obj-b (box 1 1 1 :color ColorRGBA/Blue
rlm@109 1065 :position b-position
rlm@110 1066 :mass 0)
rlm@110 1067 node (nodify [obj-b])
rlm@110 1068 bounds-b
rlm@110 1069 (doto (Picture.)
rlm@110 1070 (.setHeight 50)
rlm@110 1071 (.setWidth 50)
rlm@110 1072 (.setImage (asset-manager)
rlm@110 1073 "Models/creature1/hand.png"
rlm@110 1074 false
rlm@110 1075 ))
rlm@110 1076
rlm@110 1077 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
rlm@110 1078
rlm@110 1079 collisions
rlm@110 1080 (let [cr (CollisionResults.)]
rlm@110 1081 (.collideWith node bounds-b cr)
rlm@110 1082 (println (map #(.getContactPoint %) cr))
rlm@110 1083 cr)
rlm@110 1084
rlm@110 1085 ;;collision-points
rlm@110 1086 ;;(map #(sphere 0.1 :position (.getContactPoint %))
rlm@110 1087 ;; collisions)
rlm@110 1088
rlm@110 1089 ;;node (nodify (conj collision-points obj-b))
rlm@110 1090
rlm@109 1091 sim
rlm@109 1092 (world node
rlm@110 1093 {"key-space"
rlm@110 1094 (fn [_ value]
rlm@110 1095 (if value
rlm@110 1096 (let [cr (CollisionResults.)]
rlm@110 1097 (.collideWith node bounds-b cr)
rlm@110 1098 (println-repl (map #(.getContactPoint %) cr))
rlm@110 1099 cr)))}
rlm@109 1100 no-op
rlm@109 1101 no-op)
rlm@109 1102
rlm@109 1103 ]
rlm@110 1104 sim
rlm@109 1105
rlm@109 1106 ))
rlm@109 1107
rlm@116 1108
rlm@116 1109 ;; the camera will stay in its initial position/rotation with relation
rlm@116 1110 ;; to the spatial.
rlm@116 1111
rlm@116 1112
rlm@117 1113 (defn follow-test
rlm@117 1114 "show a camera that stays in the same relative position to a blue cube."
rlm@117 1115 []
rlm@116 1116 (let [camera-pos (Vector3f. 0 30 0)
rlm@116 1117 rock (box 1 1 1 :color ColorRGBA/Blue
rlm@116 1118 :position (Vector3f. 0 10 0)
rlm@116 1119 :mass 30
rlm@116 1120 )
rlm@118 1121 rot (.getWorldRotation rock)
rlm@116 1122
rlm@116 1123 table (box 3 1 10 :color ColorRGBA/Gray :mass 0
rlm@116 1124 :position (Vector3f. 0 -3 0))]
rlm@116 1125
rlm@116 1126 (world
rlm@116 1127 (nodify [rock table])
rlm@116 1128 standard-debug-controls
rlm@116 1129 (fn [world]
rlm@116 1130 (let
rlm@116 1131 [cam (doto (.clone (.getCamera world))
rlm@116 1132 (.setLocation camera-pos)
rlm@116 1133 (.lookAt Vector3f/ZERO
rlm@116 1134 Vector3f/UNIT_X))]
rlm@123 1135 (bind-sense rock cam)
rlm@116 1136
rlm@116 1137 (.setTimer world (RatchetTimer. 60))
rlm@116 1138 (add-eye world cam (comp (view-image) BufferedImage!))
rlm@116 1139 (add-eye world (.getCamera world) no-op))
rlm@116 1140 )
rlm@118 1141 (fn [_ _] (println-repl rot)))))
rlm@116 1142
rlm@118 1143
rlm@123 1144
rlm@87 1145 #+end_src
rlm@83 1146
rlm@87 1147 #+results: body-1
rlm@109 1148 : #'cortex.silly/test-creature
rlm@78 1149
rlm@78 1150
rlm@78 1151 * COMMENT purgatory
rlm@78 1152 #+begin_src clojure
rlm@77 1153 (defn bullet-trans []
rlm@77 1154 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 1155 :position (Vector3f. -10 5 0))
rlm@77 1156 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 1157 :position (Vector3f. -10 -5 0)
rlm@77 1158 :mass 0)
rlm@77 1159 control-a (.getControl obj-a RigidBodyControl)
rlm@77 1160 control-b (.getControl obj-b RigidBodyControl)
rlm@77 1161 swivel
rlm@77 1162 (.toRotationMatrix
rlm@77 1163 (doto (Quaternion.)
rlm@77 1164 (.fromAngleAxis (/ Math/PI 2)
rlm@77 1165 Vector3f/UNIT_X)))]
rlm@77 1166 (doto
rlm@77 1167 (ConeJoint.
rlm@77 1168 control-a control-b
rlm@77 1169 (Vector3f. 0 5 0)
rlm@77 1170 (Vector3f. 0 -5 0)
rlm@77 1171 swivel swivel)
rlm@77 1172 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 1173 (/ Math/PI 4)
rlm@77 1174 (* Math/PI 0.8)))
rlm@77 1175 (world (nodify
rlm@77 1176 [obj-a obj-b])
rlm@77 1177 standard-debug-controls
rlm@77 1178 enable-debug
rlm@77 1179 no-op)))
rlm@74 1180
rlm@74 1181
rlm@77 1182 (defn bullet-trans* []
rlm@77 1183 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 1184 :position (Vector3f. 5 0 0)
rlm@77 1185 :mass 90)
rlm@77 1186 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 1187 :position (Vector3f. -5 0 0)
rlm@77 1188 :mass 0)
rlm@77 1189 control-a (.getControl obj-a RigidBodyControl)
rlm@77 1190 control-b (.getControl obj-b RigidBodyControl)
rlm@77 1191 move-up? (atom nil)
rlm@77 1192 move-down? (atom nil)
rlm@77 1193 move-left? (atom nil)
rlm@77 1194 move-right? (atom nil)
rlm@77 1195 roll-left? (atom nil)
rlm@77 1196 roll-right? (atom nil)
rlm@77 1197 force 100
rlm@77 1198 swivel
rlm@77 1199 (.toRotationMatrix
rlm@77 1200 (doto (Quaternion.)
rlm@77 1201 (.fromAngleAxis (/ Math/PI 2)
rlm@77 1202 Vector3f/UNIT_X)))
rlm@77 1203 x-move
rlm@77 1204 (doto (Matrix3f.)
rlm@77 1205 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 1206 (.normalize (Vector3f. 1 1 0))))
rlm@77 1207
rlm@77 1208 timer (atom 0)]
rlm@77 1209 (doto
rlm@77 1210 (ConeJoint.
rlm@77 1211 control-a control-b
rlm@77 1212 (Vector3f. -8 0 0)
rlm@77 1213 (Vector3f. 2 0 0)
rlm@77 1214 ;;swivel swivel
rlm@77 1215 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 1216 x-move Matrix3f/IDENTITY
rlm@77 1217 )
rlm@77 1218 (.setCollisionBetweenLinkedBodys false)
rlm@77 1219 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 1220 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 1221 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 1222 (world (nodify
rlm@77 1223 [obj-a obj-b])
rlm@77 1224 (merge standard-debug-controls
rlm@77 1225 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 1226 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 1227 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 1228 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 1229 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 1230 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 1231
rlm@77 1232 (fn [world]
rlm@77 1233 (enable-debug world)
rlm@77 1234 (set-gravity world Vector3f/ZERO)
rlm@77 1235 )
rlm@77 1236
rlm@77 1237 (fn [world _]
rlm@77 1238
rlm@77 1239 (if @move-up?
rlm@77 1240 (.applyForce control-a
rlm@77 1241 (Vector3f. force 0 0)
rlm@77 1242 (Vector3f. 0 0 0)))
rlm@77 1243 (if @move-down?
rlm@77 1244 (.applyForce control-a
rlm@77 1245 (Vector3f. (- force) 0 0)
rlm@77 1246 (Vector3f. 0 0 0)))
rlm@77 1247 (if @move-left?
rlm@77 1248 (.applyForce control-a
rlm@77 1249 (Vector3f. 0 force 0)
rlm@77 1250 (Vector3f. 0 0 0)))
rlm@77 1251 (if @move-right?
rlm@77 1252 (.applyForce control-a
rlm@77 1253 (Vector3f. 0 (- force) 0)
rlm@77 1254 (Vector3f. 0 0 0)))
rlm@77 1255
rlm@77 1256 (if @roll-left?
rlm@77 1257 (.applyForce control-a
rlm@77 1258 (Vector3f. 0 0 force)
rlm@77 1259 (Vector3f. 0 0 0)))
rlm@77 1260 (if @roll-right?
rlm@77 1261 (.applyForce control-a
rlm@77 1262 (Vector3f. 0 0 (- force))
rlm@77 1263 (Vector3f. 0 0 0)))
rlm@77 1264
rlm@77 1265 (if (zero? (rem (swap! timer inc) 100))
rlm@77 1266 (.attachChild
rlm@77 1267 (.getRootNode world)
rlm@77 1268 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 1269 :physical? false :position
rlm@77 1270 (.getWorldTranslation obj-a)))))
rlm@77 1271 )
rlm@77 1272 ))
rlm@77 1273
rlm@94 1274 (defn transform-trianglesdsd
rlm@94 1275 "Transform that converts each vertex in the first triangle
rlm@94 1276 into the corresponding vertex in the second triangle."
rlm@94 1277 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 1278 (let [in [(.get1 tri-1)
rlm@94 1279 (.get2 tri-1)
rlm@94 1280 (.get3 tri-1)]
rlm@94 1281 out [(.get1 tri-2)
rlm@94 1282 (.get2 tri-2)
rlm@94 1283 (.get3 tri-2)]]
rlm@94 1284 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 1285 in* [(.mult translate (in 0))
rlm@94 1286 (.mult translate (in 1))
rlm@94 1287 (.mult translate (in 2))]
rlm@94 1288 final-translation
rlm@94 1289 (doto (Matrix4f.)
rlm@94 1290 (.setTranslation (out 1)))
rlm@94 1291
rlm@94 1292 rotate-1
rlm@94 1293 (doto (Matrix3f.)
rlm@94 1294 (.fromStartEndVectors
rlm@94 1295 (.normalize
rlm@94 1296 (.subtract
rlm@94 1297 (in* 1) (in* 0)))
rlm@94 1298 (.normalize
rlm@94 1299 (.subtract
rlm@94 1300 (out 1) (out 0)))))
rlm@94 1301 in** [(.mult rotate-1 (in* 0))
rlm@94 1302 (.mult rotate-1 (in* 1))
rlm@94 1303 (.mult rotate-1 (in* 2))]
rlm@94 1304 scale-factor-1
rlm@94 1305 (.mult
rlm@94 1306 (.normalize
rlm@94 1307 (.subtract
rlm@94 1308 (out 1)
rlm@94 1309 (out 0)))
rlm@94 1310 (/ (.length
rlm@94 1311 (.subtract (out 1)
rlm@94 1312 (out 0)))
rlm@94 1313 (.length
rlm@94 1314 (.subtract (in** 1)
rlm@94 1315 (in** 0)))))
rlm@94 1316 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 1317 in*** [(.mult scale-1 (in** 0))
rlm@94 1318 (.mult scale-1 (in** 1))
rlm@94 1319 (.mult scale-1 (in** 2))]
rlm@94 1320
rlm@94 1321
rlm@94 1322
rlm@94 1323
rlm@94 1324
rlm@94 1325 ]
rlm@94 1326
rlm@94 1327 (dorun (map println in))
rlm@94 1328 (println)
rlm@94 1329 (dorun (map println in*))
rlm@94 1330 (println)
rlm@94 1331 (dorun (map println in**))
rlm@94 1332 (println)
rlm@94 1333 (dorun (map println in***))
rlm@94 1334 (println)
rlm@94 1335
rlm@99 1336 ))))
rlm@94 1337
rlm@94 1338
rlm@106 1339 (defn world-setup [joint]
rlm@106 1340 (let [joint-position (Vector3f. 0 0 0)
rlm@106 1341 joint-rotation
rlm@106 1342 (.toRotationMatrix
rlm@106 1343 (.mult
rlm@106 1344 (doto (Quaternion.)
rlm@106 1345 (.fromAngleAxis
rlm@106 1346 (* 1 (/ Math/PI 4))
rlm@106 1347 (Vector3f. -1 0 0)))
rlm@106 1348 (doto (Quaternion.)
rlm@106 1349 (.fromAngleAxis
rlm@106 1350 (* 1 (/ Math/PI 2))
rlm@106 1351 (Vector3f. 0 0 1)))))
rlm@106 1352 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 1353
rlm@106 1354 origin (doto
rlm@106 1355 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 1356 :position top-position))
rlm@106 1357 top (doto
rlm@106 1358 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 1359 :position top-position)
rlm@106 1360
rlm@106 1361 (.addControl
rlm@106 1362 (RigidBodyControl.
rlm@106 1363 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 1364 bottom (doto
rlm@106 1365 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 1366 :position (Vector3f. 0 0 0))
rlm@106 1367 (.addControl
rlm@106 1368 (RigidBodyControl.
rlm@106 1369 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 1370 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 1371 :color ColorRGBA/Gray :mass 0)
rlm@106 1372 a (.getControl top RigidBodyControl)
rlm@106 1373 b (.getControl bottom RigidBodyControl)]
rlm@106 1374
rlm@106 1375 (cond
rlm@106 1376 (= joint :cone)
rlm@106 1377
rlm@106 1378 (doto (ConeJoint.
rlm@106 1379 a b
rlm@106 1380 (world-to-local top joint-position)
rlm@106 1381 (world-to-local bottom joint-position)
rlm@106 1382 joint-rotation
rlm@106 1383 joint-rotation
rlm@106 1384 )
rlm@106 1385
rlm@106 1386
rlm@106 1387 (.setLimit (* (/ 10) Math/PI)
rlm@106 1388 (* (/ 4) Math/PI)
rlm@106 1389 0)))
rlm@106 1390 [origin top bottom table]))
rlm@106 1391
rlm@106 1392 (defn test-joint [joint]
rlm@106 1393 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1394 control (.getControl top RigidBodyControl)
rlm@106 1395 move-up? (atom false)
rlm@106 1396 move-down? (atom false)
rlm@106 1397 move-left? (atom false)
rlm@106 1398 move-right? (atom false)
rlm@106 1399 roll-left? (atom false)
rlm@106 1400 roll-right? (atom false)
rlm@106 1401 timer (atom 0)]
rlm@106 1402
rlm@106 1403 (world
rlm@106 1404 (nodify [top bottom floor origin])
rlm@106 1405 (merge standard-debug-controls
rlm@106 1406 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1407 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1408 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1409 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1410 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1411 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1412
rlm@106 1413 (fn [world]
rlm@106 1414 (light-up-everything world)
rlm@106 1415 (enable-debug world)
rlm@106 1416 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1417 )
rlm@106 1418
rlm@106 1419 (fn [world _]
rlm@106 1420 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1421 (do
rlm@106 1422 ;; (println-repl @timer)
rlm@106 1423 (.attachChild (.getRootNode world)
rlm@106 1424 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1425 :position (.getWorldTranslation top)
rlm@106 1426 :physical? false))
rlm@106 1427 (.attachChild (.getRootNode world)
rlm@106 1428 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1429 :position (.getWorldTranslation bottom)
rlm@106 1430 :physical? false))))
rlm@106 1431
rlm@106 1432 (if @move-up?
rlm@106 1433 (.applyTorque control
rlm@106 1434 (.mult (.getPhysicsRotation control)
rlm@106 1435 (Vector3f. 0 0 10))))
rlm@106 1436 (if @move-down?
rlm@106 1437 (.applyTorque control
rlm@106 1438 (.mult (.getPhysicsRotation control)
rlm@106 1439 (Vector3f. 0 0 -10))))
rlm@106 1440 (if @move-left?
rlm@106 1441 (.applyTorque control
rlm@106 1442 (.mult (.getPhysicsRotation control)
rlm@106 1443 (Vector3f. 0 10 0))))
rlm@106 1444 (if @move-right?
rlm@106 1445 (.applyTorque control
rlm@106 1446 (.mult (.getPhysicsRotation control)
rlm@106 1447 (Vector3f. 0 -10 0))))
rlm@106 1448 (if @roll-left?
rlm@106 1449 (.applyTorque control
rlm@106 1450 (.mult (.getPhysicsRotation control)
rlm@106 1451 (Vector3f. -1 0 0))))
rlm@106 1452 (if @roll-right?
rlm@106 1453 (.applyTorque control
rlm@106 1454 (.mult (.getPhysicsRotation control)
rlm@106 1455 (Vector3f. 1 0 0))))))))
rlm@106 1456
rlm@99 1457
rlm@99 1458
rlm@107 1459 (defprotocol Frame
rlm@107 1460 (frame [this]))
rlm@107 1461
rlm@107 1462 (extend-type BufferedImage
rlm@107 1463 Frame
rlm@107 1464 (frame [image]
rlm@107 1465 (merge
rlm@107 1466 (apply
rlm@107 1467 hash-map
rlm@107 1468 (interleave
rlm@107 1469 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1470 (vector x y)))
rlm@107 1471 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1472 (let [data (.getRGB image x y)]
rlm@107 1473 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1474 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1475 :b (bit-and 0x0000ff data)))))))
rlm@107 1476 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1477
rlm@107 1478
rlm@107 1479 (extend-type ImagePlus
rlm@107 1480 Frame
rlm@107 1481 (frame [image+]
rlm@107 1482 (frame (.getBufferedImage image+))))
rlm@107 1483
rlm@107 1484
rlm@99 1485 #+end_src
rlm@99 1486
rlm@99 1487
rlm@99 1488 * COMMENT generate source
rlm@99 1489 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1490 <<body-1>>
rlm@99 1491 #+end_src
rlm@99 1492
rlm@99 1493
rlm@94 1494
rlm@94 1495