annotate org/test-creature.org @ 114:9d0fe7f54e14

merged image viewing code to cortex.util
author Robert McIntyre <rlm@mit.edu>
date Thu, 19 Jan 2012 22:19:24 -0700
parents 128fa71ee188
children 247860e25536
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@108 68 (use 'clojure.contrib.def)
rlm@73 69
rlm@100 70 (defn points->image
rlm@100 71 "Take a sparse collection of points and visuliaze it as a
rlm@100 72 BufferedImage."
rlm@102 73
rlm@102 74 ;; TODO maybe parallelize this since it's easy
rlm@102 75
rlm@100 76 [points]
rlm@106 77 (if (empty? points)
rlm@106 78 (BufferedImage. 1 1 BufferedImage/TYPE_BYTE_BINARY)
rlm@106 79 (let [xs (vec (map first points))
rlm@106 80 ys (vec (map second points))
rlm@106 81 x0 (apply min xs)
rlm@106 82 y0 (apply min ys)
rlm@106 83 width (- (apply max xs) x0)
rlm@106 84 height (- (apply max ys) y0)
rlm@106 85 image (BufferedImage. (inc width) (inc height)
rlm@106 86 BufferedImage/TYPE_BYTE_BINARY)]
rlm@106 87 (dorun
rlm@106 88 (for [index (range (count points))]
rlm@106 89 (.setRGB image (- (xs index) x0) (- (ys index) y0) -1)))
rlm@106 90
rlm@106 91 image)))
rlm@100 92
rlm@101 93 (defn test-data
rlm@101 94 []
rlm@101 95 (vec
rlm@102 96 (for [a (range 0 1000 2)
rlm@102 97 b (range 0 1000 2)]
rlm@101 98 (vector a b))
rlm@101 99 ))
rlm@99 100
rlm@101 101 (defn average [coll]
rlm@101 102 (/ (reduce + coll) (count coll)))
rlm@101 103
rlm@101 104 (defn collapse-1d
rlm@101 105 "One dimensional analogue of collapse"
rlm@101 106 [center line]
rlm@101 107 (let [length (count line)
rlm@101 108 num-above (count (filter (partial < center) line))
rlm@101 109 num-below (- length num-above)]
rlm@101 110 (range (- center num-below)
rlm@101 111 (+ center num-above))
rlm@101 112 ))
rlm@99 113
rlm@99 114 (defn collapse
rlm@99 115 "Take a set of pairs of integers and collapse them into a
rlm@99 116 contigous bitmap."
rlm@99 117 [points]
rlm@108 118 (if (empty? points) []
rlm@108 119 (let
rlm@108 120 [num-points (count points)
rlm@108 121 center (vector
rlm@108 122 (int (average (map first points)))
rlm@108 123 (int (average (map first points))))
rlm@108 124 flattened
rlm@108 125 (reduce
rlm@108 126 concat
rlm@108 127 (map
rlm@108 128 (fn [column]
rlm@108 129 (map vector
rlm@108 130 (map first column)
rlm@108 131 (collapse-1d (second center)
rlm@108 132 (map second column))))
rlm@108 133 (partition-by first (sort-by first points))))
rlm@108 134 squeezed
rlm@108 135 (reduce
rlm@108 136 concat
rlm@108 137 (map
rlm@108 138 (fn [row]
rlm@108 139 (map vector
rlm@108 140 (collapse-1d (first center)
rlm@108 141 (map first row))
rlm@108 142 (map second row)))
rlm@108 143 (partition-by second (sort-by second flattened))))
rlm@108 144 relocate
rlm@108 145 (let [min-x (apply min (map first squeezed))
rlm@108 146 min-y (apply min (map second squeezed))]
rlm@108 147 (map (fn [[x y]]
rlm@108 148 [(- x min-x)
rlm@108 149 (- y min-y)])
rlm@108 150 squeezed))]
rlm@108 151 relocate
rlm@108 152 )))
rlm@83 153
rlm@83 154 (defn load-bullet []
rlm@84 155 (let [sim (world (Node.) {} no-op no-op)]
rlm@102 156 (doto sim
rlm@102 157 (.enqueue
rlm@102 158 (fn []
rlm@102 159 (.stop sim)))
rlm@102 160 (.start))))
rlm@83 161
rlm@73 162 (defn load-blender-model
rlm@73 163 "Load a .blend file using an asset folder relative path."
rlm@73 164 [^String model]
rlm@73 165 (.loadModel
rlm@73 166 (doto (asset-manager)
rlm@73 167 (.registerLoader BlenderModelLoader (into-array String ["blend"])))
rlm@73 168 model))
rlm@73 169
rlm@74 170 (defn meta-data [blender-node key]
rlm@74 171 (if-let [data (.getUserData blender-node "properties")]
rlm@74 172 (.findValue data key)
rlm@74 173 nil))
rlm@73 174
rlm@78 175 (defn blender-to-jme
rlm@78 176 "Convert from Blender coordinates to JME coordinates"
rlm@78 177 [#^Vector3f in]
rlm@78 178 (Vector3f. (.getX in)
rlm@78 179 (.getZ in)
rlm@78 180 (- (.getY in))))
rlm@74 181
rlm@79 182 (defn jme-to-blender
rlm@79 183 "Convert from JME coordinates to Blender coordinates"
rlm@79 184 [#^Vector3f in]
rlm@79 185 (Vector3f. (.getX in)
rlm@79 186 (- (.getZ in))
rlm@79 187 (.getY in)))
rlm@79 188
rlm@78 189 (defn joint-targets
rlm@78 190 "Return the two closest two objects to the joint object, ordered
rlm@78 191 from bottom to top according to the joint's rotation."
rlm@78 192 [#^Node parts #^Node joint]
rlm@78 193 ;;(println (meta-data joint "joint"))
rlm@78 194 (.getWorldRotation joint)
rlm@78 195 (loop [radius (float 0.01)]
rlm@78 196 (let [results (CollisionResults.)]
rlm@78 197 (.collideWith
rlm@78 198 parts
rlm@78 199 (BoundingBox. (.getWorldTranslation joint)
rlm@78 200 radius radius radius)
rlm@78 201 results)
rlm@78 202 (let [targets
rlm@78 203 (distinct
rlm@78 204 (map #(.getGeometry %) results))]
rlm@78 205 (if (>= (count targets) 2)
rlm@78 206 (sort-by
rlm@79 207 #(let [v
rlm@79 208 (jme-to-blender
rlm@79 209 (.mult
rlm@79 210 (.inverse (.getWorldRotation joint))
rlm@79 211 (.subtract (.getWorldTranslation %)
rlm@79 212 (.getWorldTranslation joint))))]
rlm@79 213 (println-repl (.getName %) ":" v)
rlm@79 214 (.dot (Vector3f. 1 1 1)
rlm@79 215 v))
rlm@78 216 (take 2 targets))
rlm@78 217 (recur (float (* radius 2))))))))
rlm@74 218
rlm@87 219 (defn world-to-local
rlm@87 220 "Convert the world coordinates into coordinates relative to the
rlm@87 221 object (i.e. local coordinates), taking into account the rotation
rlm@87 222 of object."
rlm@87 223 [#^Spatial object world-coordinate]
rlm@87 224 (let [out (Vector3f.)]
rlm@88 225 (.worldToLocal object world-coordinate out) out))
rlm@87 226
rlm@96 227 (defn local-to-world
rlm@96 228 "Convert the local coordinates into coordinates into world relative
rlm@96 229 coordinates"
rlm@96 230 [#^Spatial object local-coordinate]
rlm@96 231 (let [world-coordinate (Vector3f.)]
rlm@96 232 (.localToWorld object local-coordinate world-coordinate)
rlm@96 233 world-coordinate))
rlm@96 234
rlm@96 235
rlm@87 236 (defmulti joint-dispatch
rlm@87 237 "Translate blender pseudo-joints into real JME joints."
rlm@88 238 (fn [constraints & _]
rlm@87 239 (:type constraints)))
rlm@87 240
rlm@87 241 (defmethod joint-dispatch :point
rlm@87 242 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 243 (println-repl "creating POINT2POINT joint")
rlm@87 244 (Point2PointJoint.
rlm@87 245 control-a
rlm@87 246 control-b
rlm@87 247 pivot-a
rlm@87 248 pivot-b))
rlm@87 249
rlm@87 250 (defmethod joint-dispatch :hinge
rlm@87 251 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 252 (println-repl "creating HINGE joint")
rlm@87 253 (let [axis
rlm@87 254 (if-let
rlm@87 255 [axis (:axis constraints)]
rlm@87 256 axis
rlm@87 257 Vector3f/UNIT_X)
rlm@87 258 [limit-1 limit-2] (:limit constraints)
rlm@87 259 hinge-axis
rlm@87 260 (.mult
rlm@87 261 rotation
rlm@87 262 (blender-to-jme axis))]
rlm@87 263 (doto
rlm@87 264 (HingeJoint.
rlm@87 265 control-a
rlm@87 266 control-b
rlm@87 267 pivot-a
rlm@87 268 pivot-b
rlm@87 269 hinge-axis
rlm@87 270 hinge-axis)
rlm@87 271 (.setLimit limit-1 limit-2))))
rlm@87 272
rlm@87 273 (defmethod joint-dispatch :cone
rlm@87 274 [constraints control-a control-b pivot-a pivot-b rotation]
rlm@87 275 (let [limit-xz (:limit-xz constraints)
rlm@87 276 limit-xy (:limit-xy constraints)
rlm@87 277 twist (:twist constraints)]
rlm@87 278
rlm@87 279 (println-repl "creating CONE joint")
rlm@87 280 (println-repl rotation)
rlm@87 281 (println-repl
rlm@87 282 "UNIT_X --> " (.mult rotation (Vector3f. 1 0 0)))
rlm@87 283 (println-repl
rlm@87 284 "UNIT_Y --> " (.mult rotation (Vector3f. 0 1 0)))
rlm@87 285 (println-repl
rlm@87 286 "UNIT_Z --> " (.mult rotation (Vector3f. 0 0 1)))
rlm@87 287 (doto
rlm@87 288 (ConeJoint.
rlm@87 289 control-a
rlm@87 290 control-b
rlm@87 291 pivot-a
rlm@87 292 pivot-b
rlm@87 293 rotation
rlm@87 294 rotation)
rlm@87 295 (.setLimit (float limit-xz)
rlm@87 296 (float limit-xy)
rlm@87 297 (float twist)))))
rlm@87 298
rlm@88 299 (defn connect
rlm@87 300 "here are some examples:
rlm@87 301 {:type :point}
rlm@87 302 {:type :hinge :limit [0 (/ Math/PI 2)] :axis (Vector3f. 0 1 0)}
rlm@87 303 (:axis defaults to (Vector3f. 1 0 0) if not provided for hinge joints)
rlm@87 304
rlm@89 305 {:type :cone :limit-xz 0]
rlm@89 306 :limit-xy 0]
rlm@89 307 :twist 0]} (use XZY rotation mode in blender!)"
rlm@87 308 [#^Node obj-a #^Node obj-b #^Node joint]
rlm@87 309 (let [control-a (.getControl obj-a RigidBodyControl)
rlm@87 310 control-b (.getControl obj-b RigidBodyControl)
rlm@87 311 joint-center (.getWorldTranslation joint)
rlm@87 312 joint-rotation (.toRotationMatrix (.getWorldRotation joint))
rlm@87 313 pivot-a (world-to-local obj-a joint-center)
rlm@87 314 pivot-b (world-to-local obj-b joint-center)]
rlm@89 315
rlm@87 316 (if-let [constraints
rlm@87 317 (map-vals
rlm@87 318 eval
rlm@87 319 (read-string
rlm@87 320 (meta-data joint "joint")))]
rlm@89 321 ;; A side-effect of creating a joint registers
rlm@89 322 ;; it with both physics objects which in turn
rlm@89 323 ;; will register the joint with the physics system
rlm@89 324 ;; when the simulation is started.
rlm@87 325 (do
rlm@87 326 (println-repl "creating joint between"
rlm@87 327 (.getName obj-a) "and" (.getName obj-b))
rlm@87 328 (joint-dispatch constraints
rlm@87 329 control-a control-b
rlm@87 330 pivot-a pivot-b
rlm@87 331 joint-rotation))
rlm@87 332 (println-repl "could not find joint meta-data!"))))
rlm@87 333
rlm@78 334 (defn assemble-creature [#^Node pieces joints]
rlm@78 335 (dorun
rlm@78 336 (map
rlm@78 337 (fn [geom]
rlm@78 338 (let [physics-control
rlm@78 339 (RigidBodyControl.
rlm@78 340 (HullCollisionShape.
rlm@78 341 (.getMesh geom))
rlm@78 342 (if-let [mass (meta-data geom "mass")]
rlm@78 343 (do
rlm@78 344 (println-repl
rlm@78 345 "setting" (.getName geom) "mass to" (float mass))
rlm@78 346 (float mass))
rlm@78 347 (float 1)))]
rlm@78 348
rlm@78 349 (.addControl geom physics-control)))
rlm@78 350 (filter #(isa? (class %) Geometry )
rlm@78 351 (node-seq pieces))))
rlm@78 352 (dorun
rlm@78 353 (map
rlm@78 354 (fn [joint]
rlm@78 355 (let [[obj-a obj-b]
rlm@78 356 (joint-targets pieces joint)]
rlm@88 357 (connect obj-a obj-b joint)))
rlm@78 358 joints))
rlm@78 359 pieces)
rlm@74 360
rlm@78 361 (defn blender-creature [blender-path]
rlm@78 362 (let [model (load-blender-model blender-path)
rlm@78 363 joints
rlm@78 364 (if-let [joint-node (.getChild model "joints")]
rlm@78 365 (seq (.getChildren joint-node))
rlm@78 366 (do (println-repl "could not find joints node")
rlm@78 367 []))]
rlm@78 368 (assemble-creature model joints)))
rlm@74 369
rlm@78 370 (def hand "Models/creature1/one.blend")
rlm@74 371
rlm@78 372 (def worm "Models/creature1/try-again.blend")
rlm@78 373
rlm@90 374 (def touch "Models/creature1/touch.blend")
rlm@90 375
rlm@90 376 (defn worm-model [] (load-blender-model worm))
rlm@90 377
rlm@80 378 (defn x-ray [#^ColorRGBA color]
rlm@80 379 (doto (Material. (asset-manager)
rlm@80 380 "Common/MatDefs/Misc/Unshaded.j3md")
rlm@80 381 (.setColor "Color" color)
rlm@80 382 (-> (.getAdditionalRenderState)
rlm@80 383 (.setDepthTest false))))
rlm@80 384
rlm@91 385 (defn colorful []
rlm@91 386 (.getChild (worm-model) "worm-21"))
rlm@90 387
rlm@90 388 (import jme3tools.converters.ImageToAwt)
rlm@90 389
rlm@90 390 (import ij.ImagePlus)
rlm@90 391
rlm@108 392 ;; Every Mesh has many triangles, each with its own index.
rlm@108 393 ;; Every vertex has its own index as well.
rlm@90 394
rlm@108 395 (defn tactile-sensor-image
rlm@110 396 "Return the touch-sensor distribution image in BufferedImage format,
rlm@110 397 or nil if it does not exist."
rlm@91 398 [#^Geometry obj]
rlm@110 399 (if-let [image-path (meta-data obj "touch")]
rlm@110 400 (ImageToAwt/convert
rlm@110 401 (.getImage
rlm@110 402 (.loadTexture
rlm@110 403 (asset-manager)
rlm@110 404 image-path))
rlm@110 405 false false 0)))
rlm@110 406
rlm@91 407 (import ij.process.ImageProcessor)
rlm@91 408 (import java.awt.image.BufferedImage)
rlm@91 409
rlm@92 410 (def white -1)
rlm@94 411
rlm@91 412 (defn filter-pixels
rlm@108 413 "List the coordinates of all pixels matching pred, within the bounds
rlm@108 414 provided. Bounds -> [x0 y0 width height]"
rlm@92 415 {:author "Dylan Holmes"}
rlm@108 416 ([pred #^BufferedImage image]
rlm@108 417 (filter-pixels pred image [0 0 (.getWidth image) (.getHeight image)]))
rlm@108 418 ([pred #^BufferedImage image [x0 y0 width height]]
rlm@108 419 ((fn accumulate [x y matches]
rlm@108 420 (cond
rlm@108 421 (>= y (+ height y0)) matches
rlm@108 422 (>= x (+ width x0)) (recur 0 (inc y) matches)
rlm@108 423 (pred (.getRGB image x y))
rlm@108 424 (recur (inc x) y (conj matches [x y]))
rlm@108 425 :else (recur (inc x) y matches)))
rlm@108 426 x0 y0 [])))
rlm@91 427
rlm@91 428 (defn white-coordinates
rlm@108 429 "Coordinates of all the white pixels in a subset of the image."
rlm@112 430 ([#^BufferedImage image bounds]
rlm@112 431 (filter-pixels #(= % white) image bounds))
rlm@112 432 ([#^BufferedImage image]
rlm@112 433 (filter-pixels #(= % white) image)))
rlm@108 434
rlm@108 435 (defn triangle
rlm@112 436 "Get the triangle specified by triangle-index from the mesh within
rlm@112 437 bounds."
rlm@108 438 [#^Mesh mesh triangle-index]
rlm@108 439 (let [scratch (Triangle.)]
rlm@108 440 (.getTriangle mesh triangle-index scratch)
rlm@108 441 scratch))
rlm@108 442
rlm@108 443 (defn triangle-vertex-indices
rlm@108 444 "Get the triangle vertex indices of a given triangle from a given
rlm@108 445 mesh."
rlm@108 446 [#^Mesh mesh triangle-index]
rlm@108 447 (let [indices (int-array 3)]
rlm@108 448 (.getTriangle mesh triangle-index indices)
rlm@108 449 (vec indices)))
rlm@108 450
rlm@108 451 (defn vertex-UV-coord
rlm@108 452 "Get the uv-coordinates of the vertex named by vertex-index"
rlm@108 453 [#^Mesh mesh vertex-index]
rlm@108 454 (let [UV-buffer
rlm@108 455 (.getData
rlm@108 456 (.getBuffer
rlm@108 457 mesh
rlm@108 458 VertexBuffer$Type/TexCoord))]
rlm@108 459 [(.get UV-buffer (* vertex-index 2))
rlm@108 460 (.get UV-buffer (+ 1 (* vertex-index 2)))]))
rlm@108 461
rlm@108 462 (defn triangle-UV-coord
rlm@108 463 "Get the uv-cooridnates of the triangle's verticies."
rlm@108 464 [#^Mesh mesh width height triangle-index]
rlm@108 465 (map (fn [[u v]] (vector (* width u) (* height v)))
rlm@108 466 (map (partial vertex-UV-coord mesh)
rlm@108 467 (triangle-vertex-indices mesh triangle-index))))
rlm@91 468
rlm@102 469 (defn same-side?
rlm@102 470 "Given the points p1 and p2 and the reference point ref, is point p
rlm@102 471 on the same side of the line that goes through p1 and p2 as ref is?"
rlm@102 472 [p1 p2 ref p]
rlm@91 473 (<=
rlm@91 474 0
rlm@91 475 (.dot
rlm@91 476 (.cross (.subtract p2 p1) (.subtract p p1))
rlm@91 477 (.cross (.subtract p2 p1) (.subtract ref p1)))))
rlm@91 478
rlm@108 479 (defn triangle-seq [#^Triangle tri]
rlm@108 480 [(.get1 tri) (.get2 tri) (.get3 tri)])
rlm@108 481
rlm@108 482 (defn vector3f-seq [#^Vector3f v]
rlm@108 483 [(.getX v) (.getY v) (.getZ v)])
rlm@108 484
rlm@108 485 (defn inside-triangle?
rlm@108 486 "Is the point inside the triangle?"
rlm@108 487 {:author "Dylan Holmes"}
rlm@108 488 [#^Triangle tri #^Vector3f p]
rlm@108 489 (let [[vert-1 vert-2 vert-3] (triangle-seq tri)]
rlm@108 490 (and
rlm@108 491 (same-side? vert-1 vert-2 vert-3 p)
rlm@108 492 (same-side? vert-2 vert-3 vert-1 p)
rlm@108 493 (same-side? vert-3 vert-1 vert-2 p))))
rlm@108 494
rlm@94 495 (defn triangle->matrix4f
rlm@108 496 "Converts the triangle into a 4x4 matrix: The first three columns
rlm@108 497 contain the vertices of the triangle; the last contains the unit
rlm@108 498 normal of the triangle. The bottom row is filled with 1s."
rlm@94 499 [#^Triangle t]
rlm@94 500 (let [mat (Matrix4f.)
rlm@94 501 [vert-1 vert-2 vert-3]
rlm@94 502 ((comp vec map) #(.get t %) (range 3))
rlm@94 503 unit-normal (do (.calculateNormal t)(.getNormal t))
rlm@94 504 vertices [vert-1 vert-2 vert-3 unit-normal]]
rlm@94 505 (dorun
rlm@94 506 (for [row (range 4) col (range 3)]
rlm@94 507 (do
rlm@94 508 (.set mat col row (.get (vertices row)col))
rlm@94 509 (.set mat 3 row 1))))
rlm@94 510 mat))
rlm@94 511
rlm@94 512 (defn triangle-transformation
rlm@94 513 "Returns the affine transformation that converts each vertex in the
rlm@94 514 first triangle into the corresponding vertex in the second
rlm@94 515 triangle."
rlm@94 516 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 517 (.mult
rlm@94 518 (triangle->matrix4f tri-2)
rlm@94 519 (.invert (triangle->matrix4f tri-1))))
rlm@94 520
rlm@108 521 (defn point->vector2f [[u v]]
rlm@108 522 (Vector2f. u v))
rlm@94 523
rlm@94 524 (defn vector2f->vector3f [v]
rlm@94 525 (Vector3f. (.getX v) (.getY v) 0))
rlm@94 526
rlm@94 527 (defn map-triangle [f #^Triangle tri]
rlm@94 528 (Triangle.
rlm@94 529 (f 0 (.get1 tri))
rlm@94 530 (f 1 (.get2 tri))
rlm@94 531 (f 2 (.get3 tri))))
rlm@94 532
rlm@108 533 (defn points->triangle
rlm@108 534 "Convert a list of points into a triangle."
rlm@108 535 [points]
rlm@108 536 (apply #(Triangle. %1 %2 %3)
rlm@108 537 (map (fn [point]
rlm@108 538 (let [point (vec point)]
rlm@108 539 (Vector3f. (get point 0 0)
rlm@108 540 (get point 1 0)
rlm@108 541 (get point 2 0))))
rlm@108 542 (take 3 points))))
rlm@94 543
rlm@108 544 (defn convex-bounds
rlm@108 545 "Dimensions of the smallest integer bounding square of the list of
rlm@108 546 2D verticies in the form: [x y width height]."
rlm@108 547 [uv-verts]
rlm@108 548 (let [xs (map first uv-verts)
rlm@108 549 ys (map second uv-verts)
rlm@108 550 x0 (Math/floor (apply min xs))
rlm@108 551 y0 (Math/floor (apply min ys))
rlm@108 552 x1 (Math/ceil (apply max xs))
rlm@108 553 y1 (Math/ceil (apply max ys))]
rlm@108 554 [x0 y0 (- x1 x0) (- y1 y0)]))
rlm@93 555
rlm@106 556 (defn sensors-in-triangle
rlm@107 557 "Find the locations of the touch sensors within a triangle in both
rlm@107 558 UV and gemoetry relative coordinates."
rlm@107 559 [image mesh tri-index]
rlm@107 560 (let [width (.getWidth image)
rlm@108 561 height (.getHeight image)
rlm@108 562 UV-vertex-coords (triangle-UV-coord mesh width height tri-index)
rlm@108 563 bounds (convex-bounds UV-vertex-coords)
rlm@108 564
rlm@108 565 cutout-triangle (points->triangle UV-vertex-coords)
rlm@108 566 UV-sensor-coords
rlm@108 567 (filter (comp (partial inside-triangle? cutout-triangle)
rlm@108 568 (fn [[u v]] (Vector3f. u v 0)))
rlm@108 569 (white-coordinates image bounds))
rlm@108 570 UV->geometry (triangle-transformation
rlm@108 571 cutout-triangle
rlm@108 572 (triangle mesh tri-index))
rlm@108 573 geometry-sensor-coords
rlm@108 574 (map (fn [[u v]] (.mult UV->geometry (Vector3f. u v 0)))
rlm@108 575 UV-sensor-coords)]
rlm@108 576 {:UV UV-sensor-coords :geometry geometry-sensor-coords}))
rlm@107 577
rlm@108 578 (defn-memo locate-feelers
rlm@94 579 "Search the geometry's tactile UV image for touch sensors, returning
rlm@94 580 their positions in geometry-relative coordinates."
rlm@94 581 [#^Geometry geo]
rlm@108 582 (let [mesh (.getMesh geo)
rlm@108 583 num-triangles (.getTriangleCount mesh)]
rlm@108 584 (if-let [image (tactile-sensor-image geo)]
rlm@108 585 (map
rlm@108 586 (partial sensors-in-triangle image mesh)
rlm@108 587 (range num-triangles))
rlm@108 588 (repeat (.getTriangleCount mesh) {:UV nil :geometry nil}))))
rlm@102 589
rlm@102 590 (use 'clojure.contrib.def)
rlm@102 591
rlm@102 592 (defn-memo touch-topology [#^Gemoetry geo]
rlm@108 593 (vec (collapse (reduce concat (map :UV (locate-feelers geo))))))
rlm@108 594
rlm@108 595 (defn-memo feeler-coordinates [#^Geometry geo]
rlm@108 596 (vec (map :geometry (locate-feelers geo))))
rlm@102 597
rlm@97 598 (defn enable-touch [#^Geometry geo]
rlm@108 599 (let [feeler-coords (feeler-coordinates geo)
rlm@96 600 tris (triangles geo)
rlm@109 601 limit 0.1
rlm@109 602 ;;results (CollisionResults.)
rlm@109 603 ]
rlm@111 604 (if (empty? (touch-topology geo))
rlm@111 605 nil
rlm@111 606 (fn [node]
rlm@111 607 (let [sensor-origins
rlm@111 608 (map
rlm@111 609 #(map (partial local-to-world geo) %)
rlm@111 610 feeler-coords)
rlm@111 611 triangle-normals
rlm@111 612 (map (partial get-ray-direction geo)
rlm@111 613 tris)
rlm@111 614 rays
rlm@111 615 (flatten
rlm@111 616 (map (fn [origins norm]
rlm@111 617 (map #(doto (Ray. % norm)
rlm@97 618 (.setLimit limit)) origins))
rlm@111 619 sensor-origins triangle-normals))]
rlm@111 620 (vector
rlm@111 621 (touch-topology geo)
rlm@111 622 (vec
rlm@111 623 (for [ray rays]
rlm@111 624 (do
rlm@111 625 (let [results (CollisionResults.)]
rlm@111 626 (.collideWith node ray results)
rlm@111 627 (let [touch-objects
rlm@111 628 (set
rlm@111 629 (filter #(not (= geo %))
rlm@111 630 (map #(.getGeometry %) results)))]
rlm@111 631 (if (> (count touch-objects) 0)
rlm@111 632 1 0))))))))))))
rlm@111 633
rlm@111 634 (defn touch [#^Node pieces]
rlm@111 635 (filter (comp not nil?)
rlm@111 636 (map enable-touch
rlm@111 637 (filter #(isa? (class %) Geometry)
rlm@111 638 (node-seq pieces)))))
rlm@94 639
rlm@109 640
rlm@111 641 ;; human eye transmits 62kb/s to brain Bandwidth is 8.75 Mb/s
rlm@111 642 ;; http://en.wikipedia.org/wiki/Retina
rlm@109 643
rlm@111 644 (defn test-eye []
rlm@111 645 (.getChild (worm-model) "worm-11"))
rlm@111 646
rlm@111 647
rlm@111 648 (defn retina-sensor-image
rlm@111 649 "Return a map of pixel selection functions to BufferedImages
rlm@111 650 describing the distribution of light-sensitive components on this
rlm@111 651 geometry's surface. Each function creates an integer from the rgb
rlm@111 652 values found in the pixel. :red, :green, :blue, :gray are already
rlm@111 653 defined as extracting the red green blue and average components
rlm@111 654 respectively."
rlm@111 655 [#^Geometry eye]
rlm@111 656 (if-let [eye-map (meta-data eye "eye")]
rlm@111 657 (map-vals
rlm@111 658 #(ImageToAwt/convert
rlm@111 659 (.getImage (.loadTexture (asset-manager) %))
rlm@111 660 false false 0)
rlm@111 661 (read-string
rlm@111 662 eye-map))))
rlm@111 663
rlm@111 664
rlm@112 665 (defn enable-vision
rlm@111 666
rlm@112 667 ;; need to create a camera based on uv image,
rlm@112 668 ;; update this camera every frame based on the position of this
rlm@112 669 ;; geometry. (maybe can get cam to follow the object)
rlm@111 670
rlm@112 671 ;; use a stack for the continuation to grab the image.
rlm@112 672
rlm@112 673
rlm@112 674 [#^Geometry eye]
rlm@112 675
rlm@112 676
rlm@112 677 ;; Here's how vision will work.
rlm@112 678
rlm@112 679 ;; Make the continuation in scene-processor take FrameBuffer,
rlm@112 680 ;; byte-buffer, BufferedImage already sized to the correct
rlm@112 681 ;; dimensions. the continuation will decide wether to "mix" them
rlm@112 682 ;; into the BufferedImage, lazily ignore them, or mix them halfway
rlm@112 683 ;; and call c/graphics card routines.
rlm@112 684
rlm@112 685 ;; (vision creature) will take an optional :skip argument which will
rlm@112 686 ;; inform the continuations in scene processor to skip the given
rlm@112 687 ;; number of cycles; 0 means that no cycles will be skipped.
rlm@112 688
rlm@112 689 ;; (vision creature) will return [init-functions sensor-functions].
rlm@112 690 ;; The init-functions are each single-arg functions that take the
rlm@112 691 ;; world and register the cameras and must each be called before the
rlm@112 692 ;; corresponding sensor-functions. Each init-function returns the
rlm@112 693 ;; viewport for that eye which can be manipulated, saved, etc. Each
rlm@112 694 ;; sensor-function is a thunk and will return data in the same
rlm@112 695 ;; format as the tactile-sensor functions; the structure is
rlm@112 696 ;; [topology, sensor-data]. Internally, these sensor-functions
rlm@112 697 ;; maintain a reference to sensor-data which is periodically updated
rlm@112 698 ;; by the continuation function established by its init-function.
rlm@112 699 ;; They can be queried every cycle, but their information may not
rlm@112 700 ;; necessairly be different every cycle.
rlm@112 701
rlm@112 702 ;; Each eye in the creature in blender will work the same way as
rlm@112 703 ;; joints -- a one dimensional object with no geometry whose local
rlm@112 704 ;; coordinate system determines the orientation of the resulting
rlm@112 705 ;; eye. All eyes will have a parent named "eyes" just as all joints
rlm@112 706 ;; have a parent named "joints". The resulting camera will be a
rlm@112 707 ;; ChaseCamera or a CameraNode bound to the geo that is closest to
rlm@112 708 ;; the eye marker. The eye marker will contain the metadata for the
rlm@112 709 ;; eye, and will be moved by it's bound geometry. The dimensions of
rlm@112 710 ;; the eye's camera are equal to the dimensions of the eye's "UV"
rlm@112 711 ;; map.
rlm@112 712
rlm@112 713
rlm@112 714 )
rlm@102 715
rlm@103 716 (defn debug-window
rlm@103 717 "creates function that offers a debug view of sensor data"
rlm@103 718 []
rlm@103 719 (let [vi (view-image)]
rlm@103 720 (fn
rlm@103 721 [[coords sensor-data]]
rlm@103 722 (let [image (points->image coords)]
rlm@103 723 (dorun
rlm@103 724 (for [i (range (count coords))]
rlm@103 725 (.setRGB image ((coords i) 0) ((coords i) 1)
rlm@103 726 ({0 -16777216
rlm@103 727 1 -1} (sensor-data i)))))
rlm@103 728 (vi image)))))
rlm@103 729
rlm@83 730
rlm@106 731 ;;(defn test-touch [world creature]
rlm@83 732
rlm@78 733
rlm@106 734 (defn test-creature [thing]
rlm@106 735 (let [x-axis
rlm@106 736 (box 1 0.01 0.01 :physical? false :color ColorRGBA/Red)
rlm@106 737 y-axis
rlm@106 738 (box 0.01 1 0.01 :physical? false :color ColorRGBA/Green)
rlm@106 739 z-axis
rlm@106 740 (box 0.01 0.01 1 :physical? false :color ColorRGBA/Blue)
rlm@106 741 creature (blender-creature thing)
rlm@106 742 touch-nerves (touch creature)
rlm@106 743 touch-debug-windows (map (fn [_] (debug-window)) touch-nerves)
rlm@106 744 ]
rlm@106 745 (world
rlm@106 746 (nodify [creature
rlm@106 747 (box 10 2 10 :position (Vector3f. 0 -9 0)
rlm@106 748 :color ColorRGBA/Gray :mass 0)
rlm@106 749 x-axis y-axis z-axis
rlm@106 750 ])
rlm@106 751 standard-debug-controls
rlm@106 752 (fn [world]
rlm@106 753 (light-up-everything world)
rlm@106 754 (enable-debug world)
rlm@106 755 ;;(com.aurellem.capture.Capture/captureVideo
rlm@106 756 ;; world (file-str "/home/r/proj/ai-videos/hand"))
rlm@110 757 ;;(.setTimer world (RatchetTimer. 60))
rlm@110 758 ;;(speed-up world)
rlm@106 759 ;;(set-gravity world (Vector3f. 0 0 0))
rlm@106 760 )
rlm@106 761 (fn [world tpf]
rlm@109 762 ;;(dorun
rlm@109 763 ;; (map #(%1 %2) touch-nerves (repeat (.getRootNode world))))
rlm@110 764
rlm@106 765 (dorun
rlm@109 766 (map #(%1 (%2 (.getRootNode world)))
rlm@110 767 touch-debug-windows touch-nerves)
rlm@110 768 )
rlm@109 769
rlm@106 770 )
rlm@106 771 ;;(let [timer (atom 0)]
rlm@106 772 ;; (fn [_ _]
rlm@106 773 ;; (swap! timer inc)
rlm@106 774 ;; (if (= (rem @timer 60) 0)
rlm@106 775 ;; (println-repl (float (/ @timer 60))))))
rlm@106 776 )))
rlm@83 777
rlm@109 778
rlm@109 779
rlm@109 780
rlm@109 781
rlm@109 782
rlm@109 783
rlm@109 784
rlm@109 785
rlm@109 786 ;;; experiments in collisions
rlm@109 787
rlm@109 788
rlm@109 789
rlm@109 790 (defn collision-test []
rlm@110 791 (let [b-radius 1
rlm@110 792 b-position (Vector3f. 0 0 0)
rlm@109 793 obj-b (box 1 1 1 :color ColorRGBA/Blue
rlm@109 794 :position b-position
rlm@110 795 :mass 0)
rlm@110 796 node (nodify [obj-b])
rlm@110 797 bounds-b
rlm@110 798 (doto (Picture.)
rlm@110 799 (.setHeight 50)
rlm@110 800 (.setWidth 50)
rlm@110 801 (.setImage (asset-manager)
rlm@110 802 "Models/creature1/hand.png"
rlm@110 803 false
rlm@110 804 ))
rlm@110 805
rlm@110 806 ;;(Ray. (Vector3f. 0 -5 0) (.normalize (Vector3f. 0 1 0)))
rlm@110 807
rlm@110 808 collisions
rlm@110 809 (let [cr (CollisionResults.)]
rlm@110 810 (.collideWith node bounds-b cr)
rlm@110 811 (println (map #(.getContactPoint %) cr))
rlm@110 812 cr)
rlm@110 813
rlm@110 814 ;;collision-points
rlm@110 815 ;;(map #(sphere 0.1 :position (.getContactPoint %))
rlm@110 816 ;; collisions)
rlm@110 817
rlm@110 818 ;;node (nodify (conj collision-points obj-b))
rlm@110 819
rlm@109 820 sim
rlm@109 821 (world node
rlm@110 822 {"key-space"
rlm@110 823 (fn [_ value]
rlm@110 824 (if value
rlm@110 825 (let [cr (CollisionResults.)]
rlm@110 826 (.collideWith node bounds-b cr)
rlm@110 827 (println-repl (map #(.getContactPoint %) cr))
rlm@110 828 cr)))}
rlm@109 829 no-op
rlm@109 830 no-op)
rlm@109 831
rlm@109 832 ]
rlm@110 833 sim
rlm@109 834
rlm@109 835 ))
rlm@109 836
rlm@109 837
rlm@109 838
rlm@109 839
rlm@87 840 #+end_src
rlm@83 841
rlm@87 842 #+results: body-1
rlm@109 843 : #'cortex.silly/test-creature
rlm@78 844
rlm@78 845
rlm@78 846 * COMMENT purgatory
rlm@78 847 #+begin_src clojure
rlm@77 848 (defn bullet-trans []
rlm@77 849 (let [obj-a (sphere 0.5 :color ColorRGBA/Red
rlm@77 850 :position (Vector3f. -10 5 0))
rlm@77 851 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 852 :position (Vector3f. -10 -5 0)
rlm@77 853 :mass 0)
rlm@77 854 control-a (.getControl obj-a RigidBodyControl)
rlm@77 855 control-b (.getControl obj-b RigidBodyControl)
rlm@77 856 swivel
rlm@77 857 (.toRotationMatrix
rlm@77 858 (doto (Quaternion.)
rlm@77 859 (.fromAngleAxis (/ Math/PI 2)
rlm@77 860 Vector3f/UNIT_X)))]
rlm@77 861 (doto
rlm@77 862 (ConeJoint.
rlm@77 863 control-a control-b
rlm@77 864 (Vector3f. 0 5 0)
rlm@77 865 (Vector3f. 0 -5 0)
rlm@77 866 swivel swivel)
rlm@77 867 (.setLimit (* 0.6 (/ Math/PI 4))
rlm@77 868 (/ Math/PI 4)
rlm@77 869 (* Math/PI 0.8)))
rlm@77 870 (world (nodify
rlm@77 871 [obj-a obj-b])
rlm@77 872 standard-debug-controls
rlm@77 873 enable-debug
rlm@77 874 no-op)))
rlm@74 875
rlm@74 876
rlm@77 877 (defn bullet-trans* []
rlm@77 878 (let [obj-a (box 1.5 0.5 0.5 :color ColorRGBA/Red
rlm@77 879 :position (Vector3f. 5 0 0)
rlm@77 880 :mass 90)
rlm@77 881 obj-b (sphere 0.5 :color ColorRGBA/Blue
rlm@77 882 :position (Vector3f. -5 0 0)
rlm@77 883 :mass 0)
rlm@77 884 control-a (.getControl obj-a RigidBodyControl)
rlm@77 885 control-b (.getControl obj-b RigidBodyControl)
rlm@77 886 move-up? (atom nil)
rlm@77 887 move-down? (atom nil)
rlm@77 888 move-left? (atom nil)
rlm@77 889 move-right? (atom nil)
rlm@77 890 roll-left? (atom nil)
rlm@77 891 roll-right? (atom nil)
rlm@77 892 force 100
rlm@77 893 swivel
rlm@77 894 (.toRotationMatrix
rlm@77 895 (doto (Quaternion.)
rlm@77 896 (.fromAngleAxis (/ Math/PI 2)
rlm@77 897 Vector3f/UNIT_X)))
rlm@77 898 x-move
rlm@77 899 (doto (Matrix3f.)
rlm@77 900 (.fromStartEndVectors Vector3f/UNIT_X
rlm@77 901 (.normalize (Vector3f. 1 1 0))))
rlm@77 902
rlm@77 903 timer (atom 0)]
rlm@77 904 (doto
rlm@77 905 (ConeJoint.
rlm@77 906 control-a control-b
rlm@77 907 (Vector3f. -8 0 0)
rlm@77 908 (Vector3f. 2 0 0)
rlm@77 909 ;;swivel swivel
rlm@77 910 ;;Matrix3f/IDENTITY Matrix3f/IDENTITY
rlm@77 911 x-move Matrix3f/IDENTITY
rlm@77 912 )
rlm@77 913 (.setCollisionBetweenLinkedBodys false)
rlm@77 914 (.setLimit (* 1 (/ Math/PI 4)) ;; twist
rlm@77 915 (* 1 (/ Math/PI 4)) ;; swing span in X-Y plane
rlm@77 916 (* 0 (/ Math/PI 4)))) ;; swing span in Y-Z plane
rlm@77 917 (world (nodify
rlm@77 918 [obj-a obj-b])
rlm@77 919 (merge standard-debug-controls
rlm@77 920 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@77 921 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@77 922 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@77 923 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@77 924 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@77 925 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@77 926
rlm@77 927 (fn [world]
rlm@77 928 (enable-debug world)
rlm@77 929 (set-gravity world Vector3f/ZERO)
rlm@77 930 )
rlm@77 931
rlm@77 932 (fn [world _]
rlm@77 933
rlm@77 934 (if @move-up?
rlm@77 935 (.applyForce control-a
rlm@77 936 (Vector3f. force 0 0)
rlm@77 937 (Vector3f. 0 0 0)))
rlm@77 938 (if @move-down?
rlm@77 939 (.applyForce control-a
rlm@77 940 (Vector3f. (- force) 0 0)
rlm@77 941 (Vector3f. 0 0 0)))
rlm@77 942 (if @move-left?
rlm@77 943 (.applyForce control-a
rlm@77 944 (Vector3f. 0 force 0)
rlm@77 945 (Vector3f. 0 0 0)))
rlm@77 946 (if @move-right?
rlm@77 947 (.applyForce control-a
rlm@77 948 (Vector3f. 0 (- force) 0)
rlm@77 949 (Vector3f. 0 0 0)))
rlm@77 950
rlm@77 951 (if @roll-left?
rlm@77 952 (.applyForce control-a
rlm@77 953 (Vector3f. 0 0 force)
rlm@77 954 (Vector3f. 0 0 0)))
rlm@77 955 (if @roll-right?
rlm@77 956 (.applyForce control-a
rlm@77 957 (Vector3f. 0 0 (- force))
rlm@77 958 (Vector3f. 0 0 0)))
rlm@77 959
rlm@77 960 (if (zero? (rem (swap! timer inc) 100))
rlm@77 961 (.attachChild
rlm@77 962 (.getRootNode world)
rlm@77 963 (sphere 0.05 :color ColorRGBA/Yellow
rlm@77 964 :physical? false :position
rlm@77 965 (.getWorldTranslation obj-a)))))
rlm@77 966 )
rlm@77 967 ))
rlm@77 968
rlm@94 969 (defn transform-trianglesdsd
rlm@94 970 "Transform that converts each vertex in the first triangle
rlm@94 971 into the corresponding vertex in the second triangle."
rlm@94 972 [#^Triangle tri-1 #^Triangle tri-2]
rlm@94 973 (let [in [(.get1 tri-1)
rlm@94 974 (.get2 tri-1)
rlm@94 975 (.get3 tri-1)]
rlm@94 976 out [(.get1 tri-2)
rlm@94 977 (.get2 tri-2)
rlm@94 978 (.get3 tri-2)]]
rlm@94 979 (let [translate (doto (Matrix4f.) (.setTranslation (.negate (in 0))))
rlm@94 980 in* [(.mult translate (in 0))
rlm@94 981 (.mult translate (in 1))
rlm@94 982 (.mult translate (in 2))]
rlm@94 983 final-translation
rlm@94 984 (doto (Matrix4f.)
rlm@94 985 (.setTranslation (out 1)))
rlm@94 986
rlm@94 987 rotate-1
rlm@94 988 (doto (Matrix3f.)
rlm@94 989 (.fromStartEndVectors
rlm@94 990 (.normalize
rlm@94 991 (.subtract
rlm@94 992 (in* 1) (in* 0)))
rlm@94 993 (.normalize
rlm@94 994 (.subtract
rlm@94 995 (out 1) (out 0)))))
rlm@94 996 in** [(.mult rotate-1 (in* 0))
rlm@94 997 (.mult rotate-1 (in* 1))
rlm@94 998 (.mult rotate-1 (in* 2))]
rlm@94 999 scale-factor-1
rlm@94 1000 (.mult
rlm@94 1001 (.normalize
rlm@94 1002 (.subtract
rlm@94 1003 (out 1)
rlm@94 1004 (out 0)))
rlm@94 1005 (/ (.length
rlm@94 1006 (.subtract (out 1)
rlm@94 1007 (out 0)))
rlm@94 1008 (.length
rlm@94 1009 (.subtract (in** 1)
rlm@94 1010 (in** 0)))))
rlm@94 1011 scale-1 (doto (Matrix4f.) (.setScale scale-factor-1))
rlm@94 1012 in*** [(.mult scale-1 (in** 0))
rlm@94 1013 (.mult scale-1 (in** 1))
rlm@94 1014 (.mult scale-1 (in** 2))]
rlm@94 1015
rlm@94 1016
rlm@94 1017
rlm@94 1018
rlm@94 1019
rlm@94 1020 ]
rlm@94 1021
rlm@94 1022 (dorun (map println in))
rlm@94 1023 (println)
rlm@94 1024 (dorun (map println in*))
rlm@94 1025 (println)
rlm@94 1026 (dorun (map println in**))
rlm@94 1027 (println)
rlm@94 1028 (dorun (map println in***))
rlm@94 1029 (println)
rlm@94 1030
rlm@99 1031 ))))
rlm@94 1032
rlm@94 1033
rlm@106 1034 (defn world-setup [joint]
rlm@106 1035 (let [joint-position (Vector3f. 0 0 0)
rlm@106 1036 joint-rotation
rlm@106 1037 (.toRotationMatrix
rlm@106 1038 (.mult
rlm@106 1039 (doto (Quaternion.)
rlm@106 1040 (.fromAngleAxis
rlm@106 1041 (* 1 (/ Math/PI 4))
rlm@106 1042 (Vector3f. -1 0 0)))
rlm@106 1043 (doto (Quaternion.)
rlm@106 1044 (.fromAngleAxis
rlm@106 1045 (* 1 (/ Math/PI 2))
rlm@106 1046 (Vector3f. 0 0 1)))))
rlm@106 1047 top-position (.mult joint-rotation (Vector3f. 8 0 0))
rlm@106 1048
rlm@106 1049 origin (doto
rlm@106 1050 (sphere 0.1 :physical? false :color ColorRGBA/Cyan
rlm@106 1051 :position top-position))
rlm@106 1052 top (doto
rlm@106 1053 (sphere 0.1 :physical? false :color ColorRGBA/Yellow
rlm@106 1054 :position top-position)
rlm@106 1055
rlm@106 1056 (.addControl
rlm@106 1057 (RigidBodyControl.
rlm@106 1058 (CapsuleCollisionShape. 0.5 1.5 1) (float 20))))
rlm@106 1059 bottom (doto
rlm@106 1060 (sphere 0.1 :physical? false :color ColorRGBA/DarkGray
rlm@106 1061 :position (Vector3f. 0 0 0))
rlm@106 1062 (.addControl
rlm@106 1063 (RigidBodyControl.
rlm@106 1064 (CapsuleCollisionShape. 0.5 1.5 1) (float 0))))
rlm@106 1065 table (box 10 2 10 :position (Vector3f. 0 -20 0)
rlm@106 1066 :color ColorRGBA/Gray :mass 0)
rlm@106 1067 a (.getControl top RigidBodyControl)
rlm@106 1068 b (.getControl bottom RigidBodyControl)]
rlm@106 1069
rlm@106 1070 (cond
rlm@106 1071 (= joint :cone)
rlm@106 1072
rlm@106 1073 (doto (ConeJoint.
rlm@106 1074 a b
rlm@106 1075 (world-to-local top joint-position)
rlm@106 1076 (world-to-local bottom joint-position)
rlm@106 1077 joint-rotation
rlm@106 1078 joint-rotation
rlm@106 1079 )
rlm@106 1080
rlm@106 1081
rlm@106 1082 (.setLimit (* (/ 10) Math/PI)
rlm@106 1083 (* (/ 4) Math/PI)
rlm@106 1084 0)))
rlm@106 1085 [origin top bottom table]))
rlm@106 1086
rlm@106 1087 (defn test-joint [joint]
rlm@106 1088 (let [[origin top bottom floor] (world-setup joint)
rlm@106 1089 control (.getControl top RigidBodyControl)
rlm@106 1090 move-up? (atom false)
rlm@106 1091 move-down? (atom false)
rlm@106 1092 move-left? (atom false)
rlm@106 1093 move-right? (atom false)
rlm@106 1094 roll-left? (atom false)
rlm@106 1095 roll-right? (atom false)
rlm@106 1096 timer (atom 0)]
rlm@106 1097
rlm@106 1098 (world
rlm@106 1099 (nodify [top bottom floor origin])
rlm@106 1100 (merge standard-debug-controls
rlm@106 1101 {"key-r" (fn [_ pressed?] (reset! move-up? pressed?))
rlm@106 1102 "key-t" (fn [_ pressed?] (reset! move-down? pressed?))
rlm@106 1103 "key-f" (fn [_ pressed?] (reset! move-left? pressed?))
rlm@106 1104 "key-g" (fn [_ pressed?] (reset! move-right? pressed?))
rlm@106 1105 "key-v" (fn [_ pressed?] (reset! roll-left? pressed?))
rlm@106 1106 "key-b" (fn [_ pressed?] (reset! roll-right? pressed?))})
rlm@106 1107
rlm@106 1108 (fn [world]
rlm@106 1109 (light-up-everything world)
rlm@106 1110 (enable-debug world)
rlm@106 1111 (set-gravity world (Vector3f. 0 0 0))
rlm@106 1112 )
rlm@106 1113
rlm@106 1114 (fn [world _]
rlm@106 1115 (if (zero? (rem (swap! timer inc) 100))
rlm@106 1116 (do
rlm@106 1117 ;; (println-repl @timer)
rlm@106 1118 (.attachChild (.getRootNode world)
rlm@106 1119 (sphere 0.05 :color ColorRGBA/Yellow
rlm@106 1120 :position (.getWorldTranslation top)
rlm@106 1121 :physical? false))
rlm@106 1122 (.attachChild (.getRootNode world)
rlm@106 1123 (sphere 0.05 :color ColorRGBA/LightGray
rlm@106 1124 :position (.getWorldTranslation bottom)
rlm@106 1125 :physical? false))))
rlm@106 1126
rlm@106 1127 (if @move-up?
rlm@106 1128 (.applyTorque control
rlm@106 1129 (.mult (.getPhysicsRotation control)
rlm@106 1130 (Vector3f. 0 0 10))))
rlm@106 1131 (if @move-down?
rlm@106 1132 (.applyTorque control
rlm@106 1133 (.mult (.getPhysicsRotation control)
rlm@106 1134 (Vector3f. 0 0 -10))))
rlm@106 1135 (if @move-left?
rlm@106 1136 (.applyTorque control
rlm@106 1137 (.mult (.getPhysicsRotation control)
rlm@106 1138 (Vector3f. 0 10 0))))
rlm@106 1139 (if @move-right?
rlm@106 1140 (.applyTorque control
rlm@106 1141 (.mult (.getPhysicsRotation control)
rlm@106 1142 (Vector3f. 0 -10 0))))
rlm@106 1143 (if @roll-left?
rlm@106 1144 (.applyTorque control
rlm@106 1145 (.mult (.getPhysicsRotation control)
rlm@106 1146 (Vector3f. -1 0 0))))
rlm@106 1147 (if @roll-right?
rlm@106 1148 (.applyTorque control
rlm@106 1149 (.mult (.getPhysicsRotation control)
rlm@106 1150 (Vector3f. 1 0 0))))))))
rlm@106 1151
rlm@99 1152
rlm@99 1153
rlm@107 1154 (defprotocol Frame
rlm@107 1155 (frame [this]))
rlm@107 1156
rlm@107 1157 (extend-type BufferedImage
rlm@107 1158 Frame
rlm@107 1159 (frame [image]
rlm@107 1160 (merge
rlm@107 1161 (apply
rlm@107 1162 hash-map
rlm@107 1163 (interleave
rlm@107 1164 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1165 (vector x y)))
rlm@107 1166 (doall (for [x (range (.getWidth image)) y (range (.getHeight image))]
rlm@107 1167 (let [data (.getRGB image x y)]
rlm@107 1168 (hash-map :r (bit-shift-right (bit-and 0xff0000 data) 16)
rlm@107 1169 :g (bit-shift-right (bit-and 0x00ff00 data) 8)
rlm@107 1170 :b (bit-and 0x0000ff data)))))))
rlm@107 1171 {:width (.getWidth image) :height (.getHeight image)})))
rlm@107 1172
rlm@107 1173
rlm@107 1174 (extend-type ImagePlus
rlm@107 1175 Frame
rlm@107 1176 (frame [image+]
rlm@107 1177 (frame (.getBufferedImage image+))))
rlm@107 1178
rlm@107 1179
rlm@99 1180 #+end_src
rlm@99 1181
rlm@99 1182
rlm@99 1183 * COMMENT generate source
rlm@99 1184 #+begin_src clojure :tangle ../src/cortex/silly.clj
rlm@99 1185 <<body-1>>
rlm@99 1186 #+end_src
rlm@99 1187
rlm@99 1188
rlm@94 1189
rlm@94 1190