annotate org/test-creature.org @ 107:53fb379ac678

saving progress
author Robert McIntyre <rlm@mit.edu>
date Sun, 15 Jan 2012 00:40:49 -0700
parents 40e72c6943d8
children 92b857b6145d
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@99 9 * objectives
rlm@103 10 - [X] get an overall bitmap-like image for touch
rlm@103 11 - [X] write code to visuliaze this bitmap
rlm@99 12 - [ ] directly change the UV-pixels to show touch sensor activation
rlm@99 13 - [ ] write an explination for why b&w bitmaps for senses is appropiate
rlm@99 14 - [ ] clean up touch code and write visulazation test
rlm@99 15 - [ ] do the same for eyes
rlm@99 16
rlm@73 17 * Intro
rlm@73 18 So far, I've made the following senses --
rlm@73 19 - Vision
rlm@73 20 - Hearing
rlm@73 21 - Touch
rlm@73 22 - Proprioception
rlm@73 23
rlm@73 24 And one effector:
rlm@73 25 - Movement
rlm@73 26
rlm@73 27 However, the code so far has only enabled these senses, but has not
rlm@73 28 actually implemented them. For example, there is still a lot of work
rlm@73 29 to be done for vision. I need to be able to create an /eyeball/ in
rlm@73 30 simulation that can be moved around and see the world from different
rlm@73 31 angles. I also need to determine weather to use log-polar or cartesian
rlm@73 32 for the visual input, and I need to determine how/wether to
rlm@73 33 disceritise the visual input.
rlm@73 34
rlm@73 35 I also want to be able to visualize both the sensors and the
rlm@104 36 effectors in pretty pictures. This semi-retarted creature will be my
rlm@73 37 first attempt at bringing everything together.
rlm@73 38
rlm@73 39 * The creature's body
rlm@73 40
rlm@73 41 Still going to do an eve-like body in blender, but due to problems
rlm@104 42 importing the joints, etc into jMonkeyEngine3, I'm going to do all
rlm@73 43 the connecting here in clojure code, using the names of the individual
rlm@73 44 components and trial and error. Later, I'll maybe make some sort of
rlm@73 45 creature-building modifications to blender that support whatever
rlm@73 46 discreitized senses I'm going to make.
rlm@73 47
rlm@73 48 #+name: body-1
rlm@73 49 #+begin_src clojure
rlm@73 50 (ns cortex.silly
rlm@73 51 "let's play!"
rlm@73 52 {:author "Robert McIntyre"})
rlm@73 53
rlm@73 54 ;; TODO remove this!
rlm@73 55 (require 'cortex.import)
rlm@73 56 (cortex.import/mega-import-jme3)
rlm@73 57 (use '(cortex world util body hearing touch vision))
rlm@73 58
rlm@73 59 (rlm.rlm-commands/help)
rlm@99 60 (import java.awt.image.BufferedImage)
rlm@99 61 (import javax.swing.JPanel)
rlm@99 62 (import javax.swing.SwingUtilities)
rlm@99 63 (import java.awt.Dimension)
rlm@99 64 (import javax.swing.JFrame)
rlm@99 65 (import java.awt.Dimension)
rlm@106 66 (import com.aurellem.capture.RatchetTimer)
rlm@99 67 (declare joint-create)
rlm@73 68
rlm@99 69 (defn view-image
rlm@99 70 "Initailizes a JPanel on which you may draw a BufferedImage.
rlm@99 71 Returns a function that accepts a BufferedImage and draws it to the
rlm@99 72 JPanel."
rlm@99 73 []
rlm@99 74 (let [image
rlm@99 75 (atom
rlm@99 76 (BufferedImage. 1 1 BufferedImage/TYPE_4BYTE_ABGR))
rlm@99 77 panel
rlm@99 78 (proxy [JPanel] []
rlm@99 79 (paint
rlm@99 80 [graphics]
rlm@99 81 (proxy-super paintComponent graphics)
rlm@99 82 (.drawImage graphics @image 0 0 nil)))
rlm@99 83 frame (JFrame. "Display Image")]
rlm@99 84 (SwingUtilities/invokeLater
rlm@99 85 (fn []
rlm@99 86 (doto frame
rlm@99 87 (-> (.getContentPane) (.add panel))
rlm@99 88 (.pack)
rlm@99 89 (.setLocationRelativeTo nil)
rlm@99 90 (.setResizable true)
rlm@99 91 (.setVisible true))))
rlm@99 92 (fn [#^BufferedImage i]
rlm@99 93 (reset! image i)
rlm@99 94 (.setSize frame (+ 8 (.getWidth i)) (+ 28 (.getHeight i)))
rlm@99 95 (.repaint panel 0 0 (.getWidth i) (.getHeight i)))))
rlm@99 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@106 113 BufferedImage/TYPE_BYTE_BINARY)]
rlm@106 114 (dorun
rlm@106 115 (for [index (range (count points))]
rlm@106 116 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
rlm@106 117
rlm@106 118 image)))
rlm@100 119
rlm@101 120 (defn test-data
rlm@101 121 []
rlm@101 122 (vec
rlm@102 123 (for [a (range 0 1000 2)
rlm@102 124 b (range 0 1000 2)]
rlm@101 125 (vector a b))
rlm@101 126 ))
rlm@99 127
rlm@101 128 (defn average [coll]
rlm@101 129 (/ (reduce + coll) (count coll)))
rlm@101 130
rlm@101 131 (defn collapse-1d
rlm@101 132 "One dimensional analogue of collapse"
rlm@101 133 [center line]
rlm@101 134 (let [length (count line)
rlm@101 135 num-above (count (filter (partial < center) line))
rlm@101 136 num-below (- length num-above)]
rlm@101 137 (range (- center num-below)
rlm@101 138 (+ center num-above))
rlm@101 139 ))
rlm@99 140
rlm@99 141 (defn collapse
rlm@99 142 "Take a set of pairs of integers and collapse them into a
rlm@99 143 contigous bitmap."
rlm@99 144 [points]
rlm@101 145 (let
rlm@101 146 [num-points (count points)
rlm@101 147 center (vector
rlm@102 148 (int (average (map first points)))
rlm@102 149 (int (average (map first points))))
rlm@101 150 flattened
rlm@101 151 (reduce
rlm@101 152 concat
rlm@101 153 (map
rlm@101 154 (fn [column]
rlm@101 155 (map vector
rlm@101 156 (map first column)
rlm@101 157 (collapse-1d (second center)
rlm@101 158 (map second column))))
rlm@101 159 (partition-by first (sort-by first points))))
rlm@101 160 squeezed
rlm@101 161 (reduce
rlm@101 162 concat
rlm@101 163 (map
rlm@101 164 (fn [row]
rlm@101 165 (map vector
rlm@101 166 (collapse-1d (first center)
rlm@101 167 (map first row))
rlm@101 168 (map second row)))
rlm@102 169 (partition-by second (sort-by second flattened))))
rlm@102 170 relocate
rlm@102 171 (let [min-x (apply min (map first squeezed))
rlm@102 172 min-y (apply min (map second squeezed))]
rlm@102 173 (map (fn [[x y]]
rlm@102 174 [(- x min-x)
rlm@102 175 (- y min-y)])
rlm@104 176 squeezed))]
rlm@102 177 relocate
rlm@101 178 ))
rlm@83 179
rlm@83 180 (defn load-bullet []
rlm@84 181 (let [sim (world (Node.) {} no-op no-op)]
rlm@102 182 (doto sim
rlm@102 183 (.enqueue
rlm@102 184 (fn []
rlm@102 185 (.stop sim)))
rlm@102 186 (.start))))
rlm@83 187
rlm@73 188 (defn load-blender-model
rlm@73 189 "Load a .blend file using an asset folder relative path."
rlm@73 190 [^String model]
rlm@73 191 (.loadModel
rlm@73 192 (doto (asset-manager)
rlm@73 193 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 194 model))
rlm@73 195
rlm@74 196 (defn meta-data [blender-node key]
rlm@74 197 (if-let [data (.getUserData blender-node "properties")]
rlm@74 198 (.findValue data key)
rlm@74 199 nil))
rlm@73 200
rlm@78 201 (defn blender-to-jme
rlm@78 202 "Convert from Blender coordinates to JME coordinates"
rlm@78 203 [#^Vector3f in]
rlm@78 204 (Vector3f. (.getX in)
rlm@78 205 (.getZ in)
rlm@78 206 (- (.getY in))))
rlm@74 207
rlm@79 208 (defn jme-to-blender
rlm@79 209 "Convert from JME coordinates to Blender coordinates"
rlm@79 210 [#^Vector3f in]
rlm@79 211 (Vector3f. (.getX in)
rlm@79 212 (- (.getZ in))
rlm@79 213 (.getY in)))
rlm@79 214
rlm@78 215 (defn joint-targets
rlm@78 216 "Return the two closest two objects to the joint object, ordered
rlm@78 217 from bottom to top according to the joint's rotation."
rlm@78 218 [#^Node parts #^Node joint]
rlm@78 219 ;;(println (meta-data joint "joint"))
rlm@78 220 (.getWorldRotation joint)
rlm@78 221 (loop [radius (float 0.01)]
rlm@78 222 (let [results (CollisionResults.)]
rlm@78 223 (.collideWith
rlm@78 224 parts
rlm@78 225 (BoundingBox. (.getWorldTranslation joint)
rlm@78 226 radius radius radius)
rlm@78 227 results)
rlm@78 228 (let [targets
rlm@78 229 (distinct
rlm@78 230 (map #(.getGeometry %) results))]
rlm@78 231 (if (>= (count targets) 2)
rlm@78 232 (sort-by
rlm@79 233 #(let [v
rlm@79 234 (jme-to-blender
rlm@79 235 (.mult
rlm@79 236 (.inverse (.getWorldRotation joint))
rlm@79 237 (.subtract (.getWorldTranslation %)
rlm@79 238 (.getWorldTranslation joint))))]
rlm@79 239 (println-repl (.getName %) ":" v)
rlm@79 240 (.dot (Vector3f. 1 1 1)
rlm@79 241 v))
rlm@78 242 (take 2 targets))
rlm@78 243 (recur (float (* radius 2))))))))
rlm@74 244
rlm@87 245 (defn world-to-local
rlm@87 246 "Convert the world coordinates into coordinates relative to the
rlm@87 247 object (i.e. local coordinates), taking into account the rotation
rlm@87 248 of object."
rlm@87 249 [#^Spatial object world-coordinate]
rlm@87 250 (let [out (Vector3f.)]
rlm@88 251 (.worldToLocal object world-coordinate out) out))
rlm@87 252
rlm@96 253 (defn local-to-world
rlm@96 254 "Convert the local coordinates into coordinates into world relative
rlm@96 255 coordinates"
rlm@96 256 [#^Spatial object local-coordinate]
rlm@96 257 (let [world-coordinate (Vector3f.)]
rlm@96 258 (.localToWorld object local-coordinate world-coordinate)
rlm@96 259 world-coordinate))
rlm@96 260
rlm@96 261
rlm@87 262 (defmulti joint-dispatch
rlm@87 263 "Translate blender pseudo-joints into real JME joints."
rlm@88 264 (fn [constraints & _]
rlm@87 265 (:type constraints)))
rlm@87 266
rlm@87 267 (defmethod joint-dispatch :point
rlm@87 268 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 269 (println-repl "creating POINT2POINT joint")
rlm@87 270 (Point2PointJoint.
rlm@87 271 control-a
rlm@87 272 control-b
rlm@87 273 pivot-a
rlm@87 274 pivot-b))
rlm@87 275
rlm@87 276 (defmethod joint-dispatch :hinge
rlm@87 277 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 278 (println-repl "creating HINGE joint")
rlm@87 279 (let [axis
rlm@87 280 (if-let
rlm@87 281 [axis (:axis constraints)]
rlm@87 282 axis
rlm@87 283 Vector3f/UNIT_X)
rlm@87 284 [limit-1 limit-2] (:limit constraints)
rlm@87 285 hinge-axis
rlm@87 286 (.mult
rlm@87 287 rotation
rlm@87 288 (blender-to-jme axis))]
rlm@87 289 (doto
rlm@87 290 (HingeJoint.
rlm@87 291 control-a
rlm@87 292 control-b
rlm@87 293 pivot-a
rlm@87 294 pivot-b
rlm@87 295 hinge-axis
rlm@87 296 hinge-axis)
rlm@87 297 (.setLimit limit-1 limit-2))))
rlm@87 298
rlm@87 299 (defmethod joint-dispatch :cone
rlm@87 300 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 301 (let [limit-xz (:limit-xz constraints)
rlm@87 302 limit-xy (:limit-xy constraints)
rlm@87 303 twist (:twist constraints)]
rlm@87 304
rlm@87 305 (println-repl "creating CONE joint")
rlm@87 306 (println-repl rotation)
rlm@87 307 (println-repl
rlm@87 308 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 309 (println-repl
rlm@87 310 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 311 (println-repl
rlm@87 312 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 313 (doto
rlm@87 314 (ConeJoint.
rlm@87 315 control-a
rlm@87 316 control-b
rlm@87 317 pivot-a
rlm@87 318 pivot-b
rlm@87 319 rotation
rlm@87 320 rotation)
rlm@87 321 (.setLimit (float limit-xz)
rlm@87 322 (float limit-xy)
rlm@87 323 (float twist)))))
rlm@87 324
rlm@88 325 (defn connect
rlm@87 326 "here are some examples:
rlm@87 327 {:type :point}
rlm@87 328 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 329 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 330
rlm@89 331 {:type :cone :limit-xz 0]
rlm@89 332 :limit-xy 0]
rlm@89 333 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 334 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 335 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 336 control-b (.getControl obj-b RigidBodyControl)
rlm@87 337 joint-center (.getWorldTranslation joint)
rlm@87 338 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 339 pivot-a (world-to-local obj-a joint-center)
rlm@87 340 pivot-b (world-to-local obj-b joint-center)]
rlm@89 341
rlm@87 342 (if-let [constraints
rlm@87 343 (map-vals
rlm@87 344 eval
rlm@87 345 (read-string
rlm@87 346 (meta-data joint "joint")))]
rlm@89 347 ;; A side-effect of creating a joint registers
rlm@89 348 ;; it with both physics objects which in turn
rlm@89 349 ;; will register the joint with the physics system
rlm@89 350 ;; when the simulation is started.
rlm@87 351 (do
rlm@87 352 (println-repl "creating joint between"
rlm@87 353 (.getName obj-a) "and" (.getName obj-b))
rlm@87 354 (joint-dispatch constraints
rlm@87 355 control-a control-b
rlm@87 356 pivot-a pivot-b
rlm@87 357 joint-rotation))
rlm@87 358 (println-repl "could not find joint meta-data!"))))
rlm@87 359
rlm@78 360 (defn assemble-creature [#^Node pieces joints]
rlm@78 361 (dorun
rlm@78 362 (map
rlm@78 363 (fn [geom]
rlm@78 364 (let [physics-control
rlm@78 365 (RigidBodyControl.
rlm@78 366 (HullCollisionShape.
rlm@78 367 (.getMesh geom))
rlm@78 368 (if-let [mass (meta-data geom "mass")]
rlm@78 369 (do
rlm@78 370 (println-repl
rlm@78 371 "setting" (.getName geom) "mass to" (float mass))
rlm@78 372 (float mass))
rlm@78 373 (float 1)))]
rlm@78 374
rlm@78 375 (.addControl geom physics-control)))
rlm@78 376 (filter #(isa? (class %) Geometry )
rlm@78 377 (node-seq pieces))))
rlm@78 378 (dorun
rlm@78 379 (map
rlm@78 380 (fn [joint]
rlm@78 381 (let [[obj-a obj-b]
rlm@78 382 (joint-targets pieces joint)]
rlm@88 383 (connect obj-a obj-b joint)))
rlm@78 384 joints))
rlm@78 385 pieces)
rlm@74 386
rlm@78 387 (defn blender-creature [blender-path]
rlm@78 388 (let [model (load-blender-model blender-path)
rlm@78 389 joints
rlm@78 390 (if-let [joint-node (.getChild model "joints")]
rlm@78 391 (seq (.getChildren joint-node))
rlm@78 392 (do (println-repl "could not find joints node")
rlm@78 393 []))]
rlm@78 394 (assemble-creature model joints)))
rlm@74 395
rlm@78 396 (def hand "Models/creature1/one.blend")
rlm@74 397
rlm@78 398 (def worm "Models/creature1/try-again.blend")
rlm@78 399
rlm@90 400 (def touch "Models/creature1/touch.blend")
rlm@90 401
rlm@90 402 (defn worm-model [] (load-blender-model worm))
rlm@90 403
rlm@80 404 (defn x-ray [#^ColorRGBA color]
rlm@80 405 (doto (Material. (asset-manager)
rlm@80 406 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 407 (.setColor "Color" color)
rlm@80 408 (-> (.getAdditionalRenderState)
rlm@80 409 (.setDepthTest false))))
rlm@80 410
rlm@106 411
rlm@90 412
rlm@91 413 (defn colorful []
rlm@91 414 (.getChild (worm-model) "worm-21"))
rlm@90 415
rlm@90 416 (import jme3tools.converters.ImageToAwt)
rlm@90 417
rlm@90 418 (import ij.ImagePlus)
rlm@90 419
rlm@90 420 (defn triangle-indices
rlm@90 421 "Get the triangle vertex indices of a given triangle from a given
rlm@90 422 mesh."
rlm@90 423 [#^Mesh mesh triangle-index]
rlm@90 424 (let [indices (int-array 3)]
rlm@90 425 (.getTriangle mesh triangle-index indices)
rlm@90 426 (vec indices)))
rlm@90 427
rlm@90 428 (defn uv-coord
rlm@90 429 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@90 430 [#^Mesh mesh vertex-index]
rlm@90 431 (let [UV-buffer
rlm@90 432 (.getData
rlm@90 433 (.getBuffer
rlm@90 434 mesh
rlm@90 435 VertexBuffer$Type/TexCoord))]
rlm@91 436 (Vector2f.
rlm@91 437 (.get UV-buffer (* vertex-index 2))
rlm@91 438 (.get UV-buffer (+ 1 (* vertex-index 2))))))
rlm@90 439
rlm@93 440 (defn tri-uv-coord
rlm@93 441 "Get the uv-cooridnates of the triangle's verticies."
rlm@93 442 [#^Mesh mesh #^Triangle triangle]
rlm@93 443 (map (partial uv-coord mesh)
rlm@93 444 (triangle-indices mesh (.getIndex triangle))))
rlm@93 445
rlm@91 446 (defn touch-receptor-image
rlm@97 447 "Return the touch-sensor distribution image in ImagePlus format, or
rlm@97 448 nil if it does not exist."
rlm@91 449 [#^Geometry obj]
rlm@97 450 (let [mat (.getMaterial obj)]
rlm@97 451 (if-let [texture-param
rlm@97 452 (.getTextureParam
rlm@97 453 mat
rlm@97 454 MaterialHelper/TEXTURE_TYPE_DIFFUSE)]
rlm@97 455 (let
rlm@97 456 [texture
rlm@97 457 (.getTextureValue texture-param)
rlm@97 458 im (.getImage texture)]
rlm@97 459 (ImagePlus.
rlm@97 460 "UV-map"
rlm@97 461 (ImageToAwt/convert im false false 0))))))
rlm@91 462
rlm@91 463 (import ij.process.ImageProcessor)
rlm@91 464 (import java.awt.image.BufferedImage)
rlm@91 465
rlm@92 466 (def white -1)
rlm@94 467
rlm@91 468 (defn filter-pixels
rlm@91 469 "List the coordinates of all pixels matching pred."
rlm@92 470 {:author "Dylan Holmes"}
rlm@91 471 [pred #^ImageProcessor ip]
rlm@91 472 (let
rlm@91 473 [width (.getWidth ip)
rlm@91 474 height (.getHeight ip)]
rlm@91 475 ((fn accumulate [x y matches]
rlm@91 476 (cond
rlm@91 477 (>= y height) matches
rlm@91 478 (>= x width) (recur 0 (inc y) matches)
rlm@91 479 (pred (.getPixel ip x y))
rlm@91 480 (recur (inc x) y (conj matches (Vector2f. x y)))
rlm@91 481 :else (recur (inc x) y matches)))
rlm@91 482 0 0 [])))
rlm@91 483
rlm@91 484 (defn white-coordinates
rlm@91 485 "List the coordinates of all the white pixels in an image."
rlm@91 486 [#^ImageProcessor ip]
rlm@92 487 (filter-pixels #(= % white) ip))
rlm@91 488
rlm@102 489 (defn same-side?
rlm@102 490 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 491 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 492 [p1 p2 ref p]
rlm@91 493 (<=
rlm@91 494 0
rlm@91 495 (.dot
rlm@91 496 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 497 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 498
rlm@94 499 (defn triangle->matrix4f
rlm@94 500 "Converts the triangle into a 4x4 matrix of vertices: The first
rlm@94 501 three columns contain the vertices of the triangle; the last
rlm@94 502 contains the unit normal of the triangle. The bottom row is filled
rlm@94 503 with 1s."
rlm@94 504 [#^Triangle t]
rlm@94 505 (let [mat (Matrix4f.)
rlm@94 506 [vert-1 vert-2 vert-3]
rlm@94 507 ((comp vec map) #(.get t %) (range 3))
rlm@94 508 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 509 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 510 (dorun
rlm@94 511 (for [row (range 4) col (range 3)]
rlm@94 512 (do
rlm@94 513 (.set mat col row (.get (vertices row)col))
rlm@94 514 (.set mat 3 row 1))))
rlm@94 515 mat))
rlm@94 516
rlm@94 517 (defn triangle-transformation
rlm@94 518 "Returns the affine transformation that converts each vertex in the
rlm@94 519 first triangle into the corresponding vertex in the second
rlm@94 520 triangle."
rlm@94 521 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 522 (.mult
rlm@94 523 (triangle->matrix4f tri-2)
rlm@94 524 (.invert (triangle->matrix4f tri-1))))
rlm@94 525
rlm@94 526 (def death (Triangle.
rlm@94 527 (Vector3f. 1 1 1)
rlm@94 528 (Vector3f. 1 2 3)
rlm@94 529 (Vector3f. 5 6 7)))
rlm@94 530
rlm@94 531 (def death-2 (Triangle.
rlm@94 532 (Vector3f. 2 2 2)
rlm@94 533 (Vector3f. 1 1 1)
rlm@94 534 (Vector3f. 0 1 0)))
rlm@94 535
rlm@94 536 (defn vector2f->vector3f [v]
rlm@94 537 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 538
rlm@94 539 (extend-type Triangle
rlm@94 540 Textual
rlm@94 541 (text [t]
rlm@94 542 (println "Triangle: " \newline (.get1 t) \newline
rlm@94 543 (.get2 t) \newline (.get3 t))))
rlm@94 544
rlm@94 545 (defn map-triangle [f #^Triangle tri]
rlm@94 546 (Triangle.
rlm@94 547 (f 0 (.get1 tri))
rlm@94 548 (f 1 (.get2 tri))
rlm@94 549 (f 2 (.get3 tri))))
rlm@94 550
rlm@94 551 (defn triangle-seq [#^Triangle tri]
rlm@94 552 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@94 553
rlm@94 554 (defn vector3f-seq [#^Vector3f v]
rlm@94 555 [(.getX v) (.getY v) (.getZ v)])
rlm@94 556
rlm@91 557 (defn inside-triangle?
rlm@94 558 "Is the point inside the triangle? Now what do we do?
rlm@94 559 You might want to hold on there"
rlm@95 560 {:author "Dylan Holmes"}
rlm@94 561 [tri p]
rlm@94 562 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@94 563 (and
rlm@94 564 (same-side? vert-1 vert-2 vert-3 p)
rlm@94 565 (same-side? vert-2 vert-3 vert-1 p)
rlm@94 566 (same-side? vert-3 vert-1 vert-2 p))))
rlm@91 567
rlm@94 568 (defn uv-triangle
rlm@94 569 "Convert the mesh triangle into the cooresponding triangle in
rlm@94 570 UV-space. Z-component of these triangles is always zero."
rlm@94 571 [#^Mesh mesh #^Triangle tri]
rlm@94 572 (apply #(Triangle. %1 %2 %3)
rlm@94 573 (map vector2f->vector3f
rlm@94 574 (tri-uv-coord mesh tri))))
rlm@91 575
rlm@94 576 (defn pixel-triangle
rlm@102 577 "Convert the mesh triangle into the corresponding triangle in
rlm@94 578 UV-pixel-space. Z compenent will be zero."
rlm@94 579 [#^Mesh mesh #^Triangle tri width height]
rlm@94 580 (map-triangle (fn [_ v]
rlm@94 581 (Vector3f. (* width (.getX v))
rlm@94 582 (* height (.getY v))
rlm@94 583 0))
rlm@94 584 (uv-triangle mesh tri)))
rlm@93 585
rlm@96 586 (def rasterize pixel-triangle)
rlm@96 587
rlm@96 588
rlm@94 589 (defn triangle-bounds
rlm@94 590 "Dimensions of the bounding square of the triangle in the form
rlm@94 591 [x y width height].
rlm@94 592 Assumes that the triangle lies in the XY plane."
rlm@94 593 [#^Triangle tri]
rlm@94 594 (let [verts (map vector3f-seq (triangle-seq tri))
rlm@94 595 x (apply min (map first verts))
rlm@94 596 y (apply min (map second verts))]
rlm@94 597 [x y
rlm@94 598 (- (apply max (map first verts)) x)
rlm@94 599 (- (apply max (map second verts)) y)
rlm@94 600 ]))
rlm@93 601
rlm@93 602
rlm@106 603 (defn sensors-in-triangle
rlm@107 604 "Find the locations of the touch sensors within a triangle in both
rlm@107 605 UV and gemoetry relative coordinates."
rlm@107 606 [image mesh tri-index]
rlm@107 607 (let [width (.getWidth image)
rlm@107 608 height (.getHeight image)]
rlm@107 609
rlm@107 610
rlm@107 611
rlm@107 612
rlm@106 613 )
rlm@106 614
rlm@106 615
rlm@97 616 (defn locate-feelers
rlm@94 617 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 618 their positions in geometry-relative coordinates."
rlm@94 619 [#^Geometry geo]
rlm@97 620 (if-let [image (touch-receptor-image geo)]
rlm@97 621 (let [mesh (.getMesh geo)
rlm@97 622 tris (triangles geo)
rlm@102 623
rlm@97 624 width (.getWidth image)
rlm@97 625 height (.getHeight image)
rlm@97 626
rlm@97 627 ;; for each triangle
rlm@97 628 sensor-coords
rlm@97 629 (fn [tri]
rlm@97 630 ;; translate triangle to uv-pixel-space
rlm@97 631 (let [uv-tri
rlm@97 632 (pixel-triangle mesh tri width height)
rlm@97 633 bounds (vec (triangle-bounds uv-tri))]
rlm@97 634
rlm@97 635 ;; get that part of the picture
rlm@97 636
rlm@97 637 (apply #(.setRoi image %1 %2 %3 %4) bounds)
rlm@97 638 (let [cutout (.crop (.getProcessor image))
rlm@97 639 ;; extract white pixels inside triangle
rlm@97 640 cutout-tri
rlm@97 641 (map-triangle
rlm@97 642 (fn [_ v]
rlm@97 643 (.subtract
rlm@97 644 v
rlm@97 645 (Vector3f. (bounds 0) (bounds 1) (float 0))))
rlm@97 646 uv-tri)
rlm@97 647 whites (filter (partial inside-triangle? cutout-tri)
rlm@97 648 (map vector2f->vector3f
rlm@97 649 (white-coordinates cutout)))
rlm@97 650 ;; translate pixel coordinates to world-space
rlm@97 651 transform (triangle-transformation cutout-tri tri)]
rlm@97 652 (map #(.mult transform %) whites))))]
rlm@97 653 (vec (map sensor-coords tris)))
rlm@97 654 (repeat (count (triangles geo)) [])))
rlm@102 655
rlm@102 656 (use 'clojure.contrib.def)
rlm@102 657
rlm@102 658 (defn-memo touch-topology [#^Gemoetry geo]
rlm@106 659 (if-let [image (touch-receptor-image geo)]
rlm@106 660 (let [feeler-coords
rlm@106 661 (map
rlm@106 662 #(vector (int (.getX %)) (int (.getY %)))
rlm@106 663 (white-coordinates
rlm@106 664 (.getProcessor image)))]
rlm@106 665 (vec (collapse feeler-coords)))
rlm@106 666 []))
rlm@102 667
rlm@97 668 (defn enable-touch [#^Geometry geo]
rlm@97 669 (let [feeler-coords (locate-feelers geo)
rlm@96 670 tris (triangles geo)
rlm@97 671 limit 0.1]
rlm@97 672 (fn [node]
rlm@97 673 (let [sensor-origins
rlm@97 674 (map
rlm@97 675 #(map (partial local-to-world geo) %)
rlm@97 676 feeler-coords)
rlm@97 677 triangle-normals
rlm@97 678 (map (partial get-ray-direction geo)
rlm@97 679 tris)
rlm@97 680 rays
rlm@97 681 (flatten
rlm@97 682 (map (fn [origins norm]
rlm@97 683 (map #(doto (Ray. % norm)
rlm@97 684 (.setLimit limit)) origins))
rlm@97 685 sensor-origins triangle-normals))]
rlm@102 686 (vector
rlm@102 687 (touch-topology geo)
rlm@102 688 (vec
rlm@102 689 (for [ray rays]
rlm@102 690 (do
rlm@102 691 (let [results (CollisionResults.)]
rlm@102 692 (.collideWith node ray results)
rlm@102 693 (let [touch-objects
rlm@102 694 (set
rlm@102 695 (filter #(not (= geo %))
rlm@102 696 (map #(.getGeometry %) results)))]
rlm@102 697 (if (> (count touch-objects) 0)
rlm@102 698 1 0)))))))))))
rlm@94 699
rlm@97 700 (defn touch [#^Node pieces]
rlm@102 701 (map enable-touch
rlm@102 702 (filter #(isa? (class %) Geometry)
rlm@102 703 (node-seq pieces))))
rlm@102 704
rlm@103 705 (defn debug-window
rlm@103 706 "creates function that offers a debug view of sensor data"
rlm@103 707 []
rlm@103 708 (let [vi (view-image)]
rlm@103 709 (fn
rlm@103 710 [[coords sensor-data]]
rlm@103 711 (let [image (points->image coords)]
rlm@103 712 (dorun
rlm@103 713 (for [i (range (count coords))]
rlm@103 714 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@103 715 ({0 -16777216
rlm@103 716 1 -1} (sensor-data i)))))
rlm@103 717 (vi image)))))
rlm@103 718
rlm@83 719
rlm@106 720 ;;(defn test-touch [world creature]
rlm@83 721
rlm@78 722
rlm@106 723 (defn test-creature [thing]
rlm@106 724 (let [x-axis
rlm@106 725 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 726 y-axis
rlm@106 727 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 728 z-axis
rlm@106 729 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 730 creature (blender-creature thing)
rlm@106 731 touch-nerves (touch creature)
rlm@106 732 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@106 733 ]
rlm@106 734 (world
rlm@106 735 (nodify [creature
rlm@106 736 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 737 :color ColorRGBA/Gray :mass 0)
rlm@106 738 x-axis y-axis z-axis
rlm@106 739 ])
rlm@106 740 standard-debug-controls
rlm@106 741 (fn [world]
rlm@106 742 (light-up-everything world)
rlm@106 743 (enable-debug world)
rlm@106 744 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 745 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@106 746 (.setTimer world (RatchetTimer. 60))
rlm@106 747 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 748 )
rlm@106 749 (fn [world tpf]
rlm@106 750 (dorun
rlm@106 751 (map #(%1 (%2 (.getRootNode world))) touch-debug-windows touch-nerves))
rlm@106 752 )
rlm@106 753 ;;(let [timer (atom 0)]
rlm@106 754 ;; (fn [_ _]
rlm@106 755 ;; (swap! timer inc)
rlm@106 756 ;; (if (= (rem @timer 60) 0)
rlm@106 757 ;; (println-repl (float (/ @timer 60))))))
rlm@106 758 )))
rlm@83 759
rlm@87 760 #+end_src
rlm@83 761
rlm@87 762 #+results: body-1
rlm@104 763 : #'cortex.silly/tactile-coords
rlm@78 764
rlm@78 765
rlm@78 766 * COMMENT purgatory
rlm@78 767 #+begin_src clojure
rlm@77 768 (defn bullet-trans []
rlm@77 769 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 770 :position (Vector3f. -10 5 0))
rlm@77 771 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 772 :position (Vector3f. -10 -5 0)
rlm@77 773 :mass 0)
rlm@77 774 control-a (.getControl obj-a RigidBodyControl)
rlm@77 775 control-b (.getControl obj-b RigidBodyControl)
rlm@77 776 swivel
rlm@77 777 (.toRotationMatrix
rlm@77 778 (doto (Quaternion.)
rlm@77 779 (.fromAngleAxis (/ Math/PI 2)
rlm@77 780 Vector3f/UNIT_X)))]
rlm@77 781 (doto
rlm@77 782 (ConeJoint.
rlm@77 783 control-a control-b
rlm@77 784 (Vector3f. 0 5 0)
rlm@77 785 (Vector3f. 0 -5 0)
rlm@77 786 swivel swivel)
rlm@77 787 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 788 (/ Math/PI 4)
rlm@77 789 (* Math/PI 0.8)))
rlm@77 790 (world (nodify
rlm@77 791 [obj-a obj-b])
rlm@77 792 standard-debug-controls
rlm@77 793 enable-debug
rlm@77 794 no-op)))
rlm@74 795
rlm@74 796
rlm@77 797 (defn bullet-trans* []
rlm@77 798 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 799 :position (Vector3f. 5 0 0)
rlm@77 800 :mass 90)
rlm@77 801 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 802 :position (Vector3f. -5 0 0)
rlm@77 803 :mass 0)
rlm@77 804 control-a (.getControl obj-a RigidBodyControl)
rlm@77 805 control-b (.getControl obj-b RigidBodyControl)
rlm@77 806 move-up? (atom nil)
rlm@77 807 move-down? (atom nil)
rlm@77 808 move-left? (atom nil)
rlm@77 809 move-right? (atom nil)
rlm@77 810 roll-left? (atom nil)
rlm@77 811 roll-right? (atom nil)
rlm@77 812 force 100
rlm@77 813 swivel
rlm@77 814 (.toRotationMatrix
rlm@77 815 (doto (Quaternion.)
rlm@77 816 (.fromAngleAxis (/ Math/PI 2)
rlm@77 817 Vector3f/UNIT_X)))
rlm@77 818 x-move
rlm@77 819 (doto (Matrix3f.)
rlm@77 820 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 821 (.normalize (Vector3f. 1 1 0))))
rlm@77 822
rlm@77 823 timer (atom 0)]
rlm@77 824 (doto
rlm@77 825 (ConeJoint.
rlm@77 826 control-a control-b
rlm@77 827 (Vector3f. -8 0 0)
rlm@77 828 (Vector3f. 2 0 0)
rlm@77 829 ;;swivel swivel
rlm@77 830 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 831 x-move Matrix3f/IDENTITY
rlm@77 832 )
rlm@77 833 (.setCollisionBetweenLinkedBodys false)
rlm@77 834 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 835 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 836 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 837 (world (nodify
rlm@77 838 [obj-a obj-b])
rlm@77 839 (merge standard-debug-controls
rlm@77 840 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 841 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 842 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 843 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 844 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 845 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 846
rlm@77 847 (fn [world]
rlm@77 848 (enable-debug world)
rlm@77 849 (set-gravity world Vector3f/ZERO)
rlm@77 850 )
rlm@77 851
rlm@77 852 (fn [world _]
rlm@77 853
rlm@77 854 (if @move-up?
rlm@77 855 (.applyForce control-a
rlm@77 856 (Vector3f. force 0 0)
rlm@77 857 (Vector3f. 0 0 0)))
rlm@77 858 (if @move-down?
rlm@77 859 (.applyForce control-a
rlm@77 860 (Vector3f. (- force) 0 0)
rlm@77 861 (Vector3f. 0 0 0)))
rlm@77 862 (if @move-left?
rlm@77 863 (.applyForce control-a
rlm@77 864 (Vector3f. 0 force 0)
rlm@77 865 (Vector3f. 0 0 0)))
rlm@77 866 (if @move-right?
rlm@77 867 (.applyForce control-a
rlm@77 868 (Vector3f. 0 (- force) 0)
rlm@77 869 (Vector3f. 0 0 0)))
rlm@77 870
rlm@77 871 (if @roll-left?
rlm@77 872 (.applyForce control-a
rlm@77 873 (Vector3f. 0 0 force)
rlm@77 874 (Vector3f. 0 0 0)))
rlm@77 875 (if @roll-right?
rlm@77 876 (.applyForce control-a
rlm@77 877 (Vector3f. 0 0 (- force))
rlm@77 878 (Vector3f. 0 0 0)))
rlm@77 879
rlm@77 880 (if (zero? (rem (swap! timer inc) 100))
rlm@77 881 (.attachChild
rlm@77 882 (.getRootNode world)
rlm@77 883 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 884 :physical? false :position
rlm@77 885 (.getWorldTranslation obj-a)))))
rlm@77 886 )
rlm@77 887 ))
rlm@77 888
rlm@94 889 (defn transform-trianglesdsd
rlm@94 890 "Transform that converts each vertex in the first triangle
rlm@94 891 into the corresponding vertex in the second triangle."
rlm@94 892 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 893 (let [in [(.get1 tri-1)
rlm@94 894 (.get2 tri-1)
rlm@94 895 (.get3 tri-1)]
rlm@94 896 out [(.get1 tri-2)
rlm@94 897 (.get2 tri-2)
rlm@94 898 (.get3 tri-2)]]
rlm@94 899 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 900 in* [(.mult translate (in 0))
rlm@94 901 (.mult translate (in 1))
rlm@94 902 (.mult translate (in 2))]
rlm@94 903 final-translation
rlm@94 904 (doto (Matrix4f.)
rlm@94 905 (.setTranslation (out 1)))
rlm@94 906
rlm@94 907 rotate-1
rlm@94 908 (doto (Matrix3f.)
rlm@94 909 (.fromStartEndVectors
rlm@94 910 (.normalize
rlm@94 911 (.subtract
rlm@94 912 (in* 1) (in* 0)))
rlm@94 913 (.normalize
rlm@94 914 (.subtract
rlm@94 915 (out 1) (out 0)))))
rlm@94 916 in** [(.mult rotate-1 (in* 0))
rlm@94 917 (.mult rotate-1 (in* 1))
rlm@94 918 (.mult rotate-1 (in* 2))]
rlm@94 919 scale-factor-1
rlm@94 920 (.mult
rlm@94 921 (.normalize
rlm@94 922 (.subtract
rlm@94 923 (out 1)
rlm@94 924 (out 0)))
rlm@94 925 (/ (.length
rlm@94 926 (.subtract (out 1)
rlm@94 927 (out 0)))
rlm@94 928 (.length
rlm@94 929 (.subtract (in** 1)
rlm@94 930 (in** 0)))))
rlm@94 931 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 932 in*** [(.mult scale-1 (in** 0))
rlm@94 933 (.mult scale-1 (in** 1))
rlm@94 934 (.mult scale-1 (in** 2))]
rlm@94 935
rlm@94 936
rlm@94 937
rlm@94 938
rlm@94 939
rlm@94 940 ]
rlm@94 941
rlm@94 942 (dorun (map println in))
rlm@94 943 (println)
rlm@94 944 (dorun (map println in*))
rlm@94 945 (println)
rlm@94 946 (dorun (map println in**))
rlm@94 947 (println)
rlm@94 948 (dorun (map println in***))
rlm@94 949 (println)
rlm@94 950
rlm@99 951 ))))
rlm@94 952
rlm@94 953
rlm@106 954 (defn world-setup [joint]
rlm@106 955 (let [joint-position (Vector3f. 0 0 0)
rlm@106 956 joint-rotation
rlm@106 957 (.toRotationMatrix
rlm@106 958 (.mult
rlm@106 959 (doto (Quaternion.)
rlm@106 960 (.fromAngleAxis
rlm@106 961 (* 1 (/ Math/PI 4))
rlm@106 962 (Vector3f. -1 0 0)))
rlm@106 963 (doto (Quaternion.)
rlm@106 964 (.fromAngleAxis
rlm@106 965 (* 1 (/ Math/PI 2))
rlm@106 966 (Vector3f. 0 0 1)))))
rlm@106 967 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 968
rlm@106 969 origin (doto
rlm@106 970 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 971 :position top-position))
rlm@106 972 top (doto
rlm@106 973 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 974 :position top-position)
rlm@106 975
rlm@106 976 (.addControl
rlm@106 977 (RigidBodyControl.
rlm@106 978 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 979 bottom (doto
rlm@106 980 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 981 :position (Vector3f. 0 0 0))
rlm@106 982 (.addControl
rlm@106 983 (RigidBodyControl.
rlm@106 984 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 985 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 986 :color ColorRGBA/Gray :mass 0)
rlm@106 987 a (.getControl top RigidBodyControl)
rlm@106 988 b (.getControl bottom RigidBodyControl)]
rlm@106 989
rlm@106 990 (cond
rlm@106 991 (= joint :cone)
rlm@106 992
rlm@106 993 (doto (ConeJoint.
rlm@106 994 a b
rlm@106 995 (world-to-local top joint-position)
rlm@106 996 (world-to-local bottom joint-position)
rlm@106 997 joint-rotation
rlm@106 998 joint-rotation
rlm@106 999 )
rlm@106 1000
rlm@106 1001
rlm@106 1002 (.setLimit (* (/ 10) Math/PI)
rlm@106 1003 (* (/ 4) Math/PI)
rlm@106 1004 0)))
rlm@106 1005 [origin top bottom table]))
rlm@106 1006
rlm@106 1007 (defn test-joint [joint]
rlm@106 1008 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1009 control (.getControl top RigidBodyControl)
rlm@106 1010 move-up? (atom false)
rlm@106 1011 move-down? (atom false)
rlm@106 1012 move-left? (atom false)
rlm@106 1013 move-right? (atom false)
rlm@106 1014 roll-left? (atom false)
rlm@106 1015 roll-right? (atom false)
rlm@106 1016 timer (atom 0)]
rlm@106 1017
rlm@106 1018 (world
rlm@106 1019 (nodify [top bottom floor origin])
rlm@106 1020 (merge standard-debug-controls
rlm@106 1021 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1022 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1023 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1024 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1025 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1026 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1027
rlm@106 1028 (fn [world]
rlm@106 1029 (light-up-everything world)
rlm@106 1030 (enable-debug world)
rlm@106 1031 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1032 )
rlm@106 1033
rlm@106 1034 (fn [world _]
rlm@106 1035 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1036 (do
rlm@106 1037 ;; (println-repl @timer)
rlm@106 1038 (.attachChild (.getRootNode world)
rlm@106 1039 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1040 :position (.getWorldTranslation top)
rlm@106 1041 :physical? false))
rlm@106 1042 (.attachChild (.getRootNode world)
rlm@106 1043 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1044 :position (.getWorldTranslation bottom)
rlm@106 1045 :physical? false))))
rlm@106 1046
rlm@106 1047 (if @move-up?
rlm@106 1048 (.applyTorque control
rlm@106 1049 (.mult (.getPhysicsRotation control)
rlm@106 1050 (Vector3f. 0 0 10))))
rlm@106 1051 (if @move-down?
rlm@106 1052 (.applyTorque control
rlm@106 1053 (.mult (.getPhysicsRotation control)
rlm@106 1054 (Vector3f. 0 0 -10))))
rlm@106 1055 (if @move-left?
rlm@106 1056 (.applyTorque control
rlm@106 1057 (.mult (.getPhysicsRotation control)
rlm@106 1058 (Vector3f. 0 10 0))))
rlm@106 1059 (if @move-right?
rlm@106 1060 (.applyTorque control
rlm@106 1061 (.mult (.getPhysicsRotation control)
rlm@106 1062 (Vector3f. 0 -10 0))))
rlm@106 1063 (if @roll-left?
rlm@106 1064 (.applyTorque control
rlm@106 1065 (.mult (.getPhysicsRotation control)
rlm@106 1066 (Vector3f. -1 0 0))))
rlm@106 1067 (if @roll-right?
rlm@106 1068 (.applyTorque control
rlm@106 1069 (.mult (.getPhysicsRotation control)
rlm@106 1070 (Vector3f. 1 0 0))))))))
rlm@106 1071
rlm@99 1072
rlm@99 1073
rlm@107 1074 (defprotocol Frame
rlm@107 1075 (frame [this]))
rlm@107 1076
rlm@107 1077 (extend-type BufferedImage
rlm@107 1078 Frame
rlm@107 1079 (frame [image]
rlm@107 1080 (merge
rlm@107 1081 (apply
rlm@107 1082 hash-map
rlm@107 1083 (interleave
rlm@107 1084 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1085 (vector x y)))
rlm@107 1086 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1087 (let [data (.getRGB image x y)]
rlm@107 1088 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1089 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1090 :b (bit-and 0x0000ff data)))))))
rlm@107 1091 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1092
rlm@107 1093
rlm@107 1094 (extend-type ImagePlus
rlm@107 1095 Frame
rlm@107 1096 (frame [image+]
rlm@107 1097 (frame (.getBufferedImage image+))))
rlm@107 1098
rlm@107 1099
rlm@99 1100 #+end_src
rlm@99 1101
rlm@99 1102
rlm@99 1103 * COMMENT generate source
rlm@99 1104 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1105 <<body-1>>
rlm@99 1106 #+end_src
rlm@99 1107
rlm@99 1108
rlm@94 1109
rlm@94 1110