annotate org/test-creature.org @ 153:c95179907951

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