annotate org/test-creature.org @ 128:4b38355ad6e3

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