annotate org/test-creature.org @ 117:94c005f7f9dd

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